From e89777b8a26517906869e105792bc83b11e92f63 Mon Sep 17 00:00:00 2001 From: Marvelous Ikponmwosa <46595957+marvikomo@users.noreply.github.com> Date: Wed, 13 May 2026 02:15:56 +0100 Subject: [PATCH] feat(eventbridge): add TestEventPattern action (#824) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(eventbridge): add TestEventPattern action Exposes the existing rule pattern matcher as a public API so callers can test whether a sample event would match an event pattern without firing any targets. Adapts the AWS event envelope (lowercase source / detail-type / detail / resources) to the internal entry shape and delegates to the same matchesPattern() helper that powers PutEvents rule dispatch. Returns {"Result": } on success, InvalidEventPatternException (400) on malformed pattern or missing event. * fix(eventbridge): reject empty EventPattern in TestEventPattern AWS marks EventPattern as a required field for TestEventPattern. The previous implementation accepted null/empty values and silently delegated to matchesPattern(), which returns true for blank patterns — turning the API into an unintended match-all. matchesPattern()'s blank-is-match-all behavior is intentional for rule dispatch (rules with no EventPattern are scheduled rules that fire on every match attempt), but exposing it here violates the spec. Now throws InvalidEventPatternException (400) when EventPattern is null or blank. Verified end-to-end via boto3: the typed exception is raised client-side. The integration test that asserted the wrong behavior is flipped, and a new test covers the missing-field case. * fix(eventbridge): honor account/region from event and reject non-object Events --- docs/services/eventbridge.md | 1 + .../eventbridge/EventBridgeHandler.java | 10 + .../eventbridge/EventBridgeService.java | 64 +++- ...BridgeTestEventPatternIntegrationTest.java | 302 ++++++++++++++++++ 4 files changed, 375 insertions(+), 2 deletions(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeTestEventPatternIntegrationTest.java diff --git a/docs/services/eventbridge.md b/docs/services/eventbridge.md index f301d4d377..5df4874303 100644 --- a/docs/services/eventbridge.md +++ b/docs/services/eventbridge.md @@ -21,6 +21,7 @@ | `RemoveTargets` | Remove targets from a rule | | `ListTargetsByRule` | List targets for a rule | | `PutEvents` | Publish custom events to an event bus | +| `TestEventPattern` | Test whether a sample event matches a given pattern (no targets fired) | ## Configuration diff --git a/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeHandler.java b/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeHandler.java index c80f72a69c..a3e10f78f6 100644 --- a/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeHandler.java @@ -64,6 +64,7 @@ public class EventBridgeHandler { case "RemoveTargets" -> handleRemoveTargets(request, region); case "ListTargetsByRule" -> handleListTargetsByRule(request, region); case "PutEvents" -> handlePutEvents(request, region); + case "TestEventPattern" -> handleTestEventPattern(request); case "ListTagsForResource" -> handleListTagsForResource(request, region); case "TagResource" -> handleTagResource(request, region); case "UntagResource" -> handleUntagResource(request, region); @@ -315,6 +316,15 @@ public class EventBridgeHandler { return Response.ok(response).build(); } + private Response handleTestEventPattern(JsonNode request) { + String eventPattern = request.path("EventPattern").asText(null); + String event = request.path("Event").asText(null); + boolean result = eventBridgeService.testEventPattern(eventPattern, event); + ObjectNode response = objectMapper.createObjectNode(); + response.put("Result", result); + return Response.ok(response).build(); + } + private Response handleListTagsForResource(JsonNode request, String region) { String resourceArn = request.path("ResourceARN").asText(null); if (resourceArn == null || resourceArn.isBlank()) { diff --git a/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeService.java b/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeService.java index 48940fba1e..30973c6733 100644 --- a/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeService.java +++ b/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeService.java @@ -628,6 +628,60 @@ public class EventBridgeService { // ──────────────────────────── Pattern Matching ──────────────────────────── + /** + * Tests whether a sample event matches a given event pattern, without firing any + * targets. Mirrors the AWS {@code TestEventPattern} API: callers pass the full event + * envelope (lowercase {@code source} / {@code detail-type} / {@code detail} / + * {@code resources}) as JSON; we adapt it to the internal entry shape and delegate + * to {@link #matchesPattern(Map, String)}. + */ + public boolean testEventPattern(String eventPattern, String eventJson) { + if (eventPattern == null || eventPattern.isBlank()) { + throw new AwsException("InvalidEventPatternException", "EventPattern is required.", 400); + } + if (eventJson == null || eventJson.isBlank()) { + throw new AwsException("InvalidEventPatternException", "Event is required.", 400); + } + try { + objectMapper.readTree(eventPattern); + } catch (Exception e) { + throw new AwsException("InvalidEventPatternException", + "Event pattern is not valid JSON: " + e.getMessage(), 400); + } + JsonNode event; + try { + event = objectMapper.readTree(eventJson); + } catch (Exception e) { + throw new AwsException("InvalidEventPatternException", + "Event is not valid JSON: " + e.getMessage(), 400); + } + if (event == null || !event.isObject()) { + throw new AwsException("InvalidEventPatternException", + "Event must be a JSON object.", 400); + } + Map entry = new HashMap<>(); + if (event.hasNonNull("source")) { + entry.put("Source", event.get("source").asText()); + } + if (event.hasNonNull("detail-type")) { + entry.put("DetailType", event.get("detail-type").asText()); + } + if (event.hasNonNull("detail")) { + JsonNode detail = event.get("detail"); + entry.put("Detail", detail.isTextual() ? detail.asText() : detail.toString()); + } + if (event.hasNonNull("resources") && event.get("resources").isArray()) { + entry.put("Resources", event.get("resources")); + } + if (event.hasNonNull("account")) { + entry.put("Account", event.get("account").asText()); + } + if (event.hasNonNull("region")) { + entry.put("Region", event.get("region").asText()); + } + return matchesPattern(entry, eventPattern); + } + boolean matchesPattern(Map event, String eventPattern) { if (eventPattern == null || eventPattern.isBlank()) { return true; @@ -650,14 +704,20 @@ public class EventBridgeService { } JsonNode accountField = pattern.get("account"); if (accountField != null && accountField.isArray()) { - String eventAccount = regionResolver.getAccountId(); + // Prefer the entry's Account (set by TestEventPattern from the + // supplied event envelope); fall back to the caller's account + // for PutEvents rule dispatch, where the event always belongs + // to the caller. + String eventAccount = (String) event.getOrDefault( + "Account", regionResolver.getAccountId()); if (!matchesArrayField(accountField, eventAccount)) { return false; } } JsonNode regionField = pattern.get("region"); if (regionField != null && regionField.isArray()) { - String eventRegion = regionResolver.getDefaultRegion(); + String eventRegion = (String) event.getOrDefault( + "Region", regionResolver.getDefaultRegion()); if (!matchesArrayField(regionField, eventRegion)) { return false; } diff --git a/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeTestEventPatternIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeTestEventPatternIntegrationTest.java new file mode 100644 index 0000000000..b4277b2ac9 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeTestEventPatternIntegrationTest.java @@ -0,0 +1,302 @@ +package io.github.hectorvent.floci.services.eventbridge; + +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; + +@QuarkusTest +class EventBridgeTestEventPatternIntegrationTest { + + private static final String EVENT_BRIDGE_CONTENT_TYPE = "application/x-amz-json-1.1"; + private static final String TARGET = "AWSEvents.TestEventPattern"; + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + void sourceMatch() { + given() + .contentType(EVENT_BRIDGE_CONTENT_TYPE) + .header("X-Amz-Target", TARGET) + .body(""" + { + "EventPattern": "{\\"source\\":[\\"com.myapp\\"]}", + "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{}}" + } + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("Result", equalTo(true)); + } + + @Test + void sourceNoMatch() { + given() + .contentType(EVENT_BRIDGE_CONTENT_TYPE) + .header("X-Amz-Target", TARGET) + .body(""" + { + "EventPattern": "{\\"source\\":[\\"com.myapp\\"]}", + "Event": "{\\"source\\":\\"com.otherapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{}}" + } + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("Result", equalTo(false)); + } + + @Test + void detailTypeMatch() { + given() + .contentType(EVENT_BRIDGE_CONTENT_TYPE) + .header("X-Amz-Target", TARGET) + .body(""" + { + "EventPattern": "{\\"source\\":[\\"com.myapp\\"],\\"detail-type\\":[\\"OrderPlaced\\"]}", + "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{}}" + } + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("Result", equalTo(true)); + } + + @Test + void detailNestedMatch() { + given() + .contentType(EVENT_BRIDGE_CONTENT_TYPE) + .header("X-Amz-Target", TARGET) + .body(""" + { + "EventPattern": "{\\"detail\\":{\\"status\\":[\\"PAID\\"]}}", + "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"status\\":\\"PAID\\"}}" + } + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("Result", equalTo(true)); + } + + @Test + void prefixFilterMatch() { + given() + .contentType(EVENT_BRIDGE_CONTENT_TYPE) + .header("X-Amz-Target", TARGET) + .body(""" + { + "EventPattern": "{\\"source\\":[{\\"prefix\\":\\"com.\\"}]}", + "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{}}" + } + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("Result", equalTo(true)); + } + + @Test + void emptyPatternRejected() { + // AWS spec: EventPattern is required; an empty string must be rejected + // rather than treated as a match-all wildcard. + given() + .contentType(EVENT_BRIDGE_CONTENT_TYPE) + .header("X-Amz-Target", TARGET) + .body(""" + { + "EventPattern": "", + "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{}}" + } + """) + .when() + .post("/") + .then() + .statusCode(400); + } + + @Test + void accountMatchFromEventEnvelope() { + given() + .contentType(EVENT_BRIDGE_CONTENT_TYPE) + .header("X-Amz-Target", TARGET) + .body(""" + { + "EventPattern": "{\\"account\\":[\\"999999999999\\"]}", + "Event": "{\\"source\\":\\"x\\",\\"detail-type\\":\\"y\\",\\"account\\":\\"999999999999\\",\\"detail\\":{}}" + } + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("Result", equalTo(true)); + } + + @Test + void accountNoMatchFromEventEnvelope() { + given() + .contentType(EVENT_BRIDGE_CONTENT_TYPE) + .header("X-Amz-Target", TARGET) + .body(""" + { + "EventPattern": "{\\"account\\":[\\"999999999999\\"]}", + "Event": "{\\"source\\":\\"x\\",\\"detail-type\\":\\"y\\",\\"account\\":\\"111111111111\\",\\"detail\\":{}}" + } + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("Result", equalTo(false)); + } + + @Test + void regionMatchFromEventEnvelope() { + given() + .contentType(EVENT_BRIDGE_CONTENT_TYPE) + .header("X-Amz-Target", TARGET) + .body(""" + { + "EventPattern": "{\\"region\\":[\\"eu-west-1\\"]}", + "Event": "{\\"source\\":\\"x\\",\\"detail-type\\":\\"y\\",\\"region\\":\\"eu-west-1\\",\\"detail\\":{}}" + } + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("Result", equalTo(true)); + } + + @Test + void regionNoMatchFromEventEnvelope() { + given() + .contentType(EVENT_BRIDGE_CONTENT_TYPE) + .header("X-Amz-Target", TARGET) + .body(""" + { + "EventPattern": "{\\"region\\":[\\"eu-west-1\\"]}", + "Event": "{\\"source\\":\\"x\\",\\"detail-type\\":\\"y\\",\\"region\\":\\"us-east-1\\",\\"detail\\":{}}" + } + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("Result", equalTo(false)); + } + + @Test + void arrayEventRejected() { + given() + .contentType(EVENT_BRIDGE_CONTENT_TYPE) + .header("X-Amz-Target", TARGET) + .body(""" + { + "EventPattern": "{\\"source\\":[\\"com.myapp\\"]}", + "Event": "[]" + } + """) + .when() + .post("/") + .then() + .statusCode(400); + } + + @Test + void scalarEventRejected() { + given() + .contentType(EVENT_BRIDGE_CONTENT_TYPE) + .header("X-Amz-Target", TARGET) + .body(""" + { + "EventPattern": "{\\"source\\":[\\"com.myapp\\"]}", + "Event": "true" + } + """) + .when() + .post("/") + .then() + .statusCode(400); + } + + @Test + void nullLiteralEventRejected() { + given() + .contentType(EVENT_BRIDGE_CONTENT_TYPE) + .header("X-Amz-Target", TARGET) + .body(""" + { + "EventPattern": "{\\"source\\":[\\"com.myapp\\"]}", + "Event": "null" + } + """) + .when() + .post("/") + .then() + .statusCode(400); + } + + @Test + void missingPatternRejected() { + given() + .contentType(EVENT_BRIDGE_CONTENT_TYPE) + .header("X-Amz-Target", TARGET) + .body(""" + { + "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{}}" + } + """) + .when() + .post("/") + .then() + .statusCode(400); + } + + @Test + void malformedPatternRejected() { + given() + .contentType(EVENT_BRIDGE_CONTENT_TYPE) + .header("X-Amz-Target", TARGET) + .body(""" + { + "EventPattern": "{not-json}", + "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{}}" + } + """) + .when() + .post("/") + .then() + .statusCode(400); + } + + @Test + void missingEventRejected() { + given() + .contentType(EVENT_BRIDGE_CONTENT_TYPE) + .header("X-Amz-Target", TARGET) + .body(""" + { + "EventPattern": "{\\"source\\":[\\"com.myapp\\"]}" + } + """) + .when() + .post("/") + .then() + .statusCode(400); + } +}