mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ses): match SES v1 on a missing custom verification template and an absent MAIL FROM domain (#9081)
This commit is contained in:
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()) {
|
||||
|
||||
Reference in new issue
Block a user