|
5 | 5 | import com.databricks.sdk.core.DatabricksException; |
6 | 6 | import java.security.SecureRandom; |
7 | 7 | import java.security.cert.X509Certificate; |
8 | | -import javax.net.ssl.HostnameVerifier; |
9 | 8 | import javax.net.ssl.SSLContext; |
10 | 9 | import javax.net.ssl.TrustManager; |
11 | 10 | import javax.net.ssl.X509TrustManager; |
12 | 11 | import org.apache.http.config.Registry; |
13 | 12 | import org.apache.http.config.RegistryBuilder; |
14 | 13 | import org.apache.http.conn.socket.ConnectionSocketFactory; |
15 | 14 | import org.apache.http.conn.socket.PlainConnectionSocketFactory; |
| 15 | +import org.apache.http.conn.ssl.NoopHostnameVerifier; |
16 | 16 | import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
17 | 17 |
|
18 | 18 | public class SocketFactoryUtil { |
19 | | - |
20 | 19 | private static final JdbcLogger LOGGER = JdbcLoggerFactory.getLogger(SocketFactoryUtil.class); |
21 | 20 |
|
22 | 21 | /** |
23 | | - * <b>NOTE: </b> Only for testing purposes and should never be used in production. |
24 | | - * |
25 | | - * <p>Builds a registry of connection socket factories that trusts all SSL certificates. |
| 22 | + * Builds a registry of connection socket factories that trusts all SSL certificates. This should |
| 23 | + * only be used in testing environments or when explicitly configured to allow self-signed |
| 24 | + * certificates. |
26 | 25 | * |
27 | 26 | * @return A registry of connection socket factories. |
28 | 27 | */ |
29 | 28 | public static Registry<ConnectionSocketFactory> getTrustAllSocketFactoryRegistry() { |
30 | 29 | LOGGER.warn( |
31 | | - "This driver is configured to trust all SSL certificates. This is insecure and should be never used in production."); |
32 | | - LOGGER.debug("Entering the getTrustAllSocketFactoryRegistry method"); |
33 | | - |
| 30 | + "This driver is configured to trust all SSL certificates. This is insecure and should never be used in production."); |
34 | 31 | try { |
35 | 32 | // Create a TrustManager that trusts all certificates |
36 | | - TrustManager[] trustAllCerts = |
37 | | - new TrustManager[] { |
38 | | - new X509TrustManager() { |
39 | | - @Override |
40 | | - public X509Certificate[] getAcceptedIssuers() { |
41 | | - return null; // Accept all issuers |
42 | | - } |
43 | | - |
44 | | - @Override |
45 | | - public void checkClientTrusted(X509Certificate[] certs, String authType) { |
46 | | - // No-op: Trust all client certificates |
47 | | - } |
48 | | - |
49 | | - @Override |
50 | | - public void checkServerTrusted(X509Certificate[] certs, String authType) { |
51 | | - // No-op: Trust all server certificates |
52 | | - } |
53 | | - } |
54 | | - }; |
| 33 | + TrustManager[] trustAllCerts = getTrustManagerThatTrustsAllCertificates(); |
55 | 34 |
|
56 | 35 | // Initialize the SSLContext with trust-all settings |
57 | 36 | SSLContext sslContext = SSLContext.getInstance("TLS"); |
58 | 37 | sslContext.init(null, trustAllCerts, new SecureRandom()); |
59 | 38 |
|
60 | | - // Disable hostname verification |
61 | | - HostnameVerifier allHostsValid = (hostname, session) -> true; |
62 | | - |
63 | | - // Configure SSLConnectionSocketFactory with the trust-all SSLContext |
| 39 | + // Use the NoopHostnameVerifier to disable hostname verification |
64 | 40 | SSLConnectionSocketFactory sslSocketFactory = |
65 | | - new SSLConnectionSocketFactory(sslContext, allHostsValid); |
| 41 | + new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); |
66 | 42 |
|
67 | 43 | // Build and return the registry |
68 | 44 | return RegistryBuilder.<ConnectionSocketFactory>create() |
69 | 45 | .register("https", sslSocketFactory) |
70 | 46 | .register("http", new PlainConnectionSocketFactory()) |
71 | 47 | .build(); |
72 | | - |
73 | 48 | } catch (Exception e) { |
74 | 49 | String errorMessage = "Error while setting up trust-all SSL context."; |
75 | | - LOGGER.error(errorMessage, e); |
| 50 | + LOGGER.error(e, errorMessage); |
76 | 51 | throw new DatabricksException(errorMessage, e); |
77 | 52 | } |
78 | 53 | } |
| 54 | + |
| 55 | + /** |
| 56 | + * Creates a TrustManager array that accepts all certificates without validation. This should only |
| 57 | + * be used in testing environments or when explicitly configured to allow self-signed |
| 58 | + * certificates. |
| 59 | + * |
| 60 | + * @return An array containing a single TrustManager that trusts all certificates. |
| 61 | + */ |
| 62 | + public static TrustManager[] getTrustManagerThatTrustsAllCertificates() { |
| 63 | + return new TrustManager[] { |
| 64 | + new X509TrustManager() { |
| 65 | + @Override |
| 66 | + public X509Certificate[] getAcceptedIssuers() { |
| 67 | + return new X509Certificate[0]; // Empty array instead of null for better compatibility |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void checkClientTrusted(X509Certificate[] certs, String authType) { |
| 72 | + // No-op: Trust all client certificates |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void checkServerTrusted(X509Certificate[] certs, String authType) { |
| 77 | + // No-op: Trust all server certificates |
| 78 | + } |
| 79 | + } |
| 80 | + }; |
| 81 | + } |
79 | 82 | } |
0 commit comments