generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 66
SigV4 Authentication support for http exporter #1019
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
Merged
+399
−1
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
779da66
[OTLP] Add custom OTLP SigV4 OkHttp exporter
majanjua-amzn 0f1ddd1
added SigV4 support for HttpSpan Exporter
liustve 10355ff
Merge branch 'main' into sigv4
liustve e5bf335
formatting fix
liustve 5c5f8e1
Merge remote-tracking branch 'origin/sigv4' into sigv4
liustve b5a3529
checking instance
liustve a460a84
updated gradle and exporter
liustve 8c35d35
added unit tests
liustve fc76008
added documentation
liustve 03d99f7
added copier constructor
liustve 51a4478
Merge branch 'main' into sigv4
liustve 4a7f845
cleaning up code
liustve 8bbbd7a
Merge remote-tracking branch 'origin/sigv4' into sigv4
liustve 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,9 +41,12 @@ dependencies { | |
| // Import AWS SDK v1 core for ARN parsing utilities | ||
| implementation("com.amazonaws:aws-java-sdk-core:1.12.773") | ||
| // Export configuration | ||
| compileOnly("io.opentelemetry:opentelemetry-exporter-otlp") | ||
| implementation("io.opentelemetry:opentelemetry-exporter-otlp") | ||
| // For Udp emitter | ||
| compileOnly("io.opentelemetry:opentelemetry-exporter-otlp-common") | ||
| // For HTTP SigV4 emitter | ||
| implementation("software.amazon.awssdk:auth:2.30.14") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why use fixed versions of http auth. Will it cause conflicts when customers use a different version in their application? |
||
| implementation("software.amazon.awssdk:http-auth-aws:2.30.14") | ||
|
|
||
| testImplementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure") | ||
| testImplementation("io.opentelemetry:opentelemetry-sdk-testing") | ||
|
|
||
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
141 changes: 141 additions & 0 deletions
141
.../src/main/java/software/amazon/opentelemetry/javaagent/providers/OtlpAwsSpanExporter.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,141 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package software.amazon.opentelemetry.javaagent.providers; | ||
|
|
||
| import io.opentelemetry.exporter.internal.otlp.traces.TraceRequestMarshaler; | ||
| import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter; | ||
| import io.opentelemetry.sdk.common.CompletableResultCode; | ||
| import io.opentelemetry.sdk.trace.data.SpanData; | ||
| import io.opentelemetry.sdk.trace.export.SpanExporter; | ||
| import java.io.*; | ||
| import java.net.URI; | ||
| import java.util.*; | ||
liustve marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import java.util.function.Supplier; | ||
| import javax.annotation.concurrent.Immutable; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import software.amazon.awssdk.auth.credentials.AwsCredentials; | ||
| import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; | ||
| import software.amazon.awssdk.http.SdkHttpFullRequest; | ||
| import software.amazon.awssdk.http.SdkHttpMethod; | ||
| import software.amazon.awssdk.http.SdkHttpRequest; | ||
| import software.amazon.awssdk.http.auth.aws.signer.AwsV4HttpSigner; | ||
| import software.amazon.awssdk.http.auth.spi.signer.SignedRequest; | ||
|
|
||
| /** | ||
| * This exporter extends the functionality of the OtlpHttpSpanExporter to allow spans to be exported | ||
| * to the XRay OTLP endpoint https://xray.[AWSRegion].amazonaws.com/v1/traces. Utilizes the AWSSDK | ||
| * library to sign and directly inject SigV4 Authentication to the exported request's headers. | ||
| * https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-OTLPEndpoint.html | ||
| */ | ||
| @Immutable | ||
| public class OtlpAwsSpanExporter implements SpanExporter { | ||
| private static final Logger logger = LoggerFactory.getLogger(OtlpAwsSpanExporter.class); | ||
| private static final AwsV4HttpSigner signer = AwsV4HttpSigner.create(); | ||
|
|
||
| private final OtlpHttpSpanExporter parentExporter; | ||
| private final String awsRegion; | ||
| private final String endpoint; | ||
| private Collection<SpanData> spanData; | ||
|
|
||
| public OtlpAwsSpanExporter(String endpoint) { | ||
| this.parentExporter = | ||
| OtlpHttpSpanExporter.builder() | ||
| .setEndpoint(endpoint) | ||
| .setHeaders(new SigV4AuthHeaderSupplier()) | ||
| .build(); | ||
|
|
||
| this.awsRegion = endpoint.split("\\.")[1]; | ||
majanjua-amzn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this.endpoint = endpoint; | ||
| this.spanData = new ArrayList<>(); | ||
| } | ||
|
|
||
| /** | ||
| * Overrides the upstream implementation of export. All behaviors are the same except if the | ||
| * endpoint is an XRay OTLP endpoint, we will sign the request with SigV4 in headers before | ||
| * sending it to the endpoint. Otherwise, we will skip signing. | ||
| */ | ||
| @Override | ||
| public CompletableResultCode export(Collection<SpanData> spans) { | ||
| this.spanData = spans; | ||
| return this.parentExporter.export(spans); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableResultCode flush() { | ||
| return this.parentExporter.flush(); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableResultCode shutdown() { | ||
| return this.parentExporter.shutdown(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return this.parentExporter.toString(); | ||
| } | ||
|
|
||
| private final class SigV4AuthHeaderSupplier implements Supplier<Map<String, String>> { | ||
|
|
||
| @Override | ||
| public Map<String, String> get() { | ||
| try { | ||
| ByteArrayOutputStream encodedSpans = new ByteArrayOutputStream(); | ||
| TraceRequestMarshaler.create(OtlpAwsSpanExporter.this.spanData).writeBinaryTo(encodedSpans); | ||
|
|
||
| SdkHttpRequest httpRequest = | ||
| SdkHttpFullRequest.builder() | ||
| .uri(URI.create(OtlpAwsSpanExporter.this.endpoint)) | ||
| .method(SdkHttpMethod.POST) | ||
| .putHeader("Content-Type", "application/x-protobuf") | ||
| .contentStreamProvider(() -> new ByteArrayInputStream(encodedSpans.toByteArray())) | ||
| .build(); | ||
|
|
||
| AwsCredentials credentials = DefaultCredentialsProvider.create().resolveCredentials(); | ||
|
|
||
| SignedRequest signedRequest = | ||
| signer.sign( | ||
| b -> | ||
| b.identity(credentials) | ||
| .request(httpRequest) | ||
| .putProperty(AwsV4HttpSigner.SERVICE_SIGNING_NAME, "xray") | ||
| .putProperty( | ||
| AwsV4HttpSigner.REGION_NAME, OtlpAwsSpanExporter.this.awsRegion) | ||
| .payload(() -> new ByteArrayInputStream(encodedSpans.toByteArray()))); | ||
|
|
||
| Map<String, String> result = new HashMap<>(); | ||
|
|
||
| Map<String, List<String>> headers = signedRequest.request().headers(); | ||
| headers.forEach( | ||
| (key, values) -> { | ||
| if (!values.isEmpty()) { | ||
| result.put(key, values.get(0)); | ||
| } | ||
| }); | ||
|
|
||
| return result; | ||
|
|
||
| } catch (Exception e) { | ||
| logger.error( | ||
| "Failed to sign/authenticate the given exported Span request to OTLP CloudWatch endpoint with error: {}", | ||
| e.getMessage()); | ||
|
|
||
| return new HashMap<>(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
174 changes: 174 additions & 0 deletions
174
.../test/java/software/amazon/opentelemetry/javaagent/providers/OtlpAwsSpanExporterTest.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,174 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package software.amazon.opentelemetry.javaagent.providers; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
| import static org.mockito.Mockito.*; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter; | ||
| import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporterBuilder; | ||
| import io.opentelemetry.sdk.common.CompletableResultCode; | ||
| import io.opentelemetry.sdk.trace.export.SpanExporter; | ||
| import java.net.URI; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Consumer; | ||
| import java.util.function.Supplier; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.mockito.Mock; | ||
| import org.mockito.MockedStatic; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
| import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; | ||
| import software.amazon.awssdk.auth.credentials.AwsCredentials; | ||
| import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; | ||
| import software.amazon.awssdk.core.exception.SdkClientException; | ||
| import software.amazon.awssdk.http.SdkHttpFullRequest; | ||
| import software.amazon.awssdk.http.SdkHttpMethod; | ||
| import software.amazon.awssdk.http.auth.aws.signer.AwsV4HttpSigner; | ||
| import software.amazon.awssdk.http.auth.spi.signer.SignRequest.Builder; | ||
| import software.amazon.awssdk.http.auth.spi.signer.SignedRequest; | ||
| import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| public class OtlpAwsSpanExporterTest { | ||
| private static final String OTLP_CW_ENDPOINT = "https://xray.us-east-1.amazonaws.com/v1/traces"; | ||
liustve marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private static final String AUTHORIZATION_HEADER = "Authorization"; | ||
| private static final String X_AMZ_DATE_HEADER = "X-Amz-Date"; | ||
| private static final String X_AMZ_SECURITY_TOKEN_HEADER = "X-Amz-Security-Token"; | ||
|
|
||
| private static final String EXPECTED_AUTH_HEADER = | ||
| "AWS4-HMAC-SHA256 Credential=test_key/some_date/us-east-1/xray/aws4_request"; | ||
| private static final String EXPECTED_AUTH_X_AMZ_DATE = "some_date"; | ||
| private static final String EXPECTED_AUTH_SECURITY_TOKEN = "test_token"; | ||
|
|
||
| AwsCredentials credentials = AwsBasicCredentials.create("test_access_key", "test_secret_key"); | ||
| SignedRequest signedRequest = | ||
| SignedRequest.builder() | ||
| .request( | ||
| SdkHttpFullRequest.builder() | ||
| .method(SdkHttpMethod.POST) | ||
| .uri(URI.create(OTLP_CW_ENDPOINT)) | ||
| .putHeader(AUTHORIZATION_HEADER, EXPECTED_AUTH_HEADER) | ||
| .putHeader("X-Amz-Date", EXPECTED_AUTH_X_AMZ_DATE) | ||
| .putHeader("X-Amz-Security-Token", EXPECTED_AUTH_SECURITY_TOKEN) | ||
| .build()) | ||
| .build(); | ||
|
|
||
| private MockedStatic<DefaultCredentialsProvider> mockDefaultCredentialsProvider; | ||
| private MockedStatic<AwsV4HttpSigner> mockAwsV4HttpSigner; | ||
| private MockedStatic<OtlpHttpSpanExporter> otlpSpanExporterMock; | ||
|
|
||
| @Mock private DefaultCredentialsProvider credentialsProvider; | ||
| @Mock private AwsV4HttpSigner signer; | ||
| @Mock private OtlpHttpSpanExporterBuilder mockBuilder; | ||
| @Mock private OtlpHttpSpanExporter mockExporter; | ||
|
|
||
| private ArgumentCaptor<Supplier<Map<String, String>>> headersCaptor; | ||
|
|
||
| @BeforeEach | ||
| void setup() { | ||
| this.mockDefaultCredentialsProvider = mockStatic(DefaultCredentialsProvider.class); | ||
| this.mockDefaultCredentialsProvider | ||
| .when(DefaultCredentialsProvider::create) | ||
| .thenReturn(credentialsProvider); | ||
|
|
||
| this.mockAwsV4HttpSigner = mockStatic(AwsV4HttpSigner.class); | ||
| this.mockAwsV4HttpSigner.when(AwsV4HttpSigner::create).thenReturn(this.signer); | ||
|
|
||
| this.otlpSpanExporterMock = mockStatic(OtlpHttpSpanExporter.class); | ||
|
|
||
| this.headersCaptor = ArgumentCaptor.forClass(Supplier.class); | ||
|
|
||
| when(OtlpHttpSpanExporter.builder()).thenReturn(mockBuilder); | ||
| when(this.mockBuilder.setEndpoint(any())).thenReturn(mockBuilder); | ||
| when(this.mockBuilder.setHeaders(headersCaptor.capture())).thenReturn(mockBuilder); | ||
| when(this.mockBuilder.build()).thenReturn(mockExporter); | ||
| when(this.mockExporter.export(any())).thenReturn(CompletableResultCode.ofSuccess()); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void afterEach() { | ||
| reset(this.signer, this.credentialsProvider); | ||
| this.mockDefaultCredentialsProvider.close(); | ||
| this.mockAwsV4HttpSigner.close(); | ||
| this.otlpSpanExporterMock.close(); | ||
| } | ||
|
|
||
| @Test | ||
| void testAwsSpanExporterAddsSigV4Headers() { | ||
|
|
||
| when(this.credentialsProvider.resolveCredentials()).thenReturn(this.credentials); | ||
| when(this.signer.sign((Consumer<Builder<AwsCredentialsIdentity>>) any())) | ||
| .thenReturn(this.signedRequest); | ||
|
|
||
| SpanExporter exporter = new OtlpAwsSpanExporter(OTLP_CW_ENDPOINT); | ||
|
|
||
| exporter.export(List.of()); | ||
|
|
||
| Map<String, String> headers = this.headersCaptor.getValue().get(); | ||
|
|
||
| assertTrue(headers.containsKey(X_AMZ_DATE_HEADER)); | ||
| assertTrue(headers.containsKey(AUTHORIZATION_HEADER)); | ||
| assertTrue(headers.containsKey(X_AMZ_SECURITY_TOKEN_HEADER)); | ||
|
|
||
| assertEquals(EXPECTED_AUTH_HEADER, headers.get(AUTHORIZATION_HEADER)); | ||
| assertEquals(EXPECTED_AUTH_X_AMZ_DATE, headers.get(X_AMZ_DATE_HEADER)); | ||
| assertEquals(EXPECTED_AUTH_SECURITY_TOKEN, headers.get(X_AMZ_SECURITY_TOKEN_HEADER)); | ||
| } | ||
liustve marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Test | ||
| void testAwsSpanExporterDoesNotAddSigV4HeadersIfFailureToRetrieveCredentials() { | ||
|
|
||
| when(this.credentialsProvider.resolveCredentials()) | ||
| .thenThrow(SdkClientException.builder().message("bad credentials").build()); | ||
|
|
||
| SpanExporter exporter = new OtlpAwsSpanExporter(OTLP_CW_ENDPOINT); | ||
|
|
||
| exporter.export(List.of()); | ||
|
|
||
| Supplier<Map<String, String>> headersSupplier = headersCaptor.getValue(); | ||
| Map<String, String> headers = headersSupplier.get(); | ||
|
|
||
| assertFalse(headers.containsKey(X_AMZ_DATE_HEADER)); | ||
| assertFalse(headers.containsKey(AUTHORIZATION_HEADER)); | ||
| assertFalse(headers.containsKey(X_AMZ_SECURITY_TOKEN_HEADER)); | ||
|
|
||
| verifyNoInteractions(this.signer); | ||
| } | ||
|
|
||
| @Test | ||
| void testAwsSpanExporterDoesNotAddSigV4HeadersIfFailureToSignHeaders() { | ||
|
|
||
| when(this.credentialsProvider.resolveCredentials()).thenReturn(this.credentials); | ||
| when(this.signer.sign((Consumer<Builder<AwsCredentialsIdentity>>) any())) | ||
| .thenThrow(SdkClientException.builder().message("bad signature").build()); | ||
|
|
||
| SpanExporter exporter = new OtlpAwsSpanExporter(OTLP_CW_ENDPOINT); | ||
|
|
||
| exporter.export(List.of()); | ||
|
|
||
| Map<String, String> headers = this.headersCaptor.getValue().get(); | ||
|
|
||
| assertFalse(headers.containsKey(X_AMZ_DATE_HEADER)); | ||
| assertFalse(headers.containsKey(AUTHORIZATION_HEADER)); | ||
| assertFalse(headers.containsKey(X_AMZ_SECURITY_TOKEN_HEADER)); | ||
| } | ||
| } | ||
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.
Why do we change it to
implementation?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.
I changed it from compileOnly to implementation is because the http exporter is now a required dependency when I began executing the auto instrumentation at runtime. Otherwise I believe I was getting a ClassNotFoundException everytime I ran the exporter
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.
We changed it to
compileOnlydue to a regression it introduced, see #651. You need to figure out a way to avoid the same issue before submit the change.