Skip to content

feat(plugin-mongodb): TLS configuration support in MongoClientConfig#25374

Merged
imjalpreet merged 1 commit intoprestodb:masterfrom
imsayari404:mongodb_tls
Oct 27, 2025
Merged

feat(plugin-mongodb): TLS configuration support in MongoClientConfig#25374
imjalpreet merged 1 commit intoprestodb:masterfrom
imsayari404:mongodb_tls

Conversation

@imsayari404
Copy link
Contributor

@imsayari404 imsayari404 commented Jun 19, 2025

This PR introduces TLS support in the MongoDB connector

Description

Motivation and Context

Impact

Test Plan

presto> SELECT * FROM mongodb.tm_lakehouse_engine_db_2.aizen;
 emp_name | emp_location | emp_id | dep_id | u_id 
----------+--------------+--------+--------+------
 wade     | sanfransico  |      7 | AF62   |    2 
 Sam      | dallas       |     46 | Zf6    |    8 
 Root     | kochi        |     12 | AF6    |    4 
 tom      | mumbai       |      4 | Ax5    |    1 
 robert   | hyderabad    |      3 | Zf6    |    9 
 ivan     | bangalore    |     43 | BF4    |   10 
 Yan      | dubai        |      8 | BF4    |    3 
 Tom      | kochi        |     58 | AF6    |    6 
 Jorge    | Sydney       |     34 | Zf6    |    5 
 Bot      | bangalore    |     97 | BF4    |    7 
(10 rows)

Query 20250806_093152_00001_ujc79, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
[Latency: client-side: 0:01, server-side: 0:01] [10 rows, 390B] [8 rows/s, 329B/s]

Contributor checklist

  • Please make sure your submission complies with our contributing guide, in particular code style and commit standards.
  • PR description addresses the issue accurately and concisely. If the change is non-trivial, a GitHub Issue is referenced.
  • Documented new properties (with its default value), SQL syntax, functions, or other functionality.
  • If release notes are required, they follow the release notes guidelines.
  • Adequate tests were added if applicable.
  • CI passed.

Release Notes

Please follow release notes guidelines and fill in the release notes below.

== RELEASE NOTES ==

MongoDB Connector Changes
* Add TLS/SSL support with automatic JKS and PEM certificate format detection. Configure using ``mongodb.tls.enabled``, ``mongodb.tls.keystore-path``, 
  ``mongodb.tls.keystore-password``, ``mongodb.tls.truststore-path``, and ``mongodb.tls.truststore-password`` properties.
* Upgrade MongoDB Java Driver to 3.12.14.

@prestodb-ci prestodb-ci added the from:IBM PR from IBM label Jun 19, 2025
@imsayari404 imsayari404 marked this pull request as ready for review June 19, 2025 06:47
@imsayari404 imsayari404 requested a review from a team as a code owner June 19, 2025 06:47
@imsayari404 imsayari404 requested a review from hantangwangd June 19, 2025 06:47
@prestodb-ci prestodb-ci requested review from a team, agrawalreetika and wanglinsong and removed request for a team June 19, 2025 06:47
@imsayari404 imsayari404 requested a review from aaneja June 19, 2025 06:49
@prestodb-ci
Copy link
Contributor

@ethanyzhang imported this issue as lakehouse/presto #25374

Copy link
Member

@agrawalreetika agrawalreetika left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add required documentation entries with the new configs in Mongo connector documentation.

implements Module
{
private static final Logger log = Logger.get(MongoClientModule.class);
public static final String PROTOCOL = "SSL";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to enhance Security Protocol to TLS?

}
validateCertificates(keystore);
KeyManagerFactory keyManagerFactory =
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here use static import

return true;
}

public boolean getTlsEnabled()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public boolean getTlsEnabled()
public boolean isTlsEnabled()


