diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLogOptions.java b/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLogOptions.java index b3e1e84b1a27..bfac6a05a7ce 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLogOptions.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLogOptions.java @@ -3,7 +3,9 @@ package com.azure.core.http.policy; +import java.util.Arrays; import java.util.HashSet; +import java.util.List; import java.util.Objects; import java.util.Set; @@ -14,11 +16,35 @@ public class HttpLogOptions { private HttpLogDetailLevel logLevel; private Set allowedHeaderNames; private Set allowedQueryParamNames; + private static final List DEFAULT_HEADERS_WHITELIST = Arrays.asList( + "x-ms-client-request-id", + "x-ms-return-client-request-id", + "traceparent", + "Accept", + "Cache-Control", + "Connection", + "Content-Length", + "Content-Type", + "Date", + "ETag", + "Expires", + "If-Match", + "If-Modified-Since", + "If-None-Match", + "If-Unmodified-Since", + "Last-Modified", + "Pragma", + "Request-Id", + "Retry-After", + "Server", + "Transfer-Encoding", + "User-Agent" + ); public HttpLogOptions() { logLevel = HttpLogDetailLevel.NONE; allowedHeaderNames = new HashSet<>(); - allowedQueryParamNames = new HashSet<>(); + allowedQueryParamNames = new HashSet<>(DEFAULT_HEADERS_WHITELIST); } /** @@ -54,18 +80,23 @@ public Set getAllowedHeaderNames() { /** * Sets the given whitelisted headers that should be logged. + *

+ * This method sets the provided header names to be the whitelisted header names which will be logged for all http + * requests and responses, overwriting any previously configured headers, including the default set. + * Additionally, user can use {@link HttpLogOptions#addAllowedHeaderName(String)} + * or {@link HttpLogOptions#getAllowedHeaderNames()} to add or remove more headers names to the existing set of + * allowed header names. * * @param allowedHeaderNames The list of whitelisted header names from the user. * @return The updated HttpLogOptions object. - * @throws NullPointerException If {@code allowedHeaderNames} is {@code null}. */ public HttpLogOptions setAllowedHeaderNames(final Set allowedHeaderNames) { - this.allowedHeaderNames = allowedHeaderNames; + this.allowedHeaderNames = allowedHeaderNames == null ? new HashSet<>() : allowedHeaderNames; return this; } /** - * Sets the given whitelisted header that should be logged. + * Sets the given whitelisted header to the default header set that should be logged. * * @param allowedHeaderName The whitelisted header name from the user. * @return The updated HttpLogOptions object. diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java b/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java index b087b8011446..06a065a5eaf6 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java @@ -122,11 +122,11 @@ private Mono logRequest(final ClientLogger logger, final HttpRequest reque private void formatAllowableHeaders(Set allowedHeaderNames, HttpHeaders requestResponseHeaders, ClientLogger logger) { - if (allowedHeaderNames != null && !allowedHeaderNames.isEmpty()) { + if (!allowedHeaderNames.isEmpty()) { StringBuilder sb = new StringBuilder(); for (HttpHeader header : requestResponseHeaders) { sb.append(header.getName()).append(":"); - if (allowedHeaderNames.contains(header.getName())) { + if (allowedHeaderNames.stream().anyMatch(header.getName()::equalsIgnoreCase)) { sb.append(header.getValue()); } else { sb.append(REDACTED_PLACEHOLDER); @@ -145,7 +145,7 @@ private void formatAllowableQueryParams(Set allowedQueryParamNames, Stri for (String queryParam : queryParams) { String[] queryPair = queryParam.split("=", 2); if (queryPair.length == 2) { - if (allowedQueryParamNames.contains(queryPair[0])) { + if (allowedQueryParamNames.stream().anyMatch(queryPair[0]::equalsIgnoreCase)) { sb.append(queryParam); } else { sb.append(queryPair[0]).append("=").append(REDACTED_PLACEHOLDER);