-
Notifications
You must be signed in to change notification settings - Fork 992
SnsMessageManager Impl #6804
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
Open
dagnir
wants to merge
1
commit into
feature/master/sns-message-manager
Choose a base branch
from
dongie/msg-manager-impl
base: feature/master/sns-message-manager
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
SnsMessageManager Impl #6804
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions
6
.changes/next-release/feature-AmazonSNSMessageManager-607af7b.json
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,6 @@ | ||
| { | ||
| "type": "feature", | ||
| "category": "Amazon SNS Message Manager", | ||
| "contributor": "", | ||
| "description": "This change introduces the SNS Message Manager for 2.x, a library used to parse and validate messages received from SNS. This aims to provide the same functionality as [SnsMessageManager](https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/sns/message/SnsMessageManager.html) from 1.x." | ||
| } |
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
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 |
|---|---|---|
|
|
@@ -98,6 +98,12 @@ | |
| <artifactId>httpclient5</artifactId> | ||
| <version>${httpcomponents.client5.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>software.amazon.awssdk</groupId> | ||
| <artifactId>apache5-client</artifactId> | ||
|
Contributor
Author
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. This more or less means we should block this until #6732 is merged |
||
| <version>${project.version}</version> | ||
| <scope>runtime</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.assertj</groupId> | ||
| <artifactId>assertj-core</artifactId> | ||
|
|
||
128 changes: 128 additions & 0 deletions
128
...ge-manager/src/main/java/software/amazon/awssdk/messagemanager/sns/SnsMessageManager.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,128 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * 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.awssdk.messagemanager.sns; | ||
|
|
||
| import java.io.InputStream; | ||
| import software.amazon.awssdk.annotations.SdkPublicApi; | ||
| import software.amazon.awssdk.http.SdkHttpClient; | ||
| import software.amazon.awssdk.messagemanager.sns.internal.DefaultSnsMessageManager; | ||
| import software.amazon.awssdk.messagemanager.sns.model.SnsMessage; | ||
| import software.amazon.awssdk.regions.Region; | ||
| import software.amazon.awssdk.utils.SdkAutoCloseable; | ||
|
|
||
|
|
||
| /** | ||
| * Message manager for validating SNS message signatures. Create an instance using {@link #builder()}. | ||
| * | ||
| * <p>This manager provides automatic validation of SNS message signatures received via HTTP/HTTPS endpoints, | ||
| * ensuring that messages originate from Amazon SNS and have not been modified during transmission. | ||
| * It supports both SignatureVersion1 (SHA1) and SignatureVersion2 (SHA256) as per AWS SNS standards. | ||
| * | ||
| * <p>The manager handles certificate retrieval, caching, and validation automatically, supporting different | ||
| * AWS regions and partitions (aws, aws-gov, aws-cn). | ||
| * | ||
| * <p>Basic usage with default configuration: | ||
| * <pre> | ||
| * {@code | ||
| * SnsMessageManager messageManager = SnsMessageManager.builder().build(); | ||
| * | ||
| * try { | ||
| * SnsMessage validatedMessage = messageManager.parseMessage(messageBody); | ||
| * String messageContent = validatedMessage.message(); | ||
| * String topicArn = validatedMessage.topicArn(); | ||
| * // Process the validated message | ||
| * } catch (SdkClientException e) { | ||
| * // Handle validation failure | ||
| * logger.error("SNS message validation failed: {}", e.getMessage()); | ||
| * } | ||
| * } | ||
| * </pre> | ||
| * | ||
| * <p>Advanced usage with custom HTTP client: | ||
| * <pre> | ||
| * {@code | ||
| * SnsMessageManager messageManager = SnsMessageManager.builder() | ||
| * .httpClient(ApacheHttpClient.create()) | ||
| * .build(); | ||
| * } | ||
| * </pre> | ||
| * | ||
| * @see SnsMessage | ||
| * @see Builder | ||
| */ | ||
| @SdkPublicApi | ||
| public interface SnsMessageManager extends SdkAutoCloseable { | ||
|
|
||
| /** | ||
| * Creates a builder for configuring and creating an {@link SnsMessageManager}. | ||
| * | ||
| * @return A new builder. | ||
| */ | ||
| static Builder builder() { | ||
| return DefaultSnsMessageManager.builder(); | ||
| } | ||
|
|
||
| /** | ||
| * Parses and validates an SNS message from a stream. | ||
| * <p> | ||
| * This method reads the JSON message payload, validates the signature, returns a parsed SNS message object with all | ||
| * message attributes if validation succeeds. | ||
| */ | ||
| SnsMessage parseMessage(InputStream messageStream); | ||
|
|
||
| /** | ||
| * Parses and validates an SNS message from a string. | ||
| * <p> | ||
| * This method reads the JSON message payload, validates the signature, returns a parsed SNS message object with all | ||
| * message attributes if validation succeeds. | ||
| */ | ||
| SnsMessage parseMessage(String messageContent); | ||
|
|
||
| /** | ||
| * Close this {@code SnsMessageManager}, releasing any resources it owned. | ||
| * <p> | ||
| * <b>Note:</b> if you provided your own {@link SdkHttpClient}, you must close it separately. | ||
| */ | ||
| @Override | ||
| void close(); | ||
|
|
||
| interface Builder { | ||
|
|
||
| /** | ||
| * Sets the HTTP client to use for certificate retrieval. The caller is responsible for closing this HTTP client after | ||
| * the {@code SnsMessageManager} is closed. | ||
| * | ||
| * @param httpClient The HTTP client to use for fetching signing certificates. | ||
| * @return This builder for method chaining. | ||
| */ | ||
| Builder httpClient(SdkHttpClient httpClient); | ||
|
|
||
| /** | ||
| * Sets the AWS region for certificate validation. This region must match the SNS region where the messages originate. | ||
| * | ||
| * @param region The AWS region where the SNS messages originate. | ||
| * @return This builder for method chaining. | ||
| */ | ||
| Builder region(Region region); | ||
|
|
||
| /** | ||
| * Builds an instance of {@link SnsMessageManager} based on the supplied configurations. | ||
| * | ||
| * @return An initialized SnsMessageManager ready to validate SNS messages. | ||
| */ | ||
| SnsMessageManager build(); | ||
| } | ||
| } |
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
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
127 changes: 127 additions & 0 deletions
127
...ain/java/software/amazon/awssdk/messagemanager/sns/internal/DefaultSnsMessageManager.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,127 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * 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.awssdk.messagemanager.sns.internal; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.InputStream; | ||
| import java.net.URI; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.security.PublicKey; | ||
| import java.time.Duration; | ||
| import software.amazon.awssdk.annotations.SdkInternalApi; | ||
| import software.amazon.awssdk.annotations.SdkTestInternalApi; | ||
| import software.amazon.awssdk.core.internal.http.loader.DefaultSdkHttpClientBuilder; | ||
| import software.amazon.awssdk.http.SdkHttpClient; | ||
| import software.amazon.awssdk.http.SdkHttpConfigurationOption; | ||
| import software.amazon.awssdk.messagemanager.sns.SnsMessageManager; | ||
| import software.amazon.awssdk.messagemanager.sns.model.SnsMessage; | ||
| import software.amazon.awssdk.regions.Region; | ||
| import software.amazon.awssdk.utils.AttributeMap; | ||
| import software.amazon.awssdk.utils.Validate; | ||
|
|
||
| @SdkInternalApi | ||
| public final class DefaultSnsMessageManager implements SnsMessageManager { | ||
| private static final AttributeMap HTTP_CLIENT_DEFAULTS = | ||
| AttributeMap.builder() | ||
| .put(SdkHttpConfigurationOption.CONNECTION_TIMEOUT, Duration.ofSeconds(10)) | ||
| .put(SdkHttpConfigurationOption.READ_TIMEOUT, Duration.ofSeconds(30)) | ||
| .build(); | ||
|
|
||
| private final SnsMessageUnmarshaller unmarshaller; | ||
| private final CertificateRetriever certRetriever; | ||
| private final SignatureValidator signatureValidator; | ||
|
|
||
| private DefaultSnsMessageManager(BuilderImpl builder) { | ||
| this.unmarshaller = new SnsMessageUnmarshaller(); | ||
|
|
||
| SnsHostProvider hostProvider = new SnsHostProvider(builder.region); | ||
| URI signingCertEndpoint = hostProvider.regionalEndpoint(); | ||
| String signingCertCommonName = hostProvider.signingCertCommonName(); | ||
|
|
||
| SdkHttpClient httpClient = resolveHttpClient(builder); | ||
| certRetriever = builder.certRetriever != null | ||
| ? builder.certRetriever | ||
| : new CertificateRetriever(httpClient, signingCertEndpoint.getHost(), signingCertCommonName); | ||
|
|
||
| signatureValidator = new SignatureValidator(); | ||
| } | ||
|
|
||
| @Override | ||
| public SnsMessage parseMessage(InputStream message) { | ||
| Validate.notNull(message, "message cannot be null"); | ||
|
|
||
| SnsMessage snsMessage = unmarshaller.unmarshall(message); | ||
| PublicKey certificate = certRetriever.retrieveCertificate(snsMessage.signingCertUrl()); | ||
|
|
||
| signatureValidator.validateSignature(snsMessage, certificate); | ||
|
|
||
| return snsMessage; | ||
| } | ||
|
|
||
| @Override | ||
| public SnsMessage parseMessage(String message) { | ||
| Validate.notNull(message, "message cannot be null"); | ||
| return parseMessage(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8))); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| certRetriever.close(); | ||
| } | ||
|
|
||
| public static Builder builder() { | ||
| return new BuilderImpl(); | ||
| } | ||
|
|
||
| private static SdkHttpClient resolveHttpClient(BuilderImpl builder) { | ||
| if (builder.httpClient != null) { | ||
| return new UnmanagedSdkHttpClient(builder.httpClient); | ||
| } | ||
|
|
||
| return new DefaultSdkHttpClientBuilder().buildWithDefaults(HTTP_CLIENT_DEFAULTS); | ||
| } | ||
|
|
||
| static class BuilderImpl implements SnsMessageManager.Builder { | ||
| private Region region; | ||
| private SdkHttpClient httpClient; | ||
|
|
||
| // Testing only | ||
| private CertificateRetriever certRetriever; | ||
|
|
||
| @Override | ||
| public Builder httpClient(SdkHttpClient httpClient) { | ||
| this.httpClient = httpClient; | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public Builder region(Region region) { | ||
| this.region = region; | ||
| return this; | ||
| } | ||
|
|
||
| @SdkTestInternalApi | ||
| Builder certificateRetriever(CertificateRetriever certificateRetriever) { | ||
| this.certRetriever = certificateRetriever; | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public SnsMessageManager build() { | ||
| return new DefaultSnsMessageManager(this); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
We'll need to discuss this change, but hopefully not contentious. I didn't want to move it out of the
internalpackage as that would be a breaking change.