|
19 | 19 | import java.security.cert.Certificate; |
20 | 20 | import java.security.cert.CertificateException; |
21 | 21 | import java.security.cert.CertificateFactory; |
| 22 | +import java.security.cert.X509Certificate; |
22 | 23 | import java.util.Collection; |
| 24 | +import javax.net.ssl.HostnameVerifier; |
23 | 25 | import javax.net.ssl.SSLContext; |
| 26 | +import javax.net.ssl.TrustManager; |
24 | 27 | import javax.net.ssl.TrustManagerFactory; |
| 28 | +import javax.net.ssl.X509TrustManager; |
25 | 29 | import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy; |
26 | | -import org.apache.hc.client5.http.ssl.HostnameVerificationPolicy; |
27 | 30 | import org.apache.hc.client5.http.ssl.HttpsSupport; |
| 31 | +import org.apache.hc.client5.http.ssl.TlsSocketStrategy; |
28 | 32 | import org.apache.iceberg.CatalogProperties; |
29 | 33 | import org.apache.iceberg.catalog.SessionCatalog; |
30 | 34 | import org.apache.iceberg.rest.HTTPClient; |
31 | 35 | import org.apache.iceberg.rest.RESTCatalog; |
32 | 36 |
|
33 | 37 | public class RESTCatalogFactory { |
34 | 38 |
|
35 | | - public static RESTCatalog create(byte[] caCrt) { |
36 | | - if (caCrt == null) { |
| 39 | + public static RESTCatalog create(byte[] caCrt, boolean sslVerify) { |
| 40 | + if (caCrt == null && sslVerify) { |
37 | 41 | return new RESTCatalog(); |
38 | 42 | } |
39 | 43 | SSLContext sslContext; |
40 | 44 | try { |
41 | | - sslContext = loadCABundle(caCrt); |
| 45 | + if (!sslVerify) { |
| 46 | + sslContext = createInsecureSSLContext(); |
| 47 | + } else { |
| 48 | + sslContext = loadCABundle(caCrt); |
| 49 | + } |
42 | 50 | } catch (CertificateException |
43 | 51 | | KeyStoreException |
44 | 52 | | IOException |
45 | 53 | | NoSuchAlgorithmException |
46 | 54 | | KeyManagementException e) { |
47 | 55 | throw new RuntimeException(e); |
48 | 56 | } |
49 | | - var tlsSocketStrategy = |
50 | | - new DefaultClientTlsStrategy( |
51 | | - sslContext, HostnameVerificationPolicy.BOTH, HttpsSupport.getDefaultHostnameVerifier()); |
| 57 | + HostnameVerifier hostnameVerifier = |
| 58 | + sslVerify ? HttpsSupport.getDefaultHostnameVerifier() : (hostname, session) -> true; |
| 59 | + TlsSocketStrategy tlsSocketStrategy = |
| 60 | + new DefaultClientTlsStrategy(sslContext, hostnameVerifier); |
52 | 61 | return new RESTCatalog( |
53 | 62 | SessionCatalog.SessionContext.createEmpty(), |
54 | 63 | x -> |
@@ -80,4 +89,23 @@ private static SSLContext loadCABundle(byte[] caCrt) |
80 | 89 | sslContext.init(null, tmf.getTrustManagers(), new SecureRandom()); |
81 | 90 | return sslContext; |
82 | 91 | } |
| 92 | + |
| 93 | + private static SSLContext createInsecureSSLContext() |
| 94 | + throws NoSuchAlgorithmException, KeyManagementException { |
| 95 | + TrustManager[] trustAllCerts = |
| 96 | + new TrustManager[] { |
| 97 | + new X509TrustManager() { |
| 98 | + public X509Certificate[] getAcceptedIssuers() { |
| 99 | + return new X509Certificate[0]; |
| 100 | + } |
| 101 | + |
| 102 | + public void checkClientTrusted(X509Certificate[] certs, String authType) {} |
| 103 | + |
| 104 | + public void checkServerTrusted(X509Certificate[] certs, String authType) {} |
| 105 | + } |
| 106 | + }; |
| 107 | + SSLContext sslContext = SSLContext.getInstance("TLS"); |
| 108 | + sslContext.init(null, trustAllCerts, new SecureRandom()); |
| 109 | + return sslContext; |
| 110 | + } |
83 | 111 | } |
0 commit comments