Skip to content

Commit b81b7ec

Browse files
authored
fix: redact the Authorization HTTP header from log (#372)
1 parent 5eeb524 commit b81b7ec

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
* Add TruncateTimeColumnFlux [FluxDSL]
1111
* Add ArrayFromFlux [FluxDSL]
1212
* Add UnionFlux [FluxDSL]
13-
13+
14+
### Bug Fixes
15+
1. [#372](https://github.com/influxdata/influxdb-client-java/pull/372): Redact the `Authorization` HTTP header from log
16+
1417
## 6.3.0 [2022-06-30]
1518

1619
### Features

client/src/main/java/com/influxdb/client/internal/AbstractInfluxDBClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public AbstractInfluxDBClient(@Nonnull final InfluxDBClientOptions options,
8989

9090
this.options = options;
9191
this.loggingInterceptor = new HttpLoggingInterceptor();
92+
this.loggingInterceptor.redactHeader("Authorization");
9293
setLogLevel(loggingInterceptor, options.getLogLevel());
9394
this.authenticateInterceptor = new AuthenticateInterceptor(options);
9495
this.gzipInterceptor = new GzipInterceptor();
@@ -103,8 +104,8 @@ public AbstractInfluxDBClient(@Nonnull final InfluxDBClientOptions options,
103104
//
104105
//.retryOnConnectionFailure(false)
105106
.addInterceptor(new UserAgentInterceptor(customClientType))
106-
.addInterceptor(this.loggingInterceptor)
107107
.addInterceptor(this.authenticateInterceptor)
108+
.addInterceptor(this.loggingInterceptor)
108109
.addInterceptor(this.gzipInterceptor)
109110
.build();
110111

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
import java.io.IOException;
2525
import java.net.InetSocketAddress;
2626
import java.net.Proxy;
27+
import java.util.List;
2728
import java.util.Objects;
29+
import java.util.logging.Level;
30+
import java.util.logging.LogRecord;
31+
import java.util.logging.Logger;
2832
import javax.annotation.Nonnull;
2933

3034
import com.influxdb.LogLevel;
@@ -440,6 +444,34 @@ public void customClientType() throws InterruptedException {
440444
}
441445
}
442446

447+
@Test
448+
public void redactedAuthorizationHeader() {
449+
450+
mockServer.enqueue(new MockResponse());
451+
452+
MockLogHandler handler = new MockLogHandler();
453+
454+
final Logger logger = Logger.getLogger("okhttp3.OkHttpClient");
455+
logger.addHandler(handler);
456+
457+
try (InfluxDBClient client = InfluxDBClientFactory.create(mockServer.url("/").toString(), "my-token".toCharArray())) {
458+
client.setLogLevel(LogLevel.HEADERS);
459+
client
460+
.getWriteApiBlocking()
461+
.writeRecord("my-bucket", "my-org", WritePrecision.NS, "m2m,tag=a field=1");
462+
}
463+
464+
List<LogRecord> records = handler.getRecords(Level.INFO);
465+
466+
LogRecord authorizationLog = records
467+
.stream()
468+
.filter(it -> it.getMessage().startsWith("Authorization: "))
469+
.findFirst()
470+
.get();
471+
472+
Assertions.assertThat(authorizationLog.getMessage()).isEqualTo("Authorization: ██");
473+
}
474+
443475
private void queryAndTest(final String expected) throws InterruptedException {
444476
RecordedRequest request = takeRequest();
445477
Assertions.assertThat(request).isNotNull();

0 commit comments

Comments
 (0)