Skip to content

Commit 8189dae

Browse files
authored
Add param for logging sensitive auth headers in CDAHttpException class. (#294)
1 parent d4751aa commit 8189dae

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

src/main/java/com/contentful/java/cda/CDAClient.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ public class CDAClient {
7373

7474
final boolean preview;
7575

76+
final boolean logSensitiveData;
77+
7678
CDAClient(Builder builder) {
7779
this(new Cache(),
7880
Platform.get().callbackExecutor(),
@@ -89,6 +91,7 @@ public class CDAClient {
8991
this.environmentId = builder.environment;
9092
this.token = builder.token;
9193
this.preview = builder.preview;
94+
this.logSensitiveData = builder.logSensitiveData;
9295
}
9396

9497
private void validate(Builder builder) {
@@ -306,6 +309,10 @@ public SyncQuery sync(SyncType type) {
306309
return sync(null, null, type);
307310
}
308311

312+
313+
public boolean shouldLogSensitiveData() {
314+
return logSensitiveData;
315+
}
309316
/**
310317
* Returns a {@link SyncQuery} for synchronization with the provided {@code syncToken} via
311318
* the Sync API.
@@ -532,6 +539,8 @@ public static class Builder {
532539
Converter.Factory converterFactory;
533540

534541
boolean preview;
542+
543+
private boolean logSensitiveData = true;
535544
Tls12Implementation tls12Implementation = useRecommendation;
536545

537546
Section application;
@@ -564,6 +573,18 @@ public Builder setEnvironment(String environment) {
564573
return this;
565574
}
566575

576+
/**
577+
* Sets the logSensitiveData value for logging
578+
* the authorization headers in the CDAHttpException.
579+
*
580+
* @param logSensitiveData boolean value to be set.
581+
* @return this builder for chaining.
582+
*/
583+
public Builder setLogSensitiveData(boolean logSensitiveData) {
584+
this.logSensitiveData = logSensitiveData;
585+
return this;
586+
}
587+
567588
/**
568589
* Sets the space access token.
569590
*
@@ -758,7 +779,7 @@ public OkHttpClient.Builder defaultCallFactoryBuilder() {
758779
.addInterceptor(new AuthorizationHeaderInterceptor(token))
759780
.addInterceptor(new UserAgentHeaderInterceptor(createUserAgent()))
760781
.addInterceptor(new ContentfulUserAgentHeaderInterceptor(sections))
761-
.addInterceptor(new ErrorInterceptor());
782+
.addInterceptor(new ErrorInterceptor(logSensitiveData));
762783

763784
setLogger(okBuilder);
764785
useTls12IfWanted(okBuilder);

src/main/java/com/contentful/java/cda/CDAHttpException.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class CDAHttpException extends RuntimeException {
2222
private final String responseBody;
2323
private final String stringRepresentation;
2424

25+
private final boolean logSensitiveData;
26+
2527
/**
2628
* Construct an error response.
2729
* <p>
@@ -32,12 +34,13 @@ public class CDAHttpException extends RuntimeException {
3234
* @param request the request issuing the error.
3335
* @param response the response from the server to this faulty request.
3436
*/
35-
public CDAHttpException(Request request, Response response) {
37+
public CDAHttpException(Request request, Response response, boolean logSensitiveData) {
3638
super(response.message());
3739
this.request = request;
3840
this.response = response;
3941
this.responseBody = readResponseBody(response);
4042
this.stringRepresentation = createString();
43+
this.logSensitiveData = logSensitiveData;
4144
}
4245

4346
private String readResponseBody(Response response) {
@@ -133,6 +136,10 @@ public int rateLimitReset() {
133136
}
134137

135138
private String headersToString(Headers headers) {
139+
if (!logSensitiveData) {
140+
return "<headers omitted>";
141+
}
142+
136143
final StringBuilder builder = new StringBuilder();
137144

138145
String divider = "";

src/main/java/com/contentful/java/cda/interceptor/ErrorInterceptor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
* This interceptor will only be used for throwing an exception, once the server returns an error.
1313
*/
1414
public class ErrorInterceptor implements Interceptor {
15+
private final boolean logSensitiveData;
16+
17+
public ErrorInterceptor(boolean logSensitiveData) {
18+
this.logSensitiveData = logSensitiveData;
19+
}
20+
1521

1622
/**
1723
* Intercepts chain to check for unsuccessful requests.
@@ -25,7 +31,7 @@ public class ErrorInterceptor implements Interceptor {
2531
final Response response = chain.proceed(request);
2632

2733
if (!response.isSuccessful()) {
28-
throw new CDAHttpException(request, response);
34+
throw new CDAHttpException(request, response, logSensitiveData);
2935
}
3036

3137
return response;

0 commit comments

Comments
 (0)