Light, fluffy, and always free - The AWS Local Emulator alternative
fix: parse S3 EventBridge notification XML (#3350)
Niels Kootstraad9a88cf7e56
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.javaindex 8b2bb7cbb5..ef6c60ab50 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java@@ -1779,7 +1779,7 @@ public class S3Controller {new LambdaNotification(parsed.id, parsed.arn, parsed.events, parsed.filterRules));}- config.setEventBridgeEnabled(xml.contains("<EventBridgeConfiguration"));+ config.setEventBridgeEnabled(parseEventBridgeConfiguration(xml));s3Service.putBucketNotificationConfiguration(bucket, config);return Response.ok().build();@@ -1788,6 +1788,20 @@ public class S3Controller {}}+ private static boolean parseEventBridgeConfiguration(String xml) {+ if (!"NotificationConfiguration".equals(XmlParser.rootElementName(xml))) {+ throw new AwsException("MalformedXML", "The XML you provided was not well-formed.", 400);+ }+ List<String> children = XmlParser.childElementNames(xml, "NotificationConfiguration");+ long eventBridgeConfigurations = children.stream()+ .filter("EventBridgeConfiguration"::equals)+ .count();+ if (eventBridgeConfigurations > 1) {+ throw new AwsException("MalformedXML", "The XML you provided was not well-formed.", 400);+ }+ return eventBridgeConfigurations == 1;+ }+private record ParsedNotificationGroup(String id, String arn, List<String> events,List<FilterRule> filterRules) {}diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3EventBridgeNotificationValidationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3EventBridgeNotificationValidationIntegrationTest.javanew file mode 100644index 0000000000..d1d5b74ff8--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3EventBridgeNotificationValidationIntegrationTest.java@@ -0,0 +1,116 @@+package io.github.hectorvent.floci.services.s3;++import io.quarkus.test.junit.QuarkusTest;+import org.junit.jupiter.api.Test;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.containsString;+import static org.hamcrest.Matchers.not;++@QuarkusTest+class S3EventBridgeNotificationValidationIntegrationTest {++ @Test+ void namespacedEventBridgeConfigurationIsAccepted() {+ String bucket = createBucket("namespaced");++ given()+ .contentType("application/xml")+ .body("""+ <s3:NotificationConfiguration xmlns:s3="http://s3.amazonaws.com/doc/2006-03-01/">+ <s3:EventBridgeConfiguration/>+ </s3:NotificationConfiguration>+ """)+ .when()+ .put("/" + bucket + "?notification")+ .then()+ .statusCode(200);++ given()+ .when()+ .get("/" + bucket + "?notification")+ .then()+ .statusCode(200)+ .body(containsString("<EventBridgeConfiguration"));+ }++ @Test+ void malformedNotificationConfigurationIsRejected() {+ String bucket = createBucket("malformed");++ putNotification(bucket, "<NotificationConfiguration><EventBridgeConfiguration></NotificationConfiguration>")+ .statusCode(400)+ .body(containsString("<Code>MalformedXML</Code>"));+ }++ @Test+ void eventBridgeTextInCommentDoesNotEnableEventBridge() {+ String bucket = createBucket("comment");++ putNotification(bucket, """+ <NotificationConfiguration>+ <!-- <EventBridgeConfiguration/> -->+ </NotificationConfiguration>+ """).statusCode(200);++ getNotification(bucket)+ .statusCode(200)+ .body(not(containsString("<EventBridgeConfiguration")));+ }++ @Test+ void wrongNotificationConfigurationRootIsRejected() {+ String bucket = createBucket("wrong-root");++ putNotification(bucket, "<OtherConfiguration><EventBridgeConfiguration/></OtherConfiguration>")+ .statusCode(400)+ .body(containsString("<Code>MalformedXML</Code>"));+ }++ @Test+ void duplicateEventBridgeConfigurationsAreRejected() {+ String bucket = createBucket("duplicate");++ putNotification(bucket, """+ <NotificationConfiguration>+ <EventBridgeConfiguration/>+ <EventBridgeConfiguration/>+ </NotificationConfiguration>+ """)+ .statusCode(400)+ .body(containsString("<Code>MalformedXML</Code>"));+ }++ @Test+ void emptyNotificationConfigurationDisablesEventBridge() {+ String bucket = createBucket("empty");++ putNotification(bucket, "<NotificationConfiguration/>").statusCode(200);++ getNotification(bucket)+ .statusCode(200)+ .body(not(containsString("<EventBridgeConfiguration")));+ }++ private static String createBucket(String suffix) {+ String bucket = "eventbridge-validation-" + suffix;+ given().when().put("/" + bucket).then().statusCode(200);+ return bucket;+ }++ private static io.restassured.response.ValidatableResponse putNotification(String bucket, String body) {+ return given()+ .contentType("application/xml")+ .body(body)+ .when()+ .put("/" + bucket + "?notification")+ .then();+ }++ private static io.restassured.response.ValidatableResponse getNotification(String bucket) {+ return given()+ .when()+ .get("/" + bucket + "?notification")+ .then();+ }+}