-
Notifications
You must be signed in to change notification settings - Fork 214
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
Support reading S3 Event messages from SNS fan-out #2622
Merged
dlvenable
merged 2 commits into
opensearch-project:main
from
dlvenable:2604-sns-sqs-messages
May 8, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
46 changes: 46 additions & 0 deletions
46
...-source/src/main/java/org/opensearch/dataprepper/plugins/source/S3EventMessageParser.java
This file contains 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,46 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.source; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
/** | ||
* Handles parsing of S3 Event messages. | ||
*/ | ||
public class S3EventMessageParser { | ||
private static final String SNS_MESSAGE_KEY = "Message"; | ||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
/** | ||
* Parses a message body into an {@link S3EventNotification} class. | ||
* | ||
* @param messageBody Input body | ||
* @return The parsed event notification | ||
* @throws JsonProcessingException An exception with parsing the event notification. | ||
*/ | ||
S3EventNotification parseMessage(final String messageBody) throws JsonProcessingException { | ||
final JsonNode parsedNode = objectMapper.readTree(messageBody); | ||
|
||
final JsonNode eventNode = getS3EventNode(parsedNode); | ||
|
||
return objectMapper.treeToValue(eventNode, S3EventNotification.class); | ||
} | ||
|
||
private JsonNode getS3EventNode(final JsonNode parsedNode) throws JsonProcessingException { | ||
if(isSnsWrappedMessage(parsedNode)) { | ||
final String messageString = parsedNode.get(SNS_MESSAGE_KEY).asText(); | ||
return objectMapper.readValue(messageString, JsonNode.class); | ||
} | ||
|
||
return parsedNode; | ||
} | ||
|
||
private boolean isSnsWrappedMessage(final JsonNode parsedNode) { | ||
return parsedNode.has(SNS_MESSAGE_KEY); | ||
} | ||
} |
This file contains 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 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 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
86 changes: 86 additions & 0 deletions
86
...rce/src/test/java/org/opensearch/dataprepper/plugins/source/S3EventMessageParserTest.java
This file contains 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,86 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.source; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.hasSize; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class S3EventMessageParserTest { | ||
private static final String DIRECT_SQS_MESSAGE = | ||
"{\"Records\":[{\"eventVersion\":\"2.1\",\"eventSource\":\"aws:s3\",\"awsRegion\":\"us-east-1\",\"eventTime\":\"2023-04-28T16:00:11.324Z\"," + | ||
"\"eventName\":\"ObjectCreated:Put\",\"userIdentity\":{\"principalId\":\"AWS:xyz\"},\"requestParameters\":{\"sourceIPAddress\":\"127.0.0.1\"}," + | ||
"\"responseElements\":{\"x-amz-request-id\":\"xyz\",\"x-amz-id-2\":\"xyz\"},\"s3\":{\"s3SchemaVersion\":\"1.0\"," + | ||
"\"configurationId\":\"xyz\",\"bucket\":{\"name\":\"my-bucket\",\"ownerIdentity\":{\"principalId\":\"ABC\"}," + | ||
"\"arn\":\"arn:aws:s3:::my-bucket\"},\"object\":{\"key\":\"path/to/myfile.log.gz\",\"size\":3159112,\"eTag\":\"abcd123\"," + | ||
"\"sequencer\":\"000\"}}}]}"; | ||
|
||
private static final String SNS_BASED_MESSAGE = "{\n" + | ||
" \"Type\" : \"Notification\",\n" + | ||
" \"MessageId\" : \"4e01e115-5b91-5096-8a74-bee95ed1e123\",\n" + | ||
" \"TopicArn\" : \"arn:aws:sns:us-east-1:123456789012:notifications\",\n" + | ||
" \"Subject\" : \"Amazon S3 Notification\",\n" + | ||
" \"Message\" : \"{\\\"Records\\\":[{\\\"eventVersion\\\":\\\"2.1\\\",\\\"eventSource\\\":\\\"aws:s3\\\",\\\"awsRegion\\\":\\\"us-east-1\\\",\\\"eventTime\\\":\\\"2023-05-02T13:37:03.502Z\\\",\\\"eventName\\\":\\\"ObjectCreated:Put\\\",\\\"userIdentity\\\":{\\\"principalId\\\":\\\"AWS:ABC\\\"},\\\"requestParameters\\\":{\\\"sourceIPAddress\\\":\\\"127.0.0.1\\\"},\\\"responseElements\\\":{\\\"x-amz-request-id\\\":\\\"ABC\\\",\\\"x-amz-id-2\\\":\\\"ABC\\\"},\\\"s3\\\":{\\\"s3SchemaVersion\\\":\\\"1.0\\\",\\\"configurationId\\\":\\\"S3ToSnsTest\\\",\\\"bucket\\\":{\\\"name\\\":\\\"my-sns-bucket\\\",\\\"ownerIdentity\\\":{\\\"principalId\\\":\\\"ABC\\\"},\\\"arn\\\":\\\"arn:aws:s3:::my-sns-bucket\\\"},\\\"object\\\":{\\\"key\\\":\\\"path/to/testlogs.log.gz\\\",\\\"size\\\":25,\\\"eTag\\\":\\\"abc\\\",\\\"sequencer\\\":\\\"ABC\\\"}}}]}\",\n" + | ||
" \"Timestamp\" : \"2023-05-02T13:37:04.554Z\",\n" + | ||
" \"SignatureVersion\" : \"1\",\n" + | ||
" \"Signature\" : \"x//abcde==\",\n" + | ||
" \"SigningCertURL\" : \"https://sns.us-east-1.amazonaws.com/SimpleNotificationService.pem\",\n" + | ||
" \"UnsubscribeURL\" : \"https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:123456789012:notifications:abc\"\n" + | ||
"}"; | ||
|
||
private S3EventMessageParser createObjectUnderTest() { | ||
return new S3EventMessageParser(); | ||
} | ||
|
||
@Test | ||
void parseMessage_returns_expected_S3EventNotification_from_SQS_message() throws JsonProcessingException { | ||
final S3EventNotification s3EventNotification = createObjectUnderTest().parseMessage(DIRECT_SQS_MESSAGE); | ||
|
||
assertThat(s3EventNotification, notNullValue()); | ||
assertThat(s3EventNotification.getRecords(), notNullValue()); | ||
assertThat(s3EventNotification.getRecords(), hasSize(1)); | ||
final S3EventNotification.S3EventNotificationRecord s3EventNotificationRecord = s3EventNotification.getRecords().get(0); | ||
assertThat(s3EventNotificationRecord, notNullValue()); | ||
assertThat(s3EventNotificationRecord.getEventName(), equalTo("ObjectCreated:Put")); | ||
assertThat(s3EventNotificationRecord.getS3(), notNullValue()); | ||
assertThat(s3EventNotificationRecord.getS3().getBucket(), notNullValue()); | ||
assertThat(s3EventNotificationRecord.getS3().getBucket().getName(), equalTo("my-bucket")); | ||
assertThat(s3EventNotificationRecord.getS3().getObject(), notNullValue()); | ||
assertThat(s3EventNotificationRecord.getS3().getObject().getKey(), equalTo("path/to/myfile.log.gz")); | ||
} | ||
|
||
@Test | ||
void parseMessage_returns_expected_S3EventNotification_from_SNS_to_SQS_message() throws JsonProcessingException { | ||
final S3EventNotification s3EventNotification = createObjectUnderTest().parseMessage(SNS_BASED_MESSAGE); | ||
|
||
assertThat(s3EventNotification, notNullValue()); | ||
assertThat(s3EventNotification.getRecords(), notNullValue()); | ||
assertThat(s3EventNotification.getRecords(), hasSize(1)); | ||
final S3EventNotification.S3EventNotificationRecord s3EventNotificationRecord = s3EventNotification.getRecords().get(0); | ||
assertThat(s3EventNotificationRecord, notNullValue()); | ||
assertThat(s3EventNotificationRecord.getEventName(), equalTo("ObjectCreated:Put")); | ||
assertThat(s3EventNotificationRecord.getS3(), notNullValue()); | ||
assertThat(s3EventNotificationRecord.getS3().getBucket(), notNullValue()); | ||
assertThat(s3EventNotificationRecord.getS3().getBucket().getName(), equalTo("my-sns-bucket")); | ||
assertThat(s3EventNotificationRecord.getS3().getObject(), notNullValue()); | ||
assertThat(s3EventNotificationRecord.getS3().getObject().getKey(), equalTo("path/to/testlogs.log.gz")); | ||
} | ||
|
||
@Test | ||
void parseMessage_throws_for_TestEvent() { | ||
final String testEventMessage = "{\"Service\":\"Amazon S3\",\"Event\":\"s3:TestEvent\",\"Time\":\"2022-10-15T16:36:25.510Z\"," + | ||
"\"Bucket\":\"bucketname\",\"RequestId\":\"abcdefg\",\"HostId\":\"hijklm\"}"; | ||
|
||
final S3EventMessageParser objectUnderTest = createObjectUnderTest(); | ||
|
||
assertThrows(JsonProcessingException.class, () -> objectUnderTest.parseMessage(testEventMessage)); | ||
} | ||
} |
This file contains 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
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.
Can we also validate the type of the message or else we are trying to parse any JOSN node with
Message
? Looks like the JSON object will have"Type" : "Notification"
field.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 think it is fine if the Type doesn't match. If the data comes and there is a
Message
which is not what we expect, we will still have a JSON parsing exception.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.
Sounds good, just looked at the failure scenario looks like we log the following if there is a failure.
LOG.error("SQS message with message ID:{} has invalid body which cannot be parsed into S3EventNotification. {}.", message.messageId(), e.getMessage());