-
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
base: main
Are you sure you want to change the base?
Changes from all commits
0b5776d
767fc60
e6faeea
12e3b74
56b5b61
86cd9ec
ea14e4f
ae870c1
683dd25
8babe73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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?
Contributor
Author
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.
|
||
| if (mode.name().equalsIgnoreCase(value)) { | ||
| return mode; | ||
| } | ||
| } | ||
| throw new IllegalArgumentException("Unknown SSL mode '" + value + "'"); | ||
| } | ||
| } | ||
| 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
Contributor
Author
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. This may be need to be a new
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. Are you planning to change on a different PR? |
||
| } | ||
|
|
||
| if (cause instanceof ConnectionRequestTimeoutException || | ||
| cause instanceof NoHttpResponseException || | ||
| cause instanceof ConnectTimeoutException || | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.