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

Redact query string values for http client spans #13114

Merged
merged 22 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c8a5c14
Redact query string values
jeanbisutti Jan 27, 2025
641260e
Update instrumentation-api/src/main/java/io/opentelemetry/instrumenta…
jeanbisutti Jan 29, 2025
e65eb6a
Add comments
jeanbisutti Jan 29, 2025
417df19
Update instrumentation-api/src/main/java/io/opentelemetry/instrumenta…
jeanbisutti Jan 31, 2025
65f0a33
Move configuration from the stable API
jeanbisutti Feb 11, 2025
951b0f4
Only use HttpClientAttributesExtractor and rename configuration
jeanbisutti Feb 12, 2025
febf108
Fix JSON metadata
jeanbisutti Feb 11, 2025
53a7a0b
Fix Spring metadata
jeanbisutti Feb 12, 2025
be63270
Enable query redaction by default and clean up
jeanbisutti Feb 12, 2025
da85a5e
Instance variable set to true to avoid confusion
jeanbisutti Feb 12, 2025
fa98de3
Re-apply feedback
jeanbisutti Feb 12, 2025
4fc1c2d
make experimental option setting for redacted query params similar to…
laurit Feb 13, 2025
dd05fbb
Re-apply feedback
jeanbisutti Feb 13, 2025
a5760ba
Simplify
trask Feb 13, 2025
f37ab6d
Update instrumentation-api/src/main/java/io/opentelemetry/instrumenta…
jeanbisutti Feb 17, 2025
9ef64ab
test method naming
jeanbisutti Feb 17, 2025
4911e83
merge
jeanbisutti Feb 17, 2025
5164fe5
Add scenarios with parameters without values
jeanbisutti Feb 18, 2025
ef736e1
Alternative version
jeanbisutti Feb 24, 2025
bbed3b3
fix typo
jeanbisutti Feb 25, 2025
1f20468
Update instrumentation-api/src/main/java/io/opentelemetry/instrumenta…
trask Feb 27, 2025
0195b2a
spotless
jeanbisutti Feb 27, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.SpanStatusExtractor;
import io.opentelemetry.instrumentation.api.internal.Experimental;
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesExtractorBuilder;
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesGetter;
Expand Down Expand Up @@ -177,6 +178,18 @@ public DefaultHttpClientInstrumenterBuilder<REQUEST, RESPONSE> setKnownMethods(
return this;
}

/**
* Configures the instrumentation to redact sensitive URL parameters.
*
* @param redactQueryParameters {@code true} if the sensitive URL parameters have to be redacted.
*/
@CanIgnoreReturnValue
public DefaultHttpClientInstrumenterBuilder<REQUEST, RESPONSE> setRedactQueryParameters(
boolean redactQueryParameters) {
Experimental.setRedactQueryParameters(httpAttributesExtractorBuilder, redactQueryParameters);
return this;
}

