From bde90de4d4d9fc28617fccb3da30231f3b739004 Mon Sep 17 00:00:00 2001 From: Kenshin Okinaka Date: Thu, 26 Nov 2026 10:59:59 +0900 Subject: [PATCH] fix(ses): word the v1 custom verification template create errors as SES does (#9440) --- .../aws/services/ses/SesCvetController.java | 24 +++++++--- .../io/floci/aws/services/ses/SesService.java | 46 +++++++++---------- .../io/floci/aws/services/ses/SesV2Json.java | 20 ++++++++ ...icationEmailTemplateV1IntegrationTest.java | 34 ++++++++++---- ...icationEmailTemplateV2IntegrationTest.java | 24 ++++++++++ .../floci/aws/services/ses/SesV2JsonTest.java | 33 +++++++++++++ 6 files changed, 141 insertions(+), 40 deletions(-) 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 eca3954a99..634629ac12 100644 --- a/src/main/java/io/floci/aws/services/ses/SesCvetController.java +++ b/src/main/java/io/floci/aws/services/ses/SesCvetController.java @@ -28,6 +28,7 @@ import static io.floci.aws.services.ses.SesV2Json.cvetNotFound; import static io.floci.aws.services.ses.SesV2Json.parseTagsArray; import static io.floci.aws.services.ses.SesV2Json.readOptionBody; import static io.floci.aws.services.ses.SesV2Json.remapCvetException; +import static io.floci.aws.services.ses.SesV2Json.remapCvetWriteException; import static io.floci.aws.services.ses.SesV2Json.remapV1Exception; /** @@ -63,17 +64,17 @@ public class SesCvetController { @Path("/custom-verification-email-templates") public Response createCustomVerificationEmailTemplate(@Context HttpHeaders headers, String body) { String region = regionResolver.resolveRegion(headers); + CustomVerificationEmailTemplate t; try { JsonNode request = readOptionBody(objectMapper, body); - CustomVerificationEmailTemplate t = parseCvet(request); + t = parseCvet(request); // Tags exist only on the create request; UpdateCustomVerificationEmailTemplate has no // Tags member and preserves the stored ones. t.setTags(parseTagsArray(request.path("Tags"))); - sesService.createCustomVerificationEmailTemplate(t, region); - return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { throw remapV1Exception(e); } + return writeCvet(t, () -> sesService.createCustomVerificationEmailTemplate(t, region)); } @GET @@ -113,13 +114,22 @@ public class SesCvetController { public Response updateCustomVerificationEmailTemplate(@Context HttpHeaders headers, @PathParam("templateName") String templateName, String body) { String region = regionResolver.resolveRegion(headers); + CustomVerificationEmailTemplate t; try { - CustomVerificationEmailTemplate t = parseCvet(readOptionBody(objectMapper, body)); - t.setTemplateName(templateName); - sesService.replaceCustomVerificationEmailTemplate(t, region); + t = parseCvet(readOptionBody(objectMapper, body)); + } catch (AwsException e) { + throw remapV1Exception(e); + } + t.setTemplateName(templateName); + return writeCvet(t, () -> sesService.replaceCustomVerificationEmailTemplate(t, region)); + } + + private Response writeCvet(CustomVerificationEmailTemplate template, Runnable write) { + try { + write.run(); return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { - throw remapCvetException(e, templateName); + throw remapCvetWriteException(e, template); } } 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 a7317a51cb..284a0cee27 100644 --- a/src/main/java/io/floci/aws/services/ses/SesService.java +++ b/src/main/java/io/floci/aws/services/ses/SesService.java @@ -93,6 +93,8 @@ public class SesService { private static final int MAX_BULK_DESTINATIONS = 50; private static final int MAX_RECIPIENTS_PER_DESTINATION = 50; + static final String CVET_SUCCESS_URL_INVALID = "The success redirection URL is invalid."; + static final String CVET_FAILURE_URL_INVALID = "The failure redirection URL is invalid."; // Identities live in SesIdentityService (CRUD, verification, MAIL FROM, notifications, tags, // and the DKIM state machine with its Route53 lookup), which the v2 controller and the v1 // handler call directly. The facade keeps the cross-domain flows (create's configuration-set @@ -915,20 +917,29 @@ public class SesService { */ 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); - } + checkCvetUrlsAndSender(changes, region); cvetService.patchCustomVerificationEmailTemplate(changes, region); } + /** + * The checks every create and update shares, in the order SES runs them (probed on v1 and v2): + * the success URL, the failure URL, then the sender. A member left out is not checked, which + * only the v1 update allows. The sentences are the v1 ones; {@link SesV2Json#remapCvetWriteException} + * restates them for v2. + */ + private void checkCvetUrlsAndSender(CustomVerificationEmailTemplate t, String region) { + if (isGiven(t.getSuccessRedirectionURL()) && !isValidRedirectUrl(t.getSuccessRedirectionURL())) { + throw new AwsException("InvalidParameterValue", CVET_SUCCESS_URL_INVALID, 400); + } + if (isGiven(t.getFailureRedirectionURL()) && !isValidRedirectUrl(t.getFailureRedirectionURL())) { + throw new AwsException("InvalidParameterValue", CVET_FAILURE_URL_INVALID, 400); + } + if (isGiven(t.getFromEmailAddress()) && !identityService.isVerifiedSender(t.getFromEmailAddress(), region)) { + throw new AwsException("FromEmailAddressNotVerified", + "The from email address " + t.getFromEmailAddress() + " is not verified.", 400); + } + } + private static boolean isGiven(String member) { return member != null && !member.isEmpty(); } @@ -1058,18 +1069,7 @@ public class SesService { requireCvetField(t.getTemplateContent(), "TemplateContent"); requireCvetField(t.getSuccessRedirectionURL(), "SuccessRedirectionURL"); requireCvetField(t.getFailureRedirectionURL(), "FailureRedirectionURL"); - if (!identityService.isVerifiedSender(t.getFromEmailAddress(), region)) { - // v1-native code (verified: FromEmailAddressNotVerified / 400); remapV1Exception - // translates it to NotFoundException / 404 for the v2 boundary. - throw new AwsException("FromEmailAddressNotVerified", - "The from email address <" + t.getFromEmailAddress() + "> is not verified", 400); - } - if (!isValidRedirectUrl(t.getSuccessRedirectionURL())) { - throw new AwsException("InvalidParameterValue", "The success redirection URL is invalid", 400); - } - if (!isValidRedirectUrl(t.getFailureRedirectionURL())) { - throw new AwsException("InvalidParameterValue", "The failure redirection URL is invalid", 400); - } + checkCvetUrlsAndSender(t, region); } private static boolean isValidRedirectUrl(String url) { diff --git a/src/main/java/io/floci/aws/services/ses/SesV2Json.java b/src/main/java/io/floci/aws/services/ses/SesV2Json.java index db76b505ed..4ae6c46a53 100644 --- a/src/main/java/io/floci/aws/services/ses/SesV2Json.java +++ b/src/main/java/io/floci/aws/services/ses/SesV2Json.java @@ -10,6 +10,7 @@ import io.floci.aws.core.common.EpochSeconds; import io.floci.aws.core.common.JsonBooleanCoercion; import io.floci.aws.core.common.JsonNumberCoercion; import io.floci.aws.core.common.SmithyValidationErrors; +import io.floci.aws.services.ses.model.CustomVerificationEmailTemplate; import io.floci.aws.services.ses.model.Tag; import java.time.Instant; @@ -77,6 +78,25 @@ final class SesV2Json { return remapV1Exception(e); } + /** + * {@link #remapCvetException} for a create or an update. The service words the sender and + * redirection URL errors the v1 way; v2 puts the sender in angle brackets and ends none of the + * three with a full stop (probed). + */ + static AwsException remapCvetWriteException(AwsException e, CustomVerificationEmailTemplate template) { + if ("FromEmailAddressNotVerified".equals(e.getErrorCode())) { + return new AwsException("NotFoundException", + "The from email address <" + template.getFromEmailAddress() + "> is not verified", 404); + } + if (SesService.CVET_SUCCESS_URL_INVALID.equals(e.getMessage())) { + return new AwsException("BadRequestException", "The success redirection URL is invalid", 400); + } + if (SesService.CVET_FAILURE_URL_INVALID.equals(e.getMessage())) { + return new AwsException("BadRequestException", "The failure redirection URL is invalid", 400); + } + return remapCvetException(e, template.getTemplateName()); + } + /** A request body that is not a JSON object: AWS names a list and answers anything else without a message. */ static void requireJsonObject(JsonNode root) { if (root != null && root.isObject()) { 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 7bf62ed7fb..8f1b85e841 100644 --- a/src/test/java/io/floci/aws/services/ses/SesCustomVerificationEmailTemplateV1IntegrationTest.java +++ b/src/test/java/io/floci/aws/services/ses/SesCustomVerificationEmailTemplateV1IntegrationTest.java @@ -79,18 +79,32 @@ class SesCustomVerificationEmailTemplateV1IntegrationTest { @Test @Order(11) - void create_unverifiedFromAddress_returnsFromEmailAddressNotVerifiedOnV1() { - // An unverified FromEmailAddress is FromEmailAddressNotVerified / 400 on v1 (verified against - // AWS), not the v2 NotFoundException / 404. - SesV1Requests.query("CreateCustomVerificationEmailTemplate") - .formParam("TemplateName", "cvet-v1-unverified-from") - .formParam("FromEmailAddress", "cvet-v1-not-verified@floci.test") + void create_checksTheUrlsBeforeTheSender_inTheV1Wording() { + // Probed: the v1 sentences end with a full stop and carry no angle brackets, and an invalid + // redirection URL is reported before an unverified sender, the success one first. + String unverified = "cvet-v1-not-verified@floci.test"; + createWith(unverified, "https://example.com/ok", "https://example.com/fail") + .when().post("/").then().statusCode(400) + .body(containsString("FromEmailAddressNotVerified")) + .body(containsString( + "The from email address " + unverified + " is not verified.")); + createWith(unverified, "https://example.com/ok", "not-a-url") + .when().post("/").then().statusCode(400) + .body(containsString("InvalidParameterValue")) + .body(containsString("The failure redirection URL is invalid.")); + createWith(unverified, "not-a-url", "not-a-url") + .when().post("/").then().statusCode(400) + .body(containsString("The success redirection URL is invalid.")); + } + + private static RequestSpecification createWith(String from, String successUrl, String failureUrl) { + return SesV1Requests.query("CreateCustomVerificationEmailTemplate") + .formParam("TemplateName", "cvet-v1-refused") + .formParam("FromEmailAddress", from) .formParam("TemplateSubject", "Verify your email") .formParam("TemplateContent", "verify") - .formParam("SuccessRedirectionURL", "https://example.com/ok") - .formParam("FailureRedirectionURL", "https://example.com/fail") - .when().post("/").then().statusCode(400) - .body(containsString("FromEmailAddressNotVerified")); + .formParam("SuccessRedirectionURL", successUrl) + .formParam("FailureRedirectionURL", failureUrl); } @Test diff --git a/src/test/java/io/floci/aws/services/ses/SesCustomVerificationEmailTemplateV2IntegrationTest.java b/src/test/java/io/floci/aws/services/ses/SesCustomVerificationEmailTemplateV2IntegrationTest.java index 4d441d7526..70679f6b7e 100644 --- a/src/test/java/io/floci/aws/services/ses/SesCustomVerificationEmailTemplateV2IntegrationTest.java +++ b/src/test/java/io/floci/aws/services/ses/SesCustomVerificationEmailTemplateV2IntegrationTest.java @@ -196,4 +196,28 @@ class SesCustomVerificationEmailTemplateV2IntegrationTest { .when().delete(BASE + "/cvet-v2-scan"); } } + + @Test + @Order(12) + void createAndUpdate_checkTheUrlsBeforeTheSenderAndTheTemplate() { + // Probed: an invalid redirection URL is reported before an unverified sender, and both + // before a missing template on the update. + String bothInvalid = body("cvet-nope", "nobody@unverified.test", "x", "not-a-url"); + v2().body(bothInvalid) + .when().post(BASE).then().statusCode(400) + .body("__type", equalTo("BadRequestException")) + .body("message", equalTo("The success redirection URL is invalid")); + v2().body(bothInvalid) + .when().put(BASE + "/cvet-nope").then().statusCode(400) + .body("message", equalTo("The success redirection URL is invalid")); + String failureInvalid = body("cvet-nope", "nobody@unverified.test", "x", "https://example.com/ok") + .replace("https://example.com/fail", "not-a-url"); + v2().body(failureInvalid) + .when().post(BASE).then().statusCode(400) + .body("message", equalTo("The failure redirection URL is invalid")); + v2().body(body("cvet-nope", "nobody@unverified.test", "x", "https://example.com/ok")) + .when().put(BASE + "/cvet-nope").then().statusCode(404) + .body("__type", equalTo("NotFoundException")) + .body("message", equalTo("The from email address is not verified")); + } } diff --git a/src/test/java/io/floci/aws/services/ses/SesV2JsonTest.java b/src/test/java/io/floci/aws/services/ses/SesV2JsonTest.java index d200891308..ac0572cef3 100644 --- a/src/test/java/io/floci/aws/services/ses/SesV2JsonTest.java +++ b/src/test/java/io/floci/aws/services/ses/SesV2JsonTest.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import io.floci.aws.core.common.AwsException; +import io.floci.aws.services.ses.model.CustomVerificationEmailTemplate; import io.floci.aws.services.ses.model.Tag; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.function.Executable; @@ -97,6 +98,38 @@ class SesV2JsonTest { assertEquals("m", other.getMessage()); } + @Test + void remapCvetWriteException_restatesTheSenderAndUrlSentencesAndRemapsTheRest() { + CustomVerificationEmailTemplate template = new CustomVerificationEmailTemplate(); + template.setTemplateName("welcome"); + template.setFromEmailAddress("alice@example.com"); + + AwsException sender = SesV2Json.remapCvetWriteException( + new AwsException("FromEmailAddressNotVerified", "v1 sentence", 400), template); + assertEquals("NotFoundException", sender.getErrorCode()); + assertEquals(404, sender.getHttpStatus()); + assertEquals("The from email address is not verified", sender.getMessage()); + + AwsException success = SesV2Json.remapCvetWriteException( + new AwsException("InvalidParameterValue", SesService.CVET_SUCCESS_URL_INVALID, 400), template); + assertEquals("BadRequestException", success.getErrorCode()); + assertEquals("The success redirection URL is invalid", success.getMessage()); + + AwsException failure = SesV2Json.remapCvetWriteException( + new AwsException("InvalidParameterValue", SesService.CVET_FAILURE_URL_INVALID, 400), template); + assertEquals("BadRequestException", failure.getErrorCode()); + assertEquals("The failure redirection URL is invalid", failure.getMessage()); + + AwsException missing = SesV2Json.remapCvetWriteException(new AwsException( + "CustomVerificationEmailTemplateDoesNotExist", "v1 sentence", 400), template); + assertEquals("Custom verification email template does not exist", missing.getMessage()); + + AwsException other = SesV2Json.remapCvetWriteException( + new AwsException("InvalidParameterValue", "TemplateSubject is required.", 400), template); + assertEquals("BadRequestException", other.getErrorCode()); + assertEquals("TemplateSubject is required.", other.getMessage()); + } + @Test void requireJsonObject_rejectsNullAndNonObjects() { assertNull(assertAws("SerializationException", 400,