From c4ace54ebedd9e57b7e5176761d406dd625c7741 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 11 Feb 2022 11:21:58 -0800 Subject: [PATCH 1/5] Renames for the better --- ...Parser.java => ForwardedHeaderParser.java} | 12 +- .../http/HttpServerAttributesExtractor.java | 8 +- .../http/ForwardedHeaderParserTest.java | 245 ++++++++++++++++++ .../http/ForwarderHeaderParserTest.java | 232 ----------------- 4 files changed, 254 insertions(+), 243 deletions(-) rename instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/{ForwarderHeaderParser.java => ForwardedHeaderParser.java} (87%) create mode 100644 instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParserTest.java delete mode 100644 instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwarderHeaderParserTest.java diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwarderHeaderParser.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParser.java similarity index 87% rename from instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwarderHeaderParser.java rename to instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParser.java index 798fd5ee2516..a696a9bbaa86 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwarderHeaderParser.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParser.java @@ -7,11 +7,10 @@ import javax.annotation.Nullable; -final class ForwarderHeaderParser { +final class ForwardedHeaderParser { - // VisibleForTesting @Nullable - static String extractForwarded(String forwarded) { + static String extractClientIpFromForwardedHeader(String forwarded) { int start = forwarded.toLowerCase().indexOf("for="); if (start < 0) { return null; @@ -23,10 +22,9 @@ static String extractForwarded(String forwarded) { return extractIpAddress(forwarded, start); } - // VisibleForTesting @Nullable - static String extractForwardedFor(String forwarded) { - return extractIpAddress(forwarded, 0); + static String extractClientIpFromXForwardedForHeader(String forwardedFor) { + return extractIpAddress(forwardedFor, 0); } // from https://www.rfc-editor.org/rfc/rfc7239 @@ -67,5 +65,5 @@ private static String extractIpAddress(String forwarded, int start) { return forwarded.substring(start); } - private ForwarderHeaderParser() {} + private ForwardedHeaderParser() {} } diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractor.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractor.java index 5f06dcdfcfa3..a55e9e1a9ab1 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractor.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractor.java @@ -5,8 +5,8 @@ package io.opentelemetry.instrumentation.api.instrumenter.http; -import static io.opentelemetry.instrumentation.api.instrumenter.http.ForwarderHeaderParser.extractForwarded; -import static io.opentelemetry.instrumentation.api.instrumenter.http.ForwarderHeaderParser.extractForwardedFor; +import static io.opentelemetry.instrumentation.api.instrumenter.http.ForwardedHeaderParser.extractClientIpFromForwardedHeader; +import static io.opentelemetry.instrumentation.api.instrumenter.http.ForwardedHeaderParser.extractClientIpFromXForwardedForHeader; import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; @@ -94,7 +94,7 @@ private String clientIp(REQUEST request) { // try Forwarded String forwarded = firstHeaderValue(getter.requestHeader(request, "forwarded")); if (forwarded != null) { - forwarded = extractForwarded(forwarded); + forwarded = extractClientIpFromForwardedHeader(forwarded); if (forwarded != null) { return forwarded; } @@ -103,7 +103,7 @@ private String clientIp(REQUEST request) { // try X-Forwarded-For forwarded = firstHeaderValue(getter.requestHeader(request, "x-forwarded-for")); if (forwarded != null) { - return extractForwardedFor(forwarded); + return extractClientIpFromXForwardedForHeader(forwarded); } return null; diff --git a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParserTest.java b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParserTest.java new file mode 100644 index 000000000000..af6bb29f39bd --- /dev/null +++ b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParserTest.java @@ -0,0 +1,245 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.api.instrumenter.http; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; + +class ForwardedHeaderParserTest { + + @Test + void extractClientIpFromForwardedHeader() { + assertThat(ForwardedHeaderParser.extractClientIpFromForwardedHeader("for=1.1.1.1")) + .isEqualTo("1.1.1.1"); + } + + @Test + void extractClientIpFromForwardedHeaderWithIpv6() { + assertThat( + ForwardedHeaderParser.extractClientIpFromForwardedHeader( + "for=\"[1111:1111:1111:1111:1111:1111:1111:1111]\"")) + .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); + } + + @Test + void extractClientIpFromForwardedHeaderWithPort() { + assertThat(ForwardedHeaderParser.extractClientIpFromForwardedHeader("for=\"1.1.1.1:2222\"")) + .isEqualTo("1.1.1.1"); + } + + @Test + void extractClientIpFromForwardedHeaderWithIpv6AndPort() { + assertThat( + ForwardedHeaderParser.extractClientIpFromForwardedHeader( + "for=\"[1111:1111:1111:1111:1111:1111:1111:1111]:2222\"")) + .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); + } + + @Test + void extractClientIpFromForwardedHeaderWithCaps() { + assertThat(ForwardedHeaderParser.extractClientIpFromForwardedHeader("For=1.1.1.1")) + .isEqualTo("1.1.1.1"); + } + + @Test + void extractClientIpFromMalformedForwardedHeader() { + assertThat(ForwardedHeaderParser.extractClientIpFromForwardedHeader("for=;for=1.1.1.1")) + .isNull(); + } + + @Test + void extractClientIpFromEmptyForwardedHeader() { + assertThat(ForwardedHeaderParser.extractClientIpFromForwardedHeader("")).isNull(); + } + + @Test + void extractClientIpFromForwardedHeaderWithEmptyValue() { + assertThat(ForwardedHeaderParser.extractClientIpFromForwardedHeader("for=")).isNull(); + } + + @Test + void extractClientIpFromForwardedHeaderWithValueAndSemicolon() { + assertThat(ForwardedHeaderParser.extractClientIpFromForwardedHeader("for=;")).isNull(); + } + + @Test + void extractClientIpFromForwardedHeaderWithNoFor() { + assertThat(ForwardedHeaderParser.extractClientIpFromForwardedHeader("by=1.1.1.1;test=1.1.1.1")) + .isNull(); + } + + @Test + void extractClientIpFromForwardedHeaderWithMultiple() { + assertThat(ForwardedHeaderParser.extractClientIpFromForwardedHeader("for=1.1.1.1;for=1.2.3.4")) + .isEqualTo("1.1.1.1"); + } + + @Test + void extractClientIpFromForwardedHeaderWithMultipleIpV6() { + assertThat( + ForwardedHeaderParser.extractClientIpFromForwardedHeader( + "for=\"[1111:1111:1111:1111:1111:1111:1111:1111]\";for=1.2.3.4")) + .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); + } + + @Test + void extractClientIpFromForwardedHeaderWithMultipleAndPort() { + assertThat( + ForwardedHeaderParser.extractClientIpFromForwardedHeader( + "for=\"1.1.1.1:2222\";for=1.2.3.4")) + .isEqualTo("1.1.1.1"); + } + + @Test + void extractClientIpFromForwardedHeaderWithMultipleIpV6AndPort() { + assertThat( + ForwardedHeaderParser.extractClientIpFromForwardedHeader( + "for=\"[1111:1111:1111:1111:1111:1111:1111:1111]:2222\";for=1.2.3.4")) + .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); + } + + @Test + void extractClientIpFromForwardedHeaderWithMixedSplitter() { + assertThat( + ForwardedHeaderParser.extractClientIpFromForwardedHeader( + "test=abcd; by=1.2.3.4, for=1.1.1.1;for=1.2.3.4")) + .isEqualTo("1.1.1.1"); + } + + @Test + void extractClientIpFromForwardedHeaderWithMixedSplitterIpv6() { + assertThat( + ForwardedHeaderParser.extractClientIpFromForwardedHeader( + "test=abcd; by=1.2.3.4, for=\"[1111:1111:1111:1111:1111:1111:1111:1111]\";for=1.2.3.4")) + .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); + } + + @Test + void extractClientIpFromForwardedHeaderWithMixedSplitterAndPort() { + assertThat( + ForwardedHeaderParser.extractClientIpFromForwardedHeader( + "test=abcd; by=1.2.3.4, for=\"1.1.1.1:2222\";for=1.2.3.4")) + .isEqualTo("1.1.1.1"); + } + + @Test + void extractClientIpFromForwardedHeaderWithMixedSplitterIpv6AndPort() { + assertThat( + ForwardedHeaderParser.extractClientIpFromForwardedHeader( + "test=abcd; by=1.2.3.4, for=\"[1111:1111:1111:1111:1111:1111:1111:1111]:2222\";for=1.2.3.4")) + .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); + } + + @Test + void extractClientIpFromXForwardedForHeader() { + assertThat(ForwardedHeaderParser.extractClientIpFromXForwardedForHeader("1.1.1.1")) + .isEqualTo("1.1.1.1"); + } + + @Test + void extractClientIpFromXForwardedForHeaderWithIpv6() { + assertThat( + ForwardedHeaderParser.extractClientIpFromXForwardedForHeader( + "\"[1111:1111:1111:1111:1111:1111:1111:1111]\"")) + .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); + } + + @Test + void extractClientIpFromXForwardedForHeaderWithIpv6Unquoted() { + assertThat( + ForwardedHeaderParser.extractClientIpFromXForwardedForHeader( + "[1111:1111:1111:1111:1111:1111:1111:1111]")) + .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); + } + + @Test + void extractClientIpFromXForwardedForHeaderWithIpv6Unbracketed() { + assertThat( + ForwardedHeaderParser.extractClientIpFromXForwardedForHeader( + "1111:1111:1111:1111:1111:1111:1111:1111")) + .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); + } + + @Test + void extractClientIpFromXForwardedForHeaderWithPort() { + assertThat(ForwardedHeaderParser.extractClientIpFromXForwardedForHeader("1.1.1.1:2222")) + .isEqualTo("1.1.1.1"); + } + + @Test + void extractClientIpFromXForwardedForHeaderWithIpv6AndPort() { + assertThat( + ForwardedHeaderParser.extractClientIpFromXForwardedForHeader( + "\"[1111:1111:1111:1111:1111:1111:1111:1111]:2222\"")) + .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); + } + + @Test + void extractClientIpFromXForwardedForHeaderWithIpv6UnquotedAndPort() { + assertThat( + ForwardedHeaderParser.extractClientIpFromXForwardedForHeader( + "[1111:1111:1111:1111:1111:1111:1111:1111]:2222")) + .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); + } + + @Test + void extractClientIpFromEmptyXForwardedForHeader() { + assertThat(ForwardedHeaderParser.extractClientIpFromXForwardedForHeader("")).isNull(); + } + + @Test + void extractClientIpFromXForwardedForHeaderWithMultiple() { + assertThat(ForwardedHeaderParser.extractClientIpFromXForwardedForHeader("1.1.1.1,1.2.3.4")) + .isEqualTo("1.1.1.1"); + } + + @Test + void extractClientIpFromXForwardedForHeaderWithMultipleIpv6() { + assertThat( + ForwardedHeaderParser.extractClientIpFromXForwardedForHeader( + "\"[1111:1111:1111:1111:1111:1111:1111:1111]\",1.2.3.4")) + .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); + } + + @Test + void extractClientIpFromXForwardedForHeaderWithMultipleIpv6Unquoted() { + assertThat( + ForwardedHeaderParser.extractClientIpFromXForwardedForHeader( + "[1111:1111:1111:1111:1111:1111:1111:1111],1.2.3.4")) + .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); + } + + @Test + void extractClientIpFromXForwardedForHeaderWithMultipleIpv6Unbracketed() { + assertThat( + ForwardedHeaderParser.extractClientIpFromXForwardedForHeader( + "1111:1111:1111:1111:1111:1111:1111:1111,1.2.3.4")) + .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); + } + + @Test + void extractClientIpFromXForwardedForHeaderWithMultipleAndPort() { + assertThat(ForwardedHeaderParser.extractClientIpFromXForwardedForHeader("1.1.1.1:2222,1.2.3.4")) + .isEqualTo("1.1.1.1"); + } + + @Test + void extractClientIpFromXForwardedForHeaderWithMultipleIpv6AndPort() { + assertThat( + ForwardedHeaderParser.extractClientIpFromXForwardedForHeader( + "\"[1111:1111:1111:1111:1111:1111:1111:1111]:2222\",1.2.3.4")) + .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); + } + + @Test + void extractClientIpFromXForwardedForHeaderWithMultipleIpv6UnquotedAndPort() { + assertThat( + ForwardedHeaderParser.extractClientIpFromXForwardedForHeader( + "[1111:1111:1111:1111:1111:1111:1111:1111]:2222,1.2.3.4")) + .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); + } +} diff --git a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwarderHeaderParserTest.java b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwarderHeaderParserTest.java deleted file mode 100644 index 3d37637bac69..000000000000 --- a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwarderHeaderParserTest.java +++ /dev/null @@ -1,232 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.api.instrumenter.http; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.jupiter.api.Test; - -class ForwarderHeaderParserTest { - - @Test - void extractForwarded() { - assertThat(ForwarderHeaderParser.extractForwarded("for=1.1.1.1")).isEqualTo("1.1.1.1"); - } - - @Test - void extractForwardedIpv6() { - assertThat( - ForwarderHeaderParser.extractForwarded( - "for=\"[1111:1111:1111:1111:1111:1111:1111:1111]\"")) - .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); - } - - @Test - void extractForwardedWithPort() { - assertThat(ForwarderHeaderParser.extractForwarded("for=\"1.1.1.1:2222\"")).isEqualTo("1.1.1.1"); - } - - @Test - void extractForwardedIpv6WithPort() { - assertThat( - ForwarderHeaderParser.extractForwarded( - "for=\"[1111:1111:1111:1111:1111:1111:1111:1111]:2222\"")) - .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); - } - - @Test - void extractForwardedCaps() { - assertThat(ForwarderHeaderParser.extractForwarded("For=1.1.1.1")).isEqualTo("1.1.1.1"); - } - - @Test - void extractForwardedMalformed() { - assertThat(ForwarderHeaderParser.extractForwarded("for=;for=1.1.1.1")).isNull(); - } - - @Test - void extractForwardedEmpty() { - assertThat(ForwarderHeaderParser.extractForwarded("")).isNull(); - } - - @Test - void extractForwardedEmptyValue() { - assertThat(ForwarderHeaderParser.extractForwarded("for=")).isNull(); - } - - @Test - void extractForwardedEmptyValueWithSemicolon() { - assertThat(ForwarderHeaderParser.extractForwarded("for=;")).isNull(); - } - - @Test - void extractForwardedNoFor() { - assertThat(ForwarderHeaderParser.extractForwarded("by=1.1.1.1;test=1.1.1.1")).isNull(); - } - - @Test - void extractForwardedMultiple() { - assertThat(ForwarderHeaderParser.extractForwarded("for=1.1.1.1;for=1.2.3.4")) - .isEqualTo("1.1.1.1"); - } - - @Test - void extractForwardedMultipleIpV6() { - assertThat( - ForwarderHeaderParser.extractForwarded( - "for=\"[1111:1111:1111:1111:1111:1111:1111:1111]\";for=1.2.3.4")) - .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); - } - - @Test - void extractForwardedMultipleWithPort() { - assertThat(ForwarderHeaderParser.extractForwarded("for=\"1.1.1.1:2222\";for=1.2.3.4")) - .isEqualTo("1.1.1.1"); - } - - @Test - void extractForwardedMultipleIpV6WithPort() { - assertThat( - ForwarderHeaderParser.extractForwarded( - "for=\"[1111:1111:1111:1111:1111:1111:1111:1111]:2222\";for=1.2.3.4")) - .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); - } - - @Test - void extractForwardedMixedSplitter() { - assertThat( - ForwarderHeaderParser.extractForwarded( - "test=abcd; by=1.2.3.4, for=1.1.1.1;for=1.2.3.4")) - .isEqualTo("1.1.1.1"); - } - - @Test - void extractForwardedMixedSplitterIpv6() { - assertThat( - ForwarderHeaderParser.extractForwarded( - "test=abcd; by=1.2.3.4, for=\"[1111:1111:1111:1111:1111:1111:1111:1111]\";for=1.2.3.4")) - .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); - } - - @Test - void extractForwardedMixedSplitterWithPort() { - assertThat( - ForwarderHeaderParser.extractForwarded( - "test=abcd; by=1.2.3.4, for=\"1.1.1.1:2222\";for=1.2.3.4")) - .isEqualTo("1.1.1.1"); - } - - @Test - void extractForwardedMixedSplitterIpv6WithPort() { - assertThat( - ForwarderHeaderParser.extractForwarded( - "test=abcd; by=1.2.3.4, for=\"[1111:1111:1111:1111:1111:1111:1111:1111]:2222\";for=1.2.3.4")) - .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); - } - - @Test - void extractForwardedFor() { - assertThat(ForwarderHeaderParser.extractForwardedFor("1.1.1.1")).isEqualTo("1.1.1.1"); - } - - @Test - void extractForwardedForIpv6() { - assertThat( - ForwarderHeaderParser.extractForwardedFor( - "\"[1111:1111:1111:1111:1111:1111:1111:1111]\"")) - .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); - } - - @Test - void extractForwardedForIpv6Unquoted() { - assertThat( - ForwarderHeaderParser.extractForwardedFor("[1111:1111:1111:1111:1111:1111:1111:1111]")) - .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); - } - - @Test - void extractForwardedForIpv6Unbracketed() { - assertThat(ForwarderHeaderParser.extractForwardedFor("1111:1111:1111:1111:1111:1111:1111:1111")) - .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); - } - - @Test - void extractForwardedForWithPort() { - assertThat(ForwarderHeaderParser.extractForwardedFor("1.1.1.1:2222")).isEqualTo("1.1.1.1"); - } - - @Test - void extractForwardedForIpv6WithPort() { - assertThat( - ForwarderHeaderParser.extractForwardedFor( - "\"[1111:1111:1111:1111:1111:1111:1111:1111]:2222\"")) - .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); - } - - @Test - void extractForwardedForIpv6UnquotedWithPort() { - assertThat( - ForwarderHeaderParser.extractForwardedFor( - "[1111:1111:1111:1111:1111:1111:1111:1111]:2222")) - .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); - } - - @Test - void extractForwardedForEmpty() { - assertThat(ForwarderHeaderParser.extractForwardedFor("")).isNull(); - } - - @Test - void extractForwardedForMultiple() { - assertThat(ForwarderHeaderParser.extractForwardedFor("1.1.1.1,1.2.3.4")).isEqualTo("1.1.1.1"); - } - - @Test - void extractForwardedForMultipleIpv6() { - assertThat( - ForwarderHeaderParser.extractForwardedFor( - "\"[1111:1111:1111:1111:1111:1111:1111:1111]\",1.2.3.4")) - .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); - } - - @Test - void extractForwardedForMultipleIpv6Unquoted() { - assertThat( - ForwarderHeaderParser.extractForwardedFor( - "[1111:1111:1111:1111:1111:1111:1111:1111],1.2.3.4")) - .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); - } - - @Test - void extractForwardedForMultipleIpv6Unbracketed() { - assertThat( - ForwarderHeaderParser.extractForwardedFor( - "1111:1111:1111:1111:1111:1111:1111:1111,1.2.3.4")) - .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); - } - - @Test - void extractForwardedForMultipleWithPort() { - assertThat(ForwarderHeaderParser.extractForwardedFor("1.1.1.1:2222,1.2.3.4")) - .isEqualTo("1.1.1.1"); - } - - @Test - void extractForwardedForMultipleIpv6WithPort() { - assertThat( - ForwarderHeaderParser.extractForwardedFor( - "\"[1111:1111:1111:1111:1111:1111:1111:1111]:2222\",1.2.3.4")) - .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); - } - - @Test - void extractForwardedForMultipleIpv6UnquotedWithPort() { - assertThat( - ForwarderHeaderParser.extractForwardedFor( - "[1111:1111:1111:1111:1111:1111:1111:1111]:2222,1.2.3.4")) - .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); - } -} From 879508e669e96322a98ca9ab0b1076d16731b265 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 11 Feb 2022 12:07:57 -0800 Subject: [PATCH 2/5] Support forwarded proto --- .../http/ForwardedHeaderParser.java | 38 +++++++++++++++ .../http/HttpServerAttributesExtractor.java | 28 ++++++++++- .../http/ForwardedHeaderParserTest.java | 47 +++++++++++++++++++ .../HttpServerAttributesExtractorTest.java | 21 ++++++++- 4 files changed, 131 insertions(+), 3 deletions(-) diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParser.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParser.java index a696a9bbaa86..7c35bd263872 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParser.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParser.java @@ -9,6 +9,24 @@ final class ForwardedHeaderParser { + @Nullable + static String extractProtoFromForwardedHeader(String forwarded) { + int start = forwarded.toLowerCase().indexOf("proto="); + if (start < 0) { + return null; + } + start += 4; // start is now the index after proto= + if (start >= forwarded.length() - 1) { // the value after for= must not be empty + return null; + } + return extractProto(forwarded, start); + } + + @Nullable + static String extractProtoFromXForwardedProtoHeader(String forwardedProto) { + return extractProto(forwardedProto, 0); + } + @Nullable static String extractClientIpFromForwardedHeader(String forwarded) { int start = forwarded.toLowerCase().indexOf("for="); @@ -27,6 +45,26 @@ static String extractClientIpFromXForwardedForHeader(String forwardedFor) { return extractIpAddress(forwardedFor, 0); } + @Nullable + private static String extractProto(String forwarded, int start) { + if (forwarded.length() == start) { + return null; + } + if (forwarded.charAt(start) == '"') { + return extractProto(forwarded, start + 1); + } + for (int i = start; i < forwarded.length() - 1; i++) { + char c = forwarded.charAt(i); + if (c == ',' || c == ';' || c == '"') { + if (i == start) { // empty string + return null; + } + return forwarded.substring(start, i); + } + } + return forwarded.substring(start); + } + // from https://www.rfc-editor.org/rfc/rfc7239 // "Note that IPv6 addresses may not be quoted in // X-Forwarded-For and may not be enclosed by square brackets, but they diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractor.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractor.java index a55e9e1a9ab1..48181abee405 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractor.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractor.java @@ -7,6 +7,8 @@ import static io.opentelemetry.instrumentation.api.instrumenter.http.ForwardedHeaderParser.extractClientIpFromForwardedHeader; import static io.opentelemetry.instrumentation.api.instrumenter.http.ForwardedHeaderParser.extractClientIpFromXForwardedForHeader; +import static io.opentelemetry.instrumentation.api.instrumenter.http.ForwardedHeaderParser.extractProtoFromForwardedHeader; +import static io.opentelemetry.instrumentation.api.instrumenter.http.ForwardedHeaderParser.extractProtoFromXForwardedProtoHeader; import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; @@ -64,7 +66,11 @@ public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST super.onStart(attributes, parentContext, request); set(attributes, SemanticAttributes.HTTP_FLAVOR, getter.flavor(request)); - set(attributes, SemanticAttributes.HTTP_SCHEME, getter.scheme(request)); + String forwardedProto = forwardedProto(request); + set( + attributes, + SemanticAttributes.HTTP_SCHEME, + forwardedProto != null ? forwardedProto : getter.scheme(request)); set(attributes, SemanticAttributes.HTTP_HOST, host(request)); set(attributes, SemanticAttributes.HTTP_TARGET, getter.target(request)); set(attributes, SemanticAttributes.HTTP_ROUTE, getter.route(request)); @@ -89,6 +95,26 @@ private String host(REQUEST request) { return firstHeaderValue(getter.requestHeader(request, "host")); } + @Nullable + private String forwardedProto(REQUEST request) { + // try Forwarded + String forwarded = firstHeaderValue(getter.requestHeader(request, "forwarded")); + if (forwarded != null) { + forwarded = extractProtoFromForwardedHeader(forwarded); + if (forwarded != null) { + return forwarded; + } + } + + // try X-Forwarded-Proto + forwarded = firstHeaderValue(getter.requestHeader(request, "x-forwarded-proto")); + if (forwarded != null) { + return extractProtoFromXForwardedProtoHeader(forwarded); + } + + return null; + } + @Nullable private String clientIp(REQUEST request) { // try Forwarded diff --git a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParserTest.java b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParserTest.java index af6bb29f39bd..c475f31d6365 100644 --- a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParserTest.java +++ b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParserTest.java @@ -11,6 +11,53 @@ class ForwardedHeaderParserTest { + @Test + void extractProtoFromForwardedHeader() { + assertThat(ForwardedHeaderParser.extractProtoFromForwardedHeader("for=1.1.1.1;proto=xyz")) + .isEqualTo("xyz"); + } + + @Test + void extractProtoFromForwardedHeaderWithTrailingSemicolon() { + assertThat(ForwardedHeaderParser.extractProtoFromForwardedHeader("for=1.1.1.1;proto=xyz;")) + .isEqualTo("xyz"); + } + + @Test + void extractProtoFromForwardedHeaderWithTrailingComma() { + assertThat(ForwardedHeaderParser.extractProtoFromForwardedHeader("for=1.1.1.1;proto=xyz,")) + .isEqualTo("xyz"); + } + + @Test + void extractProtoFromForwardedHeaderWithQuotes() { + assertThat(ForwardedHeaderParser.extractProtoFromForwardedHeader("for=1.1.1.1;proto=\"xyz\"")) + .isEqualTo("xyz"); + } + + @Test + void extractProtoFromForwardedHeaderWithQuotesAndTrailingSemicolon() { + assertThat(ForwardedHeaderParser.extractProtoFromForwardedHeader("for=1.1.1.1;proto=\"xyz\";")) + .isEqualTo("xyz"); + } + + @Test + void extractProtoFromForwardedHeaderWithQuotesAndTrailingComma() { + assertThat(ForwardedHeaderParser.extractProtoFromForwardedHeader("for=1.1.1.1;proto=\"xyz\",")) + .isEqualTo("xyz"); + } + + @Test + void extractProtoFromXForwardedProtoHeader() { + assertThat(ForwardedHeaderParser.extractProtoFromXForwardedProtoHeader("xyz")).isEqualTo("xyz"); + } + + @Test + void extractProtoFromXForwardedProtoHeaderWithQuotes() { + assertThat(ForwardedHeaderParser.extractProtoFromXForwardedProtoHeader("\"xyz\"")) + .isEqualTo("xyz"); + } + @Test void extractClientIpFromForwardedHeader() { assertThat(ForwardedHeaderParser.extractClientIpFromForwardedHeader("for=1.1.1.1")) diff --git a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorTest.java b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorTest.java index e4e6b63f3357..f93d025e13ec 100644 --- a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorTest.java +++ b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorTest.java @@ -109,7 +109,7 @@ void normal() { request.put("method", "POST"); request.put("url", "http://github.com"); request.put("target", "/repositories/1"); - request.put("scheme", "https"); + request.put("scheme", "http"); request.put("requestContentLength", "10"); request.put("requestContentLengthUncompressed", "11"); request.put("flavor", "http/2"); @@ -117,7 +117,7 @@ void normal() { request.put("serverName", "server"); request.put("header.user-agent", "okhttp 3.x"); request.put("header.host", "github.com"); - request.put("header.forwarded", "for=1.1.1.1"); + request.put("header.forwarded", "for=1.1.1.1;proto=https"); request.put("header.custom-request-header", "123,456"); Map response = new HashMap<>(); @@ -194,4 +194,21 @@ void extractClientIpFromX_Forwarded_For() { assertThat(attributes.build()) .containsOnly(entry(SemanticAttributes.HTTP_CLIENT_IP, "1.1.1.1")); } + + @Test + void extractClientIpFromX_Forwarded_Proto() { + Map request = new HashMap<>(); + request.put("header.x-forwarded-proto", "https"); + + HttpServerAttributesExtractor, Map> extractor = + HttpServerAttributesExtractor.create( + new TestHttpServerAttributesExtractor(), CapturedHttpHeaders.empty()); + + AttributesBuilder attributes = Attributes.builder(); + extractor.onStart(attributes, Context.root(), request); + assertThat(attributes.build()).containsOnly(entry(SemanticAttributes.HTTP_SCHEME, "https")); + + extractor.onEnd(attributes, Context.root(), request, null, null); + assertThat(attributes.build()).containsOnly(entry(SemanticAttributes.HTTP_SCHEME, "https")); + } } From 5e5e8df968a2d914a350830e3cd572d3d44fb528 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 11 Feb 2022 13:33:00 -0800 Subject: [PATCH 3/5] fix --- .../api/instrumenter/http/ForwardedHeaderParser.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParser.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParser.java index 7c35bd263872..441dbf78966c 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParser.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParser.java @@ -15,7 +15,7 @@ static String extractProtoFromForwardedHeader(String forwarded) { if (start < 0) { return null; } - start += 4; // start is now the index after proto= + start += 6; // start is now the index after proto= if (start >= forwarded.length() - 1) { // the value after for= must not be empty return null; } @@ -53,7 +53,7 @@ private static String extractProto(String forwarded, int start) { if (forwarded.charAt(start) == '"') { return extractProto(forwarded, start + 1); } - for (int i = start; i < forwarded.length() - 1; i++) { + for (int i = start; i < forwarded.length(); i++) { char c = forwarded.charAt(i); if (c == ',' || c == ';' || c == '"') { if (i == start) { // empty string @@ -89,7 +89,7 @@ private static String extractIpAddress(String forwarded, int start) { return forwarded.substring(start + 1, end); } boolean inIpv4 = false; - for (int i = start; i < forwarded.length() - 1; i++) { + for (int i = start; i < forwarded.length(); i++) { char c = forwarded.charAt(i); if (c == '.') { inIpv4 = true; From 3944bc24d614770bf40872ee37245e2e70aa3e57 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 11 Feb 2022 16:45:04 -0800 Subject: [PATCH 4/5] Fix checkstyle complaining about abbrevation --- .../http/ForwardedHeaderParser.java | 8 +++-- .../http/HttpServerAttributesExtractor.java | 8 ++--- .../http/ForwardedHeaderParserTest.java | 34 +++++++++---------- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParser.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParser.java index 441dbf78966c..f4c045df1ccd 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParser.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParser.java @@ -9,6 +9,7 @@ final class ForwardedHeaderParser { + /** Extract proto (aka scheme) from "Forwarded" http header. */ @Nullable static String extractProtoFromForwardedHeader(String forwarded) { int start = forwarded.toLowerCase().indexOf("proto="); @@ -22,11 +23,13 @@ static String extractProtoFromForwardedHeader(String forwarded) { return extractProto(forwarded, start); } + /** Extract proto (aka scheme) from "X-Forwarded-Proto" http header. */ @Nullable - static String extractProtoFromXForwardedProtoHeader(String forwardedProto) { + static String extractProtoFromForwardedProtoHeader(String forwardedProto) { return extractProto(forwardedProto, 0); } + /** Extract client IP address from "Forwarded" http header. */ @Nullable static String extractClientIpFromForwardedHeader(String forwarded) { int start = forwarded.toLowerCase().indexOf("for="); @@ -40,8 +43,9 @@ static String extractClientIpFromForwardedHeader(String forwarded) { return extractIpAddress(forwarded, start); } + /** Extract client IP address from "X-Forwarded-For" http header. */ @Nullable - static String extractClientIpFromXForwardedForHeader(String forwardedFor) { + static String extractClientIpFromForwardedForHeader(String forwardedFor) { return extractIpAddress(forwardedFor, 0); } diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractor.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractor.java index 48181abee405..dc07dd0136c7 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractor.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractor.java @@ -5,10 +5,10 @@ package io.opentelemetry.instrumentation.api.instrumenter.http; +import static io.opentelemetry.instrumentation.api.instrumenter.http.ForwardedHeaderParser.extractClientIpFromForwardedForHeader; import static io.opentelemetry.instrumentation.api.instrumenter.http.ForwardedHeaderParser.extractClientIpFromForwardedHeader; -import static io.opentelemetry.instrumentation.api.instrumenter.http.ForwardedHeaderParser.extractClientIpFromXForwardedForHeader; import static io.opentelemetry.instrumentation.api.instrumenter.http.ForwardedHeaderParser.extractProtoFromForwardedHeader; -import static io.opentelemetry.instrumentation.api.instrumenter.http.ForwardedHeaderParser.extractProtoFromXForwardedProtoHeader; +import static io.opentelemetry.instrumentation.api.instrumenter.http.ForwardedHeaderParser.extractProtoFromForwardedProtoHeader; import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; @@ -109,7 +109,7 @@ private String forwardedProto(REQUEST request) { // try X-Forwarded-Proto forwarded = firstHeaderValue(getter.requestHeader(request, "x-forwarded-proto")); if (forwarded != null) { - return extractProtoFromXForwardedProtoHeader(forwarded); + return extractProtoFromForwardedProtoHeader(forwarded); } return null; @@ -129,7 +129,7 @@ private String clientIp(REQUEST request) { // try X-Forwarded-For forwarded = firstHeaderValue(getter.requestHeader(request, "x-forwarded-for")); if (forwarded != null) { - return extractClientIpFromXForwardedForHeader(forwarded); + return extractClientIpFromForwardedForHeader(forwarded); } return null; diff --git a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParserTest.java b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParserTest.java index c475f31d6365..0e0e28cd5084 100644 --- a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParserTest.java +++ b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParserTest.java @@ -49,12 +49,12 @@ void extractProtoFromForwardedHeaderWithQuotesAndTrailingComma() { @Test void extractProtoFromXForwardedProtoHeader() { - assertThat(ForwardedHeaderParser.extractProtoFromXForwardedProtoHeader("xyz")).isEqualTo("xyz"); + assertThat(ForwardedHeaderParser.extractProtoFromForwardedProtoHeader("xyz")).isEqualTo("xyz"); } @Test void extractProtoFromXForwardedProtoHeaderWithQuotes() { - assertThat(ForwardedHeaderParser.extractProtoFromXForwardedProtoHeader("\"xyz\"")) + assertThat(ForwardedHeaderParser.extractProtoFromForwardedProtoHeader("\"xyz\"")) .isEqualTo("xyz"); } @@ -183,14 +183,14 @@ void extractClientIpFromForwardedHeaderWithMixedSplitterIpv6AndPort() { @Test void extractClientIpFromXForwardedForHeader() { - assertThat(ForwardedHeaderParser.extractClientIpFromXForwardedForHeader("1.1.1.1")) + assertThat(ForwardedHeaderParser.extractClientIpFromForwardedForHeader("1.1.1.1")) .isEqualTo("1.1.1.1"); } @Test void extractClientIpFromXForwardedForHeaderWithIpv6() { assertThat( - ForwardedHeaderParser.extractClientIpFromXForwardedForHeader( + ForwardedHeaderParser.extractClientIpFromForwardedForHeader( "\"[1111:1111:1111:1111:1111:1111:1111:1111]\"")) .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); } @@ -198,7 +198,7 @@ void extractClientIpFromXForwardedForHeaderWithIpv6() { @Test void extractClientIpFromXForwardedForHeaderWithIpv6Unquoted() { assertThat( - ForwardedHeaderParser.extractClientIpFromXForwardedForHeader( + ForwardedHeaderParser.extractClientIpFromForwardedForHeader( "[1111:1111:1111:1111:1111:1111:1111:1111]")) .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); } @@ -206,21 +206,21 @@ void extractClientIpFromXForwardedForHeaderWithIpv6Unquoted() { @Test void extractClientIpFromXForwardedForHeaderWithIpv6Unbracketed() { assertThat( - ForwardedHeaderParser.extractClientIpFromXForwardedForHeader( + ForwardedHeaderParser.extractClientIpFromForwardedForHeader( "1111:1111:1111:1111:1111:1111:1111:1111")) .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); } @Test void extractClientIpFromXForwardedForHeaderWithPort() { - assertThat(ForwardedHeaderParser.extractClientIpFromXForwardedForHeader("1.1.1.1:2222")) + assertThat(ForwardedHeaderParser.extractClientIpFromForwardedForHeader("1.1.1.1:2222")) .isEqualTo("1.1.1.1"); } @Test void extractClientIpFromXForwardedForHeaderWithIpv6AndPort() { assertThat( - ForwardedHeaderParser.extractClientIpFromXForwardedForHeader( + ForwardedHeaderParser.extractClientIpFromForwardedForHeader( "\"[1111:1111:1111:1111:1111:1111:1111:1111]:2222\"")) .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); } @@ -228,26 +228,26 @@ void extractClientIpFromXForwardedForHeaderWithIpv6AndPort() { @Test void extractClientIpFromXForwardedForHeaderWithIpv6UnquotedAndPort() { assertThat( - ForwardedHeaderParser.extractClientIpFromXForwardedForHeader( + ForwardedHeaderParser.extractClientIpFromForwardedForHeader( "[1111:1111:1111:1111:1111:1111:1111:1111]:2222")) .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); } @Test void extractClientIpFromEmptyXForwardedForHeader() { - assertThat(ForwardedHeaderParser.extractClientIpFromXForwardedForHeader("")).isNull(); + assertThat(ForwardedHeaderParser.extractClientIpFromForwardedForHeader("")).isNull(); } @Test void extractClientIpFromXForwardedForHeaderWithMultiple() { - assertThat(ForwardedHeaderParser.extractClientIpFromXForwardedForHeader("1.1.1.1,1.2.3.4")) + assertThat(ForwardedHeaderParser.extractClientIpFromForwardedForHeader("1.1.1.1,1.2.3.4")) .isEqualTo("1.1.1.1"); } @Test void extractClientIpFromXForwardedForHeaderWithMultipleIpv6() { assertThat( - ForwardedHeaderParser.extractClientIpFromXForwardedForHeader( + ForwardedHeaderParser.extractClientIpFromForwardedForHeader( "\"[1111:1111:1111:1111:1111:1111:1111:1111]\",1.2.3.4")) .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); } @@ -255,7 +255,7 @@ void extractClientIpFromXForwardedForHeaderWithMultipleIpv6() { @Test void extractClientIpFromXForwardedForHeaderWithMultipleIpv6Unquoted() { assertThat( - ForwardedHeaderParser.extractClientIpFromXForwardedForHeader( + ForwardedHeaderParser.extractClientIpFromForwardedForHeader( "[1111:1111:1111:1111:1111:1111:1111:1111],1.2.3.4")) .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); } @@ -263,21 +263,21 @@ void extractClientIpFromXForwardedForHeaderWithMultipleIpv6Unquoted() { @Test void extractClientIpFromXForwardedForHeaderWithMultipleIpv6Unbracketed() { assertThat( - ForwardedHeaderParser.extractClientIpFromXForwardedForHeader( + ForwardedHeaderParser.extractClientIpFromForwardedForHeader( "1111:1111:1111:1111:1111:1111:1111:1111,1.2.3.4")) .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); } @Test void extractClientIpFromXForwardedForHeaderWithMultipleAndPort() { - assertThat(ForwardedHeaderParser.extractClientIpFromXForwardedForHeader("1.1.1.1:2222,1.2.3.4")) + assertThat(ForwardedHeaderParser.extractClientIpFromForwardedForHeader("1.1.1.1:2222,1.2.3.4")) .isEqualTo("1.1.1.1"); } @Test void extractClientIpFromXForwardedForHeaderWithMultipleIpv6AndPort() { assertThat( - ForwardedHeaderParser.extractClientIpFromXForwardedForHeader( + ForwardedHeaderParser.extractClientIpFromForwardedForHeader( "\"[1111:1111:1111:1111:1111:1111:1111:1111]:2222\",1.2.3.4")) .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); } @@ -285,7 +285,7 @@ void extractClientIpFromXForwardedForHeaderWithMultipleIpv6AndPort() { @Test void extractClientIpFromXForwardedForHeaderWithMultipleIpv6UnquotedAndPort() { assertThat( - ForwardedHeaderParser.extractClientIpFromXForwardedForHeader( + ForwardedHeaderParser.extractClientIpFromForwardedForHeader( "[1111:1111:1111:1111:1111:1111:1111:1111]:2222,1.2.3.4")) .isEqualTo("1111:1111:1111:1111:1111:1111:1111:1111"); } From 30dfbc32efc4334f245df8b3adb2f220d1bffef9 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Sat, 12 Feb 2022 10:56:58 -0800 Subject: [PATCH 5/5] checkstyle --- .../http/ForwardedHeaderParserTest.java | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParserTest.java b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParserTest.java index 0e0e28cd5084..2756aab924a8 100644 --- a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParserTest.java +++ b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHeaderParserTest.java @@ -48,12 +48,12 @@ void extractProtoFromForwardedHeaderWithQuotesAndTrailingComma() { } @Test - void extractProtoFromXForwardedProtoHeader() { + void extractProtoFromForwardedProtoHeader() { assertThat(ForwardedHeaderParser.extractProtoFromForwardedProtoHeader("xyz")).isEqualTo("xyz"); } @Test - void extractProtoFromXForwardedProtoHeaderWithQuotes() { + void extractProtoFromForwardedProtoHeaderWithQuotes() { assertThat(ForwardedHeaderParser.extractProtoFromForwardedProtoHeader("\"xyz\"")) .isEqualTo("xyz"); } @@ -182,13 +182,13 @@ void extractClientIpFromForwardedHeaderWithMixedSplitterIpv6AndPort() { } @Test - void extractClientIpFromXForwardedForHeader() { + void extractClientIpFromForwardedForHeader() { assertThat(ForwardedHeaderParser.extractClientIpFromForwardedForHeader("1.1.1.1")) .isEqualTo("1.1.1.1"); } @Test - void extractClientIpFromXForwardedForHeaderWithIpv6() { + void extractClientIpFromForwardedForHeaderWithIpv6() { assertThat( ForwardedHeaderParser.extractClientIpFromForwardedForHeader( "\"[1111:1111:1111:1111:1111:1111:1111:1111]\"")) @@ -196,7 +196,7 @@ void extractClientIpFromXForwardedForHeaderWithIpv6() { } @Test - void extractClientIpFromXForwardedForHeaderWithIpv6Unquoted() { + void extractClientIpFromForwardedForHeaderWithIpv6Unquoted() { assertThat( ForwardedHeaderParser.extractClientIpFromForwardedForHeader( "[1111:1111:1111:1111:1111:1111:1111:1111]")) @@ -204,7 +204,7 @@ void extractClientIpFromXForwardedForHeaderWithIpv6Unquoted() { } @Test - void extractClientIpFromXForwardedForHeaderWithIpv6Unbracketed() { + void extractClientIpFromForwardedForHeaderWithIpv6Unbracketed() { assertThat( ForwardedHeaderParser.extractClientIpFromForwardedForHeader( "1111:1111:1111:1111:1111:1111:1111:1111")) @@ -212,13 +212,13 @@ void extractClientIpFromXForwardedForHeaderWithIpv6Unbracketed() { } @Test - void extractClientIpFromXForwardedForHeaderWithPort() { + void extractClientIpFromForwardedForHeaderWithPort() { assertThat(ForwardedHeaderParser.extractClientIpFromForwardedForHeader("1.1.1.1:2222")) .isEqualTo("1.1.1.1"); } @Test - void extractClientIpFromXForwardedForHeaderWithIpv6AndPort() { + void extractClientIpFromForwardedForHeaderWithIpv6AndPort() { assertThat( ForwardedHeaderParser.extractClientIpFromForwardedForHeader( "\"[1111:1111:1111:1111:1111:1111:1111:1111]:2222\"")) @@ -226,7 +226,7 @@ void extractClientIpFromXForwardedForHeaderWithIpv6AndPort() { } @Test - void extractClientIpFromXForwardedForHeaderWithIpv6UnquotedAndPort() { + void extractClientIpFromForwardedForHeaderWithIpv6UnquotedAndPort() { assertThat( ForwardedHeaderParser.extractClientIpFromForwardedForHeader( "[1111:1111:1111:1111:1111:1111:1111:1111]:2222")) @@ -234,18 +234,18 @@ void extractClientIpFromXForwardedForHeaderWithIpv6UnquotedAndPort() { } @Test - void extractClientIpFromEmptyXForwardedForHeader() { + void extractClientIpFromEmptyForwardedForHeader() { assertThat(ForwardedHeaderParser.extractClientIpFromForwardedForHeader("")).isNull(); } @Test - void extractClientIpFromXForwardedForHeaderWithMultiple() { + void extractClientIpFromForwardedForHeaderWithMultiple() { assertThat(ForwardedHeaderParser.extractClientIpFromForwardedForHeader("1.1.1.1,1.2.3.4")) .isEqualTo("1.1.1.1"); } @Test - void extractClientIpFromXForwardedForHeaderWithMultipleIpv6() { + void extractClientIpFromForwardedForHeaderWithMultipleIpv6() { assertThat( ForwardedHeaderParser.extractClientIpFromForwardedForHeader( "\"[1111:1111:1111:1111:1111:1111:1111:1111]\",1.2.3.4")) @@ -253,7 +253,7 @@ void extractClientIpFromXForwardedForHeaderWithMultipleIpv6() { } @Test - void extractClientIpFromXForwardedForHeaderWithMultipleIpv6Unquoted() { + void extractClientIpFromForwardedForHeaderWithMultipleIpv6Unquoted() { assertThat( ForwardedHeaderParser.extractClientIpFromForwardedForHeader( "[1111:1111:1111:1111:1111:1111:1111:1111],1.2.3.4")) @@ -261,7 +261,7 @@ void extractClientIpFromXForwardedForHeaderWithMultipleIpv6Unquoted() { } @Test - void extractClientIpFromXForwardedForHeaderWithMultipleIpv6Unbracketed() { + void extractClientIpFromForwardedForHeaderWithMultipleIpv6Unbracketed() { assertThat( ForwardedHeaderParser.extractClientIpFromForwardedForHeader( "1111:1111:1111:1111:1111:1111:1111:1111,1.2.3.4")) @@ -269,13 +269,13 @@ void extractClientIpFromXForwardedForHeaderWithMultipleIpv6Unbracketed() { } @Test - void extractClientIpFromXForwardedForHeaderWithMultipleAndPort() { + void extractClientIpFromForwardedForHeaderWithMultipleAndPort() { assertThat(ForwardedHeaderParser.extractClientIpFromForwardedForHeader("1.1.1.1:2222,1.2.3.4")) .isEqualTo("1.1.1.1"); } @Test - void extractClientIpFromXForwardedForHeaderWithMultipleIpv6AndPort() { + void extractClientIpFromForwardedForHeaderWithMultipleIpv6AndPort() { assertThat( ForwardedHeaderParser.extractClientIpFromForwardedForHeader( "\"[1111:1111:1111:1111:1111:1111:1111:1111]:2222\",1.2.3.4")) @@ -283,7 +283,7 @@ void extractClientIpFromXForwardedForHeaderWithMultipleIpv6AndPort() { } @Test - void extractClientIpFromXForwardedForHeaderWithMultipleIpv6UnquotedAndPort() { + void extractClientIpFromForwardedForHeaderWithMultipleIpv6UnquotedAndPort() { assertThat( ForwardedHeaderParser.extractClientIpFromForwardedForHeader( "[1111:1111:1111:1111:1111:1111:1111:1111]:2222,1.2.3.4"))