Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support proxy and ssl #230

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 1.1.0 [unreleased]

### Features

1. [#229](https://github.com/InfluxCommunity/influxdb3-java/pull/229): Support proxy and ssl

## 1.0.0 [2024-12-11]

### Features
Expand Down
34 changes: 34 additions & 0 deletions examples/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#
# The MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

version: "3"

services:
envoy:
image: envoyproxy/envoy:v1.26-latest
volumes:
- ./envoy.yaml:/etc/envoy/envoy.yaml
ports:
- "10000:10000"
environment:
- ENVOY_UID=0

84 changes: 84 additions & 0 deletions examples/envoy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#
# The MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

static_resources:
listeners:
- name: listener_0
address:
socket_address: { address: 0.0.0.0, port_value: 10000 }
filter_chains:
- filter_chain_match:
filters:
- name: envoy.filters.network.http_connection_manager
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
stat_prefix: ingress_http
access_log:
- name: envoy.access_loggers.stdout
typed_config:
"@type": type.googleapis.com/envoy.extensions.access_loggers.stream.v3.StdoutAccessLog
http2_protocol_options:
allow_connect: true
upgrade_configs:
- upgrade_type: CONNECT
http_filters:
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router

route_config:
name: local_route
virtual_hosts:
- name: local_service
domains: [ "*" ]
routes:
- match:
connect_matcher: { }
route:
cluster: influxdb_cluster
upgrade_configs:
upgrade_type: CONNECT
connect_config: { }
- match:
prefix: "/"
route:
cluster: influxdb_cluster
prefix_rewrite: "/"
auto_host_rewrite: true
timeout: 10s
cors:
allow_origin_string_match:
- prefix: "*"
allow_methods: GET, PUT, DELETE, POST, OPTIONS
clusters:
- name: influxdb_cluster
connect_timeout: 10s
type: STRICT_DNS
load_assignment:
cluster_name: influxdb_cluster
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: "us-east-1-1.aws.cloud2.influxdata.com"
port_value: 443
80 changes: 80 additions & 0 deletions examples/src/main/java/com/influxdb/v3/ProxyExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.influxdb.v3;

import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.URI;
import java.util.stream.Stream;

import io.grpc.HttpConnectProxiedSocketAddress;
import io.grpc.ProxyDetector;

import com.influxdb.v3.client.InfluxDBClient;
import com.influxdb.v3.client.Point;
import com.influxdb.v3.client.PointValues;
import com.influxdb.v3.client.config.ClientConfig;

public final class ProxyExample {

private ProxyExample() { }

public static void main(final String[] args) throws Exception {
// Run docker-compose.yml file to start Envoy proxy

URI queryProxyUri = new URI("proxyUrl");
URI uri = new URI(System.getenv("url"));

ProxyDetector proxyDetector = (targetServerAddress) -> {
InetSocketAddress targetAddress = (InetSocketAddress) targetServerAddress;
if (uri.getHost().equals(targetAddress.getHostString())) {
return HttpConnectProxiedSocketAddress.newBuilder()
.setProxyAddress(new InetSocketAddress(queryProxyUri.getHost(), queryProxyUri.getPort()))
.setTargetAddress(targetAddress)
.build();
}
return null;
};
ProxySelector proxy = ProxySelector.of(new InetSocketAddress(queryProxyUri.getHost(), queryProxyUri.getPort()));
ClientConfig clientConfig = new ClientConfig.Builder()
.host(uri.toString())
.token(System.getenv("token").toCharArray())
.database(System.getenv("database"))
.proxy(proxy)
.queryApiProxy(proxyDetector)
.build();

InfluxDBClient influxDBClient = InfluxDBClient.getInstance(clientConfig);
influxDBClient.writePoint(
Point.measurement("test1")
.setField("field", "field1")
);

try (Stream<PointValues> stream = influxDBClient.queryPoints("SELECT * FROM test1")) {
stream.findFirst()
.ifPresent(pointValues -> {
// do something
});
}
}
}

95 changes: 92 additions & 3 deletions src/main/java/com/influxdb/v3/client/config/ClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
import java.util.function.BiFunction;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.net.ssl.SSLContext;

import io.grpc.ProxyDetector;
import io.netty.handler.ssl.SslContext;

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

Expand All @@ -58,6 +62,7 @@
* disable server certificate validation for HTTPS connections
* </li>
* <li><code>proxy</code> - HTTP proxy selector</li>
* <li><code>queryApiProxy</code> - HTTP query detector</li>
* <li><code>authenticator</code> - HTTP proxy authenticator</li>
* <li><code>headers</code> - headers to be added to requests</li>
* </ul>
Expand Down Expand Up @@ -97,8 +102,11 @@
private final Boolean allowHttpRedirects;
private final Boolean disableServerCertificateValidation;
private final ProxySelector proxy;
private final ProxyDetector queryApiProxy;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This self-descriptive naming might be more precise as grpcApiProxy or grpcProxy. It would also match with the grpcSslContext naming below.

private final Authenticator authenticator;
private final Map<String, String> headers;
private final SslContext grpcSslContext;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see either grpcSslContext or sslContext in the Javadoc above the class.

private final SSLContext sslContext;

/**
* Gets URL of the InfluxDB server.
Expand Down Expand Up @@ -219,6 +227,16 @@
return proxy;
}

/**
* Gets the proxy for query api.
*
* @return the proxy, may be null
*/
@Nullable
public ProxyDetector getQueryApiProxy() {
return queryApiProxy;
}

/**
* Gets the (proxy) authenticator.
*
Expand All @@ -239,6 +257,26 @@
return headers;
}

/**
* Gets SslContext object from grpc.
*
* @return the SslContext object
*/
@Nullable
public SslContext getGrpcSslContext() {
return grpcSslContext;
}

/**
* Gets SSLContext object.
*
* @return the SSLContext object
*/
@Nullable
public SSLContext getSslContext() {
return sslContext;
}

/**
* Validates the configuration properties.
*/
Expand Down Expand Up @@ -269,17 +307,20 @@
&& Objects.equals(allowHttpRedirects, that.allowHttpRedirects)
&& Objects.equals(disableServerCertificateValidation, that.disableServerCertificateValidation)
&& Objects.equals(proxy, that.proxy)
&& Objects.equals(queryApiProxy, that.queryApiProxy)
&& Objects.equals(authenticator, that.authenticator)
&& Objects.equals(headers, that.headers);
&& Objects.equals(headers, that.headers)
&& Objects.equals(grpcSslContext, that.grpcSslContext)
&& Objects.equals(sslContext, that.sslContext);
}

