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

acosentino pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-kamelets.git


The following commit(s) were added to refs/heads/main by this push:
     new ff8914ea Added ssl support to mongodb kamelets.
ff8914ea is described below

commit ff8914ea04b84086aaec99419abe5ce6629a4662
Author: Andrea Tarocchi <andrea.taroc...@gmail.com>
AuthorDate: Mon Jan 23 17:25:48 2023 +0100

    Added ssl support to mongodb kamelets.
---
 .../mongodb-changes-stream-source.kamelet.yaml     |  26 ++-
 kamelets/mongodb-sink.kamelet.yaml                 |  26 ++-
 kamelets/mongodb-source.kamelet.yaml               |  26 ++-
 library/camel-kamelets-utils/pom.xml               |   7 +
 .../utils/mongodb/SslAwareMongoClient.java         | 245 +++++++++++++++++++++
 .../mongodb-changes-stream-source.kamelet.yaml     |  26 ++-
 .../resources/kamelets/mongodb-sink.kamelet.yaml   |  26 ++-
 .../resources/kamelets/mongodb-source.kamelet.yaml |  26 ++-
 8 files changed, 390 insertions(+), 18 deletions(-)

diff --git a/kamelets/mongodb-changes-stream-source.kamelet.yaml 
b/kamelets/mongodb-changes-stream-source.kamelet.yaml
index fa8be79c..1fb0985e 100644
--- a/kamelets/mongodb-changes-stream-source.kamelet.yaml
+++ b/kamelets/mongodb-changes-stream-source.kamelet.yaml
@@ -59,6 +59,20 @@ spec:
         type: string
         x-descriptors:
         - urn:camel:group:credentials
+      ssl:
+        title: Enable Ssl for Mongodb Connection
+        description: whether to enable ssl connection to mongodb
+        type: boolean
+        default: true
+        x-descriptors:
+          - 'urn:alm:descriptor:com.tectonic.ui:checkbox'
+      sslValidationEnabled:
+        title: Enables Ssl Certificates Validation and Host name checks.
+        description: IMPORTANT this should be disabled only in test 
environment since can pose security issues.
+        type: boolean
+        default: true
+        x-descriptors:
+          - 'urn:alm:descriptor:com.tectonic.ui:checkbox'
       database:
         title: MongoDB Database
         description: Sets the name of the MongoDB database to target.
@@ -76,16 +90,22 @@ spec:
     beans:
       - name: local-mongodb
         type: "#class:org.apache.camel.component.mongodb.MongoDbComponent"
+      - name: mongo-client
+        type: 
"#class:org.apache.camel.kamelets.utils.mongodb.SslAwareMongoClient"
+        properties:
+          password: "{{?password}}"
+          username: "{{?username}}"
+          hosts: "{{hosts}}"
+          ssl: "{{ssl}}"
+          sslValidationEnabled: "{{sslValidationEnabled}}"
     from:
       uri: "{{local-mongodb}}:test"
       parameters:
-        hosts: "{{hosts}}"
         collection: "{{collection}}"
-        password: "{{?password}}"
-        username: "{{?username}}"
         database: "{{database}}"
         consumerType: "changeStreams"
         streamFilter: "{{?streamFilter}}"
+        mongoConnection: "#{{mongo-client}}"
       steps:
       - marshal:
           json: {}
diff --git a/kamelets/mongodb-sink.kamelet.yaml 
b/kamelets/mongodb-sink.kamelet.yaml
index c4f8e64b..bd757c41 100644
--- a/kamelets/mongodb-sink.kamelet.yaml
+++ b/kamelets/mongodb-sink.kamelet.yaml
@@ -63,6 +63,20 @@ spec:
         type: string
         x-descriptors:
         - urn:camel:group:credentials
+      ssl:
+        title: Enable Ssl for Mongodb Connection
+        description: whether to enable ssl connection to mongodb
+        type: boolean
+        default: true
+        x-descriptors:
+          - 'urn:alm:descriptor:com.tectonic.ui:checkbox'
+      sslValidationEnabled:
+        title: Enables Ssl Certificates Validation and Host name checks.
+        description: IMPORTANT this should be disabled only in test 
environment since can pose security issues.
+        type: boolean
+        default: true
+        x-descriptors:
+          - 'urn:alm:descriptor:com.tectonic.ui:checkbox'
       database:
         title: MongoDB Database
         description: The name of the MongoDB database.
@@ -90,6 +104,14 @@ spec:
     beans:
       - name: local-mongodb
         type: "#class:org.apache.camel.component.mongodb.MongoDbComponent"
