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
@@ -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
|
||||
|
||||
Reference in new issue
Block a user