mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 14:30:28 -05:00
fix(ses): create email identities atomically so failures leave nothing behind (#3146)
* fix(ses): create email identities atomically so failures leave nothing behind * fix(ses): serialize the v1 verify family on the identity creation lock
This commit is contained in:
1 parent
148ec839bd
commit
618ca58424
4 files changed
+313
-65
No files matched your search
@@ -115,41 +115,18 @@ public class SesController {
|
||||
configurationSetName = configSetNode.textValue();
|
||||
}
|
||||
|
||||
if (sesService.getIdentityVerificationAttributes(emailIdentity, region) != null) {
|
||||
throw new AwsException("AlreadyExistsException",
|
||||
"Email identity " + emailIdentity + " already exist.", 400);
|
||||
}
|
||||
|
||||
// Parse Tags up front. parseTagsArray is pure (it only reads the node), so validating the
|
||||
// shape before creating the identity keeps the call atomic — a malformed Tags value fails
|
||||
// without leaving a half-created identity behind, matching AWS and the ConfigurationSetName
|
||||
// pre-check below.
|
||||
List<Tag> parsedTags = parseTagsArray(request.path("Tags"));
|
||||
// Validate tags before creating the identity so an invalid set fails atomically
|
||||
// instead of leaving the identity behind.
|
||||
SesTags.validate(parsedTags);
|
||||
|
||||
// Verified against AWS: a non-existent ConfigurationSetName fails the whole call
|
||||
// (NotFoundException) without creating the identity, so validate it before creating.
|
||||
// Only the empty string means "no default configuration set" (consistent with the
|
||||
// PutEmailIdentityConfigurationSetAttributes path); a whitespace-only name flows through
|
||||
// name validation and is rejected as invalid input, rather than being silently ignored.
|
||||
boolean hasConfigSet = configurationSetName != null && !configurationSetName.isEmpty();
|
||||
if (hasConfigSet) {
|
||||
sesService.getConfigurationSet(configurationSetName, region);
|
||||
}
|
||||
|
||||
Identity identity = emailIdentity.contains("@")
|
||||
? sesService.verifyEmailIdentity(emailIdentity, region)
|
||||
: sesService.verifyDomainIdentity(emailIdentity, region);
|
||||
|
||||
if (hasConfigSet) {
|
||||
sesService.setEmailIdentityConfigurationSet(emailIdentity, configurationSetName, region);
|
||||
}
|
||||
|
||||
if (parsedTags != null) {
|
||||
sesService.setIdentityTags(emailIdentity, region, parsedTags);
|
||||
}
|
||||
// The service builds the complete identity (default configuration set and tags included)
|
||||
// and persists it with a single write, so any failure (AlreadyExists, invalid tags, a
|
||||
// missing configuration set) fails the whole call and creates nothing, matching AWS.
|
||||
Identity identity = sesService.createEmailIdentity(emailIdentity,
|
||||
hasConfigSet ? configurationSetName : null, parsedTags, region);
|
||||
|
||||
ObjectNode result = objectMapper.createObjectNode();
|
||||
result.put("IdentityType", toV2IdentityType(identity.getIdentityType()));
|
||||
|
||||
@@ -51,6 +51,10 @@ public class SesIdentityService {
|
||||
private final Route53Service route53Service;
|
||||
private final Clock clock;
|
||||
private final ConcurrentHashMap<String, DkimLookupCacheEntry> dkimLookupCache = new ConcurrentHashMap<>();
|
||||
// Serializes every identity check-then-put (v2 CreateEmailIdentity, the v1 verify family, the
|
||||
// CVET pending registration) so concurrent creators can't lose each other's writes
|
||||
// (InMemoryStorage only makes each individual operation thread-safe).
|
||||
private final Object identityCreationLock = new Object();
|
||||
|
||||
@Inject
|
||||
public SesIdentityService(StorageFactory storageFactory, Route53Service route53Service, Clock clock) {
|
||||
@@ -71,13 +75,15 @@ public class SesIdentityService {
|
||||
throw new AwsException("InvalidParameterValue", "Email address is required.", 400);
|
||||
}
|
||||
String key = identityKey(region, emailAddress);
|
||||
Identity existing = identityStore.get(key).orElse(null);
|
||||
if (existing != null) {
|
||||
return existing;
|
||||
Identity identity;
|
||||
synchronized (identityCreationLock) {
|
||||
Identity existing = identityStore.get(key).orElse(null);
|
||||
if (existing != null) {
|
||||
return existing;
|
||||
}
|
||||
identity = new Identity(emailAddress, "EmailAddress");
|
||||
identityStore.put(key, identity);
|
||||
}
|
||||
|
||||
Identity identity = new Identity(emailAddress, "EmailAddress");
|
||||
identityStore.put(key, identity);
|
||||
LOG.infov("Verified email identity: {0} in region {1}", emailAddress, region);
|
||||
return identity;
|
||||
}
|
||||
@@ -88,11 +94,20 @@ public class SesIdentityService {
|
||||
throw new AwsException("InvalidParameterValue", "Domain is required.", 400);
|
||||
}
|
||||
String key = identityKey(region, domain);
|
||||
Identity existing = identityStore.get(key).orElse(null);
|
||||
if (existing != null) {
|
||||
return existing;
|
||||
Identity identity;
|
||||
synchronized (identityCreationLock) {
|
||||
Identity existing = identityStore.get(key).orElse(null);
|
||||
if (existing != null) {
|
||||
return existing;
|
||||
}
|
||||
identity = newDomainIdentity(domain);
|
||||
identityStore.put(key, identity);
|
||||
}
|
||||
LOG.infov("Verified domain identity: {0} in region {1}", domain, region);
|
||||
return identity;
|
||||
}
|
||||
|
||||
private Identity newDomainIdentity(String domain) {
|
||||
Identity identity = new Identity(domain, "Domain");
|
||||
regenerateDkimTokens(identity);
|
||||
identity.setVerificationStatus("Pending");
|
||||
@@ -101,8 +116,49 @@ public class SesIdentityService {
|
||||
// CNAMEs yet); the first Get/List refresh transitions it to Pending. Matches AWS, where
|
||||
// CreateEmailIdentity returns NOT_STARTED but a subsequent GetEmailIdentity returns PENDING.
|
||||
identity.setDkimVerificationStatus("NotStarted");
|
||||
identityStore.put(key, identity);
|
||||
LOG.infov("Verified domain identity: {0} in region {1}", domain, region);
|
||||
return identity;
|
||||
}
|
||||
|
||||
/**
|
||||
* v2 CreateEmailIdentity: builds the complete identity (default configuration set and tags
|
||||
* included) and persists it with a single write, so a failing validation leaves nothing behind,
|
||||
* matching real SESv2 where the whole call fails and no resource is created (probe-confirmed
|
||||
* 2026-09-06: a failed create leaves a 404 behind, not a half-created identity). The validation
|
||||
* order is probe-confirmed too: AlreadyExists, then tag validation, then the caller-supplied
|
||||
* configuration-set existence check (cross-domain, so the facade passes it in), then the write.
|
||||
* The lock closes the check-then-put race where two concurrent creates for the same identity
|
||||
* would both return 200; the loser now gets the AlreadyExists error, as on AWS.
|
||||
*/
|
||||
public Identity createEmailIdentity(String emailIdentity, String configurationSetName,
|
||||
List<Tag> tags, String region,
|
||||
Runnable configurationSetExistsCheck) {
|
||||
// Validate with the wording the pre-atomic path produced (the verify methods' field names),
|
||||
// so the key derivation's generic "Identity" message never surfaces on this API.
|
||||
validateIdentityWhitespace(emailIdentity,
|
||||
emailIdentity != null && emailIdentity.contains("@") ? "Email address" : "Domain");
|
||||
String key = identityKey(region, emailIdentity);
|
||||
Identity identity;
|
||||
synchronized (identityCreationLock) {
|
||||
if (identityStore.get(key).isPresent()) {
|
||||
throw new AwsException("AlreadyExistsException",
|
||||
"Email identity " + emailIdentity + " already exist.", 400);
|
||||
}
|
||||
SesTags.validate(tags);
|
||||
if (configurationSetExistsCheck != null) {
|
||||
configurationSetExistsCheck.run();
|
||||
}
|
||||
identity = emailIdentity.contains("@")
|
||||
? new Identity(emailIdentity, "EmailAddress")
|
||||
: newDomainIdentity(emailIdentity);
|
||||
if (configurationSetName != null) {
|
||||
identity.setConfigurationSetName(configurationSetName);
|
||||
}
|
||||
if (tags != null) {
|
||||
identity.setTags(tags);
|
||||
}
|
||||
identityStore.put(key, identity);
|
||||
}
|
||||
LOG.infov("Created email identity: {0} in region {1}", emailIdentity, region);
|
||||
return identity;
|
||||
}
|
||||
|
||||
@@ -211,13 +267,16 @@ public class SesIdentityService {
|
||||
/** Registers the CVET recipient as a pending email identity when it is not already known. */
|
||||
public void markPendingEmailIdentity(String emailAddress, String region) {
|
||||
String key = identityKey(region, emailAddress);
|
||||
if (identityStore.get(key).isEmpty()) {
|
||||
synchronized (identityCreationLock) {
|
||||
if (identityStore.get(key).isPresent()) {
|
||||
return;
|
||||
}
|
||||
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);
|
||||
}
|
||||
LOG.infov("SES custom verification email registered pending identity {0} in region {1}",
|
||||
emailAddress, region);
|
||||
}
|
||||
|
||||
static final List<String> NOTIFICATION_TYPES = List.of("Bounce", "Complaint", "Delivery");
|
||||
@@ -293,13 +352,6 @@ public class SesIdentityService {
|
||||
LOG.infov("Untagged SES identity: {0} (region {1}, -{2} keys)", identityValue, region, tagKeys.size());
|
||||
}
|
||||
|
||||
public void setTags(String identityValue, String region, List<Tag> tags) {
|
||||
SesTags.validate(tags);
|
||||
Identity identity = requireForTags(identityValue, region);
|
||||
identity.setTags(tags);
|
||||
identityStore.put(identityKey(region, identityValue), identity);
|
||||
}
|
||||
|
||||
private Identity requireForTags(String identityValue, String region) {
|
||||
return identityStore.get(identityKey(region, identityValue))
|
||||
.orElseThrow(() -> new AwsException("NotFoundException",
|
||||
@@ -382,17 +434,20 @@ public class SesIdentityService {
|
||||
// Domain-only action: an email-shaped value must not create an email-valued "Domain".
|
||||
throw new AwsException("InvalidParameterValue", "Domain " + domain + " is invalid.", 400);
|
||||
}
|
||||
Identity identity = identityStore.get(identityKey(region, domain)).orElse(null);
|
||||
if (identity == null) {
|
||||
identity = new Identity(domain, "Domain");
|
||||
identity.setVerificationStatus("Pending");
|
||||
identity.setDkimEnabled(true);
|
||||
identity.setDkimVerificationStatus("Pending");
|
||||
Identity identity;
|
||||
synchronized (identityCreationLock) {
|
||||
identity = identityStore.get(identityKey(region, domain)).orElse(null);
|
||||
if (identity == null) {
|
||||
identity = new Identity(domain, "Domain");
|
||||
identity.setVerificationStatus("Pending");
|
||||
identity.setDkimEnabled(true);
|
||||
identity.setDkimVerificationStatus("Pending");
|
||||
}
|
||||
if (!hasDkimTokens(identity)) {
|
||||
regenerateDkimTokens(identity);
|
||||
}
|
||||
identityStore.put(identityKey(region, domain), identity);
|
||||
}
|
||||
if (!hasDkimTokens(identity)) {
|
||||
regenerateDkimTokens(identity);
|
||||
}
|
||||
identityStore.put(identityKey(region, domain), identity);
|
||||
LOG.infov("VerifyDomainDkim: {0} (region {1})", domain, region);
|
||||
return identity.getDkimTokens();
|
||||
}
|
||||
|
||||
@@ -172,6 +172,19 @@ public class SesService {
|
||||
return identityService.verifyEmailIdentity(emailAddress, region);
|
||||
}
|
||||
|
||||
/**
|
||||
* v2 CreateEmailIdentity. The identity domain builds and persists the complete record in one
|
||||
* write; only the configuration-set existence check is cross-domain, so it is passed in as the
|
||||
* in-lock callback (a missing set fails the whole call and nothing is created, matching AWS).
|
||||
*/
|
||||
public Identity createEmailIdentity(String emailIdentity, String configurationSetName,
|
||||
List<Tag> tags, String region) {
|
||||
Runnable configurationSetExistsCheck = configurationSetName == null ? null
|
||||
: () -> getConfigurationSet(configurationSetName, region);
|
||||
return identityService.createEmailIdentity(emailIdentity, configurationSetName, tags, region,
|
||||
configurationSetExistsCheck);
|
||||
}
|
||||
|
||||
public Identity verifyDomainIdentity(String domain, String region) {
|
||||
return identityService.verifyDomainIdentity(domain, region);
|
||||
}
|
||||
@@ -1527,10 +1540,6 @@ public class SesService {
|
||||
}
|
||||
}
|
||||
|
||||
public void setIdentityTags(String identityValue, String region, List<Tag> tags) {
|
||||
identityService.setTags(identityValue, region, tags);
|
||||
}
|
||||
|
||||
// name is everything after the type's first slash and may itself contain one: a tenant ARN's
|
||||
// <name>/<tenantId> remainder passes through whole, decomposed by the tenant domain.
|
||||
private record ResourceRef(String account, String region, String type, String name) {}
|
||||
|
||||
Reference in new issue
Block a user