public KeyVaultClient(ConnectionParams connection, CredentialParams credential)
        {
            _keyVault = connection.GetAsNullableString("key_vault")
                        ?? connection.GetAsNullableString("uri")
                        ?? connection.GetAsNullableString("KeyVault");
            if (_keyVault == null)
            {
                throw new ArgumentNullException("KeyVault parameter is not defined");
            }
            if (!_keyVault.StartsWith("http"))
            {
                _keyVault = "https://" + _keyVault + ".vault.azure.net";
            }

            _clientId = credential.AccessId ?? credential.GetAsNullableString("ClientId");
            if (_clientId == null)
            {
                throw new ArgumentNullException("CliendId parameter is not defined");
            }

            _clientKey  = credential.AccessKey ?? credential.GetAsNullableString("ClientKey");
            _thumbPrint = credential.GetAsNullableString("thumbprint")
                          ?? credential.GetAsNullableString("ThumbPrint");
            if (_clientKey == null && _thumbPrint == null)
            {
                throw new ArgumentNullException("Neither ClientKey or ThumbPrint parameters are not defined");
            }

            _client = new Microsoft.Azure.KeyVault.KeyVaultClient(
                new Microsoft.Azure.KeyVault.KeyVaultClient.AuthenticationCallback(GetAccessToken));
        }
Esempio n. 2
0
        private void ValidateConnection(string correlationId, ConnectionParams connection)
        {
            var uri = connection.Uri;

            if (uri != null)
            {
                return;
            }

            var host = connection.Host;

            if (host == null)
            {
                throw new ConfigException(correlationId, "NO_HOST", "Connection host is not set");
            }

            var port = connection.Port;

            if (port == 0)
            {
                throw new ConfigException(correlationId, "NO_PORT", "Connection port is not set");
            }

            var database = connection.GetAsNullableString("database");

            if (database == null)
            {
                throw new ConfigException(correlationId, "NO_DATABASE", "Connection database is not set");
            }
        }
        public async override Task OpenAsync(string correlationId, ConnectionParams connection, CredentialParams credential)
        {
            try
            {
                var connectionString = ConfigParams.FromTuples(
                    "DefaultEndpointsProtocol", connection.Protocol ?? connection.GetAsNullableString("DefaultEndpointsProtocol") ?? "https",
                    "AccountName", credential.AccessId ?? credential.GetAsNullableString("account_name") ?? credential.GetAsNullableString("AccountName"),
                    "AccountKey", credential.AccessKey ?? credential.GetAsNullableString("account_key") ?? credential.GetAsNullableString("AccountKey")
                    ).ToString();

                _logger.Info(null, "Connecting queue {0} to {1}", Name, connectionString);

                var storageAccount = CloudStorageAccount.Parse(connectionString);
                var client         = storageAccount.CreateCloudQueueClient();

                var queueName = connection.Get("queue") ?? Name;
                _queue = client.GetQueueReference(queueName);
                await _queue.CreateIfNotExistsAsync();

                var deadName = connection.Get("dead");
                _deadQueue = deadName != null?client.GetQueueReference(deadName) : null;
            }
            catch (Exception ex)
            {
                _queue = null;
                _logger.Error(correlationId, ex, $"Failed to open queue {Name}.");
            }

            await Task.Delay(0);
        }
        public async Task OpenAsync(string correlationId, ConnectionParams connection, CredentialParams credential)
        {
            if (connection == null)
            {
                throw new ConfigException(correlationId, "NO_CONNECTION", "Database connection is not set");
            }

            var uri          = connection.Uri;
            var host         = connection.Host;
            var port         = connection.Port;
            var databaseName = connection.GetAsNullableString("database");

            if (uri != null)
            {
                databaseName = MongoUrl.Create(uri).DatabaseName;
            }
            else
            {
                if (host == null)
                {
                    throw new ConfigException(correlationId, "NO_HOST", "Connection host is not set");
                }

                if (port == 0)
                {
                    throw new ConfigException(correlationId, "NO_PORT", "Connection port is not set");
                }

                if (databaseName == null)
                {
                    throw new ConfigException(correlationId, "NO_DATABASE", "Connection database is not set");
                }
            }

            _logger.Trace(correlationId, "Connecting to mongodb database {0}, collection {1}", databaseName, _collectionName);

            try
            {
                if (uri != null)
                {
                    _connection = new MongoClient(uri);
                }
                else
                {
                    var settings = new MongoClientSettings
                    {
                        Server = new MongoServerAddress(host, port),
                        MaxConnectionPoolSize = _options.GetAsInteger("poll_size"),
                        ConnectTimeout        = _options.GetAsTimeSpan("connect_timeout"),
                        //SocketTimeout =
                        //    new TimeSpan(options.GetInteger("server.socketOptions.socketTimeoutMS")*
                        //                 TimeSpan.TicksPerMillisecond)
                    };

                    if (credential.Username != null)
                    {
                        settings.Credential = MongoCredential.CreateCredential(databaseName, credential.Username, credential.Password);
                    }

                    _connection = new MongoClient(settings);
                }

                _database   = _connection.GetDatabase(databaseName);
                _collection = _database.GetCollection <T>(_collectionName);

                _logger.Debug(correlationId, "Connected to mongodb database {0}, collection {1}", databaseName, _collectionName);
            }
            catch (Exception ex)
            {
                throw new ConnectionException(correlationId, "ConnectFailed", "Connection to mongodb failed", ex);
            }

            await Task.Delay(0);
        }