Skip to content

Commit 88d97d0

Browse files
NDHAppsNick Hall
authored andcommitted
Add support for basic proxy auth
1 parent ff13581 commit 88d97d0

File tree

6 files changed

+54
-9
lines changed

6 files changed

+54
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## Latest
2+
3+
### New Features
4+
- Added basic auth support for proxies. Now you can specify username/password when connecting via a proxy that requires it with HttpURLConnection and Apache HttpClient.
5+
16
## 0.7.1-patch1
27

38
### Bug Fixes

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ public static Map<ClickHouseOption, Serializable> toClientOptions(Map<?, ?> prop
245245
private final ClickHouseProxyType proxyType;
246246
private final String proxyHost;
247247
private final int proxyPort;
248+
private final String proxyUserName;
249+
private final char[] proxyPassword;
248250
// client specific options
249251
private final Map<ClickHouseOption, Serializable> options;
250252
private final ClickHouseCredentials credentials;
@@ -383,6 +385,8 @@ public ClickHouseConfig(Map<ClickHouseOption, Serializable> options, ClickHouseC
383385
this.proxyType = getOption(ClickHouseClientOption.PROXY_TYPE, ClickHouseProxyType.class);
384386
this.proxyHost = getStrOption(ClickHouseClientOption.PROXY_HOST);
385387
this.proxyPort = getIntOption(ClickHouseClientOption.PROXY_PORT);
388+
this.proxyUserName = getStrOption(ClickHouseClientOption.PROXY_USERNAME);
389+
this.proxyPassword = getStrOption(ClickHouseClientOption.PROXY_PASSWORD).toCharArray();
386390
}
387391

388392
@Override
@@ -674,14 +678,6 @@ public boolean isUseObjectsInArray() {
674678
return useObjectsInArray;
675679
}
676680

677-
/**
678-
* Checks whether no proxy is used or not.
679-
*
680-
* @return true if no proxy is used; false otherwise
681-
* @deprecated will be dropped in 0.5, please use {@link #getProxyType()}
682-
* instead
683-
*/
684-
685681
public ClickHouseProxyType getProxyType() {
686682
return proxyType;
687683
}
@@ -694,6 +690,14 @@ public int getProxyPort() {
694690
return proxyPort;
695691
}
696692

693+
public String getProxyUserName() {
694+
return proxyUserName;
695+
}
696+
697+
public char[] getProxyPassword() {
698+
return proxyPassword;
699+
}
700+
697701
public boolean isUseServerTimeZone() {
698702
return useServerTimeZone;
699703
}

clickhouse-client/src/main/java/com/clickhouse/client/config/ClickHouseClientOption.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,15 @@ public enum ClickHouseClientOption implements ClickHouseOption {
402402
/**
403403
* Set ClickHouse proxy port.
404404
*/
405-
PROXY_PORT("proxy_port", -1, "Set ClickHouse server proxy hostname."),
405+
PROXY_PORT("proxy_port", -1, "Set ClickHouse server proxy port."),
406+
/**
407+
* Set Clickhouse proxy username.
408+
*/
409+
PROXY_USERNAME("proxy_username", "", "Set ClickHouse server proxy username."),
410+
/**
411+
* Set ClickHouse proxy password.
412+
*/
413+
PROXY_PASSWORD("proxy_password", "", "Set ClickHouse server proxy password."),
406414
/**
407415
* Whether to use server time zone.
408416
*/

clickhouse-http-client/src/main/java/com/clickhouse/client/http/ApacheHttpConnectionImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919
import com.clickhouse.data.ClickHouseUtils;
2020
import com.clickhouse.logging.Logger;
2121
import com.clickhouse.logging.LoggerFactory;
22+
import org.apache.hc.client5.http.auth.AuthScope;
23+
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
2224
import org.apache.hc.client5.http.classic.methods.HttpGet;
2325
import org.apache.hc.client5.http.classic.methods.HttpPost;
2426
import org.apache.hc.client5.http.config.ConnectionConfig;
27+
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
2528
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
2629
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
2730
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
@@ -121,6 +124,14 @@ private CloseableHttpClient newConnection(ClickHouseConfig c) throws IOException
121124
}
122125
if (c.getProxyType() == ClickHouseProxyType.HTTP) {
123126
builder.setProxy(new HttpHost(c.getProxyHost(), c.getProxyPort()));
127+
128+
if (c.getProxyUserName() != null && c.getProxyUserName() != "") {
129+
AuthScope authScope = new AuthScope(c.getProxyHost(), c.getProxyPort());
130+
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(c.getProxyUserName(), c.getProxyPassword());
131+
BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
132+
credsProvider.setCredentials(authScope, credentials);
133+
builder.setDefaultCredentialsProvider(credsProvider);
134+
}
124135
}
125136
return builder.build();
126137
}

clickhouse-http-client/src/main/java/com/clickhouse/client/http/ClickHouseHttpConnection.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,19 @@ protected static Proxy getProxy(ClickHouseConfig config) {
287287
return proxy;
288288
}
289289

290+
protected static String getProxyAuth(ClickHouseConfig config) {
291+
String authHeader = null;
292+
if (config.getProxyType() == ClickHouseProxyType.HTTP) {
293+
String userName = config.getProxyUserName();
294+
if (!ClickHouseChecker.isNullOrEmpty(userName)) {
295+
String auth = userName + ":" + new String(config.getProxyPassword());
296+
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
297+
authHeader = "Basic " + new String(encodedAuth);
298+
}
299+
}
300+
return authHeader;
301+
}
302+
290303
protected static String parseErrorFromException(String errorCode, String serverName, IOException e, byte[] bytes) {
291304
log.debug("Failed to read error message[code=%s] from server [%s] due to: %s", errorCode, serverName,
292305
e.getMessage());

clickhouse-http-client/src/main/java/com/clickhouse/client/http/HttpUrlConnectionImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ private HttpURLConnection newConnection(String url, boolean post) throws IOExcep
111111
if (proxy != null) {
112112
log.debug("using proxy type [%s] address [%s]", proxy.type().name(), proxy.address().toString());
113113
newConn = (HttpURLConnection) new URL(url).openConnection(proxy);
114+
String authString = getProxyAuth(c);
115+
if (authString != null) {
116+
newConn.setRequestProperty("Proxy-Authorization", authString);
117+
}
114118
} else {
115119
newConn = (HttpURLConnection) new URL(url).openConnection();
116120
}

0 commit comments

Comments
 (0)