fix(ses): match SES v1 on a missing custom verification template and an absent MAIL FROM domain (#9081)

This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-11-20 07:24:33 -04:00
1 parent a3c45a4ce2
commit 7c4ed46223
10 files changed
+177 -44

No files matched your search

@@ -24,8 +24,10 @@ import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.jboss.logging.Logger;
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.remapV1Exception;
/**
@@ -102,7 +104,7 @@ public class SesCvetController {
cvetJson(cvetService.getCustomVerificationEmailTemplate(templateName, region),
true)).build();
} catch (AwsException e) {
throw remapV1Exception(e);
throw remapCvetException(e, templateName);
}
}
@@ -117,7 +119,7 @@ public class SesCvetController {
sesService.updateCustomVerificationEmailTemplate(t, region);
return Response.ok(objectMapper.createObjectNode()).build();
} catch (AwsException e) {
throw remapV1Exception(e);
throw remapCvetException(e, templateName);
}
}
@@ -126,12 +128,10 @@ public class SesCvetController {
public Response deleteCustomVerificationEmailTemplate(@Context HttpHeaders headers,
@PathParam("templateName") String templateName) {
String region = regionResolver.resolveRegion(headers);
try {
cvetService.deleteCustomVerificationEmailTemplate(templateName, region);
return Response.ok(objectMapper.createObjectNode()).build();
} catch (AwsException e) {
throw remapV1Exception(e);
if (!cvetService.deleteCustomVerificationEmailTemplate(templateName, region)) {
throw cvetNotFound(templateName);
}
return Response.ok(objectMapper.createObjectNode()).build();
}
private CustomVerificationEmailTemplate parseCvet(JsonNode request) {
@@ -70,7 +70,7 @@ public class SesCvetService implements SesTaggable {
}
public CustomVerificationEmailTemplate getCustomVerificationEmailTemplate(String templateName, String region) {
return cvetStore.get(cvetKey(region, templateName)).orElseThrow(() -> cvetNotFound(templateName));
return cvetStore.get(cvetKey(region, templateName)).orElseThrow(SesCvetService::cvetNotFound);
}
public PaginatedResult<CustomVerificationEmailTemplate> listCustomVerificationEmailTemplates(
@@ -94,7 +94,7 @@ public class SesCvetService implements SesTaggable {
// them and have the update resurrect the just-deleted template.
synchronized (cvetMutationLock) {
CustomVerificationEmailTemplate existing = cvetStore.get(key)
.orElseThrow(() -> cvetNotFound(template.getTemplateName()));
.orElseThrow(SesCvetService::cvetNotFound);
// The update request has no Tags member — preserve the stored ones (copied, so the
// two objects never share a list instance).
template.setTags(new ArrayList<>(existing.getTags()));
@@ -104,17 +104,22 @@ public class SesCvetService implements SesTaggable {
template.getTemplateName(), region);
}
public void deleteCustomVerificationEmailTemplate(String templateName, String region) {
/**
* Reports whether the template existed instead of throwing, because the two protocols disagree
* (probed): v1 answers 200 for an unknown name, v2 answers 404.
*/
public boolean deleteCustomVerificationEmailTemplate(String templateName, String region) {
String key = cvetKey(region, templateName);
// Guard the check-then-delete on the same lock as create/update so the three mutations
// serialize against each other.
synchronized (cvetMutationLock) {
if (cvetStore.get(key).isEmpty()) {
throw cvetNotFound(templateName);
return false;
}
cvetStore.delete(key);
}
LOG.infov("Deleted custom verification email template {0} in region {1}", templateName, region);
return true;
}
/**
@@ -174,11 +179,11 @@ public class SesCvetService implements SesTaggable {
"No CustomVerificationEmailTemplate present with name: " + templateName, 404);
}
private static AwsException cvetNotFound(String templateName) {
// v1-native code (verified: CustomVerificationEmailTemplateDoesNotExist / 400).
// SesV2Json.remapV1Exception translates it to NotFoundException / 404 for the v2 boundary.
private static AwsException cvetNotFound() {
// v1-native code and message (probed: a fixed sentence that does not name the template).
// The v2 controller restates it as NotFoundException / 404 in the v2 wording.
return new AwsException("CustomVerificationEmailTemplateDoesNotExist",
"Custom verification email template <" + templateName + "> does not exist", 400);
"Provided custom verification email template name does not exist.", 400);
}
private static String cvetKey(String region, String templateName) {
@@ -496,10 +496,6 @@ public class SesQueryHandler {
private Response handleSetIdentityMailFromDomain(MultivaluedMap<String, String> params, String region) {
String identityValue = getParam(params, "Identity");
String mailFromDomain = getParam(params, "MailFromDomain");
if (mailFromDomain == null) {
throw new AwsException("InvalidParameterValue",
"MailFromDomain is required (use an empty string to clear the existing setting).", 400);
}
String behaviorOnMxFailure = getParam(params, "BehaviorOnMXFailure");
identityService.setMailFromDomain(identityValue, mailFromDomain, behaviorOnMxFailure,
region);
@@ -517,20 +513,26 @@ public class SesQueryHandler {
return raw != null && AwsQueryBooleans.parse(raw);
}
/**
* Probed: an identity the account does not hold gets no entry, and one without a MAIL FROM
* domain reports {@code BehaviorOnMXFailure} alone, although the model requires all three.
*/
private Response handleGetIdentityMailFromDomainAttributes(MultivaluedMap<String, String> params, String region) {
List<String> identities = extractMembers(params, "Identities");
XmlBuilder xml = new XmlBuilder().start("MailFromDomainAttributes");
for (String identityValue : identities) {
Identity identity = identityService.getMailFromAttributes(identityValue, region);
if (identity == null) {
continue;
}
xml.start("entry");
xml.elem("key", identityValue);
xml.start("value");
xml.elem("MailFromDomain", identity != null && identity.getMailFromDomain() != null
? identity.getMailFromDomain() : "");
xml.elem("MailFromDomainStatus", identity != null
? identity.getMailFromDomainStatus() : "Pending");
xml.elem("BehaviorOnMXFailure", identity != null
? identity.getBehaviorOnMxFailure() : "UseDefaultValue");
xml.elem("BehaviorOnMXFailure", identity.getBehaviorOnMxFailure());
if (identity.getMailFromDomain() != null) {
xml.elem("MailFromDomain", identity.getMailFromDomain());
xml.elem("MailFromDomainStatus", identity.getMailFromDomainStatus());
}
xml.end("value");
xml.end("entry");
}
@@ -40,6 +40,7 @@ import java.util.List;
import java.util.Map;
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.remapV1Exception;
/**
@@ -394,13 +395,7 @@ public class SesSendController {
result.put("MessageId", messageId);
return Response.ok(result).build();
} catch (AwsException e) {
// AWS returns a longer not-found message on v2 than the v1 send message ("Template <name>
// does not exist"); the service throws the v1-native form, so restate it in the v2 wording.
if ("CustomVerificationEmailTemplateDoesNotExist".equals(e.getErrorCode())) {
throw new AwsException("NotFoundException",
"Custom verification email template <" + templateName + "> does not exist", 404);
}
throw remapV1Exception(e);
throw remapCvetException(e, templateName);
}
}
@@ -46,8 +46,7 @@ final class SesV2Json {
case "InvalidParameterValue", "InvalidTemplate", "ValidationError",
"InvalidRenderingParameter", "MissingRenderingAttribute", "InvalidSNSDestination" ->
new AwsException("BadRequestException", e.getMessage(), 400);
case "TemplateDoesNotExist", "ConfigurationSetDoesNotExist",
"CustomVerificationEmailTemplateDoesNotExist", "FromEmailAddressNotVerified" ->
case "TemplateDoesNotExist", "ConfigurationSetDoesNotExist", "FromEmailAddressNotVerified" ->
new AwsException("NotFoundException", e.getMessage(), 404);
case "AlreadyExists", "ConfigurationSetAlreadyExists",
"CustomVerificationEmailTemplateAlreadyExists" ->
@@ -58,6 +57,26 @@ final class SesV2Json {
};
}
/**
* The v2 answer for a missing custom verification email template (probed). It names the
* template, unlike either v1 message, so callers restate it instead of remapping the v1 error.
*/
static AwsException cvetNotFound(String templateName) {
return new AwsException("NotFoundException",
"Custom verification email template <" + templateName + "> does not exist", 404);
}
/**
* {@link #remapV1Exception} for an operation that names a custom verification email template:
* the service throws a v1 sentence for a missing one, which v2 restates around the requested name.
*/
static AwsException remapCvetException(AwsException e, String templateName) {
if ("CustomVerificationEmailTemplateDoesNotExist".equals(e.getErrorCode())) {
return cvetNotFound(templateName);
}
return remapV1Exception(e);
}
/** 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()) {
@@ -25,6 +25,8 @@ class SesCustomVerificationEmailTemplateV1IntegrationTest {
"AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/ses/aws4_request";
private static final String FROM = "cvet-v1-sender@floci.test";
private static final String NAME = "cvet-v1-a";
private static final String MISSING_MESSAGE =
"Provided custom verification email template name does not exist.";
private static RequestSpecification query(String action) {
return given().contentType("application/x-www-form-urlencoded").header("Authorization", AUTH)
@@ -51,10 +53,35 @@ class SesCustomVerificationEmailTemplateV1IntegrationTest {
@Order(10)
void get_missing_returnsDoesNotExistOnV1() {
// v1 returns CustomVerificationEmailTemplateDoesNotExist / 400 (verified against AWS), so the
// v1 SDK's typed exception fires — not the v2 NotFoundException / 404.
// v1 SDK's typed exception fires, not the v2 NotFoundException / 404. The message is a
// fixed sentence that does not name the template, unlike the v2 one.
query("GetCustomVerificationEmailTemplate").formParam("TemplateName", "cvet-v1-ghost")
.when().post("/").then().statusCode(400)
.body(containsString("<Code>CustomVerificationEmailTemplateDoesNotExist</Code>"));
.body(containsString("<Code>CustomVerificationEmailTemplateDoesNotExist</Code>"))
.body(containsString("<Message>" + MISSING_MESSAGE + "</Message>"));
}
@Test
@Order(12)
void update_missing_returnsTheSameFixedSentenceOnV1() {
query("UpdateCustomVerificationEmailTemplate")
.formParam("TemplateName", "cvet-v1-ghost").formParam("FromEmailAddress", FROM)
.formParam("TemplateSubject", "Verify your email")
.formParam("TemplateContent", "<html><body>verify</body></html>")
.formParam("SuccessRedirectionURL", "https://example.com/ok")
.formParam("FailureRedirectionURL", "https://example.com/fail")
.when().post("/").then().statusCode(400)
.body(containsString("<Code>CustomVerificationEmailTemplateDoesNotExist</Code>"))
.body(containsString("<Message>" + MISSING_MESSAGE + "</Message>"));
}
@Test
@Order(13)
void delete_missing_succeedsOnV1() {
// Probed: v1 answers 200 for a name the account does not hold, where v2 answers 404.
query("DeleteCustomVerificationEmailTemplate").formParam("TemplateName", "cvet-v1-ghost")
.when().post("/").then().statusCode(200)
.body(containsString("DeleteCustomVerificationEmailTemplateResponse"));
}
@Test
@@ -119,7 +119,8 @@ class SesCustomVerificationEmailTemplateV2IntegrationTest {
given().contentType("application/json").header("Authorization", AUTH)
.body(body("nope", FROM, "x", "https://example.com/ok"))
.when().put(BASE + "/nope").then().statusCode(404)
.body("__type", equalTo("NotFoundException"));
.body("__type", equalTo("NotFoundException"))
.body("message", equalTo("Custom verification email template <nope> does not exist"));
}
@Test
@@ -136,7 +137,8 @@ class SesCustomVerificationEmailTemplateV2IntegrationTest {
void deleteMissing_throwsNotFound() {
given().header("Authorization", AUTH)
.when().delete(BASE + "/nope").then().statusCode(404)
.body("__type", equalTo("NotFoundException"));
.body("__type", equalTo("NotFoundException"))
.body("message", equalTo("Custom verification email template <nope> does not exist"));
}
@Test
@@ -10,6 +10,7 @@ import org.junit.jupiter.api.Test;
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.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -58,6 +59,12 @@ class SesCvetServiceTest {
AwsException e = assertThrows(AwsException.class,
() -> service.getCustomVerificationEmailTemplate("ghost", REGION));
assertEquals("CustomVerificationEmailTemplateDoesNotExist", e.getErrorCode());
assertEquals("Provided custom verification email template name does not exist.", e.getMessage());
}
@Test
void delete_unknown_reportsAbsenceWithoutThrowing() {
assertFalse(service.deleteCustomVerificationEmailTemplate("ghost", REGION));
}
@Test
@@ -65,7 +72,7 @@ class SesCvetServiceTest {
service.createCustomVerificationEmailTemplate(template("t1"), REGION);
assertTrue(service.find("t1", REGION).isPresent());
service.deleteCustomVerificationEmailTemplate("t1", REGION);
assertTrue(service.deleteCustomVerificationEmailTemplate("t1", REGION));
assertTrue(service.find("t1", REGION).isEmpty());
}
@@ -97,5 +104,6 @@ class SesCvetServiceTest {
AwsException e = assertThrows(AwsException.class,
() -> service.updateCustomVerificationEmailTemplate(template("ghost"), REGION));
assertEquals("CustomVerificationEmailTemplateDoesNotExist", e.getErrorCode());
assertEquals("Provided custom verification email template name does not exist.", e.getMessage());
}
}
@@ -89,8 +89,41 @@ class SesIdentityAttributesV1IntegrationTest {
.post("/")
.then()
.statusCode(200)
.body(containsString("<MailFromDomain></MailFromDomain>"))
.body(containsString("<MailFromDomainStatus>Pending</MailFromDomainStatus>"));
.body(containsString("<key>v1-attrs.floci.test</key>"))
.body(containsString("<BehaviorOnMXFailure>UseDefaultValue</BehaviorOnMXFailure>"))
.body(not(containsString("MailFromDomain>")))
.body(not(containsString("MailFromDomainStatus")));
}
@Test
void getIdentityMailFromDomainAttributes_unknownIdentity_hasNoEntry() {
given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", AUTH)
.formParam("Action", "GetIdentityMailFromDomainAttributes")
.formParam("Identities.member.1", "v1-attrs-unknown.floci.test")
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("MailFromDomainAttributes"))
.body(not(containsString("<entry>")));
}
@Test
void getIdentityMailFromDomainAttributes_skipsOnlyTheUnknownIdentity() {
given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", AUTH)
.formParam("Action", "GetIdentityMailFromDomainAttributes")
.formParam("Identities.member.1", "v1-attrs-unknown.floci.test")
.formParam("Identities.member.2", "v1-attrs.floci.test")
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("<key>v1-attrs.floci.test</key>"))
.body(not(containsString("v1-attrs-unknown.floci.test")));
}
@Test
@@ -145,7 +178,19 @@ class SesIdentityAttributesV1IntegrationTest {
@Test
@Order(11)
void setIdentityMailFromDomain_missingMailFromDomain_returnsInvalidParameterValue() {
void setIdentityMailFromDomain_missingMailFromDomain_clears() {
// Probed: SES takes an absent MailFromDomain as the request to clear the domain.
given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", AUTH)
.formParam("Action", "SetIdentityMailFromDomain")
.formParam("Identity", "v1-attrs.floci.test")
.formParam("MailFromDomain", "mail.v1-attrs.floci.test")
.when()
.post("/")
.then()
.statusCode(200);
given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", AUTH)
@@ -154,8 +199,20 @@ class SesIdentityAttributesV1IntegrationTest {
.when()
.post("/")
.then()
.statusCode(400)
.body(containsString("InvalidParameterValue"));
.statusCode(200)
.body(containsString("SetIdentityMailFromDomainResponse"));
given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", AUTH)
.formParam("Action", "GetIdentityMailFromDomainAttributes")
.formParam("Identities.member.1", "v1-attrs.floci.test")
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("<BehaviorOnMXFailure>UseDefaultValue</BehaviorOnMXFailure>"))
.body(not(containsString("MailFromDomain>")));
}
@Test
@@ -64,10 +64,28 @@ class SesV2JsonTest {
assertEquals("SendingPausedException", SesV2Json.remapV1Exception(new AwsException(
"ConfigurationSetSendingPausedException", "m", 400)).getErrorCode());
AwsException restatedByCallers = new AwsException(
"CustomVerificationEmailTemplateDoesNotExist", "v1 sentence", 400);
assertSame(restatedByCallers, SesV2Json.remapV1Exception(restatedByCallers));
AwsException untouched = new AwsException("NotFoundException", "already v2", 404);
assertSame(untouched, SesV2Json.remapV1Exception(untouched));
}
@Test
void remapCvetException_restatesAMissingTemplateAndRemapsTheRest() {
AwsException missing = SesV2Json.remapCvetException(new AwsException(
"CustomVerificationEmailTemplateDoesNotExist", "v1 sentence", 400), "welcome");
assertEquals("NotFoundException", missing.getErrorCode());
assertEquals(404, missing.getHttpStatus());
assertEquals("Custom verification email template <welcome> does not exist", missing.getMessage());
AwsException other = SesV2Json.remapCvetException(
new AwsException("InvalidParameterValue", "m", 400), "welcome");
assertEquals("BadRequestException", other.getErrorCode());
assertEquals("m", other.getMessage());
}
@Test
void requireJsonObject_rejectsNullAndNonObjects() {
assertNull(assertAws("SerializationException", 400,