Skip to content

Commit

Permalink
add ssl context to mongoClientOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroVega committed Jun 18, 2024
1 parent 6b46e17 commit 171b7c1
Showing 1 changed file with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLContext;
import java.security.NoSuchAlgorithmException;
import org.bson.Document;

/**
Expand Down Expand Up @@ -588,6 +590,14 @@ private MongoDatabase getDatabase(String dbName) {
// create a Mongo client

if (client == null) {
SSLContext sslContext = null;
if (sslEnabled) {
try {
sslContext = SSLContext.getInstance("TLS");
} catch (NoSuchAlgorithmException e) {
LOGGER.warn("Error with TLS algorithm " + e.getMessage());
}
}
if (mongoUsername.length() != 0) {
String authSource;
if ((mongoAuthSource != null) && !mongoAuthSource.isEmpty()) {
Expand All @@ -608,17 +618,20 @@ private MongoDatabase getDatabase(String dbName) {
requiredReplicaSetName(mongoReplicaSet).
sslEnabled(sslEnabled).
sslInvalidHostNameAllowed(sslInvalidHostNameAllowed).
sslContext(sslContext).
build());
} else {
client = new MongoClient(servers, credential, new MongoClientOptions.Builder().
sslEnabled(sslEnabled).
sslInvalidHostNameAllowed(sslInvalidHostNameAllowed).
sslContext(sslContext).
build());
}
} else {
MongoClientOptions options = MongoClientOptions.builder()
.sslEnabled(sslEnabled)
.sslInvalidHostNameAllowed(sslInvalidHostNameAllowed)
.sslContext(sslContext)
.build();
client = new MongoClient(servers, options);
} // if else
Expand Down

0 comments on commit 171b7c1

Please sign in to comment.