Skip to content

Commit

Permalink
Merge pull request #36561 from loicmathieu/liquibase-mongodb-auth-mec…
Browse files Browse the repository at this point in the history
…hanism

Add auth mechanism to the Liquibase MongoDB connection string
  • Loading branch information
gsmet authored Oct 24, 2023
2 parents 53cd58f + 4f5eca2 commit 10558f1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/src/main/asciidoc/liquibase-mongodb.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ quarkus.liquibase-mongodb.migrate-at-start=true
# quarkus.liquibase-mongodb.default-schema-name=DefaultSchema
----

NOTE: Liquibase MongoDB is configured using a connection string, we do our best to craft a connection string that matches the MongoDB client configuration but if some configuration properties are not working you may consider adding them directly into the `quarkus.mongodb.connection-string` config property.

Add a changeLog file to the default folder following the Liquibase naming conventions: `{change-log}`
YAML, JSON and XML formats are supported for the changeLog.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public Liquibase createLiquibase() {
Thread.currentThread().getContextClassLoader())) {
String connectionString = this.mongoClientConfig.connectionString.orElse("mongodb://localhost:27017");

// Every MongoDB client configuration must be added to the connection string, we didn't add all as it would be too much to support.
// For reference, all connections string options can be found here: https://www.mongodb.com/docs/manual/reference/connection-string/#connection-string-options.

Matcher matcher = HAS_DB.matcher(connectionString);
if (!matcher.matches() || matcher.group("db") == null || matcher.group("db").isEmpty()) {
connectionString = matcher.replaceFirst(
Expand All @@ -50,6 +53,17 @@ public Liquibase createLiquibase() {
connectionString += (alreadyHasQueryParams ? "&" : "?") + "authSource="
+ mongoClientConfig.credentials.authSource.get();
}
if (mongoClientConfig.credentials.authMechanism.isPresent()) {
boolean alreadyHasQueryParams = connectionString.contains("?");
connectionString += (alreadyHasQueryParams ? "&" : "?") + "authMechanism="
+ mongoClientConfig.credentials.authMechanism.get();
}
if (!mongoClientConfig.credentials.authMechanismProperties.isEmpty()) {
boolean alreadyHasQueryParams = connectionString.contains("?");
connectionString += (alreadyHasQueryParams ? "&" : "?") + "authMechanismProperties="
+ mongoClientConfig.credentials.authMechanismProperties.entrySet().stream()
.map(prop -> prop.getKey() + ":" + prop.getValue()).collect(Collectors.joining(","));
}

Database database = DatabaseFactory.getInstance().openDatabase(connectionString,
this.mongoClientConfig.credentials.username.orElse(null),
Expand Down

0 comments on commit 10558f1

Please sign in to comment.