mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(eventbridge): propagate PutEvents call region and account through pattern matching and delivered envelope (#1125)
* fix(eventbridge): propagate PutEvents call region and account through pattern matching and delivered envelope EventBridgeService.putEvents previously had two related defects in how the call's region and account flowed through to rule matching and the delivered EventBridge envelope: 1. buildEventEnvelope hardcoded the envelope's "region" and "account" fields to RegionResolver.getDefaultRegion() / RegionResolver.getAccountId(), regardless of which region/account the PutEvents call was made against. A rule scoped to eu-west-1 would fire correctly for an eu-west-1 PutEvents call (rules are stored per (region, bus)), but the envelope delivered to its target would report region=us-east-1. Downstream consumers / re-routing rules that filter on the envelope region saw an incorrect value. 2. matchesPattern read entry.Region / entry.Account with a fallback to the RegionResolver defaults. For a PutEvents entry without explicit Region (the common case — the AWS SDK doesn't model per-entry region), the pattern's region/account filter saw the resolver default while the envelope (after fix #1) saw the actual call region. Same kind of inconsistency, one step earlier. - putEvents() now defensively copies each entry into a mutable HashMap and fills missing/blank Region / Account from the call context (region argument; accountId if non-null else regionResolver.getAccountId()) at the top of the per-entry loop. matchesPattern, buildEventEnvelope, and captureToArchives all subsequently see the same normalized entry, so a pattern's region/account filter agrees with the delivered envelope. - buildEventEnvelope now takes the PutEvents call's region and accountId and resolves region/account with the same precedence: entry-supplied value wins (cross-region producers, archive replay), then the call's region/accountId, finally the resolver default. For the putEvents path the entry is already normalized, so this is defense-in-depth for any other callers. - A small isBlank(Object) helper centralizes the null + blank-string predicate used in both the entry normalization and the envelope fallback. Tests - Five new EventBridgeServiceTest cases pin the new behavior: * envelope.region equals the PutEvents call's region for a non-default-region rule+target+entry triple (eu-west-1). * entry.Region / entry.Account win when set explicitly (mirrors the archive-replay scenario where the originating envelope's region/account are stamped back onto the re-emitted entry). * default-region call still stamps the resolver default — regression pin so the pre-fix behavior isn't silently restored. * a region-filtered rule for the call region fires on a PutEvents call to that region with no explicit entry.Region (matchesPattern + envelope agree). * a region-filtered rule for the resolver default does NOT fire under a non-default-region PutEvents call — pre-fix this accidentally matched via the resolver default. - All 173 EventBridge tests pass. * fix(eventbridge): default to RegionResolver when normalizing a missing PutEvents region Address okinaka's review on the envelope-region fix: the Region branch of the entry normalization passed the raw `region` argument straight through, so a null/blank `region` would stamp `entry.Region = null`. The matching Account branch already fell back to `regionResolver.getAccountId()` when accountId was null, so the two branches were asymmetric. Apply the same fallback chain to Region — `entry.Region = isBlank(region) ? regionResolver.getDefaultRegion() : region` — and tighten Account's null check to `isBlank(accountId)` for symmetry. No new test: every real caller of EventBridgeService.putEvents (EventBridgeHandler, ScheduleInvoker, PipesTargetInvoker, S3Service) passes a non-null region, and the surrounding bus/rule-prefix lookups also use the raw `region` argument, so a null `region` would short- circuit at the bus lookup before the normalization invariant matters. This is a consistency fix for the in-loop normalization itself; the "normalize once at the top, downstream sees a stable value" invariant Copilot's review established now holds for Region the same way it holds for Account. * style(eventbridge): keep envelope region/account fallback ternaries on one line Match the formatting of the new entry-normalization fallbacks (entry.put("Region", isBlank(region) ? ... : region) — single line) by joining the envelope ternaries that the previous commits left wrapped at the question mark. Same code, just consistent layout.
This commit is contained in:
1 parent
19b0432b8c
commit
c576d3341b
2 files changed
+162
-5
No files matched your search
+39
-5
@@ -618,7 +618,22 @@ public class EventBridgeService {
|
||||
int failed = 0;
|
||||
List<Map<String, String>> resultEntries = new ArrayList<>();
|
||||
|
||||
for (Map<String, Object> entry : entries) {
|
||||
for (Map<String, Object> originalEntry : entries) {
|
||||
// Defensive copy: callers may pass Map.of(...) (immutable) and we don't want to
|
||||
// mutate caller state regardless. Normalize Region / Account from the PutEvents
|
||||
// call context up front so matchesPattern, buildEventEnvelope, and the archive
|
||||
// capture all see the same values — without this, a pattern's region/account
|
||||
// filter would fall back to the resolver default in matchesPattern while the
|
||||
// delivered envelope would correctly carry the call's region/account, giving an
|
||||
// inconsistent observable.
|
||||
Map<String, Object> entry = new HashMap<>(originalEntry);
|
||||
if (isBlank(entry.get("Region"))) {
|
||||
entry.put("Region", isBlank(region) ? regionResolver.getDefaultRegion() : region);
|
||||
}
|
||||
if (isBlank(entry.get("Account"))) {
|
||||
entry.put("Account", isBlank(accountId) ? regionResolver.getAccountId() : accountId);
|
||||
}
|
||||
|
||||
String eventBusNameRaw = (String) entry.get("EventBusName");
|
||||
String effectiveBus = resolvedBusName(eventBusNameRaw);
|
||||
String busStoreKey = busKey(region, effectiveBus);
|
||||
@@ -645,7 +660,7 @@ public class EventBridgeService {
|
||||
if (matchesPattern(entry, rule.getEventPattern())) {
|
||||
String ruleKey = ruleKey(region, effectiveBus, rule.getName());
|
||||
List<Target> targets = accountGet(targetStore, accountId, ruleKey).orElse(List.of());
|
||||
String eventJson = buildEventEnvelope(entry, effectiveBus, eventId);
|
||||
String eventJson = buildEventEnvelope(entry, effectiveBus, eventId, region, accountId);
|
||||
for (Target target : targets) {
|
||||
invoker.invokeTarget(target, eventJson, region);
|
||||
}
|
||||
@@ -900,20 +915,35 @@ public class EventBridgeService {
|
||||
// ──────────────────────────── Target Routing ────────────────────────────
|
||||
|
||||
|
||||
private String buildEventEnvelope(Map<String, Object> entry, String busName, String eventId) {
|
||||
private String buildEventEnvelope(Map<String, Object> entry, String busName, String eventId,
|
||||
String callRegion, String callAccountId) {
|
||||
try {
|
||||
String source = (String) entry.getOrDefault("Source", "");
|
||||
String detailType = (String) entry.getOrDefault("DetailType", "");
|
||||
String detail = (String) entry.getOrDefault("Detail", "{}");
|
||||
ArrayNode resources = (ArrayNode) entry.getOrDefault("Resources", objectMapper.createArrayNode());
|
||||
// Envelope region/account precedence: entry-supplied "Region"/"Account" (set by
|
||||
// archive replay paths and cross-region producers) wins; otherwise stamp the
|
||||
// region/account the PutEvents call was made against. Only when neither is
|
||||
// available do we fall back to the RegionResolver defaults. This keeps the
|
||||
// envelope consistent with matchesPattern() which also reads entry.Region with
|
||||
// the same fallback chain.
|
||||
String envelopeRegion = (String) entry.get("Region");
|
||||
if (isBlank(envelopeRegion)) {
|
||||
envelopeRegion = isBlank(callRegion) ? regionResolver.getDefaultRegion() : callRegion;
|
||||
}
|
||||
String envelopeAccount = (String) entry.get("Account");
|
||||
if (isBlank(envelopeAccount)) {
|
||||
envelopeAccount = isBlank(callAccountId) ? regionResolver.getAccountId() : callAccountId;
|
||||
}
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("version", "0");
|
||||
node.put("id", eventId);
|
||||
node.put("source", source);
|
||||
node.put("detail-type", detailType);
|
||||
node.put("account", regionResolver.getAccountId());
|
||||
node.put("account", envelopeAccount);
|
||||
node.put("time", Instant.now().toString());
|
||||
node.put("region", regionResolver.getDefaultRegion());
|
||||
node.put("region", envelopeRegion);
|
||||
node.putArray("resources").addAll(resources);
|
||||
node.set("detail", objectMapper.readTree(detail));
|
||||
node.put("event-bus-name", busName);
|
||||
@@ -941,6 +971,10 @@ public class EventBridgeService {
|
||||
"EventBus not found: " + busName, 404));
|
||||
}
|
||||
|
||||
private static boolean isBlank(Object value) {
|
||||
return !(value instanceof String s) || s.isBlank();
|
||||
}
|
||||
|
||||
private static String resolvedBusName(String busName) {
|
||||
if (busName == null || busName.isBlank()) {
|
||||
return "default";
|
||||
|
||||
Reference in new issue
Block a user