fix(s3): emit ObjectTagging notifications for PutObjectTagging and DeleteObjectTagging (#10062)

This commit is contained in:
Stefan Dobrovolny authored and GitHub committed 2026-12-03 08:29:15 -04:00
1 parent 00f3d9f894
commit de2fbf8dee
4 files changed
+229 -4

No files matched your search

@@ -3667,15 +3667,21 @@ public class S3Service implements Resettable, ResourceProvider {
Map<String, String> tags) {
Bucket bucket = resolveBucket(bucketName)
.orElseThrow(() -> noSuchBucket());
S3Object obj;
String sequencer;
synchronized (bucket) {
AccountAwareStorageBackend.OwnedEntry<S3Object> ownedObject =
getTaggingTargetEntry(bucketName, key, versionId, method);
S3Object obj = ownedObject.value();
obj = ownedObject.value();
obj.setTags(tags);
putObjectMetadataForAccount(ownedObject.account(), bucketName, key, obj);
LOG.debugv("{0} tags of object: {1}/{2} version {3}", method, bucketName, key, obj.getVersionId());
return obj.getVersionId();
sequencer = nextEventSequencer();
}
// Fired outside the bucket monitor, like the annotation events.
String eventName = "PUT".equals(method) ? "ObjectTagging:Put" : "ObjectTagging:Delete";
fireNotifications(bucketName, key, eventName, obj, sequencer);
return obj.getVersionId();
}
/**
@@ -7930,6 +7936,8 @@ public class S3Service implements Resettable, ResourceProvider {
String detailType = switch (eventName) {
case "ObjectAnnotation:Put" -> "Object Annotation Created";
case "ObjectAnnotation:Delete" -> "Object Annotation Removed";
case "ObjectTagging:Put" -> "Object Tags Added";
case "ObjectTagging:Delete" -> "Object Tags Deleted";
default -> eventName.startsWith("ObjectCreated") ? "Object Created" : "Object Deleted";
};
Map<String, Object> entry = EventBridgeService.serviceEventEntry("aws.s3", detailType,
@@ -7968,7 +7976,10 @@ public class S3Service implements Resettable, ResourceProvider {
detail.put("requester", "aws:emulator");
detail.put("source-ip-address", "127.0.0.1");
}
detail.put("reason", eventBridgeReason(eventName));
String reason = eventBridgeReason(eventName);
if (reason != null) {
detail.put("reason", reason);
}
String deletionType = eventBridgeDeletionType(eventName);
if (deletionType != null) {
detail.put("deletion-type", deletionType);
@@ -7979,9 +7990,13 @@ public class S3Service implements Resettable, ResourceProvider {
}
}
/** EventBridge names the API rather than the classic notification event. */
/**
* EventBridge names the API rather than the classic notification event. Object Tags Added
* and Object Tags Deleted carry no reason, so tagging events return null.
*/
private static String eventBridgeReason(String eventName) {
return switch (eventName) {
case "ObjectTagging:Put", "ObjectTagging:Delete" -> null;
case "ObjectCreated:Put" -> "PutObject";
case "ObjectCreated:Post" -> "POST Object";
case "ObjectCreated:Copy" -> "CopyObject";
@@ -131,4 +131,40 @@ class S3EventBridgeEventShapeIntegrationTest {
.event(event.toString())).result(), "Consumers must be able to filter on the API reason and bucket ARN"));
}
}
/** Object Tags Added and Object Tags Deleted name no API reason on AWS. */
@ParameterizedTest
@CsvSource({
"PutObjectTagging,Object Tags Added",
"DeleteObjectTagging,Object Tags Deleted"
})
void taggingEventCarriesDetailTypeWithoutReason(String operation, String detailType) throws Exception {
String name = "s3-eb-tags-" + UUID.randomUUID();
String authorization = EnforcementFixtures.auth(ACCOUNT, "us-east-1", "s3");
String pattern = "{\"source\":[\"aws.s3\"],\"detail\":{\"bucket\":{\"name\":[\"" + name + "\"]}}}";
try (S3Client s3 = AwsTestClients.create(S3Client.builder().forcePathStyle(true), ACCOUNT, Region.US_EAST_1);
EventRuleQueue queue = EventRuleQueue.route(ACCOUNT, "us-east-1", name, pattern)) {
s3.createBucket(request -> request.bucket(name));
int port = RestAssured.port;
cleanup.register(() -> EnforcementFixtures.removeBucket(port, authorization, name));
s3.putObject(request -> request.bucket(name).key("tagged"), RequestBody.fromString("content"));
s3.putBucketNotificationConfiguration(request -> request.bucket(name)
.notificationConfiguration(configuration -> configuration.eventBridgeConfiguration(bridge -> {})));
switch (operation) {
case "PutObjectTagging" -> s3.putObjectTagging(request -> request.bucket(name).key("tagged")
.tagging(tagging -> tagging.tagSet(tag -> tag.key("verdict").value("clean"))));
case "DeleteObjectTagging" -> s3.deleteObjectTagging(request -> request.bucket(name).key("tagged"));
default -> throw new AssertionError("Unknown S3 API: " + operation);
}
List<JsonNode> messages = queue.drain();
assertEquals(1, messages.size(), "Expected the tagging event through EventBridge and SQS");
JsonNode event = messages.getFirst();
assertAll(
() -> assertEquals(detailType, event.path("detail-type").asString()),
() -> assertEquals("tagged", event.path("detail").path("object").path("key").asString()),
() -> assertFalse(event.path("detail").has("reason"), "Tagging events carry no reason"));
}
}
}
@@ -0,0 +1,172 @@
package io.floci.aws.services.s3;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.path.xml.XmlPath;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
/**
* PutObjectTagging and DeleteObjectTagging emit s3:ObjectTagging:Put and s3:ObjectTagging:Delete,
* as on AWS. GuardDuty Malware Protection for S3 reports a verdict by tagging the object, and
* consumers subscribe to that tagging event.
*/
@QuarkusTest
class S3ObjectTaggingNotificationIntegrationTest {
private static final String BUCKET = "object-tagging-notification-bucket";
private static final String KEY = "uploads/scanned.txt";
@Test
void taggingEmitsObjectTaggingNotifications() {
String putQueueName = "object-tagging-put-queue";
String wildcardQueueName = "object-tagging-wildcard-queue";
String createdQueueName = "object-tagging-created-queue";
String putQueueUrl = createQueue(putQueueName);
String wildcardQueueUrl = createQueue(wildcardQueueName);
String createdQueueUrl = createQueue(createdQueueName);
given().put("/" + BUCKET).then().statusCode(200);
given().body("scanned").put("/" + BUCKET + "/" + KEY).then().statusCode(200);
try {
given()
.contentType("application/xml")
.queryParam("notification", "")
.body("""
<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<QueueConfiguration>
<Id>tagging-put</Id>
<Queue>arn:aws:sqs:us-east-1:000000000000:%s</Queue>
<Event>s3:ObjectTagging:Put</Event>
</QueueConfiguration>
<QueueConfiguration>
<Id>tagging-wildcard</Id>
<Queue>arn:aws:sqs:us-east-1:000000000000:%s</Queue>
<Event>s3:ObjectTagging:*</Event>
</QueueConfiguration>
<QueueConfiguration>
<Id>created</Id>
<Queue>arn:aws:sqs:us-east-1:000000000000:%s</Queue>
<Event>s3:ObjectCreated:*</Event>
</QueueConfiguration>
</NotificationConfiguration>
""".formatted(putQueueName, wildcardQueueName, createdQueueName))
.when()
.put("/" + BUCKET)
.then()
.statusCode(200);
consumeMessage(putQueueUrl, "\"Event\":\"s3:TestEvent\"");
consumeMessage(wildcardQueueUrl, "\"Event\":\"s3:TestEvent\"");
consumeMessage(createdQueueUrl, "\"Event\":\"s3:TestEvent\"");
given()
.contentType("application/xml")
.queryParam("tagging", "")
.body("""
<Tagging><TagSet><Tag>
<Key>GuardDutyMalwareScanStatus</Key><Value>NO_THREATS_FOUND</Value>
</Tag></TagSet></Tagging>
""")
.when()
.put("/" + BUCKET + "/" + KEY)
.then()
.statusCode(200);
String putEvent = consumeMessage(putQueueUrl, "\"eventName\":\"ObjectTagging:Put\"");
assertThat(putEvent, containsString("\"key\":\"" + KEY + "\""));
assertThat(putEvent, containsString("\"name\":\"" + BUCKET + "\""));
consumeMessage(wildcardQueueUrl, "\"eventName\":\"ObjectTagging:Put\"");
assertQueueEmpty(createdQueueUrl);
given()
.queryParam("tagging", "")
.when()
.delete("/" + BUCKET + "/" + KEY)
.then()
.statusCode(204);
consumeMessage(wildcardQueueUrl, "\"eventName\":\"ObjectTagging:Delete\"");
assertQueueEmpty(putQueueUrl);
assertQueueEmpty(createdQueueUrl);
} finally {
given()
.contentType("application/xml")
.queryParam("notification", "")
.body("<NotificationConfiguration xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"/>")
.put("/" + BUCKET);
deleteQueue(putQueueUrl);
deleteQueue(wildcardQueueUrl);
deleteQueue(createdQueueUrl);
}
}
private String createQueue(String queueName) {
return given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateQueue")
.formParam("QueueName", queueName)
.when()
.post("/")
.then()
.statusCode(200)
.extract().xmlPath().getString("CreateQueueResponse.CreateQueueResult.QueueUrl");
}
/** Receives one message, asserts its body, deletes it and returns the body. */
private String consumeMessage(String queueUrl, String expectedBodyPart) {
XmlPath message = given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "ReceiveMessage")
.formParam("QueueUrl", queueUrl)
.formParam("MaxNumberOfMessages", "1")
.formParam("WaitTimeSeconds", "5")
.when()
.post("/")
.then()
.statusCode(200)
.body("ReceiveMessageResponse.ReceiveMessageResult.Message.Body", containsString(expectedBodyPart))
.extract().xmlPath();
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DeleteMessage")
.formParam("QueueUrl", queueUrl)
.formParam("ReceiptHandle",
message.getString("ReceiveMessageResponse.ReceiveMessageResult.Message.ReceiptHandle"))
.when()
.post("/")
.then()
.statusCode(200);
return message.getString("ReceiveMessageResponse.ReceiveMessageResult.Message.Body");
}
private void assertQueueEmpty(String queueUrl) {
String response = given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "ReceiveMessage")
.formParam("QueueUrl", queueUrl)
.formParam("MaxNumberOfMessages", "1")
.when()
.post("/")
.then()
.statusCode(200)
.extract().body().asString();
assertThat(response, not(containsString("<Message>")));
}
private void deleteQueue(String queueUrl) {
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DeleteQueue")
.formParam("QueueUrl", queueUrl)
.when()
.post("/");
}
}