1616import com .databricks .sdk .core .utils .ProxyUtils ;
1717import com .google .common .annotations .VisibleForTesting ;
1818import java .io .IOException ;
19+ import java .security .KeyManagementException ;
20+ import java .security .KeyStoreException ;
21+ import java .security .NoSuchAlgorithmException ;
1922import java .util .Objects ;
2023import java .util .Set ;
2124import java .util .concurrent .ConcurrentHashMap ;
2225import java .util .concurrent .TimeUnit ;
26+ import javax .net .ssl .SSLContext ;
2327import org .apache .http .HttpException ;
2428import org .apache .http .HttpHost ;
2529import org .apache .http .HttpResponse ;
2832import org .apache .http .client .methods .HttpUriRequest ;
2933import org .apache .http .conn .UnsupportedSchemeException ;
3034import org .apache .http .conn .routing .HttpRoute ;
35+ import org .apache .http .conn .ssl .NoopHostnameVerifier ;
3136import org .apache .http .impl .client .CloseableHttpClient ;
3237import org .apache .http .impl .client .HttpClientBuilder ;
3338import org .apache .http .impl .conn .DefaultSchemePortResolver ;
3439import org .apache .http .impl .conn .PoolingHttpClientConnectionManager ;
3540import org .apache .http .protocol .HttpContext ;
41+ import org .apache .http .ssl .SSLContextBuilder ;
3642
3743/** Http client implementation to be used for executing http requests. */
3844public class DatabricksHttpClient implements IDatabricksHttpClient {
@@ -72,6 +78,7 @@ public class DatabricksHttpClient implements IDatabricksHttpClient {
7278 private static boolean shouldRetryRateLimitError ;
7379 private static int rateLimitRetryTimeout ;
7480 protected static int idleHttpConnectionExpiry ;
81+ private CloseableHttpClient httpDisabledSSLClient ;
7582
7683 private DatabricksHttpClient (IDatabricksConnectionContext connectionContext ) {
7784 initializeConnectionManager ();
@@ -81,6 +88,7 @@ private DatabricksHttpClient(IDatabricksConnectionContext connectionContext) {
8188 shouldRetryRateLimitError = connectionContext .shouldRetryRateLimitError ();
8289 rateLimitRetryTimeout = connectionContext .getRateLimitRetryTimeout ();
8390 httpClient = makeClosableHttpClient (connectionContext );
91+ httpDisabledSSLClient = makeClosableDisabledSslHttpClient ();
8492 idleHttpConnectionExpiry = connectionContext .getIdleHttpConnectionExpiry ();
8593 }
8694
@@ -143,6 +151,26 @@ private CloseableHttpClient makeClosableHttpClient(
143151 return builder .build ();
144152 }
145153
154+ private CloseableHttpClient makeClosableDisabledSslHttpClient () {
155+ try {
156+ // Create SSL context that trusts all certificates
157+ SSLContext sslContext =
158+ new SSLContextBuilder ().loadTrustMaterial (null , (chain , authType ) -> true ).build ();
159+
160+ // Create HttpClient with the SSL context
161+ return HttpClientBuilder .create ()
162+ .setSSLContext (sslContext )
163+ .setSSLHostnameVerifier (new NoopHostnameVerifier ())
164+ .build ();
165+ } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e ) {
166+ LoggingUtil .log (
167+ LogLevel .DEBUG ,
168+ String .format (
169+ "Error in creating HttpClient with the SSL context [{%s}]" , e .getMessage ()));
170+ }
171+ return null ;
172+ }
173+
146174 private boolean handleRetry (IOException exception , int executionCount , HttpContext context ) {
147175 int errCode = getErrorCode (exception );
148176 if (!isErrorCodeRetryable (errCode )) {
@@ -322,20 +350,22 @@ public CloseableHttpResponse execute(HttpUriRequest request) throws DatabricksHt
322350 try {
323351 return httpClient .execute (request );
324352 } catch (IOException e ) {
325- Throwable cause = e ;
326- while (cause != null ) {
327- if (cause instanceof DatabricksRetryHandlerException ) {
328- throw new DatabricksHttpException (cause .getMessage (), cause );
329- }
330- cause = cause .getCause ();
331- }
332- String errorMsg =
333- String .format (
334- "Caught error while executing http request: [%s]. Error Message: [%s]" ,
335- RequestSanitizer .sanitizeRequest (request ), e );
336- LoggingUtil .log (LogLevel .ERROR , errorMsg );
337- throw new DatabricksHttpException (errorMsg , e );
353+ throwHttpException (e , request , LogLevel .ERROR );
354+ }
355+ return null ;
356+ }
357+
358+ public CloseableHttpResponse executeWithoutSSL (HttpUriRequest request )
359+ throws DatabricksHttpException {
360+ LoggingUtil .log (
361+ LogLevel .DEBUG ,
362+ String .format ("Executing HTTP request [{%s}]" , RequestSanitizer .sanitizeRequest (request )));
363+ try {
364+ return httpDisabledSSLClient .execute (request );
365+ } catch (Exception e ) {
366+ throwHttpException (e , request , LogLevel .DEBUG );
338367 }
368+ return null ;
339369 }
340370
341371 @ Override
@@ -385,4 +415,21 @@ public static synchronized void removeInstance(IDatabricksConnectionContext cont
385415 }
386416 }
387417 }
418+
419+ private static void throwHttpException (Exception e , HttpUriRequest request , LogLevel logLevel )
420+ throws DatabricksHttpException {
421+ Throwable cause = e ;
422+ while (cause != null ) {
423+ if (cause instanceof DatabricksRetryHandlerException ) {
424+ throw new DatabricksHttpException (cause .getMessage (), cause );
425+ }
426+ cause = cause .getCause ();
427+ }
428+ String errorMsg =
429+ String .format (
430+ "Caught error while executing http request: [%s]. Error Message: [%s]" ,
431+ RequestSanitizer .sanitizeRequest (request ), e );
432+ LoggingUtil .log (logLevel , errorMsg );
433+ throw new DatabricksHttpException (errorMsg , e );
434+ }
388435}
0 commit comments