From 9452bddb29083b8475ace98ba9792740050cc8e2 Mon Sep 17 00:00:00 2001 From: shnrndk Date: Thu, 8 Jun 2023 11:08:54 +0530 Subject: [PATCH] Add SSL and Auth source support to Mongo data-sources. --- .../dataservices/common/DBConstants.java | 2 ++ .../core/description/config/MongoConfig.java | 19 +++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/components/data/data-services/org.wso2.micro.integrator.dataservices.common/src/main/java/org/wso2/micro/integrator/dataservices/common/DBConstants.java b/components/data/data-services/org.wso2.micro.integrator.dataservices.common/src/main/java/org/wso2/micro/integrator/dataservices/common/DBConstants.java index 0e3b0089a1..343ede65a1 100644 --- a/components/data/data-services/org.wso2.micro.integrator.dataservices.common/src/main/java/org/wso2/micro/integrator/dataservices/common/DBConstants.java +++ b/components/data/data-services/org.wso2.micro.integrator.dataservices.common/src/main/java/org/wso2/micro/integrator/dataservices/common/DBConstants.java @@ -673,6 +673,8 @@ private MongoDB() { public static final String CONNECT_TIMEOUT = "mongoDB_connectTimeout"; public static final String MAX_WAIT_TIME = "mongoDB_maxWaitTime"; public static final String SOCKET_TIMEOUT = "mongoDB_socketTimeout"; + public static final String SSL_ENABLED = "mongoDB_ssl_enabled"; + public static final String AUTH_SOURCE = "mongoDB_auth_source"; public static final String CONNECTIONS_PER_HOST = "mongoDB_connectionsPerHost"; public static final String THREADS_ALLOWED_TO_BLOCK_CONN_MULTIPLIER = "mongoDB_threadsAllowedToBlockForConnectionMultiplier"; public static final String RESULT_COLUMN_NAME = "Document"; diff --git a/components/data/data-services/org.wso2.micro.integrator.dataservices.core/src/main/java/org/wso2/micro/integrator/dataservices/core/description/config/MongoConfig.java b/components/data/data-services/org.wso2.micro.integrator.dataservices.core/src/main/java/org/wso2/micro/integrator/dataservices/core/description/config/MongoConfig.java index a7974b35eb..1999e5b2a5 100644 --- a/components/data/data-services/org.wso2.micro.integrator.dataservices.core/src/main/java/org/wso2/micro/integrator/dataservices/core/description/config/MongoConfig.java +++ b/components/data/data-services/org.wso2.micro.integrator.dataservices.core/src/main/java/org/wso2/micro/integrator/dataservices/core/description/config/MongoConfig.java @@ -143,6 +143,11 @@ private MongoClientOptions extractMongoOptions(Map properties) { builder.threadsAllowedToBlockForConnectionMultiplier( Integer.parseInt(threadsAllowedToBlockForConnectionMultiplier)); } + + String sslEnabled = (properties.get(DBConstants.MongoDB.SSL_ENABLED)); + if (Boolean.parseBoolean(sslEnabled)) { + builder.sslEnabled(true); + } return builder.build(); } @@ -169,17 +174,23 @@ private MongoCredential createCredential(Map properties) throws String authenticationType = properties.get(DBConstants.MongoDB.AUTHENTICATION_TYPE); String username = properties.get(DBConstants.MongoDB.USERNAME); String password = properties.get(DBConstants.MongoDB.PASSWORD); - String database = properties.get(DBConstants.MongoDB.DATABASE); + String authSource = properties.get(DBConstants.MongoDB.AUTH_SOURCE); + if (authSource == null || authSource.isEmpty()) { + // For MONGODB-CR, SCRAM-SHA-1, and SCRAM-SHA-256, PLAIN the default auth source is the database tyring to connect + // refer: https://docs.mongodb.com/ruby-driver/master/reference/authentication/ + // since database is mandatory, we will not address the case where DB is not defined. + authSource = properties.get(DBConstants.MongoDB.DATABASE); + } if (authenticationType != null) { switch (authenticationType) { case DBConstants.MongoDB.MongoAuthenticationTypes.PLAIN: - credential = MongoCredential.createPlainCredential(username, database, password.toCharArray()); + credential = MongoCredential.createPlainCredential(username, authSource, password.toCharArray()); break; case DBConstants.MongoDB.MongoAuthenticationTypes.SCRAM_SHA_1: - credential = MongoCredential.createScramSha1Credential(username, database, password.toCharArray()); + credential = MongoCredential.createScramSha1Credential(username, authSource, password.toCharArray()); break; case DBConstants.MongoDB.MongoAuthenticationTypes.MONGODB_CR: - credential = MongoCredential.createMongoCRCredential(username, database, password.toCharArray()); + credential = MongoCredential.createMongoCRCredential(username, authSource, password.toCharArray()); break; case DBConstants.MongoDB.MongoAuthenticationTypes.GSSAPI: credential = MongoCredential.createGSSAPICredential(username);