Skip to content

Commit 7409993

Browse files
Reject non-SSLContext ssl_context value at point of use in createHttpClient
Per @chernser review: when the 'ssl_context' configuration value is present but not a javax.net.ssl.SSLContext instance, HttpAPIClientHelper.createHttpClient now throws ClientMisconfigurationException instead of silently falling back to building a context from trust/key material. A null value still builds from material and a real SSLContext is still used as-is. Implements: #2909
1 parent a1f8b04 commit 7409993

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,15 @@ public CloseableHttpClient createHttpClient(boolean initSslContext, Map<String,
285285
SSLContext sslContext = null;
286286
if (initSslContext) {
287287
Object customSSLContext = configuration.get(ClientConfigProperties.SSL_CONTEXT.getKey());
288-
sslContext = customSSLContext instanceof SSLContext
289-
? (SSLContext) customSSLContext
290-
: createSSLContext(configuration);
288+
if (customSSLContext == null) {
289+
sslContext = createSSLContext(configuration);
290+
} else if (customSSLContext instanceof SSLContext) {
291+
sslContext = (SSLContext) customSSLContext;
292+
} else {
293+
throw new ClientMisconfigurationException("'" + ClientConfigProperties.SSL_CONTEXT.getKey()
294+
+ "' must be a javax.net.ssl.SSLContext instance but was "
295+
+ customSSLContext.getClass().getName() + "; supply it via Client.Builder.setSSLContext(...)");
296+
}
291297
}
292298
LayeredConnectionSocketFactory sslConnectionSocketFactory;
293299
if (sslContext != null) {

client-v2/src/test/java/com/clickhouse/client/api/ClientBuilderTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,23 @@ public void testCreateSSLContextIgnoresCustomContext() throws Exception {
198198
}
199199
}
200200

201+
@Test
202+
public void testCreateHttpClientRejectsNonSSLContextValue() throws Exception {
203+
try (Client client = new Client.Builder()
204+
.addEndpoint("https://localhost:8443")
205+
.setUsername("default")
206+
.setPassword("")
207+
.build()) {
208+
HttpAPIClientHelper helper = extractHttpClientHelper(client);
209+
Map<String, Object> configWithBadContext = new HashMap<>(extractConfiguration(client));
210+
configWithBadContext.put(ClientConfigProperties.SSL_CONTEXT.getKey(), "not-a-context");
211+
// A non-SSLContext value under 'ssl_context' is a misconfiguration; createHttpClient must
212+
// reject it instead of silently building a context from trust/key material.
213+
Assert.expectThrows(ClientMisconfigurationException.class,
214+
() -> helper.createHttpClient(true, configWithBadContext));
215+
}
216+
}
217+
201218
private static HttpAPIClientHelper extractHttpClientHelper(Client client) throws Exception {
202219
Field helperField = Client.class.getDeclaredField("httpClientHelper");
203220
helperField.setAccessible(true);

0 commit comments

Comments
 (0)