|
23 | 23 | import static java.util.Collections.emptyMap;
|
24 | 24 | import static java.util.Collections.singletonList;
|
25 | 25 | import static org.assertj.core.api.Assertions.entry;
|
| 26 | +import static org.junit.jupiter.params.provider.Arguments.arguments; |
26 | 27 |
|
27 | 28 | import io.opentelemetry.api.common.AttributeKey;
|
28 | 29 | import io.opentelemetry.api.common.Attributes;
|
29 | 30 | import io.opentelemetry.api.common.AttributesBuilder;
|
30 | 31 | import io.opentelemetry.context.Context;
|
31 | 32 | import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
|
| 33 | +import io.opentelemetry.instrumentation.api.internal.Experimental; |
32 | 34 | import io.opentelemetry.instrumentation.api.internal.HttpConstants;
|
33 | 35 | import java.net.ConnectException;
|
34 | 36 | import java.util.HashMap;
|
35 | 37 | import java.util.HashSet;
|
36 | 38 | import java.util.List;
|
37 | 39 | import java.util.Map;
|
38 | 40 | import java.util.function.ToIntFunction;
|
| 41 | +import java.util.stream.Stream; |
39 | 42 | import javax.annotation.Nullable;
|
40 | 43 | import org.junit.jupiter.api.Test;
|
| 44 | +import org.junit.jupiter.api.extension.ExtensionContext; |
41 | 45 | import org.junit.jupiter.params.ParameterizedTest;
|
| 46 | +import org.junit.jupiter.params.provider.Arguments; |
| 47 | +import org.junit.jupiter.params.provider.ArgumentsProvider; |
42 | 48 | import org.junit.jupiter.params.provider.ArgumentsSource;
|
43 | 49 | import org.junit.jupiter.params.provider.ValueSource;
|
44 | 50 |
|
@@ -200,6 +206,93 @@ void normal() {
|
200 | 206 | entry(NETWORK_PEER_PORT, 456L));
|
201 | 207 | }
|
202 | 208 |
|
| 209 | + @ParameterizedTest |
| 210 | + @ArgumentsSource(UrlSourceToRedact.class) |
| 211 | + void shouldRedactUserInfoAndQueryParameters(String url, String expectedResult) { |
| 212 | + Map<String, String> request = new HashMap<>(); |
| 213 | + request.put("urlFull", url); |
| 214 | + |
| 215 | + HttpClientAttributesExtractorBuilder<Map<String, String>, Map<String, String>> builder = |
| 216 | + HttpClientAttributesExtractor.builder(new TestHttpClientAttributesGetter()); |
| 217 | + Experimental.setRedactQueryParameters(builder, true); |
| 218 | + AttributesExtractor<Map<String, String>, Map<String, String>> extractor = builder.build(); |
| 219 | + |
| 220 | + AttributesBuilder attributes = Attributes.builder(); |
| 221 | + extractor.onStart(attributes, Context.root(), request); |
| 222 | + |
| 223 | + assertThat(attributes.build()).containsOnly(entry(URL_FULL, expectedResult)); |
| 224 | + } |
| 225 | + |
| 226 | + static final class UrlSourceToRedact implements ArgumentsProvider { |
| 227 | + |
| 228 | + @Override |
| 229 | + public Stream<? extends Arguments> provideArguments(ExtensionContext context) { |
| 230 | + return Stream.of( |
| 231 | + arguments( "https://user1:[email protected]", "https://REDACTED:[email protected]"), |
| 232 | + arguments( |
| 233 | + "https://user1:[email protected]/path/", |
| 234 | + "https://REDACTED:[email protected]/path/"), |
| 235 | + arguments( |
| 236 | + "https://user1:[email protected]#test.html", |
| 237 | + "https://REDACTED:[email protected]#test.html"), |
| 238 | + arguments( |
| 239 | + "https://user1:[email protected]?foo=b@r", |
| 240 | + "https://REDACTED:[email protected]?foo=b@r"), |
| 241 | + arguments( |
| 242 | + "https://user1:[email protected]/p@th?foo=b@r", |
| 243 | + "https://REDACTED:[email protected]/p@th?foo=b@r"), |
| 244 | + arguments("https://github.com/p@th?foo=b@r", "https://github.com/p@th?foo=b@r"), |
| 245 | + arguments( "https://github.com#[email protected]", "https://github.com#[email protected]"), |
| 246 | + |
| 247 | + arguments("https://github.com@", "https://github.com@"), |
| 248 | + arguments( |
| 249 | + "https://service.com?paramA=valA¶mB=valB", |
| 250 | + "https://service.com?paramA=valA¶mB=valB"), |
| 251 | + arguments( |
| 252 | + "https://service.com?AWSAccessKeyId=AKIAIOSFODNN7", |
| 253 | + "https://service.com?AWSAccessKeyId=REDACTED"), |
| 254 | + arguments( |
| 255 | + "https://service.com?Signature=39Up9jzHkxhuIhFE9594DJxe7w6cIRCg0V6ICGS0%3A377", |
| 256 | + "https://service.com?Signature=REDACTED"), |
| 257 | + arguments( |
| 258 | + "https://service.com?sig=39Up9jzHkxhuIhFE9594DJxe7w6cIRCg0V6ICGS0", |
| 259 | + "https://service.com?sig=REDACTED"), |
| 260 | + arguments( |
| 261 | + "https://service.com?X-Goog-Signature=39Up9jzHkxhuIhFE9594DJxe7w6cIRCg0V6ICGS0", |
| 262 | + "https://service.com?X-Goog-Signature=REDACTED"), |
| 263 | + arguments( |
| 264 | + "https://service.com?paramA=valA&AWSAccessKeyId=AKIAIOSFODNN7¶mB=valB", |
| 265 | + "https://service.com?paramA=valA&AWSAccessKeyId=REDACTED¶mB=valB"), |
| 266 | + arguments( |
| 267 | + "https://service.com?AWSAccessKeyId=AKIAIOSFODNN7¶mA=valA", |
| 268 | + "https://service.com?AWSAccessKeyId=REDACTED¶mA=valA"), |
| 269 | + arguments( |
| 270 | + "https://service.com?paramA=valA&AWSAccessKeyId=AKIAIOSFODNN7", |
| 271 | + "https://service.com?paramA=valA&AWSAccessKeyId=REDACTED"), |
| 272 | + arguments( |
| 273 | + "https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&AWSAccessKeyId=ZGIAIOSFODNN7", |
| 274 | + "https://service.com?AWSAccessKeyId=REDACTED&AWSAccessKeyId=REDACTED"), |
| 275 | + arguments( |
| 276 | + "https://service.com?AWSAccessKeyId=AKIAIOSFODNN7#ref", |
| 277 | + "https://service.com?AWSAccessKeyId=REDACTED#ref"), |
| 278 | + arguments( |
| 279 | + "https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&aa&bb", |
| 280 | + "https://service.com?AWSAccessKeyId=REDACTED&aa&bb"), |
| 281 | + arguments( |
| 282 | + "https://service.com?aa&bb&AWSAccessKeyId=AKIAIOSFODNN7", |
| 283 | + "https://service.com?aa&bb&AWSAccessKeyId=REDACTED"), |
| 284 | + arguments( |
| 285 | + "https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&&", |
| 286 | + "https://service.com?AWSAccessKeyId=REDACTED&&"), |
| 287 | + arguments( |
| 288 | + "https://service.com?&&AWSAccessKeyId=AKIAIOSFODNN7", |
| 289 | + "https://service.com?&&AWSAccessKeyId=REDACTED"), |
| 290 | + arguments( |
| 291 | + "https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&a&b#fragment", |
| 292 | + "https://service.com?AWSAccessKeyId=REDACTED&a&b#fragment")); |
| 293 | + } |
| 294 | + } |
| 295 | + |
203 | 296 | @ParameterizedTest
|
204 | 297 | @ArgumentsSource(ValidRequestMethodsProvider.class)
|
205 | 298 | void shouldExtractKnownMethods(String requestMethod) {
|
|
0 commit comments