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 authored and gsmet committed Oct 24, 2023
1 parent 89ed09e commit 4f5eca2
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.

Check warning on line 84 in docs/src/main/asciidoc/liquibase-mongodb.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.TermsSuggestions] Depending on the context, consider using 'by using' or 'that uses' rather than 'using'. Raw Output: {"message": "[Quarkus.TermsSuggestions] Depending on the context, consider using 'by using' or 'that uses' rather than 'using'.", "location": {"path": "docs/src/main/asciidoc/liquibase-mongodb.adoc", "range": {"start": {"line": 84, "column": 27}}}, "severity": "INFO"}

Check warning on line 84 in docs/src/main/asciidoc/liquibase-mongodb.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.TermsWarnings] Consider using 'might (for possiblity)' or 'can (for ability)' rather than 'may' unless updating existing content that uses the term. Raw Output: {"message": "[Quarkus.TermsWarnings] Consider using 'might (for possiblity)' or 'can (for ability)' rather than 'may' unless updating existing content that uses the term.", "location": {"path": "docs/src/main/asciidoc/liquibase-mongodb.adoc", "range": {"start": {"line": 84, "column": 202}}}, "severity": "WARNING"}

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 4f5eca2

Please sign in to comment.