-
Notifications
You must be signed in to change notification settings - Fork 623
[client-v2, jdbc-v2] Implement SSL Modes #2874
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chernser
wants to merge
9
commits into
main
Choose a base branch
from
06/10/26/ssl_modes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0b5776d
Added ssl_mode to client-v2 and jdbc-v2
chernser 767fc60
Added examples
chernser e6faeea
Merge branch '06/10/26/certs_as_string' into 06/10/26/ssl_modes
chernser 12e3b74
updated tests and docs
chernser 56b5b61
Merge branch 'main' into 06/10/26/ssl_modes
chernser 86cd9ec
Merge branch 'main' into 06/10/26/ssl_modes
chernser ea14e4f
Made building ssl context in builder way. Renamed SSLMode constants t…
chernser ae870c1
Fixed problem of disabled verification
chernser 683dd25
Added test to verify that strict mode throws exception with no trust …
chernser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
client-v2/src/main/java/com/clickhouse/client/api/enums/SSLMode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package com.clickhouse.client.api.enums; | ||
|
|
||
| /** | ||
| * Defines how strictly the client verifies a server identity when a secure protocol is used. | ||
| * | ||
| * <p>The mode affects only connections that are already using a secure transport (for example, | ||
| * an {@code https://} endpoint). It does <b>not</b> enable encryption for plain protocols - an | ||
| * {@code http://} endpoint stays unencrypted whatever the mode is.</p> | ||
| * | ||
| * <p>Modes from the least to the most strict:</p> | ||
| * <ul> | ||
| * <li>{@link #DISABLED} - SSL is not used. Plain protocols only.</li> | ||
| * <li>{@link #TRUST} - the hostname is not verified and any server certificate is accepted, which | ||
| * is susceptible to MITM attacks - use that only for testing or in fully trusted environments. A | ||
| * configured trust store or CA certificate has no effect in this mode and is ignored (a warning is | ||
| * logged); a configured client certificate/key is still applied for mTLS.</li> | ||
| * <li>{@link #VERIFY_CA} - the server certificate chain is validated against the trust material | ||
| * (default JVM trust store, configured trust store, or a CA certificate), but the hostname is | ||
| * not checked against the certificate.</li> | ||
| * <li>{@link #STRICT} - full verification (default): certificate chain is validated and the | ||
| * hostname must match the certificate.</li> | ||
| * </ul> | ||
| */ | ||
| public enum SSLMode { | ||
|
|
||
| /** | ||
| * SSL is not used. Connection is not encrypted. Doesn't work with HTTPS. | ||
| * Reserved for TCP where protocol doesn't define encryption. | ||
| */ | ||
| DISABLED, | ||
|
|
||
| /** | ||
| * The hostname is not verified and any server certificate is accepted. A configured trust store or | ||
| * CA certificate has no effect in this mode and is ignored (a warning is logged). A configured | ||
| * client certificate/key is still applied for mTLS. | ||
| */ | ||
| TRUST, | ||
|
|
||
| /** | ||
| * Server certificate chain is validated, but the hostname is not verified. | ||
| */ | ||
| VERIFY_CA, | ||
|
|
||
| /** | ||
| * Full verification: certificate chain is validated and the hostname must match | ||
| * the certificate. Default mode for HTTPs. | ||
| */ | ||
| STRICT; | ||
|
|
||
| /** | ||
| * Case-insensitive variant of {@link #valueOf(String)}. | ||
| * | ||
| * @param value mode name in any case | ||
| * @return matching mode | ||
| * @throws IllegalArgumentException when the value does not match any mode | ||
| */ | ||
| public static SSLMode fromValue(String value) { | ||
| for (SSLMode mode : values()) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use a predefined map? |
||
| if (mode.name().equalsIgnoreCase(value)) { | ||
| return mode; | ||
| } | ||
| } | ||
| throw new IllegalArgumentException("Unknown SSL mode '" + value + "'"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,9 +11,9 @@ | |
| import com.clickhouse.client.api.DataTransferException; | ||
| import com.clickhouse.client.api.ServerException; | ||
| import com.clickhouse.client.api.enums.ProxyType; | ||
| import com.clickhouse.client.api.enums.SSLMode; | ||
| import com.clickhouse.client.api.http.ClickHouseHttpProto; | ||
| import com.clickhouse.client.api.transport.Endpoint; | ||
| import com.clickhouse.client.config.ClickHouseDefaultSslContextProvider; | ||
| import com.clickhouse.data.ClickHouseFormat; | ||
| import net.jpountz.lz4.LZ4Factory; | ||
| import org.apache.commons.compress.compressors.CompressorStreamFactory; | ||
|
|
@@ -85,7 +85,6 @@ | |
| import java.net.URLEncoder; | ||
| import java.net.UnknownHostException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.security.NoSuchAlgorithmException; | ||
| import java.util.Arrays; | ||
| import java.util.Base64; | ||
| import java.util.Collection; | ||
|
|
@@ -131,7 +130,7 @@ public class HttpAPIClientHelper { | |
|
|
||
| LZ4Factory lz4Factory; | ||
|
|
||
| private final ClickHouseDefaultSslContextProvider sslContextProvider = new ClickHouseDefaultSslContextProvider(); | ||
| private final SslContextProvider sslContextProvider = new SslContextProvider(); | ||
|
|
||
| public HttpAPIClientHelper(Map<String, Object> configuration, Object metricsRegistry, boolean initSslContext, LZ4Factory lz4Factory) { | ||
| this.metricsRegistry = metricsRegistry; | ||
|
|
@@ -159,34 +158,46 @@ public HttpAPIClientHelper(Map<String, Object> configuration, Object metricsRegi | |
| * @return SSLContext | ||
| */ | ||
| public SSLContext createSSLContext(Map<String, Object> configuration) { | ||
| SSLContext sslContext; | ||
| try { | ||
| sslContext = SSLContext.getDefault(); | ||
| } catch (NoSuchAlgorithmException e) { | ||
| throw new ClientException("Failed to create default SSL context", e); | ||
| } | ||
| final SSLMode sslMode = ClientConfigProperties.SSL_MODE.getOrDefault(configuration); | ||
| final String trustStorePath = (String) configuration.get(ClientConfigProperties.SSL_TRUST_STORE.getKey()); | ||
| final String caCertificate = (String) configuration.get(ClientConfigProperties.CA_CERTIFICATE.getKey()); | ||
| final String sslCertificate = (String) configuration.get(ClientConfigProperties.SSL_CERTIFICATE.getKey()); | ||
| final String sslKey = (String) configuration.get(ClientConfigProperties.SSL_KEY.getKey()); | ||
| if (trustStorePath != null) { | ||
| try { | ||
| sslContext = sslContextProvider.getSslContextFromKeyStore( | ||
| trustStorePath, | ||
| (String) configuration.get(ClientConfigProperties.SSL_KEY_STORE_PASSWORD.getKey()), | ||
| (String) configuration.get(ClientConfigProperties.SSL_KEYSTORE_TYPE.getKey()) | ||
| ); | ||
| } catch (SSLException e) { | ||
| throw new ClientMisconfigurationException("Failed to create SSL context from a keystore", e); | ||
|
|
||
| SslContextProvider.Builder builder = sslContextProvider.builder(); | ||
|
|
||
| // The client certificate/key (mTLS) are independent of how the server certificate is verified, | ||
| // so they are applied whenever configured, regardless of the SSL mode. | ||
| if (sslCertificate != null && !sslCertificate.isEmpty()) { | ||
| builder.clientCertificate(sslCertificate, sslKey); | ||
| } | ||
|
|
||
| if (sslMode == SSLMode.TRUST) { | ||
| // TRUST accepts any server certificate and skips the hostname check (the latter is applied | ||
| // where the connection socket factory is created). A configured trust store or CA | ||
| // certificate has no effect in this mode and is ignored with a warning. | ||
| if (trustStorePath != null || caCertificate != null) { | ||
| LOG.warn("SSL mode '{}' trusts any server certificate; the configured {} is ignored.", | ||
| SSLMode.TRUST, trustStorePath != null ? "trust store" : "CA certificate"); | ||
| } | ||
| } else if (caCertificate != null || sslCertificate != null|| sslKey != null) { | ||
| try { | ||
| sslContext = sslContextProvider.getSslContextFromCerts(sslCertificate, sslKey, caCertificate); | ||
| } catch (SSLException e) { | ||
| throw new ClientMisconfigurationException("Failed to create SSL context from certificates", e); | ||
| builder.trustAllCertificates(); | ||
| } else if (trustStorePath != null) { | ||
| // VERIFY_CA / STRICT: validate against the trust store. A trust store and a CA certificate | ||
| // cannot both take effect, so the CA certificate is ignored with a warning. | ||
| if (caCertificate != null) { | ||
| LOG.warn("Both a trust store and a CA certificate are configured; using the trust store and" | ||
| + " ignoring the CA certificate. Import the CA certificate into the trust store instead."); | ||
| } | ||
| builder.trustStore(trustStorePath, | ||
| (String) configuration.get(ClientConfigProperties.SSL_KEY_STORE_PASSWORD.getKey()), | ||
| (String) configuration.get(ClientConfigProperties.SSL_KEYSTORE_TYPE.getKey())); | ||
| } else if (caCertificate != null) { | ||
| // VERIFY_CA / STRICT: validate against the CA certificate. | ||
| builder.rootCertificate(caCertificate); | ||
| } | ||
| return sslContext; | ||
| // else VERIFY_CA / STRICT with no trust material: the JVM default trust store is used. | ||
|
|
||
| return builder.build(); | ||
| } | ||
|
|
||
| private static final long CONNECTION_INACTIVITY_CHECK = 5000L; | ||
|
|
@@ -272,7 +283,11 @@ public CloseableHttpClient createHttpClient(boolean initSslContext, Map<String, | |
| LayeredConnectionSocketFactory sslConnectionSocketFactory; | ||
| if (sslContext != null) { | ||
| String socketSNI = (String)configuration.get(ClientConfigProperties.SSL_SOCKET_SNI.getKey()); | ||
| if (socketSNI != null && !socketSNI.trim().isEmpty()) { | ||
| SSLMode sslMode = ClientConfigProperties.SSL_MODE.getOrDefault(configuration); | ||
| // Trust and VerifyCa skip hostname verification. The same applies when a custom SNI is | ||
| // set because the connection hostname will not match the certificate. | ||
| boolean trustAllHostnames = sslMode == SSLMode.TRUST || sslMode == SSLMode.VERIFY_CA; | ||
| if (socketSNI != null && !socketSNI.trim().isEmpty() || trustAllHostnames) { | ||
| sslConnectionSocketFactory = new CustomSSLConnectionFactory(socketSNI, sslContext, (hostname, session) -> true); | ||
| } else { | ||
| sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext); | ||
|
|
@@ -880,6 +895,10 @@ public RuntimeException wrapException(String message, Exception cause, String qu | |
| return (RuntimeException) cause; | ||
| } | ||
|
|
||
| if (cause instanceof SSLException) { | ||
| return new ClickHouseException("SSL Problem", cause, queryId); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we need to change it to ClientException |
||
| } | ||
|
|
||
| if (cause instanceof ConnectionRequestTimeoutException || | ||
| cause instanceof NoHttpResponseException || | ||
| cause instanceof ConnectTimeoutException || | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.