This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-4.18.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-4.18.x by this push:
     new 77b24af75bda [backport camel-4.18.x] CAMEL-27218: camel-mongodb - the 
tls and tlsAllowInvalidHostnames options are applied to the client created from 
hosts or connectionUriString (#30403)
77b24af75bda is described below

commit 77b24af75bda5ba196842f98c24df50fa7d9b821
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Nov 19 15:13:40 2026 +0100

    [backport camel-4.18.x] CAMEL-27218: camel-mongodb - the tls and 
tlsAllowInvalidHostnames options are applied to the client created from hosts 
or connectionUriString (#30403)
    
    (backport of #30362, adapted for camel-4.18.x)
    
    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   | 51 +++++++++---
 .../component/mongodb/MongoDbEndpointTlsTest.java  | 95 ++++++++++++++++++++++
 .../dsl/MongoDbEndpointBuilderFactory.java         | 18 ++--
 5 files changed, 149 insertions(+), 19 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 39568e127aa4..19e95d9b30a6 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
@@ -111,7 +111,7 @@
     "streamFilter": { "index": 50, "kind": "parameter", "displayName": "Stream 
Filter", "group": "changeStream", "label": "consumer,changeStream", "required": 
false, "type": "string", "javaType": "java.lang.String", "deprecated": false, 
"autowired": false, "secret": false, "description": "Filter condition for 
change streams consumer." },
     "authSource": { "index": 51, "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": 52, "kind": "parameter", "displayName": "Password", 
"group": "security", "label": "security", "required": false, "type": "string", 
"javaType": "java.lang.String", "deprecated": false, "autowired": false, 
"secret": true, "description": "User password for mongodb connection" },
-    "tls": { "index": 53, "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": 53, "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": 54, "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": 55, "kind": "parameter", "displayName": "Username", 
"group": "security", "label": "security", "required": false, "type": "string", 
"javaType": "java.lang.String", "deprecated": false, "autowired": false, 
"secret": true, "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 39568e127aa4..19e95d9b30a6 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
@@ -111,7 +111,7 @@
     "streamFilter": { "index": 50, "kind": "parameter", "displayName": "Stream 
Filter", "group": "changeStream", "label": "consumer,changeStream", "required": 
false, "type": "string", "javaType": "java.lang.String", "deprecated": false, 
"autowired": false, "secret": false, "description": "Filter condition for 
change streams consumer." },
     "authSource": { "index": 51, "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": 52, "kind": "parameter", "displayName": "Password", 
"group": "security", "label": "security", "required": false, "type": "string", 
"javaType": "java.lang.String", "deprecated": false, "autowired": false, 
"secret": true, "description": "User password for mongodb connection" },
-    "tls": { "index": 53, "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": 53, "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": 54, "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": 55, "kind": "parameter", "displayName": "Username", 
"group": "security", "label": "security", "required": false, "type": "string", 
"javaType": "java.lang.String", "deprecated": false, "autowired": false, 
"secret": true, "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 d5b41a87e4bc..a506ff73d130 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
@@ -24,6 +24,8 @@ import java.util.Map;
 import java.util.stream.StreamSupport;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.mongodb.ConnectionString;
+import com.mongodb.MongoClientSettings;
 import com.mongodb.ReadPreference;
 import com.mongodb.WriteConcern;
 import com.mongodb.client.MongoClient;
@@ -433,16 +435,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;
-            if (connectionUriString != null) {
-                mongoClient = MongoClients.create(connectionUriString);
-            } else {
-                mongoClient = 
MongoClients.create(String.format("mongodb://%s%s%s", credentials, hosts, 
connectionOptions));
-            }
+            mongoClient = MongoClients.create(createMongoClientSettings());
             LOG.debug("Connection created using provided credentials");
         } else {
             mongoClient = getComponent().getMongoConnection();
@@ -456,6 +449,41 @@ public class MongoDbEndpoint extends DefaultEndpoint 
implements EndpointServiceL
         return mongoClient;
     }
 
+    /**
+     * 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 and 
tlsAllowInvalidHostnames) 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));
+
+        if (tls || tlsAllowInvalidHostnames) {
+            builder.applyToSslSettings(ssl -> {
+                if (tls) {
+                    ssl.enabled(true);
+                }
+                if (tlsAllowInvalidHostnames) {
+                    ssl.invalidHostNameAllowed(true);
+                }
+            });
+        }
+        return builder.build();
+    }
+
     public String getConnectionBean() {
         return connectionBean;
     }
@@ -882,7 +910,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..92b7cc4b246e
--- /dev/null
+++ 
b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbEndpointTlsTest.java
@@ -0,0 +1,95 @@
+/*
+ * 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.test.junit5.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.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 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 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 39028de92732..ee909aa4be7f 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
@@ -463,7 +463,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.
          * 
@@ -479,7 +480,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.
          * 
@@ -1779,7 +1781,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.
          * 
@@ -1795,7 +1798,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.
          * 
@@ -3024,7 +3028,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.
          * 
@@ -3040,7 +3045,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.
          * 

Reply via email to