Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(eventbridge): boolean/numeric pattern values are matched correctly (#1889)

Usman Zaheer517094adc13d
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 7e375bdc7b..60dd8f1561 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
@@ -24,6 +24,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.fasterxml.jackson.databind.node.TextNode;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@@ -883,8 +884,7 @@ public class EventBridgeService {
JsonNode expected = field.getValue();
JsonNode actualField = actual.get(field.getKey());
if (expected.isArray()) {
- String actualStr = actualField != null ? actualField.asText(null) : null;
- if (!matchesArrayField(expected, actualStr)) {
+ if (!matchesArrayField(expected, actualField)) {
return false;
}
} else if (expected.isObject()) {
@@ -908,25 +908,23 @@ public class EventBridgeService {
}
private boolean matchesArrayField(JsonNode arrayNode, String value) {
+ JsonNode actual = value != null ? TextNode.valueOf(value) : null;
+ return matchesArrayField(arrayNode, actual);
+ }
+
+ private boolean matchesArrayField(JsonNode arrayNode, JsonNode actual) {
for (JsonNode element : arrayNode) {
- if (matchesSingleElement(element, value)) {
+ if (matchesSingleElement(element, actual)) {
return true;
}
}
return false;
}
- private boolean matchesSingleElement(JsonNode element, String value) {
- // Exact string match
- if (element.isTextual()) {
- return value != null && value.equals(element.asText());
- }
- // Null literal match
- if (element.isNull()) {
- return value == null;
- }
+ private boolean matchesSingleElement(JsonNode element, JsonNode actual) {
// Content filter object
if (element.isObject()) {
+ String value = actual != null && actual.isTextual() ? actual.asText() : null;
if (element.has("prefix")) {
return value != null && value.startsWith(element.get("prefix").asText());
}
@@ -938,11 +936,16 @@ public class EventBridgeService {
}
if (element.has("anything-but")) {
JsonNode anythingBut = element.get("anything-but");
+ if (anythingBut.isValueNode()) {
+ return actual != null && !actual.isNull() && !matchesExactValue(anythingBut, actual);
+ }
if (anythingBut.isArray()) {
for (JsonNode v : anythingBut) {
- if (v.isTextual() && v.asText().equals(value)) return false;
+ if (matchesExactValue(v, actual)) {
+ return false;
+ }
}
- return value != null;
+ return actual != null && !actual.isNull();
}
if (anythingBut.isObject() && anythingBut.has("prefix")) {
return value != null && !value.startsWith(anythingBut.get("prefix").asText());
@@ -950,8 +953,32 @@ public class EventBridgeService {
}
if (element.has("exists")) {
boolean shouldExist = element.get("exists").asBoolean();
- return shouldExist ? (value != null) : (value == null);
+ boolean present = actual != null;
+ return shouldExist == present;
}
+ return false;
+ }
+ // Exact literal match (string, null, boolean, number)
+ return matchesExactValue(element, actual);
+ }
+
+ private boolean matchesExactValue(JsonNode expected, JsonNode actual) {
+ if (expected.isNull()) {
+ return actual != null && actual.isNull();
+ }
+ if (actual == null) {
+ return false;
+ }
+ if (expected.isTextual()) {
+ return actual.isTextual() && expected.asText().equals(actual.asText());
+ }
+ if (expected.isBoolean()) {
+ return actual.isBoolean() && expected.booleanValue() == actual.booleanValue();
+ }
+ if (expected.isNumber()) {
+ // AWS normalizes numbers before comparing: 300, 300.0 and 3.0e2
+ // are all considered equal.
+ return actual.isNumber() && expected.decimalValue().compareTo(actual.decimalValue()) == 0;
}
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
index b4277b2ac9..7338e1bacc 100644
--- a/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeTestEventPatternIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeTestEventPatternIntegrationTest.java
@@ -91,6 +91,424 @@ class EventBridgeTestEventPatternIntegrationTest {
.body("Result", equalTo(true));
}
+ @Test
+ void detailBooleanLiteralMatch() {
+ given()
+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET)
+ .body("""
+ {
+ "EventPattern": "{\\"detail\\":{\\"enabled\\":[true]}}",
+ "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"enabled\\":true}}"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Result", equalTo(true));
+ }
+
+ @Test
+ void detailBooleanLiteralNoMatchOnDifferentValue() {
+ given()
+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET)
+ .body("""
+ {
+ "EventPattern": "{\\"detail\\":{\\"enabled\\":[true]}}",
+ "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"enabled\\":false}}"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Result", equalTo(false));
+ }
+
+ @Test
+ void detailStringPatternDoesNotMatchBooleanValue() {
+ // Pattern ["true"] must not match the JSON boolean true (type-strict, as in AWS)
+ given()
+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET)
+ .body("""
+ {
+ "EventPattern": "{\\"detail\\":{\\"enabled\\":[\\"true\\"]}}",
+ "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"enabled\\":true}}"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Result", equalTo(false));
+ }
+
+ @Test
+ void detailBooleanPatternDoesNotMatchStringValue() {
+ given()
+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET)
+ .body("""
+ {
+ "EventPattern": "{\\"detail\\":{\\"enabled\\":[true]}}",
+ "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"enabled\\":\\"true\\"}}"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Result", equalTo(false));
+ }
+
+ @Test
+ void detailNumberLiteralMatch() {
+ given()
+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET)
+ .body("""
+ {
+ "EventPattern": "{\\"detail\\":{\\"count\\":[5]}}",
+ "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"count\\":5}}"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Result", equalTo(true));
+ }
+
+ @Test
+ void detailStringPatternDoesNotMatchNumberValue() {
+ // Pattern ["5"] must not match the JSON number 5 (type-strict, as in AWS)
+ given()
+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET)
+ .body("""
+ {
+ "EventPattern": "{\\"detail\\":{\\"count\\":[\\"5\\"]}}",
+ "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"count\\":5}}"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Result", equalTo(false));
+ }
+
+ @Test
+ void detailNumberPatternDoesNotMatchStringValue() {
+ given()
+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET)
+ .body("""
+ {
+ "EventPattern": "{\\"detail\\":{\\"count\\":[5]}}",
+ "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"count\\":\\"5\\"}}"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Result", equalTo(false));
+ }
+
+ @Test
+ void detailNumberMatchIsByNumericValue() {
+ // AWS normalizes numbers before comparing: 300 and 300.0 are equal.
+ given()
+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET)
+ .body("""
+ {
+ "EventPattern": "{\\"detail\\":{\\"count\\":[300]}}",
+ "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"count\\":300.0}}"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Result", equalTo(true));
+ }
+
+ @Test
+ void detailNumberScientificNotationMatchesDecimal() {
+ given()
+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET)
+ .body("""
+ {
+ "EventPattern": "{\\"detail\\":{\\"count\\":[3.0e2]}}",
+ "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"count\\":300.0}}"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Result", equalTo(true));
+ }
+
+ @Test
+ void detailExistsMatchesBooleanValue() {
+ given()
+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET)
+ .body("""
+ {
+ "EventPattern": "{\\"detail\\":{\\"enabled\\":[{\\"exists\\":true}]}}",
+ "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"enabled\\":false}}"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Result", equalTo(true));
+ }
+
+ @Test
+ void detailNullLiteralMatchesNullValue() {
+ given()
+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET)
+ .body("""
+ {
+ "EventPattern": "{\\"detail\\":{\\"field\\":[null]}}",
+ "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"field\\":null}}"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Result", equalTo(true));
+ }
+
+ @Test
+ void detailNullLiteralDoesNotMatchMissingField() {
+ given()
+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET)
+ .body("""
+ {
+ "EventPattern": "{\\"detail\\":{\\"field\\":[null]}}",
+ "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"other\\":1}}"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Result", equalTo(false));
+ }
+
+ @Test
+ void detailExistsTrueMatchesNullValue() {
+ // A key carrying the JSON literal null still exists in AWS.
+ given()
+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET)
+ .body("""
+ {
+ "EventPattern": "{\\"detail\\":{\\"field\\":[{\\"exists\\":true}]}}",
+ "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"field\\":null}}"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Result", equalTo(true));
+ }
+
+ @Test
+ void detailExistsFalseDoesNotMatchNullValue() {
+ given()
+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET)
+ .body("""
+ {
+ "EventPattern": "{\\"detail\\":{\\"field\\":[{\\"exists\\":false}]}}",
+ "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"field\\":null}}"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Result", equalTo(false));
+ }
+
+ @Test
+ void detailExistsFalseMatchesMissingField() {
+ given()
+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET)
+ .body("""
+ {
+ "EventPattern": "{\\"detail\\":{\\"field\\":[{\\"exists\\":false}]}}",
+ "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"other\\":1}}"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Result", equalTo(true));
+ }
+
+ @Test
+ void detailExistsTrueDoesNotMatchMissingField() {
+ given()
+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET)
+ .body("""
+ {
+ "EventPattern": "{\\"detail\\":{\\"field\\":[{\\"exists\\":true}]}}",
+ "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"other\\":1}}"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Result", equalTo(false));
+ }
+
+ @Test
+ void detailAnythingButNumberValue() {
+ given()
+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET)
+ .body("""
+ {
+ "EventPattern": "{\\"detail\\":{\\"count\\":[{\\"anything-but\\":[3]}]}}",
+ "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"count\\":5}}"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Result", equalTo(true));
+ }
+
+ @Test
+ void detailAnythingButNumberValueExcluded() {
+ given()
+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET)
+ .body("""
+ {
+ "EventPattern": "{\\"detail\\":{\\"count\\":[{\\"anything-but\\":[5]}]}}",
+ "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"count\\":5}}"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Result", equalTo(false));
+ }
+
+ @Test
+ void detailAnythingButScalarStringValue() {
+ given()
+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET)
+ .body("""
+ {
+ "EventPattern": "{\\"detail\\":{\\"state\\":[{\\"anything-but\\":\\"initializing\\"}]}}",
+ "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"state\\":\\"running\\"}}"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Result", equalTo(true));
+ }
+
+ @Test
+ void detailAnythingButScalarStringValueExcluded() {
+ given()
+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET)
+ .body("""
+ {
+ "EventPattern": "{\\"detail\\":{\\"state\\":[{\\"anything-but\\":\\"initializing\\"}]}}",
+ "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"state\\":\\"initializing\\"}}"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Result", equalTo(false));
+ }
+
+ @Test
+ void detailAnythingButScalarNumberValue() {
+ given()
+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET)
+ .body("""
+ {
+ "EventPattern": "{\\"detail\\":{\\"count\\":[{\\"anything-but\\":5}]}}",
+ "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"count\\":3}}"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Result", equalTo(true));
+ }
+
+ @Test
+ void detailAnythingButScalarNumberValueExcluded() {
+ given()
+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET)
+ .body("""
+ {
+ "EventPattern": "{\\"detail\\":{\\"count\\":[{\\"anything-but\\":5}]}}",
+ "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"count\\":5}}"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Result", equalTo(false));
+ }
+
+ @Test
+ void detailAnythingButScalarMissingFieldDoesNotMatch() {
+ given()
+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET)
+ .body("""
+ {
+ "EventPattern": "{\\"detail\\":{\\"state\\":[{\\"anything-but\\":\\"initializing\\"}]}}",
+ "Event": "{\\"source\\":\\"com.myapp\\",\\"detail-type\\":\\"OrderPlaced\\",\\"detail\\":{\\"other\\":1}}"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Result", equalTo(false));
+ }
+
@Test
void prefixFilterMatch() {
given()