Skip to content

Commit 357f400

Browse files
committed
merge
2 parents cd07ad2 + f919802 commit 357f400

3 files changed

Lines changed: 72 additions & 253 deletions

File tree

src/main/java/com/databricks/jdbc/dbclient/impl/common/ConfiguratorUtils.java

Lines changed: 1 addition & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,6 @@ public static PoolingHttpClientConnectionManager getBaseConnectionManager(
5151
SocketFactoryUtil.getTrustAllSocketFactoryRegistry());
5252
}
5353

54-
// If self-signed certificates are allowed, use a trust-all socket factory
55-
if (connectionContext.allowSelfSignedCerts()) {
56-
LOGGER.warn(
57-
"Self-signed certificates are allowed. Please only use this parameter (AllowSelfSignedCerts) when you're sure of what you're doing. This is not recommended for production use.");
58-
return new PoolingHttpClientConnectionManager(
59-
SocketFactoryUtil.getTrustAllSocketFactoryRegistry());
60-
}
61-
6254
// For standard SSL configuration, create a custom socket factory registry
6355
Registry<ConnectionSocketFactory> socketFactoryRegistry =
6456
createConnectionSocketFactoryRegistry(connectionContext);
@@ -75,59 +67,7 @@ public static PoolingHttpClientConnectionManager getBaseConnectionManager(
7567
public static Registry<ConnectionSocketFactory> createConnectionSocketFactoryRegistry(
7668
IDatabricksConnectionContext connectionContext) throws DatabricksHttpException {
7769

78-
// First check if a custom trust store is specified
79-
if (connectionContext.getSSLTrustStore() != null) {
80-
return createRegistryWithCustomTrustStore(connectionContext);
81-
} else {
82-
return createRegistryWithSystemOrDefaultTrustStore(connectionContext);
83-
}
84-
}
85-
86-
/**
87-
* Creates a socket factory registry using a custom trust store.
88-
*
89-
* @param connectionContext The connection context containing the trust store information.
90-
* @return A registry of connection socket factories.
91-
* @throws DatabricksHttpException If there is an error setting up the trust store.
92-
*/
93-
private static Registry<ConnectionSocketFactory> createRegistryWithCustomTrustStore(
94-
IDatabricksConnectionContext connectionContext) throws DatabricksHttpException {
95-
96-
try {
97-
KeyStore trustStore = loadTruststoreOrNull(connectionContext);
98-
if (trustStore == null) {
99-
String errorMessage =
100-
"Specified trust store could not be loaded: " + connectionContext.getSSLTrustStore();
101-
handleError(errorMessage, new IOException(errorMessage));
102-
}
103-
104-
// Get trust anchors from custom store
105-
Set<TrustAnchor> trustAnchors = getTrustAnchorsFromTrustStore(trustStore);
106-
if (trustAnchors.isEmpty()) {
107-
String errorMessage =
108-
"Custom trust store contains no trust anchors. Certificate validation will fail.";
109-
handleError(errorMessage, new KeyStoreException(errorMessage));
110-
}
111-
112-
LOGGER.info("Using custom trust store: " + connectionContext.getSSLTrustStore());
113-
114-
// Create trust managers from trust store
115-
TrustManager[] trustManagers =
116-
createTrustManagers(
117-
trustAnchors,
118-
connectionContext.checkCertificateRevocation(),
119-
connectionContext.acceptUndeterminedCertificateRevocation());
120-
121-
// Create socket factory registry
122-
return createSocketFactoryRegistry(trustManagers);
123-
} catch (DatabricksHttpException
124-
| NoSuchAlgorithmException
125-
| InvalidAlgorithmParameterException
126-
| KeyManagementException e) {
127-
handleError(
128-
"Error while setting up custom trust store: " + connectionContext.getSSLTrustStore(), e);
129-
}
130-
return null; // This will never be reached, but is required for method signature.
70+
return createRegistryWithSystemOrDefaultTrustStore(connectionContext);
13171
}
13272

13373
/**
@@ -357,57 +297,6 @@ private static X509TrustManager findX509TrustManager(TrustManager[] trustManager
357297
return null;
358298
}
359299

360-
/**
361-
* Loads a trust store from the path specified in the connection context.
362-
*
363-
* @param connectionContext The connection context containing trust store configuration.
364-
* @return The loaded KeyStore or null if it could not be loaded.
365-
* @throws DatabricksHttpException If there is an error during loading.
366-
*/
367-
public static KeyStore loadTruststoreOrNull(IDatabricksConnectionContext connectionContext)
368-
throws DatabricksHttpException {
369-
String trustStorePath = connectionContext.getSSLTrustStore();
370-
if (trustStorePath == null) {
371-
return null;
372-
}
373-
374-
// If the specified file doesn't exist, throw a specific error
375-
File trustStoreFile = new File(trustStorePath);
376-
if (!trustStoreFile.exists()) {
377-
String errorMessage = "Specified trust store file does not exist: " + trustStorePath;
378-
handleError(errorMessage, new IOException(errorMessage));
379-
}
380-
381-
char[] password = null;
382-
if (connectionContext.getSSLTrustStorePassword() != null) {
383-
password = connectionContext.getSSLTrustStorePassword().toCharArray();
384-
}
385-
386-
// Get the specified type, defaulting to JKS if not specified
387-
String trustStoreType = connectionContext.getSSLTrustStoreType();
388-
if (trustStoreType == null || trustStoreType.isEmpty()) {
389-
trustStoreType = "JKS"; // Default to JKS if not specified
390-
}
391-
392-
try (FileInputStream trustStoreStream = new FileInputStream(trustStorePath)) {
393-
LOGGER.info("Loading trust store as type: " + trustStoreType);
394-
KeyStore trustStore = KeyStore.getInstance(trustStoreType);
395-
trustStore.load(trustStoreStream, password);
396-
LOGGER.info("Successfully loaded trust store: " + trustStorePath);
397-
return trustStore;
398-
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
399-
String errorMessage =
400-
"Failed to load trust store: "
401-
+ trustStorePath
402-
+ " with type "
403-
+ trustStoreType
404-
+ ": "
405-
+ e.getMessage();
406-
handleError(errorMessage, e);
407-
}
408-
return null; // This will never be reached, but is required for method signature.
409-
}
410-
411300
/**
412301
* Extracts trust anchors from a KeyStore.
413302
*

src/test/java/com/databricks/client/jdbc/SSLTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,67 @@ public void testWithSystemTrustStore() {
289289
}
290290
}
291291

292+
@Test
293+
public void testDirectConnectionSystemTrustStoreFallback() {
294+
System.out.println(
295+
"Scenario: UseSystemTrustStore=1 with no system property -> fallback to cacerts (direct)");
296+
297+
// ensure the property is *unset* for this test run
298+
String savedProp = System.getProperty("javax.net.ssl.trustStore");
299+
try {
300+
System.clearProperty("javax.net.ssl.trustStore");
301+
302+
for (boolean thrift : new boolean[] {true, false}) {
303+
String url = buildJdbcUrl(thrift, false, false, false, true, false);
304+
try {
305+
verifyConnect(url);
306+
} catch (Exception e) {
307+
fail(
308+
"Fallback‑to‑cacerts direct connect failed (thrift="
309+
+ thrift
310+
+ "): "
311+
+ e.getMessage());
312+
}
313+
}
314+
} finally {
315+
// restore original system state
316+
if (savedProp != null) {
317+
System.setProperty("javax.net.ssl.trustStore", savedProp);
318+
}
319+
}
320+
}
321+
322+
@Test
323+
public void testIgnoreSystemPropertyWhenUseSystemTrustStoreDisabled() {
324+
System.out.println(
325+
"Scenario: bogus javax.net.ssl.trustStore present but UseSystemTrustStore=0 (driver must ignore)");
326+
327+
String savedProp = System.getProperty("javax.net.ssl.trustStore");
328+
try {
329+
System.setProperty("javax.net.ssl.trustStore", "/path/that/does/not/exist.jks");
330+
331+
for (boolean thrift : new boolean[] {true, false}) {
332+
String url = buildJdbcUrl(thrift, false, false, false, false, false);
333+
try {
334+
verifyConnect(url);
335+
} catch (Exception e) {
336+
fail(
337+
"Driver failed to ignore bogus system trust store (thrift="
338+
+ thrift
339+
+ "): "
340+
+ e.getMessage());
341+
}
342+
}
343+
} finally {
344+
// restore original value
345+
if (savedProp != null) {
346+
System.setProperty("javax.net.ssl.trustStore", savedProp);
347+
} else {
348+
System.clearProperty("javax.net.ssl.trustStore");
349+
}
350+
}
351+
}
352+
292353
@Test
293354
public void testWithCustomTrustStore() {
294355
System.out.println("Scenario: Testing with custom trust store");

0 commit comments

Comments
 (0)