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

fix: Issue/623 #706

Merged
merged 20 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -694,12 +694,13 @@ private ParsedUrl(@Nonnull final String connectionString) {

HttpUrl url = this.httpUrl.newBuilder().build();

String urlWithoutParams = url.scheme() + "://" + url.host() + ":" + url.port() + url.encodedPath();
if (!urlWithoutParams.endsWith("/")) {
urlWithoutParams += "/";
}
//detect IPV6
String host = url.host().contains(":") ? "[" + url.host() + "]" : url.host();
String urlWithoutParams = url.scheme() + "://" + host + ":" + url.port() + url.encodedPath();

this.urlWithoutParams = urlWithoutParams;
this.urlWithoutParams = urlWithoutParams.endsWith("/")
? urlWithoutParams
: urlWithoutParams + "/";
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
*/
package com.influxdb.client;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.influxdb.client.domain.WritePrecision;

import com.influxdb.exceptions.InfluxException;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import org.assertj.core.api.Assertions;
Expand Down Expand Up @@ -156,4 +159,71 @@ public void customClientTypeFromProperties() {

Assertions.assertThat(options.getClientType()).isEqualTo("properties-service");
}

@Test
public void ipv6Loopback(){
String[] loopbacks = {"[::1]", "[0000:0000:0000:0000:0000:0000:0000:0001]"};

for (String loopback : loopbacks) {
InfluxDBClientOptions options = InfluxDBClientOptions.builder()
.url(String.format("http://%s:9999/api/v2/", loopback))
.authenticateToken("xyz".toCharArray())
.org("my-org")
.build();

Assertions.assertThat(options.getUrl()).isEqualTo("http://[::1]:9999/api/v2/");
Assertions.assertThat(options.getAuthScheme()).isEqualTo(InfluxDBClientOptions.AuthScheme.TOKEN);
Assertions.assertThat(options.getOkHttpClient()).isNotNull();
Assertions.assertThat(options.getPrecision()).isEqualTo(WritePrecision.NS);
Assertions.assertThat(options.getOrg()).isEqualTo("my-org");
}
}

@Test
public void ipv6General(){
Map<String, String> ipv6Expected = Map.of(
"[2001:db80:0001:1000:1100:0011:1110:0111]", "[2001:db80:1:1000:1100:11:1110:111]",
"[2001:db8:1000:0000:0000:0000:0000:0001]", "[2001:db8:1000::1]",
"[2001:db8f:0ff0:00ee:0ddd:000c:bbbb:aaaa]", "[2001:db8f:ff0:ee:ddd:c:bbbb:aaaa]",
"[2001:0db8:0000:0000:0000:9876:0000:001f]", "[2001:db8::9876:0:1f]",
"[0000:0000:0000:0000:0000:0000:0000:0000]", "[::]",
"[2001:0db8:fedc:edcb:dcba:cba9:ba98:a987]", "[2001:db8:fedc:edcb:dcba:cba9:ba98:a987]"//,
//"[::1]", ""
);

for(String key : ipv6Expected.keySet()){
InfluxDBClientOptions options = InfluxDBClientOptions.builder()
.url(String.format("http://%s:9999/api/v2/query?orgID=my-org", key))
.authenticateToken("xyz".toCharArray())
.build();

System.out.println(key + ": " + options.getUrl());

Assertions.assertThat(options.getUrl())
.isEqualTo(String.format("http://%s:9999/api/v2/query/", ipv6Expected.get(key)));
Assertions.assertThat(options.getToken())
.isEqualTo("xyz".toCharArray());
}
}

@Test
public void ipv6Invalid(){
List<String> invalidIpv6 = Arrays.asList(
"[:1]",
"[:::1]",
"[2001:db8:0000:1]",
"[2001:db8:00000::1]",
"[2001:db8:0000:::1]",
"[:0000::1]",
"[:::0000::1]");
for(String ipv6 : invalidIpv6){
Assertions.assertThatThrownBy(() -> { InfluxDBClientOptions options2 = InfluxDBClientOptions.builder()
.url(String.format("http://%s:9999/api/v2/query?orgID=my-org", ipv6))
.authenticateToken("xyz".toCharArray())
.build();}).isInstanceOf(InfluxException.class)
.hasMessage(String.format("Unable to parse connection string http://%s:9999/api/v2/query?orgID=my-org", ipv6));
}

}

}
84 changes: 84 additions & 0 deletions client/src/test/java/com/influxdb/client/InfluxDBClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
*/
package com.influxdb.client;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.List;
Expand All @@ -31,6 +33,9 @@
import java.util.logging.Logger;
import javax.annotation.Nonnull;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpHandler;
import com.influxdb.LogLevel;
import com.influxdb.client.domain.Authorization;
import com.influxdb.client.domain.Run;
Expand Down Expand Up @@ -469,6 +474,85 @@ public void redactedAuthorizationHeader() {
Assertions.assertThat(authorizationLog.getMessage()).isEqualTo("Authorization: ██");
}

