Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanitize log message - remove Authorization HTTP header values #207

Merged
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
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
21 changes: 21 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,21 @@
package org.jgroups.protocols.kubernetes;

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

import java.util.Map;

public class UtilsTest {

@Test
public void testSanitizeHttpHeaders() {
Map<String, String> sanitized = Utils.sanitizeHttpHeaders(Map.of(
"Host", "jgroups.org",
"Authorization", "Basic abcd",
"authorization", "Bearer abcd"
));
Assertions.assertThat(sanitized.get("Host")).isEqualTo("jgroups.org");
Assertions.assertThat(sanitized.get("Authorization")).isEqualTo("***");
Assertions.assertThat(sanitized.get("authorization")).isEqualTo("***");
}
}
Loading