Skip to content

Commit 46979b5

Browse files
refactor: proxy and ssl
1 parent ec530c4 commit 46979b5

File tree

5 files changed

+164
-139
lines changed

5 files changed

+164
-139
lines changed

src/main/java/com/influxdb/v3/client/config/ClientConfig.java

Lines changed: 36 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import java.net.Authenticator;
2525
import java.net.MalformedURLException;
26-
import java.net.ProxySelector;
2726
import java.net.URL;
2827
import java.time.Duration;
2928
import java.util.Arrays;
@@ -35,10 +34,6 @@
3534
import java.util.function.BiFunction;
3635
import javax.annotation.Nonnull;
3736
import javax.annotation.Nullable;
38-
import javax.net.ssl.SSLContext;
39-
40-
import io.grpc.ProxyDetector;
41-
import io.netty.handler.ssl.SslContext;
4237

4338
import com.influxdb.v3.client.write.WritePrecision;
4439

@@ -61,16 +56,17 @@
6156
* <li><code>disableServerCertificateValidation</code> -
6257
* disable server certificate validation for HTTPS connections
6358
* </li>
64-
* <li><code>proxy</code> - HTTP proxy selector</li>
59+
* <li><code>proxyUrl</code> - Proxy url for query api and write api</li>
6560
* <li><code>queryApiProxy</code> - HTTP query detector</li>
6661
* <li><code>authenticator</code> - HTTP proxy authenticator</li>
6762
* <li><code>headers</code> - headers to be added to requests</li>
63+
* <li><code>certificateFilePath</code> - Path to the stored certificates file</li>
6864
* </ul>
6965
* <p>
7066
* If you want to create a client with custom configuration, you can use following code:
7167
* <pre>
7268
* ClientConfig config = new ClientConfig.Builder()
73-
* .host("https://us-east-1-1.aws.cloud2.influxdata.com")
69+
* .host("<a href="https://us-east-1-1.aws.cloud2.influxdata.com">https://us-east-1-1.aws.cloud2.influxdata.com</a>")
7470
* .token("my-token".toCharArray())
7571
* .database("my-database")
7672
* .writePrecision(WritePrecision.S)
@@ -89,7 +85,8 @@
8985
* Immutable class.
9086
*/
9187
public final class ClientConfig {
92-
88+
//todo check if main use proxySelector for backward compality
89+
//todo check comments
9390
private final String host;
9491
private final char[] token;
9592
private final String authScheme;
@@ -101,12 +98,10 @@ public final class ClientConfig {
10198
private final Duration timeout;
10299
private final Boolean allowHttpRedirects;
103100
private final Boolean disableServerCertificateValidation;
104-
private final ProxySelector proxy;
105-
private final ProxyDetector queryApiProxy;
101+
private final String proxyUrl;
106102
private final Authenticator authenticator;
107103
private final Map<String, String> headers;
108-
private final SslContext grpcSslContext;
109-
private final SSLContext sslContext;
104+
private final String certificateFilePath;
110105

111106
/**
112107
* Gets URL of the InfluxDB server.
@@ -218,23 +213,23 @@ public Boolean getDisableServerCertificateValidation() {
218213
}
219214

220215
/**
221-
* Gets the proxy.
216+
* Gets the proxy url.
222217
*
223-
* @return the proxy, may be null
218+
* @return the proxy url, may be null
224219
*/
225220
@Nullable
226-
public ProxySelector getProxy() {
227-
return proxy;
221+
public String getProxyUrl() {
222+
return proxyUrl;
228223
}
229224

230225
/**
231-
* Gets the proxy for query api.
226+
* Gets certificates file path
232227
*
233-
* @return the proxy, may be null
228+
* @return the certificates file path, may be null
234229
*/
235230
@Nullable
236-
public ProxyDetector getQueryApiProxy() {
237-
return queryApiProxy;
231+
public String certificateFilePath() {
232+
return certificateFilePath;
238233
}
239234

240235
/**
@@ -257,26 +252,6 @@ public Map<String, String> getHeaders() {
257252
return headers;
258253
}
259254

260-
/**
261-
* Gets SslContext object from grpc.
262-
*
263-
* @return the SslContext object
264-
*/
265-
@Nullable
266-
public SslContext getGrpcSslContext() {
267-
return grpcSslContext;
268-
}
269-
270-
/**
271-
* Gets SSLContext object.
272-
*
273-
* @return the SSLContext object
274-
*/
275-
@Nullable
276-
public SSLContext getSslContext() {
277-
return sslContext;
278-
}
279-
280255
/**
281256
* Validates the configuration properties.
282257
*/
@@ -306,21 +281,19 @@ public boolean equals(final Object o) {
306281
&& Objects.equals(timeout, that.timeout)
307282
&& Objects.equals(allowHttpRedirects, that.allowHttpRedirects)
308283
&& Objects.equals(disableServerCertificateValidation, that.disableServerCertificateValidation)
309-
&& Objects.equals(proxy, that.proxy)
310-
&& Objects.equals(queryApiProxy, that.queryApiProxy)
284+
&& Objects.equals(proxyUrl, that.proxyUrl)
311285
&& Objects.equals(authenticator, that.authenticator)
312286
&& Objects.equals(headers, that.headers)
313-
&& Objects.equals(grpcSslContext, that.grpcSslContext)
314-
&& Objects.equals(sslContext, that.sslContext);
287+
&& Objects.equals(certificateFilePath, that.certificateFilePath);
315288
}
316289

317290
@Override
318291
public int hashCode() {
319292
return Objects.hash(host, Arrays.hashCode(token), authScheme, organization,
320-
database, writePrecision, gzipThreshold,
321-
timeout, allowHttpRedirects, disableServerCertificateValidation,
322-
proxy, queryApiProxy, authenticator, headers,
323-
defaultTags, grpcSslContext, sslContext);
293+
database, writePrecision, gzipThreshold,
294+
timeout, allowHttpRedirects, disableServerCertificateValidation,
295+
proxyUrl, authenticator, headers,
296+
defaultTags, certificateFilePath);
324297
}
325298

326299
@Override
@@ -334,13 +307,11 @@ public String toString() {
334307
.add("timeout=" + timeout)
335308
.add("allowHttpRedirects=" + allowHttpRedirects)
336309
.add("disableServerCertificateValidation=" + disableServerCertificateValidation)
337-
.add("proxy=" + proxy)
338-
.add("queryApiProxy=" + queryApiProxy)
310+
.add("proxy=" + proxyUrl)
339311
.add("authenticator=" + authenticator)
340312
.add("headers=" + headers)
341313
.add("defaultTags=" + defaultTags)
342-
.add("grpcSslContext=" + grpcSslContext)
343-
.add("sslContext=" + sslContext)
314+
.add("certificateFilePath=" + certificateFilePath)
344315
.toString();
345316
}
346317

@@ -361,12 +332,10 @@ public static final class Builder {
361332
private Duration timeout;
362333
private Boolean allowHttpRedirects;
363334
private Boolean disableServerCertificateValidation;
364-
private ProxySelector proxy;
365-
private ProxyDetector queryApiProxy;
335+
private String proxyUrl;
366336
private Authenticator authenticator;
367337
private Map<String, String> headers;
368-
private SslContext grpcSslContext;
369-
private SSLContext sslContext;
338+
private String certificateFilePath;
370339

371340
/**
372341
* Sets the URL of the InfluxDB server.
@@ -515,28 +484,15 @@ public Builder disableServerCertificateValidation(@Nullable final Boolean disabl
515484
}
516485

517486
/**
518-
* Sets the proxy selector. Default is 'null'.
519-
*
520-
* @param proxy Proxy selector.
521-
* @return this
522-
*/
523-
@Nonnull
524-
public Builder proxy(@Nullable final ProxySelector proxy) {
525-
526-
this.proxy = proxy;
527-
return this;
528-
}
529-
530-
/**
531-
* Sets the proxy detector for query api. Default is 'null'.
487+
* Sets the proxy url. Default is 'null'.
532488
*
533-
* @param proxy Proxy detector.
489+
* @param proxyUrl Proxy url.
534490
* @return this
535491
*/
536492
@Nonnull
537-
public Builder queryApiProxy(@Nullable final ProxyDetector proxy) {
493+
public Builder proxyUrl(@Nullable final String proxyUrl) {
538494

539-
this.queryApiProxy = proxy;
495+
this.proxyUrl = proxyUrl;
540496
return this;
541497
}
542498

@@ -558,7 +514,7 @@ public Builder authenticator(@Nullable final Authenticator authenticator) {
558514
* such as tracing headers. To add custom headers use following code:
559515
* <pre>
560516
* ClientConfig config = new ClientConfig.Builder()
561-
* .host("https://us-east-1-1.aws.cloud2.influxdata.com")
517+
* .host("<a href="https://us-east-1-1.aws.cloud2.influxdata.com">https://us-east-1-1.aws.cloud2.influxdata.com</a>")
562518
* .token("my-token".toCharArray())
563519
* .database("my-database")
564520
* .headers(Map.of("X-Tracing-Id", "123"))
@@ -584,28 +540,15 @@ public Builder headers(@Nullable final Map<String, String> headers) {
584540
}
585541

586542
/**
587-
* Sets SslContext for grpc client. Default is 'null'.
588-
*
589-
* @param grpcSslContext The SSLContext
590-
* @return this
591-
*/
592-
@Nonnull
593-
public Builder grpcSslContext(@Nullable final SslContext grpcSslContext) {
594-
595-
this.grpcSslContext = grpcSslContext;
596-
return this;
597-
}
598-
599-
/**
600-
* Sets SSLContext for rest client. Default is 'null'.
543+
* Sets certificate file path. Default is 'null'.
601544
*
602-
* @param sslContext The SSLContext
545+
* @param certificateFilePath The certificate file path
603546
* @return this
604547
*/
605548
@Nonnull
606-
public Builder sslContext(@Nullable final SSLContext sslContext) {
549+
public Builder certificateFilePath(@Nullable final String certificateFilePath) {
607550

608-
this.sslContext = sslContext;
551+
this.certificateFilePath = certificateFilePath;
609552
return this;
610553
}
611554

@@ -743,11 +686,9 @@ private ClientConfig(@Nonnull final Builder builder) {
743686
allowHttpRedirects = builder.allowHttpRedirects != null ? builder.allowHttpRedirects : false;
744687
disableServerCertificateValidation = builder.disableServerCertificateValidation != null
745688
? builder.disableServerCertificateValidation : false;
746-
proxy = builder.proxy;
747-
queryApiProxy = builder.queryApiProxy;
689+
proxyUrl = builder.proxyUrl;
748690
authenticator = builder.authenticator;
749691
headers = builder.headers;
750-
grpcSslContext = builder.grpcSslContext;
751-
sslContext = builder.sslContext;
692+
certificateFilePath = builder.certificateFilePath;
752693
}
753694
}

src/main/java/com/influxdb/v3/client/internal/FlightSqlClient.java

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
*/
2222
package com.influxdb.v3.client.internal;
2323

24+
import java.io.FileInputStream;
25+
import java.io.IOException;
2426
import java.lang.reflect.InvocationTargetException;
27+
import java.net.InetSocketAddress;
2528
import java.net.URI;
2629
import java.net.URISyntaxException;
2730
import java.nio.charset.StandardCharsets;
@@ -41,12 +44,13 @@
4144

4245
import com.fasterxml.jackson.core.JsonProcessingException;
4346
import com.fasterxml.jackson.databind.ObjectMapper;
47+
import io.grpc.HttpConnectProxiedSocketAddress;
4448
import io.grpc.Metadata;
49+
import io.grpc.ProxyDetector;
4550
import io.grpc.netty.GrpcSslContexts;
4651
import io.grpc.netty.NettyChannelBuilder;
4752
import io.netty.channel.EventLoopGroup;
4853
import io.netty.channel.ServerChannel;
49-
import io.netty.handler.ssl.SslContext;
5054
import io.netty.handler.ssl.SslContextBuilder;
5155
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
5256
import org.apache.arrow.flight.FlightClient;
@@ -159,27 +163,32 @@ private FlightClient createFlightClient(@Nonnull final ClientConfig config) {
159163
nettyChannelBuilder.useTransportSecurity();
160164

161165
SslContextBuilder sslContextBuilder;
162-
SslContext sslContext;
163-
if (config.getGrpcSslContext() != null) {
164-
sslContext = config.getGrpcSslContext();
165-
nettyChannelBuilder.sslContext(sslContext);
166-
} else {
167-
sslContextBuilder = GrpcSslContexts.forClient();
168-
if (config.getDisableServerCertificateValidation()) {
169-
sslContextBuilder.trustManager(InsecureTrustManagerFactory.INSTANCE);
170-
}
171-
try {
172-
nettyChannelBuilder.sslContext(sslContextBuilder.build());
173-
} catch (SSLException e) {
174-
throw new RuntimeException(e);
166+
sslContextBuilder = GrpcSslContexts.forClient();
167+
if (!config.getDisableServerCertificateValidation()) {
168+
if (config.certificateFilePath() != null) {
169+
try (FileInputStream fileInputStream = new FileInputStream(config.certificateFilePath())) {
170+
sslContextBuilder.trustManager(fileInputStream);
171+
} catch (IOException e) {
172+
throw new RuntimeException(e);
173+
}
175174
}
175+
} else {
176+
sslContextBuilder.trustManager(InsecureTrustManagerFactory.INSTANCE);
176177
}
178+
179+
try {
180+
nettyChannelBuilder.sslContext(sslContextBuilder.build());
181+
} catch (SSLException e) {
182+
throw new RuntimeException(e);
183+
}
184+
177185
} else {
178186
nettyChannelBuilder.usePlaintext();
179187
}
180188

181-
if (config.getQueryApiProxy() != null) {
182-
nettyChannelBuilder.proxyDetector(config.getQueryApiProxy());
189+
if (config.getProxyUrl() != null) {
190+
ProxyDetector proxyDetector = createProxyDetector(config.getProxyUrl());
191+
nettyChannelBuilder.proxyDetector(proxyDetector);
183192
}
184193

185194
nettyChannelBuilder.maxTraceEvents(0)
@@ -254,6 +263,20 @@ private void setChannelTypeAndEventLoop(@Nonnull final NettyChannelBuilder netty
254263
}
255264
}
256265

266+
private ProxyDetector createProxyDetector(@Nonnull String url) {
267+
URI proxyUri = URI.create(url);
268+
return (targetServerAddress) -> {
269+
InetSocketAddress targetAddress = (InetSocketAddress) targetServerAddress;
270+
if (proxyUri.getHost().equals(targetAddress.getHostString())) {
271+
return HttpConnectProxiedSocketAddress.newBuilder()
272+
.setProxyAddress(new InetSocketAddress(proxyUri.getHost(), proxyUri.getPort()))
273+
.setTargetAddress(targetAddress)
274+
.build();
275+
}
276+
return null;
277+
};
278+
}
279+
257280
private static final class FlightSqlIterator implements Iterator<VectorSchemaRoot>, AutoCloseable {
258281

259282
private final List<AutoCloseable> autoCloseable = new ArrayList<>();

0 commit comments

Comments
 (0)