Skip to content

Commit 5b80029

Browse files
feat: support proxy and ssl
1 parent 8db709b commit 5b80029

File tree

8 files changed

+405
-16
lines changed

8 files changed

+405
-16
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## 1.1.0 [unreleased]
22

3+
### Features
4+
5+
1. [#229](https://github.com/InfluxCommunity/influxdb3-java/pull/229): Support proxy and ssl
6+
37
## 1.0.0 [2024-12-11]
48

59
### Features

examples/docker-compose.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: "3"
2+
3+
services:
4+
envoy:
5+
image: envoyproxy/envoy:v1.26-latest
6+
volumes:
7+
- ./envoy.yaml:/etc/envoy/envoy.yaml
8+
ports:
9+
- "10000:10000"
10+
environment:
11+
- ENVOY_UID=0
12+

examples/envoy.yaml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
static_resources:
2+
listeners:
3+
- name: listener_0
4+
address:
5+
socket_address: { address: 0.0.0.0, port_value: 10000 }
6+
filter_chains:
7+
- filter_chain_match:
8+
filters:
9+
- name: envoy.filters.network.http_connection_manager
10+
typed_config:
11+
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
12+
stat_prefix: ingress_http
13+
access_log:
14+
- name: envoy.access_loggers.stdout
15+
typed_config:
16+
"@type": type.googleapis.com/envoy.extensions.access_loggers.stream.v3.StdoutAccessLog
17+
http2_protocol_options:
18+
allow_connect: true
19+
upgrade_configs:
20+
- upgrade_type: CONNECT
21+
http_filters:
22+
- name: envoy.filters.http.router
23+
typed_config:
24+
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
25+
26+
route_config:
27+
name: local_route
28+
virtual_hosts:
29+
- name: local_service
30+
domains: [ "*" ]
31+
routes:
32+
- match:
33+
connect_matcher: { }
34+
route:
35+
cluster: influxdb_cluster
36+
upgrade_configs:
37+
upgrade_type: CONNECT
38+
connect_config: { }
39+
- match:
40+
prefix: "/"
41+
route:
42+
cluster: influxdb_cluster
43+
prefix_rewrite: "/"
44+
auto_host_rewrite: true
45+
timeout: 10s
46+
cors:
47+
allow_origin_string_match:
48+
- prefix: "*"
49+
allow_methods: GET, PUT, DELETE, POST, OPTIONS
50+
clusters:
51+
- name: influxdb_cluster
52+
connect_timeout: 10s
53+
type: STRICT_DNS
54+
load_assignment:
55+
cluster_name: influxdb_cluster
56+
endpoints:
57+
- lb_endpoints:
58+
- endpoint:
59+
address:
60+
socket_address:
61+
address: "us-east-1-1.aws.cloud2.influxdata.com"
62+
port_value: 443
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.influxdb.v3;
2+
3+
import java.net.InetSocketAddress;
4+
import java.net.ProxySelector;
5+
import java.net.URI;
6+
import java.util.stream.Stream;
7+
8+
import io.grpc.HttpConnectProxiedSocketAddress;
9+
import io.grpc.ProxyDetector;
10+
11+
import com.influxdb.v3.client.InfluxDBClient;
12+
import com.influxdb.v3.client.Point;
13+
import com.influxdb.v3.client.PointValues;
14+
import com.influxdb.v3.client.config.ClientConfig;
15+
16+
public final class ProxyExample {
17+
18+
private ProxyExample() { }
19+
20+
public static void main(final String[] args) throws Exception {
21+
// Run docker-compose.yml file to start Envoy proxy
22+
23+
URI queryProxyUri = new URI("proxyUrl");
24+
URI uri = new URI(System.getenv("url"));
25+
26+
ProxyDetector proxyDetector = (targetServerAddress) -> {
27+
InetSocketAddress targetAddress = (InetSocketAddress) targetServerAddress;
28+
if (uri.getHost().equals(targetAddress.getHostString())) {
29+
return HttpConnectProxiedSocketAddress.newBuilder()
30+
.setProxyAddress(new InetSocketAddress(queryProxyUri.getHost(), queryProxyUri.getPort()))
31+
.setTargetAddress(targetAddress)
32+
.build();
33+
}
34+
return null;
35+
};
36+
ProxySelector proxy = ProxySelector.of(new InetSocketAddress(queryProxyUri.getHost(), queryProxyUri.getPort()));
37+
ClientConfig clientConfig = new ClientConfig.Builder()
38+
.host(uri.toString())
39+
.token(System.getenv("token").toCharArray())
40+
.database(System.getenv("database"))
41+
.proxy(proxy)
42+
.queryApiProxy(proxyDetector)
43+
.build();
44+
45+
InfluxDBClient influxDBClient = InfluxDBClient.getInstance(clientConfig);
46+
influxDBClient.writePoint(
47+
Point.measurement("test1")
48+
.setField("field", "field1")
49+
);
50+
51+
try (Stream<PointValues> stream = influxDBClient.queryPoints("SELECT * FROM test1")) {
52+
stream.findFirst()
53+
.ifPresent(pointValues -> {
54+
// do something
55+
});
56+
}
57+
}
58+
}
59+

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

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
import java.util.function.BiFunction;
3636
import javax.annotation.Nonnull;
3737
import javax.annotation.Nullable;
38+
import javax.net.ssl.SSLContext;
39+
40+
import io.grpc.ProxyDetector;
41+
import io.netty.handler.ssl.SslContext;
3842

3943
import com.influxdb.v3.client.write.WritePrecision;
4044

@@ -58,6 +62,7 @@
5862
* disable server certificate validation for HTTPS connections
5963
* </li>
6064
* <li><code>proxy</code> - HTTP proxy selector</li>
65+
* <li><code>queryApiProxy</code> - HTTP query detector</li>
6166
* <li><code>authenticator</code> - HTTP proxy authenticator</li>
6267
* <li><code>headers</code> - headers to be added to requests</li>
6368
* </ul>
@@ -97,8 +102,11 @@ public final class ClientConfig {
97102
private final Boolean allowHttpRedirects;
98103
private final Boolean disableServerCertificateValidation;
99104
private final ProxySelector proxy;
105+
private final ProxyDetector queryApiProxy;
100106
private final Authenticator authenticator;
101107
private final Map<String, String> headers;
108+
private final SslContext grpcSslContext;
109+
private final SSLContext sslContext;
102110

103111
/**
104112
* Gets URL of the InfluxDB server.
@@ -219,6 +227,16 @@ public ProxySelector getProxy() {
219227
return proxy;
220228
}
221229

230+
/**
231+
* Gets the proxy for query api.
232+
*
233+
* @return the proxy, may be null
234+
*/
235+
@Nullable
236+
public ProxyDetector getQueryApiProxy() {
237+
return queryApiProxy;
238+
}
239+
222240
/**
223241
* Gets the (proxy) authenticator.
224242
*
@@ -239,6 +257,26 @@ public Map<String, String> getHeaders() {
239257
return headers;
240258
}
241259

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+
242280
/**
243281
* Validates the configuration properties.
244282
*/
@@ -269,17 +307,20 @@ public boolean equals(final Object o) {
269307
&& Objects.equals(allowHttpRedirects, that.allowHttpRedirects)
270308
&& Objects.equals(disableServerCertificateValidation, that.disableServerCertificateValidation)
271309
&& Objects.equals(proxy, that.proxy)
310+
&& Objects.equals(queryApiProxy, that.queryApiProxy)
272311
&& Objects.equals(authenticator, that.authenticator)
273-
&& Objects.equals(headers, that.headers);
312+
&& Objects.equals(headers, that.headers)
313+
&& Objects.equals(grpcSslContext, that.grpcSslContext)
314+
&& Objects.equals(sslContext, that.sslContext);
274315
}
275316

276317
@Override
277318
public int hashCode() {
278319
return Objects.hash(host, Arrays.hashCode(token), authScheme, organization,
279320
database, writePrecision, gzipThreshold,
280321
timeout, allowHttpRedirects, disableServerCertificateValidation,
281-
proxy, authenticator, headers,
282-
defaultTags);
322+
proxy, queryApiProxy, authenticator, headers,
323+
defaultTags, grpcSslContext, sslContext);
283324
}
284325

285326
@Override
@@ -294,9 +335,12 @@ public String toString() {
294335
.add("allowHttpRedirects=" + allowHttpRedirects)
295336
.add("disableServerCertificateValidation=" + disableServerCertificateValidation)
296337
.add("proxy=" + proxy)
338+
.add("queryApiProxy=" + queryApiProxy)
297339
.add("authenticator=" + authenticator)
298340
.add("headers=" + headers)
299341
.add("defaultTags=" + defaultTags)
342+
.add("grpcSslContext=" + grpcSslContext)
343+
.add("sslContext=" + sslContext)
300344
.toString();
301345
}
302346

@@ -318,8 +362,11 @@ public static final class Builder {
318362
private Boolean allowHttpRedirects;
319363
private Boolean disableServerCertificateValidation;
320364
private ProxySelector proxy;
365+
private ProxyDetector queryApiProxy;
321366
private Authenticator authenticator;
322367
private Map<String, String> headers;
368+
private SslContext grpcSslContext;
369+
private SSLContext sslContext;
323370

324371
/**
325372
* Sets the URL of the InfluxDB server.
@@ -480,6 +527,19 @@ public Builder proxy(@Nullable final ProxySelector proxy) {
480527
return this;
481528
}
482529

530+
/**
531+
* Sets the proxy detector for query api. Default is 'null'.
532+
*
533+
* @param proxy Proxy detector.
534+
* @return this
535+
*/
536+
@Nonnull
537+
public Builder queryApiProxy(@Nullable final ProxyDetector proxy) {
538+
539+
this.queryApiProxy = proxy;
540+
return this;
541+
}
542+
483543
/**
484544
* Sets the proxy authenticator. Default is 'null'.
485545
*
@@ -523,6 +583,32 @@ public Builder headers(@Nullable final Map<String, String> headers) {
523583
return this;
524584
}
525585

586+
/**
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'.
601+
*
602+
* @param sslContext The SSLContext
603+
* @return this
604+
*/
605+
@Nonnull
606+
public Builder sslContext(@Nullable final SSLContext sslContext) {
607+
608+
this.sslContext = sslContext;
609+
return this;
610+
}
611+
526612
/**
527613
* Build an instance of {@code ClientConfig}.
528614
*
@@ -658,7 +744,10 @@ private ClientConfig(@Nonnull final Builder builder) {
658744
disableServerCertificateValidation = builder.disableServerCertificateValidation != null
659745
? builder.disableServerCertificateValidation : false;
660746
proxy = builder.proxy;
747+
queryApiProxy = builder.queryApiProxy;
661748
authenticator = builder.authenticator;
662749
headers = builder.headers;
750+
grpcSslContext = builder.grpcSslContext;
751+
sslContext = builder.sslContext;
663752
}
664753
}

0 commit comments

Comments
 (0)