fix(eventbridge): stop schedule-only rules from matching put events (#5168)

A rule with a ScheduleExpression and no EventPattern was matched by every
PutEvents call on its bus, because matchesPattern treats a missing pattern
as match-all. In AWS such a rule only fires on its schedule. Skip rules
without an event pattern when routing put events; archives keep the
match-all semantics they rely on.
This commit is contained in:
Abhigya authored and GitHub committed 2026-10-06 07:18:57 -04:00
1 parent 957d49255a
commit bb24ee09d1
2 files changed
+30

No files matched your search

@@ -812,6 +812,10 @@ public class EventBridgeService implements ResourceProvider {
if (rule.getState() != RuleState.ENABLED) {
continue;
}
// A rule without an event pattern is a schedule rule: it never matches put events.
if (rule.getEventPattern() == null || rule.getEventPattern().isBlank()) {
continue;
}
if (matchesPattern(entry, rule.getEventPattern())) {
String ruleKey = ruleKey(region, effectiveBus, rule.getName());
List<Target> targets = accountGet(targetStore, accountId, ruleKey).orElse(List.of());
@@ -36,6 +36,7 @@ import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
@@ -683,6 +684,31 @@ class EventBridgeServiceTest {
currentTargets.getValue().get().stream().map(Target::getId).toList());
}
@Test
void putEventsDoesNotMatchAScheduleOnlyRule() {
service.putRule("nightly", null, null, "rate(1 day)", RuleState.ENABLED,
null, null, null, REGION);
Target target = new Target("t1", "arn:aws:sqs:us-east-1:000000000000:queue-1", null, null);
service.putTargets("nightly", null, List.of(target), REGION);
service.putEvents(List.of(Map.of("Source", "aws.ecs", "DetailType", "ECS Deployment State Change",
"Detail", "{}")), REGION);
verify(dispatcherMock, never()).dispatch(anyString(), any(), anyString(), anyString(), any());
}
@Test
void putEventsMatchesARuleWithBothAPatternAndASchedule() {
Rule rule = service.putRule("both", null, "{\"source\":[\"my.app\"]}", "rate(1 day)",
RuleState.ENABLED, null, null, null, REGION);
Target target = new Target("t1", "arn:aws:sqs:us-east-1:000000000000:queue-1", null, null);
service.putTargets("both", null, List.of(target), REGION);
service.putEvents(List.of(Map.of("Source", "my.app", "DetailType", "Test", "Detail", "{}")), REGION);
verify(dispatcherMock).dispatch(eq(rule.getArn()), eq(target), anyString(), eq(REGION), any());
}
private void assertPutTargetsValidation(String expectedMessage, Target... targets) {
AwsException error = assertThrows(AwsException.class, () ->
service.putTargets("missing-rule", null, List.of(targets), REGION));