@Override
public int hashCode() {
return Objects.hash(host, Arrays.hashCode(token), authScheme, organization,
database, writePrecision, gzipThreshold,
timeout, allowHttpRedirects, disableServerCertificateValidation,
proxy, authenticator, headers,
defaultTags);
proxy, queryApiProxy, authenticator, headers,
defaultTags, grpcSslContext, sslContext);
}

@Override
Expand All @@ -294,9 +335,12 @@
.add("allowHttpRedirects=" + allowHttpRedirects)
.add("disableServerCertificateValidation=" + disableServerCertificateValidation)
.add("proxy=" + proxy)
.add("queryApiProxy=" + queryApiProxy)
.add("authenticator=" + authenticator)
.add("headers=" + headers)
.add("defaultTags=" + defaultTags)
.add("grpcSslContext=" + grpcSslContext)
.add("sslContext=" + sslContext)
.toString();
}

Expand All @@ -318,8 +362,11 @@
private Boolean allowHttpRedirects;
private Boolean disableServerCertificateValidation;
private ProxySelector proxy;
private ProxyDetector queryApiProxy;
private Authenticator authenticator;
private Map<String, String> headers;
private SslContext grpcSslContext;
private SSLContext sslContext;

/**
* Sets the URL of the InfluxDB server.
Expand Down Expand Up @@ -480,6 +527,19 @@
return this;
}

/**
* Sets the proxy detector for query api. Default is 'null'.
*
* @param proxy Proxy detector.
* @return this
*/
@Nonnull
public Builder queryApiProxy(@Nullable final ProxyDetector proxy) {

this.queryApiProxy = proxy;
return this;

Check warning on line 540 in src/main/java/com/influxdb/v3/client/config/ClientConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/influxdb/v3/client/config/ClientConfig.java#L539-L540

Added lines #L539 - L540 were not covered by tests
}

/**
* Sets the proxy authenticator. Default is 'null'.
*
Expand Down Expand Up @@ -523,6 +583,32 @@
return this;
}

/**
* Sets SslContext for grpc client. Default is 'null'.
*
* @param grpcSslContext The SSLContext
* @return this
*/
@Nonnull
public Builder grpcSslContext(@Nullable final SslContext grpcSslContext) {

this.grpcSslContext = grpcSslContext;
return this;
}

/**
* Sets SSLContext for rest client. Default is 'null'.
*
* @param sslContext The SSLContext
* @return this
*/
@Nonnull
public Builder sslContext(@Nullable final SSLContext sslContext) {

this.sslContext = sslContext;
return this;
}

/**
* Build an instance of {@code ClientConfig}.
*
Expand Down Expand Up @@ -658,7 +744,10 @@
disableServerCertificateValidation = builder.disableServerCertificateValidation != null
? builder.disableServerCertificateValidation : false;
proxy = builder.proxy;
queryApiProxy = builder.queryApiProxy;
authenticator = builder.authenticator;
headers = builder.headers;
grpcSslContext = builder.grpcSslContext;
sslContext = builder.sslContext;
}
}
Loading
Loading