diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesEventPublishingTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesEventPublishingTest.java index 97e7a2fc99..5a57416528 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesEventPublishingTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesEventPublishingTest.java @@ -33,6 +33,11 @@ import software.amazon.awssdk.services.sesv2.model.SendBulkEmailRequest; import software.amazon.awssdk.services.sesv2.model.SendEmailRequest; import software.amazon.awssdk.services.sesv2.model.SnsDestination; import software.amazon.awssdk.services.sesv2.model.Template; +import software.amazon.awssdk.services.sesv2.model.CloudWatchDestination; +import software.amazon.awssdk.services.sesv2.model.CloudWatchDimensionConfiguration; +import software.amazon.awssdk.services.sesv2.model.DimensionValueSource; +import software.amazon.awssdk.services.sesv2.model.EventBridgeDestination; +import software.amazon.awssdk.services.sesv2.model.KinesisFirehoseDestination; import software.amazon.awssdk.services.sns.SnsClient; import software.amazon.awssdk.services.sns.model.CreateTopicRequest; @@ -50,18 +55,48 @@ import software.amazon.awssdk.services.sqs.model.QueueAttributeName; import software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest; import software.amazon.awssdk.services.sqs.model.ReceiveMessageResponse; +import software.amazon.awssdk.services.firehose.FirehoseClient; +import software.amazon.awssdk.services.firehose.model.CreateDeliveryStreamRequest; +import software.amazon.awssdk.services.firehose.model.DeleteDeliveryStreamRequest; +import software.amazon.awssdk.services.firehose.model.S3DestinationConfiguration; + +import software.amazon.awssdk.services.s3.S3Client; +import software.amazon.awssdk.services.s3.model.CreateBucketRequest; +import software.amazon.awssdk.services.s3.model.DeleteBucketRequest; +import software.amazon.awssdk.services.s3.model.DeleteObjectsRequest; +import software.amazon.awssdk.services.s3.model.GetObjectRequest; +import software.amazon.awssdk.services.s3.model.ListObjectsV2Request; +import software.amazon.awssdk.services.s3.model.ListObjectsV2Response; +import software.amazon.awssdk.services.s3.model.ObjectIdentifier; +import software.amazon.awssdk.services.s3.model.S3Object; + +import software.amazon.awssdk.services.eventbridge.EventBridgeClient; +import software.amazon.awssdk.services.eventbridge.model.DeleteRuleRequest; +import software.amazon.awssdk.services.eventbridge.model.PutRuleRequest; +import software.amazon.awssdk.services.eventbridge.model.PutTargetsRequest; +import software.amazon.awssdk.services.eventbridge.model.RemoveTargetsRequest; +import software.amazon.awssdk.services.eventbridge.model.Target; + +import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; +import software.amazon.awssdk.services.cloudwatch.model.ListMetricsRequest; +import software.amazon.awssdk.services.cloudwatch.model.Metric; + +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; import static org.assertj.core.api.Assertions.assertThat; /** * SES event-publishing compatibility tests using the AWS SDK for Java v2. * - * Drives the entire flow through real SDK clients (SesV2Client / SnsClient / - * SqsClient): create a configuration set + SNS event destination, send via - * SesV2Client.SendEmail with ConfigurationSetName, then receive the resulting - * SES event JSON from a subscribed SQS queue and assert the AWS-format shape. + * Drives the entire flow through real SDK clients (SesV2Client + SnsClient / + * FirehoseClient + S3Client / EventBridgeClient + SqsClient / CloudWatchClient): + * create a configuration set per destination type, send via SesV2Client.SendEmail + * with the ConfigurationSetName, then assert the AWS-format event JSON shape at + * each destination's natural sink — SNS-subscribed SQS, S3 (Firehose flush), + * EventBridge rule's SQS target, and CloudWatch metric registration. */ @DisplayName("SES Event Publishing") @TestMethodOrder(MethodOrderer.OrderAnnotation.class) @@ -69,24 +104,48 @@ class SesEventPublishingTest { private static final ObjectMapper MAPPER = new ObjectMapper(); private static final String EVENT_DEST_NAME = "ed-sns"; + private static final String EVENT_DEST_NAME_FIREHOSE = "ed-firehose"; + private static final String EVENT_DEST_NAME_EB = "ed-eventbridge"; + private static final String EVENT_DEST_NAME_CW = "ed-cloudwatch"; private static SesV2Client ses; private static SnsClient sns; private static SqsClient sqs; + private static FirehoseClient firehose; + private static S3Client s3; + private static EventBridgeClient eventBridge; + private static CloudWatchClient cloudWatch; private static String csName; + private static String csNameFirehose; + private static String csNameEB; + private static String csNameCW; private static String identity; private static String queueUrl; private static String queueArn; private static String topicArn; private static String subscriptionArn; + private static String firehoseStreamName; + private static String firehoseStreamArn; + private static String firehoseBucket; + private static String ebQueueUrl; + private static String ebQueueArn; + private static String ebRuleName; + private static String ebBusArn; @BeforeAll static void setup() { ses = TestFixtures.sesV2Client(); sns = TestFixtures.snsClient(); sqs = TestFixtures.sqsClient(); + firehose = TestFixtures.firehoseClient(); + s3 = TestFixtures.s3Client(); + eventBridge = TestFixtures.eventBridgeClient(); + cloudWatch = TestFixtures.cloudWatchClient(); String suffix = TestFixtures.uniqueName(); csName = "sdk-evt-cs-" + suffix; + csNameFirehose = "sdk-evt-cs-fh-" + suffix; + csNameEB = "sdk-evt-cs-eb-" + suffix; + csNameCW = "sdk-evt-cs-cw-" + suffix; identity = "sdk-evt-from-" + suffix + "@floci.test"; queueUrl = sqs.createQueue(CreateQueueRequest.builder() @@ -129,22 +188,122 @@ class SesEventPublishingTest { .snsDestination(SnsDestination.builder().topicArn(topicArn).build()) .build()) .build()); + + // Firehose destination plumbing: S3 bucket + delivery stream + CS + Firehose dest. + firehoseBucket = "sdk-evt-fh-bucket-" + suffix; + s3.createBucket(CreateBucketRequest.builder().bucket(firehoseBucket).build()); + firehoseStreamName = "sdk-evt-fh-stream-" + suffix; + firehoseStreamArn = firehose.createDeliveryStream(CreateDeliveryStreamRequest.builder() + .deliveryStreamName(firehoseStreamName) + .s3DestinationConfiguration(S3DestinationConfiguration.builder() + .bucketARN("arn:aws:s3:::" + firehoseBucket) + .roleARN("arn:aws:iam::000000000000:role/sdk-evt-fh-role") + .build()) + .build()) + .deliveryStreamARN(); + ses.createConfigurationSet(CreateConfigurationSetRequest.builder() + .configurationSetName(csNameFirehose) + .build()); + ses.createConfigurationSetEventDestination(CreateConfigurationSetEventDestinationRequest.builder() + .configurationSetName(csNameFirehose) + .eventDestinationName(EVENT_DEST_NAME_FIREHOSE) + .eventDestination(EventDestinationDefinition.builder() + .enabled(true) + .matchingEventTypes(EventType.SEND) + .kinesisFirehoseDestination(KinesisFirehoseDestination.builder() + .iamRoleArn("arn:aws:iam::000000000000:role/sdk-evt-fh-role") + .deliveryStreamArn(firehoseStreamArn) + .build()) + .build()) + .build()); + + // EventBridge destination plumbing: SQS target + rule on default bus + CS + EB dest. + ebQueueUrl = sqs.createQueue(CreateQueueRequest.builder() + .queueName("sdk-evt-eb-q-" + suffix) + .build()) + .queueUrl(); + ebQueueArn = sqs.getQueueAttributes(GetQueueAttributesRequest.builder() + .queueUrl(ebQueueUrl) + .attributeNames(QueueAttributeName.QUEUE_ARN) + .build()) + .attributes().get(QueueAttributeName.QUEUE_ARN); + ebRuleName = "sdk-evt-eb-rule-" + suffix; + eventBridge.putRule(PutRuleRequest.builder() + .name(ebRuleName) + .eventPattern("{\"source\":[\"aws.ses\"]}") + .build()); + eventBridge.putTargets(PutTargetsRequest.builder() + .rule(ebRuleName) + .targets(Target.builder().id("1").arn(ebQueueArn).build()) + .build()); + ebBusArn = "arn:aws:events:us-east-1:000000000000:event-bus/default"; + ses.createConfigurationSet(CreateConfigurationSetRequest.builder() + .configurationSetName(csNameEB) + .build()); + ses.createConfigurationSetEventDestination(CreateConfigurationSetEventDestinationRequest.builder() + .configurationSetName(csNameEB) + .eventDestinationName(EVENT_DEST_NAME_EB) + .eventDestination(EventDestinationDefinition.builder() + .enabled(true) + .matchingEventTypes(EventType.SEND, EventType.DELIVERY) + .eventBridgeDestination(EventBridgeDestination.builder() + .eventBusArn(ebBusArn) + .build()) + .build()) + .build()); + + // CloudWatch destination plumbing: CS + CW dest with three dimension configs. + ses.createConfigurationSet(CreateConfigurationSetRequest.builder() + .configurationSetName(csNameCW) + .build()); + ses.createConfigurationSetEventDestination(CreateConfigurationSetEventDestinationRequest.builder() + .configurationSetName(csNameCW) + .eventDestinationName(EVENT_DEST_NAME_CW) + .eventDestination(EventDestinationDefinition.builder() + .enabled(true) + .matchingEventTypes(EventType.SEND, EventType.DELIVERY) + .cloudWatchDestination(CloudWatchDestination.builder() + .dimensionConfigurations( + CloudWatchDimensionConfiguration.builder() + .dimensionName("ses:configuration-set") + .dimensionValueSource(DimensionValueSource.MESSAGE_TAG) + .defaultDimensionValue("unknown") + .build(), + CloudWatchDimensionConfiguration.builder() + .dimensionName("campaign") + .dimensionValueSource(DimensionValueSource.MESSAGE_TAG) + .defaultDimensionValue("default-campaign") + .build(), + CloudWatchDimensionConfiguration.builder() + .dimensionName("X-Custom-Header") + .dimensionValueSource(DimensionValueSource.EMAIL_HEADER) + .defaultDimensionValue("default-header") + .build()) + .build()) + .build()) + .build()); } @AfterAll static void cleanup() { if (ses != null) { - try { - ses.deleteConfigurationSetEventDestination(DeleteConfigurationSetEventDestinationRequest.builder() - .configurationSetName(csName) - .eventDestinationName(EVENT_DEST_NAME) - .build()); - } catch (Exception ignored) {} - try { - ses.deleteConfigurationSet(DeleteConfigurationSetRequest.builder() - .configurationSetName(csName) - .build()); - } catch (Exception ignored) {} + for (String[] pair : new String[][]{ + {csName, EVENT_DEST_NAME}, + {csNameFirehose, EVENT_DEST_NAME_FIREHOSE}, + {csNameEB, EVENT_DEST_NAME_EB}, + {csNameCW, EVENT_DEST_NAME_CW}}) { + try { + ses.deleteConfigurationSetEventDestination(DeleteConfigurationSetEventDestinationRequest.builder() + .configurationSetName(pair[0]) + .eventDestinationName(pair[1]) + .build()); + } catch (Exception ignored) {} + try { + ses.deleteConfigurationSet(DeleteConfigurationSetRequest.builder() + .configurationSetName(pair[0]) + .build()); + } catch (Exception ignored) {} + } try { ses.deleteEmailIdentity(DeleteEmailIdentityRequest.builder() .emailIdentity(identity) @@ -161,12 +320,53 @@ class SesEventPublishingTest { } catch (Exception ignored) {} sns.close(); } + if (eventBridge != null) { + try { + eventBridge.removeTargets(RemoveTargetsRequest.builder() + .rule(ebRuleName).ids("1").build()); + } catch (Exception ignored) {} + try { + eventBridge.deleteRule(DeleteRuleRequest.builder().name(ebRuleName).build()); + } catch (Exception ignored) {} + eventBridge.close(); + } if (sqs != null) { try { sqs.deleteQueue(DeleteQueueRequest.builder().queueUrl(queueUrl).build()); } catch (Exception ignored) {} + try { + sqs.deleteQueue(DeleteQueueRequest.builder().queueUrl(ebQueueUrl).build()); + } catch (Exception ignored) {} sqs.close(); } + if (firehose != null) { + try { + firehose.deleteDeliveryStream(DeleteDeliveryStreamRequest.builder() + .deliveryStreamName(firehoseStreamName).build()); + } catch (Exception ignored) {} + firehose.close(); + } + if (s3 != null) { + try { + ListObjectsV2Response listed = s3.listObjectsV2(ListObjectsV2Request.builder() + .bucket(firehoseBucket).build()); + if (!listed.contents().isEmpty()) { + s3.deleteObjects(DeleteObjectsRequest.builder() + .bucket(firehoseBucket) + .delete(d -> d.objects(listed.contents().stream() + .map(o -> ObjectIdentifier.builder().key(o.key()).build()) + .collect(Collectors.toList()))) + .build()); + } + } catch (Exception ignored) {} + try { + s3.deleteBucket(DeleteBucketRequest.builder().bucket(firehoseBucket).build()); + } catch (Exception ignored) {} + s3.close(); + } + if (cloudWatch != null) { + cloudWatch.close(); + } } @Test @@ -275,6 +475,204 @@ class SesEventPublishingTest { .isEqualTo("override"); } + @Test + @Order(5) + @DisplayName("Firehose destination: 5 sends trigger auto-flush to S3 as NDJSON") + void firehoseDestination_fiveSends_triggerAutoFlushToS3() throws Exception { + for (int i = 0; i < 5; i++) { + ses.sendEmail(SendEmailRequest.builder() + .fromEmailAddress(identity) + .destination(Destination.builder() + .toAddresses("fh-recipient" + i + "@example.com") + .build()) + .configurationSetName(csNameFirehose) + .content(EmailContent.builder() + .simple(Message.builder() + .subject(Content.builder().data("fh-evt-" + i).build()) + .body(Body.builder() + .text(Content.builder().data("hi").build()) + .build()) + .build()) + .build()) + .build()); + } + + ListObjectsV2Response listed = s3.listObjectsV2(ListObjectsV2Request.builder() + .bucket(firehoseBucket) + .prefix(firehoseStreamName + "/") + .build()); + assertThat(listed.contents()) + .as("Firehose should have flushed exactly one S3 object after 5 records") + .hasSize(1); + + S3Object obj = listed.contents().get(0); + String body = new String(s3.getObject(GetObjectRequest.builder() + .bucket(firehoseBucket).key(obj.key()).build()) + .readAllBytes(), StandardCharsets.UTF_8); + String[] lines = body.split("\\R"); + assertThat(lines).hasSize(5); + for (String line : lines) { + JsonNode event = MAPPER.readTree(line); + assertThat(event.path("eventType").asText()).isEqualTo("Send"); + assertThat(event.path("mail").path("source").asText()).isEqualTo(identity); + assertThat(event.path("mail").path("tags").path("ses:configuration-set").get(0).asText()) + .isEqualTo(csNameFirehose); + } + } + + @Test + @Order(6) + @DisplayName("EventBridge destination: regular recipient routes Email Send to SQS via rule") + void eventBridgeDestination_regularRecipient_routesEmailSendToSqs() throws Exception { + drainEbQueue(); + ses.sendEmail(SendEmailRequest.builder() + .fromEmailAddress(identity) + .destination(Destination.builder().toAddresses("eb-recipient@example.com").build()) + .configurationSetName(csNameEB) + .content(EmailContent.builder() + .simple(Message.builder() + .subject(Content.builder().data("eb-evt").build()) + .body(Body.builder() + .text(Content.builder().data("hi").build()) + .build()) + .build()) + .build()) + .build()); + + List envelopes = receiveEbEvents(1); + assertThat(envelopes).hasSize(1); + JsonNode env = envelopes.get(0); + assertThat(env.path("source").asText()).isEqualTo("aws.ses"); + assertThat(env.path("detail-type").asText()).isEqualTo("Email Sent"); + JsonNode detail = env.path("detail"); + assertThat(detail.path("eventType").asText()).isEqualTo("Send"); + assertThat(detail.path("mail").path("source").asText()).isEqualTo(identity); + assertThat(detail.path("mail").path("tags").path("ses:configuration-set").get(0).asText()) + .isEqualTo(csNameEB); + } + + @Test + @Order(7) + @DisplayName("EventBridge destination: success simulator routes both Email Send and Email Delivery") + void eventBridgeDestination_successSimulator_routesSendAndDelivery() throws Exception { + drainEbQueue(); + ses.sendEmail(SendEmailRequest.builder() + .fromEmailAddress(identity) + .destination(Destination.builder() + .toAddresses("success@simulator.amazonses.com").build()) + .configurationSetName(csNameEB) + .content(EmailContent.builder() + .simple(Message.builder() + .subject(Content.builder().data("eb-evt-success").build()) + .body(Body.builder() + .text(Content.builder().data("hi").build()) + .build()) + .build()) + .build()) + .build()); + + List envelopes = receiveEbEvents(2); + assertThat(envelopes).hasSize(2); + assertThat(envelopes).anyMatch(e -> "Email Sent".equals(e.path("detail-type").asText())); + assertThat(envelopes).anyMatch(e -> "Email Delivered".equals(e.path("detail-type").asText())); + } + + @Test + @Order(8) + @DisplayName("CloudWatch destination: send registers AWS/SES Send metric with resolved dimensions") + void cloudWatchDestination_registersSendMetricWithResolvedDimensions() { + ses.sendEmail(SendEmailRequest.builder() + .fromEmailAddress(identity) + .destination(Destination.builder().toAddresses("cw-recipient@example.com").build()) + .configurationSetName(csNameCW) + .emailTags(MessageTag.builder().name("campaign").value("launch").build()) + .content(EmailContent.builder() + .simple(Message.builder() + .subject(Content.builder().data("cw-evt").build()) + .body(Body.builder() + .text(Content.builder().data("hi").build()) + .build()) + .headers(MessageHeader.builder() + .name("X-Custom-Header").value("header-value").build()) + .build()) + .build()) + .build()); + + List sendMetrics = cloudWatch.listMetrics(ListMetricsRequest.builder() + .namespace("AWS/SES") + .metricName("Send") + .build()) + .metrics(); + assertThat(sendMetrics) + .as("expected at least one Send metric in AWS/SES") + .isNotEmpty(); + assertThat(sendMetrics).anyMatch(m -> + m.dimensions().stream() + .anyMatch(d -> "ses:configuration-set".equals(d.name()) + && csNameCW.equals(d.value())) + && m.dimensions().stream() + .anyMatch(d -> "campaign".equals(d.name()) && "launch".equals(d.value())) + && m.dimensions().stream() + .anyMatch(d -> "X-Custom-Header".equals(d.name()) + && "header-value".equals(d.value()))); + } + + private void drainEbQueue() { + for (int i = 0; i < 5; i++) { + ReceiveMessageResponse r = sqs.receiveMessage(ReceiveMessageRequest.builder() + .queueUrl(ebQueueUrl) + .maxNumberOfMessages(10) + .waitTimeSeconds(0) + .build()); + if (r.messages().isEmpty()) { + return; + } + List entries = new ArrayList<>(); + for (int j = 0; j < r.messages().size(); j++) { + entries.add(DeleteMessageBatchRequestEntry.builder() + .id("m" + j) + .receiptHandle(r.messages().get(j).receiptHandle()) + .build()); + } + sqs.deleteMessageBatch(DeleteMessageBatchRequest.builder() + .queueUrl(ebQueueUrl) + .entries(entries) + .build()); + } + } + + private List receiveEbEvents(int expectedAtLeast) throws Exception { + List events = new ArrayList<>(); + for (int attempt = 0; attempt < 10; attempt++) { + if (events.size() >= expectedAtLeast) { + break; + } + ReceiveMessageResponse r = sqs.receiveMessage(ReceiveMessageRequest.builder() + .queueUrl(ebQueueUrl) + .maxNumberOfMessages(10) + .waitTimeSeconds(1) + .build()); + if (r.messages().isEmpty()) { + continue; + } + for (var m : r.messages()) { + events.add(MAPPER.readTree(m.body())); + } + List entries = new ArrayList<>(); + for (int j = 0; j < r.messages().size(); j++) { + entries.add(DeleteMessageBatchRequestEntry.builder() + .id("m" + j) + .receiptHandle(r.messages().get(j).receiptHandle()) + .build()); + } + sqs.deleteMessageBatch(DeleteMessageBatchRequest.builder() + .queueUrl(ebQueueUrl) + .entries(entries) + .build()); + } + return events; + } + private void sendEmailTo(String to) { ses.sendEmail(SendEmailRequest.builder() .fromEmailAddress(identity) diff --git a/docs/services/ses.md b/docs/services/ses.md index 6036fa6774..2ab0972fd0 100644 --- a/docs/services/ses.md +++ b/docs/services/ses.md @@ -203,7 +203,7 @@ Alongside the classic Query API, Floci implements a subset of the SES v2 REST JS Configuration set event destinations are stored as configuration. The target is not validated for existence; missing targets cause Floci to log a warning and skip that destination. Each event destination must specify exactly one destination type and at least one matching event type. A CloudWatch destination requires a non-empty dimension configuration list, and a Pinpoint destination requires an application ARN. -Floci publishes SES events to `SnsDestination` only in this version (`KinesisFirehoseDestination` / `EventBridgeDestination` / `CloudWatchDestination` / `PinpointDestination` log a warning and skip). The published payload matches the AWS SES SNS notification format with an outer `eventType` plus `mail` and event-type-specific blocks. Events fire whenever a configuration set has at least one event destination matching the event type — disable per-destination via `EventDestination.Enabled=false`, or remove the destination entirely. +Floci publishes SES events to `SnsDestination`, `KinesisFirehoseDestination`, `EventBridgeDestination`, and `CloudWatchDestination`. `PinpointDestination` logs a warning and skips. The published payload follows the [AWS SES SNS notification format](https://docs.aws.amazon.com/ses/latest/dg/event-publishing-retrieving-sns-contents.html) with an outer `eventType` plus `mail` and event-type-specific blocks. Events fire whenever a configuration set has at least one event destination matching the event type — disable per-destination via `EventDestination.Enabled=false`, or remove the destination entirely. Floci recognises the AWS [mailbox simulator addresses](https://docs.aws.amazon.com/ses/latest/dg/send-an-email-from-console.html#send-email-simulator) for deterministic event-type emission: diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesEventPayload.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesEventPayload.java index 0948fcb4c5..4fd6629162 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesEventPayload.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesEventPayload.java @@ -219,7 +219,7 @@ final class SesEventPayload { } } - private static String eventTypeLabel(String eventType) { + static String eventTypeLabel(String eventType) { return switch (eventType) { case "SEND" -> "Send"; case "REJECT" -> "Reject"; @@ -235,6 +235,58 @@ final class SesEventPayload { }; } + /** + * Returns the CloudWatch {@code MetricName} value AWS SES uses for an event. + * Mostly matches {@link #eventTypeLabel}, but at least one event type has a + * different spelling on CloudWatch than on the SNS payload's {@code eventType} + * field, and they must not be unified through {@code eventTypeLabel} or the SNS + * payload regresses to a non-AWS value: + * - {@code RENDERING_FAILURE} → CW {@code "RenderingFailure"} (no space) + * vs SNS {@code "Rendering Failure"} (with space) — verified against real + * AWS via a CW event destination + a templated send with a missing + * {@code TemplateData} key. + * {@code DELIVERY_DELAY} is not directly observed yet (sandbox cannot easily + * trigger SES backoff); kept as {@code "DeliveryDelay"} pending verification. + */ + static String cloudWatchMetricName(String eventType) { + return switch (eventType) { + case "SEND" -> "Send"; + case "REJECT" -> "Reject"; + case "BOUNCE" -> "Bounce"; + case "COMPLAINT" -> "Complaint"; + case "DELIVERY" -> "Delivery"; + case "OPEN" -> "Open"; + case "CLICK" -> "Click"; + case "RENDERING_FAILURE" -> "RenderingFailure"; + case "DELIVERY_DELAY" -> "DeliveryDelay"; + case "SUBSCRIPTION" -> "Subscription"; + default -> eventType; + }; + } + + /** + * Returns the EventBridge {@code detail-type} value for a SES event. AWS uses a + * separate past-tense vocabulary on EventBridge (e.g. {@code Email Sent}) that + * differs from the SNS notification {@code eventType} value (e.g. {@code Send}) + * — a rule pattern keyed on the EventBridge value will not match the SNS value. + * Reference: https://docs.aws.amazon.com/eventbridge/latest/ref/events-ref-ses.html + */ + static String eventBridgeDetailType(String eventType) { + return switch (eventType) { + case "SEND" -> "Email Sent"; + case "REJECT" -> "Email Rejected"; + case "BOUNCE" -> "Email Bounced"; + case "COMPLAINT" -> "Email Complaint Received"; + case "DELIVERY" -> "Email Delivered"; + case "OPEN" -> "Email Opened"; + case "CLICK" -> "Email Clicked"; + case "RENDERING_FAILURE" -> "Email Rendering Failed"; + case "DELIVERY_DELAY" -> "Email Delivery Delayed"; + case "SUBSCRIPTION" -> "Email Subscribed"; + default -> "Email " + eventType; + }; + } + private static String blockName(String eventType) { return switch (eventType) { case "SEND" -> "send"; diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesEventPublisher.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesEventPublisher.java index f561d52c20..34c7a36cb2 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesEventPublisher.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesEventPublisher.java @@ -1,9 +1,18 @@ package io.github.hectorvent.floci.services.ses; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsService; +import io.github.hectorvent.floci.services.cloudwatch.metrics.model.Dimension; +import io.github.hectorvent.floci.services.cloudwatch.metrics.model.MetricDatum; +import io.github.hectorvent.floci.services.eventbridge.EventBridgeService; +import io.github.hectorvent.floci.services.firehose.FirehoseService; +import io.github.hectorvent.floci.services.firehose.model.Record; +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.ConfigurationSet; import io.github.hectorvent.floci.services.ses.model.EventDestination; import io.github.hectorvent.floci.services.ses.model.MessageHeader; @@ -13,25 +22,42 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import org.jboss.logging.Logger; +import java.nio.charset.StandardCharsets; import java.time.Instant; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * Publishes SES event-publishing notifications to the event destinations attached - * to a {@link ConfigurationSet}. This MVP only delivers to {@code SnsDestination}; - * Firehose, EventBridge, CloudWatch, and Pinpoint destinations are logged and skipped. + * to a {@link ConfigurationSet}. {@code SnsDestination}, + * {@code KinesisFirehoseDestination}, {@code EventBridgeDestination}, and + * {@code CloudWatchDestination} are delivered live; Pinpoint destinations are + * logged and skipped. */ @ApplicationScoped public class SesEventPublisher { private static final Logger LOG = Logger.getLogger(SesEventPublisher.class); + private static final String SES_METRIC_NAMESPACE = "AWS/SES"; + private final SnsService snsService; + private final FirehoseService firehoseService; + private final EventBridgeService eventBridgeService; + private final CloudWatchMetricsService cloudWatchMetricsService; private final ObjectMapper objectMapper; @Inject - public SesEventPublisher(SnsService snsService, ObjectMapper objectMapper) { + public SesEventPublisher(SnsService snsService, FirehoseService firehoseService, + EventBridgeService eventBridgeService, + CloudWatchMetricsService cloudWatchMetricsService, + ObjectMapper objectMapper) { this.snsService = snsService; + this.firehoseService = firehoseService; + this.eventBridgeService = eventBridgeService; + this.cloudWatchMetricsService = cloudWatchMetricsService; this.objectMapper = objectMapper; } @@ -61,7 +87,8 @@ public class SesEventPublisher { continue; } try { - dispatch(ed, eventType, payloadJson, defaultRegion); + dispatch(ed, eventType, payloadJson, sourceArn, configurationSet.getName(), + emailTags, additionalHeaders, defaultRegion); } catch (Exception e) { LOG.warnf(e, "Failed to publish SES event %s to destination %s", eventType, ed.getName()); @@ -69,7 +96,10 @@ public class SesEventPublisher { } } - private void dispatch(EventDestination ed, String eventType, String payloadJson, String defaultRegion) { + private void dispatch(EventDestination ed, String eventType, String payloadJson, + String sourceArn, String configurationSetName, + List emailTags, List additionalHeaders, + String defaultRegion) { if (ed.getSnsDestination() != null) { String topicArn = ed.getSnsDestination().getTopicArn(); if (topicArn == null || topicArn.isBlank()) { @@ -85,11 +115,29 @@ public class SesEventPublisher { ed.getName(), eventType); return; } - if (ed.getKinesisFirehoseDestination() != null - || ed.getEventBridgeDestination() != null - || ed.getCloudWatchDestination() != null) { - LOG.warnf("SES destination %s: event %s not delivered (only SNS publishing is implemented).", - ed.getName(), eventType); + if (ed.getKinesisFirehoseDestination() != null) { + String streamArn = ed.getKinesisFirehoseDestination().getDeliveryStreamArn(); + if (streamArn == null || streamArn.isBlank()) { + LOG.warnf("SES Firehose destination %s: event %s not delivered (DeliveryStreamArn is missing).", + ed.getName(), eventType); + return; + } + publishFirehose(streamArn, payloadJson, ed.getName(), eventType); + return; + } + if (ed.getEventBridgeDestination() != null) { + String busArn = ed.getEventBridgeDestination().getEventBusArn(); + if (busArn == null || busArn.isBlank()) { + LOG.warnf("SES EventBridge destination %s: event %s not delivered (EventBusArn is missing).", + ed.getName(), eventType); + return; + } + publishEventBridge(busArn, payloadJson, eventType, sourceArn, ed.getName(), defaultRegion); + return; + } + if (ed.getCloudWatchDestination() != null) { + publishCloudWatch(ed.getCloudWatchDestination(), eventType, configurationSetName, + emailTags, additionalHeaders, defaultRegion); return; } LOG.warnf("SES destination %s: event %s not delivered (no destination target configured).", @@ -104,4 +152,173 @@ public class SesEventPublisher { LOG.warnf(e, "SES event publish to SNS topic %s skipped", topicArn); } } + + private void publishFirehose(String streamArn, String payloadJson, + String destinationName, String eventType) { + String streamName = extractArnResourceName(streamArn); + if (streamName == null) { + LOG.warnf("SES Firehose destination %s: event %s not delivered " + + "(DeliveryStreamArn %s is malformed).", + destinationName, eventType, streamArn); + return; + } + try { + Record record = new Record(); + record.setData(payloadJson.getBytes(StandardCharsets.UTF_8)); + firehoseService.putRecord(streamName, record); + } catch (Exception e) { + LOG.warnf(e, "SES event publish to Firehose stream %s skipped", streamName); + } + } + + private void publishEventBridge(String busArn, String payloadJson, String eventType, + String sourceArn, String destinationName, String defaultRegion) { + String busName = extractArnResourceName(busArn); + if (busName == null) { + LOG.warnf("SES EventBridge destination %s: event %s not delivered " + + "(EventBusArn %s is malformed).", + destinationName, eventType, busArn); + return; + } + String region = AwsArnUtils.regionOrDefault(busArn, defaultRegion); + try { + Map entry = new HashMap<>(); + entry.put("Source", "aws.ses"); + entry.put("DetailType", SesEventPayload.eventBridgeDetailType(eventType)); + entry.put("Detail", payloadJson); + entry.put("EventBusName", busName); + entry.put("Region", region); + // Always emit Resources (possibly empty) — EventBridgeService.matchesPattern + // casts entry.get("Resources") to ArrayNode unconditionally for any rule with + // a "resources" filter; an absent key NPEs and silently turns the rule into + // a no-match. + ArrayNode resources = objectMapper.createArrayNode(); + if (sourceArn != null && !sourceArn.isBlank()) { + resources.add(sourceArn); + } + entry.put("Resources", resources); + EventBridgeService.PutEventsResult result = + eventBridgeService.putEvents(List.of(entry), region); + if (result.failedCount() > 0) { + LOG.warnf("SES event publish to EventBridge bus %s skipped: %s", + busName, result.entries()); + } + } catch (Exception e) { + LOG.warnf(e, "SES event publish to EventBridge bus %s skipped", busName); + } + } + + private void publishCloudWatch(CloudWatchDestination dest, String eventType, + String configurationSetName, + List emailTags, + List additionalHeaders, + String defaultRegion) { + List dimensions = new ArrayList<>(); + if (dest.getDimensionConfigurations() != null) { + for (CloudWatchDimensionConfiguration cfg : dest.getDimensionConfigurations()) { + if (cfg == null || cfg.getDimensionName() == null + || cfg.getDimensionName().isBlank()) { + continue; + } + String value = resolveCloudWatchDimensionValue(cfg, configurationSetName, + emailTags, additionalHeaders); + if (value == null || value.isBlank()) { + continue; + } + dimensions.add(new Dimension(cfg.getDimensionName(), value)); + } + } + MetricDatum datum = new MetricDatum(); + datum.setNamespace(SES_METRIC_NAMESPACE); + datum.setMetricName(SesEventPayload.cloudWatchMetricName(eventType)); + datum.setValue(1.0); + datum.setUnit("Count"); + datum.setDimensions(dimensions); + // Leave timestamp unset: CloudWatchMetricsService.putMetricData defaults a zero + // timestamp to Instant.now().getEpochSecond() (epoch seconds, not milliseconds). + try { + cloudWatchMetricsService.putMetricData(SES_METRIC_NAMESPACE, List.of(datum), + defaultRegion); + } catch (Exception e) { + LOG.warnf(e, "SES event publish to CloudWatch (event %s) skipped", eventType); + } + } + + /** + * Resolves the value of a CloudWatch dimension for a given SES event according to + * {@code DimensionValueSource}. {@code messageTag} consults the user-supplied + * {@code emailTags} plus the auto-added {@code ses:configuration-set} tag; + * {@code emailHeader} consults the email's additional headers (case-insensitive + * lookup per RFC 5322). When the source can't yield a value, falls back to + * {@code DefaultDimensionValue}. {@code linkTag} and unknown sources are not + * resolved against any real input here and immediately fall back to the default. + * + *

The V1 SES API documents these enum values as camelCase + * ({@code messageTag} / {@code emailHeader} / {@code linkTag}); the V2 SES API + * documents the same values as SCREAMING_SNAKE + * ({@code MESSAGE_TAG} / {@code EMAIL_HEADER} / {@code LINK_TAG}) and the AWS SDK + * for Java v2 marshals exactly those constant names on the wire. Both forms are + * accepted here so callers on either protocol land on the same resolution path. + */ + private static String resolveCloudWatchDimensionValue(CloudWatchDimensionConfiguration cfg, + String configurationSetName, + List emailTags, + List additionalHeaders) { + String source = cfg.getDimensionValueSource(); + String name = cfg.getDimensionName(); + String defaultValue = cfg.getDefaultDimensionValue(); + if (isMessageTagSource(source)) { + if ("ses:configuration-set".equals(name)) { + return configurationSetName; + } + if (emailTags != null) { + for (MessageTag t : emailTags) { + if (t != null && name.equals(t.name()) + && t.value() != null && !t.value().isBlank()) { + return t.value(); + } + } + } + return defaultValue; + } + if (isEmailHeaderSource(source)) { + if (additionalHeaders != null) { + for (MessageHeader h : additionalHeaders) { + if (h != null && name.equalsIgnoreCase(h.name()) + && h.value() != null && !h.value().isBlank()) { + return h.value(); + } + } + } + return defaultValue; + } + return defaultValue; + } + + private static boolean isMessageTagSource(String source) { + return "messageTag".equalsIgnoreCase(source) || "MESSAGE_TAG".equalsIgnoreCase(source); + } + + private static boolean isEmailHeaderSource(String source) { + return "emailHeader".equalsIgnoreCase(source) || "EMAIL_HEADER".equalsIgnoreCase(source); + } + + /** + * Extracts the resource name from an AWS ARN's resource segment that follows the + * {@code /} convention (e.g., {@code deliverystream/my-stream}, + * {@code event-bus/my-bus}). Returns {@code null} for a malformed ARN or a + * resource segment with no slash or an empty name after the slash. + */ + private static String extractArnResourceName(String arn) { + try { + String resource = AwsArnUtils.parse(arn).resource(); + int slash = resource.indexOf('/'); + if (slash < 0 || slash == resource.length() - 1) { + return null; + } + return resource.substring(slash + 1); + } catch (IllegalArgumentException e) { + return null; + } + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesEventPayloadTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesEventPayloadTest.java index 21f1bf99cb..cb3b41c108 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesEventPayloadTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesEventPayloadTest.java @@ -308,4 +308,51 @@ class SesEventPayloadTest { assertEquals("Reject", node.get("eventType").asText()); assertNotNull(node.get("reject").get("reason")); } + + /** + * AWS CloudWatch metric names for SES events are mostly the same as the SNS + * {@code eventType} value, except {@code RENDERING_FAILURE} is emitted as + * {@code RenderingFailure} (no space) on CW but {@code Rendering Failure} + * (with space) on SNS — verified directly against real AWS by sending a + * templated email with a missing {@code TemplateData} key through a CS with a + * CloudWatch event destination, then capturing the new metric name from + * {@code aws cloudwatch list-metrics --namespace AWS/SES}. Pin both spellings + * so a future cleanup of either accessor cannot silently regress the other. + */ + @Test + void cloudWatchMetricName_matchesAwsCwSpelling() { + assertEquals("Send", SesEventPayload.cloudWatchMetricName("SEND")); + assertEquals("Reject", SesEventPayload.cloudWatchMetricName("REJECT")); + assertEquals("Bounce", SesEventPayload.cloudWatchMetricName("BOUNCE")); + assertEquals("Complaint", SesEventPayload.cloudWatchMetricName("COMPLAINT")); + assertEquals("Delivery", SesEventPayload.cloudWatchMetricName("DELIVERY")); + assertEquals("Open", SesEventPayload.cloudWatchMetricName("OPEN")); + assertEquals("Click", SesEventPayload.cloudWatchMetricName("CLICK")); + // Verified against real AWS — no space, unlike the SNS spelling. + assertEquals("RenderingFailure", SesEventPayload.cloudWatchMetricName("RENDERING_FAILURE")); + assertEquals("DeliveryDelay", SesEventPayload.cloudWatchMetricName("DELIVERY_DELAY")); + assertEquals("Subscription", SesEventPayload.cloudWatchMetricName("SUBSCRIPTION")); + } + + /** + * EventBridge uses a separate past-tense vocabulary for {@code detail-type}, distinct + * from the SNS notification {@code eventType} value. Pin every mapping against + * https://docs.aws.amazon.com/eventbridge/latest/ref/events-ref-ses.html so a + * future cleanup of {@link SesEventPayload#eventTypeLabel} cannot accidentally + * regress the EventBridge value (which would silently break customer rules + * keyed on the AWS detail-type names). + */ + @Test + void eventBridgeDetailType_matchesAwsPastTenseVocabulary() { + assertEquals("Email Sent", SesEventPayload.eventBridgeDetailType("SEND")); + assertEquals("Email Rejected", SesEventPayload.eventBridgeDetailType("REJECT")); + assertEquals("Email Bounced", SesEventPayload.eventBridgeDetailType("BOUNCE")); + assertEquals("Email Complaint Received", SesEventPayload.eventBridgeDetailType("COMPLAINT")); + assertEquals("Email Delivered", SesEventPayload.eventBridgeDetailType("DELIVERY")); + assertEquals("Email Opened", SesEventPayload.eventBridgeDetailType("OPEN")); + assertEquals("Email Clicked", SesEventPayload.eventBridgeDetailType("CLICK")); + assertEquals("Email Rendering Failed", SesEventPayload.eventBridgeDetailType("RENDERING_FAILURE")); + assertEquals("Email Delivery Delayed", SesEventPayload.eventBridgeDetailType("DELIVERY_DELAY")); + assertEquals("Email Subscribed", SesEventPayload.eventBridgeDetailType("SUBSCRIPTION")); + } } 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 new file mode 100644 index 0000000000..f203cc1157 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesEventPublisherTest.java @@ -0,0 +1,133 @@ +package io.github.hectorvent.floci.services.ses; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsService; +import io.github.hectorvent.floci.services.eventbridge.EventBridgeService; +import io.github.hectorvent.floci.services.firehose.FirehoseService; +import io.github.hectorvent.floci.services.ses.model.ConfigurationSet; +import io.github.hectorvent.floci.services.ses.model.EventBridgeDestination; +import io.github.hectorvent.floci.services.ses.model.EventDestination; +import io.github.hectorvent.floci.services.sns.SnsService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.time.Instant; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Unit tests for {@link SesEventPublisher}'s PutEvents entry construction. + * + *

The integration suite ({@link SesEventPublishingV2IntegrationTest}) cannot + * distinguish the with-fix and without-fix shapes of the EventBridge entry, because + * {@code EventBridgeService.buildEventEnvelope} defensively defaults + * a missing {@code Resources} entry to an empty {@code ArrayNode} when assembling + * the delivered envelope. These mock-based tests inspect the entry handed to + * {@code eventBridgeService.putEvents} directly so we can pin the publisher's + * contract regardless of downstream defaulting. + */ +@ExtendWith(MockitoExtension.class) +class SesEventPublisherTest { + + private static final ObjectMapper MAPPER = new ObjectMapper(); + private static final String DEFAULT_BUS_ARN = + "arn:aws:events:us-east-1:000000000000:event-bus/default"; + + @Mock private SnsService snsService; + @Mock private FirehoseService firehoseService; + @Mock private EventBridgeService eventBridgeService; + @Mock private CloudWatchMetricsService cloudWatchMetricsService; + + private SesEventPublisher publisher; + + @BeforeEach + void setUp() { + publisher = new SesEventPublisher(snsService, firehoseService, eventBridgeService, + cloudWatchMetricsService, MAPPER); + } + + @Test + void publishEventBridge_withNullSourceArn_emitsEmptyResourcesArrayNotAbsent() { + // Regression pin: SesEventPublisher.publishEventBridge must always set the + // "Resources" key on the PutEvents entry, even when sourceArn is null. Omitting + // the key causes EventBridgeService.matchesPattern to NPE on + // `((ArrayNode) entry.get("Resources")).elements()` for any rule with a + // "resources" pattern filter — the exception is caught and the rule silently + // turns into a no-match plus log noise. An empty ArrayNode matches AWS's + // behavior (the resources field is always present, sometimes empty). + when(eventBridgeService.putEvents(any(), anyString())) + .thenReturn(new EventBridgeService.PutEventsResult(0, List.of())); + + publisher.publish(configurationSetWithEventBridgeDestination(DEFAULT_BUS_ARN), + "SEND", "msg-1", null, null, "000000000000", "subj", + List.of("to@example.com"), null, null, List.of("to@example.com"), + null, null, null, null, Instant.now(), "us-east-1"); + + Map entry = captureSingleEntry(); + assertTrue(entry.containsKey("Resources"), + "Resources key must be present on the entry even when sourceArn is null"); + Object resources = entry.get("Resources"); + assertInstanceOf(ArrayNode.class, resources, + "Resources must be an ArrayNode (EventBridgeService.matchesPattern " + + "casts entry.get(\"Resources\") unconditionally)"); + assertEquals(0, ((ArrayNode) resources).size(), + "Resources must be an empty array when sourceArn is null, not absent"); + } + + @Test + void publishEventBridge_withSourceArn_emitsResourcesContainingArn() { + String arn = "arn:aws:ses:us-east-1:000000000000:identity/sender@example.com"; + when(eventBridgeService.putEvents(any(), anyString())) + .thenReturn(new EventBridgeService.PutEventsResult(0, List.of())); + + publisher.publish(configurationSetWithEventBridgeDestination(DEFAULT_BUS_ARN), + "SEND", "msg-1", "sender@example.com", arn, "000000000000", "subj", + List.of("to@example.com"), null, null, List.of("to@example.com"), + null, null, null, null, Instant.now(), "us-east-1"); + + Map entry = captureSingleEntry(); + Object resources = entry.get("Resources"); + assertNotNull(resources); + assertInstanceOf(ArrayNode.class, resources); + ArrayNode arr = (ArrayNode) resources; + assertEquals(1, arr.size()); + assertEquals(arn, arr.get(0).asText()); + } + + @SuppressWarnings("unchecked") + private Map captureSingleEntry() { + ArgumentCaptor>> captor = + ArgumentCaptor.forClass(List.class); + verify(eventBridgeService).putEvents(captor.capture(), anyString()); + List> entries = captor.getValue(); + assertEquals(1, entries.size(), "publisher must emit exactly one entry per send"); + return entries.get(0); + } + + private ConfigurationSet configurationSetWithEventBridgeDestination(String busArn) { + EventBridgeDestination ebd = new EventBridgeDestination(); + ebd.setEventBusArn(busArn); + EventDestination ed = new EventDestination(); + ed.setName("ed-eb"); + ed.setEnabled(true); + ed.setMatchingEventTypes(List.of("SEND")); + ed.setEventBridgeDestination(ebd); + ConfigurationSet cs = new ConfigurationSet("cs-test"); + cs.setEventDestinations(List.of(ed)); + return cs; + } +} 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 0f5ad028b6..96742db674 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 @@ -2,28 +2,38 @@ package io.github.hectorvent.floci.services.ses; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsService; +import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsService.MetricIdentity; +import io.github.hectorvent.floci.services.s3.S3Service; +import io.github.hectorvent.floci.services.s3.model.S3Object; import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; import io.quarkus.test.junit.QuarkusTest; import io.restassured.response.Response; +import jakarta.inject.Inject; import org.junit.jupiter.api.BeforeAll; 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 java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import static io.restassured.RestAssured.given; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; /** - * End-to-end integration test for SES V2 event publishing: SES SendEmail with a - * ConfigurationSetName whose event destination points at an SNS topic results in - * AWS-format event JSON being published to that topic and delivered to a - * subscribed SQS queue. + * End-to-end integration test for SES V2 event publishing. SES SendEmail with a + * ConfigurationSetName whose event destination points at one of the supported + * destination types — {@code SnsDestination}, {@code KinesisFirehoseDestination}, + * {@code EventBridgeDestination}, or {@code CloudWatchDestination} — results in + * AWS-format event JSON / metric being delivered to that destination's natural + * sink and asserted there. Each destination has its own configuration set and + * runs after the previous block via {@link Order}. */ @QuarkusTest @TestMethodOrder(MethodOrderer.OrderAnnotation.class) @@ -35,18 +45,44 @@ class SesEventPublishingV2IntegrationTest { "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/sns/aws4_request"; private static final String SQS_AUTH = "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/sqs/aws4_request"; + private static final String EVENTS_AUTH = + "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/events/aws4_request"; + private static final String FIREHOSE_AUTH = + "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/firehose/aws4_request"; private static final String SQS_CONTENT_TYPE = "application/x-amz-json-1.0"; private static final String SNS_CONTENT_TYPE = "application/x-amz-json-1.0"; + private static final String EVENTS_CONTENT_TYPE = "application/x-amz-json-1.1"; + private static final String FIREHOSE_CONTENT_TYPE = "application/x-amz-json-1.1"; private static final String CS = "ses-events-cs"; + private static final String CS_FIREHOSE = "ses-events-firehose-cs"; + private static final String CS_EB = "ses-events-eb-cs"; + private static final String CS_CW = "ses-events-cw-cs"; private static final String SENDER = "evt-from@floci.test"; + private static final String FIREHOSE_STREAM = "ses-events-stream"; + private static final String FIREHOSE_BUCKET = "ses-events-firehose-bucket"; + private static final String EB_QUEUE = "ses-events-eb-queue"; + private static final String EB_RULE = "ses-events-eb-rule"; + private static final String EB_DEFAULT_BUS_ARN = + "arn:aws:events:us-east-1:000000000000:event-bus/default"; + private static final String CW_REGION = "us-east-1"; + private static final String CW_NAMESPACE = "AWS/SES"; private static String queueUrl; private static String queueArn; private static String topicArn; + private static String firehoseStreamArn; + private static String ebQueueUrl; + private static String ebQueueArn; private static final ObjectMapper MAPPER = new ObjectMapper(); + @Inject + S3Service s3Service; + + @Inject + CloudWatchMetricsService metricsService; + @BeforeAll static void configureRestAssured() { RestAssuredJsonUtils.configureAwsContentTypes(); @@ -499,6 +535,57 @@ class SesEventPublishingV2IntegrationTest { } } + // ============================== Firehose ============================== + + @Test + @Order(14) + void firehose_setupStreamAndConfigSet() { + firehoseStreamArn = given() + .contentType(FIREHOSE_CONTENT_TYPE) + .header("Authorization", FIREHOSE_AUTH) + .header("X-Amz-Target", "Firehose_20150804.CreateDeliveryStream") + .body(""" + {"DeliveryStreamName": "%s", + "S3DestinationConfiguration": {"BucketARN": "arn:aws:s3:::%s"}} + """.formatted(FIREHOSE_STREAM, FIREHOSE_BUCKET)) + .when() + .post("/") + .then() + .statusCode(200) + .extract().jsonPath().getString("DeliveryStreamARN"); + assertNotNull(firehoseStreamArn); + + given() + .contentType("application/json") + .header("Authorization", SES_AUTH) + .body("{\"ConfigurationSetName\":\"" + CS_FIREHOSE + "\"}") + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(200); + + given() + .contentType("application/json") + .header("Authorization", SES_AUTH) + .body(""" + { + "EventDestinationName": "ed-firehose", + "EventDestination": { + "Enabled": true, + "MatchingEventTypes": ["SEND"], + "KinesisFirehoseDestination": { + "IamRoleArn": "arn:aws:iam::000000000000:role/ses-firehose-role", + "DeliveryStreamArn": "%s" + } + } + } + """.formatted(firehoseStreamArn)) + .when() + .post("/v2/email/configuration-sets/" + CS_FIREHOSE + "/event-destinations") + .then() + .statusCode(200); + } + @Test @Order(13) void sendToSuppressedAddress_withComplaintReason_publishesSyntheticComplaintEvent() throws Exception { @@ -550,6 +637,934 @@ class SesEventPublishingV2IntegrationTest { } } + @Test + @Order(15) + void firehose_fiveSends_triggerAutoFlushAndWriteNdjsonToS3() throws Exception { + for (int i = 0; i < 5; i++) { + sendEmailToConfigSet(CS_FIREHOSE, "recipient" + i + "@example.com", "fh-evt-" + i); + } + + List objects = s3Service.listObjects(FIREHOSE_BUCKET, FIREHOSE_STREAM + "/", null, 100); + assertEquals(1, objects.size(), + "expected exactly one flushed S3 object after 5 putRecord calls (DEFAULT_FLUSH_COUNT)"); + + S3Object obj = s3Service.getObject(FIREHOSE_BUCKET, objects.get(0).getKey()); + String body = new String(obj.getData(), StandardCharsets.UTF_8); + String[] lines = body.split("\\R"); + assertEquals(5, lines.length, "flushed object should contain 5 NDJSON records"); + + for (int i = 0; i < 5; i++) { + JsonNode event = MAPPER.readTree(lines[i]); + assertEquals("Send", event.path("eventType").asText()); + assertEquals(SENDER, event.path("mail").path("source").asText()); + assertEquals(CS_FIREHOSE, + event.path("mail").path("tags").path("ses:configuration-set").get(0).asText()); + } + } + + @Test + @Order(16) + void firehose_disabledEventDestination_doesNotPublish() throws Exception { + String csDisabled = "ses-events-firehose-cs-disabled"; + String streamDisabled = "ses-events-stream-disabled"; + + // Reuse FIREHOSE_BUCKET so listObjects never hits a missing-bucket condition; + // the disabled stream just has its own prefix which should stay empty. + String disabledStreamArn = given() + .contentType(FIREHOSE_CONTENT_TYPE) + .header("Authorization", FIREHOSE_AUTH) + .header("X-Amz-Target", "Firehose_20150804.CreateDeliveryStream") + .body(""" + {"DeliveryStreamName": "%s", + "S3DestinationConfiguration": {"BucketARN": "arn:aws:s3:::%s"}} + """.formatted(streamDisabled, FIREHOSE_BUCKET)) + .when() + .post("/") + .then() + .statusCode(200) + .extract().jsonPath().getString("DeliveryStreamARN"); + + given() + .contentType("application/json") + .header("Authorization", SES_AUTH) + .body("{\"ConfigurationSetName\":\"" + csDisabled + "\"}") + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(200); + + given() + .contentType("application/json") + .header("Authorization", SES_AUTH) + .body(""" + { + "EventDestinationName": "ed-firehose-disabled", + "EventDestination": { + "Enabled": false, + "MatchingEventTypes": ["SEND"], + "KinesisFirehoseDestination": { + "IamRoleArn": "arn:aws:iam::000000000000:role/ses-firehose-role", + "DeliveryStreamArn": "%s" + } + } + } + """.formatted(disabledStreamArn)) + .when() + .post("/v2/email/configuration-sets/" + csDisabled + "/event-destinations") + .then() + .statusCode(200); + + for (int i = 0; i < 5; i++) { + sendEmailToConfigSet(csDisabled, "recipient" + i + "@example.com", "fh-disabled-" + i); + } + + List objects = s3Service.listObjects(FIREHOSE_BUCKET, streamDisabled + "/", null, 100); + assertTrue(objects.isEmpty(), + "disabled event destination should not produce any flushed S3 object"); + } + + @Test + @Order(17) + void firehose_unknownStreamArn_sendStillSucceedsAndDoesNotCrash() throws Exception { + String csUnknown = "ses-events-firehose-cs-unknown"; + String bogusStreamArn = + "arn:aws:firehose:us-east-1:000000000000:deliverystream/does-not-exist"; + + given() + .contentType("application/json") + .header("Authorization", SES_AUTH) + .body("{\"ConfigurationSetName\":\"" + csUnknown + "\"}") + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(200); + + given() + .contentType("application/json") + .header("Authorization", SES_AUTH) + .body(""" + { + "EventDestinationName": "ed-firehose-unknown", + "EventDestination": { + "Enabled": true, + "MatchingEventTypes": ["SEND"], + "KinesisFirehoseDestination": { + "IamRoleArn": "arn:aws:iam::000000000000:role/ses-firehose-role", + "DeliveryStreamArn": "%s" + } + } + } + """.formatted(bogusStreamArn)) + .when() + .post("/v2/email/configuration-sets/" + csUnknown + "/event-destinations") + .then() + .statusCode(200); + + // SendEmail must still succeed even though the Firehose dispatch hits a missing stream. + sendEmailToConfigSet(csUnknown, "recipient@example.com", "fh-unknown"); + + // Sanity: the main stream's S3 contents are unchanged by this test. + List objects = s3Service.listObjects(FIREHOSE_BUCKET, FIREHOSE_STREAM + "/", null, 100); + assertFalse(objects.isEmpty(), + "main stream's S3 contents should still be present (sanity)"); + } + + // ============================ EventBridge ============================ + + @Test + @Order(18) + void eventBridge_setupSqsQueueAndRuleAndConfigSet() { + ebQueueUrl = given() + .contentType(SQS_CONTENT_TYPE) + .header("Authorization", SQS_AUTH) + .header("X-Amz-Target", "AmazonSQS.CreateQueue") + .body("{\"QueueName\":\"" + EB_QUEUE + "\"}") + .when() + .post("/") + .then() + .statusCode(200) + .extract().jsonPath().getString("QueueUrl"); + assertNotNull(ebQueueUrl); + + ebQueueArn = given() + .contentType(SQS_CONTENT_TYPE) + .header("Authorization", SQS_AUTH) + .header("X-Amz-Target", "AmazonSQS.GetQueueAttributes") + .body("{\"QueueUrl\":\"" + ebQueueUrl + "\",\"AttributeNames\":[\"All\"]}") + .when() + .post("/") + .then() + .statusCode(200) + .extract().jsonPath().getString("Attributes.QueueArn"); + assertNotNull(ebQueueArn); + + given() + .contentType(EVENTS_CONTENT_TYPE) + .header("Authorization", EVENTS_AUTH) + .header("X-Amz-Target", "AWSEvents.PutRule") + .body("{\"Name\":\"" + EB_RULE + + "\",\"EventPattern\":\"{\\\"source\\\":[\\\"aws.ses\\\"]}\"}") + .when() + .post("/") + .then() + .statusCode(200); + + given() + .contentType(EVENTS_CONTENT_TYPE) + .header("Authorization", EVENTS_AUTH) + .header("X-Amz-Target", "AWSEvents.PutTargets") + .body("{\"Rule\":\"" + EB_RULE + "\",\"Targets\":[{\"Id\":\"1\",\"Arn\":\"" + + ebQueueArn + "\"}]}") + .when() + .post("/") + .then() + .statusCode(200); + + given() + .contentType("application/json") + .header("Authorization", SES_AUTH) + .body("{\"ConfigurationSetName\":\"" + CS_EB + "\"}") + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(200); + + given() + .contentType("application/json") + .header("Authorization", SES_AUTH) + .body(""" + { + "EventDestinationName": "ed-eventbridge", + "EventDestination": { + "Enabled": true, + "MatchingEventTypes": ["SEND", "DELIVERY", "BOUNCE"], + "EventBridgeDestination": {"EventBusArn": "%s"} + } + } + """.formatted(EB_DEFAULT_BUS_ARN)) + .when() + .post("/v2/email/configuration-sets/" + CS_EB + "/event-destinations") + .then() + .statusCode(200); + } + + @Test + @Order(19) + void eventBridge_regularRecipient_routesSendEventToSqsTarget() throws Exception { + drainEbQueue(); + sendEmailToConfigSet(CS_EB, "recipient@example.com", "eb-evt"); + + List envelopes = receiveEbEvents(1); + assertEquals(1, envelopes.size()); + + JsonNode envelope = envelopes.get(0); + assertEquals("aws.ses", envelope.path("source").asText()); + assertEquals("Email Sent", envelope.path("detail-type").asText()); + assertEquals("default", envelope.path("event-bus-name").asText()); + // AWS always emits a `resources` array on the envelope, and SesEventPublisher mirrors + // that contract. SesService derives sourceArn from FromEmailAddress, so this case + // produces a one-element array carrying the SES identity ARN; the sourceArn-null + // branch (empty array on the PutEvents entry) is pinned by SesEventPublisherTest. + assertTrue(envelope.has("resources") && envelope.get("resources").isArray(), + "envelope must always carry a resources array"); + assertEquals(1, envelope.get("resources").size()); + assertEquals("arn:aws:ses:us-east-1:000000000000:identity/" + SENDER, + envelope.get("resources").get(0).asText()); + + JsonNode detail = envelope.path("detail"); + assertEquals("Send", detail.path("eventType").asText()); + assertEquals(SENDER, detail.path("mail").path("source").asText()); + assertEquals(CS_EB, + detail.path("mail").path("tags").path("ses:configuration-set").get(0).asText()); + } + + @Test + @Order(20) + void eventBridge_successSimulator_routesSendAndDeliveryDetailTypes() throws Exception { + drainEbQueue(); + sendEmailToConfigSet(CS_EB, "success@simulator.amazonses.com", "eb-evt-success"); + + List envelopes = receiveEbEvents(2); + assertEquals(2, envelopes.size()); + assertTrue(envelopes.stream().anyMatch( + e -> "Email Sent".equals(e.path("detail-type").asText()))); + assertTrue(envelopes.stream().anyMatch( + e -> "Email Delivered".equals(e.path("detail-type").asText()))); + } + + @Test + @Order(21) + void eventBridge_disabledEventDestination_skipsPublish() throws Exception { + String csDisabled = "ses-events-eb-cs-disabled"; + given() + .contentType("application/json") + .header("Authorization", SES_AUTH) + .body("{\"ConfigurationSetName\":\"" + csDisabled + "\"}") + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(200); + + given() + .contentType("application/json") + .header("Authorization", SES_AUTH) + .body(""" + { + "EventDestinationName": "ed-eventbridge-disabled", + "EventDestination": { + "Enabled": false, + "MatchingEventTypes": ["SEND"], + "EventBridgeDestination": {"EventBusArn": "%s"} + } + } + """.formatted(EB_DEFAULT_BUS_ARN)) + .when() + .post("/v2/email/configuration-sets/" + csDisabled + "/event-destinations") + .then() + .statusCode(200); + + drainEbQueue(); + sendEmailToConfigSet(csDisabled, "recipient@example.com", "eb-disabled"); + + List envelopes = receiveEbEvents(0); + assertEquals(0, envelopes.size(), + "disabled event destination should not publish to EventBridge"); + } + + @Test + @Order(22) + void eventBridge_unknownBusName_sendStillSucceeds() { + String csUnknown = "ses-events-eb-cs-unknown"; + String bogusBusArn = + "arn:aws:events:us-east-1:000000000000:event-bus/does-not-exist"; + + given() + .contentType("application/json") + .header("Authorization", SES_AUTH) + .body("{\"ConfigurationSetName\":\"" + csUnknown + "\"}") + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(200); + + given() + .contentType("application/json") + .header("Authorization", SES_AUTH) + .body(""" + { + "EventDestinationName": "ed-eventbridge-unknown", + "EventDestination": { + "Enabled": true, + "MatchingEventTypes": ["SEND"], + "EventBridgeDestination": {"EventBusArn": "%s"} + } + } + """.formatted(bogusBusArn)) + .when() + .post("/v2/email/configuration-sets/" + csUnknown + "/event-destinations") + .then() + .statusCode(200); + + // 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"); + } + + // ============================ CloudWatch ============================= + + @Test + @Order(23) + void cloudWatch_setupConfigSetAndEventDestination() { + given() + .contentType("application/json") + .header("Authorization", SES_AUTH) + .body("{\"ConfigurationSetName\":\"" + CS_CW + "\"}") + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(200); + + given() + .contentType("application/json") + .header("Authorization", SES_AUTH) + .body(""" + { + "EventDestinationName": "ed-cloudwatch", + "EventDestination": { + "Enabled": true, + "MatchingEventTypes": ["SEND", "DELIVERY", "BOUNCE"], + "CloudWatchDestination": { + "DimensionConfigurations": [ + { + "DimensionName": "ses:configuration-set", + "DimensionValueSource": "messageTag", + "DefaultDimensionValue": "unknown" + }, + { + "DimensionName": "campaign", + "DimensionValueSource": "messageTag", + "DefaultDimensionValue": "default-campaign" + }, + { + "DimensionName": "X-Custom-Header", + "DimensionValueSource": "emailHeader", + "DefaultDimensionValue": "default-header" + } + ] + } + } + } + """) + .when() + .post("/v2/email/configuration-sets/" + CS_CW + "/event-destinations") + .then() + .statusCode(200); + } + + @Test + @Order(24) + void cloudWatch_sendWithTagsAndHeaders_registersSendMetricWithResolvedDimensions() { + given() + .contentType("application/json") + .header("Authorization", SES_AUTH) + .body(""" + { + "FromEmailAddress": "%s", + "Destination": {"ToAddresses": ["recipient@example.com"]}, + "ConfigurationSetName": "%s", + "EmailTags": [ + {"Name": "campaign", "Value": "launch"} + ], + "Content": { + "Simple": { + "Subject": {"Data": "cw-evt"}, + "Body": {"Text": {"Data": "hi"}}, + "Headers": [ + {"Name": "X-Custom-Header", "Value": "header-value"} + ] + } + } + } + """.formatted(SENDER, CS_CW)) + .when() + .post("/v2/email/outbound-emails") + .then() + .statusCode(200); + + List metrics = metricsService.listMetrics(CW_NAMESPACE, "Send", null, CW_REGION); + assertFalse(metrics.isEmpty(), "expected a Send metric in AWS/SES"); + MetricIdentity send = metrics.stream() + .filter(m -> cloudWatch_hasDimensions(m, CS_CW, "launch", "header-value")) + .findFirst() + .orElseThrow(() -> new AssertionError( + "Send metric with dimensions ses:configuration-set=" + CS_CW + + ", campaign=launch, X-Custom-Header=header-value not found: " + + metrics)); + assertEquals(3, send.dimensions().size(), + "expected exactly the three configured dimensions, got: " + send.dimensions()); + } + + @Test + @Order(25) + void cloudWatch_sendWithoutTags_fallsBackToDefaultDimensionValue() { + given() + .contentType("application/json") + .header("Authorization", SES_AUTH) + .body(""" + { + "FromEmailAddress": "%s", + "Destination": {"ToAddresses": ["recipient2@example.com"]}, + "ConfigurationSetName": "%s", + "Content": { + "Simple": { + "Subject": {"Data": "cw-no-tags"}, + "Body": {"Text": {"Data": "hi"}} + } + } + } + """.formatted(SENDER, CS_CW)) + .when() + .post("/v2/email/outbound-emails") + .then() + .statusCode(200); + + List metrics = metricsService.listMetrics(CW_NAMESPACE, "Send", null, CW_REGION); + assertTrue(metrics.stream().anyMatch(m -> cloudWatch_hasDimensions(m, CS_CW, + "default-campaign", "default-header")), + "expected Send metric with default-campaign / default-header fallback dimensions, got: " + + metrics); + } + + @Test + @Order(26) + void cloudWatch_successSimulator_registersBothSendAndDeliveryMetrics() { + given() + .contentType("application/json") + .header("Authorization", SES_AUTH) + .body(""" + { + "FromEmailAddress": "%s", + "Destination": {"ToAddresses": ["success@simulator.amazonses.com"]}, + "ConfigurationSetName": "%s", + "Content": { + "Simple": { + "Subject": {"Data": "cw-success"}, + "Body": {"Text": {"Data": "hi"}} + } + } + } + """.formatted(SENDER, CS_CW)) + .when() + .post("/v2/email/outbound-emails") + .then() + .statusCode(200); + + List sendMetrics = metricsService.listMetrics(CW_NAMESPACE, "Send", null, CW_REGION); + List deliveryMetrics = metricsService.listMetrics(CW_NAMESPACE, "Delivery", null, CW_REGION); + assertFalse(sendMetrics.isEmpty(), "Send metric should be present"); + assertFalse(deliveryMetrics.isEmpty(), + "Delivery metric should be present for success simulator address"); + } + + @Test + @Order(27) + void cloudWatch_disabledEventDestination_doesNotPublishMetric() { + String csDisabled = "ses-events-cw-cs-disabled"; + given() + .contentType("application/json") + .header("Authorization", SES_AUTH) + .body("{\"ConfigurationSetName\":\"" + csDisabled + "\"}") + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(200); + + given() + .contentType("application/json") + .header("Authorization", SES_AUTH) + .body(""" + { + "EventDestinationName": "ed-cloudwatch-disabled", + "EventDestination": { + "Enabled": false, + "MatchingEventTypes": ["SEND"], + "CloudWatchDestination": { + "DimensionConfigurations": [ + { + "DimensionName": "ses:configuration-set", + "DimensionValueSource": "messageTag", + "DefaultDimensionValue": "unknown" + } + ] + } + } + } + """) + .when() + .post("/v2/email/configuration-sets/" + csDisabled + "/event-destinations") + .then() + .statusCode(200); + + given() + .contentType("application/json") + .header("Authorization", SES_AUTH) + .body(""" + { + "FromEmailAddress": "%s", + "Destination": {"ToAddresses": ["recipient@example.com"]}, + "ConfigurationSetName": "%s", + "Content": { + "Simple": { + "Subject": {"Data": "cw-disabled"}, + "Body": {"Text": {"Data": "hi"}} + } + } + } + """.formatted(SENDER, csDisabled)) + .when() + .post("/v2/email/outbound-emails") + .then() + .statusCode(200); + + List metrics = metricsService.listMetrics(CW_NAMESPACE, "Send", null, CW_REGION); + assertTrue(metrics.stream().noneMatch(m -> m.dimensions().stream() + .anyMatch(d -> "ses:configuration-set".equals(d.name()) + && csDisabled.equals(d.value()))), + "disabled event destination should not register any Send metric for " + csDisabled); + } + + @Test + @Order(28) + void eventBridge_nonDefaultRegion_matchesRegionFilteredRuleAndStampsEnvelopeRegion() + throws Exception { + // Pins the SES→EventBridge cross-region path against two regressions: + // (a) SesEventPublisher.publishEventBridge must stamp `entry.Region = `, + // otherwise EventBridgeService.matchesPattern falls back to the default region + // and a rule scoped to eu-west-1 will never match a SES-originated event. + // (b) EventBridgeService.buildEventEnvelope (post #1125) must propagate the + // PutEvents call's region to the delivered envelope's `region` field, so + // downstream consumers see eu-west-1, not us-east-1. + String region = "eu-west-1"; + String queueName = "ses-events-eb-eu-west-1-queue"; + String ruleName = "ses-events-eb-eu-west-1-rule"; + String csName = "ses-events-eb-eu-west-1-cs"; + String busArn = "arn:aws:events:" + region + ":000000000000:event-bus/default"; + String sqsAuth = + "AWS4-HMAC-SHA256 Credential=AKID/20260101/" + region + "/sqs/aws4_request"; + String eventsAuth = + "AWS4-HMAC-SHA256 Credential=AKID/20260101/" + region + "/events/aws4_request"; + String sesAuth = + "AWS4-HMAC-SHA256 Credential=AKID/20260101/" + region + "/ses/aws4_request"; + + String euQueueUrl = given() + .contentType(SQS_CONTENT_TYPE) + .header("Authorization", sqsAuth) + .header("X-Amz-Target", "AmazonSQS.CreateQueue") + .body("{\"QueueName\":\"" + queueName + "\"}") + .when() + .post("/") + .then() + .statusCode(200) + .extract().jsonPath().getString("QueueUrl"); + assertNotNull(euQueueUrl); + + String euQueueArn = given() + .contentType(SQS_CONTENT_TYPE) + .header("Authorization", sqsAuth) + .header("X-Amz-Target", "AmazonSQS.GetQueueAttributes") + .body("{\"QueueUrl\":\"" + euQueueUrl + "\",\"AttributeNames\":[\"All\"]}") + .when() + .post("/") + .then() + .statusCode(200) + .extract().jsonPath().getString("Attributes.QueueArn"); + assertNotNull(euQueueArn); + + given() + .contentType(EVENTS_CONTENT_TYPE) + .header("Authorization", eventsAuth) + .header("X-Amz-Target", "AWSEvents.PutRule") + .body("{\"Name\":\"" + ruleName + + "\",\"EventPattern\":\"{\\\"source\\\":[\\\"aws.ses\\\"]," + + "\\\"region\\\":[\\\"" + region + "\\\"]}\"}") + .when() + .post("/") + .then() + .statusCode(200); + + given() + .contentType(EVENTS_CONTENT_TYPE) + .header("Authorization", eventsAuth) + .header("X-Amz-Target", "AWSEvents.PutTargets") + .body("{\"Rule\":\"" + ruleName + "\",\"Targets\":[{\"Id\":\"1\",\"Arn\":\"" + + euQueueArn + "\"}]}") + .when() + .post("/") + .then() + .statusCode(200); + + given() + .contentType("application/json") + .header("Authorization", sesAuth) + .body("{\"ConfigurationSetName\":\"" + csName + "\"}") + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(200); + + given() + .contentType("application/json") + .header("Authorization", sesAuth) + .body(""" + { + "EventDestinationName": "ed-eventbridge-eu-west-1", + "EventDestination": { + "Enabled": true, + "MatchingEventTypes": ["SEND"], + "EventBridgeDestination": {"EventBusArn": "%s"} + } + } + """.formatted(busArn)) + .when() + .post("/v2/email/configuration-sets/" + csName + "/event-destinations") + .then() + .statusCode(200); + + given() + .contentType("application/json") + .header("Authorization", sesAuth) + .body(""" + { + "FromEmailAddress": "%s", + "Destination": {"ToAddresses": ["recipient@example.com"]}, + "ConfigurationSetName": "%s", + "Content": { + "Simple": { + "Subject": {"Data": "eb-eu-west-1-evt"}, + "Body": {"Text": {"Data": "hi"}} + } + } + } + """.formatted(SENDER, csName)) + .when() + .post("/v2/email/outbound-emails") + .then() + .statusCode(200); + + List envelopes = new ArrayList<>(); + for (int attempt = 0; attempt < 10 && envelopes.isEmpty(); attempt++) { + Response r = given() + .contentType(SQS_CONTENT_TYPE) + .header("Authorization", sqsAuth) + .header("X-Amz-Target", "AmazonSQS.ReceiveMessage") + .body("{\"QueueUrl\":\"" + euQueueUrl + + "\",\"MaxNumberOfMessages\":10,\"WaitTimeSeconds\":1}") + .when() + .post("/") + .then() + .statusCode(200) + .extract().response(); + List bodies = r.jsonPath().getList("Messages.Body"); + if (bodies != null) { + for (String b : bodies) { + envelopes.add(MAPPER.readTree(b)); + } + } + } + assertFalse(envelopes.isEmpty(), + "rule with region=eu-west-1 filter must match SES eu-west-1 event " + + "(regression: SesEventPublisher must stamp entry.Region)"); + + JsonNode envelope = envelopes.get(0); + assertEquals(region, envelope.path("region").asText(), + "envelope.region must equal the PutEvents call region " + + "(regression: EventBridgeService.buildEventEnvelope per #1125)"); + assertEquals("aws.ses", envelope.path("source").asText()); + assertEquals("Email Sent", envelope.path("detail-type").asText()); + } + + @Test + @Order(29) + void eventBridge_detailTypePattern_keyedOnAwsSentValue_matches() throws Exception { + // Structural pin against EventBridge detail-type drift. The rule pattern is keyed + // on the AWS-correct value "Email Sent" (past tense, per + // https://docs.aws.amazon.com/eventbridge/latest/ref/events-ref-ses.html). If the + // publisher regresses to the SNS-style "Email Send" — the wrong form for + // EventBridge — this rule will not match and no message reaches the SQS target, + // failing the test without relying on any hard-coded assertion string. + String queueName = "ses-events-eb-detail-q"; + String ruleName = "ses-events-eb-detail-rule"; + String csName = "ses-events-eb-detail-cs"; + + String detailQueueUrl = given() + .contentType(SQS_CONTENT_TYPE) + .header("Authorization", SQS_AUTH) + .header("X-Amz-Target", "AmazonSQS.CreateQueue") + .body("{\"QueueName\":\"" + queueName + "\"}") + .when() + .post("/") + .then() + .statusCode(200) + .extract().jsonPath().getString("QueueUrl"); + assertNotNull(detailQueueUrl); + + String detailQueueArn = given() + .contentType(SQS_CONTENT_TYPE) + .header("Authorization", SQS_AUTH) + .header("X-Amz-Target", "AmazonSQS.GetQueueAttributes") + .body("{\"QueueUrl\":\"" + detailQueueUrl + "\",\"AttributeNames\":[\"All\"]}") + .when() + .post("/") + .then() + .statusCode(200) + .extract().jsonPath().getString("Attributes.QueueArn"); + + given() + .contentType(EVENTS_CONTENT_TYPE) + .header("Authorization", EVENTS_AUTH) + .header("X-Amz-Target", "AWSEvents.PutRule") + .body("{\"Name\":\"" + ruleName + + "\",\"EventPattern\":\"{\\\"source\\\":[\\\"aws.ses\\\"]," + + "\\\"detail-type\\\":[\\\"Email Sent\\\"]}\"}") + .when() + .post("/") + .then() + .statusCode(200); + + given() + .contentType(EVENTS_CONTENT_TYPE) + .header("Authorization", EVENTS_AUTH) + .header("X-Amz-Target", "AWSEvents.PutTargets") + .body("{\"Rule\":\"" + ruleName + "\",\"Targets\":[{\"Id\":\"1\",\"Arn\":\"" + + detailQueueArn + "\"}]}") + .when() + .post("/") + .then() + .statusCode(200); + + given() + .contentType("application/json") + .header("Authorization", SES_AUTH) + .body("{\"ConfigurationSetName\":\"" + csName + "\"}") + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(200); + + given() + .contentType("application/json") + .header("Authorization", SES_AUTH) + .body(""" + { + "EventDestinationName": "ed-eventbridge-detail", + "EventDestination": { + "Enabled": true, + "MatchingEventTypes": ["SEND"], + "EventBridgeDestination": {"EventBusArn": "%s"} + } + } + """.formatted(EB_DEFAULT_BUS_ARN)) + .when() + .post("/v2/email/configuration-sets/" + csName + "/event-destinations") + .then() + .statusCode(200); + + sendEmailToConfigSet(csName, "recipient@example.com", "eb-detail-type-pin"); + + List envelopes = new ArrayList<>(); + for (int attempt = 0; attempt < 10 && envelopes.isEmpty(); attempt++) { + Response r = given() + .contentType(SQS_CONTENT_TYPE) + .header("Authorization", SQS_AUTH) + .header("X-Amz-Target", "AmazonSQS.ReceiveMessage") + .body("{\"QueueUrl\":\"" + detailQueueUrl + + "\",\"MaxNumberOfMessages\":10,\"WaitTimeSeconds\":1}") + .when() + .post("/") + .then() + .statusCode(200) + .extract().response(); + List bodies = r.jsonPath().getList("Messages.Body"); + if (bodies != null) { + for (String b : bodies) { + envelopes.add(MAPPER.readTree(b)); + } + } + } + assertFalse(envelopes.isEmpty(), + "rule with detail-type=[\"Email Sent\"] must match a SEND emission " + + "(regression: SesEventPayload.eventBridgeDetailType returning the SNS-style " + + "'Email Send' for EventBridge would cause this rule to silently drop the event)"); + assertEquals("Email Sent", envelopes.get(0).path("detail-type").asText()); + } + + private static boolean cloudWatch_hasDimensions(MetricIdentity metric, String configurationSet, + String campaign, String headerValue) { + return metric.dimensions().stream() + .anyMatch(d -> "ses:configuration-set".equals(d.name()) + && configurationSet.equals(d.value())) + && metric.dimensions().stream() + .anyMatch(d -> "campaign".equals(d.name()) && campaign.equals(d.value())) + && metric.dimensions().stream() + .anyMatch(d -> "X-Custom-Header".equals(d.name()) + && headerValue.equals(d.value())); + } + + private void sendEmailToConfigSet(String configSet, String to, String subject) { + given() + .contentType("application/json") + .header("Authorization", SES_AUTH) + .body(""" + { + "FromEmailAddress": "%s", + "Destination": {"ToAddresses": ["%s"]}, + "ConfigurationSetName": "%s", + "Content": { + "Simple": { + "Subject": {"Data": "%s"}, + "Body": {"Text": {"Data": "hi"}} + } + } + } + """.formatted(SENDER, to, configSet, subject)) + .when() + .post("/v2/email/outbound-emails") + .then() + .statusCode(200); + } + + private void drainEbQueue() { + for (int i = 0; i < 5; i++) { + Response r = given() + .contentType(SQS_CONTENT_TYPE) + .header("Authorization", SQS_AUTH) + .header("X-Amz-Target", "AmazonSQS.ReceiveMessage") + .body("{\"QueueUrl\":\"" + ebQueueUrl + + "\",\"MaxNumberOfMessages\":10,\"WaitTimeSeconds\":0}") + .when() + .post("/") + .then() + .statusCode(200) + .extract().response(); + List handles = r.jsonPath().getList("Messages.ReceiptHandle"); + if (handles == null || handles.isEmpty()) { + return; + } + deleteEbMessages(handles); + } + } + + private List receiveEbEvents(int expectedAtLeast) throws Exception { + List events = new ArrayList<>(); + int maxAttempts = expectedAtLeast > 0 ? 10 : 2; + for (int attempt = 0; attempt < maxAttempts; attempt++) { + if (expectedAtLeast > 0 && events.size() >= expectedAtLeast) { + break; + } + Response r = given() + .contentType(SQS_CONTENT_TYPE) + .header("Authorization", SQS_AUTH) + .header("X-Amz-Target", "AmazonSQS.ReceiveMessage") + .body("{\"QueueUrl\":\"" + ebQueueUrl + + "\",\"MaxNumberOfMessages\":10,\"WaitTimeSeconds\":1}") + .when() + .post("/") + .then() + .statusCode(200) + .extract().response(); + List bodies = r.jsonPath().getList("Messages.Body"); + List handles = r.jsonPath().getList("Messages.ReceiptHandle"); + if (bodies == null || bodies.isEmpty()) { + continue; + } + for (String body : bodies) { + events.add(MAPPER.readTree(body)); + } + deleteEbMessages(handles); + } + return events; + } + + private void deleteEbMessages(List receiptHandles) { + StringBuilder entries = new StringBuilder(); + for (int i = 0; i < receiptHandles.size(); i++) { + if (i > 0) { + entries.append(","); + } + entries.append("{\"Id\":\"m").append(i).append("\",\"ReceiptHandle\":\"") + .append(receiptHandles.get(i)).append("\"}"); + } + given() + .contentType(SQS_CONTENT_TYPE) + .header("Authorization", SQS_AUTH) + .header("X-Amz-Target", "AmazonSQS.DeleteMessageBatch") + .body("{\"QueueUrl\":\"" + ebQueueUrl + "\",\"Entries\":[" + entries + "]}") + .when() + .post("/") + .then() + .statusCode(200); + } + private void sendEmail(String to) { given() .contentType("application/json")