Light, fluffy, and always free - The AWS Local Emulator alternative
feat(ses): run custom verification emails through the shared send preamble (#4212)
Kenshin Okinaka678b50f14b9d
diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesService.javaindex 91d164a731..d64b58da98 100644--- a/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java+++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java@@ -692,9 +692,11 @@ public class SesService {"Email address is not verified. The following identities failed the check in region "+ region.toUpperCase(Locale.ROOT) + ": " + template.getFromEmailAddress(), 400);}- if (configurationSetName != null && !configurationSetName.isBlank()) {- configSetService.get(configurationSetName, region);- }+ String effectiveConfigSet = resolveDefaultConfigurationSet(configurationSetName,+ template.getFromEmailAddress(), region);+ configSetService.validateForSending(effectiveConfigSet, region);+ Map<String, String> suppressedReasons =+ collectSuppressedReasons(List.of(emailAddress), effectiveConfigSet, region);// AWS registers the recipient as a pending-verification identity as part of sending the// verification email, so ListIdentities / GetIdentityVerificationAttributes surface it.@@ -708,27 +710,42 @@ public class SesService {String renderedHtml = body + "<p>" + CUSTOM_VERIFICATION_DISCLAIMER + "</p>";String messageId = UUID.randomUUID().toString();+ // The template content is account-supplied, so it goes through the same content scan as+ // any other send; a rejected verification email is recorded without its body and not relayed.+ boolean rejected = SesContentScan.containsTestVirus(template.getTemplateSubject(), renderedHtml);SentEmail email = new SentEmail(messageId, region, template.getFromEmailAddress(),List.of(emailAddress), List.of(), List.of(), List.of(),template.getTemplateSubject(), null, renderedHtml);email.setReturnPath(template.getFromEmailAddress());email.setInsights(SesMessageInsights.build(List.of(emailAddress),- SesRecipientEvents.classify(List.of(emailAddress), Map.of(), false),+ SesRecipientEvents.classify(List.of(emailAddress), suppressedReasons, rejected),email.getSentAt()));+ if (rejected) {+ email.discardContent(SesRecipientEvents.CONTENT_REJECT_REASON);+ }sentEmailService.record(region, messageId, email);- smtpRelay.relay(SmtpRelay.RelayMessage.builder(template.getFromEmailAddress())- .returnPath(template.getFromEmailAddress())- .to(List.of(emailAddress))- .cc(List.of())- .bcc(List.of())- .replyTo(List.of())- .subject(template.getTemplateSubject())- .bodyHtml(renderedHtml)- .headers(List.of())- .messageId(messageId)- .build());- LOG.infov("SES custom verification email sent: to={0}, template={1}, messageId={2}",- emailAddress, templateName, messageId);+ if (!rejected && suppressedReasons.isEmpty()) {+ smtpRelay.relay(SmtpRelay.RelayMessage.builder(template.getFromEmailAddress())+ .returnPath(template.getFromEmailAddress())+ .to(List.of(emailAddress))+ .cc(List.of())+ .bcc(List.of())+ .replyTo(List.of())+ .subject(template.getTemplateSubject())+ .bodyHtml(renderedHtml)+ .headers(List.of())+ .messageId(messageId)+ .build());+ LOG.infov("SES custom verification email sent: to={0}, template={1}, messageId={2}",+ emailAddress, templateName, messageId);+ } else {+ LOG.infov("SES custom verification email not relayed ({0}): to={1}, template={2}, messageId={3}",+ rejected ? "content rejected" : "recipient suppressed", emailAddress, templateName,+ messageId);+ }+ publishSendEvents(effectiveConfigSet, messageId, template.getFromEmailAddress(),+ template.getTemplateSubject(), List.of(emailAddress), List.of(), List.of(),+ List.of(emailAddress), suppressedReasons, rejected, List.of(), List.of(), region);return messageId;}diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesCustomVerificationEmailTemplateV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesCustomVerificationEmailTemplateV2IntegrationTest.javaindex dee96e5572..c125463f8f 100644--- a/src/test/java/io/github/hectorvent/floci/services/ses/SesCustomVerificationEmailTemplateV2IntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesCustomVerificationEmailTemplateV2IntegrationTest.java@@ -9,6 +9,8 @@ import org.junit.jupiter.api.TestMethodOrder;import static io.restassured.RestAssured.given;import static org.hamcrest.Matchers.equalTo;import static org.hamcrest.Matchers.hasItem;+import static org.hamcrest.Matchers.hasKey;+import static org.hamcrest.Matchers.not;import static org.hamcrest.Matchers.nullValue;/**@@ -155,4 +157,40 @@ class SesCustomVerificationEmailTemplateV2IntegrationTest {.body("__type", equalTo("BadRequestException")).body("message", equalTo("The success redirection URL is invalid"));}++ @Test+ @Order(11)+ void sendWithTemplateCarryingTheTestSignature_isRecordedWithoutItsBodyAndNotRelayed() {+ // The signature comes from the scanner so it never appears in test source; the JSON needs+ // its one backslash escaped.+ String content = SesContentScan.signature().replace("\\", "\\\\");+ given().contentType("application/json").header("Authorization", AUTH)+ .body("""+ {"TemplateName": "cvet-v2-scan", "FromEmailAddress": "%s", "TemplateSubject": "scan",+ "TemplateContent": "<p>%s</p>",+ "SuccessRedirectionURL": "https://example.com/ok",+ "FailureRedirectionURL": "https://example.com/ng"}+ """.formatted(FROM, content))+ .when().post(BASE).then().statusCode(200);++ // Sending registers the recipient as a pending identity in the shared region, so it is+ // removed again below; a domain other tests assert on must not be used here.+ String messageId = given().contentType("application/json").header("Authorization", AUTH)+ .body("{\"EmailAddress\":\"[email protected]\",\"TemplateName\":\"cvet-v2-scan\"}")+ .when().post("/v2/email/outbound-custom-verification-emails").then().statusCode(200)+ .extract().jsonPath().getString("MessageId");++ try {+ given().header("Authorization", AUTH)+ .when().get("/_aws/ses?id=" + messageId).then().statusCode(200)+ .body("messages[0].RejectReason", equalTo("Bad content"))+ .body("messages[0]", not(hasKey("Subject")))+ .body("messages[0]", not(hasKey("Body")));+ } finally {+ given().header("Authorization", AUTH)+ .when().delete("/v2/email/identities/[email protected]");+ given().header("Authorization", AUTH)+ .when().delete(BASE + "/cvet-v2-scan");+ }+ }}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.javaindex 809726e689..a74e52d4ed 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@@ -1809,6 +1809,109 @@ class SesEventPublishingV2IntegrationTest {}}+ @Test+ @Order(37)+ void customVerificationEmail_publishesThroughTheSameClassifierAsAnyOtherSend() throws Exception {+ // SendCustomVerificationEmail accepts a ConfigurationSetName like the other send APIs, so a+ // clean verification email publishes a Send and one carrying the signature adds a Reject.+ String signature = SesContentScan.signature().replace("\\", "\\\\");+ String recipient = "[email protected]";+ createCustomVerificationTemplate("cvet-events-clean", "<p>verify</p>");+ createCustomVerificationTemplate("cvet-events-scan", "<p>" + signature + "</p>");+ try {+ drainQueue();+ sendCustomVerificationEmail(recipient, "cvet-events-clean", CS);+ List<JsonNode> clean = receiveSesEvents(1);+ assertEquals(List.of("Send"), clean.stream().map(e -> e.path("eventType").asText()).toList());++ drainQueue();+ String messageId = sendCustomVerificationEmail(recipient, "cvet-events-scan", CS);+ List<JsonNode> rejected = receiveSesEvents(2);+ assertEquals(2, rejected.size(), "expected Send and Reject");+ assertTrue(rejected.stream().anyMatch(e -> "Reject".equals(e.path("eventType").asText())));+ assertTrue(rejected.stream().allMatch(e -> messageId.equals(e.path("mail").path("messageId").asText())));+ } finally {+ given().header("Authorization", SES_AUTH).when().delete("/v2/email/identities/" + recipient);+ given().header("Authorization", SES_AUTH)+ .when().delete("/v2/email/custom-verification-email-templates/cvet-events-clean");+ given().header("Authorization", SES_AUTH)+ .when().delete("/v2/email/custom-verification-email-templates/cvet-events-scan");+ }+ }++ private void createCustomVerificationTemplate(String name, String content) {+ given().contentType("application/json").header("Authorization", SES_AUTH)+ .body("""+ {"TemplateName": "%s", "FromEmailAddress": "%s", "TemplateSubject": "verify",+ "TemplateContent": "%s",+ "SuccessRedirectionURL": "https://example.com/ok",+ "FailureRedirectionURL": "https://example.com/ng"}+ """.formatted(name, SENDER, content))+ .when().post("/v2/email/custom-verification-email-templates").then().statusCode(200);+ }++ private String sendCustomVerificationEmail(String recipient, String templateName, String configurationSet) {+ String configurationSetField = configurationSet == null+ ? "" : ", \"ConfigurationSetName\": \"" + configurationSet + "\"";+ return given().contentType("application/json").header("Authorization", SES_AUTH)+ .body("""+ {"EmailAddress": "%s", "TemplateName": "%s"%s}+ """.formatted(recipient, templateName, configurationSetField))+ .when().post("/v2/email/outbound-custom-verification-emails").then().statusCode(200)+ .extract().jsonPath().getString("MessageId");+ }++ @Test+ @Order(38)+ void customVerificationEmail_sharesTheSendPreambleWithTheOtherSendPaths() throws Exception {+ // The identity's default configuration set, the account suppression list and a paused+ // configuration set apply to a verification email exactly as they do to SendEmail.+ String recipient = "[email protected]";+ createCustomVerificationTemplate("cvet-preamble", "<p>verify</p>");+ try {+ given().contentType("application/json").header("Authorization", SES_AUTH)+ .body("{\"ConfigurationSetName\":\"" + CS + "\"}")+ .when().put("/v2/email/identities/" + SENDER + "/configuration-set").then().statusCode(200);+ drainQueue();+ sendCustomVerificationEmail(recipient, "cvet-preamble", null);+ assertEquals(List.of("Send"),+ receiveSesEvents(1).stream().map(e -> e.path("eventType").asText()).toList(),+ "the identity default configuration set publishes without an explicit name");++ given().contentType("application/json").header("Authorization", SES_AUTH)+ .body("{\"EmailAddress\":\"" + recipient + "\",\"Reason\":\"BOUNCE\"}")+ .when().put("/v2/email/suppression/addresses").then().statusCode(200);+ drainQueue();+ sendCustomVerificationEmail(recipient, "cvet-preamble", CS);+ JsonNode bounce = receiveSesEvents(2).stream()+ .filter(e -> "Bounce".equals(e.path("eventType").asText())).findFirst().orElseThrow();+ assertEquals("OnAccountSuppressionList", bounce.path("bounce").path("bounceSubType").asText());++ given().contentType("application/json").header("Authorization", SES_AUTH)+ .body("{\"ConfigurationSetName\":\"cvet-paused\"}")+ .when().post("/v2/email/configuration-sets").then().statusCode(200);+ given().contentType("application/json").header("Authorization", SES_AUTH)+ .body("{\"SendingEnabled\":false}")+ .when().put("/v2/email/configuration-sets/cvet-paused/sending").then().statusCode(200);+ given().contentType("application/json").header("Authorization", SES_AUTH)+ .body("""+ {"EmailAddress": "%s", "TemplateName": "cvet-preamble", "ConfigurationSetName": "cvet-paused"}+ """.formatted(recipient))+ .when().post("/v2/email/outbound-custom-verification-emails").then().statusCode(400)+ .body("__type", equalTo("SendingPausedException"));+ } finally {+ given().contentType("application/json").header("Authorization", SES_AUTH).body("{}")+ .when().put("/v2/email/identities/" + SENDER + "/configuration-set");+ given().header("Authorization", SES_AUTH)+ .when().delete("/v2/email/suppression/addresses/" + recipient);+ given().header("Authorization", SES_AUTH)+ .when().delete("/v2/email/configuration-sets/cvet-paused");+ given().header("Authorization", SES_AUTH)+ .when().delete("/v2/email/custom-verification-email-templates/cvet-preamble");+ given().header("Authorization", SES_AUTH).when().delete("/v2/email/identities/" + recipient);+ }+ }+private void sendEmail(String to) {given().contentType("application/json")diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceSmtpTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceSmtpTest.javaindex f4df69dcc0..2374db7ffb 100644--- a/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceSmtpTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceSmtpTest.java@@ -3,6 +3,8 @@ package io.github.hectorvent.floci.services.ses;import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.storage.InMemoryStorage;import io.github.hectorvent.floci.services.ses.model.ConfigurationSet;+import io.github.hectorvent.floci.services.ses.model.CustomVerificationEmailTemplate;+import io.github.hectorvent.floci.services.ses.model.InsightsEvent;import io.github.hectorvent.floci.services.ses.model.SentEmail;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;@@ -27,6 +29,8 @@ class SesServiceSmtpTest {private SesConfigurationSetService configSets;private SesSentEmailService sentEmails;private SesSuppressionService suppression;+ private SesCvetService cvetTemplates;+ private SesIdentityService identities;private InMemoryStorage<String, SentEmail> emailStore;@BeforeEach@@ -37,6 +41,57 @@ class SesServiceSmtpTest {configSets = builder.configSetService();sentEmails = builder.sentEmailService();suppression = builder.suppressionService();+ cvetTemplates = builder.cvetService();+ identities = builder.identityService();+ }++ private void cvetTemplate(String name, String subject, String content) {+ identities.verifyEmailIdentity("[email protected]", "us-east-1");+ CustomVerificationEmailTemplate template = new CustomVerificationEmailTemplate();+ template.setTemplateName(name);+ template.setFromEmailAddress("[email protected]");+ template.setTemplateSubject(subject);+ template.setTemplateContent(content);+ template.setSuccessRedirectionURL("https://example.com/ok");+ template.setFailureRedirectionURL("https://example.com/ng");+ cvetTemplates.createCustomVerificationEmailTemplate(template, "us-east-1");+ }++ @Test+ void sendCustomVerificationEmail_signatureInTheTemplateSubject_isRejectedAndNotRelayed() {+ // The scan covers the subject as well as the content, and neither reaches the relay.+ cvetTemplate("cvet-subject", SesContentScan.signature(), "<p>clean</p>");++ String messageId = service.sendCustomVerificationEmail("[email protected]", "cvet-subject",+ null, "us-east-1");++ assertEquals("Bad content", storedEmail(messageId).getRejectReason());+ assertEquals(List.of("SEND", "REJECT"), insightsEventTypes(messageId));+ verify(smtpRelay, never()).relay(any());+ }++ @Test+ void sendCustomVerificationEmail_signatureInTheTemplateContent_isRejectedAndNotRelayed() {+ cvetTemplate("cvet-content", "clean", "<p>" + SesContentScan.signature() + "</p>");++ String messageId = service.sendCustomVerificationEmail("[email protected]", "cvet-content",+ null, "us-east-1");++ assertEquals("Bad content", storedEmail(messageId).getRejectReason());+ verify(smtpRelay, never()).relay(any());+ }++ @Test+ void sendCustomVerificationEmail_suppressedRecipient_isRecordedButNotRelayed() {+ cvetTemplate("cvet-suppressed", "clean", "<p>clean</p>");+ suppression.putSuppressedDestination("us-east-1", "[email protected]", "BOUNCE");++ String messageId = service.sendCustomVerificationEmail("[email protected]",+ "cvet-suppressed", null, "us-east-1");++ assertNull(storedEmail(messageId).getRejectReason());+ assertEquals(List.of("SEND", "BOUNCE"), insightsEventTypes(messageId));+ verify(smtpRelay, never()).relay(any());}private SentEmail storedEmail(String messageId) {@@ -46,6 +101,12 @@ class SesServiceSmtpTest {.orElseThrow();}+ private List<String> insightsEventTypes(String messageId) {+ return storedEmail(messageId).getInsights().get(0).events().stream()+ .map(InsightsEvent::type)+ .toList();+ }+private SmtpRelay.RelayMessage capturedRelay() {ArgumentCaptor<SmtpRelay.RelayMessage> captor =ArgumentCaptor.forClass(SmtpRelay.RelayMessage.class);diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceTestBuilder.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceTestBuilder.javaindex ad3236999e..bd18b4df3a 100644--- a/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceTestBuilder.java+++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceTestBuilder.java@@ -58,6 +58,7 @@ final class SesServiceTestBuilder {private SesSuppressionService suppressionService;private SesConfigurationSetService configSetService;private SesIdentityService identityService;+ private SesCvetService cvetService;private SesSentEmailService sentEmailService;static SesServiceTestBuilder create() {@@ -142,6 +143,13 @@ final class SesServiceTestBuilder {return sentEmailService;}+ SesCvetService cvetService() {+ if (cvetService == null) {+ throw new IllegalStateException("call build() first");+ }+ return cvetService;+ }+SesIdentityService identityService() {if (identityService == null) {throw new IllegalStateException("call build() first");@@ -156,6 +164,7 @@ final class SesServiceTestBuilder {configSetService = new SesConfigurationSetService(configSetStore);identityService = new SesIdentityService(identityStore, route53Service, clock);sentEmailService = new SesSentEmailService(emailStore);+ cvetService = new SesCvetService(cvetStore);return new SesService(identityService,sentEmailService,@@ -165,7 +174,7 @@ final class SesServiceTestBuilder {new SesDedicatedIpService(dedicatedIpPoolStore),contactService,new SesPolicyService(policyStore, objectMapper),- new SesCvetService(cvetStore),+ cvetService,new SesTenantService(tenantStore, tenantAssociationStore, clock, new SecureRandom()),smtpRelay);}