Skip to content

Commit cdfc402

Browse files
wip
1 parent 7f82762 commit cdfc402

File tree

7 files changed

+110
-112
lines changed

7 files changed

+110
-112
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public void configureChannelProxy(Supplier<HttpProxyHandler> configureHttpProxyH
4747
this.httpProxyHandler = configureHttpProxyHandler.get();
4848
}
4949

50-
public void configureManagedChannelProxy(Supplier<ProxyDetector> configureManagedChannelProxy) {
51-
this.proxyDetector = configureManagedChannelProxy.get();
50+
public void configureManagedChannelProxy(ProxyDetector configureManagedChannelProxy) {
51+
this.proxyDetector = configureManagedChannelProxy;
5252
}
5353

5454
public SslContext getSslContext() {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,8 @@ private FlightClient createFlightClient(@Nonnull final ClientConfig config) thro
139139
nettyChannelBuilder.usePlaintext();
140140
}
141141

142-
if (config.getProxyUrl() != null) {
143-
ProxyDetector proxyDetector = createProxyDetector(config.getHost(), config.getProxyUrl());
144-
nettyChannelBuilder.proxyDetector(proxyDetector);
142+
if (config.getNettyHttpClientConfig() != null && config.getNettyHttpClientConfig().getProxyDetector() != null) {
143+
nettyChannelBuilder.proxyDetector(config.getNettyHttpClientConfig().getProxyDetector());
145144
}
146145

147146
if (config.getProxy() != null) {

src/test/java/com/influxdb/v3/client/InfluxDBClientWriteTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ public void writeTimeoutExceededTest() {
607607
public void writeTimeoutOKTest() {
608608
mockServer.enqueue(createResponse(200));
609609

610-
Duration testDuration = Duration.ofMillis(2000);
610+
Duration testDuration = Duration.ofMillis(2700);
611611

612612
ClientConfig config = new ClientConfig.Builder()
613613
.host(baseURL)

src/test/java/com/influxdb/v3/client/TestUtils.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222
package com.influxdb.v3.client;
2323

24+
import io.grpc.HttpConnectProxiedSocketAddress;
25+
import io.grpc.ProxyDetector;
2426
import io.netty.handler.ssl.ClientAuth;
2527
import io.netty.handler.ssl.SslContext;
2628
import io.netty.handler.ssl.SslContextBuilder;
@@ -40,10 +42,12 @@
4042
import org.jetbrains.annotations.NotNull;
4143

4244
import javax.annotation.Nonnull;
45+
import javax.annotation.Nullable;
4346
import javax.net.ssl.KeyManagerFactory;
4447
import javax.net.ssl.TrustManagerFactory;
4548
import java.io.FileInputStream;
4649
import java.io.IOException;
50+
import java.net.InetSocketAddress;
4751
import java.net.URI;
4852
import java.nio.charset.StandardCharsets;
4953
import java.security.KeyStore;
@@ -132,6 +136,24 @@ public static SslContext createNettySslContext(boolean isServer, String format,
132136
return sslContextBuilder.build();
133137
}
134138

139+
public static ProxyDetector createProxyDetector(@Nonnull final String targetUrl, @Nonnull final String proxyUrl, @Nullable final String username, @Nullable final String password) {
140+
URI targetUri = URI.create(targetUrl);
141+
URI proxyUri = URI.create(proxyUrl);
142+
return (targetServerAddress) -> {
143+
InetSocketAddress targetAddress = (InetSocketAddress) targetServerAddress;
144+
if (targetUri.getHost().equals(targetAddress.getHostString())
145+
&& targetUri.getPort() == targetAddress.getPort()) {
146+
return HttpConnectProxiedSocketAddress.newBuilder()
147+
.setProxyAddress(new InetSocketAddress(proxyUri.getHost(), proxyUri.getPort()))
148+
.setTargetAddress(targetAddress)
149+
.setUsername(username)
150+
.setPassword(password)
151+
.build();
152+
}
153+
return null;
154+
};
155+
}
156+
135157
@NotNull
136158
private static TrustManagerFactory getTrustManagerFactory(String format, String password, String trustFilePath) throws NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException {
137159
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());

src/test/java/com/influxdb/v3/client/integration/E2ETest.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@
2424
import com.influxdb.v3.client.InfluxDBClient;
2525
import com.influxdb.v3.client.Point;
2626
import com.influxdb.v3.client.PointValues;
27+
import com.influxdb.v3.client.TestUtils;
2728
import com.influxdb.v3.client.config.ClientConfig;
29+
import com.influxdb.v3.client.config.NettyHttpClientConfig;
2830
import com.influxdb.v3.client.query.QueryOptions;
2931
import com.influxdb.v3.client.write.WriteOptions;
3032
import com.influxdb.v3.client.write.WritePrecision;
33+
import io.grpc.ProxyDetector;
34+
import io.netty.handler.proxy.HttpProxyHandler;
3135
import org.apache.arrow.flight.FlightRuntimeException;
3236
import org.assertj.core.api.Assertions;
3337
import org.junit.jupiter.api.Test;
@@ -36,16 +40,14 @@
3640
import javax.annotation.Nonnull;
3741
import javax.net.ssl.SSLException;
3842
import java.math.BigInteger;
39-
import java.net.ConnectException;
40-
import java.net.URISyntaxException;
41-
import java.net.URL;
42-
import java.net.URLConnection;
43+
import java.net.*;
4344
import java.time.Instant;
4445
import java.time.temporal.ChronoUnit;
4546
import java.util.ArrayList;
4647
import java.util.List;
4748
import java.util.Map;
4849
import java.util.UUID;
50+
import java.util.function.Supplier;
4951
import java.util.logging.Logger;
5052
import java.util.stream.Collectors;
5153
import java.util.stream.Stream;
@@ -62,6 +64,9 @@ public class E2ETest {
6264
@Test
6365
void testQueryWithProxy() throws URISyntaxException, SSLException {
6466
String proxyUrl = "http://localhost:10000";
67+
String targetUrl = "http://localhost:8086";
68+
String username = "username";
69+
String password = "password";
6570

6671
try {
6772
// Continue to run this test only if Envoy proxy is running on this address http://localhost:10000
@@ -76,11 +81,21 @@ void testQueryWithProxy() throws URISyntaxException, SSLException {
7681
}
7782
}
7883

84+
NettyHttpClientConfig nettyHttpClientConfig = new NettyHttpClientConfig();
85+
86+
// Set proxy for write api
87+
Supplier<HttpProxyHandler> writeApiProxy = () -> new HttpProxyHandler(new InetSocketAddress("localhost", 10000));
88+
nettyHttpClientConfig.configureChannelProxy(writeApiProxy);
89+
90+
// Set proxy for query api
91+
ProxyDetector proxyDetector = TestUtils.createProxyDetector(targetUrl, proxyUrl, username, password);
92+
nettyHttpClientConfig.configureManagedChannelProxy(proxyDetector);
93+
7994
ClientConfig clientConfig = new ClientConfig.Builder()
8095
.host(System.getenv("TESTING_INFLUXDB_URL"))
8196
.token(System.getenv("TESTING_INFLUXDB_TOKEN").toCharArray())
8297
.database(System.getenv("TESTING_INFLUXDB_DATABASE"))
83-
.proxyUrl(proxyUrl)
98+
.nettyHttpClientConfig(nettyHttpClientConfig)
8499
.build();
85100

86101
InfluxDBClient influxDBClient = InfluxDBClient.getInstance(clientConfig);

src/test/java/com/influxdb/v3/client/internal/FlightSqlClientTest.java

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

24+
import java.io.IOException;
2425
import java.net.InetSocketAddress;
26+
import java.net.URI;
2527
import java.net.URISyntaxException;
2628
import java.util.HashMap;
2729
import java.util.Map;
2830
import java.util.stream.IntStream;
2931
import java.util.stream.Stream;
3032

33+
import com.influxdb.v3.client.config.NettyHttpClientConfig;
3134
import io.grpc.HttpConnectProxiedSocketAddress;
3235
import io.grpc.ProxyDetector;
3336
import io.grpc.internal.GrpcUtil;
@@ -346,28 +349,38 @@ void flightSqlClient() throws Exception {
346349
}
347350

348351
@Test
349-
void createProxyDetector() {
350-
String targetUrl = "https://localhost:80";
352+
void createProxyDetector() throws IOException, URISyntaxException {
353+
String target = "https://localhost:80";
354+
String proxy = "http://localhost:10000";
355+
356+
var targetUrl = new URI(target) ;
357+
var proxyUrl = new URI(proxy);
358+
String username = "username";
359+
String password = "password";
360+
361+
ProxyDetector proxyDetector = TestUtils.createProxyDetector(target, proxy, username, password);
362+
363+
NettyHttpClientConfig nettyHttpClientConfig = new NettyHttpClientConfig();
364+
nettyHttpClientConfig.configureManagedChannelProxy(proxyDetector);
365+
351366
ClientConfig clientConfig = new ClientConfig.Builder()
352-
.host(targetUrl)
353-
.build();
354-
try (FlightSqlClient flightSqlClient = new FlightSqlClient(clientConfig)) {
355-
String proxyUrl = "http://localhost:10000";
356-
ProxyDetector proxyDetector = flightSqlClient.createProxyDetector(targetUrl, proxyUrl);
357-
Assertions.assertThat(proxyDetector.proxyFor(
358-
new InetSocketAddress("localhost", 80)
367+
.host(target)
368+
.nettyHttpClientConfig(nettyHttpClientConfig)
369+
.build();
370+
371+
Assertions.assertThat(clientConfig.getNettyHttpClientConfig().getProxyDetector().proxyFor(
372+
new InetSocketAddress(targetUrl.getHost(), targetUrl.getPort())
359373
)).isEqualTo(HttpConnectProxiedSocketAddress.newBuilder()
360-
.setProxyAddress(new InetSocketAddress("localhost", 10000))
361-
.setTargetAddress(new InetSocketAddress("localhost", 80))
374+
.setTargetAddress(new InetSocketAddress(targetUrl.getHost(), targetUrl.getPort()))
375+
.setProxyAddress(new InetSocketAddress(proxyUrl.getHost(), proxyUrl.getPort()))
376+
.setUsername(username)
377+
.setPassword(password)
362378
.build());
363379

364-
// Return null case
365-
Assertions.assertThat(proxyDetector.proxyFor(
380+
// Return null case
381+
Assertions.assertThat(proxyDetector.proxyFor(
366382
new InetSocketAddress("123.2.3.1", 80)
367-
)).isNull();
368-
} catch (Exception e) {
369-
throw new RuntimeException(e);
370-
}
383+
)).isNull();
371384
}
372385

373386
static class HeaderCaptureMiddleware implements FlightServerMiddleware {

0 commit comments

Comments
 (0)