mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ses): word the v1 custom verification template create errors as SES does (#9440)
This commit is contained in:
1 parent
aeb4593696
commit
bde90de4d4
6 files changed
+141
-40
No files matched your search
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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()) {
|
||||
|
||||
+24
-10
@@ -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("<Code>FromEmailAddressNotVerified</Code>"))
|
||||
.body(containsString(
|
||||
"<Message>The from email address " + unverified + " is not verified.</Message>"));
|
||||
createWith(unverified, "https://example.com/ok", "not-a-url")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(containsString("<Code>InvalidParameterValue</Code>"))
|
||||
.body(containsString("<Message>The failure redirection URL is invalid.</Message>"));
|
||||
createWith(unverified, "not-a-url", "not-a-url")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(containsString("<Message>The success redirection URL is invalid.</Message>"));
|
||||
}
|
||||
|
||||
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", "<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>FromEmailAddressNotVerified</Code>"));
|
||||
.formParam("SuccessRedirectionURL", successUrl)
|
||||
.formParam("FailureRedirectionURL", failureUrl);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+24
@@ -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 <nobody@unverified.test> is not verified"));
|
||||
}
|
||||
}
|
||||
@@ -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 <alice@example.com> 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 <welcome> 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,
|
||||
|
||||
Reference in new issue
Block a user