feat(ses): add v2 GetMessageInsights (#4157)

* feat(ses): add v2 GetMessageInsights

Serves the per-recipient event timeline for a sent message. The timeline is
derived at send time from the events SesRecipientEvents classifies for the
send, the same classification that drives configuration-set event publishing
and identity notifications, then stored on the SentEmail record, so the read
path is a plain lookup and no new event store is needed. SentEmail also gains
the EmailTags the operation returns, and a raw send now stores its parsed MIME
subject, when present, as a simple send does.

Insights additionally report DELIVERY for an ordinary recipient so a plain
send does not look stuck at SEND; a suppressed recipient never gets DELIVERY,
since it is dropped before the relay, and a message the content scan rejected
ends every recipient's timeline at REJECT. What gets published is unchanged.

Probed against real SES in us-east-1: the operation is gated on Virtual
Deliverability Manager and reports that before it looks at the message id, a
missing message names the account alongside the id, Isp carries the literal
UNKNOWN_ISP when the provider cannot be identified, BounceType is the
upper-case enum member, and an account-suppressed recipient is reported with
BounceSubType ON_ACCOUNT_SUPPRESSION_LIST and SES's own suppression text as
the diagnostic, the upper snake spelling this operation uses where the
published events say OnAccountSuppressionList. Floci deliberately records the
mailbox-simulator sends and the multi-recipient mail that VDM drops, because
dropping both would leave insights permanently empty, and returns one entry
per envelope recipient where an AWS response carries at most one.

* test(ses): pin the message-insights VDM gate to one account

The gate reads the account VDM attributes, which are account-scoped like
every other SES store, but no test said so: the isolation test enables VDM
for both of its accounts. An account that never enabled it now has to meet
the gate while others in the same region are past it.

* feat(ses): report a content-rejected raw send in message insights

The raw send path classified its recipients with a hardcoded false, because
the content scan did not exist on this branch's base. It does now, so the
real scan result reaches the timeline: a rejected raw send reports SEND then
REJECT instead of a delivery it never had.

Its stored tags follow the same rule the published events already use, the
request's own tags only, so tags read off a refused message's headers cannot
resurface through GetMessageInsights.
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-09-23 00:17:28 -04:00
1 parent a0cb86cc13
commit e947b6a2a1
17 files changed
+1328 -4

No files matched your search

@@ -0,0 +1,207 @@
package com.floci.test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
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 software.amazon.awssdk.services.sesv2.SesV2Client;
import software.amazon.awssdk.services.sesv2.model.Body;
import software.amazon.awssdk.services.sesv2.model.Content;
import software.amazon.awssdk.services.sesv2.model.CreateEmailIdentityRequest;
import software.amazon.awssdk.services.sesv2.model.DeleteEmailIdentityRequest;
import software.amazon.awssdk.services.sesv2.model.Destination;
import software.amazon.awssdk.services.sesv2.model.EmailContent;
import software.amazon.awssdk.services.sesv2.model.EmailInsights;
import software.amazon.awssdk.services.sesv2.model.GetAccountRequest;
import software.amazon.awssdk.services.sesv2.model.GetAccountResponse;
import software.amazon.awssdk.services.sesv2.model.GetMessageInsightsRequest;
import software.amazon.awssdk.services.sesv2.model.GetMessageInsightsResponse;
import software.amazon.awssdk.services.sesv2.model.InsightsEvent;
import software.amazon.awssdk.services.sesv2.model.Message;
import software.amazon.awssdk.services.sesv2.model.MessageTag;
import software.amazon.awssdk.services.sesv2.model.NotFoundException;
import software.amazon.awssdk.services.sesv2.model.PutAccountVdmAttributesRequest;
import software.amazon.awssdk.services.sesv2.model.SendEmailRequest;
import software.amazon.awssdk.services.sesv2.model.VdmAttributes;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* SDK compatibility test for SES v2 GetMessageInsights: the Virtual Deliverability Manager gate,
* the not-found shape, and the per-recipient event timeline a send produces, against a live Floci
* instance. VDM is account and region scoped, so the original attributes are captured and restored.
*/
@DisplayName("SES v2 Message Insights")
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class SesMessageInsightsTest {
private static final String SENDER = "compat-insights@example.com";
private static SesV2Client sesV2;
private static VdmAttributes originalVdm;
@BeforeAll
static void setup() {
sesV2 = TestFixtures.sesV2Client();
GetAccountResponse account = sesV2.getAccount(GetAccountRequest.builder().build());
originalVdm = account.vdmAttributes();
// An interrupted earlier run can leave the identity behind; creating it again would fail
// with AlreadyExistsException before any test ran.
quietly(() -> sesV2.deleteEmailIdentity(
DeleteEmailIdentityRequest.builder().emailIdentity(SENDER).build()));
setVdmEnabled(false);
sesV2.createEmailIdentity(CreateEmailIdentityRequest.builder().emailIdentity(SENDER).build());
}
@AfterAll
static void cleanup() {
if (sesV2 == null) {
return;
}
quietly(() -> sesV2.deleteEmailIdentity(
DeleteEmailIdentityRequest.builder().emailIdentity(SENDER).build()));
quietly(SesMessageInsightsTest::restoreVdm);
sesV2.close();
}
@Test
@Order(1)
@DisplayName("reports the VDM gate before it looks at the message id")
void vdmDisabledIsReportedFirst() {
assertThatThrownBy(() -> sesV2.getMessageInsights(
GetMessageInsightsRequest.builder().messageId("not-even-an-id").build()))
.isInstanceOf(NotFoundException.class)
.hasMessageContaining("Virtual Deliverability Manager");
}
@Test
@Order(2)
@DisplayName("an unknown message names the account alongside the id")
void unknownMessageNamesTheAccount() {
setVdmEnabled(true);
assertThatThrownBy(() -> sesV2.getMessageInsights(
GetMessageInsightsRequest.builder().messageId("no-such-message").build()))
.isInstanceOf(NotFoundException.class)
.hasMessageContaining("Message <no-such-message> not found for account <");
}
@Test
@Order(3)
@DisplayName("an ordinary recipient reports SEND then DELIVERY with the send's tags")
void ordinaryRecipientTimeline() {
String messageId = send("user@example.com", "compat-ordinary");
GetMessageInsightsResponse insights = sesV2.getMessageInsights(
GetMessageInsightsRequest.builder().messageId(messageId).build());
assertThat(insights.messageId()).isEqualTo(messageId);
assertThat(insights.fromEmailAddress()).isEqualTo(SENDER);
assertThat(insights.subject()).isEqualTo("compat-ordinary");
assertThat(insights.emailTags()).singleElement()
.satisfies(tag -> {
assertThat(tag.name()).isEqualTo("campaign");
assertThat(tag.value()).isEqualTo("compat");
});
assertThat(insights.insights()).singleElement().satisfies(recipient -> {
assertThat(recipient.destination()).isEqualTo("user@example.com");
assertThat(recipient.isp()).isEqualTo("UNKNOWN_ISP");
assertThat(recipient.events()).extracting(event -> event.type().toString())
.containsExactly("SEND", "DELIVERY");
assertThat(recipient.events()).allSatisfy(
event -> assertThat(event.timestamp()).isNotNull());
});
}
@Test
@Order(4)
@DisplayName("the bounce simulator carries the bounce details")
void bounceSimulatorTimeline() {
String messageId = send("bounce@simulator.amazonses.com", "compat-bounce");
GetMessageInsightsResponse insights = sesV2.getMessageInsights(
GetMessageInsightsRequest.builder().messageId(messageId).build());
List<InsightsEvent> events = insights.insights().get(0).events();
assertThat(events).extracting(event -> event.type().toString())
.containsExactly("SEND", "BOUNCE");
assertThat(events.get(1).details().bounce().bounceTypeAsString()).isEqualTo("PERMANENT");
assertThat(events.get(1).details().bounce().diagnosticCode()).contains("550 5.1.1");
}
@Test
@Order(5)
@DisplayName("Floci reports one timeline per recipient where AWS records nothing at all")
void multipleRecipientsGetOneTimelineEach() {
String messageId = sesV2.sendEmail(SendEmailRequest.builder()
.fromEmailAddress(SENDER)
.destination(Destination.builder()
.toAddresses("to@example.com")
.ccAddresses("cc@example.com")
.build())
.content(content("compat-multi"))
.build()).messageId();
GetMessageInsightsResponse insights = sesV2.getMessageInsights(
GetMessageInsightsRequest.builder().messageId(messageId).build());
assertThat(insights.insights()).extracting(EmailInsights::destination)
.containsExactly("to@example.com", "cc@example.com");
}
private static String send(String recipient, String subject) {
return sesV2.sendEmail(SendEmailRequest.builder()
.fromEmailAddress(SENDER)
.destination(Destination.builder().toAddresses(recipient).build())
.emailTags(MessageTag.builder().name("campaign").value("compat").build())
.content(content(subject))
.build()).messageId();
}
private static EmailContent content(String subject) {
return EmailContent.builder()
.simple(Message.builder()
.subject(Content.builder().data(subject).build())
.body(Body.builder().text(Content.builder().data("hi").build()).build())
.build())
.build();
}
private static void setVdmEnabled(boolean enabled) {
sesV2.putAccountVdmAttributes(PutAccountVdmAttributesRequest.builder()
.vdmAttributes(VdmAttributes.builder()
.vdmEnabled(enabled ? "ENABLED" : "DISABLED")
.build())
.build());
}
// VDM has no un-configure API, so a region that started unconfigured is left DISABLED rather
// than restored to absent. Same compromise as SesAccountAttributesTest.
private static void restoreVdm() {
VdmAttributes.Builder vdm = VdmAttributes.builder();
if (originalVdm != null && originalVdm.vdmEnabled() != null) {
vdm.vdmEnabled(originalVdm.vdmEnabled())
.dashboardAttributes(originalVdm.dashboardAttributes())
.guardianAttributes(originalVdm.guardianAttributes());
} else {
vdm.vdmEnabled("DISABLED");
}
sesV2.putAccountVdmAttributes(
PutAccountVdmAttributesRequest.builder().vdmAttributes(vdm.build()).build());
}
private static void quietly(Runnable action) {
try {
action.run();
} catch (RuntimeException expected) {
// Cleanup is best effort: a failed teardown must not mask the test result.
}
}
}