mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ses): make v1 UpdateCustomVerificationEmailTemplate a partial update (#9419)
This commit is contained in:
1 parent
1d74e9adc8
commit
354e5b9ea9
7 files changed
+213
-10
No files matched your search
@@ -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 |
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -687,7 +687,7 @@ public class SesQueryHandler {
|
||||
}
|
||||
|
||||
private Response handleUpdateCustomVerificationEmailTemplate(MultivaluedMap<String, String> params, String region) {
|
||||
sesService.updateCustomVerificationEmailTemplate(readCvetParams(params), region);
|
||||
sesService.patchCustomVerificationEmailTemplate(readCvetParams(params), region);
|
||||
return Response.ok(AwsQueryResponse.envelopeEmptyResult(
|
||||
"UpdateCustomVerificationEmailTemplate", AwsNamespaces.SES)).build();
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
+79
@@ -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("<TemplateName>" + NAME + "</TemplateName>"));
|
||||
}
|
||||
|
||||
@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("<Code>CustomVerificationEmailTemplateDoesNotExist</Code>"));
|
||||
SesV1Requests.query("UpdateCustomVerificationEmailTemplate").formParam("TemplateName", "cvet-v1-ghost")
|
||||
.formParam("TemplateSubject", "").formParam("TemplateContent", "")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(containsString("<Code>CustomVerificationEmailTemplateDoesNotExist</Code>"));
|
||||
SesV1Requests.query("UpdateCustomVerificationEmailTemplate").formParam("TemplateName", "cvet-v1-ghost")
|
||||
.formParam("FromEmailAddress", "nobody@unverified.test")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(containsString("<Code>FromEmailAddressNotVerified</Code>"))
|
||||
.body(containsString(
|
||||
"<Message>The from email address nobody@unverified.test is not verified.</Message>"));
|
||||
SesV1Requests.query("UpdateCustomVerificationEmailTemplate").formParam("TemplateName", "cvet-v1-ghost")
|
||||
.formParam("FailureRedirectionURL", "not-a-url")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(containsString("<Code>InvalidParameterValue</Code>"))
|
||||
.body(containsString("<Message>The failure redirection URL is invalid.</Message>"));
|
||||
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("<Code>InvalidParameterValue</Code>"))
|
||||
.body(containsString("<Message>The success redirection URL is invalid.</Message>"));
|
||||
}
|
||||
|
||||
@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("<TemplateSubject>Verify again</TemplateSubject>"))
|
||||
.body(containsString("<FromEmailAddress>" + FROM + "</FromEmailAddress>"))
|
||||
.body(containsString("<TemplateContent><html><body>verify</body></html>"
|
||||
+ "</TemplateContent>"))
|
||||
.body(containsString("<SuccessRedirectionURL>https://example.com/ok</SuccessRedirectionURL>"))
|
||||
.body(containsString("<FailureRedirectionURL>https://example.com/fail</FailureRedirectionURL>"));
|
||||
|
||||
// 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("<Code>FromEmailAddressNotVerified</Code>"));
|
||||
SesV1Requests.query("GetCustomVerificationEmailTemplate").formParam("TemplateName", name)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<TemplateSubject>Verify again</TemplateSubject>"));
|
||||
|
||||
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("<TemplateSubject>")))
|
||||
.body(containsString("<FromEmailAddress>" + FROM + "</FromEmailAddress>"));
|
||||
// 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() {
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user