+      - name: mongo-client
+        type: 
"#class:org.apache.camel.kamelets.utils.mongodb.SslAwareMongoClient"
+        properties:
+          password: "{{?password}}"
+          username: "{{?username}}"
+          hosts: "{{hosts}}"
+          ssl: "{{ssl}}"
+          sslValidationEnabled: "{{sslValidationEnabled}}"
     from:
       uri: kamelet:source
       steps:
@@ -110,9 +132,7 @@ spec:
           parameters:
             createCollection: "{{?createCollection}}"
             writeConcern: "{{?writeConcern}}"
-            hosts: "{{hosts}}"
             collection: "{{collection}}"
-            password: "{{?password}}"
-            username: "{{?username}}"
             database: "{{database}}"
             operation: "insert"
+            mongoConnection: "#{{mongo-client}}"
diff --git a/kamelets/mongodb-source.kamelet.yaml 
b/kamelets/mongodb-source.kamelet.yaml
index 9ea2cfcc..e928e808 100644
--- a/kamelets/mongodb-source.kamelet.yaml
+++ b/kamelets/mongodb-source.kamelet.yaml
@@ -65,6 +65,20 @@ spec:
         type: string
         x-descriptors:
         - urn:camel:group:credentials
+      ssl:
+        title: Enable Ssl for Mongodb Connection
+        description: whether to enable ssl connection to mongodb
+        type: boolean
+        default: true
+        x-descriptors:
+          - 'urn:alm:descriptor:com.tectonic.ui:checkbox'
+      sslValidationEnabled:
+        title: Enables Ssl Certificates Validation and Host name checks.
+        description: IMPORTANT this should be disabled only in test 
environment since can pose security issues.
+        type: boolean
+        default: true
+        x-descriptors:
+          - 'urn:alm:descriptor:com.tectonic.ui:checkbox'
       database:
         title: MongoDB Database
         description: The name of the MongoDB database.
@@ -88,16 +102,22 @@ spec:
     beans:
       - name: local-mongodb
         type: "#class:org.apache.camel.component.mongodb.MongoDbComponent"
+      - name: mongo-client
+        type: 
"#class:org.apache.camel.kamelets.utils.mongodb.SslAwareMongoClient"
+        properties:
+          password: "{{?password}}"
+          username: "{{?username}}"
+          hosts: "{{hosts}}"
+          ssl: "{{ssl}}"
+          sslValidationEnabled: "{{sslValidationEnabled}}"
     from:
       uri: "{{local-mongodb}}:test"
       parameters:
-        hosts: "{{hosts}}"
         collection: "{{collection}}"
-        password: "{{?password}}"
-        username: "{{?username}}"
         database: "{{database}}"
         persistentTailTracking: "{{persistentTailTracking}}"
         tailTrackIncreasingField: "{{?tailTrackIncreasingField}}"
+        mongoConnection: "#{{mongo-client}}"
       steps:
       - marshal:
           json: {}
diff --git a/library/camel-kamelets-utils/pom.xml 
b/library/camel-kamelets-utils/pom.xml
index 3950786f..a5a05809 100644
--- a/library/camel-kamelets-utils/pom.xml
+++ b/library/camel-kamelets-utils/pom.xml
@@ -83,6 +83,13 @@
             <scope>provided</scope>
         </dependency>
 
+        <!-- Dependencies for mongodb connection configuration -->
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-mongodb</artifactId>
+            <scope>provided</scope>
+        </dependency>
+
         <!-- Test scoped dependencies -->
         <dependency>
             <groupId>org.junit.jupiter</groupId>
diff --git 
a/library/camel-kamelets-utils/src/main/java/org/apache/camel/kamelets/utils/mongodb/SslAwareMongoClient.java
 
