mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -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
@@ -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()) {
|
||||
|
||||
+62
-2
@@ -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<String, Object> 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<String, Object> 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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user