Skip to content

Commit

Permalink
Add auth mechanism to the Liquibase MongoDB connection string
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu committed Oct 19, 2023
1 parent 89ed09e commit 52aeb39
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 match 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 @@ -37,6 +37,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 @@ -51,6 +54,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 52aeb39

Please sign in to comment.