Skip to content

Commit 6d69974

Browse files
committed
Sanitize log message - remove Authorization HTTP header values
1 parent 69f8ae8 commit 6d69974

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

src/main/java/org/jgroups/protocols/kubernetes/Utils.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.nio.file.Path;
1212
import java.security.AccessController;
1313
import java.security.PrivilegedAction;
14+
import java.util.HashMap;
1415
import java.util.Map;
1516
import java.util.concurrent.Callable;
1617
import java.util.logging.Level;
@@ -155,5 +156,23 @@ public static void close(AutoCloseable cl) {
155156
}
156157
}
157158

159+
/**
160+
* Sanitizes a map of HTTP headers - all entries where the key equals "Authorization" (case-insensitive) are
161+
* overridden to mask the original authorization data.
162+
*
163+
* @param headers HTTP header map
164+
* @return map where all "Authorization" entries are masked
165+
*/
166+
public static Map<String, String> sanitizeHttpHeaders(Map<String, String> headers) {
167+
HashMap<String, String> newHeaders = new HashMap<>(headers);
168+
// Iterate over all keys to find all case combinations
169+
newHeaders.keySet().forEach(key -> {
170+
if (key != null && key.equalsIgnoreCase("Authorization")) {
171+
newHeaders.put(key, "***");
172+
}
173+
});
174+
return newHeaders;
175+
}
176+
158177
private Utils() {}
159178
}

src/main/java/org/jgroups/protocols/kubernetes/stream/BaseStreamProvider.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.jgroups.protocols.kubernetes.stream;
22

3+
import org.jgroups.protocols.kubernetes.Utils;
4+
35
import java.io.IOException;
46
import java.net.URL;
57
import java.net.URLConnection;
@@ -12,7 +14,8 @@ public abstract class BaseStreamProvider implements StreamProvider {
1214

1315
public URLConnection openConnection(String url, Map<String, String> headers, int connectTimeout, int readTimeout) throws IOException {
1416
if (log.isLoggable(Level.FINE)) {
15-
log.log(Level.FINE, String.format("%s opening connection: url [%s], headers [%s], connectTimeout [%s], readTimeout [%s]", getClass().getSimpleName(), url, headers, connectTimeout, readTimeout));
17+
log.log(Level.FINE, String.format("%s opening connection: url [%s], headers [%s], connectTimeout [%s], readTimeout [%s]",
18+
getClass().getSimpleName(), url, Utils.sanitizeHttpHeaders(headers), connectTimeout, readTimeout));
1619
}
1720
URLConnection connection = new URL(url).openConnection();
1821
if (headers != null) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.jgroups.protocols.kubernetes;
2+
3+
import org.assertj.core.api.Assertions;
4+
import org.junit.Test;
5+
6+
import java.util.Map;
7+
8+
public class UtilsTest {
9+
10+
@Test
11+
public void testSanitizeHttpHeaders() {
12+
Map<String, String> sanitized = Utils.sanitizeHttpHeaders(Map.of(
13+
"Host", "jgroups.org",
14+
"Authorization", "Basic abcd",
15+
"authorization", "Bearer abcd"
16+
));
17+
Assertions.assertThat(sanitized.get("Host")).isEqualTo("jgroups.org");
18+
Assertions.assertThat(sanitized.get("Authorization")).isEqualTo("***");
19+
Assertions.assertThat(sanitized.get("authorization")).isEqualTo("***");
20+
}
21+
}

0 commit comments

Comments
 (0)