static class HTTPServerThread extends Thread {
HttpServer server;
public HTTPServerThread(ServerHandler handler) throws IOException {
server = HttpServer.create(new InetSocketAddress(44404), 0);
server.createContext("/", handler);
}

@Override
public void run() {
server.start();
}
}

static class ServerHandler implements HttpHandler {

public String lastUri;
public String lastMethod;
public String lastPostBody;

@Override
public void handle(HttpExchange exchange) throws IOException {
lastUri = exchange.getRequestURI().toString();
lastMethod = exchange.getRequestMethod();
if(lastMethod.equalsIgnoreCase("POST")){
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for(int length; (length = exchange.getRequestBody().read(buffer)) != -1; ) {
os.write(buffer, 0, length);
}
lastPostBody = os.toString();
}
String response = "OK";
exchange.sendResponseHeaders(200, response.length());
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
}

@Test
public void ipv6Calls() throws InterruptedException, IOException {
ServerHandler handler = new ServerHandler();
HTTPServerThread st = new HTTPServerThread(handler);
st.start();

Thread.sleep(500);

InfluxDBClientOptions options = InfluxDBClientOptions.builder()
.url("http://[::1]:44404")
.bucket("my-bucket")
.org("my-org")
.build();
InfluxDBClient client = InfluxDBClientFactory.create(options);
WriteApiBlocking writer = client.getWriteApiBlocking();
writer.writeRecord(WritePrecision.NS, "mem,source=ff10 use=87");

Assertions.assertThat(handler.lastUri)
.isEqualTo("/api/v2/write?org=my-org&bucket=my-bucket&precision=ns");
Assertions.assertThat(handler.lastMethod).isEqualTo("POST");
Assertions.assertThat(handler.lastPostBody).isEqualTo("mem,source=ff10 use=87");

QueryApi queryApi = client.getQueryApi();
String qresult = queryApi
.queryRaw("from(bucket: \"my-bucket\") " +
"|> range(start: -1h) " +
"|> filter(fn: (r) => r._field == \"cpu\"");
Assertions.assertThat("OK").isEqualTo(qresult);
Assertions.assertThat(handler.lastUri).isEqualTo("/api/v2/query?org=my-org");
Assertions.assertThat(handler.lastMethod).isEqualTo("POST");
Assertions.assertThat(handler.lastPostBody)
.isEqualTo("{\"query\":\"from(bucket: \\\"my-bucket\\\") " +
"|\\u003e range(start: -1h) " +
"|\\u003e filter(fn: (r) " +
"\\u003d\\u003e r._field " +
"\\u003d\\u003d \\\"cpu\\\"\",\"type\":\"flux\",\"params\":{}}");

st.server.stop(0);
}

private void queryAndTest(final String expected) throws InterruptedException {
RecordedRequest request = takeRequest();
Assertions.assertThat(request).isNotNull();
Expand Down