This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-4.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.22.x by this push:
new af6914a112b4 [backport camel-4.22.x] CAMEL-27218: camel-mongodb - the
tls and tlsAllowInvalidHostnames options are applied to the client created from
hosts or connectionUriString (#30402)
af6914a112b4 is described below
commit af6914a112b49ab0d0ca00f395d5422da49ab50f
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Nov 19 15:04:16 2026 +0100
[backport camel-4.22.x] CAMEL-27218: camel-mongodb - the tls and
tlsAllowInvalidHostnames options are applied to the client created from hosts
or connectionUriString (#30402)
(backport of #30362, adapted for camel-4.22.x)
Co-Authored-By: Claude Opus 5.5 <[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 | 115 +++++++++++++++++++++
.../dsl/MongoDbEndpointBuilderFactory.java | 18 ++--
5 files changed, 176 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 2ef4b7c20a99..256ee9104c88 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
@@ -115,7 +115,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 when hosts is set. 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, "defaultValue":
false, "description": "Specifies that the driver should allow invalid hostnames
in the certificate for TLS connections. Supersedes sslInvalidHostNameAllowed.
Has the same effect as tlsInsecure by setti [...]
"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 2ef4b7c20a99..256ee9104c88 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
@@ -115,7 +115,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 when hosts is set. 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, "defaultValue":
false, "description": "Specifies that the driver should allow invalid hostnames
in the certificate for TLS connections. Supersedes sslInvalidHostNameAllowed.
Has the same effect as tlsInsecure by setti [...]
"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 97949cd2eb88..27a51e54c87b 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
@@ -446,20 +446,7 @@ public class MongoDbEndpoint extends DefaultEndpoint
implements EndpointServiceL
private MongoClient resolveMongoConnection() {
MongoClient mongoClient;
if (this.hosts != 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());
LOG.debug("Connection created using provided credentials");
} else {
mongoClient = getComponent().getMongoConnection();
@@ -473,20 +460,51 @@ public class MongoDbEndpoint extends DefaultEndpoint
implements EndpointServiceL
return mongoClient;
}
- private MongoClient createMongoClientWithSslContext(String connectionUri) {
+ /**
+ * The settings of the client this endpoint creates when hosts is set,
from connectionUriString when set, otherwise
+ * from hosts. 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 connection string comes from hosts or
+ * from 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);
}
@@ -918,7 +936,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 when hosts is set. 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..352b05986409
--- /dev/null
+++
b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbEndpointTlsTest.java
@@ -0,0 +1,115 @@
+/*
+ * 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
when hosts is set, whether the connection
+ * string comes from hosts or from 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?hosts=localhost:27017&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?hosts=localhost:27017"
+ +
"&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/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 93b98e3d4b50..1cdd2600a5df 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
@@ -541,7 +541,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 when hosts is set. Default: false.
*
* The option is a: <code>boolean</code> type.
*
@@ -557,7 +558,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 when hosts is set. Default: false.
*
* The option will be converted to a <code>boolean</code> type.
*
@@ -1889,7 +1891,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 when hosts is set. Default: false.
*
* The option is a: <code>boolean</code> type.
*
@@ -1905,7 +1908,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 when hosts is set. Default: false.
*
* The option will be converted to a <code>boolean</code> type.
*
@@ -3166,7 +3170,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 when hosts is set. Default: false.
*
* The option is a: <code>boolean</code> type.
*
@@ -3182,7 +3187,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 when hosts is set. Default: false.
*
* The option will be converted to a <code>boolean</code> type.
*