99import com .databricks .jdbc .client .DatabricksRetryHandlerException ;
1010import com .databricks .jdbc .client .IDatabricksHttpClient ;
1111import com .databricks .jdbc .commons .LogLevel ;
12+ import com .databricks .jdbc .commons .util .HttpExecuteExceptionUtil ;
1213import com .databricks .jdbc .commons .util .LoggingUtil ;
1314import com .databricks .jdbc .driver .IDatabricksConnectionContext ;
1415import com .databricks .sdk .core .UserAgent ;
1516import com .google .common .annotations .VisibleForTesting ;
1617import java .io .IOException ;
18+ import java .security .KeyManagementException ;
19+ import java .security .KeyStoreException ;
20+ import java .security .NoSuchAlgorithmException ;
1721import java .util .Objects ;
1822import java .util .Set ;
1923import java .util .concurrent .ConcurrentHashMap ;
2024import java .util .concurrent .TimeUnit ;
25+ import javax .net .ssl .SSLContext ;
2126import org .apache .http .HttpException ;
2227import org .apache .http .HttpHost ;
2328import org .apache .http .HttpResponse ;
3035import org .apache .http .client .methods .HttpUriRequest ;
3136import org .apache .http .conn .UnsupportedSchemeException ;
3237import org .apache .http .conn .routing .HttpRoute ;
38+ import org .apache .http .conn .ssl .NoopHostnameVerifier ;
3339import org .apache .http .impl .client .BasicCredentialsProvider ;
3440import org .apache .http .impl .client .CloseableHttpClient ;
3541import org .apache .http .impl .client .HttpClientBuilder ;
3642import org .apache .http .impl .client .ProxyAuthenticationStrategy ;
3743import org .apache .http .impl .conn .DefaultSchemePortResolver ;
3844import org .apache .http .impl .conn .PoolingHttpClientConnectionManager ;
3945import org .apache .http .protocol .HttpContext ;
46+ import org .apache .http .ssl .SSLContextBuilder ;
4047
4148/** Http client implementation to be used for executing http requests. */
4249public class DatabricksHttpClient implements IDatabricksHttpClient {
@@ -76,6 +83,7 @@ public class DatabricksHttpClient implements IDatabricksHttpClient {
7683 private static boolean shouldRetryRateLimitError ;
7784 private static int rateLimitRetryTimeout ;
7885 protected static int idleHttpConnectionExpiry ;
86+ private CloseableHttpClient httpDisabledSSLClient ;
7987
8088 private DatabricksHttpClient (IDatabricksConnectionContext connectionContext ) {
8189 initializeConnectionManager ();
@@ -85,6 +93,7 @@ private DatabricksHttpClient(IDatabricksConnectionContext connectionContext) {
8593 shouldRetryRateLimitError = connectionContext .shouldRetryRateLimitError ();
8694 rateLimitRetryTimeout = connectionContext .getRateLimitRetryTimeout ();
8795 httpClient = makeClosableHttpClient (connectionContext );
96+ httpDisabledSSLClient = makeClosableDisabledSslHttpClient ();
8897 idleHttpConnectionExpiry = connectionContext .getIdleHttpConnectionExpiry ();
8998 }
9099
@@ -151,6 +160,23 @@ private CloseableHttpClient makeClosableHttpClient(
151160 return builder .build ();
152161 }
153162
163+ private CloseableHttpClient makeClosableDisabledSslHttpClient () {
164+ try {
165+ // Create SSL context that trusts all certificates
166+ SSLContext sslContext =
167+ new SSLContextBuilder ().loadTrustMaterial (null , (chain , authType ) -> true ).build ();
168+
169+ // Create HttpClient with the SSL context
170+ return HttpClientBuilder .create ()
171+ .setSSLContext (sslContext )
172+ .setSSLHostnameVerifier (new NoopHostnameVerifier ())
173+ .build ();
174+ } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e ) {
175+ System .out .println ("Error in creating HttpClient with the SSL context" );
176+ }
177+ return null ;
178+ }
179+
154180 private boolean handleRetry (IOException exception , int executionCount , HttpContext context ) {
155181 int errCode = getErrorCode (exception );
156182 if (!isErrorCodeRetryable (errCode )) {
@@ -342,20 +368,22 @@ public CloseableHttpResponse execute(HttpUriRequest request) throws DatabricksHt
342368 try {
343369 return httpClient .execute (request );
344370 } catch (IOException e ) {
345- Throwable cause = e ;
346- while (cause != null ) {
347- if (cause instanceof DatabricksRetryHandlerException ) {
348- throw new DatabricksHttpException (cause .getMessage (), cause );
349- }
350- cause = cause .getCause ();
351- }
352- String errorMsg =
353- String .format (
354- "Caught error while executing http request: [%s]. Error Message: [%s]" ,
355- RequestSanitizer .sanitizeRequest (request ), e );
356- LoggingUtil .log (LogLevel .ERROR , errorMsg );
357- throw new DatabricksHttpException (errorMsg , e );
371+ HttpExecuteExceptionUtil .throwException (e , request );
372+ }
373+ return null ;
374+ }
375+
376+ public CloseableHttpResponse executeWithoutSSL (HttpUriRequest request )
377+ throws DatabricksHttpException {
378+ LoggingUtil .log (
379+ LogLevel .DEBUG ,
380+ String .format ("Executing HTTP request [{%s}]" , RequestSanitizer .sanitizeRequest (request )));
381+ try {
382+ return httpDisabledSSLClient .execute (request );
383+ } catch (Exception e ) {
384+ HttpExecuteExceptionUtil .throwException (e , request );
358385 }
386+ return null ;
359387 }
360388
361389 @ Override
0 commit comments