Skip to content

Commit ee7fc92

Browse files
feat: validate host ipv4 and ipv6
1 parent 25dcd91 commit ee7fc92

3 files changed

Lines changed: 52 additions & 12 deletions

File tree

src/main/java/com/influxdb/v3/client/InfluxDBClient.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ Stream<VectorSchemaRoot> queryBatches(@Nonnull final String query,
483483
* Creates a new instance of the {@link InfluxDBClient} for interacting with an InfluxDB server, simplifying
484484
* common operations such as writing, querying.
485485
*
486-
* @param host the URL of the InfluxDB server
486+
* @param host the URL of the InfluxDB server. If IPv6 is using it must be wrapped inside brackets.
487487
* @param token the authentication token for accessing the InfluxDB server, can be null
488488
* @param database the database to be used for InfluxDB operations, can be null
489489
* @return new instance of the {@link InfluxDBClient}
@@ -505,7 +505,7 @@ static InfluxDBClient getInstance(@Nonnull final String host,
505505
* Creates a new instance of the {@link InfluxDBClient} for interacting with an InfluxDB server, simplifying
506506
* common operations such as writing, querying.
507507
*
508-
* @param host the URL of the InfluxDB server
508+
* @param host the URL of the InfluxDB server. If IPv6 is using it must be wrapped inside brackets.
509509
* @param token the authentication token for accessing the InfluxDB server, can be null
510510
* @param database the database to be used for InfluxDB operations, can be null
511511
* @param defaultTags tags to be added by default to writes of points
@@ -561,7 +561,7 @@ static InfluxDBClient getInstance(@Nonnull final ClientConfig config) {
561561
* <li>writeUseV2Api - use V2 API endpoint</li>
562562
* </ul>
563563
*
564-
* @param connectionString connection string
564+
* @param connectionString connection string. If IPv6 is using it must be wrapped inside brackets.
565565
* @return instance of {@link InfluxDBClient}
566566
*/
567567
@Nonnull
@@ -584,7 +584,7 @@ static InfluxDBClient getInstance(@Nonnull final String connectionString) {
584584
* <p>
585585
* Supported environment variables:
586586
* <ul>
587-
* <li>INFLUX_HOST - cloud/server URL <i>required</i></li>
587+
* <li>INFLUX_HOST - cloud/server URL. If IPv6 is using it must be wrapped inside brackets. <i>required</i></li>
588588
* <li>INFLUX_TOKEN - authentication token <i>required</i></li>
589589
* <li>INFLUX_AUTH_SCHEME - authentication scheme</li>
590590
* <li>INFLUX_ORG - organization name</li>
@@ -597,7 +597,7 @@ static InfluxDBClient getInstance(@Nonnull final String connectionString) {
597597
* </ul>
598598
* Supported system properties:
599599
* <ul>
600-
* <li>influx.host - cloud/server URL <i>required</i></li>
600+
* <li>influx.host - cloud/server URL. If IPv6 is using it must be wrapped inside brackets. <i>required</i></li>
601601
* <li>influx.token - authentication token <i>required</i></li>
602602
* <li>influx.authScheme - authentication scheme</li>
603603
* <li>influx.org - organization name</li>

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.net.Authenticator;
2525
import java.net.MalformedURLException;
2626
import java.net.ProxySelector;
27+
import java.net.URI;
28+
import java.net.URISyntaxException;
2729
import java.net.URL;
2830
import java.time.Duration;
2931
import java.util.Arrays;
@@ -48,7 +50,10 @@
4850
* <p>
4951
* You can configure following properties:
5052
* <ul>
51-
* <li><code>host</code> - hostname or IP address of the InfluxDB server</li>
53+
* <li>
54+
* <code>host</code> - hostname or IP address of the InfluxDB server.
55+
* If IPv6 is using it must be wrapped inside brackets.
56+
* </li>
5257
* <li><code>token</code> - authentication token for accessing the InfluxDB server</li>
5358
* <li><code>authScheme</code> - authentication scheme</li>
5459
* <li><code>organization</code> - organization to be used for operations</li>
@@ -375,8 +380,17 @@ public List<ClientInterceptor> getInterceptors() {
375380
* Validates the configuration properties.
376381
*/
377382
public void validate() {
378-
if (host == null || host.isBlank()) {
379-
throw new IllegalArgumentException("The URL of the InfluxDB server has to be defined.");
383+
try {
384+
if (host == null) {
385+
throw new IllegalArgumentException("Invalid URL.");
386+
}
387+
388+
URI uri = new URI(host);
389+
if (uri.getHost() == null) {
390+
throw new URISyntaxException(host, "Invalid URL.");
391+
}
392+
} catch (URISyntaxException e) {
393+
throw new IllegalArgumentException(e.getMessage());
380394
}
381395
}
382396

@@ -481,7 +495,7 @@ public static final class Builder {
481495
private List<ClientInterceptor> interceptors;
482496

483497
/**
484-
* Sets the URL of the InfluxDB server.
498+
* Sets the URL of the InfluxDB server. If IPv6 is using it must be wrapped inside brackets.
485499
*
486500
* @param host URL of the InfluxDB server
487501
* @return this
@@ -827,7 +841,7 @@ public ClientConfig build() {
827841
/**
828842
* Build an instance of {@code ClientConfig} from connection string.
829843
*
830-
* @param connectionString connection string in URL format
844+
* @param connectionString connection string in URL format. If IPv6 is using it must be wrapped inside brackets.
831845
* @return the configuration for an {@code InfluxDBClient}
832846
* @throws MalformedURLException when argument is not valid URL
833847
*/

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

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

24+
import java.net.URISyntaxException;
25+
import java.net.UnknownHostException;
26+
import java.util.List;
2427
import java.util.Map;
2528
import java.util.Properties;
2629

@@ -40,6 +43,30 @@ void withProxyUrl() {
4043
Assertions.assertThat(clientConfig.getProxyUrl()).isEqualTo(proxyUrl);
4144
}
4245

46+
@Test
47+
void parseIpv6() throws UnknownHostException, URISyntaxException {
48+
record Test(String url, boolean isCorrect) {
49+
}
50+
var tests = List.of(
51+
new Test("http://[2001:db8::1]/", true),
52+
new Test("http://[2001:db8:a0b:12f0::1]/index.html", true),
53+
new Test("http://[2001:db8:a0b:12f0::1]:80/index.html", true),
54+
new Test("https://[2001:db8:a0b:12f0::1%25eth0]:15000/", true),
55+
new Test("http://[2607:f8b0:4005:802::1007]/", true),
56+
new Test("http://2001:db8::1/", false),
57+
new Test("http://2001:db8::1:8080/", false)
58+
);
59+
for (Test test : tests) {
60+
if (!test.isCorrect()) {
61+
Assertions.assertThatThrownBy(() ->
62+
InfluxDBClient.getInstance(test.url(), "my-token".toCharArray(), "bucket0")
63+
).hasMessageContaining("Invalid URL.");
64+
} else {
65+
InfluxDBClient.getInstance(test.url(), "my-token".toCharArray(), "bucket0");
66+
}
67+
}
68+
}
69+
4370
@Test
4471
void withSslRootsFilePath() {
4572
String path = "/path/to/cert";
@@ -51,10 +78,9 @@ void withSslRootsFilePath() {
5178

5279
@Test
5380
void requiredHost() {
54-
5581
Assertions.assertThatThrownBy(() -> InfluxDBClient.getInstance(null, "my-token".toCharArray(), "my-database"))
5682
.isInstanceOf(IllegalArgumentException.class)
57-
.hasMessage("The URL of the InfluxDB server has to be defined.");
83+
.hasMessage("Invalid URL.");
5884
}
5985

6086
@Test

0 commit comments

Comments
 (0)