diff --git a/docs/services/ses.md b/docs/services/ses.md index 87972eeb21..b705c9306e 100644 --- a/docs/services/ses.md +++ b/docs/services/ses.md @@ -28,7 +28,7 @@ Floci exposes the classic Amazon SES Query API used by `aws ses ...` commands an | `CreateCustomVerificationEmailTemplate` | Create a custom verification email template | | `GetCustomVerificationEmailTemplate` | Return a custom verification email template | | `ListCustomVerificationEmailTemplates` | List custom verification email templates (no content) | -| `UpdateCustomVerificationEmailTemplate` | Replace a custom verification email template | +| `UpdateCustomVerificationEmailTemplate` | Update a custom verification email template | | `DeleteCustomVerificationEmailTemplate` | Delete a custom verification email template | | `SendCustomVerificationEmail` | Send a custom verification email and register the recipient as a pending identity | | `GetSendQuota` | Return local send quota counters | diff --git a/src/main/java/io/floci/aws/services/ses/SesCvetController.java b/src/main/java/io/floci/aws/services/ses/SesCvetController.java index 4774acba29..eca3954a99 100644 --- a/src/main/java/io/floci/aws/services/ses/SesCvetController.java +++ b/src/main/java/io/floci/aws/services/ses/SesCvetController.java @@ -116,7 +116,7 @@ public class SesCvetController { try { CustomVerificationEmailTemplate t = parseCvet(readOptionBody(objectMapper, body)); t.setTemplateName(templateName); - sesService.updateCustomVerificationEmailTemplate(t, region); + sesService.replaceCustomVerificationEmailTemplate(t, region); return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { throw remapCvetException(e, templateName); @@ -150,8 +150,11 @@ public class SesCvetController { ObjectNode o = objectMapper.createObjectNode(); o.put("TemplateName", t.getTemplateName()); o.put("FromEmailAddress", t.getFromEmailAddress()); - o.put("TemplateSubject", t.getTemplateSubject()); - if (includeContent) { + // A v1 update can clear the subject or the content; a cleared member is left out. + if (t.getTemplateSubject() != null) { + o.put("TemplateSubject", t.getTemplateSubject()); + } + if (includeContent && t.getTemplateContent() != null) { o.put("TemplateContent", t.getTemplateContent()); } o.put("SuccessRedirectionURL", t.getSuccessRedirectionURL()); diff --git a/src/main/java/io/floci/aws/services/ses/SesCvetService.java b/src/main/java/io/floci/aws/services/ses/SesCvetService.java index 871e534b0b..1714772f18 100644 --- a/src/main/java/io/floci/aws/services/ses/SesCvetService.java +++ b/src/main/java/io/floci/aws/services/ses/SesCvetService.java @@ -88,7 +88,11 @@ public class SesCvetService implements SesTaggable { .toList(); } - public void updateCustomVerificationEmailTemplate(CustomVerificationEmailTemplate template, String region) { + /** + * The v2 update: the template given replaces the stored one, which keeps only its tags. The v1 + * update is {@link #patchCustomVerificationEmailTemplate}. + */ + public void replaceCustomVerificationEmailTemplate(CustomVerificationEmailTemplate template, String region) { String key = cvetKey(region, template.getTemplateName()); // Guard the existence check and the put together so a concurrent delete can't slip between // them and have the update resurrect the just-deleted template. @@ -104,6 +108,44 @@ public class SesCvetService implements SesTaggable { template.getTemplateName(), region); } + /** + * The v1 update (probed): a member left out keeps its stored value, and an empty subject clears + * the stored one, so the template then reads back without it. The content follows the subject. + * An empty sender or redirection URL was not probed and is treated as left out, which keeps the + * template sendable. + */ + public void patchCustomVerificationEmailTemplate(CustomVerificationEmailTemplate changes, String region) { + String key = cvetKey(region, changes.getTemplateName()); + synchronized (cvetMutationLock) { + CustomVerificationEmailTemplate existing = cvetStore.get(key) + .orElseThrow(SesCvetService::cvetNotFound); + CustomVerificationEmailTemplate patched = new CustomVerificationEmailTemplate(); + patched.setTemplateName(existing.getTemplateName()); + patched.setFromEmailAddress(kept(changes.getFromEmailAddress(), existing.getFromEmailAddress())); + patched.setTemplateSubject(cleared(changes.getTemplateSubject(), existing.getTemplateSubject())); + patched.setTemplateContent(cleared(changes.getTemplateContent(), existing.getTemplateContent())); + patched.setSuccessRedirectionURL( + kept(changes.getSuccessRedirectionURL(), existing.getSuccessRedirectionURL())); + patched.setFailureRedirectionURL( + kept(changes.getFailureRedirectionURL(), existing.getFailureRedirectionURL())); + patched.setTags(new ArrayList<>(existing.getTags())); + cvetStore.put(key, patched); + } + LOG.infov("Updated custom verification email template {0} in region {1}", + changes.getTemplateName(), region); + } + + private static String kept(String change, String stored) { + return change == null || change.isEmpty() ? stored : change; + } + + private static String cleared(String change, String stored) { + if (change == null) { + return stored; + } + return change.isEmpty() ? null : change; + } + /** * Reports whether the template existed instead of throwing, because the two protocols disagree * (probed): v1 answers 200 for an unknown name, v2 answers 404. diff --git a/src/main/java/io/floci/aws/services/ses/SesQueryHandler.java b/src/main/java/io/floci/aws/services/ses/SesQueryHandler.java index b609d95503..a0b97d4911 100644 --- a/src/main/java/io/floci/aws/services/ses/SesQueryHandler.java +++ b/src/main/java/io/floci/aws/services/ses/SesQueryHandler.java @@ -687,7 +687,7 @@ public class SesQueryHandler { } private Response handleUpdateCustomVerificationEmailTemplate(MultivaluedMap params, String region) { - sesService.updateCustomVerificationEmailTemplate(readCvetParams(params), region); + sesService.patchCustomVerificationEmailTemplate(readCvetParams(params), region); return Response.ok(AwsQueryResponse.envelopeEmptyResult( "UpdateCustomVerificationEmailTemplate", AwsNamespaces.SES)).build(); } diff --git a/src/main/java/io/floci/aws/services/ses/SesService.java b/src/main/java/io/floci/aws/services/ses/SesService.java index 1ae5da46cf..a7317a51cb 100644 --- a/src/main/java/io/floci/aws/services/ses/SesService.java +++ b/src/main/java/io/floci/aws/services/ses/SesService.java @@ -897,11 +897,40 @@ public class SesService { cvetService.createCustomVerificationEmailTemplate(template, region); } - public void updateCustomVerificationEmailTemplate(CustomVerificationEmailTemplate template, String region) { + /** + * The v2 update, which takes every member and replaces the stored template with them. The v1 + * update is {@link #patchCustomVerificationEmailTemplate}. + */ + public void replaceCustomVerificationEmailTemplate(CustomVerificationEmailTemplate template, String region) { // Validate (including the From-verified identity check and the required-field checks) before // delegating the storage update, matching createCustomVerificationEmailTemplate. validateCustomVerificationTemplate(template, region); - cvetService.updateCustomVerificationEmailTemplate(template, region); + cvetService.replaceCustomVerificationEmailTemplate(template, region); + } + + /** + * The v1 update, which unlike the v2 one takes any subset of the members (probed). Only the + * members given are checked, redirection URLs before the sender, and all of it before the + * template's existence. An empty member is checked as if it were absent. + */ + public void patchCustomVerificationEmailTemplate(CustomVerificationEmailTemplate changes, String region) { + requireCvetField(changes.getTemplateName(), "TemplateName"); + if (isGiven(changes.getSuccessRedirectionURL()) && !isValidRedirectUrl(changes.getSuccessRedirectionURL())) { + throw new AwsException("InvalidParameterValue", "The success redirection URL is invalid.", 400); + } + if (isGiven(changes.getFailureRedirectionURL()) && !isValidRedirectUrl(changes.getFailureRedirectionURL())) { + throw new AwsException("InvalidParameterValue", "The failure redirection URL is invalid.", 400); + } + if (isGiven(changes.getFromEmailAddress()) + && !identityService.isVerifiedSender(changes.getFromEmailAddress(), region)) { + throw new AwsException("FromEmailAddressNotVerified", + "The from email address " + changes.getFromEmailAddress() + " is not verified.", 400); + } + cvetService.patchCustomVerificationEmailTemplate(changes, region); + } + + private static boolean isGiven(String member) { + return member != null && !member.isEmpty(); } // AWS appends this exact disclaimer to the end of every custom verification email and it cannot diff --git a/src/test/java/io/floci/aws/services/ses/SesCustomVerificationEmailTemplateV1IntegrationTest.java b/src/test/java/io/floci/aws/services/ses/SesCustomVerificationEmailTemplateV1IntegrationTest.java index 4b520d28d6..7bf62ed7fb 100644 --- a/src/test/java/io/floci/aws/services/ses/SesCustomVerificationEmailTemplateV1IntegrationTest.java +++ b/src/test/java/io/floci/aws/services/ses/SesCustomVerificationEmailTemplateV1IntegrationTest.java @@ -10,6 +10,8 @@ import org.junit.jupiter.api.TestMethodOrder; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasKey; +import static org.hamcrest.Matchers.not; /** * Integration tests for the SES V1 Query-protocol custom-verification-email-template APIs. @@ -116,6 +118,83 @@ class SesCustomVerificationEmailTemplateV1IntegrationTest { .body(containsString("" + NAME + "")); } + @Test + @Order(14) + void update_checksOnlyTheMembersGivenBeforeTheTemplateExists() { + // Probed: the name alone reaches the existence check, and a member that is given is + // checked first, redirection URLs before the sender. + SesV1Requests.query("UpdateCustomVerificationEmailTemplate").formParam("TemplateName", "cvet-v1-ghost") + .when().post("/").then().statusCode(400) + .body(containsString("CustomVerificationEmailTemplateDoesNotExist")); + SesV1Requests.query("UpdateCustomVerificationEmailTemplate").formParam("TemplateName", "cvet-v1-ghost") + .formParam("TemplateSubject", "").formParam("TemplateContent", "") + .when().post("/").then().statusCode(400) + .body(containsString("CustomVerificationEmailTemplateDoesNotExist")); + SesV1Requests.query("UpdateCustomVerificationEmailTemplate").formParam("TemplateName", "cvet-v1-ghost") + .formParam("FromEmailAddress", "nobody@unverified.test") + .when().post("/").then().statusCode(400) + .body(containsString("FromEmailAddressNotVerified")) + .body(containsString( + "The from email address nobody@unverified.test is not verified.")); + SesV1Requests.query("UpdateCustomVerificationEmailTemplate").formParam("TemplateName", "cvet-v1-ghost") + .formParam("FailureRedirectionURL", "not-a-url") + .when().post("/").then().statusCode(400) + .body(containsString("InvalidParameterValue")) + .body(containsString("The failure redirection URL is invalid.")); + SesV1Requests.query("UpdateCustomVerificationEmailTemplate").formParam("TemplateName", "cvet-v1-ghost") + .formParam("FromEmailAddress", "nobody@unverified.test") + .formParam("SuccessRedirectionURL", "not-a-url") + .formParam("FailureRedirectionURL", "not-a-url") + .when().post("/").then().statusCode(400) + .body(containsString("InvalidParameterValue")) + .body(containsString("The success redirection URL is invalid.")); + } + + @Test + @Order(15) + void update_changesOnlyTheMembersGiven_andAnEmptySubjectClearsIt() { + String name = "cvet-v1-partial"; + create(name, "Verify your email").when().post("/").then().statusCode(200); + + SesV1Requests.query("UpdateCustomVerificationEmailTemplate").formParam("TemplateName", name) + .formParam("TemplateSubject", "Verify again") + .when().post("/").then().statusCode(200); + SesV1Requests.query("GetCustomVerificationEmailTemplate").formParam("TemplateName", name) + .when().post("/").then().statusCode(200) + .body(containsString("Verify again")) + .body(containsString("" + FROM + "")) + .body(containsString("<html><body>verify</body></html>" + + "")) + .body(containsString("https://example.com/ok")) + .body(containsString("https://example.com/fail")); + + // A refused update leaves the stored template as it was. + SesV1Requests.query("UpdateCustomVerificationEmailTemplate").formParam("TemplateName", name) + .formParam("TemplateSubject", "Never stored") + .formParam("FromEmailAddress", "nobody@unverified.test") + .when().post("/").then().statusCode(400) + .body(containsString("FromEmailAddressNotVerified")); + SesV1Requests.query("GetCustomVerificationEmailTemplate").formParam("TemplateName", name) + .when().post("/").then().statusCode(200) + .body(containsString("Verify again")); + + SesV1Requests.query("UpdateCustomVerificationEmailTemplate").formParam("TemplateName", name) + .formParam("TemplateSubject", "") + .when().post("/").then().statusCode(200); + SesV1Requests.query("GetCustomVerificationEmailTemplate").formParam("TemplateName", name) + .when().post("/").then().statusCode(200) + .body(not(containsString(""))) + .body(containsString("" + FROM + "")); + // The v2 view of the same template leaves the cleared member out instead of answering null. + given().header("Authorization", SesV2Requests.AUTH) + .when().get("/v2/email/custom-verification-email-templates/" + name).then().statusCode(200) + .body("$", not(hasKey("TemplateSubject"))) + .body("FromEmailAddress", equalTo(FROM)); + + SesV1Requests.query("DeleteCustomVerificationEmailTemplate").formParam("TemplateName", name) + .when().post("/").then().statusCode(200); + } + @Test @Order(4) void update() { diff --git a/src/test/java/io/floci/aws/services/ses/SesCvetServiceTest.java b/src/test/java/io/floci/aws/services/ses/SesCvetServiceTest.java index f5e20d5f9c..2fb6450f66 100644 --- a/src/test/java/io/floci/aws/services/ses/SesCvetServiceTest.java +++ b/src/test/java/io/floci/aws/services/ses/SesCvetServiceTest.java @@ -11,6 +11,7 @@ import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -92,17 +93,66 @@ class SesCvetServiceTest { CustomVerificationEmailTemplate replacement = template("t1"); replacement.setTemplateSubject("Verify again"); - service.updateCustomVerificationEmailTemplate(replacement, REGION); + service.replaceCustomVerificationEmailTemplate(replacement, REGION); CustomVerificationEmailTemplate updated = service.getCustomVerificationEmailTemplate("t1", REGION); assertEquals("Verify again", updated.getTemplateSubject()); assertEquals(List.of(new Tag("env", "dev")), updated.getTags()); } + @Test + void patch_keepsTheMembersLeftOutAndTheTags() { + service.createCustomVerificationEmailTemplate(template("t1"), REGION); + service.tag("t1", REGION, List.of(new Tag("env", "dev"))); + CustomVerificationEmailTemplate before = service.getCustomVerificationEmailTemplate("t1", REGION); + + CustomVerificationEmailTemplate changes = new CustomVerificationEmailTemplate(); + changes.setTemplateName("t1"); + changes.setTemplateSubject("Verify again"); + service.patchCustomVerificationEmailTemplate(changes, REGION); + + CustomVerificationEmailTemplate patched = service.getCustomVerificationEmailTemplate("t1", REGION); + assertEquals("Verify again", patched.getTemplateSubject()); + assertEquals(before.getFromEmailAddress(), patched.getFromEmailAddress()); + assertEquals(before.getTemplateContent(), patched.getTemplateContent()); + assertEquals(before.getSuccessRedirectionURL(), patched.getSuccessRedirectionURL()); + assertEquals(before.getFailureRedirectionURL(), patched.getFailureRedirectionURL()); + assertEquals(List.of(new Tag("env", "dev")), patched.getTags()); + } + + @Test + void patch_emptySubjectAndContentClear_emptySenderAndUrlsAreLeftAlone() { + service.createCustomVerificationEmailTemplate(template("t1"), REGION); + CustomVerificationEmailTemplate before = service.getCustomVerificationEmailTemplate("t1", REGION); + + CustomVerificationEmailTemplate changes = new CustomVerificationEmailTemplate(); + changes.setTemplateName("t1"); + changes.setTemplateSubject(""); + changes.setTemplateContent(""); + changes.setFromEmailAddress(""); + changes.setSuccessRedirectionURL(""); + changes.setFailureRedirectionURL(""); + service.patchCustomVerificationEmailTemplate(changes, REGION); + + CustomVerificationEmailTemplate patched = service.getCustomVerificationEmailTemplate("t1", REGION); + assertNull(patched.getTemplateSubject()); + assertNull(patched.getTemplateContent()); + assertEquals(before.getFromEmailAddress(), patched.getFromEmailAddress()); + assertEquals(before.getSuccessRedirectionURL(), patched.getSuccessRedirectionURL()); + assertEquals(before.getFailureRedirectionURL(), patched.getFailureRedirectionURL()); + } + + @Test + void patch_unknown_throwsDoesNotExist() { + AwsException e = assertThrows(AwsException.class, + () -> service.patchCustomVerificationEmailTemplate(template("ghost"), REGION)); + assertEquals("CustomVerificationEmailTemplateDoesNotExist", e.getErrorCode()); + } + @Test void update_unknown_throwsDoesNotExist() { AwsException e = assertThrows(AwsException.class, - () -> service.updateCustomVerificationEmailTemplate(template("ghost"), REGION)); + () -> service.replaceCustomVerificationEmailTemplate(template("ghost"), REGION)); assertEquals("CustomVerificationEmailTemplateDoesNotExist", e.getErrorCode()); assertEquals("Provided custom verification email template name does not exist.", e.getMessage()); }