-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Mask certain HTTP headers in the HTTP access log #44475
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
Draft
mabartos
wants to merge
1
commit into
keycloak:main
Choose a base branch
from
mabartos:KC-43811
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
.../src/main/java/org/keycloak/quarkus/runtime/logging/CustomAllRequestHeadersAttribute.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| package org.keycloak.quarkus.runtime.logging; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.StringJoiner; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import org.keycloak.quarkus.runtime.configuration.Configuration; | ||
|
|
||
| import io.quarkus.vertx.http.runtime.attribute.AllRequestHeadersAttribute; | ||
| import io.quarkus.vertx.http.runtime.attribute.ExchangeAttribute; | ||
| import io.quarkus.vertx.http.runtime.attribute.ExchangeAttributeBuilder; | ||
| import io.quarkus.vertx.http.runtime.attribute.ReadOnlyAttributeException; | ||
| import io.smallrye.config.ConfigValue; | ||
| import io.vertx.core.MultiMap; | ||
| import io.vertx.core.http.HttpHeaders; | ||
| import io.vertx.ext.web.RoutingContext; | ||
|
|
||
| // modified AllRequestHeadersAttribute class -> https://github.com/quarkusio/quarkus/blob/main/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/attribute/AllRequestHeadersAttribute.java | ||
| // remove once current Quarkus version includes the https://github.com/quarkusio/quarkus/pull/50672 | ||
| public class CustomAllRequestHeadersAttribute implements ExchangeAttribute { | ||
|
|
||
| //---------------------Keycloak-changes-BEGIN----------------------------// | ||
| private static Set<String> getMaskedCookies(){ | ||
| return Optional.ofNullable(Configuration.getConfigValue("quarkus.http.access-log.masked-cookies")) | ||
| .map(ConfigValue::getValue) | ||
| .map(f -> f.split(",")) | ||
| .map(f -> new HashSet<>(Arrays.stream(f).toList())) | ||
| .orElseGet(HashSet::new); | ||
| } | ||
| //---------------------Keycloak-changes-END----------------------------// | ||
|
|
||
| private static final String AUTHORIZATION_HEADER = String.valueOf(HttpHeaders.AUTHORIZATION).toLowerCase(); | ||
| private static final String COOKIE_HEADER = String.valueOf(HttpHeaders.COOKIE).toLowerCase(); | ||
|
|
||
| private final Set<String> maskedHeaders; | ||
| private final Set<String> maskedCookies; | ||
|
|
||
| CustomAllRequestHeadersAttribute() { | ||
| this(Set.of(), Set.of()); | ||
| } | ||
|
|
||
| CustomAllRequestHeadersAttribute(Set<String> maskedHeaders, Set<String> maskedCookies) { | ||
| this.maskedHeaders = toLowerCaseStringSet(maskedHeaders); | ||
| this.maskedCookies = toLowerCaseStringSet(maskedCookies); | ||
| } | ||
|
|
||
| private static Set<String> toLowerCaseStringSet(Set<String> set) { | ||
| return set.stream().map(String::toLowerCase).collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| @Override | ||
| public String readAttribute(RoutingContext exchange) { | ||
| return readAttribute(exchange.request().headers()); | ||
| } | ||
|
|
||
| String readAttribute(MultiMap headers) { | ||
| if (headers.isEmpty()) { | ||
| return null; | ||
| } else { | ||
| final StringJoiner joiner = new StringJoiner(System.lineSeparator()); | ||
|
|
||
| for (Map.Entry<String, String> header : headers) { | ||
| joiner.add(header.getKey() + ": " + maskHeaderValue(header.getKey(), header.getValue())); | ||
| } | ||
|
|
||
| return joiner.toString(); | ||
| } | ||
| } | ||
|
|
||
| String maskHeaderValue(String headerName, String headerValue) { | ||
| if (headerValue == null) { | ||
| return null; | ||
| } | ||
|
|
||
| String headerNameLowerCase = headerName.toLowerCase(); | ||
|
|
||
| if (AUTHORIZATION_HEADER.equals(headerNameLowerCase)) { | ||
| return maskAuthorizationHeaderValue(headerValue); | ||
| } | ||
|
|
||
| if (COOKIE_HEADER.equals(headerNameLowerCase)) { | ||
| return maskCookieHeaderValue(headerValue); | ||
| } | ||
|
|
||
| if (maskedHeaders.contains(headerNameLowerCase)) { | ||
| return "..."; | ||
| } | ||
|
|
||
| return headerValue; | ||
| } | ||
|
|
||
| private String maskAuthorizationHeaderValue(String headerValue) { | ||
| int idx = headerValue.indexOf(' '); | ||
| final String scheme = idx > 0 ? headerValue.substring(0, idx) : null; | ||
|
|
||
| if (scheme != null) { | ||
| return scheme + " ..."; | ||
| } else { | ||
| return "..."; | ||
| } | ||
| } | ||
|
|
||
| private String maskCookieHeaderValue(String headerValue) { | ||
| int idx = headerValue.indexOf('='); | ||
|
|
||
| final String cookieName = idx > 0 ? headerValue.substring(0, idx) : null; | ||
|
|
||
| if (cookieName != null && maskedCookies.contains(cookieName.toLowerCase())) { | ||
| return cookieName + "=..."; | ||
| } | ||
|
|
||
| return headerValue; | ||
| } | ||
|
|
||
| @Override | ||
| public void writeAttribute(RoutingContext exchange, String newValue) throws ReadOnlyAttributeException { | ||
| throw new ReadOnlyAttributeException("Headers", newValue); | ||
| } | ||
|
|
||
| public static final class Builder implements ExchangeAttributeBuilder { | ||
|
|
||
| @Override | ||
| public String name() { | ||
| return "Headers"; | ||
| } | ||
|
|
||
| @Override | ||
| public ExchangeAttribute build(final String token) { | ||
| if (token.equals("%{ALL_REQUEST_HEADERS}")) { | ||
| //---------------------Keycloak-changes-BEGIN----------------------------// | ||
| return new CustomAllRequestHeadersAttribute(Set.of(), getMaskedCookies()); | ||
| //---------------------Keycloak-changes-END----------------------------// | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public int priority() { | ||
| //---------------------Keycloak-changes-BEGIN----------------------------// | ||
| // increase the priority | ||
| return new AllRequestHeadersAttribute.Builder().priority() + 1; | ||
| //---------------------Keycloak-changes-END----------------------------// | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
1 change: 1 addition & 0 deletions
1
...ources/META-INF/services/io.quarkus.vertx.http.runtime.attribute.ExchangeAttributeBuilder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| org.keycloak.quarkus.runtime.logging.CustomAllRequestHeadersAttribute$Builder |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it matter if there's quarkus support whether this is hidden?
Will this ever be set by a user? If so, we could consider making it additive to the default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was rather for purposes to be on a safe side, but yes, it might not be hidden.
It's expected that some additives will be there: #44433