fix(s3): retain notification rule identity and event metadata (#6963)

This commit is contained in:
Daniel Adler authored and GitHub committed 2026-10-30 07:36:32 -04:00
1 parent 4aaa5cc466
commit 1053d60346
4 files changed
+182 -12

No files matched your search

@@ -13,6 +13,8 @@ import jakarta.inject.Inject;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.time.Instant;
import java.util.ArrayList;
@@ -175,6 +177,62 @@ class S3NotificationDestinationValidationIntegrationTest {
assertTestEvent(receiveQueueMessage(queueUrls.get(1)).getFirst().getBody());
}
@Test
void queueAndTopicCopiesKeepOperationMetadataAndTheirOwnRuleIds() throws Exception {
String directQueueArn = createQueue("event-direct", Map.of());
String subscribedQueueArn = createQueue("event-subscribed", Map.of());
String topicArn = createTopic("event-topic");
snsService.subscribe(topicArn, "sqs", subscribedQueueArn, REGION,
Map.of("RawMessageDelivery", "true"));
putNotification(notificationConfiguration(
queueConfiguration(directQueueArn, "<Id>queue-rule</Id>")
+ topicConfiguration(topicArn, "<Id>topic-rule</Id>")), true)
.statusCode(200);
try {
given().body("payload").put("/" + bucket + "/a%20b%2Bc.txt").then().statusCode(200);
JsonNode queueEvent = objectMapper.readTree(receiveQueueMessage(queueUrls.get(0))
.getFirst().getBody()).path("Records").get(0);
JsonNode topicEvent = objectMapper.readTree(receiveQueueMessage(queueUrls.get(1))
.getFirst().getBody()).path("Records").get(0);
assertEquals("queue-rule", queueEvent.path("s3").path("configurationId").asText());
assertEquals("topic-rule", topicEvent.path("s3").path("configurationId").asText());
assertFalse(queueEvent.path("responseElements").path("x-amz-id-2").asText().isBlank());
assertEquals("000000000000", queueEvent.path("s3").path("bucket")
.path("ownerIdentity").path("principalId").asText());
assertEquals(queueEvent.path("eventTime"), topicEvent.path("eventTime"));
assertEquals(queueEvent.path("responseElements"), topicEvent.path("responseElements"));
assertEquals(queueEvent.path("s3").path("object"), topicEvent.path("s3").path("object"));
} finally {
given().delete("/" + bucket + "/a%20b%2Bc.txt").then().statusCode(204);
}
}
@ParameterizedTest
@ValueSource(strings = {"", "<Id></Id>", "<Id> \t </Id>"})
void missingQueueAndTopicRuleIdsUseEmulator(String idXml) throws Exception {
String directQueueArn = createQueue("fallback-direct", Map.of());
String subscribedQueueArn = createQueue("fallback-subscribed", Map.of());
String topicArn = createTopic("fallback-topic");
snsService.subscribe(topicArn, "sqs", subscribedQueueArn, REGION,
Map.of("RawMessageDelivery", "true"));
putNotification(notificationConfiguration(
queueConfiguration(directQueueArn, idXml)
+ topicConfiguration(topicArn, idXml)), true)
.statusCode(200);
try {
given().body("payload").put("/" + bucket + "/fallback.txt").then().statusCode(200);
for (String queueUrl : queueUrls) {
JsonNode event = objectMapper.readTree(receiveQueueMessage(queueUrl)
.getFirst().getBody()).path("Records").get(0);
assertEquals("emulator", event.path("s3").path("configurationId").asText());
}
} finally {
given().delete("/" + bucket + "/fallback.txt").then().statusCode(204);
}
}
@Test
void crossRegionTopicReceivesTestEventAndLaterObjectEvent() throws Exception {
String topicRegion = "ap-southeast-2";
@@ -335,12 +393,20 @@ class S3NotificationDestinationValidationIntegrationTest {
}
private static String queueConfiguration(String arn) {
return "<QueueConfiguration><Queue>" + arn
return queueConfiguration(arn, "");
}
private static String queueConfiguration(String arn, String idXml) {
return "<QueueConfiguration>" + idXml + "<Queue>" + arn
+ "</Queue><Event>s3:ObjectCreated:*</Event></QueueConfiguration>";
}
private static String topicConfiguration(String arn) {
return "<TopicConfiguration><Topic>" + arn
return topicConfiguration(arn, "");
}
private static String topicConfiguration(String arn, String idXml) {
return "<TopicConfiguration>" + idXml + "<Topic>" + arn
+ "</Topic><Event>s3:ObjectCreated:*</Event></TopicConfiguration>";
}
@@ -28,6 +28,7 @@ import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;
import java.io.ByteArrayInputStream;
@@ -972,6 +973,68 @@ class S3ServiceTest {
assertTrue(deleted.compareTo(put) > 0, put + " then " + deleted);
}
@Test
void notificationCarriesDocumentedEventFieldsAndConfiguredId() throws IOException {
RecordingLambdaInvoker invoker = new RecordingLambdaInvoker();
S3Service service = bucketNotifyingOnCreation(invoker, "notification-consumer-fields");
service.putObject("test-bucket", "original/a b+c.txt",
"payload".getBytes(StandardCharsets.UTF_8), "text/plain", null);
JsonNode event = onlyRecordedS3Event(invoker);
assertFalse(event.path("responseElements").path("x-amz-id-2").asText().isBlank());
assertFalse(event.path("responseElements").path("x-amz-request-id").asText().isBlank());
assertEquals("000000000000", event.path("s3").path("bucket")
.path("ownerIdentity").path("principalId").asText());
assertEquals("lambda-notif", event.path("s3").path("configurationId").asText());
assertEquals("original/a+b%2Bc.txt", event.path("s3").path("object").path("key").asText());
assertEquals(7, event.path("s3").path("object").path("size").asInt());
}
@Test
void eachMatchingNotificationCarriesItsOwnConfigurationId() throws IOException {
RecordingLambdaInvoker invoker = new RecordingLambdaInvoker();
S3Service service = bucketNotifyingOnCreation(invoker, "notification-rule-identity");
NotificationConfiguration config = service.getBucketNotificationConfiguration("test-bucket");
config.getLambdaFunctionConfigurations().add(new LambdaNotification(
"second-rule", "arn:aws:lambda:us-east-1:000000000000:function:second",
List.of("s3:ObjectCreated:*"), List.of()));
service.putBucketNotificationConfiguration("test-bucket", config, true);
service.putBucketVersioning("test-bucket", "Enabled");
invoker.afterDelivery = () -> service.putBucketVersioning("test-bucket", "Suspended");
S3Object stored = service.putObject("test-bucket", "original.txt", new byte[]{1}, "text/plain", null);
assertEquals(2, invoker.payloads.size());
ObjectMapper mapper = new ObjectMapper();
assertEquals("lambda-notif", mapper.readTree(invoker.payloads.get(0))
.path("Records").get(0).path("s3").path("configurationId").asText());
assertEquals("second-rule", mapper.readTree(invoker.payloads.get(1))
.path("Records").get(0).path("s3").path("configurationId").asText());
JsonNode first = mapper.readTree(invoker.payloads.get(0)).path("Records").get(0);
JsonNode second = mapper.readTree(invoker.payloads.get(1)).path("Records").get(0);
assertFalse(first.path("s3").path("object").path("versionId").asText().isBlank());
assertEquals(stored.getVersionId(), first.path("s3").path("object").path("versionId").asText());
assertEquals(first.path("eventTime"), second.path("eventTime"));
assertEquals(first.path("responseElements"), second.path("responseElements"));
assertEquals(first.path("s3").path("object"), second.path("s3").path("object"));
}
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {" \t "})
void missingLambdaNotificationRuleIdUsesEmulator(String id) throws IOException {
RecordingLambdaInvoker invoker = new RecordingLambdaInvoker();
S3Service service = bucketNotifyingOnCreation(invoker, "notification-missing-rule-id");
NotificationConfiguration config = new NotificationConfiguration();
config.getLambdaFunctionConfigurations().add(new LambdaNotification(
id, "arn:aws:lambda:us-east-1:000000000000:function:s3-notif-test",
List.of("s3:ObjectCreated:*"), List.of()));
service.putBucketNotificationConfiguration("test-bucket", config, true);
service.putObject("test-bucket", "original.txt", new byte[]{1}, "text/plain", null);
assertEquals("emulator", onlyRecordedS3Event(invoker).path("s3").path("configurationId").asText());
}
private S3Service bucketNotifyingOnCreation(RecordingLambdaInvoker lambdaInvoker, String dataDir) {
return bucketNotifyingOn(lambdaInvoker, dataDir, "s3:ObjectCreated:*");
}
@@ -1029,6 +1092,8 @@ class S3ServiceTest {
private String region;
private String functionName;
private byte[] payload;
private final List<byte[]> payloads = new ArrayList<>();
private Runnable afterDelivery;
private InvocationType type;
@Override
@@ -1036,7 +1101,11 @@ class S3ServiceTest {
this.region = region;
this.functionName = functionName;
this.payload = payload;
this.payloads.add(payload);
this.type = type;
if (afterDelivery != null) {
afterDelivery.run();
}
}
}