mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(eventbridge): add TestEventPattern action (#824)
* 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": <bool>} 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
This commit is contained in:
1 parent
944f82a6a4
commit
e89777b8a2
4 files changed
+375
-2
No files matched your search
+302
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user