/** Sets custom {@link SpanNameExtractor} via transform function. */
@CanIgnoreReturnValue
public DefaultHttpClientInstrumenterBuilder<REQUEST, RESPONSE> setSpanNameExtractor(
Expand Down Expand Up @@ -225,6 +238,7 @@ public Instrumenter<REQUEST, RESPONSE> build() {
.addAttributesExtractor(HttpExperimentalAttributesExtractor.create(attributesGetter))
.addOperationMetrics(HttpClientExperimentalMetrics.get());
}

builderCustomizer.accept(builder);

if (headerSetter != null) {
Expand All @@ -248,6 +262,7 @@ public DefaultHttpClientInstrumenterBuilder<REQUEST, RESPONSE> configure(CommonC
set(
config::shouldEmitExperimentalHttpClientTelemetry,
this::setEmitExperimentalHttpClientMetrics);
set(config::redactQueryParameters, this::setRedactQueryParameters);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public final class CommonConfig {
private final boolean statementSanitizationEnabled;
private final boolean emitExperimentalHttpClientTelemetry;
private final boolean emitExperimentalHttpServerTelemetry;
private final boolean redactQueryParameters;
private final String loggingTraceIdKey;
private final String loggingSpanIdKey;
private final String loggingTraceFlagsKey;
Expand All @@ -57,6 +58,9 @@ public CommonConfig(InstrumentationConfig config) {
config.getBoolean("otel.instrumentation.common.db-statement-sanitizer.enabled", true);
emitExperimentalHttpClientTelemetry =
config.getBoolean("otel.instrumentation.http.client.emit-experimental-telemetry", false);
redactQueryParameters =
config.getBoolean(
"otel.instrumentation.http.client.experimental.redact-query-parameters", true);
emitExperimentalHttpServerTelemetry =
config.getBoolean("otel.instrumentation.http.server.emit-experimental-telemetry", false);
enduserConfig = new EnduserConfig(config);
Expand Down Expand Up @@ -111,6 +115,10 @@ public boolean shouldEmitExperimentalHttpServerTelemetry() {
return emitExperimentalHttpServerTelemetry;
}

public boolean redactQueryParameters() {
return redactQueryParameters;
}

public String getTraceIdKey() {
return loggingTraceIdKey;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.api.internal;

import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesExtractorBuilder;
import java.util.function.BiConsumer;
import javax.annotation.Nullable;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
public final class Experimental {

@Nullable
private static volatile BiConsumer<HttpClientAttributesExtractorBuilder<?, ?>, Boolean>
redactHttpClientQueryParameters;

private Experimental() {}

public static void setRedactQueryParameters(
HttpClientAttributesExtractorBuilder<?, ?> builder, boolean redactQueryParameters) {
if (redactHttpClientQueryParameters != null) {
redactHttpClientQueryParameters.accept(builder, redactQueryParameters);
}
}

public static void internalSetRedactHttpClientQueryParameters(
BiConsumer<HttpClientAttributesExtractorBuilder<?, ?>, Boolean>
redactHttpClientQueryParameters) {
Experimental.redactHttpClientQueryParameters = redactHttpClientQueryParameters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import io.opentelemetry.instrumentation.api.semconv.network.internal.InternalServerAttributesExtractor;
import io.opentelemetry.semconv.HttpAttributes;
import io.opentelemetry.semconv.UrlAttributes;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.function.ToIntFunction;
import javax.annotation.Nullable;

Expand All @@ -32,6 +35,9 @@ public final class HttpClientAttributesExtractor<REQUEST, RESPONSE>
REQUEST, RESPONSE, HttpClientAttributesGetter<REQUEST, RESPONSE>>
implements SpanKeyProvider {

private static final Set<String> PARAMS_TO_REDACT =
new HashSet<>(Arrays.asList("AWSAccessKeyId", "Signature", "sig", "X-Goog-Signature"));

/**
* Creates the HTTP client attributes extractor with default configuration.
*
Expand All @@ -54,6 +60,7 @@ public static <REQUEST, RESPONSE> HttpClientAttributesExtractorBuilder<REQUEST,
private final InternalNetworkAttributesExtractor<REQUEST, RESPONSE> internalNetworkExtractor;
private final InternalServerAttributesExtractor<REQUEST> internalServerExtractor;
private final ToIntFunction<Context> resendCountIncrementer;
private final boolean redactQueryParameters;

HttpClientAttributesExtractor(HttpClientAttributesExtractorBuilder<REQUEST, RESPONSE> builder) {
super(
Expand All @@ -65,6 +72,7 @@ public static <REQUEST, RESPONSE> HttpClientAttributesExtractorBuilder<REQUEST,
internalNetworkExtractor = builder.buildNetworkExtractor();
internalServerExtractor = builder.buildServerExtractor();
resendCountIncrementer = builder.resendCountIncrementer;
redactQueryParameters = builder.redactQueryParameters;
}

@Override
Expand Down Expand Up @@ -104,11 +112,21 @@ public SpanKey internalGetSpanKey() {
}

@Nullable
private static String stripSensitiveData(@Nullable String url) {
private String stripSensitiveData(@Nullable String url) {
if (url == null || url.isEmpty()) {
return url;
}

url = redactUserInfo(url);

if (redactQueryParameters) {
url = redactQueryParameters(url);
}

return url;
}

private static String redactUserInfo(String url) {
int schemeEndIndex = url.indexOf(':');

if (schemeEndIndex == -1) {
Expand Down Expand Up @@ -145,4 +163,58 @@ private static String stripSensitiveData(@Nullable String url) {
}
return url.substring(0, schemeEndIndex + 3) + "REDACTED:REDACTED" + url.substring(atIndex);
}

private static String redactQueryParameters(String url) {
int questionMarkIndex = url.indexOf('?');
if (questionMarkIndex == -1 || !containsParamToRedact(url)) {
return url;
}

StringBuilder urAfterQuestionMark = new StringBuilder();

// To build a parameter name until we reach the '=' character
// If the parameter name is a one to redact, we will redact the value
StringBuilder currentParamName = new StringBuilder();

for (int i = questionMarkIndex + 1; i < url.length(); i++) {
char currentChar = url.charAt(i);

if (currentChar == '=') {
urAfterQuestionMark.append('=');
if (PARAMS_TO_REDACT.contains(currentParamName.toString())) {
urAfterQuestionMark.append("REDACTED");
// skip over parameter value
for (; i + 1 < url.length(); i++) {
char c = url.charAt(i + 1);
if (c == '&' || c == '#') {
break;
}
}
}
} else if (currentChar == '&') { // New parameter delimiter
urAfterQuestionMark.append(currentChar);
// To avoid creating a new StringBuilder for each new parameter
currentParamName.setLength(0);
} else if (currentChar == '#') { // Reference delimiter
urAfterQuestionMark.append(url.substring(i));
break;
} else {
currentParamName.append(
currentChar); // param values can be appended to currentParamName here but it's not an
// issue
urAfterQuestionMark.append(currentChar);
}
}

return url.substring(0, questionMarkIndex) + "?" + urAfterQuestionMark;
}

private static boolean containsParamToRedact(String urlpart) {
for (String param : PARAMS_TO_REDACT) {
if (urlpart.contains(param)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
import io.opentelemetry.instrumentation.api.internal.Experimental;
import io.opentelemetry.instrumentation.api.internal.HttpConstants;
import io.opentelemetry.instrumentation.api.semconv.network.internal.AddressAndPortExtractor;
import io.opentelemetry.instrumentation.api.semconv.network.internal.InternalNetworkAttributesExtractor;
Expand All @@ -37,6 +38,12 @@ public final class HttpClientAttributesExtractorBuilder<REQUEST, RESPONSE> {
List<String> capturedResponseHeaders = emptyList();
Set<String> knownMethods = HttpConstants.KNOWN_METHODS;
ToIntFunction<Context> resendCountIncrementer = HttpClientRequestResendCount::getAndIncrement;
boolean redactQueryParameters;

static {
Experimental.internalSetRedactHttpClientQueryParameters(
(builder, redact) -> builder.redactQueryParameters = redact);
}

HttpClientAttributesExtractorBuilder(
HttpClientAttributesGetter<REQUEST, RESPONSE> httpAttributesGetter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,28 @@
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.entry;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.internal.Experimental;
import io.opentelemetry.instrumentation.api.internal.HttpConstants;
import java.net.ConnectException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.function.ToIntFunction;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.ValueSource;

Expand Down Expand Up @@ -200,6 +206,93 @@ void normal() {
entry(NETWORK_PEER_PORT, 456L));
}

@ParameterizedTest
@ArgumentsSource(UrlSourceToRedact.class)
void shouldRedactUserInfoAndQueryParameters(String url, String expectedResult) {
Map<String, String> request = new HashMap<>();
request.put("urlFull", url);

HttpClientAttributesExtractorBuilder<Map<String, String>, Map<String, String>> builder =
HttpClientAttributesExtractor.builder(new TestHttpClientAttributesGetter());
Experimental.setRedactQueryParameters(builder, true);
AttributesExtractor<Map<String, String>, Map<String, String>> extractor = builder.build();

AttributesBuilder attributes = Attributes.builder();
extractor.onStart(attributes, Context.root(), request);

assertThat(attributes.build()).containsOnly(entry(URL_FULL, expectedResult));
}

static final class UrlSourceToRedact implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
arguments("https://user1:[email protected]", "https://REDACTED:[email protected]"),
arguments(
"https://user1:[email protected]/path/",
"https://REDACTED:[email protected]/path/"),
arguments(
"https://user1:[email protected]#test.html",
"https://REDACTED:[email protected]#test.html"),
arguments(
"https://user1:[email protected]?foo=b@r",
"https://REDACTED:[email protected]?foo=b@r"),
arguments(
"https://user1:[email protected]/p@th?foo=b@r",
"https://REDACTED:[email protected]/p@th?foo=b@r"),
arguments("https://github.com/p@th?foo=b@r", "https://github.com/p@th?foo=b@r"),
arguments("https://github.com#[email protected]", "https://github.com#[email protected]"),
arguments("user1:[email protected]", "user1:[email protected]"),
arguments("https://github.com@", "https://github.com@"),
arguments(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was (probably inadvertly) removed in #9925.

The test is added again with new test cases.

"https://service.com?paramA=valA&paramB=valB",
"https://service.com?paramA=valA&paramB=valB"),
arguments(
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7",
"https://service.com?AWSAccessKeyId=REDACTED"),
arguments(
"https://service.com?Signature=39Up9jzHkxhuIhFE9594DJxe7w6cIRCg0V6ICGS0%3A377",
"https://service.com?Signature=REDACTED"),
arguments(
"https://service.com?sig=39Up9jzHkxhuIhFE9594DJxe7w6cIRCg0V6ICGS0",
"https://service.com?sig=REDACTED"),
arguments(
"https://service.com?X-Goog-Signature=39Up9jzHkxhuIhFE9594DJxe7w6cIRCg0V6ICGS0",
"https://service.com?X-Goog-Signature=REDACTED"),
arguments(
"https://service.com?paramA=valA&AWSAccessKeyId=AKIAIOSFODNN7&paramB=valB",
"https://service.com?paramA=valA&AWSAccessKeyId=REDACTED&paramB=valB"),
arguments(
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&paramA=valA",
"https://service.com?AWSAccessKeyId=REDACTED&paramA=valA"),
arguments(
"https://service.com?paramA=valA&AWSAccessKeyId=AKIAIOSFODNN7",
"https://service.com?paramA=valA&AWSAccessKeyId=REDACTED"),
arguments(
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&AWSAccessKeyId=ZGIAIOSFODNN7",
"https://service.com?AWSAccessKeyId=REDACTED&AWSAccessKeyId=REDACTED"),
arguments(
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7#ref",
"https://service.com?AWSAccessKeyId=REDACTED#ref"),
arguments(
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&aa&bb",
"https://service.com?AWSAccessKeyId=REDACTED&aa&bb"),
arguments(
"https://service.com?aa&bb&AWSAccessKeyId=AKIAIOSFODNN7",
"https://service.com?aa&bb&AWSAccessKeyId=REDACTED"),
arguments(
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&&",
"https://service.com?AWSAccessKeyId=REDACTED&&"),
arguments(
"https://service.com?&&AWSAccessKeyId=AKIAIOSFODNN7",
"https://service.com?&&AWSAccessKeyId=REDACTED"),
arguments(
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&a&b#fragment",
"https://service.com?AWSAccessKeyId=REDACTED&a&b#fragment"));
}
}

@ParameterizedTest
@ArgumentsSource(ValidRequestMethodsProvider.class)
void shouldExtractKnownMethods(String requestMethod) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,12 @@
"description": "Enable the capture of experimental HTTP client telemetry. Add the <code>http.request.body.size</code> and <code>http.response.body.size> attributes to spans, and record the <code>http.client.request.size</code> and <code>http.client.response.size</code> metrics.",
"defaultValue": false
},
{
"name": "otel.instrumentation.http.client.experimental.redact-query-parameters",
"type": "java.lang.Boolean",
"description": "Redact sensitive URL parameters. See https://opentelemetry.io/docs/specs/semconv/http/http-spans.",
"defaultValue": true
},
{
"name": "otel.instrumentation.http.known-methods",
"type": "java.util.List<java.lang.String>",
Expand Down
Loading
Loading