Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Sanitize log message - remove Authorization HTTP header values
  • Loading branch information
TomasHofman committed Feb 5, 2024
commit d9109ac0ed60c295ddfa41dd639f42a00591c7a2
19 changes: 19 additions & 0 deletions src/main/java/org/jgroups/protocols/kubernetes/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.nio.file.Path;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.logging.Level;
Expand Down Expand Up @@ -155,5 +156,23 @@ public static void close(AutoCloseable cl) {
}
}

/**
* Sanitizes a map of HTTP headers - all entries where the key equals "Authorization" (case-insensitive) are
* overridden to mask the original authorization data.
*
* @param headers HTTP header map
* @return map where all "Authorization" entries are masked
*/
public static Map<String, String> sanitizeHttpHeaders(Map<String, String> headers) {
HashMap<String, String> newHeaders = new HashMap<>(headers);
// Iterate over all keys to find all case combinations
newHeaders.keySet().forEach(key -> {
if (key != null && key.equalsIgnoreCase("Authorization")) {
newHeaders.put(key, "***");
}
});
return newHeaders;
}

private Utils() {}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jgroups.protocols.kubernetes.stream;

import org.jgroups.protocols.kubernetes.Utils;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
Expand All @@ -12,7 +14,8 @@ public abstract class BaseStreamProvider implements StreamProvider {

public URLConnection openConnection(String url, Map<String, String> headers, int connectTimeout, int readTimeout) throws IOException {
if (log.isLoggable(Level.FINE)) {
log.log(Level.FINE, String.format("%s opening connection: url [%s], headers [%s], connectTimeout [%s], readTimeout [%s]", getClass().getSimpleName(), url, headers, connectTimeout, readTimeout));
log.log(Level.FINE, String.format("%s opening connection: url [%s], headers [%s], connectTimeout [%s], readTimeout [%s]",
getClass().getSimpleName(), url, Utils.sanitizeHttpHeaders(headers), connectTimeout, readTimeout));
}
URLConnection connection = new URL(url).openConnection();
if (headers != null) {
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/org/jgroups/protocols/kubernetes/UtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.jgroups.protocols.kubernetes;

import org.assertj.core.api.Assertions;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class UtilsTest {

@Test
public void testSanitizeHttpHeaders() {
HashMap<String, String> params = new HashMap<>();
params.put("Host", "jgroups.org");
params.put("Authorization", "Basic abcd");
params.put("authorization", "Bearer abcd");

Map<String, String> sanitized = Utils.sanitizeHttpHeaders(params);

Assertions.assertThat(sanitized.get("Host")).isEqualTo("jgroups.org");
Assertions.assertThat(sanitized.get("Authorization")).isEqualTo("***");
Assertions.assertThat(sanitized.get("authorization")).isEqualTo("***");
}
}