Skip to content

Commit 4ecf685

Browse files
committed
fix ssl for URL defined endpoints
1 parent 287919a commit 4ecf685

File tree

4 files changed

+56
-21
lines changed

4 files changed

+56
-21
lines changed

client-v2/src/main/java/com/clickhouse/client/api/Client.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.io.InputStream;
4444
import java.lang.reflect.InvocationTargetException;
4545
import java.lang.reflect.Method;
46+
import java.net.URISyntaxException;
4647
import java.net.URL;
4748
import java.time.Duration;
4849
import java.time.temporal.ChronoUnit;
@@ -174,7 +175,10 @@ public Builder addEndpoint(String endpoint) {
174175
endpointURL.getProtocol().equalsIgnoreCase("http"))) {
175176
throw new IllegalArgumentException("Only HTTP and HTTPS protocols are supported");
176177
}
177-
} catch (java.net.MalformedURLException e) {
178+
if (endpointURL.toURI().getScheme().equalsIgnoreCase("https")) {
179+
this.configuration.put(ClickHouseClientOption.SSL.getKey(), "true");
180+
}
181+
} catch (java.net.MalformedURLException | URISyntaxException e) {
178182
throw new IllegalArgumentException("Endpoint should be a valid URL string", e);
179183
}
180184
this.endpoints.add(endpoint);
@@ -640,7 +644,9 @@ public CompletableFuture<InsertResponse> insert(String tableName,
640644
try {
641645
InsertResponse response = new InsertResponse(client, future.get(), clientStats);
642646
responseFuture.complete(response);
643-
} catch (InterruptedException | ExecutionException e) {
647+
} catch (ExecutionException e) {
648+
responseFuture.completeExceptionally(new ClientException("Failed to get insert response", e.getCause()));
649+
} catch (InterruptedException e) {
644650
responseFuture.completeExceptionally(new ClientException("Operation has likely timed out.", e));
645651
}
646652
}
@@ -807,6 +813,8 @@ public List<GenericRecord> queryAll(String sqlQuery) {
807813
}
808814
return records;
809815
}
816+
} catch (ExecutionException e) {
817+
throw new ClientException("Failed to get query response", e.getCause());
810818
} catch (Exception e) {
811819
throw new ClientException("Failed to get query response", e);
812820
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.clickhouse.client.api.enums;
22

33
public enum Protocol {
4-
HTTP,
5-
//HTTPS,
6-
//TCP,
4+
HTTP
75
}

client-v2/src/main/java/com/clickhouse/client/api/query/QueryResponse.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ private void makeComplete() {
6868
ClickHouseResponse response = responseRef.get(completeTimeout, TimeUnit.MILLISECONDS);
6969
completed = true;
7070
operationMetrics.operationComplete(response.getSummary());
71-
} catch (TimeoutException | InterruptedException | ExecutionException e) {
71+
} catch (ExecutionException e) {
72+
throw new ClientException("Failed to get command response", e.getCause());
73+
} catch (TimeoutException | InterruptedException e) {
7274
throw new ClientException("Query request failed", e);
7375
}
7476
}
Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,58 @@
11
package com.clickhouse.client;
22

33
import com.clickhouse.client.api.Client;
4+
import com.clickhouse.client.api.ClientException;
45
import com.clickhouse.client.api.enums.Protocol;
56
import com.clickhouse.client.api.query.GenericRecord;
7+
import com.clickhouse.client.config.ClickHouseClientOption;
68
import org.junit.Assert;
9+
import org.testng.annotations.DataProvider;
710
import org.testng.annotations.Test;
811

12+
import java.net.ConnectException;
13+
import java.sql.Connection;
914
import java.util.Optional;
1015

1116
public class ClientTests extends BaseIntegrationTest {
1217

1318

14-
@Test(enabled = false)
15-
public void testWithSSL() {
16-
17-
ClickHouseNode secureNode = getSecureServer(ClickHouseProtocol.HTTP);
18-
Client client = new Client.Builder()
19-
.addEndpoint(Protocol.HTTP, "localhost", secureNode.getPort(), true)
20-
.setUsername("default")
21-
.setPassword("")
22-
.build();
23-
24-
25-
Optional<GenericRecord> genericRecord = client
26-
.queryAll("SELECT hostname()").stream().findFirst();
27-
Assert.assertTrue(genericRecord.isPresent());
19+
@Test(dataProvider = "clientProvider")
20+
public void testAddSecureEndpoint(Client client) {
21+
try {
22+
Optional<GenericRecord> genericRecord = client
23+
.queryAll("SELECT hostname()").stream().findFirst();
24+
Assert.assertTrue(genericRecord.isPresent());
25+
} catch (ClientException e) {
26+
e.printStackTrace();
27+
if (e.getCause().getCause() instanceof ClickHouseException) {
28+
Exception cause = (Exception) e.getCause().getCause().getCause();
29+
Assert.assertTrue(cause instanceof ConnectException);
30+
// TODO: correct when SSL support is fully implemented.
31+
Assert.assertTrue(cause.getMessage()
32+
.startsWith("HTTP request failed: PKIX path building failed"));
33+
return;
34+
}
35+
Assert.fail(e.getMessage());
36+
}
37+
}
2838

29-
System.out.println(genericRecord.get().getString(1));
39+
@DataProvider(name = "clientProvider")
40+
private static Client[] secureClientProvider() throws Exception {
41+
ClickHouseNode node = ClickHouseServerForTest.getClickHouseNode(ClickHouseProtocol.HTTP,
42+
true, ClickHouseNode.builder()
43+
.addOption(ClickHouseClientOption.SSL_MODE.getKey(), "none")
44+
.addOption(ClickHouseClientOption.SSL.getKey(), "true").build());
45+
return new Client[]{
46+
new Client.Builder()
47+
.addEndpoint("https://" + node.getHost() + ":" + node.getPort())
48+
.setUsername("default")
49+
.setPassword("")
50+
.build(),
51+
new Client.Builder()
52+
.addEndpoint(Protocol.HTTP, node.getHost(), node.getPort(), true)
53+
.setUsername("default")
54+
.setPassword("")
55+
.build()
56+
};
3057
}
3158
}

0 commit comments

Comments
 (0)