public async Task Should_be_able_to_get_a_user_by_name() { var userInfo = new UserInfo(testUser, "topSecret"); await managementClient.CreateUserAsync(userInfo).ConfigureAwait(false); (await managementClient.GetUserAsync(testUser).ConfigureAwait(false)).Name.ShouldEqual(testUser); }
public async Task Should_be_able_to_change_the_password_of_a_user() { var userInfo = new UserInfo(testUser, "topSecret").AddTag("monitoring").AddTag("management"); var user = await managementClient.CreateUserAsync(userInfo).ConfigureAwait(false); var updatedUser = await managementClient.ChangeUserPasswordAsync(testUser, "newPassword") .ConfigureAwait(false); updatedUser.Name.Should().Be(user.Name); updatedUser.Tags.Should().Be(user.Tags); updatedUser.PasswordHash.Should().NotBe(user.PasswordHash); }
/// <summary> /// Create a new user /// </summary> /// <param name="source"></param> /// <param name="userInfo">The user to create</param> /// <param name="cancellationToken"></param> public static User CreateUser( [NotNull] this IManagementClient source, [NotNull] UserInfo userInfo, CancellationToken cancellationToken = default ) { if (source == null) { throw new ArgumentNullException(nameof(source)); } return(source.CreateUserAsync(userInfo, cancellationToken) .GetAwaiter() .GetResult()); }
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); }