示例#1
0
        public async Task Should_be_able_to_get_an_individual_exchange_by_name()
        {
            var vhost = new Vhost {
                Name = vhostName
            };
            var exchange = await managementClient.GetExchangeAsync(testExchange, vhost).ConfigureAwait(false);

            exchange.Name.ShouldEqual(testExchange);
        }
        /// <summary>
        ///     Get an individual exchange by name
        /// </summary>
        /// <param name="source"></param>
        /// <param name="exchangeName">The name of the exchange</param>
        /// <param name="vhost">The virtual host that contains the exchange</param>
        /// <param name="ratesCriteria">Criteria for getting samples of rate data</param>
        /// <param name="cancellationToken"></param>
        /// <returns>The exchange</returns>
        public static Exchange GetExchange(
            [NotNull] this IManagementClient source,
            string exchangeName,
            [NotNull] Vhost vhost,
            GetRatesCriteria ratesCriteria      = null,
            CancellationToken cancellationToken = default
            )
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            return(source.GetExchangeAsync(exchangeName, vhost, ratesCriteria, cancellationToken)
                   .GetAwaiter()
                   .GetResult());
        }
示例#3
0
        public async Task <bool> CreateUserAsync(string username, string password)
        {
            var vhost = await _managementClient.GetVhostAsync("/");

            if ((await _managementClient.GetUsersAsync()).Any(u => string.Equals(u.Name, username, StringComparison.InvariantCultureIgnoreCase)))
            {
                _logger.LogInformation($"User '{username}' already exists");
                return(false);
            }

            var userInfo   = new UserInfo(username, password);
            var rabbitUser = await _managementClient.CreateUserAsync(userInfo);

            _logger.LogInformation($"RabbitMQ user '{username}' created");

            var logExchangeName = $"logs.{username}";
            var exchangeInfo    = new ExchangeInfo(logExchangeName, ExchangeType.Fanout);
            await _managementClient.CreateExchangeAsync(exchangeInfo, vhost);

            _logger.LogInformation($"RabbitMQ exchange for user '{username}' created");

            var logQueueName = (await _bus.Advanced.QueueDeclareAsync("", maxLength: 1, maxLengthBytes: 4096)).Name;

            var logExchange = await _managementClient.GetExchangeAsync(logExchangeName, vhost);

            var logQueue = await _managementClient.GetQueueAsync(logQueueName, vhost);

            await _managementClient.CreateBindingAsync(logExchange, logQueue, new BindingInfo(""));

            var permissionInfo = new PermissionInfo(rabbitUser, vhost)
                                 .DenyAllConfigure()
                                 .SetRead("^amq\\.")
                                 .SetWrite("^logs\\.");
            await _managementClient.CreatePermissionAsync(permissionInfo);

            _logger.LogInformation($"RabbitMQ permissions for user '{username}' set");

            var errorExchange = await _managementClient.GetExchangeAsync("errors", vhost);

            var errorQueueName = (await _bus.Advanced.QueueDeclareAsync("")).Name;

            _logger.LogInformation($"RabbitMQ error queue for user '{username}' created");

            var errorQueue = await _managementClient.GetQueueAsync(errorQueueName, vhost);

            await _managementClient.CreateBindingAsync(errorExchange, errorQueue, new BindingInfo(username));

            _logger.LogInformation($"RabbitMQ error queue for user '{username}' bound to error exchange");

            await _elasticsearchClient.CreateUserAsync(username, password);

            _logger.LogInformation($"ElasticSearch user '{username}' created");

            await _elasticsearchClient.CreateIndexAsync(username);

            _logger.LogInformation($"ElasticSearch index for '{username}' created");

            var salt         = CryptoUtils.GenerateSalt();
            var passwordHash = CryptoUtils.ComputeHash(salt, password);

            var user = new Deer.Models.User
            {
                Username        = username,
                Salt            = salt,
                PasswordHash    = passwordHash,
                LogIndexName    = username,
                LogExchangeName = logExchangeName,
                LogQueueName    = logQueueName,
                ErrorQueueName  = errorQueueName
            };
            await _userRepository.CreateAsync(user);

            _logger.LogInformation($"UserInfo for user '{username}' saved to MongoDB");

            await _logConsumerService.AddConsumerForUserAsync(username);

            _logger.LogInformation($"Added log consumer for user '{username}'");

            return(true);
        }