feat(cloudformation): provision SES email identities (#4812)

* feat(cloudformation): provision SES email identities

* fix(cloudformation): retry pending SES update rollback

* fix(cloudformation): keep recovered SES resource complete

* fix(cloudformation): retain recovered SES state on failed replacement

* test(cloudformation): use shared waiter for SES DKIM verification
This commit is contained in:
XuJian authored and GitHub committed 2026-10-15 10:26:40 -04:00
1 parent fdfc74146e
commit 7f8383db38
14 files changed
+2655 -3

No files matched your search

@@ -0,0 +1,257 @@
package com.floci.test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
import software.amazon.awssdk.services.cloudformation.model.CloudFormationException;
import software.amazon.awssdk.services.cloudformation.model.Output;
import software.amazon.awssdk.services.cloudformation.model.Stack;
import software.amazon.awssdk.services.route53.Route53Client;
import software.amazon.awssdk.services.route53.model.Change;
import software.amazon.awssdk.services.route53.model.ChangeAction;
import software.amazon.awssdk.services.route53.model.ChangeBatch;
import software.amazon.awssdk.services.route53.model.RRType;
import software.amazon.awssdk.services.route53.model.ResourceRecord;
import software.amazon.awssdk.services.route53.model.ResourceRecordSet;
import software.amazon.awssdk.services.sesv2.SesV2Client;
import software.amazon.awssdk.services.sesv2.model.DkimSigningKeyLength;
import software.amazon.awssdk.services.sesv2.model.DkimStatus;
import software.amazon.awssdk.services.sesv2.model.GetEmailIdentityResponse;
import software.amazon.awssdk.services.sesv2.model.NotFoundException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
class CloudFormationSesEmailIdentityTest {
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final String SIGNING_ZONE = "dkim.amazonses.com";
private CloudFormationClient cfn;
private SesV2Client ses;
private Route53Client route53;
private String stackName;
private String stackId;
@BeforeEach
void setup() {
cfn = TestFixtures.cloudFormationClient();
ses = TestFixtures.sesV2Client();
route53 = TestFixtures.route53Client();
stackName = TestFixtures.uniqueName("compat-cfn-ses");
}
@AfterEach
void cleanup() throws InterruptedException {
try {
deleteStack();
} finally {
route53.close();
ses.close();
cfn.close();
}
}
@Test
void domainDnsAttributesTrackRotationAndVerifyWithInheritedEmail() throws Exception {
String domain = stackName + ".floci.test";
String email = "user@" + domain;
createStack(domain);
List<String> originalTokens = identity(domain).dkimAttributes().tokens();
assertDnsOutputs(domain, originalTokens);
ses.createEmailIdentity(r -> r.emailIdentity(email));
String zoneId = null;
List<ResourceRecordSet> records = List.of();
boolean published = false;
try {
assertThat(identity(email).dkimAttributes().signingHostedZone()).isEqualTo(SIGNING_ZONE);
cfn.updateStack(r -> r.stackName(stackId).templateBody(template(domain, true, false)));
awaitStatus("UPDATE_COMPLETE");
GetEmailIdentityResponse updated = identity(domain);
List<String> tokens = updated.dkimAttributes().tokens();
assertThat(tokens).hasSize(3).doesNotContainAnyElementsOf(originalTokens);
assertThat(updated.dkimAttributes().signingHostedZone()).isEqualTo(SIGNING_ZONE);
assertDnsOutputs(domain, tokens);
assertThat(identity(email).dkimAttributes().tokens()).containsExactlyElementsOf(tokens);
assertThat(identity(email).dkimAttributes().signingHostedZone()).isEqualTo(SIGNING_ZONE);
Map<String, String> outputs = outputs();
records = List.of(1, 2, 3).stream().map(index -> ResourceRecordSet.builder()
.name(outputs.get("DkimName" + index)).type(RRType.CNAME).ttl(60L)
.resourceRecords(ResourceRecord.builder().value(outputs.get("DkimValue" + index)).build())
.build()).toList();
zoneId = route53.createHostedZone(r -> r.name(domain).callerReference(stackName)).hostedZone().id();
changeRecords(zoneId, records, ChangeAction.CREATE);
published = true;
TestFixtures.await(() -> identity(domain).dkimAttributes().status() == DkimStatus.SUCCESS,
domain, "DKIM verification", TestFixtures.isRealAws() ? 600_000_000_000L : 60_000_000_000L,
TestFixtures.isRealAws() ? 2_000 : 500);
GetEmailIdentityResponse verified = identity(domain);
assertThat(verified.dkimAttributes().status()).isEqualTo(DkimStatus.SUCCESS);
assertThat(verified.verifiedForSendingStatus()).isTrue();
assertThat(identity(email).dkimAttributes().status()).isEqualTo(DkimStatus.SUCCESS);
} finally {
try {
if (zoneId != null) {
if (published) {
changeRecords(zoneId, records, ChangeAction.DELETE);
}
String cleanupZone = zoneId;
route53.deleteHostedZone(r -> r.id(cleanupZone));
}
} finally {
ses.deleteEmailIdentity(r -> r.emailIdentity(email));
}
}
}
@Test
void failedDependentRestoresDkimTokensSettingsTagsAndOutputs() throws Exception {
String domain = stackName + ".floci.test";
createStack(domain);
List<String> originalTokens = identity(domain).dkimAttributes().tokens();
Map<String, String> originalOutputs = outputs();
cfn.updateStack(r -> r.stackName(stackId).templateBody(template(domain, true, true)));
awaitStatus("UPDATE_ROLLBACK_COMPLETE");
assertThat(cfn.describeStackEvents(r -> r.stackName(stackId)).stackEvents())
.filteredOn(event -> "Broken".equals(event.logicalResourceId()))
.anySatisfy(event -> {
assertThat(event.resourceStatusAsString()).isEqualTo("CREATE_FAILED");
assertThat(event.resourceStatusReason())
.isEqualTo("You can't specify both SecretString and GenerateSecretString");
});
GetEmailIdentityResponse restored = identity(domain);
assertThat(restored.dkimAttributes().tokens()).containsExactlyElementsOf(originalTokens);
assertThat(restored.dkimAttributes().signingHostedZone()).isEqualTo(SIGNING_ZONE);
assertThat(restored.dkimAttributes().nextSigningKeyLength()).isEqualTo(DkimSigningKeyLength.RSA_2048_BIT);
assertThat(restored.dkimAttributes().signingEnabled()).isTrue();
assertThat(restored.feedbackForwardingStatus()).isTrue();
assertThat(restored.mailFromAttributes().mailFromDomain()).isNull();
assertThat(restored.mailFromAttributes().behaviorOnMxFailureAsString()).isEqualTo("USE_DEFAULT_VALUE");
assertThat(restored.tags()).anySatisfy(tag -> {
assertThat(tag.key()).isEqualTo("purpose");
assertThat(tag.value()).isEqualTo("old");
});
assertThat(outputs()).isEqualTo(originalOutputs);
assertDnsOutputs(domain, originalTokens);
}
@Test
void standaloneEmailHasRefAndEmptyDnsAttributesAndDeletesWithItsStack() throws Exception {
String email = "user@" + stackName + ".floci.test";
createStack(email);
assertThat(outputs().get("IdentityRef")).isEqualTo(email);
assertThat(identity(email).dkimAttributes().tokens()).isEmpty();
assertThat(identity(email).dkimAttributes().signingHostedZone()).isNull();
for (int index = 1; index <= 3; index++) {
assertThat(outputs().get("DkimName" + index)).isEmpty();
assertThat(outputs().get("DkimValue" + index)).isEmpty();
}
deleteStack();
assertThatThrownBy(() -> identity(email)).isInstanceOf(NotFoundException.class);
}
private String template(String identity, boolean updated, boolean fail) {
Map<String, Object> properties = new LinkedHashMap<>();
properties.put("EmailIdentity", identity);
properties.put("Tags", List.of(Map.of("Key", "purpose", "Value", updated ? "new" : "old")));
if (updated) {
properties.put("DkimSigningAttributes", Map.of("NextSigningKeyLength", "RSA_1024_BIT"));
properties.put("DkimAttributes", Map.of("SigningEnabled", false));
properties.put("MailFromAttributes", Map.of("MailFromDomain", "mail." + identity,
"BehaviorOnMxFailure", "REJECT_MESSAGE"));
properties.put("FeedbackAttributes", Map.of("EmailForwardingEnabled", false));
}
Map<String, Object> resources = new LinkedHashMap<>();
resources.put("Identity", Map.of("Type", "AWS::SES::EmailIdentity", "Properties", properties));
if (fail) {
resources.put("Broken", Map.of("Type", "AWS::SecretsManager::Secret", "DependsOn", "Identity",
"Properties", Map.of("SecretString", "explicit",
"GenerateSecretString", Map.of("PasswordLength", 32))));
}
Map<String, Object> outputs = new LinkedHashMap<>();
outputs.put("IdentityRef", Map.of("Value", Map.of("Ref", "Identity")));
for (int index = 1; index <= 3; index++) {
outputs.put("DkimName" + index,
Map.of("Value", Map.of("Fn::GetAtt", List.of("Identity", "DkimDNSTokenName" + index))));
outputs.put("DkimValue" + index,
Map.of("Value", Map.of("Fn::GetAtt", List.of("Identity", "DkimDNSTokenValue" + index))));
}
try {
return MAPPER.writeValueAsString(Map.of("Resources", resources, "Outputs", outputs));
} catch (JsonProcessingException failure) {
throw new IllegalStateException(failure);
}
}
private void createStack(String identity) throws InterruptedException {
stackId = cfn.createStack(r -> r.stackName(stackName).templateBody(template(identity, false, false))).stackId();
awaitStatus("CREATE_COMPLETE");
}
private void awaitStatus(String expected) throws InterruptedException {
TestFixtures.await(() -> {
Stack stack = cfn.describeStacks(r -> r.stackName(stackId)).stacks().get(0);
String actual = stack.stackStatusAsString();
if (expected.equals(actual)) {
return true;
}
if (actual.endsWith("_FAILED") || (!"DELETE_COMPLETE".equals(expected)
&& actual.contains("ROLLBACK") && !actual.endsWith("_IN_PROGRESS"))) {
throw new AssertionError(stackName + " reached " + actual + " instead of " + expected + ": "
+ stack.stackStatusReason());
}
return false;
}, stackName, expected, TestFixtures.isRealAws() ? 600_000_000_000L : 60_000_000_000L,
TestFixtures.isRealAws() ? 2_000 : 100);
}
private GetEmailIdentityResponse identity(String name) {
return ses.getEmailIdentity(r -> r.emailIdentity(name));
}
private Map<String, String> outputs() {
return cfn.describeStacks(r -> r.stackName(stackId)).stacks().get(0).outputs().stream()
.collect(Collectors.toMap(Output::outputKey, Output::outputValue));
}
private void assertDnsOutputs(String domain, List<String> tokens) {
Map<String, String> values = outputs();
assertThat(values.get("IdentityRef")).isEqualTo(domain);
for (int index = 1; index <= 3; index++) {
assertThat(values.get("DkimName" + index)).isEqualTo(tokens.get(index - 1) + "._domainkey." + domain);
assertThat(values.get("DkimValue" + index)).isEqualTo(tokens.get(index - 1) + "." + SIGNING_ZONE);
}
}
private void changeRecords(String zoneId, List<ResourceRecordSet> records, ChangeAction action) {
List<Change> changes = records.stream().map(record -> Change.builder().action(action)
.resourceRecordSet(record).build()).toList();
route53.changeResourceRecordSets(r -> r.hostedZoneId(zoneId)
.changeBatch(ChangeBatch.builder().changes(changes).build()));
}
private void deleteStack() throws InterruptedException {
if (stackId == null) {
return;
}
cfn.deleteStack(r -> r.stackName(stackId));
try {
awaitStatus("DELETE_COMPLETE");
} catch (CloudFormationException failure) {
assertThat(failure.awsErrorDetails().errorCode()).isEqualTo("ValidationError");
assertThat(failure.awsErrorDetails().errorMessage())
.isEqualTo("Stack with id " + stackId + " does not exist");
}
stackId = null;
}
}
+16
View File
@@ -147,6 +147,7 @@ cross-resource references.
| S3 | `Bucket`, `BucketPolicy` (document stored on the bucket; S3 does not evaluate it) |
| SQS | `Queue`, `QueuePolicy` (accepted; policy not enforced) |
| SNS | `Topic`, `Subscription`, `TopicPolicy` |
| SES | `EmailIdentity` (BYODKIM is not supported by this resource; Easy DKIM DNS attributes use the shared emulator signing zone `dkim.amazonses.com` rather than AWS's region- and identity-specific zone assignment) |
| DynamoDB | `Table` (`TimeToLiveSpecification` applied on create and update; to rename an enabled TTL attribute, disable TTL in one update and enable the new attribute in a later one), `GlobalTable` |
| Lambda | `Function` (Zip via S3/inline `ZipFile`, Image, and the `hot-reload` bind-mount bucket), `LayerVersion`, `EventSourceMapping` (SQS, Kinesis, DynamoDB Streams; `ParallelizationFactor` and `TumblingWindowInSeconds` are ignored), `Version`, `Alias` (also what SAM's `AutoPublishAlias` expands into), `Permission`, `EventInvokeConfig` (also what a SAM function's `EventInvokeConfig` expands into), `MicrovmImage`, `NetworkConnector`, `Url`. Inline `ZipFile` packages include the `cfn-response` (Node.js) / `cfnresponse` (Python) module AWS injects for that code path, so Solutions-style custom-resource handlers work. |
| IAM | `Role`, `User` (template `LoginProfile` and `PermissionsBoundary` are ignored; an API-created login profile is removed on delete), `AccessKey`, `Policy`, `ManagedPolicy`, `InstanceProfile` |
@@ -211,6 +212,21 @@ Set `floci.services.cloudformation.allow-stub-unsupported-resource-types` to `fa
resource instead: it reaches `CREATE_FAILED` and the stack rolls back. Use it in a pipeline that
must not pass over a resource it never got.
## SES Email Identities
`AWS::SES::EmailIdentity` provisions an SES identity and supports in-place option updates and
replacement when `EmailIdentity` changes. Change-set previews also mark the replacement and its
`Ref` dependents.
If an update rollback cannot restore the identity snapshot, the stack remains in
`UPDATE_ROLLBACK_FAILED`. `ContinueUpdateRollback` retries the restoration; `UpdateStack` is
rejected until rollback completes. A successful retry permits later updates. `DeleteStack` can
delete the current managed identity without first restoring its snapshot.
Cleanup keeps displaced-identity records while deletion attempts remain. After three cleanup
attempts, an exhausted entry is removed from stack management and a later `DeleteStack` does not
retry it. Committed update cleanup uses the same three-attempt limit.
## EventBridge Scheduler Schedules
`AWS::Scheduler::Schedule` creates a real schedule. `Ref` returns its name and `Fn::GetAtt Arn`
@@ -1715,6 +1715,15 @@ public class CloudFormationService implements ResourceProvider {
if (resources.isObject()) {
List<String> sortedLogicalIds = topologicalSort(resources, conditions);
if (!isCreate) {
for (String logicalId : sortedLogicalIds) {
StackResource existing = stack.getResources().get(logicalId);
if (existing != null && dispatcher.requiresUpdateRecovery(existing)) {
changedResourceIds.add(logicalId);
}
}
}
for (String logicalId : sortedLogicalIds) {
if (!isCreate && !changedResourceIds.contains(logicalId)) {
continue;
@@ -2451,8 +2460,9 @@ public class CloudFormationService implements ResourceProvider {
|| dispatcher.rollbackUpdate(resource,
event -> addEvent(stack, resource.getLogicalId(), event.getPhysicalResourceId(),
resource.getResourceType(), event.getResourceStatus(), event.getResourceStatusReason()))) {
resource.setStatus(previous.getStatus());
resource.setStatusReason(previous.getStatusReason());
boolean recoveredPendingUpdate = dispatcher.requiresUpdateRecovery(previous);
resource.setStatus(recoveredPendingUpdate ? "UPDATE_COMPLETE" : previous.getStatus());
resource.setStatusReason(recoveredPendingUpdate ? null : previous.getStatusReason());
addEvent(stack, resource.getLogicalId(), resource.getPhysicalId(),
resource.getResourceType(), "UPDATE_COMPLETE",
"Resource update rolled back");
@@ -277,6 +277,12 @@ public class CfnResourceDispatcher {
.orElse(false);
}
public boolean requiresUpdateRecovery(StackResource resource) {
return registry.forType(resource.getResourceType())
.map(owner -> owner.requiresUpdateRecovery(resource))
.orElse(false);
}
/** Whether this update replaced the resource's physical entity, so the stack has cleanup pending. */
public boolean hasReplacementUpdate(StackResource resource) {
return registry.forType(resource.getResourceType())
@@ -59,6 +59,11 @@ public interface CfnResourceProvisioner {
return false;
}
/** Whether a prior failed rollback must be retried before even an outputs-only update commits. */
default boolean requiresUpdateRecovery(StackResource resource) {
return false;
}
/**
* The physical id of the entity this update's replacement displaced, or null when this type
* owes no replacement cleanup. {@code CloudFormationService} announces it as DELETE_IN_PROGRESS
@@ -0,0 +1,506 @@
package io.github.hectorvent.floci.services.cloudformation.provisioners;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
import io.github.hectorvent.floci.services.ses.SesIdentityService;
import io.github.hectorvent.floci.services.ses.SesService;
import io.github.hectorvent.floci.services.ses.model.Identity;
import io.github.hectorvent.floci.services.ses.model.Tag;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ApplicationScoped
public class SesCfnProvisioner implements CfnResourceProvisioner {
private static final String TYPE = "AWS::SES::EmailIdentity";
private static final String DEFAULT_KEY_LENGTH = "RSA_2048_BIT";
private static final String MANAGED_TAGS_ATTR = "__FlociSesManagedTags";
private static final String UPDATE_SNAPSHOT_ATTR = "__FlociSesUpdateSnapshot";
private static final ObjectMapper MAPPER = new ObjectMapper().findAndRegisterModules();
private final SesService sesService;
private final SesIdentityService identityService;
@Inject
public SesCfnProvisioner(SesService sesService, SesIdentityService identityService) {
this.sesService = sesService;
this.identityService = identityService;
}
@Override
public Set<String> resourceTypes() {
return Set.of(TYPE);
}
@Override
public void provision(StackResource resource, JsonNode props, ProvisionContext ctx) {
Properties desired = readProperties(props, ctx);
boolean recoveredSnapshot = false;
if (resource.getAttributes().containsKey(UPDATE_SNAPSHOT_ATTR)) {
// A previous rollback may have failed to persist the last successful identity. Restore
// it before either an in-place update or replacement can overwrite its snapshot.
rollbackUpdate(resource);
recoveredSnapshot = true;
ctx = new ProvisionContext(ctx.engine(), ctx.region(), ctx.accountId(), ctx.stackName(),
resource.getPhysicalId(), ctx.progress(), ctx.priorProperties());
}
String identityName = desired.identity();
boolean updating = ctx.reusesPriorEntity(identityName);
Map<String, String> attributesBefore = new HashMap<>(resource.getAttributes());
boolean created = false;
try {
Identity identity;
if (updating) {
identity = identityService.getIdentityVerificationAttributes(identityName, ctx.region());
if (identity == null) {
throw new AwsException("NotFoundException", "Email identity " + identityName
+ " does not exist.", 404);
}
snapshotUpdate(resource, identity, ctx);
} else {
identity = sesService.createEmailIdentity(identityName, desired.configurationSet(),
desired.tags(), ctx.region());
created = true;
}
reconcile(resource, identity, desired, ctx);
Identity current = identityService.getIdentityVerificationAttributes(identityName, ctx.region());
if (current == null) {
throw new AwsException("NotFoundException", "Email identity " + identityName + " does not exist.", 404);
}
resource.setPhysicalId(identityName);
populateDkimAttributes(resource, current);
resource.getAttributes().put(MANAGED_TAGS_ATTR, MAPPER.valueToTree(desired.tags()).toString());
ReplacementCleanup.record(resource, ctx, attributesBefore);
if (ctx.isUpdate() && !updating) {
ObjectNode snapshot = MAPPER.createObjectNode();
snapshot.put("managedTags", attributesBefore.get(MANAGED_TAGS_ATTR));
resource.getAttributes().put(UPDATE_SNAPSHOT_ATTR, snapshot.toString());
}
} catch (RuntimeException failure) {
if (!updating && created) {
try {
sesService.deleteIdentity(identityName, ctx.region());
resource.setPhysicalId(ctx.priorPhysicalId());
resource.getAttributes().clear();
resource.getAttributes().putAll(attributesBefore);
} catch (RuntimeException cleanupFailure) {
if (!ctx.isUpdate()) {
resource.setPhysicalId(identityName);
resource.getAttributes().put(CfnRollback.ROLLBACK_OWNED_ATTR, "true");
}
ReplacementCleanup.recordOrphan(resource, identityName, TYPE, ctx.region());
failure.addSuppressed(cleanupFailure);
}
}
if (recoveredSnapshot && !resource.getAttributes().containsKey(UPDATE_SNAPSHOT_ATTR)) {
// The prior recovery already restored the live identity. Keep the attempted
// resource so the rollback walker does not put the stale snapshot back over it.
resource.getAttributes().put(CfnRollback.UPDATE_ROLLBACK_RESTORED_ATTR, "true");
}
throw failure;
}
}
@Override
public void delete(String resourceType, String physicalId, String region) {
sesService.deleteIdentity(physicalId, region);
}
@Override
public void delete(StackResource resource, String region) {
UpdateCleanupResult cleanup = ReplacementCleanup.complete(resource, this::delete);
if (cleanup.applicable() && !cleanup.complete()) {
throw new AwsException("InternalFailure",
"Could not delete displaced SES identity " + cleanup.previousPhysicalId()
+ ": " + cleanup.failureReason(), 500);
}
delete(resource.getResourceType(), resource.getPhysicalId(), region);
resource.getAttributes().remove(UPDATE_SNAPSHOT_ATTR);
}
@Override
public boolean hasReplacementUpdate(StackResource resource) {
return ReplacementCleanup.hasReplacement(resource);
}
@Override
public String updateCleanupPhysicalId(StackResource resource) {
return ReplacementCleanup.cleanupPhysicalId(resource);
}
@Override
public UpdateCleanupResult completeUpdate(StackResource resource) {
if ("UPDATE_FAILED".equals(resource.getStatus())
&& resource.getAttributes().containsKey(UPDATE_SNAPSHOT_ATTR)) {
throw new AwsException("InternalFailure",
"SES identity rollback is still pending for " + resource.getPhysicalId(), 500);
}
UpdateCleanupResult cleanup = ReplacementCleanup.complete(resource, this::delete);
if (!cleanup.applicable() && resource.getAttributes().containsKey(UPDATE_SNAPSHOT_ATTR)) {
return new UpdateCleanupResult(true, true, null, 0, null);
}
return cleanup;
}
@Override
public UpdateCleanupResult completeDeleteCleanup(StackResource resource) {
return ReplacementCleanup.complete(resource, this::delete);
}
@Override
public void clearDeleteCleanup(StackResource resource) {
ReplacementCleanup.clear(resource);
}
@Override
public void clearUpdate(StackResource resource) {
ReplacementCleanup.clear(resource);
resource.getAttributes().remove(UPDATE_SNAPSHOT_ATTR);
}
@Override
public boolean retainsFailedUpdateState(StackResource resource) {
// Orphan cleanup is additive ownership, not a mutation of the prior identity. A completed
// recovery also keeps the attempted resource when a replacement then fails, so the engine
// must not restore the stale snapshot from the previous resource object.
return resource.getAttributes().containsKey(UPDATE_SNAPSHOT_ATTR)
|| resource.getAttributes().containsKey(CfnRollback.UPDATE_ROLLBACK_RESTORED_ATTR);
}
@Override
public boolean requiresUpdateRecovery(StackResource resource) {
return "UPDATE_FAILED".equals(resource.getStatus())
&& resource.getAttributes().containsKey(UPDATE_SNAPSHOT_ATTR);
}
@Override
public boolean rollbackUpdate(StackResource resource) {
if (ReplacementCleanup.rollback(resource, this::delete)) {
restoreManagedTags(resource, readSnapshot(resource));
resource.getAttributes().remove(UPDATE_SNAPSHOT_ATTR);
return true;
}
JsonNode snapshot = readSnapshot(resource);
if (snapshot == null) {
return false;
}
if (!snapshot.has("identity")) {
restoreManagedTags(resource, snapshot);
resource.getAttributes().remove(UPDATE_SNAPSHOT_ATTR);
return true;
}
String region = snapshot.path("region").asText();
try {
Identity previous = MAPPER.treeToValue(snapshot.path("identity"), Identity.class);
identityService.save(previous, region);
populateDkimAttributes(resource, previous);
} catch (Exception failure) {
throw new IllegalStateException("Could not restore SES identity update snapshot", failure);
}
restoreManagedTags(resource, snapshot);
resource.getAttributes().remove(UPDATE_SNAPSHOT_ATTR);
return true;
}
private void restoreManagedTags(StackResource resource, JsonNode snapshot) {
JsonNode managedTags = snapshot == null ? null : snapshot.path("managedTags");
if (managedTags != null && managedTags.isTextual()) {
resource.getAttributes().put(MANAGED_TAGS_ATTR, managedTags.textValue());
} else {
resource.getAttributes().remove(MANAGED_TAGS_ATTR);
}
}
private void snapshotUpdate(StackResource resource, Identity identity, ProvisionContext ctx) {
ObjectNode snapshot = MAPPER.createObjectNode();
snapshot.set("identity", MAPPER.valueToTree(identity));
snapshot.put("region", ctx.region());
snapshot.put("managedTags", resource.getAttributes().get(MANAGED_TAGS_ATTR));
resource.getAttributes().put(UPDATE_SNAPSHOT_ATTR, snapshot.toString());
}
private JsonNode readSnapshot(StackResource resource) {
String raw = resource.getAttributes().get(UPDATE_SNAPSHOT_ATTR);
if (raw == null) {
return null;
}
try {
return MAPPER.readTree(raw);
} catch (Exception failure) {
throw new IllegalStateException("Invalid SES identity update snapshot", failure);
}
}
private void reconcile(StackResource resource, Identity current, Properties desired, ProvisionContext ctx) {
String identityName = desired.identity();
String region = ctx.region();
if (!same(current.getConfigurationSetName(), desired.configurationSet())) {
sesService.setEmailIdentityConfigurationSet(identityName, desired.configurationSet(), region);
}
if ("Domain".equals(current.getIdentityType())) {
String keyLength = desired.nextSigningKeyLength() == null
? DEFAULT_KEY_LENGTH : desired.nextSigningKeyLength();
if (!keyLength.equals(current.getDkimNextSigningKeyLength())) {
identityService.putDkimSigningAttributes(identityName, "AWS_SES", null, keyLength, region);
}
}
boolean signingEnabled = desired.signingEnabled() != null
? desired.signingEnabled() : "Domain".equals(current.getIdentityType());
if (current.isDkimEnabled() != signingEnabled) {
identityService.setDkimAttributes(identityName, signingEnabled, region);
}
String mailFromDomain = desired.mailFromDomain();
String currentMailFromDomain = current.getMailFromDomain();
String behavior = desired.behaviorOnMxFailure() == null
? "UseDefaultValue" : desired.behaviorOnMxFailure();
if (!same(currentMailFromDomain, mailFromDomain)
|| (mailFromDomain != null && !behavior.equals(current.getBehaviorOnMxFailure()))) {
identityService.setMailFromDomain(identityName,
mailFromDomain == null ? "" : mailFromDomain, behavior, region);
}
boolean forwarding = desired.emailForwardingEnabled() == null || desired.emailForwardingEnabled();
if (current.isFeedbackForwardingEnabled() != forwarding) {
identityService.setFeedbackForwardingEnabled(identityName, forwarding, region);
}
reconcileTags(resource, identityName, desired.tags(), ctx);
}
private void reconcileTags(StackResource resource, String identityName, List<Tag> desired, ProvisionContext ctx) {
String arn = identityArn(identityName, ctx.region(), ctx.accountId());
List<Tag> current = sesService.listResourceTags(arn, ctx.region());
List<Tag> previouslyManaged = managedTags(resource, ctx.reusesPriorEntity(identityName), current);
Map<String, String> desiredByKey = new HashMap<>();
for (Tag tag : desired) {
desiredByKey.put(tag.key(), tag.value());
}
List<String> staleKeys = new ArrayList<>();
Map<String, String> currentByKey = new HashMap<>();
for (Tag tag : current) {
currentByKey.put(tag.key(), tag.value());
}
for (Tag tag : previouslyManaged) {
if (!desiredByKey.containsKey(tag.key()) && currentByKey.containsKey(tag.key())) {
staleKeys.add(tag.key());
}
}
if (!staleKeys.isEmpty()) {
sesService.untagResource(arn, ctx.region(), staleKeys);
}
List<Tag> changed = new ArrayList<>();
for (Tag tag : desired) {
if (!tag.value().equals(currentByKey.get(tag.key()))) {
changed.add(tag);
}
}
if (!changed.isEmpty()) {
sesService.tagResource(arn, ctx.region(), changed);
}
}
private List<Tag> managedTags(StackResource resource, boolean updating, List<Tag> current) {
if (!updating) {
return current;
}
String raw = resource.getAttributes().get(MANAGED_TAGS_ATTR);
if (raw == null) {
return List.of();
}
try {
JsonNode stored = MAPPER.readTree(raw);
List<Tag> tags = new ArrayList<>();
for (JsonNode tag : stored) {
tags.add(new Tag(tag.path("Key").asText(), tag.path("Value").asText()));
}
return tags;
} catch (Exception failure) {
throw new IllegalStateException("Invalid SES identity managed tags", failure);
}
}
private String identityArn(String identityName, String region, String accountId) {
return AwsArnUtils.Arn.of("ses", region, accountId, "identity/" + identityName).toString();
}
private void populateDkimAttributes(StackResource resource, Identity identity) {
List<String> tokens = identity != null && "Domain".equals(identity.getIdentityType())
&& "AWS_SES".equals(identity.getDkimSigningAttributesOrigin())
&& identity.getDkimTokens() != null ? identity.getDkimTokens() : List.of();
String[] names = new String[3];
String[] values = new String[3];
for (int index = 0; index < 3; index++) {
String token = index < tokens.size() ? tokens.get(index) : null;
names[index] = token == null ? "" : token + "._domainkey." + identity.getIdentity();
values[index] = token == null ? "" : token + "." + SesIdentityService.DKIM_SIGNING_HOSTED_ZONE;
}
resource.getAttributes().put("DkimDNSTokenName1", names[0]);
resource.getAttributes().put("DkimDNSTokenName2", names[1]);
resource.getAttributes().put("DkimDNSTokenName3", names[2]);
resource.getAttributes().put("DkimDNSTokenValue1", values[0]);
resource.getAttributes().put("DkimDNSTokenValue2", values[1]);
resource.getAttributes().put("DkimDNSTokenValue3", values[2]);
}
private Properties readProperties(JsonNode props, ProvisionContext ctx) {
if (props == null || !props.isObject()) {
throw invalid("EmailIdentity is required.");
}
validateKeys(props, Set.of("EmailIdentity", "ConfigurationSetAttributes", "DkimSigningAttributes",
"DkimAttributes", "MailFromAttributes", "FeedbackAttributes", "Tags"), "Properties");
String identity = requiredText(ctx.engine().resolveNode(props.get("EmailIdentity")), "EmailIdentity");
JsonNode config = object(props, "ConfigurationSetAttributes", Set.of("ConfigurationSetName"), ctx);
String configurationSet = optionalText(config, "ConfigurationSetName");
if (configurationSet != null && configurationSet.isEmpty()) {
configurationSet = null;
}
JsonNode signing = object(props, "DkimSigningAttributes",
Set.of("DomainSigningSelector", "DomainSigningPrivateKey", "NextSigningKeyLength"), ctx);
if (optionalText(signing, "DomainSigningSelector") != null
|| optionalText(signing, "DomainSigningPrivateKey") != null) {
throw invalid("BYODKIM is not supported by this CloudFormation resource.");
}
String keyLength = optionalText(signing, "NextSigningKeyLength");
if (keyLength != null && !Set.of("RSA_1024_BIT", DEFAULT_KEY_LENGTH).contains(keyLength)) {
throw invalid("NextSigningKeyLength must be RSA_1024_BIT or RSA_2048_BIT.");
}
if (identity.contains("@") && keyLength != null) {
throw invalid("DkimSigningAttributes requires a domain identity.");
}
JsonNode dkim = object(props, "DkimAttributes", Set.of("SigningEnabled"), ctx);
Boolean signingEnabled = optionalBoolean(dkim, "SigningEnabled");
JsonNode mailFrom = object(props, "MailFromAttributes",
Set.of("MailFromDomain", "BehaviorOnMxFailure"), ctx);
String mailFromDomain = optionalText(mailFrom, "MailFromDomain");
if (mailFromDomain != null && mailFromDomain.isEmpty()) {
mailFromDomain = null;
}
String behavior = optionalText(mailFrom, "BehaviorOnMxFailure");
if (behavior != null) {
behavior = switch (behavior) {
case "USE_DEFAULT_VALUE" -> "UseDefaultValue";
case "REJECT_MESSAGE" -> "RejectMessage";
default -> throw invalid("BehaviorOnMxFailure must be USE_DEFAULT_VALUE or REJECT_MESSAGE.");
};
if (mailFromDomain == null) {
throw invalid("MailFromDomain is required when BehaviorOnMxFailure is set.");
}
}
JsonNode feedback = object(props, "FeedbackAttributes", Set.of("EmailForwardingEnabled"), ctx);
Boolean forwarding = optionalBoolean(feedback, "EmailForwardingEnabled");
return new Properties(identity, configurationSet, keyLength, signingEnabled, mailFromDomain,
behavior, forwarding, readTags(props, ctx));
}
private List<Tag> readTags(JsonNode props, ProvisionContext ctx) {
if (!props.has("Tags")) {
return List.of();
}
JsonNode tags = ctx.engine().resolveNodeOmittingNoValue(props.get("Tags"));
if (tags == null || tags.isNull() || tags.isMissingNode()) {
return List.of();
}
if (!tags.isArray()) {
throw invalid("Tags must be an array.");
}
List<Tag> parsed = new ArrayList<>();
Set<String> keys = new HashSet<>();
for (JsonNode tag : tags) {
if (!tag.isObject()) {
throw invalid("Each tag must be an object.");
}
validateKeys(tag, Set.of("Key", "Value"), "Tag");
String key = requiredText(tag.get("Key"), "Tag.Key");
String value = text(tag.get("Value"), "Tag.Value");
if (!keys.add(key)) {
throw invalid("Duplicate tag key: " + key);
}
parsed.add(new Tag(key, value));
}
return parsed;
}
private JsonNode object(JsonNode props, String name, Set<String> allowed, ProvisionContext ctx) {
if (props == null || !props.has(name)) {
return null;
}
JsonNode resolved = ctx.engine().resolveNodeOmittingNoValue(props.get(name));
if (resolved == null || resolved.isNull() || resolved.isMissingNode()) {
return null;
}
if (!resolved.isObject()) {
throw invalid(name + " must be an object.");
}
validateKeys(resolved, allowed, name);
return resolved;
}
private void validateKeys(JsonNode object, Set<String> allowed, String name) {
Iterator<String> fields = object.fieldNames();
while (fields.hasNext()) {
String field = fields.next();
if (!allowed.contains(field)) {
throw invalid(name + " has unsupported property " + field + ".");
}
}
}
private String optionalText(JsonNode props, String name) {
return props == null || !props.has(name) || props.get(name).isNull()
? null : text(props.get(name), name);
}
private String requiredText(JsonNode node, String name) {
String value = text(node, name);
if (value.isBlank()) {
throw invalid(name + " must not be empty.");
}
return value;
}
private String text(JsonNode node, String name) {
if (node == null || !node.isTextual()) {
throw invalid(name + " must be a string.");
}
return node.textValue();
}
private Boolean optionalBoolean(JsonNode props, String name) {
if (props == null || !props.has(name) || props.get(name).isNull()) {
return null;
}
JsonNode node = props.get(name);
if (!node.isBoolean()) {
throw invalid(name + " must be a boolean.");
}
return node.booleanValue();
}
private boolean same(String left, String right) {
return left == null ? right == null : left.equals(right);
}
private AwsException invalid(String message) {
return new AwsException("ValidationError", message, 400);
}
private record Properties(String identity, String configurationSet, String nextSigningKeyLength,
Boolean signingEnabled, String mailFromDomain, String behaviorOnMxFailure,
Boolean emailForwardingEnabled, List<Tag> tags) {}
}
@@ -45,7 +45,7 @@ import java.util.concurrent.ConcurrentHashMap;
@ApplicationScoped
public class SesIdentityService implements SesTaggable {
static final String DKIM_SIGNING_HOSTED_ZONE = "dkim.amazonses.com";
public static final String DKIM_SIGNING_HOSTED_ZONE = "dkim.amazonses.com";
private static final Logger LOG = Logger.getLogger(SesIdentityService.class);
@@ -705,6 +705,9 @@
"AWS::Scheduler::ScheduleGroup": [
"Name"
],
"AWS::SES::EmailIdentity": [
"EmailIdentity"
],
"AWS::SecretsManager::Secret": [
"Name"
],
@@ -101,6 +101,7 @@ import io.github.hectorvent.floci.services.cloudformation.provisioners.Scheduler
import io.github.hectorvent.floci.services.cloudformation.provisioners.SchedulerScheduleGroupCfnProvisioner;
import io.github.hectorvent.floci.services.cloudformation.provisioners.SecretTargetAttachmentCfnProvisioner;
import io.github.hectorvent.floci.services.cloudformation.provisioners.SecretsManagerCfnProvisioner;
import io.github.hectorvent.floci.services.cloudformation.provisioners.SesCfnProvisioner;
import io.github.hectorvent.floci.services.cloudformation.provisioners.SnsCfnProvisioner;
import io.github.hectorvent.floci.services.cloudformation.provisioners.SqsCfnProvisioner;
import io.github.hectorvent.floci.services.cloudformation.provisioners.SsmCfnProvisioner;
@@ -150,6 +151,8 @@ import io.github.hectorvent.floci.services.route53.Route53Service;
import io.github.hectorvent.floci.services.s3.S3Service;
import io.github.hectorvent.floci.services.scheduler.SchedulerService;
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
import io.github.hectorvent.floci.services.ses.SesIdentityService;
import io.github.hectorvent.floci.services.ses.SesService;
import io.github.hectorvent.floci.services.sns.SnsService;
import io.github.hectorvent.floci.services.sqs.SqsService;
import io.github.hectorvent.floci.services.ssm.SsmService;
@@ -188,6 +191,8 @@ final class CfnProvisionerFixture {
private S3Service s3Service;
private SnsService snsService;
private SesService sesService;
private SesIdentityService sesIdentityService;
private DynamoDbService dynamoDbService;
private LambdaService lambdaService;
private AppSyncService appSyncService;
@@ -304,6 +309,9 @@ final class CfnProvisionerFixture {
if (snsService != null) {
discovered.add(new SnsCfnProvisioner(snsService));
}
if (sesService != null && sesIdentityService != null) {
discovered.add(new SesCfnProvisioner(sesService, sesIdentityService));
}
if (dynamoDbService != null) {
NativeDynamoDbBackend backend = new NativeDynamoDbBackend(null, null, dynamoDbService, null, null, objectMapper);
discovered.add(new DynamoDbCfnProvisioner(new DynamoDbFacade(backend, backend,
@@ -504,6 +512,16 @@ final class CfnProvisionerFixture {
return this;
}
public Builder ses(SesService v) {
this.sesService = v;
return this;
}
public Builder sesIdentity(SesIdentityService v) {
this.sesIdentityService = v;
return this;
}
public Builder dynamoDb(DynamoDbService v) {
this.dynamoDbService = v;
return this;
@@ -107,6 +107,19 @@ class CfnSchemaCoverageTest {
}
}
@Test
void sesDkimDnsAttributesAreVisibleToTheCoverageScan() {
Set<String> expected = Set.of("DkimDNSTokenName1", "DkimDNSTokenName2", "DkimDNSTokenName3",
"DkimDNSTokenValue1", "DkimDNSTokenValue2", "DkimDNSTokenValue3");
Set<String> actual = scanProvisioners().get("SesCfnProvisioner")
.attributesFor("AWS::SES::EmailIdentity");
Set<String> missing = new HashSet<>(expected);
missing.removeAll(actual);
if (!missing.isEmpty()) {
throw new AssertionError("SES DKIM DNS attributes are invisible to schema coverage: " + missing);
}
}
/** "AWS::SNS::Topic\tTopicArn" -> reason. */
private static Map<String, String> recordedGaps() {
Map<String, String> gaps = new TreeMap<>();
@@ -0,0 +1,586 @@
package io.github.hectorvent.floci.services.cloudformation.provisioners;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
import io.github.hectorvent.floci.services.ses.SesIdentityService;
import io.github.hectorvent.floci.services.ses.SesService;
import io.github.hectorvent.floci.services.ses.model.Identity;
import io.github.hectorvent.floci.services.ses.model.Tag;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class SesCfnProvisionerTest {
private final ObjectMapper mapper = new ObjectMapper();
private final SesService ses = mock(SesService.class);
private final SesIdentityService identities = mock(SesIdentityService.class);
private final SesCfnProvisioner provisioner = new SesCfnProvisioner(ses, identities);
@Test
void domainCreationSetsRefAndAllDkimDnsAttributes() throws Exception {
Identity identity = domain("example.com");
when(ses.createEmailIdentity(eq("example.com"), isNull(), eq(List.of()), eq("us-east-1")))
.thenReturn(identity);
when(identities.getIdentityVerificationAttributes("example.com", "us-east-1")).thenReturn(identity);
when(ses.listResourceTags(anyString(), eq("us-east-1"))).thenReturn(List.of());
StackResource resource = resource();
provisioner.provision(resource, props("{\"EmailIdentity\":\"example.com\"}"), context(null));
assertEquals("example.com", resource.getPhysicalId());
for (int index = 1; index <= 3; index++) {
String token = "token" + index;
assertEquals(token + "._domainkey.example.com",
resource.getAttributes().get("DkimDNSTokenName" + index));
assertEquals(token + ".dkim.amazonses.com",
resource.getAttributes().get("DkimDNSTokenValue" + index));
}
}
@Test
void updateReconcilesOptionsAndTagsWithoutCreatingAgain() throws Exception {
Identity identity = domain("example.com");
identity.setConfigurationSetName("old");
identity.setFeedbackForwardingEnabled(true);
when(identities.getIdentityVerificationAttributes("example.com", "us-east-1")).thenReturn(identity);
when(ses.listResourceTags(anyString(), eq("us-east-1")))
.thenReturn(List.of(new Tag("removed", "old"), new Tag("changed", "old"),
new Tag("external", "keep")));
StackResource resource = resource();
resource.getAttributes().put("__FlociSesManagedTags", """
[{"Key":"removed","Value":"old"},{"Key":"changed","Value":"old"}]
""");
JsonNode props = props("""
{"EmailIdentity":"example.com",
"ConfigurationSetAttributes":{"ConfigurationSetName":"new"},
"DkimSigningAttributes":{"NextSigningKeyLength":"RSA_1024_BIT"},
"DkimAttributes":{"SigningEnabled":false},
"MailFromAttributes":{"MailFromDomain":"mail.example.com",
"BehaviorOnMxFailure":"REJECT_MESSAGE"},
"FeedbackAttributes":{"EmailForwardingEnabled":false},
"Tags":[{"Key":"changed","Value":"new"}]}
""");
provisioner.provision(resource, props, context("example.com"));
assertTrue(provisioner.retainsFailedUpdateState(resource));
verify(ses, never()).createEmailIdentity(anyString(), any(), any(), anyString());
verify(ses).setEmailIdentityConfigurationSet("example.com", "new", "us-east-1");
verify(identities).putDkimSigningAttributes("example.com", "AWS_SES", null,
"RSA_1024_BIT", "us-east-1");
verify(identities).setDkimAttributes("example.com", false, "us-east-1");
verify(identities).setMailFromDomain("example.com", "mail.example.com",
"RejectMessage", "us-east-1");
verify(identities).setFeedbackForwardingEnabled("example.com", false, "us-east-1");
String arn = "arn:aws:ses:us-east-1:000000000000:identity/example.com";
verify(ses).untagResource(arn, "us-east-1", List.of("removed"));
verify(ses).tagResource(arn, "us-east-1", List.of(new Tag("changed", "new")));
}
@Test
void failedRollbackKeepsTheOriginalSnapshotForAnotherFailedUpdate() throws Exception {
Identity identity = domain("example.com");
identity.setTags(List.of(new Tag("purpose", "original")));
when(identities.getIdentityVerificationAttributes("example.com", "us-east-1")).thenReturn(identity);
when(ses.listResourceTags(anyString(), eq("us-east-1"))).thenReturn(identity.getTags());
StackResource resource = resource();
resource.setPhysicalId("example.com");
String managedTags = "[{\"Key\":\"purpose\",\"Value\":\"original\"}]";
resource.getAttributes().put("__FlociSesManagedTags", managedTags);
JsonNode firstAttempt = props("""
{"EmailIdentity":"example.com",
"FeedbackAttributes":{"EmailForwardingEnabled":false},
"Tags":[{"Key":"purpose","Value":"first-attempt"}]}
""");
provisioner.provision(resource, firstAttempt, context("example.com"));
identity.setFeedbackForwardingEnabled(false);
identity.setTags(List.of(new Tag("purpose", "first-attempt")));
doThrow(new AwsException("ServiceUnavailableException", "temporary restore failure", 503))
.doAnswer(call -> {
Identity restoredIdentity = call.getArgument(0);
identity.setFeedbackForwardingEnabled(restoredIdentity.isFeedbackForwardingEnabled());
identity.setTags(restoredIdentity.getTags());
identity.setDkimTokens(restoredIdentity.getDkimTokens());
return null;
}).when(identities).save(any(Identity.class), eq("us-east-1"));
assertThrows(IllegalStateException.class, () -> provisioner.rollbackUpdate(resource));
assertTrue(provisioner.retainsFailedUpdateState(resource));
provisioner.provision(resource, props("""
{"EmailIdentity":"example.com",
"FeedbackAttributes":{"EmailForwardingEnabled":false},
"Tags":[{"Key":"purpose","Value":"second-attempt"}]}
"""), context("example.com"));
assertTrue(provisioner.rollbackUpdate(resource));
ArgumentCaptor<Identity> restored = ArgumentCaptor.forClass(Identity.class);
verify(identities, times(3)).save(restored.capture(), eq("us-east-1"));
Identity recovered = restored.getValue();
assertTrue(recovered.isFeedbackForwardingEnabled());
assertEquals(List.of(new Tag("purpose", "original")), recovered.getTags());
assertEquals(List.of("token1", "token2", "token3"), recovered.getDkimTokens());
assertEquals("token1.dkim.amazonses.com", resource.getAttributes().get("DkimDNSTokenValue1"));
assertEquals(managedTags, resource.getAttributes().get("__FlociSesManagedTags"));
assertFalse(provisioner.retainsFailedUpdateState(resource));
}
@Test
void failedSnapshotRestoreRejectsReplacementBeforeCreatingAnotherIdentity() throws Exception {
Identity identity = domain("example.com");
when(identities.getIdentityVerificationAttributes("example.com", "us-east-1")).thenReturn(identity);
when(ses.listResourceTags(anyString(), eq("us-east-1"))).thenReturn(List.of());
StackResource resource = resource();
resource.setPhysicalId("example.com");
provisioner.provision(resource, props("""
{"EmailIdentity":"example.com",
"FeedbackAttributes":{"EmailForwardingEnabled":false}}
"""), context("example.com"));
doThrow(new AwsException("ServiceUnavailableException", "restore unavailable", 503))
.when(identities).save(any(Identity.class), eq("us-east-1"));
assertThrows(IllegalStateException.class, () -> provisioner.rollbackUpdate(resource));
assertThrows(IllegalStateException.class, () -> provisioner.provision(resource,
props("{\"EmailIdentity\":\"other.example.com\"}"), context("example.com")));
verify(ses, never()).createEmailIdentity(anyString(), any(), any(), anyString());
verify(ses, never()).deleteIdentity(anyString(), anyString());
assertEquals("example.com", resource.getPhysicalId());
assertTrue(provisioner.retainsFailedUpdateState(resource));
}
@Test
void committedCleanupCannotDiscardAnUnrestoredIdentitySnapshot() throws Exception {
Identity identity = domain("example.com");
when(identities.getIdentityVerificationAttributes("example.com", "us-east-1")).thenReturn(identity);
when(ses.listResourceTags(anyString(), eq("us-east-1"))).thenReturn(List.of());
StackResource resource = resource();
resource.setPhysicalId("example.com");
provisioner.provision(resource, props("""
{"EmailIdentity":"example.com",
"FeedbackAttributes":{"EmailForwardingEnabled":false}}
"""), context("example.com"));
doThrow(new AwsException("ServiceUnavailableException", "restore unavailable", 503))
.when(identities).save(any(Identity.class), eq("us-east-1"));
assertThrows(IllegalStateException.class, () -> provisioner.rollbackUpdate(resource));
resource.setStatus("UPDATE_FAILED");
assertThrows(AwsException.class, () -> provisioner.completeUpdate(resource));
assertTrue(provisioner.retainsFailedUpdateState(resource));
verify(ses, never()).deleteIdentity(anyString(), anyString());
}
@Test
void deletingAfterFailedRollbackKeepsTheSnapshotUntilTheIdentityIsDeleted() throws Exception {
Identity identity = domain("example.com");
when(identities.getIdentityVerificationAttributes("example.com", "us-east-1")).thenReturn(identity);
when(ses.listResourceTags(anyString(), eq("us-east-1"))).thenReturn(List.of());
StackResource resource = resource();
resource.setPhysicalId("example.com");
provisioner.provision(resource, props("""
{"EmailIdentity":"example.com",
"FeedbackAttributes":{"EmailForwardingEnabled":false}}
"""), context("example.com"));
doThrow(new AwsException("ServiceUnavailableException", "restore unavailable", 503))
.when(identities).save(any(Identity.class), eq("us-east-1"));
assertThrows(IllegalStateException.class, () -> provisioner.rollbackUpdate(resource));
resource.setStatus("UPDATE_FAILED");
assertFalse(provisioner.completeDeleteCleanup(resource).applicable());
provisioner.clearDeleteCleanup(resource);
assertTrue(provisioner.retainsFailedUpdateState(resource));
doThrow(new AwsException("ServiceUnavailableException", "temporary delete failure", 503))
.doNothing().when(ses).deleteIdentity("example.com", "us-east-1");
assertThrows(AwsException.class, () -> provisioner.delete(resource, "us-east-1"));
assertTrue(provisioner.retainsFailedUpdateState(resource));
provisioner.delete(resource, "us-east-1");
assertFalse(provisioner.retainsFailedUpdateState(resource));
verify(ses, times(2)).deleteIdentity("example.com", "us-east-1");
}
@Test
void restoredReplacementUsesTheRestoredPhysicalIdForTheNextUpdate() throws Exception {
Identity original = domain("example.com");
Identity replacement = domain("other.example.com");
when(ses.createEmailIdentity(eq("other.example.com"), isNull(), eq(List.of()), eq("us-east-1")))
.thenReturn(replacement);
when(identities.getIdentityVerificationAttributes("example.com", "us-east-1")).thenReturn(original);
when(identities.getIdentityVerificationAttributes("other.example.com", "us-east-1"))
.thenReturn(replacement);
when(ses.listResourceTags(anyString(), eq("us-east-1"))).thenReturn(List.of());
StackResource resource = resource();
resource.setPhysicalId("example.com");
provisioner.provision(resource, props("{\"EmailIdentity\":\"other.example.com\"}"),
context("example.com"));
provisioner.provision(resource, props("{\"EmailIdentity\":\"example.com\"}"),
context("other.example.com"));
verify(ses).deleteIdentity("other.example.com", "us-east-1");
verify(ses, never()).createEmailIdentity(eq("example.com"), any(), any(), anyString());
assertEquals("example.com", resource.getPhysicalId());
assertEquals("token1._domainkey.example.com", resource.getAttributes().get("DkimDNSTokenName1"));
}
@Test
void changedIdentityCreatesReplacementWithoutDeletingPriorEntity() throws Exception {
Identity identity = domain("other.example.com");
when(ses.createEmailIdentity(eq("other.example.com"), isNull(), eq(List.of()), eq("us-east-1")))
.thenReturn(identity);
when(identities.getIdentityVerificationAttributes("other.example.com", "us-east-1"))
.thenReturn(identity);
when(ses.listResourceTags(anyString(), eq("us-east-1"))).thenReturn(List.of());
StackResource resource = resource();
String previousTags = "[{\"Key\":\"previous\",\"Value\":\"old\"}]";
resource.getAttributes().put("__FlociSesManagedTags", previousTags);
provisioner.provision(resource, props("{\"EmailIdentity\":\"other.example.com\"}"),
context("example.com"));
assertEquals("other.example.com", resource.getPhysicalId());
assertTrue(provisioner.retainsFailedUpdateState(resource));
verify(ses, never()).deleteIdentity("example.com", "us-east-1");
assertTrue(provisioner.rollbackUpdate(resource));
assertEquals("example.com", resource.getPhysicalId());
assertEquals(previousTags, resource.getAttributes().get("__FlociSesManagedTags"));
verify(ses).deleteIdentity("other.example.com", "us-east-1");
}
@Test
void unsupportedByodkimFailsBeforeCreatingAnIdentity() throws Exception {
JsonNode props = props("""
{"EmailIdentity":"example.com",
"DkimSigningAttributes":{"DomainSigningSelector":"selector",
"DomainSigningPrivateKey":"secret"}}
""");
AwsException failure = assertThrows(AwsException.class,
() -> provisioner.provision(resource(), props, context(null)));
assertEquals("ValidationError", failure.getErrorCode());
assertTrue(failure.getMessage().contains("BYODKIM"));
verify(ses, never()).createEmailIdentity(anyString(), any(), any(), anyString());
}
@Test
void failedPostCreateConfigurationRemovesTheNewIdentity() throws Exception {
Identity identity = domain("example.com");
when(ses.createEmailIdentity(eq("example.com"), isNull(), eq(List.of()), eq("us-east-1")))
.thenReturn(identity);
doThrow(new AwsException("BadRequestException", "MAIL FROM rejected", 400))
.when(identities).setMailFromDomain("example.com", "mail.example.com",
"UseDefaultValue", "us-east-1");
JsonNode props = props("""
{"EmailIdentity":"example.com",
"MailFromAttributes":{"MailFromDomain":"mail.example.com"}}
""");
assertThrows(AwsException.class, () -> provisioner.provision(resource(), props, context(null)));
verify(ses).deleteIdentity("example.com", "us-east-1");
}
@Test
void failedPostCreateReadRemovesTheNewIdentity() throws Exception {
Identity identity = domain("example.com");
when(ses.createEmailIdentity(eq("example.com"), isNull(), eq(List.of()), eq("us-east-1")))
.thenReturn(identity);
when(ses.listResourceTags(anyString(), eq("us-east-1"))).thenReturn(List.of());
when(identities.getIdentityVerificationAttributes("example.com", "us-east-1"))
.thenThrow(new AwsException("ServiceUnavailableException", "temporary", 503));
StackResource resource = resource();
assertThrows(AwsException.class, () -> provisioner.provision(resource,
props("{\"EmailIdentity\":\"example.com\"}"), context(null)));
verify(ses).deleteIdentity("example.com", "us-east-1");
assertNull(resource.getPhysicalId());
assertTrue(resource.getAttributes().isEmpty());
}
@Test
void failedReplacementReadRestoresPriorResourceMetadata() throws Exception {
Identity identity = domain("other.example.com");
when(ses.createEmailIdentity(eq("other.example.com"), isNull(), eq(List.of()), eq("us-east-1")))
.thenReturn(identity);
when(ses.listResourceTags(anyString(), eq("us-east-1"))).thenReturn(List.of());
when(identities.getIdentityVerificationAttributes("other.example.com", "us-east-1"))
.thenThrow(new AwsException("ServiceUnavailableException", "temporary", 503));
StackResource resource = resource();
resource.setPhysicalId("example.com");
resource.getAttributes().put("__FlociSesManagedTags", "old");
assertThrows(AwsException.class, () -> provisioner.provision(resource,
props("{\"EmailIdentity\":\"other.example.com\"}"), context("example.com")));
verify(ses).deleteIdentity("other.example.com", "us-east-1");
assertEquals("example.com", resource.getPhysicalId());
assertEquals(Map.of("__FlociSesManagedTags", "old"), resource.getAttributes());
}
@Test
void failedReplacementCleanupDoesNotRetainUnmutatedUpdateState() throws Exception {
Identity identity = domain("other.example.com");
when(ses.createEmailIdentity(eq("other.example.com"), isNull(), eq(List.of()), eq("us-east-1")))
.thenReturn(identity);
when(ses.listResourceTags(anyString(), eq("us-east-1"))).thenReturn(List.of());
when(identities.getIdentityVerificationAttributes("other.example.com", "us-east-1"))
.thenThrow(new AwsException("ServiceUnavailableException", "temporary read failure", 503));
doThrow(new AwsException("ServiceUnavailableException", "temporary delete failure", 503))
.doNothing().when(ses).deleteIdentity("other.example.com", "us-east-1");
StackResource resource = resource();
resource.setPhysicalId("example.com");
resource.getAttributes().put("DkimDNSTokenName1", "prior-token._domainkey.example.com");
AwsException failure = assertThrows(AwsException.class, () -> provisioner.provision(resource,
props("{\"EmailIdentity\":\"other.example.com\"}"), context("example.com")));
assertEquals(1, failure.getSuppressed().length);
assertFalse(provisioner.retainsFailedUpdateState(resource));
assertEquals("example.com", resource.getPhysicalId());
assertEquals("prior-token._domainkey.example.com", resource.getAttributes().get("DkimDNSTokenName1"));
provisioner.delete(resource, "us-east-1");
verify(ses, times(2)).deleteIdentity("other.example.com", "us-east-1");
verify(ses).deleteIdentity("example.com", "us-east-1");
}
@Test
void failedPostCreateCleanupRetainsIdentityForStackRollback() throws Exception {
Identity identity = domain("example.com");
when(ses.createEmailIdentity(eq("example.com"), isNull(), eq(List.of()), eq("us-east-1")))
.thenReturn(identity);
doThrow(new AwsException("BadRequestException", "MAIL FROM rejected", 400))
.when(identities).setMailFromDomain("example.com", "mail.example.com",
"UseDefaultValue", "us-east-1");
doThrow(new AwsException("ServiceUnavailableException", "temporary", 503))
.doNothing().when(ses).deleteIdentity("example.com", "us-east-1");
StackResource resource = resource();
JsonNode props = props("""
{"EmailIdentity":"example.com",
"MailFromAttributes":{"MailFromDomain":"mail.example.com"}}
""");
AwsException failure = assertThrows(AwsException.class,
() -> provisioner.provision(resource, props, context(null)));
assertEquals(1, failure.getSuppressed().length);
assertEquals("example.com", resource.getPhysicalId());
assertEquals("true", resource.getAttributes().get(CfnRollback.ROLLBACK_OWNED_ATTR));
provisioner.delete(resource, "us-east-1");
verify(ses, times(2)).deleteIdentity("example.com", "us-east-1");
}
@Test
void malformedOptionFailsBeforeCreatingAnIdentity() throws Exception {
JsonNode props = props("""
{"EmailIdentity":"example.com",
"FeedbackAttributes":{"EmailForwardingEnabled":"false"}}
""");
assertThrows(AwsException.class, () -> provisioner.provision(resource(), props, context(null)));
verify(ses, never()).createEmailIdentity(anyString(), any(), any(), anyString());
}
@Test
void deleteDelegatesToSes() {
provisioner.delete("AWS::SES::EmailIdentity", "example.com", "us-east-1");
verify(ses).deleteIdentity("example.com", "us-east-1");
}
@Test
void onlyFailedUpdatesWithPendingSnapshotsRequireRecovery() {
StackResource resource = resource();
resource.setStatus("UPDATE_FAILED");
assertFalse(provisioner.requiresUpdateRecovery(resource));
resource.getAttributes().put("__FlociSesUpdateSnapshot", "{\"managedTags\":\"[]\"}");
assertTrue(provisioner.requiresUpdateRecovery(resource));
resource.setStatus("UPDATE_COMPLETE");
assertFalse(provisioner.requiresUpdateRecovery(resource));
}
@Test
void recoveredReplacementFailureRetainsAttemptedResourceForRollbackWalker() {
StackResource resource = resource();
resource.getAttributes().put(CfnRollback.UPDATE_ROLLBACK_RESTORED_ATTR, "true");
assertTrue(provisioner.retainsFailedUpdateState(resource));
resource.getAttributes().remove(CfnRollback.UPDATE_ROLLBACK_RESTORED_ATTR);
assertFalse(provisioner.retainsFailedUpdateState(resource));
}
@Test
void failedDisplacedIdentityCleanupDoesNotReportStackDeleteSuccess() throws Exception {
Identity replacement = domain("other.example.com");
when(ses.createEmailIdentity(eq("other.example.com"), isNull(), eq(List.of()), eq("us-east-1")))
.thenReturn(replacement);
when(identities.getIdentityVerificationAttributes("other.example.com", "us-east-1"))
.thenReturn(replacement);
when(ses.listResourceTags(anyString(), eq("us-east-1"))).thenReturn(List.of());
StackResource resource = resource();
provisioner.provision(resource, props("{\"EmailIdentity\":\"other.example.com\"}"),
context("example.com"));
doThrow(new AwsException("ServiceUnavailableException", "temporary", 503))
.when(ses).deleteIdentity("example.com", "us-east-1");
assertThrows(AwsException.class, () -> provisioner.delete(resource, "us-east-1"));
verify(ses, never()).deleteIdentity("other.example.com", "us-east-1");
}
@Test
void exhaustedStackDeleteAbandonsEveryExhaustedIdentity() throws Exception {
StackResource resource = resource();
resource.setPhysicalId("current.example.com");
resource.setUpdateReplacePolicy("Retain");
resource.getAttributes().put("__FlociSesUpdateSnapshot", "{\"managedTags\":\"[]\"}");
ReplacementCleanup.recordOrphan(resource, "orphan-one.example.com", resource.getResourceType(), "us-east-1");
ReplacementCleanup.recordOrphan(resource, "orphan-two.example.com", resource.getResourceType(), "us-east-1");
ReplacementCleanup.recordOrphan(resource, resource.getPhysicalId(), resource.getResourceType(), "us-east-1");
doThrow(new AwsException("ServiceUnavailableException", "delete unavailable", 503))
.when(ses).deleteIdentity("orphan-one.example.com", "us-east-1");
doThrow(new AwsException("ServiceUnavailableException", "delete unavailable", 503))
.when(ses).deleteIdentity("orphan-two.example.com", "us-east-1");
for (int attempt = 1; attempt <= 3; attempt++) {
UpdateCleanupResult failed = provisioner.completeDeleteCleanup(resource);
assertFalse(failed.complete());
assertEquals(attempt, failed.attempts());
}
provisioner.clearDeleteCleanup(resource);
assertFalse(provisioner.hasReplacementUpdate(resource));
assertNull(provisioner.updateCleanupPhysicalId(resource));
assertFalse(provisioner.completeDeleteCleanup(resource).applicable());
assertTrue(provisioner.retainsFailedUpdateState(resource));
provisioner.delete(resource, "us-east-1");
assertFalse(provisioner.hasReplacementUpdate(resource));
assertFalse(provisioner.retainsFailedUpdateState(resource));
verify(ses, times(3)).deleteIdentity("orphan-one.example.com", "us-east-1");
verify(ses, times(3)).deleteIdentity("orphan-two.example.com", "us-east-1");
verify(ses, times(1)).deleteIdentity("current.example.com", "us-east-1");
}
@Test
void stackDeleteDoesNotRetryAnAlreadyExhaustedIdentity() throws Exception {
StackResource resource = resource();
resource.setPhysicalId("current.example.com");
ReplacementCleanup.recordOrphan(resource, "reused.example.com", resource.getResourceType(), "us-east-1");
for (int attempt = 0; attempt < 3; attempt++) {
ReplacementCleanup.complete(resource, (type, id, region) -> {
throw new IllegalStateException("historical delete unavailable");
});
}
UpdateCleanupResult exhausted = provisioner.completeDeleteCleanup(resource);
assertFalse(exhausted.complete());
assertEquals(3, exhausted.attempts());
assertThrows(AwsException.class, () -> provisioner.delete(resource, "us-east-1"));
verify(ses, never()).deleteIdentity("reused.example.com", "us-east-1");
verify(ses, never()).deleteIdentity("current.example.com", "us-east-1");
provisioner.clearDeleteCleanup(resource);
provisioner.delete(resource, "us-east-1");
verify(ses, never()).deleteIdentity("reused.example.com", "us-east-1");
verify(ses).deleteIdentity("current.example.com", "us-east-1");
}
@Test
void stackDeleteKeepsCleanupEntriesWithAttemptsLeft() throws Exception {
StackResource resource = resource();
resource.setPhysicalId("current.example.com");
resource.setUpdateReplacePolicy("Retain");
resource.getAttributes().put("__FlociSesUpdateSnapshot", "{\"managedTags\":\"[]\"}");
ReplacementCleanup.recordOrphan(resource, "exhausted.example.com", resource.getResourceType(), "us-east-1");
for (int attempt = 0; attempt < 3; attempt++) {
ReplacementCleanup.complete(resource, (type, id, region) -> {
throw new IllegalStateException("historical delete unavailable");
});
}
ReplacementCleanup.recordOrphan(resource, "pending.example.com", resource.getResourceType(), "us-east-1");
provisioner.clearDeleteCleanup(resource);
assertTrue(provisioner.hasReplacementUpdate(resource));
assertEquals("pending.example.com", provisioner.updateCleanupPhysicalId(resource));
assertTrue(provisioner.retainsFailedUpdateState(resource));
assertTrue(provisioner.completeDeleteCleanup(resource).complete());
verify(ses).deleteIdentity("pending.example.com", "us-east-1");
verify(ses, never()).deleteIdentity("exhausted.example.com", "us-east-1");
provisioner.delete(resource, "us-east-1");
verify(ses).deleteIdentity("current.example.com", "us-east-1");
assertFalse(provisioner.retainsFailedUpdateState(resource));
}
@Test
void stackDeleteKeepsRetainedReplacementAndDeletesTheCurrentIdentity() throws Exception {
Identity replacement = domain("other.example.com");
when(ses.createEmailIdentity(eq("other.example.com"), isNull(), eq(List.of()), eq("us-east-1")))
.thenReturn(replacement);
when(identities.getIdentityVerificationAttributes("other.example.com", "us-east-1"))
.thenReturn(replacement);
when(ses.listResourceTags(anyString(), eq("us-east-1"))).thenReturn(List.of());
StackResource resource = resource();
resource.setUpdateReplacePolicy("Retain");
provisioner.provision(resource, props("{\"EmailIdentity\":\"other.example.com\"}"),
context("example.com"));
assertTrue(provisioner.completeDeleteCleanup(resource).complete());
provisioner.clearDeleteCleanup(resource);
provisioner.delete(resource, "us-east-1");
verify(ses, never()).deleteIdentity("example.com", "us-east-1");
verify(ses).deleteIdentity("other.example.com", "us-east-1");
assertFalse(provisioner.hasReplacementUpdate(resource));
}
private Identity domain(String name) {
Identity identity = new Identity(name, "Domain");
identity.setDkimEnabled(true);
identity.setDkimTokens(List.of("token1", "token2", "token3"));
return identity;
}
private StackResource resource() {
StackResource resource = new StackResource();
resource.setLogicalId("Identity");
resource.setResourceType("AWS::SES::EmailIdentity");
resource.setAttributes(new HashMap<>());
return resource;
}
private ProvisionContext context(String priorPhysicalId) {
CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class);
when(engine.resolveNode(any())).thenAnswer(call -> call.getArgument(0));
when(engine.resolveNodeOmittingNoValue(any())).thenAnswer(call -> call.getArgument(0));
return new ProvisionContext(engine, "us-east-1", "000000000000", "ses-stack", priorPhysicalId);
}
private JsonNode props(String json) throws Exception {
return mapper.readTree(json);
}
}
@@ -160,6 +160,7 @@ AWS::Route53::HostedZone Route53CfnProvisioner
AWS::Route53::RecordSet Route53CfnProvisioner
AWS::S3::Bucket S3CfnProvisioner
AWS::S3::BucketPolicy S3CfnProvisioner
AWS::SES::EmailIdentity SesCfnProvisioner
AWS::SNS::Subscription SnsCfnProvisioner
AWS::SNS::Topic SnsCfnProvisioner
AWS::SNS::TopicPolicy SnsCfnProvisioner
1 AWS::ApiGateway::Account ApiGatewayAccountCfnProvisioner
160 AWS::Route53::RecordSet Route53CfnProvisioner
161 AWS::S3::Bucket S3CfnProvisioner
162 AWS::S3::BucketPolicy S3CfnProvisioner
163 AWS::SES::EmailIdentity SesCfnProvisioner
164 AWS::SNS::Subscription SnsCfnProvisioner
165 AWS::SNS::Topic SnsCfnProvisioner
166 AWS::SNS::TopicPolicy SnsCfnProvisioner
+3
View File
@@ -13,6 +13,7 @@ order:
- AWS::S3
- AWS::SQS
- AWS::SNS
- AWS::SES
- AWS::DynamoDB
- AWS::Lambda
- AWS::IAM
@@ -157,6 +158,7 @@ service_labels:
AWS::Route53: Route 53
AWS::S3: S3
AWS::SNS: SNS
AWS::SES: SES
AWS::SQS: SQS
AWS::SSM: SSM
AWS::Scheduler: EventBridge Scheduler
@@ -196,6 +198,7 @@ notes:
Custom::DynamoDBReplica: applied natively against DynamoDB, not via a provider Lambda
AWS::DynamoDB::Table: '`TimeToLiveSpecification` applied on create and update; to rename an enabled TTL attribute, disable TTL in one update and enable the new attribute in a later one'
AWS::Cognito::UserPool: '`ProviderURL` is the local issuer of the tokens Floci mints, `<base-url>/<pool id>`'
AWS::SES::EmailIdentity: 'BYODKIM is not supported by this resource; Easy DKIM DNS attributes use the shared emulator signing zone `dkim.amazonses.com` rather than AWS''s region- and identity-specific zone assignment'
AWS::Events::Archive: '`KmsKeyIdentifier` is ignored'
AWS::CloudFront::Function: '`KeyValueStoreAssociations` is ignored'
AWS::GuardDuty::MalwareProtectionPlan: 'management plane only: no scan runs, so the plan stays ACTIVE; Ref returns the plan id; a BucketName change is rejected'