Skip to content

Commit

Permalink
Merge pull request #2918 from shnrndk/master
Browse files Browse the repository at this point in the history
Add SSL and Auth source support to Mongo data-sources.
  • Loading branch information
chanikag authored Aug 22, 2023
2 parents 09b5a41 + 9452bdd commit 5bc85e4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ private MongoClientOptions extractMongoOptions(Map<String, String> properties) {
builder.threadsAllowedToBlockForConnectionMultiplier(
Integer.parseInt(threadsAllowedToBlockForConnectionMultiplier));
}

String sslEnabled = (properties.get(DBConstants.MongoDB.SSL_ENABLED));
if (Boolean.parseBoolean(sslEnabled)) {
builder.sslEnabled(true);
}
return builder.build();
}

Expand All @@ -169,17 +174,23 @@ private MongoCredential createCredential(Map<String, String> 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);
Expand Down

0 comments on commit 5bc85e4

Please sign in to comment.