Skip to content

Commit 6539f9c

Browse files
authored
Merge pull request #1702 from ClickHouse/fix_ssl_properties
[client-v2] fixed passing properties to old client
2 parents ddac0ca + 53148b3 commit 6539f9c

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
lines changed

clickhouse-client/src/main/java/com/clickhouse/client/ClickHouseNode.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,10 @@ public static ClickHouseNode of(String uri, Map<?, ?> options) {
787787
String scheme = normalizedUri.getScheme();
788788
ClickHouseProtocol protocol = ClickHouseProtocol.fromUriScheme(scheme);
789789
int port = extract(scheme, normalizedUri.getPort(), protocol, params);
790-
790+
if ((options == null || options.get(ClickHouseClientOption.SSL.getKey()) == null)
791+
&& scheme.equalsIgnoreCase("https")) {
792+
params.put(ClickHouseClientOption.SSL.getKey(), "true");
793+
}
791794
ClickHouseCredentials credentials = extract(normalizedUri.getRawUserInfo(), params, null);
792795

793796
return new ClickHouseNode(normalizedUri.getHost(), protocol, port, credentials, params, tags);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,11 @@ public Builder addEndpoint(Protocol protocol, String host, int port, boolean sec
191191
ValidationUtils.checkNonBlank(host, "host");
192192
ValidationUtils.checkNotNull(protocol, "protocol");
193193
ValidationUtils.checkRange(port, 1, ValidationUtils.TCP_PORT_NUMBER_MAX, "port");
194-
194+
if (secure) {
195+
// For some reason com.clickhouse.client.http.ApacheHttpConnectionImpl.newConnection checks only client config
196+
// for SSL, so we need to set it here. But it actually should be set for each node separately.
197+
this.configuration.put(ClickHouseClientOption.SSL.getKey(), "true");
198+
}
195199
String endpoint = String.format("%s%s://%s:%d", protocol.toString().toLowerCase(), secure ? "s": "", host, port);
196200
this.addEndpoint(endpoint);
197201
return this;

client-v2/src/main/java/com/clickhouse/client/api/internal/ClientV1AdaptorHelper.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,29 @@
1818

1919
public class ClientV1AdaptorHelper {
2020

21-
private static void copyProxySettings(Map<ClickHouseOption, Serializable> target, Map<String, String> config) {
22-
ClickHouseClientOption opt = ClickHouseClientOption.PROXY_HOST;
23-
String value = config.get(opt.getKey());
24-
if (value != null) {
25-
target.put(opt, value);
26-
}
27-
opt = ClickHouseClientOption.PROXY_PORT;
28-
value = config.get(opt.getKey());
29-
if (value != null) {
30-
target.put(opt, Integer.parseInt(value));
31-
}
32-
opt = ClickHouseClientOption.PROXY_TYPE;
33-
value = config.get(opt.getKey());
34-
if (value != null) {
35-
target.put(opt, ClickHouseProxyType.valueOf(value));
21+
private static void copyClientOptions(Map<ClickHouseOption, Serializable> target, Map<String, String> config) {
22+
23+
for (ClickHouseClientOption opt : ClickHouseClientOption.values()) {
24+
String value = config.get(opt.getKey());
25+
if (value == null) {
26+
continue;
27+
}
28+
29+
if (opt.getValueType().isAssignableFrom(Integer.class)) {
30+
target.put(opt, Integer.parseInt(value));
31+
} else if (opt.getValueType().isAssignableFrom(Boolean.class)) {
32+
target.put(opt, Boolean.parseBoolean(value));
33+
} else if (opt.getValueType().isEnum()) {
34+
target.put(opt, Enum.valueOf((Class<Enum>) opt.getValueType(), value));
35+
} else if (opt.getValueType().isAssignableFrom(String.class)) {
36+
target.put(opt, value);
37+
}
3638
}
3739
}
3840

3941
public static ClickHouseClient createClient(Map<String, String> configuration) {
4042
Map<ClickHouseOption, Serializable> config = new HashMap<>();
41-
copyProxySettings(config, configuration);
43+
copyClientOptions(config, configuration);
4244

4345
ClickHouseConfig clientConfig = new ClickHouseConfig(config);
4446

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.clickhouse.client;
2+
3+
import com.clickhouse.client.api.Client;
4+
import com.clickhouse.client.api.enums.Protocol;
5+
import com.clickhouse.client.api.query.GenericRecord;
6+
import org.junit.Assert;
7+
import org.testng.annotations.Test;
8+
9+
import java.util.Optional;
10+
11+
public class ClientTests extends BaseIntegrationTest {
12+
13+
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());
28+
29+
System.out.println(genericRecord.get().getString(1));
30+
}
31+
}

0 commit comments

Comments
 (0)