-
Notifications
You must be signed in to change notification settings - Fork 4
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
NguyenHoangSon96
wants to merge
4
commits into
main
Choose a base branch
from
feat/support-proxy-and-ssl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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> | ||
|
@@ -97,8 +102,11 @@ | |
private final Boolean allowHttpRedirects; | ||
private final Boolean disableServerCertificateValidation; | ||
private final ProxySelector proxy; | ||
private final ProxyDetector queryApiProxy; | ||
private final Authenticator authenticator; | ||
private final Map<String, String> headers; | ||
private final SslContext grpcSslContext; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see either |
||
private final SSLContext sslContext; | ||
|
||
/** | ||
* Gets URL of the InfluxDB server. | ||
|
@@ -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. | ||
* | ||
|
@@ -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. | ||
*/ | ||
|
@@ -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 | ||
|
@@ -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(); | ||
} | ||
|
||
|
@@ -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. | ||
|
@@ -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; | ||
} | ||
|
||
/** | ||
* Sets the proxy authenticator. Default is 'null'. | ||
* | ||
|
@@ -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}. | ||
* | ||
|
@@ -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; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
orgrpcProxy
. It would also match with thegrpcSslContext
naming below.