/**
* @deprecated Use {@link #isTlsEnabled()} instead. This method is kept for backward compatibility.
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Little confused, What backward compatibility is being enforced with this method now if you are already pointing @LegacyConfig("mongodb.ssl.enabled")? Do we still need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right. Since we're using @LegacyConfig("mongodb.ssl.enabled") on the setTlsEnabled() method, the old SSL configuration will automatically be routed to the new TLS configuration. I will remove the deprecated methods.

{
return this.sslEnabled;
return this.tlsEnabled;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above?

return trustStore;
}

public static void validateCertificates(KeyStore keyStore) throws GeneralSecurityException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make it private?

return (X509TrustManager) trustManagers[0];
}

public static KeyStore loadTrustStore(File trustStorePath, Optional<String> trustStorePassword)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be private?

Comment on lines +98 to +103
Optional<SSLContext> sslContext = sslContextProvider.buildSslContext();
if (sslContext.isPresent()) {
options.sslContext(sslContext.get());
options.sslEnabled(true);
log.debug("SSL enabled for MongoDB client with TLS protocol");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Optional<SSLContext> sslContext = sslContextProvider.buildSslContext();
if (sslContext.isPresent()) {
options.sslContext(sslContext.get());
options.sslEnabled(true);
log.debug("SSL enabled for MongoDB client with TLS protocol");
}
sslContextProvider.buildSslContext()
.ifPresent(sslContext -> {
options.sslContext(sslContext);
options.sslEnabled(true);
});

<dependency>
<groupId>com.facebook.airlift</groupId>
<artifactId>security</artifactId>
</dependency>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this? and below pom changes?

<dependency>
<groupId>com.facebook.airlift</groupId>
<artifactId>security</artifactId>
<scope>provided</scope>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we marking this and below as scope provided?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The presto-spi pom changes are required because I've moved SslContextProvider to the spi package to make it reusable across connectors. The presto-mongodb dependency was added to resolve compilation issues when using SslContextProvider but I have removed them now with the new changes.

Copy link
Contributor Author

@imsayari404 imsayari404 Jun 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the presto-spi pom changes because SslContextProvider directly imports and uses Logger from airlift.log and PemReader from airlift.security.pem , without these dependencies, the code won't compile as shown in the compilation errors.

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project presto-spi: Compilation failure: Compilation failure: 
[ERROR] /Users/sayarimukherjee/Desktop/os-presto/presto/presto-spi/src/main/java/com/facebook/presto/spi/security/SslContextProvider.java:[16,32] package com.facebook.airlift.log does not exist
[ERROR] /Users/sayarimukherjee/Desktop/os-presto/presto/presto-spi/src/main/java/com/facebook/presto/spi/security/SslContextProvider.java:[17,41] package com.facebook.airlift.security.pem does not exist
[ERROR] /Users/sayarimukherjee/Desktop/os-presto/presto/presto-spi/src/main/java/com/facebook/presto/spi/security/SslContextProvider.java:[54,26] cannot find symbol
[ERROR]   symbol:   class Logger
[ERROR]   location: class com.facebook.presto.spi.security.SslContextProvider
[ERROR] /Users/sayarimukherjee/Desktop/os-presto/presto/presto-spi/src/main/java/com/facebook/presto/spi/security/SslContextProvider.java:[54,39] cannot find symbol
[ERROR]   symbol:   variable Logger
[ERROR]   location: class com.facebook.presto.spi.security.SslContextProvider
[ERROR] /Users/sayarimukherjee/Desktop/os-presto/presto/presto-spi/src/main/java/com/facebook/presto/spi/security/SslContextProvider.java:[132,24] cannot find symbol
[ERROR]   symbol:   variable PemReader
[ERROR]   location: class com.facebook.presto.spi.security.SslContextProvider
[ERROR] /Users/sayarimukherjee/Desktop/os-presto/presto/presto-spi/src/main/java/com/facebook/presto/spi/security/SslContextProvider.java:[177,54] cannot find symbol
[ERROR]   symbol:   variable PemReader
[ERROR]   location: class com.facebook.presto.spi.security.SslContextProvider
[ERROR] -> [Help 1]

@steveburnett
Copy link
Contributor

@imsayari404
Copy link
Contributor Author

imsayari404 commented Jun 30, 2025

Does https://github.com/prestodb/presto/blob/master/presto-docs/src/main/sphinx/connector/mongodb.rst need any documentation update for this?

@steveburnett , yes the documentation will need to be updated. I'm currently working on addressing the review feedback from @agrawalreetika and will include the necessary documentation updates for all the new TLS configuration properties in the next commit. This will include proper documentation for the new mongodb.tls.* configs and deprecation notices for the old SSL setting. I'll make sure to get these changes pushed soon.

Copy link
Member

@agrawalreetika agrawalreetika left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making the changes. Overall LGTM.

Please add the documentation & squash all your commits into one.

Comment on lines +84 to +87
keystorePath != null ||
keystorePassword != null ||
truststorePath != null ||
truststorePassword != null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we checking these here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added to handle cases where users provide TLS-related configurations like mongodb.tls.keystore-path but forget to explicitly enable mongodb.tls.enabled=true. Automatically enabling TLS in such cases helps prevent common configuration mistakes. However, I'm happy to remove or adjust this logic if you think it’s unnecessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I see the issue now. The current logic in isTlsEnabled() is problematic because it automatically enables TLS whenever any TLS-related properties are set, regardless of the mongodb.tls.enabled flag. This could lead to unexpected behavior.
I'll simplify this to just return this.tlsEnabled and remove the automatic detection logic.

Copy link
Member

@agrawalreetika agrawalreetika left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@imsayari404 There are compilation failures, please fix those, add the documentation & squash all your commits into one.

@imsayari404 imsayari404 force-pushed the mongodb_tls branch 3 times, most recently from f6d48f7 to e89a22c Compare July 4, 2025 15:11
@imsayari404
Copy link
Contributor Author

@imsayari404 There are compilation failures, please fix those, add the documentation & squash all your commits into one.

@agrawalreetika The updated changes are now pushed to the branch.

@imsayari404 imsayari404 force-pushed the mongodb_tls branch 2 times, most recently from e025cf6 to d1b46b5 Compare August 20, 2025 16:36
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just want to make sure that this will expire very far into the future?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or we could generate it programmatically? I just remembered we were trying it for HMS SSL tests: #25313. Looks like I forgot to review it further but that could be one alternative.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use programmatic certificate generation similar to the Hive SSL tests (#25313).

@imsayari404
Copy link
Contributor Author

@tdcmeehan, could you please review this PR at your convenience?

tdcmeehan
tdcmeehan previously approved these changes Aug 22, 2025
@tdcmeehan tdcmeehan dismissed their stale review August 22, 2025 19:36

Needs squash

@tdcmeehan
Copy link
Contributor

@imjalpreet PTAL.

@imsayari404 please squash commits.

@imsayari404
Copy link
Contributor Author

Thanks @tdcmeehan , I've squashed the commits
@imjalpreet, could you please review this PR at your convenience?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are using the same implementation, I will finish the review of #25313 and get it added to a common module so that we can re-use across plugins.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay @imjalpreet, thank you!

tdcmeehan
tdcmeehan previously approved these changes Aug 25, 2025
Copy link
Member

@imjalpreet imjalpreet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@imsayari404, I don't see the new test for TLS I was reviewing earlier. Can you please take a look?

Copy link
Member

@imjalpreet imjalpreet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @imsayari404. I did another pass. One question: Is it possible to add a test for TLS specifically for the MongoDB connector?

Comment on lines +196 to +200
SslContextProvider sslContextProvider = new SslContextProvider(
config.getKeystorePath(),
config.getKeystorePassword(),
config.getTruststorePath(),
config.getTruststorePassword());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's create a method for creating this object and reuse

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated

SslKeystoreManager.initializeKeystoreAndTruststore();

keystoreFile = new File(SslKeystoreManager.getKeystorePath());
truststoreFile = new File(SslKeystoreManager.getTruststorePath());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use static imports

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated

Copy link
Member

@agrawalreetika agrawalreetika left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly lgtm, just few nits

Cherry-pick of commits in https://github.ibm.com/lakehouse/presto/pull/447/commits

Commits picked -
d2c50df
7c6a8b1

Co-authored-by: Anant Aneja <1797669+aaneja@users.noreply.github.com>
Copy link
Member

@agrawalreetika agrawalreetika left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Member

@imjalpreet imjalpreet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @imsayari404. LGTM now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

from:IBM PR from IBM

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants