This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27218 in repository https://gitbox.apache.org/repos/asf/camel.git
commit aa8cc27ca735456bb89aaadcba67209062a5b922 Author: Claus Ibsen <[email protected]> AuthorDate: Thu Nov 19 07:41:11 2026 +0100 CAMEL-27218: camel-mongodb - the tls and tlsAllowInvalidHostnames options are applied to the client created from hosts or connectionUriString Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../apache/camel/catalog/components/mongodb.json | 2 +- .../apache/camel/component/mongodb/mongodb.json | 2 +- .../camel/component/mongodb/MongoDbEndpoint.java | 75 +++++++++----- .../component/mongodb/MongoDbEndpointTlsTest.java | 114 +++++++++++++++++++++ .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 9 ++ .../dsl/MongoDbEndpointBuilderFactory.java | 18 ++-- 6 files changed, 184 insertions(+), 36 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/mongodb.json b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/mongodb.json index 14621bc09bfc..802ed5605b89 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/mongodb.json +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/mongodb.json @@ -120,7 +120,7 @@ "authSource": { "index": 53, "kind": "parameter", "displayName": "Auth Source", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "The database name associated with the user's credentials." }, "password": { "index": 54, "kind": "parameter", "displayName": "Password", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": true, "security": "secret", "description": "User password for mongodb connection" }, "sslContextParameters": { "index": 55, "kind": "parameter", "displayName": "Ssl Context Parameters", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "org.apache.camel.support.jsse.SSLContextParameters", "deprecated": false, "autowired": false, "secret": false, "description": "SSL configuration using a Camel SSLContextParameters object. When configured, TLS is automatically enabled on the connection." }, - "tls": { "index": 56, "kind": "parameter", "displayName": "Tls", "group": "security", "label": "security", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Specifies that all communication with MongoDB instances should use TLS. Supersedes the ssl option. Default: false" }, + "tls": { "index": 56, "kind": "parameter", "displayName": "Tls", "group": "security", "label": "security", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Specifies that all communication with MongoDB instances should use TLS. Supersedes the ssl option. Applies to the client the endpoint creates from hosts or connectionUriString. Default: false" }, "tlsAllowInvalidHostnames": { "index": 57, "kind": "parameter", "displayName": "Tls Allow Invalid Hostnames", "group": "security", "label": "security", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "security": "insecure:ssl", "defaultValue": false, "description": "Specifies that the driver should allow invalid hostnames in the certificate for TLS connections. Supersedes sslInvalidHostNameAllowed. Has the same ef [...] "username": { "index": 58, "kind": "parameter", "displayName": "Username", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": true, "security": "secret", "description": "Username for mongodb connection" } }, diff --git a/components/camel-mongodb/src/generated/resources/META-INF/org/apache/camel/component/mongodb/mongodb.json b/components/camel-mongodb/src/generated/resources/META-INF/org/apache/camel/component/mongodb/mongodb.json index 14621bc09bfc..802ed5605b89 100644 --- a/components/camel-mongodb/src/generated/resources/META-INF/org/apache/camel/component/mongodb/mongodb.json +++ b/components/camel-mongodb/src/generated/resources/META-INF/org/apache/camel/component/mongodb/mongodb.json @@ -120,7 +120,7 @@ "authSource": { "index": 53, "kind": "parameter", "displayName": "Auth Source", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "The database name associated with the user's credentials." }, "password": { "index": 54, "kind": "parameter", "displayName": "Password", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": true, "security": "secret", "description": "User password for mongodb connection" }, "sslContextParameters": { "index": 55, "kind": "parameter", "displayName": "Ssl Context Parameters", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "org.apache.camel.support.jsse.SSLContextParameters", "deprecated": false, "autowired": false, "secret": false, "description": "SSL configuration using a Camel SSLContextParameters object. When configured, TLS is automatically enabled on the connection." }, - "tls": { "index": 56, "kind": "parameter", "displayName": "Tls", "group": "security", "label": "security", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Specifies that all communication with MongoDB instances should use TLS. Supersedes the ssl option. Default: false" }, + "tls": { "index": 56, "kind": "parameter", "displayName": "Tls", "group": "security", "label": "security", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Specifies that all communication with MongoDB instances should use TLS. Supersedes the ssl option. Applies to the client the endpoint creates from hosts or connectionUriString. Default: false" }, "tlsAllowInvalidHostnames": { "index": 57, "kind": "parameter", "displayName": "Tls Allow Invalid Hostnames", "group": "security", "label": "security", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "security": "insecure:ssl", "defaultValue": false, "description": "Specifies that the driver should allow invalid hostnames in the certificate for TLS connections. Supersedes sslInvalidHostNameAllowed. Has the same ef [...] "username": { "index": 58, "kind": "parameter", "displayName": "Username", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": true, "security": "secret", "description": "Username for mongodb connection" } }, diff --git a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbEndpoint.java b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbEndpoint.java index 52f599ed3b4c..3fd8daa1ed16 100644 --- a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbEndpoint.java +++ b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbEndpoint.java @@ -531,20 +531,7 @@ public class MongoDbEndpoint extends DefaultEndpoint implements EndpointServiceL private MongoClient resolveMongoConnection() { MongoClient mongoClient; if (this.hosts != null || this.connectionUriString != null) { - String credentials = username == null ? "" : username; - if (!credentials.isEmpty()) { - credentials += this.password == null ? "@" : ":" + password + "@"; - } - String connectionOptions = authSource == null ? "" : "/?authSource=" + authSource; - String connectionUri = connectionUriString != null - ? connectionUriString - : String.format("mongodb://%s%s%s", credentials, hosts, connectionOptions); - - if (sslContextParameters != null) { - mongoClient = createMongoClientWithSslContext(connectionUri); - } else { - mongoClient = MongoClients.create(connectionUri); - } + mongoClient = MongoClients.create(createMongoClientSettings()); // doStart() and initializeConnection() both get their client here, so doStop() closes it whichever ran first createdMongoConnection = mongoClient; LOG.debug("Connection created using provided credentials"); @@ -560,20 +547,51 @@ public class MongoDbEndpoint extends DefaultEndpoint implements EndpointServiceL return mongoClient; } - private MongoClient createMongoClientWithSslContext(String connectionUri) { + /** + * The settings of the client this endpoint creates from hosts or connectionUriString. The TLS options of the + * endpoint (tls, tlsAllowInvalidHostnames and sslContextParameters) are applied on top of the connection string, so + * they are used the same way whether the endpoint has hosts or a connectionUriString. When none of them is set, the + * TLS settings of the connection string are kept. + */ + MongoClientSettings createMongoClientSettings() { + String connectionUri; + if (connectionUriString != null) { + connectionUri = connectionUriString; + } else { + String credentials = username == null ? "" : username; + if (!credentials.isEmpty()) { + credentials += this.password == null ? "@" : ":" + password + "@"; + } + String connectionOptions = authSource == null ? "" : "/?authSource=" + authSource; + connectionUri = String.format("mongodb://%s%s%s", credentials, hosts, connectionOptions); + } + + MongoClientSettings.Builder builder = MongoClientSettings.builder() + .applyConnectionString(new ConnectionString(connectionUri)); + + SSLContext sslContext = createSslContext(); + if (tls || sslContext != null || tlsAllowInvalidHostnames) { + builder.applyToSslSettings(ssl -> { + if (tls || sslContext != null) { + ssl.enabled(true); + } + if (sslContext != null) { + ssl.context(sslContext); + } + if (tlsAllowInvalidHostnames) { + ssl.invalidHostNameAllowed(true); + } + }); + } + return builder.build(); + } + + private SSLContext createSslContext() { + if (sslContextParameters == null) { + return null; + } try { - SSLContext sslContext = sslContextParameters.createSSLContext(getCamelContext()); - MongoClientSettings settings = MongoClientSettings.builder() - .applyConnectionString(new ConnectionString(connectionUri)) - .applyToSslSettings(builder -> { - builder.enabled(true); - builder.context(sslContext); - if (tlsAllowInvalidHostnames) { - builder.invalidHostNameAllowed(true); - } - }) - .build(); - return MongoClients.create(settings); + return sslContextParameters.createSSLContext(getCamelContext()); } catch (GeneralSecurityException | IOException e) { throw new CamelMongoDbException("Error creating SSLContext from SSLContextParameters", e); } @@ -1005,7 +1023,8 @@ public class MongoDbEndpoint extends DefaultEndpoint implements EndpointServiceL } /** - * Specifies that all communication with MongoDB instances should use TLS. Supersedes the ssl option. Default: false + * Specifies that all communication with MongoDB instances should use TLS. Supersedes the ssl option. Applies to the + * client the endpoint creates from hosts or connectionUriString. Default: false */ public void setTls(boolean tls) { this.tls = tls; diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbEndpointTlsTest.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbEndpointTlsTest.java new file mode 100644 index 000000000000..b34a7a43032a --- /dev/null +++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbEndpointTlsTest.java @@ -0,0 +1,114 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.mongodb; + +import com.mongodb.MongoClientSettings; +import com.mongodb.client.internal.MongoClientImpl; +import com.mongodb.connection.SslSettings; +import org.apache.camel.support.jsse.SSLContextParameters; +import org.apache.camel.test.junit6.CamelTestSupport; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * The TLS options of a mongodb endpoint are applied to the client it creates, whether the endpoint has hosts or a + * connectionUriString. + */ +class MongoDbEndpointTlsTest extends CamelTestSupport { + + private static final String HOSTS = "mongodb:dummy?hosts=localhost:27017&username=user&password=pass" + + "&database=test&collection=test&operation=findAll"; + private static final String CONNECTION_URI_STRING + = "mongodb:dummy?connectionUriString=RAW(mongodb://user:pass@localhost:27017/)" + + "&database=test&collection=test&operation=findAll"; + + private SslSettings sslSettings(String uri) { + MongoDbEndpoint endpoint = context.getEndpoint(uri, MongoDbEndpoint.class); + return endpoint.createMongoClientSettings().getSslSettings(); + } + + @Test + void hostsWithoutTlsIsPlain() { + assertFalse(sslSettings(HOSTS).isEnabled()); + } + + @Test + void hostsWithTlsUsesTls() { + SslSettings ssl = sslSettings(HOSTS + "&tls=true"); + assertTrue(ssl.isEnabled()); + assertFalse(ssl.isInvalidHostNameAllowed()); + } + + @Test + void hostsWithTlsAllowInvalidHostnames() { + SslSettings ssl = sslSettings(HOSTS + "&tls=true&tlsAllowInvalidHostnames=true"); + assertTrue(ssl.isEnabled()); + assertTrue(ssl.isInvalidHostNameAllowed()); + } + + @Test + void hostsWithSslContextParametersUsesTls() { + context.getRegistry().bind("ssl", new SSLContextParameters()); + + SslSettings ssl = sslSettings(HOSTS + "&sslContextParameters=#ssl"); + assertTrue(ssl.isEnabled()); + assertNotNull(ssl.getContext()); + } + + @Test + void connectionUriStringWithoutTlsIsPlain() { + assertFalse(sslSettings(CONNECTION_URI_STRING).isEnabled()); + } + + @Test + void connectionUriStringWithTlsUsesTls() { + SslSettings ssl = sslSettings(CONNECTION_URI_STRING + "&tls=true"); + assertTrue(ssl.isEnabled()); + assertNull(ssl.getContext()); + } + + @Test + void tlsInsideConnectionUriStringIsKept() { + SslSettings ssl = sslSettings( + "mongodb:dummy?connectionUriString=RAW(mongodb://localhost:27017/?tls=true&tlsAllowInvalidHostnames=true)" + + "&database=test&collection=test&operation=findAll"); + assertTrue(ssl.isEnabled()); + assertTrue(ssl.isInvalidHostNameAllowed()); + } + + @Test + void connectionUriStringWithSslContextParametersUsesTls() { + context.getRegistry().bind("ssl", new SSLContextParameters()); + + SslSettings ssl = sslSettings(CONNECTION_URI_STRING + "&sslContextParameters=#ssl"); + assertTrue(ssl.isEnabled()); + assertNotNull(ssl.getContext()); + } + + @Test + void clientCreatedFromHostsUsesTls() { + MongoDbEndpoint endpoint = context.getEndpoint(HOSTS + "&tls=true", MongoDbEndpoint.class); + + MongoClientSettings settings = assertInstanceOf(MongoClientImpl.class, endpoint.getMongoConnection()).getSettings(); + assertTrue(settings.getSslSettings().isEnabled()); + } +} diff --git a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc index d552fbdb2309..4f98c370a5e7 100644 --- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc +++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc @@ -6944,6 +6944,15 @@ the `CamelContext` stops, and creates a new one when it is started again. Before open. A client the endpoint did not create (the `mongoConnection` option, the client of the component, or the bean named by the endpoint path) is not closed. +=== camel-mongodb - the tls and tlsAllowInvalidHostnames options are applied + +The `tls` and `tlsAllowInvalidHostnames` endpoint options are now applied to the client the endpoint creates from +`hosts` or `connectionUriString`, as `sslContextParameters` already was. Before, they were not used: an endpoint with +`tls=true` and no `sslContextParameters` connected without TLS. Such an endpoint now connects with TLS, so the MongoDB +server must accept TLS connections, and its certificate must be trusted by the JVM (or set `sslContextParameters`). +When none of these options is set, the TLS settings of `connectionUriString` (for example `?tls=true`) are used as +before. + === camel-mongodb-gridfs - the consumer routes a file once, offers the file of a failed exchange again and sets CamelFileName, and an unknown operation fails With the `TimeStamp` (default) and `PersistentTimestamp` query strategies, the `mongodb-gridfs` consumer routed diff --git a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/MongoDbEndpointBuilderFactory.java b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/MongoDbEndpointBuilderFactory.java index 68894cc94327..71b23f1d745b 100644 --- a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/MongoDbEndpointBuilderFactory.java +++ b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/MongoDbEndpointBuilderFactory.java @@ -543,7 +543,8 @@ public interface MongoDbEndpointBuilderFactory { } /** * Specifies that all communication with MongoDB instances should use - * TLS. Supersedes the ssl option. Default: false. + * TLS. Supersedes the ssl option. Applies to the client the endpoint + * creates from hosts or connectionUriString. Default: false. * * The option is a: <code>boolean</code> type. * @@ -559,7 +560,8 @@ public interface MongoDbEndpointBuilderFactory { } /** * Specifies that all communication with MongoDB instances should use - * TLS. Supersedes the ssl option. Default: false. + * TLS. Supersedes the ssl option. Applies to the client the endpoint + * creates from hosts or connectionUriString. Default: false. * * The option will be converted to a <code>boolean</code> type. * @@ -1893,7 +1895,8 @@ public interface MongoDbEndpointBuilderFactory { } /** * Specifies that all communication with MongoDB instances should use - * TLS. Supersedes the ssl option. Default: false. + * TLS. Supersedes the ssl option. Applies to the client the endpoint + * creates from hosts or connectionUriString. Default: false. * * The option is a: <code>boolean</code> type. * @@ -1909,7 +1912,8 @@ public interface MongoDbEndpointBuilderFactory { } /** * Specifies that all communication with MongoDB instances should use - * TLS. Supersedes the ssl option. Default: false. + * TLS. Supersedes the ssl option. Applies to the client the endpoint + * creates from hosts or connectionUriString. Default: false. * * The option will be converted to a <code>boolean</code> type. * @@ -3172,7 +3176,8 @@ public interface MongoDbEndpointBuilderFactory { } /** * Specifies that all communication with MongoDB instances should use - * TLS. Supersedes the ssl option. Default: false. + * TLS. Supersedes the ssl option. Applies to the client the endpoint + * creates from hosts or connectionUriString. Default: false. * * The option is a: <code>boolean</code> type. * @@ -3188,7 +3193,8 @@ public interface MongoDbEndpointBuilderFactory { } /** * Specifies that all communication with MongoDB instances should use - * TLS. Supersedes the ssl option. Default: false. + * TLS. Supersedes the ssl option. Applies to the client the endpoint + * creates from hosts or connectionUriString. Default: false. * * The option will be converted to a <code>boolean</code> type. *
