diff --git a/src/main/java/com/knecon/fforesight/tenantusermanagement/service/MongoDbOntheFlyMigrationService.java b/src/main/java/com/knecon/fforesight/tenantusermanagement/service/MongoDbOntheFlyMigrationService.java index 56e362a..9f6f0f3 100644 --- a/src/main/java/com/knecon/fforesight/tenantusermanagement/service/MongoDbOntheFlyMigrationService.java +++ b/src/main/java/com/knecon/fforesight/tenantusermanagement/service/MongoDbOntheFlyMigrationService.java @@ -20,6 +20,7 @@ import com.mongodb.client.MongoClients; import com.mongodb.client.MongoDatabase; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; // This is just for migration from 4.0 to 4.1 and can be removed afterward. @@ -50,7 +51,7 @@ public class MongoDbOntheFlyMigrationService { @Transactional public TenantEntity updateMongoDatabaseConnection(TenantEntity tenantEntity) { - if ((tenantEntity.getMongoDBConnection() == null || tenantEntity.getMongoDBConnection().getUsername() == null) && defaultMongoDBUsername != null) { + if ((tenantEntity.getMongoDBConnection() == null || tenantEntity.getMongoDBConnection().getUsername() == null) && defaultMongoDBUsername != null && !defaultMongoDBUsername.isEmpty()) { tenantEntity.setMongoDBConnection(MongoDBConnectionEntity.builder() .prefix(defaultMongoDBPrefix) .username(defaultMongoDBUsername) @@ -62,6 +63,7 @@ public class MongoDbOntheFlyMigrationService { tenantRepository.save(tenantEntity); createMongoDBDatabase(convert(tenantEntity.getMongoDBConnection())); } + return tenantEntity; } @@ -87,14 +89,16 @@ public class MongoDbOntheFlyMigrationService { private void createMongoDBDatabase(MongoDBConnection mongoDBConnection) { + mongoDBConnection.setPassword(encryptionService.decrypt(mongoDBConnection.getPassword())); + try (MongoClient mongoClient = MongoClients.create(MongoConnectionStringHelper.buildGenericMongoConnectionString(mongoDBConnection))) { String databaseName = mongoDBConnection.getDatabase(); String username = mongoDBConnection.getUsername(); - String password = defaultMongoDBPassword; + MongoDatabase database = mongoClient.getDatabase(databaseName); BsonDocument createUserCommand = new BsonDocument(); createUserCommand.append("createUser", new BsonString(username)); - createUserCommand.append("pwd", new BsonString(password)); + createUserCommand.append("pwd", new BsonString(mongoDBConnection.getPassword())); BsonArray roles = new BsonArray(); roles.add(new BsonDocument("role", new BsonString("dbOwner")).append("db", new BsonString(databaseName))); createUserCommand.append("roles", roles);