Repository navigation
fix(s3): emit API reasons and bucket resources in EventBridge events #9141
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
aniketshukla1
wants to merge
3
commits into
floci-io:main
Choose a base branch
from
aniketshukla1:fix/s3-eventbridge-created-reason-9135
base: main
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.
+251
−24
Open
Changes from 1 commit
Commits
Show all changes
3 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 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
118 changes: 118 additions & 0 deletions
118
src/test/java/io/floci/aws/services/s3/S3EventBridgeEventShapeIntegrationTest.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,118 @@ | ||
| package io.floci.aws.services.s3; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import io.floci.aws.testing.AwsTestClients; | ||
| import io.quarkus.test.junit.QuarkusTest; | ||
| import jakarta.inject.Inject; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.CsvSource; | ||
| import software.amazon.awssdk.core.sync.RequestBody; | ||
| import software.amazon.awssdk.regions.Region; | ||
| import software.amazon.awssdk.services.eventbridge.EventBridgeClient; | ||
| import software.amazon.awssdk.services.eventbridge.model.Target; | ||
| import software.amazon.awssdk.services.s3.S3Client; | ||
| import software.amazon.awssdk.services.s3.model.CompletedPart; | ||
| import software.amazon.awssdk.services.sqs.SqsClient; | ||
| import software.amazon.awssdk.services.sqs.model.Message; | ||
| import software.amazon.awssdk.services.sqs.model.QueueAttributeName; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| import static io.restassured.RestAssured.given; | ||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| @QuarkusTest | ||
| class S3EventBridgeEventShapeIntegrationTest { | ||
|
|
||
| private static final String ACCOUNT = "000000000000"; | ||
|
|
||
| @Inject | ||
| ObjectMapper objectMapper; | ||
|
|
||
| @ParameterizedTest | ||
| @CsvSource({ | ||
| "us-east-1,aws,PutObject", | ||
| "us-east-1,aws,POST Object", | ||
| "us-east-1,aws,CopyObject", | ||
| "us-east-1,aws,CompleteMultipartUpload", | ||
| "cn-north-1,aws-cn,PutObject" | ||
| }) | ||
| void creationEventCarriesApiReasonAndBucketArn(String regionId, String partition, String reason) throws Exception { | ||
| Region region = Region.of(regionId); | ||
| String name = "s3-eb-shape-" + UUID.randomUUID(); | ||
| String bucketArn = "arn:" + partition + ":s3:::" + name; | ||
| try (S3Client s3 = AwsTestClients.create(S3Client.builder().forcePathStyle(true), ACCOUNT, region); | ||
| SqsClient sqs = AwsTestClients.create(SqsClient.builder(), ACCOUNT, region); | ||
| EventBridgeClient events = AwsTestClients.create(EventBridgeClient.builder(), ACCOUNT, region)) { | ||
| s3.createBucket(request -> { | ||
| request.bucket(name); | ||
| if (!region.equals(Region.US_EAST_1)) { | ||
| request.createBucketConfiguration(configuration -> configuration.locationConstraint(regionId)); | ||
| } | ||
| }); | ||
| String queueUrl = sqs.createQueue(request -> request.queueName(name)).queueUrl(); | ||
| String queueArn = sqs.getQueueAttributes(request -> request.queueUrl(queueUrl) | ||
| .attributeNames(QueueAttributeName.QUEUE_ARN)).attributes().get(QueueAttributeName.QUEUE_ARN); | ||
| events.putRule(request -> request.name(name).eventPattern("{\"source\":[\"aws.s3\"]}")); | ||
| try { | ||
| assertEquals(0, events.putTargets(request -> request.rule(name) | ||
| .targets(Target.builder().id("queue").arn(queueArn).build())).failedEntryCount()); | ||
| // Seed copy input before enabling notifications, so the queue receives only the operation under test. | ||
| s3.putObject(request -> request.bucket(name).key("source"), RequestBody.fromString("content")); | ||
| s3.putBucketNotificationConfiguration(request -> request.bucket(name) | ||
| .notificationConfiguration(configuration -> configuration.eventBridgeConfiguration(bridge -> {}))); | ||
|
|
||
| switch (reason) { | ||
| case "PutObject" -> s3.putObject(request -> request.bucket(name).key("created"), | ||
| RequestBody.fromString("content")); | ||
| case "POST Object" -> given() | ||
| .header("Authorization", "AWS4-HMAC-SHA256 Credential=" + ACCOUNT + "/20261121/" | ||
| + regionId + "/s3/aws4_request, SignedHeaders=host, Signature=test") | ||
| .multiPart("key", "created") | ||
| .multiPart("file", "created", "content".getBytes(StandardCharsets.UTF_8), "text/plain") | ||
| .when().post("/" + name).then().statusCode(204); | ||
| case "CopyObject" -> s3.copyObject(request -> request.copySource(name + "/source") | ||
| .destinationBucket(name).destinationKey("created")); | ||
| case "CompleteMultipartUpload" -> { | ||
| String uploadId = s3.createMultipartUpload(request -> request.bucket(name).key("created")) | ||
| .uploadId(); | ||
| String etag = s3.uploadPart(request -> request.bucket(name).key("created") | ||
| .uploadId(uploadId).partNumber(1), RequestBody.fromString("content")).eTag(); | ||
| s3.completeMultipartUpload(request -> request.bucket(name).key("created").uploadId(uploadId) | ||
| .multipartUpload(upload -> upload.parts(CompletedPart.builder() | ||
| .partNumber(1).eTag(etag).build()))); | ||
| } | ||
| default -> throw new AssertionError("Unknown creation API: " + reason); | ||
| } | ||
|
|
||
| List<Message> messages = sqs.receiveMessage(request -> request.queueUrl(queueUrl) | ||
| .waitTimeSeconds(2).maxNumberOfMessages(1)).messages(); | ||
| assertEquals(1, messages.size(), "Expected the S3 event through EventBridge and SQS"); | ||
| String body = messages.getFirst().body(); | ||
| JsonNode event = objectMapper.readTree(body); | ||
| String pattern = "{\"source\":[\"aws.s3\"],\"resources\":[\"" + bucketArn | ||
| + "\"],\"detail\":{\"reason\":[\"" + reason + "\"]}}"; | ||
| assertAll( | ||
| () -> assertEquals("Object Created", event.path("detail-type").asText()), | ||
| () -> assertEquals(regionId, event.path("region").asText()), | ||
| () -> assertEquals("created", event.path("detail").path("object").path("key").asText()), | ||
| () -> assertEquals(reason, event.path("detail").path("reason").asText()), | ||
| () -> assertEquals(objectMapper.valueToTree(List.of(bucketArn)), event.path("resources")), | ||
| () -> assertTrue(events.testEventPattern(request -> request.eventPattern(pattern).event(body)) | ||
| .result(), "Consumers must be able to filter on the API reason and bucket ARN")); | ||
| } finally { | ||
| events.removeTargets(request -> request.rule(name).ids("queue")); | ||
| events.deleteRule(request -> request.name(name)); | ||
| sqs.deleteQueue(request -> request.queueUrl(queueUrl)); | ||
| s3.putBucketNotificationConfiguration(request -> request.bucket(name) | ||
| .notificationConfiguration(configuration -> {})); | ||
| s3.deleteObject(request -> request.bucket(name).key("created")); | ||
| s3.deleteObject(request -> request.bucket(name).key("source")); | ||
| s3.deleteBucket(request -> request.bucket(name)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.