fix(eventbridge): require EventPattern or ScheduleExpression in PutRule (#5172)

AWS rejects a PutRule that has neither field with a ValidationException,
"Parameter(s) EventPattern or ScheduleExpression must be specified."
Floci accepted it and stored a rule that matched every event on its bus.
Validate before the bus lookup, and give the existing tests that created
such rules a pattern or schedule.
This commit is contained in:
Abhigya authored and GitHub committed 2026-10-06 07:29:00 -04:00
1 parent bb24ee09d1
commit be668acefd
6 files changed
+47 -6

No files matched your search

@@ -313,6 +313,11 @@ public class EventBridgeService implements ResourceProvider {
public Rule putRule(String name, String busName, String eventPattern,
String scheduleExpression, RuleState state, String description,
String roleArn, Map<String, String> tags, String region) {
if ((eventPattern == null || eventPattern.isBlank())
&& (scheduleExpression == null || scheduleExpression.isBlank())) {
throw new AwsException("ValidationException",
"Parameter(s) EventPattern or ScheduleExpression must be specified.", 400);
}
String effectiveBus = resolvedBusName(busName);
ensureBusExists(effectiveBus, region);
@@ -208,7 +208,8 @@ class EventBridgeIntegrationTest {
given()
.contentType(EVENT_BRIDGE_CONTENT_TYPE)
.header("X-Amz-Target", "AWSEvents.PutRule")
.body("{\"Name\":\"" + ruleName + "\",\"EventBusName\":\"" + busName + "\"}")
.body("{\"Name\":\"" + ruleName + "\",\"EventBusName\":\"" + busName
+ "\",\"EventPattern\":\"{\\\"source\\\":[\\\"my.app\\\"]}\"}")
.when().post("/")
.then().statusCode(200);
@@ -254,4 +255,25 @@ class EventBridgeIntegrationTest {
.then().statusCode(400)
.body("__type", equalTo("ResourceNotFoundException"));
}
@Test
@Order(8)
void putRuleWithoutPatternOrScheduleIsRejected() {
given()
.contentType(EVENT_BRIDGE_CONTENT_TYPE)
.header("X-Amz-Target", "AWSEvents.PutRule")
.body("{\"Name\":\"eb-no-pattern-or-schedule\"}")
.when().post("/")
.then().statusCode(400)
.body("__type", equalTo("ValidationException"))
.body("message", equalTo("Parameter(s) EventPattern or ScheduleExpression must be specified."));
given()
.contentType(EVENT_BRIDGE_CONTENT_TYPE)
.header("X-Amz-Target", "AWSEvents.DescribeRule")
.body("{\"Name\":\"eb-no-pattern-or-schedule\"}")
.when().post("/")
.then().statusCode(400)
.body("__type", equalTo("ResourceNotFoundException"));
}
}
@@ -32,6 +32,7 @@ class EventBridgeListTagsIntegrationTest {
.body("""
{
"Name": "tagged-rule",
"ScheduleExpression": "rate(1 day)",
"Tags": [
{"Key": "env", "Value": "test"},
{"Key": "team", "Value": "platform"}
@@ -80,7 +81,7 @@ class EventBridgeListTagsIntegrationTest {
given()
.contentType(EVENT_BRIDGE_CONTENT_TYPE)
.header("X-Amz-Target", "AWSEvents.PutRule")
.body("{\"Name\":\"untagged-rule\"}")
.body("{\"Name\":\"untagged-rule\",\"ScheduleExpression\":\"rate(1 day)\"}")
.when()
.post("/")
.then()
@@ -148,7 +148,7 @@ class EventBridgeSchedulerIntegrationTest {
assertTrue(scheduler.isRunning(arn));
eventBridgeService.putRule(
"test-rule", "default", null, null,
"test-rule", "default", "{\"source\":[\"my.app\"]}", null,
RuleState.ENABLED, null, null, null, REGION);
assertFalse(scheduler.isRunning(arn));
@@ -276,7 +276,7 @@ class EventBridgeSchedulerIntegrationTest {
assertTrue(scheduler.isRunning(arn));
eventBridgeService.putRule(
"test-cron-rule", "default", null, null,
"test-cron-rule", "default", "{\"source\":[\"my.app\"]}", null,
RuleState.ENABLED, null, null, null, REGION);
assertFalse(scheduler.isRunning(arn));
@@ -24,6 +24,7 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
@@ -372,7 +373,19 @@ class EventBridgeServiceTest {
@Test
void putRuleForNonExistentBusThrows() {
assertThrows(AwsException.class, () ->
service.putRule("rule", "missing-bus", null, null, null, null, null, null, REGION));
service.putRule("rule", "missing-bus", "{\"source\":[\"my.app\"]}", null, null,
null, null, null, REGION));
}
@ParameterizedTest
@NullAndEmptySource
void putRuleWithoutPatternOrScheduleThrowsValidation(String missing) {
AwsException error = assertThrows(AwsException.class, () ->
service.putRule("rule", null, missing, missing, RuleState.ENABLED, null, null, null, REGION));
assertEquals("ValidationException", error.getErrorCode());
assertEquals(400, error.getHttpStatus());
assertEquals("Parameter(s) EventPattern or ScheduleExpression must be specified.", error.getMessage());
assertTrue(service.listRules(null, null, REGION).isEmpty());
}
@Test
@@ -76,7 +76,7 @@ class EventBridgeTagResourceIntegrationTest {
given()
.contentType(EVENT_BRIDGE_CONTENT_TYPE)
.header("X-Amz-Target", "AWSEvents.PutRule")
.body("{\"Name\":\"tag-test-rule\"}")
.body("{\"Name\":\"tag-test-rule\",\"ScheduleExpression\":\"rate(1 day)\"}")
.when()
.post("/")
.then()