mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(eventbridge): match exists only on leaf values (#9372)
This commit is contained in:
1 parent
a3a6cef531
commit
54961f0519
3 files changed
+173
-9
No files matched your search
@@ -1419,6 +1419,9 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
|
||||
private boolean matchesNestedDetail(JsonNode actual, JsonNode pattern) {
|
||||
if (actual.isArray()) {
|
||||
if (actual.isEmpty()) {
|
||||
return matchesDetailNode(JsonNodeFactory.instance.nullNode(), pattern);
|
||||
}
|
||||
for (JsonNode element : actual) {
|
||||
if (matchesDetailNode(element, pattern)) {
|
||||
return true;
|
||||
@@ -1430,6 +1433,9 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
}
|
||||
|
||||
private boolean matchesDetailNode(JsonNode actual, JsonNode pattern) {
|
||||
if (pattern.isEmpty()) {
|
||||
return actual != null && actual.isObject();
|
||||
}
|
||||
JsonNode orClauses = pattern.get("$or");
|
||||
if (orClauses != null && orClauses.isArray()) {
|
||||
boolean anyMatches = false;
|
||||
@@ -1458,15 +1464,13 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
return false;
|
||||
}
|
||||
} else if (expected.isObject()) {
|
||||
if (actualField == null || actualField.isNull()) {
|
||||
return false;
|
||||
}
|
||||
JsonNode nestedActual = actualField;
|
||||
if (actualField.isTextual()) {
|
||||
JsonNode nestedActual = actualField == null
|
||||
? JsonNodeFactory.instance.nullNode() : actualField;
|
||||
if (nestedActual.isTextual()) {
|
||||
try {
|
||||
nestedActual = objectMapper.readTree(actualField.asText());
|
||||
nestedActual = objectMapper.readTree(nestedActual.asText());
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
nestedActual = JsonNodeFactory.instance.nullNode();
|
||||
}
|
||||
}
|
||||
if (!matchesNestedDetail(nestedActual, expected)) {
|
||||
@@ -1483,15 +1487,30 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
}
|
||||
|
||||
private boolean matchesArrayField(JsonNode arrayNode, JsonNode actual) {
|
||||
return matchesArrayField(arrayNode, actual, true);
|
||||
}
|
||||
|
||||
private boolean matchesArrayField(JsonNode arrayNode, JsonNode actual, boolean checkExists) {
|
||||
if (actual != null && actual.isArray()) {
|
||||
if (checkExists) {
|
||||
for (JsonNode element : arrayNode) {
|
||||
if (element.isObject() && element.has("exists")
|
||||
&& matchesSingleElement(element, actual)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (JsonNode value : actual) {
|
||||
if (matchesArrayField(arrayNode, value)) {
|
||||
if (matchesArrayField(arrayNode, value, false)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
for (JsonNode element : arrayNode) {
|
||||
if (!checkExists && element.isObject() && element.has("exists")) {
|
||||
continue;
|
||||
}
|
||||
if (matchesSingleElement(element, actual)) {
|
||||
return true;
|
||||
}
|
||||
@@ -1544,7 +1563,7 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
}
|
||||
if (element.has("exists")) {
|
||||
boolean shouldExist = element.get("exists").asBoolean();
|
||||
boolean present = actual != null;
|
||||
boolean present = containsLeafValue(actual);
|
||||
return shouldExist == present;
|
||||
}
|
||||
return false;
|
||||
@@ -1553,6 +1572,21 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
return matchesExactValue(element, actual);
|
||||
}
|
||||
|
||||
private boolean containsLeafValue(JsonNode actual) {
|
||||
if (actual == null || actual.isObject()) {
|
||||
return false;
|
||||
}
|
||||
if (actual.isArray()) {
|
||||
for (JsonNode element : actual) {
|
||||
if (containsLeafValue(element)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return actual.isValueNode();
|
||||
}
|
||||
|
||||
private boolean isValidNumericFilter(JsonNode comparisons) {
|
||||
if (!comparisons.isArray() || (comparisons.size() != 2 && comparisons.size() != 4)) {
|
||||
return false;
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package io.floci.aws.services.eventbridge;
|
||||
|
||||
import io.floci.aws.testing.AwsTestClients;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.eventbridge.EventBridgeClient;
|
||||
import software.amazon.awssdk.services.sqs.SqsClient;
|
||||
import software.amazon.awssdk.services.sqs.model.QueueAttributeName;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
class EventBridgeExistsIntegrationTest {
|
||||
|
||||
private static final String ACCOUNT = "725018800073";
|
||||
private static final String PRESENT_PATTERN = "{\"detail\":{\"status\":[{\"exists\":true}]}}";
|
||||
private static final String ABSENT_PATTERN = "{\"detail\":{\"status\":[{\"exists\":false}]}}";
|
||||
private static final String OBJECT_EVENT = """
|
||||
{"version":"0","id":"exists-leaf","detail-type":"Example","source":"example.test",
|
||||
"account":"725018800073","time":"2026-11-24T00:00:00Z","region":"us-east-1",
|
||||
"resources":[],"detail":{"status":{"nested":"v"}}}
|
||||
""";
|
||||
|
||||
@Test
|
||||
void objectFieldMatchesOnlyAbsentRuleThroughSdk() {
|
||||
try (EventBridgeClient events = AwsTestClients.create(EventBridgeClient.builder(), ACCOUNT, Region.US_EAST_1);
|
||||
SqsClient sqs = AwsTestClients.create(SqsClient.builder(), ACCOUNT, Region.US_EAST_1)) {
|
||||
assertFalse(events.testEventPattern(request -> request
|
||||
.eventPattern(PRESENT_PATTERN).event(OBJECT_EVENT)).result());
|
||||
assertTrue(events.testEventPattern(request -> request
|
||||
.eventPattern(ABSENT_PATTERN).event(OBJECT_EVENT)).result());
|
||||
|
||||
String name = "exists-leaf-" + UUID.randomUUID();
|
||||
String queueUrl = sqs.createQueue(request -> request.queueName(name)).queueUrl();
|
||||
boolean ruleCreated = false;
|
||||
try {
|
||||
String queueArn = sqs.getQueueAttributes(request -> request.queueUrl(queueUrl)
|
||||
.attributeNames(QueueAttributeName.QUEUE_ARN)).attributes().get(QueueAttributeName.QUEUE_ARN);
|
||||
events.putRule(request -> request.name(name).eventPattern(PRESENT_PATTERN));
|
||||
ruleCreated = true;
|
||||
events.putTargets(request -> request.rule(name).targets(target -> target.id("q").arn(queueArn)));
|
||||
events.putEvents(request -> request.entries(entry -> entry
|
||||
.source("example.test").detailType("Example")
|
||||
.detail("{\"status\":{\"nested\":\"v\"}}")));
|
||||
assertTrue(sqs.receiveMessage(request -> request.queueUrl(queueUrl)
|
||||
.waitTimeSeconds(1)).messages().isEmpty());
|
||||
|
||||
events.putRule(request -> request.name(name).eventPattern(ABSENT_PATTERN));
|
||||
events.putEvents(request -> request.entries(entry -> entry
|
||||
.source("example.test").detailType("Example")
|
||||
.detail("{\"status\":{\"nested\":\"v\"}}")));
|
||||
assertEquals(1, sqs.receiveMessage(request -> request.queueUrl(queueUrl)
|
||||
.waitTimeSeconds(5)).messages().size());
|
||||
} finally {
|
||||
try {
|
||||
if (ruleCreated) {
|
||||
events.removeTargets(request -> request.rule(name).ids("q"));
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
if (ruleCreated) {
|
||||
events.deleteRule(request -> request.name(name));
|
||||
}
|
||||
} finally {
|
||||
sqs.deleteQueue(request -> request.queueUrl(queueUrl));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1816,6 +1816,60 @@ class EventBridgeServiceTest {
|
||||
assertTrue(service.matchesPattern(event, "{\"detail\":{\"other\":[{\"exists\":false}]}}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesPatternByExistsTreatsIntermediateObjectAsAbsent() {
|
||||
Map<String, Object> event = Map.of(
|
||||
"Source", "my.app",
|
||||
"Detail", "{\"status\":{\"nested\":\"v\"}}"
|
||||
);
|
||||
assertFalse(service.matchesPattern(event, "{\"detail\":{\"status\":[{\"exists\":true}]}}"));
|
||||
assertTrue(service.matchesPattern(event, "{\"detail\":{\"status\":[{\"exists\":false}]}}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesPatternByExistsChecksEntireArrayForLeafValues() {
|
||||
String presentPattern = "{\"detail\":{\"status\":[{\"exists\":true}]}}";
|
||||
String absentPattern = "{\"detail\":{\"status\":[{\"exists\":false}]}}";
|
||||
for (String value : List.of("{}", "[]", "[{}]", "[[]]", "[[{}]]")) {
|
||||
Map<String, Object> event = Map.of("Detail", "{\"status\":" + value + "}");
|
||||
assertFalse(service.matchesPattern(event, presentPattern), value);
|
||||
assertTrue(service.matchesPattern(event, absentPattern), value);
|
||||
}
|
||||
for (String value : List.of("null", "\"ok\"", "[{},[1]]")) {
|
||||
Map<String, Object> event = Map.of("Detail", "{\"status\":" + value + "}");
|
||||
assertTrue(service.matchesPattern(event, presentPattern), value);
|
||||
assertFalse(service.matchesPattern(event, absentPattern), value);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesPatternByExistsTreatsMissingParentAsAbsent() {
|
||||
String pattern = "{\"detail\":{\"parent\":{\"child\":[{\"exists\":false}]}}}";
|
||||
for (String detail : List.of("{}", "{\"parent\":null}", "{\"parent\":\"plain\"}")) {
|
||||
assertTrue(service.matchesPattern(Map.of("Detail", detail), pattern), detail);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesPatternByEmptyNestedDetailRequiresObject() {
|
||||
String pattern = "{\"detail\":{\"parent\":{}}}";
|
||||
for (String detail : List.of("{}", "{\"parent\":null}", "{\"parent\":[]}",
|
||||
"{\"parent\":\"plain\"}")) {
|
||||
assertFalse(service.matchesPattern(Map.of("Detail", detail), pattern), detail);
|
||||
}
|
||||
for (String detail : List.of("{\"parent\":{}}", "{\"parent\":[{}]}")) {
|
||||
assertTrue(service.matchesPattern(Map.of("Detail", detail), pattern), detail);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesPatternByEmptyOrBranchDoesNotMatchMissingParent() {
|
||||
Map<String, Object> event = Map.of("Detail", "{}");
|
||||
assertFalse(service.matchesPattern(event, "{\"detail\":{\"parent\":{\"$or\":[{}]}}}"));
|
||||
assertTrue(service.matchesPattern(event,
|
||||
"{\"detail\":{\"parent\":{\"$or\":[{},{\"child\":[{\"exists\":false}]}]}}}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesPatternByAccount_matches() {
|
||||
Map<String, Object> event = Map.of("Source", "my.app", "DetailType", "Order");
|
||||
|
||||
Reference in new issue
Block a user