b/library/camel-kamelets-utils/src/main/java/org/apache/camel/kamelets/utils/mongodb/SslAwareMongoClient.java
new file mode 100644
index 00000000..e85ba448
--- /dev/null
+++ 
b/library/camel-kamelets-utils/src/main/java/org/apache/camel/kamelets/utils/mongodb/SslAwareMongoClient.java
@@ -0,0 +1,245 @@
+/*
+ * 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.kamelets.utils.mongodb;
+
+import com.mongodb.ClientSessionOptions;
+import com.mongodb.ConnectionString;
+import com.mongodb.MongoClientSettings;
+import com.mongodb.client.ChangeStreamIterable;
+import com.mongodb.client.ClientSession;
+import com.mongodb.client.ListDatabasesIterable;
+import com.mongodb.client.MongoClient;
+import com.mongodb.client.MongoClients;
+import com.mongodb.client.MongoDatabase;
+import com.mongodb.client.MongoIterable;
+import com.mongodb.connection.ClusterDescription;
+import org.apache.camel.util.function.Suppliers;
+import org.bson.Document;
+import org.bson.conversions.Bson;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
+import java.util.List;
+import java.util.function.Supplier;
+
+public class SslAwareMongoClient implements MongoClient {
+    private static final Logger LOG = 
LoggerFactory.getLogger(SslAwareMongoClient.class);
+    private static final TrustManager[] trustAllCerts = new TrustManager[] {
+            new X509TrustManager() {
+                public X509Certificate[] getAcceptedIssuers() {
+                    return null;
+                }
+
+                @Override
+                public void checkClientTrusted(X509Certificate[] arg0, String 
arg1)
+                        throws CertificateException {
+                }
+
+                @Override
+                public void checkServerTrusted(X509Certificate[] arg0, String 
arg1)
+                        throws CertificateException {
+                }
+            }
+    };
+    private final Supplier<MongoClient> wrappedMongoClient = 
Suppliers.memorize(new Supplier<MongoClient>() {
+        @Override
+        public MongoClient get() {
+            String credentials = username == null ? "" : username;
+
+            if (!credentials.equals("")) {
+                credentials += password == null ? "@" : ":" + password + "@";
+            }
+
+            MongoClientSettings settings = MongoClientSettings.builder()
+                    .applyToSslSettings(builder -> {
+                        builder.enabled(ssl);
+                        if (!sslValidationEnabled) {
+                            builder.invalidHostNameAllowed(true);
+                            SSLContext sc = null;
+                            try {
+                                sc = SSLContext.getInstance("SSL");
+                            } catch (NoSuchAlgorithmException e) {
+                                throw new RuntimeException("Error 
instantiating trust all SSL context.", e);
+                            }
+                            try {
+                                sc.init(null, trustAllCerts, new 
java.security.SecureRandom());
+                            } catch (KeyManagementException e) {
+                                throw new RuntimeException("Error 
instantiating trust all SSL context.", e);
+                            }
+                            builder.context(sc);
+                        }
+                    })
+                    .applyConnectionString(new 
ConnectionString(String.format("mongodb://%s%s", credentials, hosts)))
+                    .build();
+            LOG.info("Connection created using provided credentials");
+            return MongoClients.create(settings);
+        }
+    });
+    private String hosts = null;
+    private String username = null;
+    private String password = null;
+    private boolean ssl = true;
+
+    private boolean sslValidationEnabled = true;
+
+    public MongoClient getWrappedMongoClient() {
+        return wrappedMongoClient.get();
+    }
+
+    @Override
+    public MongoDatabase getDatabase(String s) {
+        return getWrappedMongoClient().getDatabase(s);
+    }
+
+    @Override
+    public ClientSession startSession() {
+        return getWrappedMongoClient().startSession();
+    }
+
+    @Override
+    public ClientSession startSession(ClientSessionOptions 
clientSessionOptions) {
+        return getWrappedMongoClient().startSession(clientSessionOptions);
+    }
+
+    @Override
+    public void close() {
+        getWrappedMongoClient().close();
+    }
+
+    @Override
+    public MongoIterable<String> listDatabaseNames() {
+        return getWrappedMongoClient().listDatabaseNames();
+    }
+
+    @Override
+    public MongoIterable<String> listDatabaseNames(ClientSession 
clientSession) {
+        return getWrappedMongoClient().listDatabaseNames(clientSession);
+    }
+
+    @Override
+    public ListDatabasesIterable<Document> listDatabases() {
+        return getWrappedMongoClient().listDatabases();
+    }
+
+    @Override
+    public ListDatabasesIterable<Document> listDatabases(ClientSession 
clientSession) {
+        return getWrappedMongoClient().listDatabases(clientSession);
+    }
+
+    @Override
+    public <TResult> ListDatabasesIterable<TResult> 
listDatabases(Class<TResult> aClass) {
+        return getWrappedMongoClient().listDatabases(aClass);
+    }
+
+    @Override
+    public <TResult> ListDatabasesIterable<TResult> 
listDatabases(ClientSession clientSession, Class<TResult> aClass) {
+        return getWrappedMongoClient().listDatabases(clientSession, aClass);
+    }
+
+    @Override
+    public ChangeStreamIterable<Document> watch() {
+        return getWrappedMongoClient().watch();
+    }
+
+    @Override
+    public <TResult> ChangeStreamIterable<TResult> watch(Class<TResult> 
aClass) {
+        return getWrappedMongoClient().watch(aClass);
+    }
+
+    @Override
+    public ChangeStreamIterable<Document> watch(List<? extends Bson> list) {
+        return getWrappedMongoClient().watch(list);
+    }
+
+    @Override
+    public <TResult> ChangeStreamIterable<TResult> watch(List<? extends Bson> 
list, Class<TResult> aClass) {
+        return getWrappedMongoClient().watch(list, aClass);
+    }
+
+    @Override
+    public ChangeStreamIterable<Document> watch(ClientSession clientSession) {
+        return getWrappedMongoClient().watch(clientSession);
+    }
+
+    @Override
+    public <TResult> ChangeStreamIterable<TResult> watch(ClientSession 
clientSession, Class<TResult> aClass) {
+        return getWrappedMongoClient().watch(clientSession, aClass);
+    }
+
+    @Override
+    public ChangeStreamIterable<Document> watch(ClientSession clientSession, 
List<? extends Bson> list) {
+        return getWrappedMongoClient().watch(clientSession, list);
+    }
+
+    @Override
+    public <TResult> ChangeStreamIterable<TResult> watch(ClientSession 
clientSession, List<? extends Bson> list,
+            Class<TResult> aClass) {
+        return getWrappedMongoClient().watch(clientSession, list, aClass);
+    }
+
+    @Override
+    public ClusterDescription getClusterDescription() {
+        return getWrappedMongoClient().getClusterDescription();
+    }
+
+    public String getHosts() {
+        return hosts;
+    }
+
+    public void setHosts(String hosts) {
+        this.hosts = hosts;
+    }
+
+    public String getUsername() {
+        return username;
+    }
+
+    public void setUsername(String username) {
+        this.username = username;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
+    public boolean isSsl() {
+        return ssl;
+    }
+
+    public void setSsl(boolean ssl) {
+        this.ssl = ssl;
+    }
+
+    public boolean isSslValidationEnabled() {
+        return sslValidationEnabled;
+    }
+
+    public void setSslValidationEnabled(boolean sslValidationEnabled) {
+        this.sslValidationEnabled = sslValidationEnabled;
+    }
+}
diff --git 
a/library/camel-kamelets/src/main/resources/kamelets/mongodb-changes-stream-source.kamelet.yaml
 
b/library/camel-kamelets/src/main/resources/kamelets/mongodb-changes-stream-source.kamelet.yaml
index fa8be79c..1fb0985e 100644
--- 
a/library/camel-kamelets/src/main/resources/kamelets/mongodb-changes-stream-source.kamelet.yaml
+++ 
b/library/camel-kamelets/src/main/resources/kamelets/mongodb-changes-stream-source.kamelet.yaml
@@ -59,6 +59,20 @@ spec:
         type: string
         x-descriptors:
         - urn:camel:group:credentials
+      ssl:
+        title: Enable Ssl for Mongodb Connection
+        description: whether to enable ssl connection to mongodb
+        type: boolean
+        default: true
+        x-descriptors:
+          - 'urn:alm:descriptor:com.tectonic.ui:checkbox'
+      sslValidationEnabled:
+        title: Enables Ssl Certificates Validation and Host name checks.
+        description: IMPORTANT this should be disabled only in test 
environment since can pose security issues.
+        type: boolean
+        default: true
+        x-descriptors:
+          - 'urn:alm:descriptor:com.tectonic.ui:checkbox'
       database:
         title: MongoDB Database
         description: Sets the name of the MongoDB database to target.
@@ -76,16 +90,22 @@ spec:
     beans:
       - name: local-mongodb
         type: "#class:org.apache.camel.component.mongodb.MongoDbComponent"
+      - name: mongo-client
+        type: 
"#class:org.apache.camel.kamelets.utils.mongodb.SslAwareMongoClient"
+        properties:
+          password: "{{?password}}"
+          username: "{{?username}}"
+          hosts: "{{hosts}}"
+          ssl: "{{ssl}}"
+          sslValidationEnabled: "{{sslValidationEnabled}}"
     from:
       uri: "{{local-mongodb}}:test"
       parameters:
-        hosts: "{{hosts}}"
         collection: "{{collection}}"
-        password: "{{?password}}"
-        username: "{{?username}}"
         database: "{{database}}"
         consumerType: "changeStreams"
         streamFilter: "{{?streamFilter}}"
+        mongoConnection: "#{{mongo-client}}"
       steps:
       - marshal:
           json: {}
diff --git 
a/library/camel-kamelets/src/main/resources/kamelets/mongodb-sink.kamelet.yaml 
b/library/camel-kamelets/src/main/resources/kamelets/mongodb-sink.kamelet.yaml
index c4f8e64b..bd757c41 100644
--- 
a/library/camel-kamelets/src/main/resources/kamelets/mongodb-sink.kamelet.yaml
+++ 
b/library/camel-kamelets/src/main/resources/kamelets/mongodb-sink.kamelet.yaml
@@ -63,6 +63,20 @@ spec:
         type: string
         x-descriptors:
         - urn:camel:group:credentials
+      ssl:
+        title: Enable Ssl for Mongodb Connection
+        description: whether to enable ssl connection to mongodb
+        type: boolean
+        default: true
+        x-descriptors:
+          - 'urn:alm:descriptor:com.tectonic.ui:checkbox'
+      sslValidationEnabled:
+        title: Enables Ssl Certificates Validation and Host name checks.
+        description: IMPORTANT this should be disabled only in test 
environment since can pose security issues.
+        type: boolean
+        default: true
+        x-descriptors:
+          - 'urn:alm:descriptor:com.tectonic.ui:checkbox'
       database:
         title: MongoDB Database
         description: The name of the MongoDB database.
@@ -90,6 +104,14 @@ spec:
     beans:
       - name: local-mongodb
         type: "#class:org.apache.camel.component.mongodb.MongoDbComponent"
+      - name: mongo-client
+        type: 
"#class:org.apache.camel.kamelets.utils.mongodb.SslAwareMongoClient"
+        properties:
+          password: "{{?password}}"
+          username: "{{?username}}"
+          hosts: "{{hosts}}"
+          ssl: "{{ssl}}"
+          sslValidationEnabled: "{{sslValidationEnabled}}"
     from:
       uri: kamelet:source
       steps:
@@ -110,9 +132,7 @@ spec:
           parameters:
             createCollection: "{{?createCollection}}"
             writeConcern: "{{?writeConcern}}"
-            hosts: "{{hosts}}"
             collection: "{{collection}}"
-            password: "{{?password}}"
-            username: "{{?username}}"
             database: "{{database}}"
             operation: "insert"
+            mongoConnection: "#{{mongo-client}}"
diff --git 
a/library/camel-kamelets/src/main/resources/kamelets/mongodb-source.kamelet.yaml
 
b/library/camel-kamelets/src/main/resources/kamelets/mongodb-source.kamelet.yaml
index 9ea2cfcc..e928e808 100644
--- 
a/library/camel-kamelets/src/main/resources/kamelets/mongodb-source.kamelet.yaml
+++ 
b/library/camel-kamelets/src/main/resources/kamelets/mongodb-source.kamelet.yaml
@@ -65,6 +65,20 @@ spec:
         type: string
         x-descriptors:
         - urn:camel:group:credentials
+      ssl:
+        title: Enable Ssl for Mongodb Connection
+        description: whether to enable ssl connection to mongodb
+        type: boolean
+        default: true
+        x-descriptors:
+          - 'urn:alm:descriptor:com.tectonic.ui:checkbox'
+      sslValidationEnabled:
+        title: Enables Ssl Certificates Validation and Host name checks.
+        description: IMPORTANT this should be disabled only in test 
environment since can pose security issues.
+        type: boolean
+        default: true
+        x-descriptors:
+          - 'urn:alm:descriptor:com.tectonic.ui:checkbox'
       database:
         title: MongoDB Database
         description: The name of the MongoDB database.
@@ -88,16 +102,22 @@ spec:
     beans:
       - name: local-mongodb
         type: "#class:org.apache.camel.component.mongodb.MongoDbComponent"
+      - name: mongo-client
+        type: 
"#class:org.apache.camel.kamelets.utils.mongodb.SslAwareMongoClient"
+        properties:
+          password: "{{?password}}"
+          username: "{{?username}}"
+          hosts: "{{hosts}}"
+          ssl: "{{ssl}}"
+          sslValidationEnabled: "{{sslValidationEnabled}}"
     from:
       uri: "{{local-mongodb}}:test"
       parameters:
-        hosts: "{{hosts}}"
         collection: "{{collection}}"
-        password: "{{?password}}"
-        username: "{{?username}}"
         database: "{{database}}"
         persistentTailTracking: "{{persistentTailTracking}}"
         tailTrackIncreasingField: "{{?tailTrackIncreasingField}}"
+        mongoConnection: "#{{mongo-client}}"
       steps:
       - marshal:
           json: {}

Reply via email to