mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(ses): implement SendCustomVerificationEmail for v1 and v2 (#2191)
* feat(ses): implement SendCustomVerificationEmail for v1 and v2
Add the SendCustomVerificationEmail action on both the v1 Query API and the v2 REST
JSON API (POST /v2/email/outbound-custom-verification-emails). The service renders the
stored custom-verification-email template into the inspection mailbox and the SMTP
relay, returns a MessageId, and registers the recipient as a pending-verification
identity, matching AWS. A missing template is the v1-native
CustomVerificationEmailTemplateDoesNotExist (remapped to NotFoundException on v2), with
the v1 send message ("Template <name> does not exist") verified against AWS.
* test(ses): log custom-verification compat cleanup failures instead of swallowing them
The @AfterAll cleanup deleted templates and identities in best-effort try/catch
blocks that swallowed every exception, so a real deletion failure would leave
state behind with no diagnostic. Log the affected template/identity and the
exception message, per AGENTS.md's rule against silently discarding exceptions.
This commit is contained in:
1 parent
ac4b865a34
commit
bd01c1a70b
8 files changed
+576
-8
No files matched your search
@@ -398,7 +398,7 @@ public class AwsQueryController {
|
||||
"TestRenderTemplate",
|
||||
"CreateCustomVerificationEmailTemplate", "GetCustomVerificationEmailTemplate",
|
||||
"ListCustomVerificationEmailTemplates", "UpdateCustomVerificationEmailTemplate",
|
||||
"DeleteCustomVerificationEmailTemplate",
|
||||
"DeleteCustomVerificationEmailTemplate", "SendCustomVerificationEmail",
|
||||
"CreateConfigurationSet", "DescribeConfigurationSet",
|
||||
"ListConfigurationSets", "DeleteConfigurationSet",
|
||||
"CreateConfigurationSetEventDestination",
|
||||
|
||||
@@ -702,6 +702,38 @@ public class SesController {
|
||||
}
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/outbound-custom-verification-emails")
|
||||
public Response sendCustomVerificationEmail(@Context HttpHeaders headers, String body) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
String templateName = null;
|
||||
try {
|
||||
if (!sesService.isAccountSendingEnabled(region)) {
|
||||
throw new AwsException("SendingPausedException",
|
||||
"Account sending is disabled.", 400);
|
||||
}
|
||||
JsonNode request = objectMapper.readTree(body);
|
||||
requireJsonObject(request);
|
||||
templateName = request.path("TemplateName").asText(null);
|
||||
String messageId = sesService.sendCustomVerificationEmail(
|
||||
request.path("EmailAddress").asText(null), templateName,
|
||||
request.path("ConfigurationSetName").asText(null), region);
|
||||
ObjectNode result = objectMapper.createObjectNode();
|
||||
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);
|
||||
} catch (com.fasterxml.jackson.core.JsonProcessingException e) {
|
||||
throw new AwsException("BadRequestException", e.getMessage(), 400);
|
||||
}
|
||||
}
|
||||
|
||||
private CustomVerificationEmailTemplate parseCvet(JsonNode request) {
|
||||
requireJsonObject(request);
|
||||
CustomVerificationEmailTemplate t = new CustomVerificationEmailTemplate();
|
||||
|
||||
@@ -97,6 +97,8 @@ public class SesQueryHandler {
|
||||
handleUpdateCustomVerificationEmailTemplate(params, region);
|
||||
case "DeleteCustomVerificationEmailTemplate" ->
|
||||
handleDeleteCustomVerificationEmailTemplate(params, region);
|
||||
case "SendCustomVerificationEmail" ->
|
||||
handleSendCustomVerificationEmail(params, region);
|
||||
case "CreateConfigurationSet" -> handleCreateConfigurationSet(params, region);
|
||||
case "DescribeConfigurationSet" -> handleDescribeConfigurationSet(params, region);
|
||||
case "ListConfigurationSets" -> handleListConfigurationSets(region);
|
||||
@@ -611,6 +613,21 @@ public class SesQueryHandler {
|
||||
"DeleteCustomVerificationEmailTemplate", AwsNamespaces.SES)).build();
|
||||
}
|
||||
|
||||
private Response handleSendCustomVerificationEmail(MultivaluedMap<String, String> params, String region) {
|
||||
if (!sesService.isAccountSendingEnabled(region)) {
|
||||
throw new AwsException("AccountSendingPausedException",
|
||||
"Account sending is disabled.", 400);
|
||||
}
|
||||
// getParam (not requireParam) so the service is the single validation authority and returns
|
||||
// the AWS-faithful "Email address not specified." / "Invalid email address<...>." shapes.
|
||||
String messageId = sesService.sendCustomVerificationEmail(
|
||||
getParam(params, "EmailAddress"), getParam(params, "TemplateName"),
|
||||
getParam(params, "ConfigurationSetName"), region);
|
||||
String result = new XmlBuilder().elem("MessageId", messageId).build();
|
||||
return Response.ok(AwsQueryResponse.envelope(
|
||||
"SendCustomVerificationEmail", AwsNamespaces.SES, result)).build();
|
||||
}
|
||||
|
||||
private CustomVerificationEmailTemplate readCvetParams(MultivaluedMap<String, String> params) {
|
||||
CustomVerificationEmailTemplate t = new CustomVerificationEmailTemplate();
|
||||
t.setTemplateName(params.getFirst("TemplateName"));
|
||||
|
||||
@@ -1146,6 +1146,104 @@ public class SesService {
|
||||
LOG.infov("Deleted custom verification email template {0} in region {1}", templateName, region);
|
||||
}
|
||||
|
||||
// AWS appends this exact disclaimer to the end of every custom verification email and it cannot
|
||||
// be removed (SES docs Q10).
|
||||
private static final String CUSTOM_VERIFICATION_DISCLAIMER =
|
||||
"If you did not request to verify this email address, please disregard this message.";
|
||||
|
||||
public String sendCustomVerificationEmail(String emailAddress, String templateName,
|
||||
String configurationSetName, String region) {
|
||||
// AWS validates the recipient before the template exists check (probe-confirmed): a blank
|
||||
// address is "Email address not specified."; anything that isn't a single valid address —
|
||||
// no local-part/domain separator, more than one separator, whitespace, or longer than 320
|
||||
// chars — is "Invalid email address<addr>." (both InvalidParameterValue / 400, remapped to
|
||||
// BadRequestException on v2). The separator count ignores '@' inside a quoted local part,
|
||||
// since AWS accepts an RFC-5321 quoted local part that contains '@' (e.g. "a@b"@example.com).
|
||||
if (emailAddress == null || emailAddress.isBlank()) {
|
||||
throw new AwsException("InvalidParameterValue", "Email address not specified.", 400);
|
||||
}
|
||||
// A leading/trailing '@' means an empty local part (@example.com) or empty domain (local@),
|
||||
// both of which AWS rejects (probe-confirmed). AWS does not require a dot in the domain
|
||||
// (a@b is accepted), so no stricter domain shape is enforced.
|
||||
boolean emptyBoundary = emailAddress.charAt(0) == '@'
|
||||
|| emailAddress.charAt(emailAddress.length() - 1) == '@';
|
||||
if (emailAddress.length() > 320 || unquotedAtCount(emailAddress) != 1 || emptyBoundary
|
||||
|| emailAddress.chars().anyMatch(Character::isWhitespace)) {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"Invalid email address<" + emailAddress + ">.", 400);
|
||||
}
|
||||
if (templateName == null || templateName.isBlank()) {
|
||||
throw new AwsException("InvalidParameterValue", "TemplateName is required.", 400);
|
||||
}
|
||||
CustomVerificationEmailTemplate template = cvetStore.get(cvetKey(region, templateName))
|
||||
.orElseThrow(() -> new AwsException("CustomVerificationEmailTemplateDoesNotExist",
|
||||
"Template <" + templateName + "> does not exist", 400));
|
||||
if (!isVerifiedSender(template.getFromEmailAddress(), region)) {
|
||||
throw new AwsException("FromEmailAddressNotVerified",
|
||||
"Email address is not verified. The following identities failed the check in region "
|
||||
+ region.toUpperCase(Locale.ROOT) + ": " + template.getFromEmailAddress(), 400);
|
||||
}
|
||||
if (configurationSetName != null && !configurationSetName.isBlank()) {
|
||||
getConfigurationSet(configurationSetName, region);
|
||||
}
|
||||
|
||||
// AWS registers the recipient as a pending-verification identity as part of sending the
|
||||
// verification email, so ListIdentities / GetIdentityVerificationAttributes surface it.
|
||||
markPendingEmailIdentity(emailAddress, region);
|
||||
|
||||
// AWS sends the template content verbatim and appends a fixed, non-removable disclaimer (SES
|
||||
// docs Q10). AWS also appends a unique verification link, which Floci does not reproduce
|
||||
// because it has no verification-click flow. The template body carries no placeholder that
|
||||
// AWS substitutes, so the content itself is passed through unchanged.
|
||||
String body = template.getTemplateContent() == null ? "" : template.getTemplateContent();
|
||||
String renderedHtml = body + "<p>" + CUSTOM_VERIFICATION_DISCLAIMER + "</p>";
|
||||
|
||||
String messageId = UUID.randomUUID().toString();
|
||||
SentEmail email = new SentEmail(messageId, region, template.getFromEmailAddress(),
|
||||
List.of(emailAddress), List.of(), List.of(), List.of(),
|
||||
template.getTemplateSubject(), null, renderedHtml);
|
||||
emailStore.put("email::" + region + "::" + messageId, email);
|
||||
smtpRelay.relay(template.getFromEmailAddress(), List.of(emailAddress), List.of(), List.of(),
|
||||
List.of(), template.getTemplateSubject(), null, renderedHtml, List.of());
|
||||
LOG.infov("SES custom verification email sent: to={0}, template={1}, messageId={2}",
|
||||
emailAddress, templateName, messageId);
|
||||
return messageId;
|
||||
}
|
||||
|
||||
// Counts '@' characters outside a quoted local part. A valid address has exactly one — the
|
||||
// local-part/domain separator; '@' inside a quoted local part (honoring backslash escapes) is
|
||||
// part of the local part and doesn't count, so AWS-accepted forms like "a@b"@example.com pass
|
||||
// while a@@b.com and "a"@@example.com are rejected.
|
||||
private static int unquotedAtCount(String address) {
|
||||
int count = 0;
|
||||
boolean inQuotes = false;
|
||||
boolean escaped = false;
|
||||
for (int i = 0; i < address.length(); i++) {
|
||||
char c = address.charAt(i);
|
||||
if (escaped) {
|
||||
escaped = false;
|
||||
} else if (c == '\\') {
|
||||
escaped = true;
|
||||
} else if (c == '"') {
|
||||
inQuotes = !inQuotes;
|
||||
} else if (c == '@' && !inQuotes) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
private void markPendingEmailIdentity(String emailAddress, String region) {
|
||||
String key = identityKey(region, emailAddress);
|
||||
if (identityStore.get(key).isEmpty()) {
|
||||
Identity identity = new Identity(emailAddress, "EmailAddress");
|
||||
identity.setVerificationStatus("Pending");
|
||||
identityStore.put(key, identity);
|
||||
LOG.infov("SES custom verification email registered pending identity {0} in region {1}",
|
||||
emailAddress, region);
|
||||
}
|
||||
}
|
||||
|
||||
private void validateCustomVerificationTemplate(CustomVerificationEmailTemplate t, String region) {
|
||||
requireCvetField(t.getTemplateName(), "TemplateName");
|
||||
requireCvetField(t.getFromEmailAddress(), "FromEmailAddress");
|
||||
|
||||
Reference in new issue
Block a user