Skip to content

Commit b67f423

Browse files
authored
fix: add Edge authentication support (#158)
1 parent c08c23a commit b67f423

6 files changed

Lines changed: 95 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
1. [#148](https://github.com/InfluxCommunity/influxdb3-java/pull/148): InfluxDB Edge (OSS) error handling
66
1. [#153](https://github.com/InfluxCommunity/influxdb3-java/pull/153): Parsing timestamp columns
7+
1. [#158](https://github.com/InfluxCommunity/influxdb3-java/pull/158): Add InfluxDB Edge (OSS) authentication support.
78

89
## 0.8.0 [2024-06-24]
910

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ static InfluxDBClient getInstance(@Nonnull final String connectionString) {
444444
* <ul>
445445
* <li>INFLUX_HOST - cloud/server URL <i>required</i></li>
446446
* <li>INFLUX_TOKEN - authentication token <i>required</i></li>
447+
* <li>INFLUX_AUTH_SCHEME - authentication scheme</li>
447448
* <li>INFLUX_ORG - organization name</li>
448449
* <li>INFLUX_DATABASE - database (bucket) name</li>
449450
* <li>INFLUX_PRECISION - timestamp precision when writing data</li>
@@ -453,6 +454,7 @@ static InfluxDBClient getInstance(@Nonnull final String connectionString) {
453454
* <ul>
454455
* <li>influx.host - cloud/server URL <i>required</i></li>
455456
* <li>influx.token - authentication token <i>required</i></li>
457+
* <li>influx.authScheme - authentication scheme</li>
456458
* <li>influx.org - organization name</li>
457459
* <li>influx.database - database (bucket) name</li>
458460
* <li>influx.precision - timestamp precision when writing data</li>

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* <ul>
4747
* <li><code>host</code> - hostname or IP address of the InfluxDB server</li>
4848
* <li><code>token</code> - authentication token for accessing the InfluxDB server</li>
49+
* <li><code>authScheme</code> - authentication scheme</li>
4950
* <li><code>organization</code> - organization to be used for operations</li>
5051
* <li><code>database</code> - database to be used for InfluxDB operations</li>
5152
* <li><code>writePrecision</code> - precision to use when writing points to InfluxDB</li>
@@ -86,6 +87,7 @@ public final class ClientConfig {
8687

8788
private final String host;
8889
private final char[] token;
90+
private final String authScheme;
8991
private final String organization;
9092
private final String database;
9193
private final WritePrecision writePrecision;
@@ -118,6 +120,16 @@ public char[] getToken() {
118120
return token;
119121
}
120122

123+
/**
124+
* Gets authentication scheme.
125+
*
126+
* @return authentication scheme, may be null
127+
*/
128+
@Nullable
129+
public String getAuthScheme() {
130+
return authScheme;
131+
}
132+
121133
/**
122134
* Gets organization to be used for operations.
123135
*
@@ -247,6 +259,7 @@ public boolean equals(final Object o) {
247259
ClientConfig that = (ClientConfig) o;
248260
return Objects.equals(host, that.host)
249261
&& Arrays.equals(token, that.token)
262+
&& Objects.equals(authScheme, that.authScheme)
250263
&& Objects.equals(organization, that.organization)
251264
&& Objects.equals(database, that.database)
252265
&& writePrecision == that.writePrecision
@@ -262,7 +275,7 @@ public boolean equals(final Object o) {
262275

263276
@Override
264277
public int hashCode() {
265-
return Objects.hash(host, Arrays.hashCode(token), organization,
278+
return Objects.hash(host, Arrays.hashCode(token), authScheme, organization,
266279
database, writePrecision, gzipThreshold,
267280
timeout, allowHttpRedirects, disableServerCertificateValidation,
268281
proxy, authenticator, headers,
@@ -295,6 +308,7 @@ public String toString() {
295308
public static final class Builder {
296309
private String host;
297310
private char[] token;
311+
private String authScheme;
298312
private String organization;
299313
private String database;
300314
private WritePrecision writePrecision;
@@ -333,6 +347,20 @@ public Builder token(@Nullable final char[] token) {
333347
return this;
334348
}
335349

350+
/**
351+
* Sets authentication scheme.
352+
*
353+
* @param authScheme authentication scheme.
354+
* Default <code>null</code> for Cloud access, set to 'Bearer' for Edge.
355+
* @return this
356+
*/
357+
@Nonnull
358+
public Builder authScheme(@Nullable final String authScheme) {
359+
360+
this.authScheme = authScheme;
361+
return this;
362+
}
363+
336364
/**
337365
* Sets organization to be used for operations.
338366
*
@@ -525,6 +553,9 @@ public ClientConfig build(@Nonnull final String connectionString) throws Malform
525553
if (parameters.containsKey("token")) {
526554
this.token(parameters.get("token").toCharArray());
527555
}
556+
if (parameters.containsKey("authScheme")) {
557+
this.authScheme(parameters.get("authScheme"));
558+
}
528559
if (parameters.containsKey("org")) {
529560
this.organization(parameters.get("org"));
530561
}
@@ -565,6 +596,10 @@ public ClientConfig build(@Nonnull final Map<String, String> env, final Properti
565596
if (token != null) {
566597
this.token(token.toCharArray());
567598
}
599+
final String authScheme = get.apply("INFLUX_AUTH_SCHEME", "influx.authScheme");
600+
if (authScheme != null) {
601+
this.authScheme(authScheme);
602+
}
568603
final String org = get.apply("INFLUX_ORG", "influx.org");
569604
if (org != null) {
570605
this.organization(org);
@@ -612,6 +647,7 @@ private WritePrecision parsePrecision(@Nonnull final String precision) {
612647
private ClientConfig(@Nonnull final Builder builder) {
613648
host = builder.host;
614649
token = builder.token;
650+
authScheme = builder.authScheme;
615651
organization = builder.organization;
616652
database = builder.database;
617653
writePrecision = builder.writePrecision != null ? builder.writePrecision : WritePrecision.NS;

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,11 @@ void request(@Nonnull final String path,
162162
}
163163
request.header("User-Agent", userAgent);
164164
if (config.getToken() != null && config.getToken().length > 0) {
165-
request.header("Authorization", String.format("Token %s", new String(config.getToken())));
165+
String authScheme = config.getAuthScheme();
166+
if (authScheme == null) {
167+
authScheme = "Token";
168+
}
169+
request.header("Authorization", String.format("%s %s", authScheme, new String(config.getToken())));
166170
}
167171

168172
HttpResponse<String> response;

src/test/java/com/influxdb/v3/client/config/ClientConfigTest.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ void fromConnectionString() throws MalformedURLException {
117117
Assertions.assertThat(cfg.getDatabase()).isEqualTo(null);
118118
Assertions.assertThat(cfg.getWritePrecision()).isEqualTo(WritePrecision.S);
119119
Assertions.assertThat(cfg.getGzipThreshold()).isEqualTo(1000); // default
120+
121+
cfg = new ClientConfig.Builder()
122+
.build("http://localhost:9999/"
123+
+ "?token=my-token&authScheme=my-auth");
124+
Assertions.assertThat(cfg.getHost()).isEqualTo("http://localhost:9999/");
125+
Assertions.assertThat(cfg.getToken()).isEqualTo("my-token".toCharArray());
126+
Assertions.assertThat(cfg.getAuthScheme()).isEqualTo("my-auth");
120127
}
121128

122129
@Test
@@ -136,7 +143,7 @@ void fromEnv() {
136143
Assertions.assertThat(cfg.getWritePrecision()).isEqualTo(WritePrecision.NS);
137144
Assertions.assertThat(cfg.getGzipThreshold()).isEqualTo(1000);
138145

139-
// simple
146+
// basic
140147
env = Map.of(
141148
"INFLUX_HOST", "http://localhost:9999/",
142149
"INFLUX_TOKEN", "my-token",
@@ -153,6 +160,18 @@ void fromEnv() {
153160
Assertions.assertThat(cfg.getWritePrecision()).isEqualTo(WritePrecision.NS);
154161
Assertions.assertThat(cfg.getGzipThreshold()).isEqualTo(1000);
155162

163+
// with Edge authentication
164+
env = Map.of(
165+
"INFLUX_HOST", "http://localhost:9999/",
166+
"INFLUX_TOKEN", "my-token",
167+
"INFLUX_AUTH_SCHEME", "my-auth"
168+
);
169+
cfg = new ClientConfig.Builder()
170+
.build(env, null);
171+
Assertions.assertThat(cfg.getHost()).isEqualTo("http://localhost:9999/");
172+
Assertions.assertThat(cfg.getToken()).isEqualTo("my-token".toCharArray());
173+
Assertions.assertThat(cfg.getAuthScheme()).isEqualTo("my-auth");
174+
156175
// with write options
157176
env = Map.of(
158177
"INFLUX_HOST", "http://localhost:9999/",
@@ -188,7 +207,7 @@ void fromSystemProperties() {
188207
Assertions.assertThat(cfg.getWritePrecision()).isEqualTo(WritePrecision.NS);
189208
Assertions.assertThat(cfg.getGzipThreshold()).isEqualTo(1000);
190209

191-
// simple
210+
// basic
192211
properties = new Properties();
193212
properties.put("influx.host", "http://localhost:9999/");
194213
properties.put("influx.token", "my-token");
@@ -204,6 +223,17 @@ void fromSystemProperties() {
204223
Assertions.assertThat(cfg.getWritePrecision()).isEqualTo(WritePrecision.NS);
205224
Assertions.assertThat(cfg.getGzipThreshold()).isEqualTo(1000);
206225

226+
// with custom auth scheme
227+
properties = new Properties();
228+
properties.put("influx.host", "http://localhost:9999/");
229+
properties.put("influx.token", "my-token");
230+
properties.put("influx.authScheme", "my-auth");
231+
cfg = new ClientConfig.Builder()
232+
.build(new HashMap<>(), properties);
233+
Assertions.assertThat(cfg.getHost()).isEqualTo("http://localhost:9999/");
234+
Assertions.assertThat(cfg.getToken()).isEqualTo("my-token".toCharArray());
235+
Assertions.assertThat(cfg.getAuthScheme()).isEqualTo("my-auth");
236+
207237
// with write options
208238
properties = new Properties();
209239
properties.put("influx.host", "http://localhost:9999/");

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,24 @@ public void authenticationHeader() throws InterruptedException {
110110
Assertions.assertThat(authorization).isEqualTo("Token my-token");
111111
}
112112

113+
@Test
114+
public void authenticationHeaderCustomAuthScheme() throws InterruptedException {
115+
mockServer.enqueue(createResponse(200));
116+
117+
restClient = new RestClient(new ClientConfig.Builder()
118+
.host(baseURL)
119+
.token("my-token".toCharArray())
120+
.authScheme("my-auth-scheme")
121+
.build());
122+
123+
restClient.request("ping", HttpMethod.GET, null, null, null);
124+
125+
RecordedRequest recordedRequest = mockServer.takeRequest();
126+
127+
String authorization = recordedRequest.getHeader("Authorization");
128+
Assertions.assertThat(authorization).isEqualTo("my-auth-scheme my-token");
129+
}
130+
113131
@Test
114132
public void authenticationHeaderNotDefined() throws InterruptedException {
115133
mockServer.enqueue(createResponse(200));

0 commit comments

Comments
 (0)