From 52aeb39530166219aac5eb909abaa741258e8c74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mathieu?= Date: Wed, 18 Oct 2023 18:08:40 +0200 Subject: [PATCH] Add auth mechanism to the Liquibase MongoDB connection string --- docs/src/main/asciidoc/liquibase-mongodb.adoc | 2 ++ .../liquibase/mongodb/LiquibaseMongodbFactory.java | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/docs/src/main/asciidoc/liquibase-mongodb.adoc b/docs/src/main/asciidoc/liquibase-mongodb.adoc index 3660175cc0f30..6739353f25f66 100644 --- a/docs/src/main/asciidoc/liquibase-mongodb.adoc +++ b/docs/src/main/asciidoc/liquibase-mongodb.adoc @@ -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. diff --git a/extensions/liquibase-mongodb/runtime/src/main/java/io/quarkus/liquibase/mongodb/LiquibaseMongodbFactory.java b/extensions/liquibase-mongodb/runtime/src/main/java/io/quarkus/liquibase/mongodb/LiquibaseMongodbFactory.java index e98532a448bae..5a0d818fbcc35 100644 --- a/extensions/liquibase-mongodb/runtime/src/main/java/io/quarkus/liquibase/mongodb/LiquibaseMongodbFactory.java +++ b/extensions/liquibase-mongodb/runtime/src/main/java/io/quarkus/liquibase/mongodb/LiquibaseMongodbFactory.java @@ -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( @@ -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),