Skip to content

Commit ee3e52c

Browse files
authored
Merge pull request #301 from databricks/bhu1rdb/sslClient
Implemented makeClosableDisabledSslHttpClient for exporting metrics
2 parents 288ee7f + 449b53c commit ee3e52c

File tree

3 files changed

+65
-62
lines changed

3 files changed

+65
-62
lines changed

src/main/java/com/databricks/jdbc/client/http/DatabricksHttpClient.java

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616
import com.databricks.sdk.core.utils.ProxyUtils;
1717
import com.google.common.annotations.VisibleForTesting;
1818
import java.io.IOException;
19+
import java.security.KeyManagementException;
20+
import java.security.KeyStoreException;
21+
import java.security.NoSuchAlgorithmException;
1922
import java.util.Objects;
2023
import java.util.Set;
2124
import java.util.concurrent.ConcurrentHashMap;
2225
import java.util.concurrent.TimeUnit;
26+
import javax.net.ssl.SSLContext;
2327
import org.apache.http.HttpException;
2428
import org.apache.http.HttpHost;
2529
import org.apache.http.HttpResponse;
@@ -28,11 +32,13 @@
2832
import org.apache.http.client.methods.HttpUriRequest;
2933
import org.apache.http.conn.UnsupportedSchemeException;
3034
import org.apache.http.conn.routing.HttpRoute;
35+
import org.apache.http.conn.ssl.NoopHostnameVerifier;
3136
import org.apache.http.impl.client.CloseableHttpClient;
3237
import org.apache.http.impl.client.HttpClientBuilder;
3338
import org.apache.http.impl.conn.DefaultSchemePortResolver;
3439
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
3540
import org.apache.http.protocol.HttpContext;
41+
import org.apache.http.ssl.SSLContextBuilder;
3642

3743
/** Http client implementation to be used for executing http requests. */
3844
public 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
}

src/main/java/com/databricks/jdbc/commons/util/DefaultHttpClientUtil.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/main/java/com/databricks/jdbc/telemetry/DatabricksMetrics.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.databricks.jdbc.telemetry;
22

33
import com.databricks.jdbc.client.DatabricksHttpException;
4-
import com.databricks.jdbc.commons.util.DefaultHttpClientUtil;
4+
import com.databricks.jdbc.client.http.DatabricksHttpClient;
55
import com.databricks.jdbc.core.DatabricksSQLException;
66
import com.databricks.jdbc.driver.IDatabricksConnectionContext;
77
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -15,7 +15,6 @@
1515
import org.apache.http.client.methods.HttpPost;
1616
import org.apache.http.client.methods.HttpUriRequest;
1717
import org.apache.http.client.utils.URIBuilder;
18-
import org.apache.http.impl.client.DefaultHttpClient;
1918
import org.apache.http.util.EntityUtils;
2019

2120
// TODO (Bhuvan) Flush out metrics once the driver connection is closed
@@ -31,7 +30,7 @@ public class DatabricksMetrics {
3130
private final String METRICS_TYPE = "metrics_type";
3231
private Boolean hasInitialExportOccurred = false;
3332
private String workspaceId = null;
34-
private DefaultHttpClient telemetryClient = null;
33+
private final DatabricksHttpClient telemetryClient;
3534

3635
private void setWorkspaceId(String workspaceId) {
3736
this.workspaceId = workspaceId;
@@ -68,13 +67,7 @@ public DatabricksMetrics(IDatabricksConnectionContext context) throws Databricks
6867
}
6968
String resourceId = context.getComputeResource().getWorkspaceId();
7069
setWorkspaceId(resourceId);
71-
// TODO: Bhuvan: Replace telemetry client with DatabricksHttpClient when non-SSL variation is
72-
// implemented
73-
try {
74-
telemetryClient = DefaultHttpClientUtil.getDefaultHttpClient();
75-
} catch (Exception e) {
76-
throw new DatabricksSQLException("Failed to create telemetry client");
77-
}
70+
this.telemetryClient = DatabricksHttpClient.getInstance(context);
7871
scheduleExportMetrics();
7972
}
8073

@@ -101,7 +94,8 @@ public String sendRequest(Map<String, Double> map, MetricsType metricsType) thro
10194
uriBuilder.addParameter(METRICS_TYPE, metricsType.name().equals("GAUGE") ? "1" : "0");
10295
HttpUriRequest request = new HttpPost(uriBuilder.build());
10396
// TODO (Bhuvan): Add authentication headers
104-
CloseableHttpResponse response = telemetryClient.execute(request);
97+
// TODO (Bhuvan): execute request using SSL
98+
CloseableHttpResponse response = telemetryClient.executeWithoutSSL(request);
10599

106100
// Error handling
107101
if (response == null) {

0 commit comments

Comments
 (0)