From d267ba3a9d21cfd25dde710ac3943cec8caa08de Mon Sep 17 00:00:00 2001 From: Langning Zhang Date: Wed, 30 Sep 2026 22:00:19 +0800 Subject: [PATCH] fix(ses): validate EventBridge default bus ARNs (#4677) * fix(ses): validate EventBridge default bus ARNs Signed-off-by: boulea7 * fix(ses): reject EventBridge bus account and Region mismatches Signed-off-by: boulea7 --------- Signed-off-by: boulea7 --- ...sConfigurationSetEventDestinationTest.java | 71 ++++ .../ses/SesConfigurationSetController.java | 5 +- .../ses/SesConfigurationSetService.java | 48 ++- .../floci/services/ses/SesQueryHandler.java | 10 +- ...sConfigurationSetEventDestinationTest.java | 136 ++++++- ...nSetEventDestinationV2IntegrationTest.java | 368 ++++++++++++++++++ .../services/ses/SesEventPublisherTest.java | 30 ++ .../SesEventPublishingV2IntegrationTest.java | 18 +- 8 files changed, 652 insertions(+), 34 deletions(-) diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesConfigurationSetEventDestinationTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesConfigurationSetEventDestinationTest.java index c7ab9ddd8a..0ce4802ffc 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesConfigurationSetEventDestinationTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesConfigurationSetEventDestinationTest.java @@ -7,14 +7,18 @@ import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import software.amazon.awssdk.awscore.exception.AwsServiceException; import software.amazon.awssdk.services.ses.SesClient; import software.amazon.awssdk.services.sesv2.SesV2Client; +import software.amazon.awssdk.services.sesv2.model.BadRequestException; import software.amazon.awssdk.services.sesv2.model.CreateConfigurationSetEventDestinationRequest; import software.amazon.awssdk.services.sesv2.model.CreateConfigurationSetRequest; import software.amazon.awssdk.services.sesv2.model.DeleteConfigurationSetEventDestinationRequest; import software.amazon.awssdk.services.sesv2.model.DeleteConfigurationSetRequest; +import software.amazon.awssdk.services.sesv2.model.EventBridgeDestination; import software.amazon.awssdk.services.sesv2.model.EventDestinationDefinition; import software.amazon.awssdk.services.sesv2.model.EventType; import software.amazon.awssdk.services.sesv2.model.GetConfigurationSetEventDestinationsRequest; @@ -43,6 +47,8 @@ class SesConfigurationSetEventDestinationTest { private static final String ED_NAME = "ed-sns"; private static final String TOPIC_ARN = "arn:aws:sns:us-east-1:000000000000:ses-events"; private static final String TOPIC_ARN_2 = "arn:aws:sns:us-east-1:000000000000:ses-events-2"; + private static final String DEFAULT_BUS_ARN = "arn:aws:events:us-east-1:000000000000:event-bus/default"; + private static final String CUSTOM_BUS_ARN = "arn:aws:events:us-east-1:000000000000:event-bus/custom"; @BeforeAll static void setup() { @@ -224,6 +230,71 @@ class SesConfigurationSetEventDestinationTest { .isEqualTo(404); } + @ParameterizedTest(name = "{0}") + @CsvSource({ + "custom," + CUSTOM_BUS_ARN, + "mismatched-account,arn:aws:events:us-east-1:111111111111:event-bus/default", + "mismatched-region,arn:aws:events:us-west-2:000000000000:event-bus/default" + }) + @Order(9) + void v2_eventBridgeDefaultBusAcceptedAndInvalidBusRejected(String caseName, String rejectedBusArn) { + String destinationName = "ed-eventbridge-" + caseName; + sesV2.createConfigurationSetEventDestination(CreateConfigurationSetEventDestinationRequest.builder() + .configurationSetName(v2CsName) + .eventDestinationName(destinationName) + .eventDestination(EventDestinationDefinition.builder() + .enabled(true) + .matchingEventTypes(EventType.SEND) + .eventBridgeDestination(EventBridgeDestination.builder().eventBusArn(DEFAULT_BUS_ARN).build()) + .build()) + .build()); + + try { + assertThatThrownBy(() -> sesV2.createConfigurationSetEventDestination( + CreateConfigurationSetEventDestinationRequest.builder() + .configurationSetName(v2CsName) + .eventDestinationName("ed-invalid-" + caseName) + .eventDestination(EventDestinationDefinition.builder() + .matchingEventTypes(EventType.SEND) + .eventBridgeDestination(EventBridgeDestination.builder().eventBusArn(rejectedBusArn).build()) + .build()) + .build())) + .isInstanceOf(BadRequestException.class) + .extracting(e -> ((AwsServiceException) e).statusCode()) + .isEqualTo(400); + + assertThatThrownBy(() -> sesV2.updateConfigurationSetEventDestination( + UpdateConfigurationSetEventDestinationRequest.builder() + .configurationSetName(v2CsName) + .eventDestinationName(destinationName) + .eventDestination(EventDestinationDefinition.builder() + .enabled(false) + .matchingEventTypes(EventType.BOUNCE) + .eventBridgeDestination(EventBridgeDestination.builder().eventBusArn(rejectedBusArn).build()) + .build()) + .build())) + .isInstanceOf(BadRequestException.class) + .extracting(e -> ((AwsServiceException) e).statusCode()) + .isEqualTo(400); + + GetConfigurationSetEventDestinationsResponse response = + sesV2.getConfigurationSetEventDestinations(GetConfigurationSetEventDestinationsRequest.builder() + .configurationSetName(v2CsName) + .build()); + assertThat(response.eventDestinations()).hasSize(1); + assertThat(response.eventDestinations().get(0).name()).isEqualTo(destinationName); + assertThat(response.eventDestinations().get(0).enabled()).isTrue(); + assertThat(response.eventDestinations().get(0).matchingEventTypes()).containsExactly(EventType.SEND); + assertThat(response.eventDestinations().get(0).eventBridgeDestination().eventBusArn()) + .isEqualTo(DEFAULT_BUS_ARN); + } finally { + sesV2.deleteConfigurationSetEventDestination(DeleteConfigurationSetEventDestinationRequest.builder() + .configurationSetName(v2CsName) + .eventDestinationName(destinationName) + .build()); + } + } + // ─────────────────────────── V1 (Query/XML) ─────────────────────────── @Test diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetController.java index 2ba352efe0..124b34c6a7 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetController.java @@ -455,7 +455,8 @@ public class SesConfigurationSetController { throw new AwsException("BadRequestException", "EventDestination is required.", 400); } EventDestination dest = objectMapper.treeToValue(edNode, EventDestination.class); - configSetService.createEventDestination(configurationSetName, edName, dest, region); + configSetService.createEventDestination(configurationSetName, edName, dest, + regionResolver.getAccountId(), region); LOG.infov("SES V2 CreateConfigurationSetEventDestination: {0} on {1}", edName, configurationSetName); return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { @@ -499,7 +500,7 @@ public class SesConfigurationSetController { } EventDestination dest = objectMapper.treeToValue(edNode, EventDestination.class); configSetService.updateEventDestination(configurationSetName, eventDestinationName, - dest, region); + dest, regionResolver.getAccountId(), region); LOG.infov("SES V2 UpdateConfigurationSetEventDestination: {0} on {1}", eventDestinationName, configurationSetName); return Response.ok(objectMapper.createObjectNode()).build(); diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetService.java index ed14d384af..2d738a314c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetService.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetService.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.ses; import com.fasterxml.jackson.core.type.TypeReference; +import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; @@ -8,6 +9,7 @@ import io.github.hectorvent.floci.services.ses.model.ArchivingOptions; import io.github.hectorvent.floci.services.ses.model.CloudWatchDimensionConfiguration; import io.github.hectorvent.floci.services.ses.model.ConfigurationSet; import io.github.hectorvent.floci.services.ses.model.DeliveryOptions; +import io.github.hectorvent.floci.services.ses.model.EventBridgeDestination; import io.github.hectorvent.floci.services.ses.model.EventDestination; import io.github.hectorvent.floci.services.ses.model.TrackingOptions; import io.github.hectorvent.floci.services.ses.model.SuppressionOptions; @@ -473,9 +475,9 @@ public class SesConfigurationSetService { // ──────────────────────────── Event destinations ──────────────────────────── public void createEventDestination(String configSetName, String eventDestinationName, - EventDestination dest, String region) { + EventDestination dest, String accountId, String region) { validateEventDestinationName(eventDestinationName); - validateEventDestination(dest); + validateEventDestination(dest, accountId, region); ConfigurationSet cs = get(configSetName, region); if (indexOfEventDestination(cs.getEventDestinations(), eventDestinationName) >= 0) { throw new AwsException("AlreadyExists", @@ -494,9 +496,9 @@ public class SesConfigurationSetService { } public void updateEventDestination(String configSetName, String eventDestinationName, - EventDestination dest, String region) { + EventDestination dest, String accountId, String region) { validateEventDestinationName(eventDestinationName); - validateEventDestination(dest); + validateEventDestination(dest, accountId, region); ConfigurationSet cs = get(configSetName, region); int index = indexOfEventDestination(cs.getEventDestinations(), eventDestinationName); if (index < 0) { @@ -549,7 +551,7 @@ public class SesConfigurationSetService { } } - static void validateEventDestination(EventDestination dest) { + static void validateEventDestination(EventDestination dest, String accountId, String region) { if (dest == null) { throw new AwsException("InvalidParameterValue", "EventDestination is required.", 400); } @@ -616,6 +618,42 @@ public class SesConfigurationSetService { "Invalid Pinpoint application ARN provided: " + dest.getPinpointDestination().getApplicationArn() + ".", 400); } + if (dest.getEventBridgeDestination() != null) { + validateEventBridgeDestination(dest.getEventBridgeDestination(), accountId, region); + } + } + + // SES publishes email events only to the default EventBridge bus, so the + // EventBusArn member must name event-bus/default of the events service in the + // configuration set's account and region. All supported AWS partitions are accepted. + private static void validateEventBridgeDestination( + EventBridgeDestination eventBridge, String accountId, String region) { + String busArn = eventBridge.getEventBusArn(); + if (busArn == null || busArn.isBlank()) { + throw new AwsException("InvalidParameterValue", + "EventBridgeDestination requires a non-blank EventBusArn.", 400); + } + AwsArnUtils.Arn arn; + try { + arn = AwsArnUtils.parse(busArn); + } catch (IllegalArgumentException e) { + throw new AwsException("InvalidParameterValue", + "EventBridgeDestination EventBusArn is not a valid ARN: " + busArn + ".", 400); + } + if (!arn.partition().matches(AwsArnUtils.PARTITION_REGEX) + || arn.region().isBlank() + || !arn.accountId().matches("[0-9]{12}") + || !"events".equals(arn.service()) + || !"event-bus/default".equals(arn.resource())) { + throw new AwsException("InvalidParameterValue", + "EventBridgeDestination EventBusArn must be a valid default event bus ARN: " + + busArn + ".", 400); + } + if (!arn.accountId().equals(accountId) || !arn.region().equals(region)) { + throw new AwsException("InvalidParameterValue", + "EventBridgeDestination EventBusArn must match the configuration set's account and Region: " + + busArn + ".", 400); + } } private static int countDestinations(EventDestination dest) { diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesQueryHandler.java index f5d2c175f4..ea5b9bf2e9 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesQueryHandler.java @@ -4,6 +4,7 @@ import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.AwsNamespaces; import io.github.hectorvent.floci.core.common.AwsQueryResponse; import io.github.hectorvent.floci.core.common.PaginatedResult; +import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.common.XmlBuilder; import io.github.hectorvent.floci.services.ses.model.BulkEmailEntry; import io.github.hectorvent.floci.services.ses.model.BulkEmailEntryResult; @@ -57,6 +58,7 @@ public class SesQueryHandler { private final SesConfigurationSetService configSetService; private final SesPolicyService policyService; private final SesSentEmailService sentEmailService; + private final RegionResolver regionResolver; private final ObjectMapper objectMapper; @Inject @@ -65,7 +67,8 @@ public class SesQueryHandler { SesCvetService cvetService, SesAccountService accountService, SesConfigurationSetService configSetService, SesPolicyService policyService, - SesSentEmailService sentEmailService, ObjectMapper objectMapper) { + SesSentEmailService sentEmailService, RegionResolver regionResolver, + ObjectMapper objectMapper) { this.sesService = sesService; this.receiptRuleService = receiptRuleService; this.identityService = identityService; @@ -75,6 +78,7 @@ public class SesQueryHandler { this.configSetService = configSetService; this.policyService = policyService; this.sentEmailService = sentEmailService; + this.regionResolver = regionResolver; this.objectMapper = objectMapper; } @@ -948,7 +952,7 @@ public class SesQueryHandler { String configSet = requireParam(params, "ConfigurationSetName"); String edName = requireParam(params, "EventDestination.Name"); EventDestination dest = readEventDestination(params, "EventDestination"); - configSetService.createEventDestination(configSet, edName, dest, region); + configSetService.createEventDestination(configSet, edName, dest, regionResolver.getAccountId(), region); return Response.ok(AwsQueryResponse.envelopeEmptyResult( "CreateConfigurationSetEventDestination", AwsNamespaces.SES)).build(); } @@ -958,7 +962,7 @@ public class SesQueryHandler { String configSet = requireParam(params, "ConfigurationSetName"); String edName = requireParam(params, "EventDestination.Name"); EventDestination dest = readEventDestination(params, "EventDestination"); - configSetService.updateEventDestination(configSet, edName, dest, region); + configSetService.updateEventDestination(configSet, edName, dest, regionResolver.getAccountId(), region); return Response.ok(AwsQueryResponse.envelopeEmptyResult( "UpdateConfigurationSetEventDestination", AwsNamespaces.SES)).build(); } diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetEventDestinationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetEventDestinationTest.java index b9c3886364..16d21f74de 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetEventDestinationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetEventDestinationTest.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.ses; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.services.ses.model.CloudWatchDestination; import io.github.hectorvent.floci.services.ses.model.CloudWatchDimensionConfiguration; +import io.github.hectorvent.floci.services.ses.model.EventBridgeDestination; import io.github.hectorvent.floci.services.ses.model.EventDestination; import io.github.hectorvent.floci.services.ses.model.KinesisFirehoseDestination; import io.github.hectorvent.floci.services.ses.model.PinpointDestination; @@ -18,11 +19,14 @@ import static org.junit.jupiter.api.Assertions.assertThrows; /** * Unit tests for the static input validation behind the SES V2 * ConfigurationSetEventDestination operations. These fail-closed paths are - * pure logic and live here rather than in the integration test. Error - * messages mirror the real AWS SESv2 wire responses. + * pure logic and live here rather than in the integration test. Protocol + * integration tests cover the AWS-modeled error responses. */ class SesConfigurationSetEventDestinationTest { + private static final String ACCOUNT_ID = "000000000000"; + private static final String REGION = "us-east-1"; + private static EventDestination withSns(List matchingEventTypes) { EventDestination ed = new EventDestination(); ed.setMatchingEventTypes(matchingEventTypes); @@ -63,13 +67,14 @@ class SesConfigurationSetEventDestinationTest { @Test void validDestination_passes() { - assertDoesNotThrow(() -> SesConfigurationSetService.validateEventDestination(withSns(List.of("SEND", "BOUNCE")))); + assertDoesNotThrow(() -> SesConfigurationSetService.validateEventDestination( + withSns(List.of("SEND", "BOUNCE")), ACCOUNT_ID, REGION)); } @Test void emptyMatchingEventTypes_throws() { AwsException ex = assertThrows(AwsException.class, - () -> SesConfigurationSetService.validateEventDestination(withSns(List.of()))); + () -> SesConfigurationSetService.validateEventDestination(withSns(List.of()), ACCOUNT_ID, REGION)); assertEquals("InvalidParameterValue", ex.getErrorCode()); assertEquals("At least one event type must be specified.", ex.getMessage()); } @@ -77,7 +82,8 @@ class SesConfigurationSetEventDestinationTest { @Test void invalidEventType_throws() { AwsException ex = assertThrows(AwsException.class, - () -> SesConfigurationSetService.validateEventDestination(withSns(List.of("SEND", "NOPE")))); + () -> SesConfigurationSetService.validateEventDestination( + withSns(List.of("SEND", "NOPE")), ACCOUNT_ID, REGION)); assertEquals("InvalidParameterValue", ex.getErrorCode()); assertEquals("Invalid event type: NOPE. Valid values are [SEND, REJECT, BOUNCE, COMPLAINT, " + "DELIVERY, OPEN, CLICK, RENDERING_FAILURE, DELIVERY_DELAY, SUBSCRIPTION].", ex.getMessage()); @@ -88,7 +94,7 @@ class SesConfigurationSetEventDestinationTest { EventDestination ed = new EventDestination(); ed.setMatchingEventTypes(List.of("SEND")); AwsException ex = assertThrows(AwsException.class, - () -> SesConfigurationSetService.validateEventDestination(ed)); + () -> SesConfigurationSetService.validateEventDestination(ed, ACCOUNT_ID, REGION)); assertEquals("InvalidParameterValue", ex.getErrorCode()); assertEquals("Event destination is not provided.", ex.getMessage()); } @@ -98,7 +104,7 @@ class SesConfigurationSetEventDestinationTest { EventDestination ed = withSns(List.of("SEND")); ed.setCloudWatchDestination(cloudWatch()); AwsException ex = assertThrows(AwsException.class, - () -> SesConfigurationSetService.validateEventDestination(ed)); + () -> SesConfigurationSetService.validateEventDestination(ed, ACCOUNT_ID, REGION)); assertEquals("InvalidParameterValue", ex.getErrorCode()); assertEquals("Please provide only one destination with each request. Either a Firehose Destination " + "or a Cloudwatch Destination or an SNS Destination or an EventBridge Destination.", @@ -111,7 +117,7 @@ class SesConfigurationSetEventDestinationTest { ed.setMatchingEventTypes(List.of("SEND")); ed.setCloudWatchDestination(new CloudWatchDestination()); AwsException ex = assertThrows(AwsException.class, - () -> SesConfigurationSetService.validateEventDestination(ed)); + () -> SesConfigurationSetService.validateEventDestination(ed, ACCOUNT_ID, REGION)); assertEquals("InvalidParameterValue", ex.getErrorCode()); assertEquals("CloudWatch metrics dimension configuration list cannot be empty.", ex.getMessage()); } @@ -121,7 +127,7 @@ class SesConfigurationSetEventDestinationTest { EventDestination ed = new EventDestination(); ed.setMatchingEventTypes(List.of("SEND")); ed.setCloudWatchDestination(cloudWatch()); - assertDoesNotThrow(() -> SesConfigurationSetService.validateEventDestination(ed)); + assertDoesNotThrow(() -> SesConfigurationSetService.validateEventDestination(ed, ACCOUNT_ID, REGION)); } @Test @@ -130,7 +136,7 @@ class SesConfigurationSetEventDestinationTest { ed.setMatchingEventTypes(List.of("SEND")); ed.setPinpointDestination(new PinpointDestination()); AwsException ex = assertThrows(AwsException.class, - () -> SesConfigurationSetService.validateEventDestination(ed)); + () -> SesConfigurationSetService.validateEventDestination(ed, ACCOUNT_ID, REGION)); assertEquals("InvalidParameterValue", ex.getErrorCode()); assertEquals("Invalid Pinpoint application ARN provided: null.", ex.getMessage()); } @@ -142,7 +148,7 @@ class SesConfigurationSetEventDestinationTest { PinpointDestination pp = new PinpointDestination(); pp.setApplicationArn("arn:aws:mobiletargeting:us-east-1:000000000000:apps/abc"); ed.setPinpointDestination(pp); - assertDoesNotThrow(() -> SesConfigurationSetService.validateEventDestination(ed)); + assertDoesNotThrow(() -> SesConfigurationSetService.validateEventDestination(ed, ACCOUNT_ID, REGION)); } @Test @@ -153,7 +159,7 @@ class SesConfigurationSetEventDestinationTest { sns.setTopicArn(""); ed.setSnsDestination(sns); AwsException ex = assertThrows(AwsException.class, - () -> SesConfigurationSetService.validateEventDestination(ed)); + () -> SesConfigurationSetService.validateEventDestination(ed, ACCOUNT_ID, REGION)); assertEquals("InvalidParameterValue", ex.getErrorCode()); assertEquals("SnsDestination requires a non-blank TopicArn.", ex.getMessage()); } @@ -164,7 +170,7 @@ class SesConfigurationSetEventDestinationTest { ed.setMatchingEventTypes(List.of("SEND")); ed.setSnsDestination(new SnsDestination()); // TopicArn left null AwsException ex = assertThrows(AwsException.class, - () -> SesConfigurationSetService.validateEventDestination(ed)); + () -> SesConfigurationSetService.validateEventDestination(ed, ACCOUNT_ID, REGION)); assertEquals("InvalidParameterValue", ex.getErrorCode()); assertEquals("SnsDestination requires a non-blank TopicArn.", ex.getMessage()); } @@ -178,7 +184,7 @@ class SesConfigurationSetEventDestinationTest { // DeliveryStreamArn left null ed.setKinesisFirehoseDestination(fh); AwsException ex = assertThrows(AwsException.class, - () -> SesConfigurationSetService.validateEventDestination(ed)); + () -> SesConfigurationSetService.validateEventDestination(ed, ACCOUNT_ID, REGION)); assertEquals("InvalidParameterValue", ex.getErrorCode()); assertEquals("KinesisFirehoseDestination requires both IamRoleArn and DeliveryStreamArn.", ex.getMessage()); @@ -192,7 +198,8 @@ class SesConfigurationSetEventDestinationTest { fh.setDeliveryStreamArn("arn:aws:firehose:us-east-1:000000000000:deliverystream/ses"); // IamRoleArn left null ed.setKinesisFirehoseDestination(fh); - assertThrows(AwsException.class, () -> SesConfigurationSetService.validateEventDestination(ed)); + assertThrows(AwsException.class, + () -> SesConfigurationSetService.validateEventDestination(ed, ACCOUNT_ID, REGION)); } @Test @@ -203,7 +210,7 @@ class SesConfigurationSetEventDestinationTest { fh.setIamRoleArn("arn:aws:iam::000000000000:role/ses-firehose"); fh.setDeliveryStreamArn("arn:aws:firehose:us-east-1:000000000000:deliverystream/ses"); ed.setKinesisFirehoseDestination(fh); - assertDoesNotThrow(() -> SesConfigurationSetService.validateEventDestination(ed)); + assertDoesNotThrow(() -> SesConfigurationSetService.validateEventDestination(ed, ACCOUNT_ID, REGION)); } @Test @@ -218,7 +225,7 @@ class SesConfigurationSetEventDestinationTest { cw.setDimensionConfigurations(List.of(dim)); ed.setCloudWatchDestination(cw); AwsException ex = assertThrows(AwsException.class, - () -> SesConfigurationSetService.validateEventDestination(ed)); + () -> SesConfigurationSetService.validateEventDestination(ed, ACCOUNT_ID, REGION)); assertEquals("InvalidParameterValue", ex.getErrorCode()); // Member index 1-based for callers, included in the error message. assertEquals("CloudWatchDestination dimension configurations require " @@ -237,7 +244,8 @@ class SesConfigurationSetEventDestinationTest { dim.setDefaultDimensionValue("default"); cw.setDimensionConfigurations(List.of(dim)); ed.setCloudWatchDestination(cw); - assertThrows(AwsException.class, () -> SesConfigurationSetService.validateEventDestination(ed)); + assertThrows(AwsException.class, + () -> SesConfigurationSetService.validateEventDestination(ed, ACCOUNT_ID, REGION)); } @Test @@ -251,7 +259,97 @@ class SesConfigurationSetEventDestinationTest { // DefaultDimensionValue left null cw.setDimensionConfigurations(List.of(dim)); ed.setCloudWatchDestination(cw); - assertThrows(AwsException.class, () -> SesConfigurationSetService.validateEventDestination(ed)); + assertThrows(AwsException.class, + () -> SesConfigurationSetService.validateEventDestination(ed, ACCOUNT_ID, REGION)); + } + + @Test + void eventBridgeDefaultBusCommercialPartition_passes() { + assertDoesNotThrow(() -> SesConfigurationSetService.validateEventDestination( + withEventBridge("arn:aws:events:us-east-1:000000000000:event-bus/default"), ACCOUNT_ID, REGION)); + } + + @Test + void eventBridgeDefaultBusNonCommercialPartition_passes() { + assertDoesNotThrow(() -> SesConfigurationSetService.validateEventDestination( + withEventBridge("arn:aws-cn:events:cn-north-1:000000000000:event-bus/default"), + ACCOUNT_ID, "cn-north-1")); + } + + @Test + void eventBridgeDefaultBusMatchingNonDefaultRequestScope_passes() { + assertDoesNotThrow(() -> SesConfigurationSetService.validateEventDestination( + withEventBridge("arn:aws:events:eu-west-1:111111111111:event-bus/default"), + "111111111111", "eu-west-1")); + } + + @Test + void eventBridgeMismatchedAccount_throws() { + AwsException ex = assertThrows(AwsException.class, + () -> SesConfigurationSetService.validateEventDestination( + withEventBridge("arn:aws:events:us-east-1:111111111111:event-bus/default"), + ACCOUNT_ID, REGION)); + assertEquals("InvalidParameterValue", ex.getErrorCode()); + assertEquals(400, ex.getHttpStatus()); + } + + @Test + void eventBridgeMismatchedRegion_throws() { + AwsException ex = assertThrows(AwsException.class, + () -> SesConfigurationSetService.validateEventDestination( + withEventBridge("arn:aws:events:us-west-2:000000000000:event-bus/default"), + ACCOUNT_ID, REGION)); + assertEquals("InvalidParameterValue", ex.getErrorCode()); + assertEquals(400, ex.getHttpStatus()); + } + + @Test + void eventBridgeNullBusArn_throws() { + AwsException ex = assertThrows(AwsException.class, + () -> SesConfigurationSetService.validateEventDestination(withEventBridge(null), ACCOUNT_ID, REGION)); + assertEquals("InvalidParameterValue", ex.getErrorCode()); + } + + @Test + void eventBridgeBlankBusArn_throws() { + AwsException ex = assertThrows(AwsException.class, + () -> SesConfigurationSetService.validateEventDestination(withEventBridge(" "), ACCOUNT_ID, REGION)); + assertEquals("InvalidParameterValue", ex.getErrorCode()); + } + + @Test + void eventBridgeCustomBus_throws() { + AwsException ex = assertThrows(AwsException.class, + () -> SesConfigurationSetService.validateEventDestination( + withEventBridge( + "arn:aws:events:us-east-1:000000000000:event-bus/custom-bus"), ACCOUNT_ID, REGION)); + assertEquals("InvalidParameterValue", ex.getErrorCode()); + } + + @Test + void eventBridgeWrongServiceWithDefaultSuffix_throws() { + AwsException ex = assertThrows(AwsException.class, + () -> SesConfigurationSetService.validateEventDestination( + withEventBridge( + "arn:aws:sns:us-east-1:000000000000:event-bus/default"), ACCOUNT_ID, REGION)); + assertEquals("InvalidParameterValue", ex.getErrorCode()); + } + + @Test + void eventBridgeMalformedArn_throws() { + AwsException ex = assertThrows(AwsException.class, + () -> SesConfigurationSetService.validateEventDestination( + withEventBridge("not-an-arn"), ACCOUNT_ID, REGION)); + assertEquals("InvalidParameterValue", ex.getErrorCode()); + } + + private static EventDestination withEventBridge(String busArn) { + EventDestination ed = new EventDestination(); + ed.setMatchingEventTypes(List.of("SEND")); + EventBridgeDestination eventBridge = new EventBridgeDestination(); + eventBridge.setEventBusArn(busArn); + ed.setEventBridgeDestination(eventBridge); + return ed; } private static CloudWatchDestination cloudWatch() { diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetEventDestinationV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetEventDestinationV2IntegrationTest.java index afc1e6cb9a..0fd918539c 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetEventDestinationV2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetEventDestinationV2IntegrationTest.java @@ -5,11 +5,16 @@ import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.util.List; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.nullValue; /** * Integration tests for SES V2 ConfigurationSetEventDestination endpoints under @@ -24,6 +29,11 @@ class SesConfigurationSetEventDestinationV2IntegrationTest { private static final String CS = "v2-cs-ed"; + private static final String SCOPED_AUTH_HEADER = + "AWS4-HMAC-SHA256 Credential=111111111111/20260101/us-west-2/ses/aws4_request"; + private static final String SCOPED_DEFAULT_BUS_ARN = + "arn:aws:events:us-west-2:111111111111:event-bus/default"; + @Test @Order(1) void createConfigurationSet() { @@ -370,4 +380,362 @@ class SesConfigurationSetEventDestinationV2IntegrationTest { .body("EventDestinations[0].SnsDestination.TopicArn", equalTo("arn:aws:sns:us-east-1:000000000000:ed-a-updated")); } + + @Test + @Order(16) + void createEventBridgeConfigurationSet() { + String cs = "v2-cs-ed-ebus"; + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body("{\"ConfigurationSetName\": \"" + cs + "\"}") + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(200); + } + + @Test + @Order(17) + void createEventDestination_eventBridgeCustomBusRejected() { + String cs = "v2-cs-ed-ebus"; + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "EventDestinationName": "ed-eb-custom", + "EventDestination": { + "Enabled": true, + "MatchingEventTypes": ["SEND"], + "EventBridgeDestination": { + "EventBusArn": "arn:aws:events:us-east-1:000000000000:event-bus/custom-bus" + } + } + } + """) + .when() + .post("/v2/email/configuration-sets/" + cs + "/event-destinations") + .then() + .statusCode(400) + .body("__type", equalTo("BadRequestException")); + + // The rejected destination must not have been stored. + given() + .header("Authorization", AUTH_HEADER) + .when() + .get("/v2/email/configuration-sets/" + cs + "/event-destinations") + .then() + .statusCode(200) + .body("EventDestinations", hasSize(0)); + } + + @Test + @Order(18) + void updateEventDestination_eventBridgeCustomBusRejectedAndDefaultPreserved() { + String cs = "v2-cs-ed-ebus"; + String defaultBusArn = "arn:aws:events:us-east-1:000000000000:event-bus/default"; + + // Seed a destination on the default event bus. + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "EventDestinationName": "ed-eb-default", + "EventDestination": { + "Enabled": true, + "MatchingEventTypes": ["SEND"], + "EventBridgeDestination": {"EventBusArn": "%s"} + } + } + """.formatted(defaultBusArn)) + .when() + .post("/v2/email/configuration-sets/" + cs + "/event-destinations") + .then() + .statusCode(200); + + // An otherwise identical ARN naming a custom event bus must be rejected. + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "EventDestination": { + "Enabled": true, + "MatchingEventTypes": ["SEND"], + "EventBridgeDestination": { + "EventBusArn": "arn:aws:events:us-east-1:000000000000:event-bus/custom-bus" + } + } + } + """) + .when() + .put("/v2/email/configuration-sets/" + cs + "/event-destinations/ed-eb-default") + .then() + .statusCode(400) + .body("__type", equalTo("BadRequestException")); + + // The stored destination must still point at the default event bus. + given() + .header("Authorization", AUTH_HEADER) + .when() + .get("/v2/email/configuration-sets/" + cs + "/event-destinations") + .then() + .statusCode(200) + .body("EventDestinations", hasSize(1)) + .body("EventDestinations[0].Name", equalTo("ed-eb-default")) + .body("EventDestinations[0].EventBridgeDestination.EventBusArn", + equalTo(defaultBusArn)); + } + + @Test + @Order(19) + void createEventDestination_eventBridgeDefaultBusNonCommercialPartitionSucceeds() { + String cs = "v2-cs-ed-ebus"; + String cnAuthHeader = + "AWS4-HMAC-SHA256 Credential=AKID/20260101/cn-north-1/ses/aws4_request"; + String cnDefaultBusArn = + "arn:aws-cn:events:cn-north-1:000000000000:event-bus/default"; + given() + .contentType("application/json") + .header("Authorization", cnAuthHeader) + .body("{\"ConfigurationSetName\": \"" + cs + "\"}") + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(200); + + given() + .contentType("application/json") + .header("Authorization", cnAuthHeader) + .body(""" + { + "EventDestinationName": "ed-eb-default-cn", + "EventDestination": { + "Enabled": true, + "MatchingEventTypes": ["SEND"], + "EventBridgeDestination": {"EventBusArn": "%s"} + } + } + """.formatted(cnDefaultBusArn)) + .when() + .post("/v2/email/configuration-sets/" + cs + "/event-destinations") + .then() + .statusCode(200); + + given() + .header("Authorization", cnAuthHeader) + .when() + .get("/v2/email/configuration-sets/" + cs + "/event-destinations") + .then() + .statusCode(200) + .body("EventDestinations.find { it.Name == 'ed-eb-default-cn' }" + + ".EventBridgeDestination.EventBusArn", + equalTo(cnDefaultBusArn)); + } + + @Test + @Order(20) + void createEventDestination_eventBridgeBlankBusArnRejected() { + String cs = "v2-cs-ed-ebus"; + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "EventDestinationName": "ed-eb-blank", + "EventDestination": { + "Enabled": true, + "MatchingEventTypes": ["SEND"], + "EventBridgeDestination": {"EventBusArn": ""} + } + } + """) + .when() + .post("/v2/email/configuration-sets/" + cs + "/event-destinations") + .then() + .statusCode(400) + .body("__type", equalTo("BadRequestException")); + } + + @ParameterizedTest(name = "{0}") + @CsvSource({ + "empty-fields,arn::events:::event-bus/default", + "missing-partition,arn::events:us-east-1:000000000000:event-bus/default", + "missing-region,arn:aws:events::000000000000:event-bus/default", + "missing-account,arn:aws:events:us-east-1::event-bus/default", + "invalid-partition,arn:example:events:us-east-1:000000000000:event-bus/default", + "short-account,arn:aws:events:us-east-1:123:event-bus/default", + "mismatched-account,arn:aws:events:us-east-1:111111111111:event-bus/default", + "mismatched-region,arn:aws:events:us-west-2:000000000000:event-bus/default" + }) + @Order(21) + void createEventDestination_eventBridgeInvalidArnRejected(String caseName, String busArn) { + String cs = "v2-cs-ed-ebus"; + String destinationName = "ed-eb-invalid-create-" + caseName; + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "EventDestinationName": "%s", + "EventDestination": { + "Enabled": true, + "MatchingEventTypes": ["SEND"], + "EventBridgeDestination": {"EventBusArn": "%s"} + } + } + """.formatted(destinationName, busArn)) + .when() + .post("/v2/email/configuration-sets/" + cs + "/event-destinations") + .then() + .statusCode(400) + .body("__type", equalTo("BadRequestException")); + + given() + .header("Authorization", AUTH_HEADER) + .when() + .get("/v2/email/configuration-sets/" + cs + "/event-destinations") + .then() + .statusCode(200) + .body("EventDestinations.find { it.Name == '" + destinationName + "' }", nullValue()); + } + + @ParameterizedTest(name = "{0}") + @CsvSource({ + "empty-fields,arn::events:::event-bus/default", + "missing-partition,arn::events:us-east-1:000000000000:event-bus/default", + "missing-region,arn:aws:events::000000000000:event-bus/default", + "missing-account,arn:aws:events:us-east-1::event-bus/default", + "invalid-partition,arn:example:events:us-east-1:000000000000:event-bus/default", + "short-account,arn:aws:events:us-east-1:123:event-bus/default", + "mismatched-account,arn:aws:events:us-east-1:111111111111:event-bus/default", + "mismatched-region,arn:aws:events:us-west-2:000000000000:event-bus/default" + }) + @Order(22) + void updateEventDestination_eventBridgeInvalidArnRejectedAndDefaultPreserved( + String caseName, String busArn) { + String cs = "v2-cs-ed-ebus"; + String destinationName = "ed-eb-invalid-update-" + caseName; + String defaultBusArn = "arn:aws:events:us-east-1:000000000000:event-bus/default"; + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "EventDestinationName": "%s", + "EventDestination": { + "Enabled": true, + "MatchingEventTypes": ["SEND"], + "EventBridgeDestination": {"EventBusArn": "%s"} + } + } + """.formatted(destinationName, defaultBusArn)) + .when() + .post("/v2/email/configuration-sets/" + cs + "/event-destinations") + .then() + .statusCode(200); + + try { + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "EventDestination": { + "Enabled": false, + "MatchingEventTypes": ["BOUNCE"], + "EventBridgeDestination": {"EventBusArn": "%s"} + } + } + """.formatted(busArn)) + .when() + .put("/v2/email/configuration-sets/" + cs + "/event-destinations/" + destinationName) + .then() + .statusCode(400) + .body("__type", equalTo("BadRequestException")); + + given() + .header("Authorization", AUTH_HEADER) + .when() + .get("/v2/email/configuration-sets/" + cs + "/event-destinations") + .then() + .statusCode(200) + .body("EventDestinations.find { it.Name == '" + destinationName + "' }" + + ".Enabled", equalTo(true)) + .body("EventDestinations.find { it.Name == '" + destinationName + "' }" + + ".MatchingEventTypes", equalTo(List.of("SEND"))) + .body("EventDestinations.find { it.Name == '" + destinationName + "' }" + + ".EventBridgeDestination.EventBusArn", equalTo(defaultBusArn)); + } finally { + given() + .header("Authorization", AUTH_HEADER) + .when() + .delete("/v2/email/configuration-sets/" + cs + "/event-destinations/" + destinationName) + .then() + .statusCode(200); + } + } + + @Test + @Order(23) + void eventBridgeDefaultBusMatchingNonDefaultRequestScopeSucceeds() { + String cs = "v2-cs-ed-request-scope"; + given() + .contentType("application/json") + .header("Authorization", SCOPED_AUTH_HEADER) + .body("{\"ConfigurationSetName\": \"" + cs + "\"}") + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(200); + + given() + .contentType("application/json") + .header("Authorization", SCOPED_AUTH_HEADER) + .body(""" + { + "EventDestinationName": "ed-eb-scoped-default", + "EventDestination": { + "Enabled": true, + "MatchingEventTypes": ["SEND"], + "EventBridgeDestination": {"EventBusArn": "%s"} + } + } + """.formatted(SCOPED_DEFAULT_BUS_ARN)) + .when() + .post("/v2/email/configuration-sets/" + cs + "/event-destinations") + .then() + .statusCode(200); + + given() + .contentType("application/json") + .header("Authorization", SCOPED_AUTH_HEADER) + .body(""" + { + "EventDestination": { + "Enabled": false, + "MatchingEventTypes": ["BOUNCE"], + "EventBridgeDestination": {"EventBusArn": "%s"} + } + } + """.formatted(SCOPED_DEFAULT_BUS_ARN)) + .when() + .put("/v2/email/configuration-sets/" + cs + "/event-destinations/ed-eb-scoped-default") + .then() + .statusCode(200); + + given() + .header("Authorization", SCOPED_AUTH_HEADER) + .when() + .get("/v2/email/configuration-sets/" + cs + "/event-destinations") + .then() + .statusCode(200) + .body("EventDestinations", hasSize(1)) + .body("EventDestinations[0].Enabled", equalTo(false)) + .body("EventDestinations[0].MatchingEventTypes", equalTo(List.of("BOUNCE"))) + .body("EventDestinations[0].EventBridgeDestination.EventBusArn", + equalTo(SCOPED_DEFAULT_BUS_ARN)); + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesEventPublisherTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesEventPublisherTest.java index 19e6b13c2d..d2b30456f2 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesEventPublisherTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesEventPublisherTest.java @@ -21,6 +21,7 @@ import java.time.Instant; import java.util.List; import java.util.Map; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -103,6 +104,35 @@ class SesEventPublisherTest { assertEquals(arn, arr.get(0).asText()); } + @Test + void publishEventBridge_failedEntries_doesNotThrow() { + when(eventBridgeService.putEvents(any(), anyString())) + .thenReturn(new EventBridgeService.PutEventsResult(1, List.of(Map.of("ErrorCode", "InternalFailure")))); + + // A failed PutEvents entry is logged by the publisher but must not propagate + // back into the SES send path. + assertDoesNotThrow(() -> publisher.publish( + configurationSetWithEventBridgeDestination(DEFAULT_BUS_ARN), + SesRecipientEvent.of("SEND", Cause.SIMULATOR, List.of()), "msg-2", null, null, + "000000000000", "subj", List.of("to@example.com"), null, null, + List.of("to@example.com"), null, null, Instant.now(), "us-east-1")); + verify(eventBridgeService).putEvents(any(), anyString()); + } + + @Test + void publishEventBridge_putEventsThrows_doesNotThrow() { + when(eventBridgeService.putEvents(any(), anyString())) + .thenThrow(new RuntimeException("event bridge unavailable")); + + // Downstream delivery failures must never fail the email send. + assertDoesNotThrow(() -> publisher.publish( + configurationSetWithEventBridgeDestination(DEFAULT_BUS_ARN), + SesRecipientEvent.of("SEND", Cause.SIMULATOR, List.of()), "msg-3", null, null, + "000000000000", "subj", List.of("to@example.com"), null, null, + List.of("to@example.com"), null, null, Instant.now(), "us-east-1")); + verify(eventBridgeService).putEvents(any(), anyString()); + } + @SuppressWarnings("unchecked") private Map captureSingleEntry() { ArgumentCaptor>> captor = diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesEventPublishingV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesEventPublishingV2IntegrationTest.java index a74e52d4ed..00746a293c 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesEventPublishingV2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesEventPublishingV2IntegrationTest.java @@ -25,6 +25,7 @@ import java.util.List; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasKey; +import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.nullValue; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -957,7 +958,7 @@ class SesEventPublishingV2IntegrationTest { @Test @Order(22) - void eventBridge_unknownBusName_sendStillSucceeds() { + void eventBridge_unknownBusName_createRejected() { String csUnknown = "ses-events-eb-cs-unknown"; String bogusBusArn = "arn:aws:events:us-east-1:000000000000:event-bus/does-not-exist"; @@ -971,6 +972,8 @@ class SesEventPublishingV2IntegrationTest { .then() .statusCode(200); + // SES only supports the account default event bus, so a custom bus ARN is + // rejected at create time and never stored. given() .contentType("application/json") .header("Authorization", SES_AUTH) @@ -987,11 +990,16 @@ class SesEventPublishingV2IntegrationTest { .when() .post("/v2/email/configuration-sets/" + csUnknown + "/event-destinations") .then() - .statusCode(200); + .statusCode(400) + .body("__type", equalTo("BadRequestException")); - // putEvents records a failed entry for an unknown bus but does not throw — SES send - // must still return 200. - sendEmailToConfigSet(csUnknown, "recipient@example.com", "eb-unknown"); + given() + .header("Authorization", SES_AUTH) + .when() + .get("/v2/email/configuration-sets/" + csUnknown + "/event-destinations") + .then() + .statusCode(200) + .body("EventDestinations", hasSize(0)); } // ============================ CloudWatch =============================