Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

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

XuJian7f8383db38e7
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationSesEmailIdentityTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationSesEmailIdentityTest.java
new file mode 100644
index 0000000000..073692fd84
--- /dev/null
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationSesEmailIdentityTest.java
@@ -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;
+ }
+}
diff --git a/docs/services/cloudformation.md b/docs/services/cloudformation.md
index d54e2c4dcb..dfbdbac872 100644
--- a/docs/services/cloudformation.md
+++ b/docs/services/cloudformation.md
@@ -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`
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java
index 1c87a5e2a4..265b21768f 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java
@@ -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");
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnResourceDispatcher.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnResourceDispatcher.java
index e00e14783e..4515834de3 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnResourceDispatcher.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnResourceDispatcher.java
@@ -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())
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnResourceProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnResourceProvisioner.java
index af6a6b6617..da2f64235d 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnResourceProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnResourceProvisioner.java
@@ -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
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SesCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SesCfnProvisioner.java
new file mode 100644
index 0000000000..64e4995db5
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SesCfnProvisioner.java
@@ -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) {}
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesIdentityService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesIdentityService.java
index f11cdf4538..67b00319c6 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ses/SesIdentityService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesIdentityService.java
@@ -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);
diff --git a/src/main/resources/cloudformation/create-only-properties.json b/src/main/resources/cloudformation/create-only-properties.json
index ab4552a746..865bf573df 100644
--- a/src/main/resources/cloudformation/create-only-properties.json
+++ b/src/main/resources/cloudformation/create-only-properties.json
@@ -705,6 +705,9 @@
"AWS::Scheduler::ScheduleGroup": [
"Name"
],
+ "AWS::SES::EmailIdentity": [
+ "EmailIdentity"
+ ],
"AWS::SecretsManager::Secret": [
"Name"
],
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.java
index 8a967cf751..41ef771bc7 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.java
@@ -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;
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnSchemaCoverageTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnSchemaCoverageTest.java
index 101646132c..2a515c1404 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnSchemaCoverageTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnSchemaCoverageTest.java
@@ -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<>();
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationSesEmailIdentityIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationSesEmailIdentityIntegrationTest.java
new file mode 100644
index 0000000000..0b1906063b
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationSesEmailIdentityIntegrationTest.java
@@ -0,0 +1,1228 @@
+package io.github.hectorvent.floci.services.cloudformation;
+
+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.AwsException;
+import io.github.hectorvent.floci.core.common.XmlParser;
+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.testing.MutableClock;
+import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
+import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.mockito.InjectSpy;
+import io.restassured.response.Response;
+import io.restassured.specification.RequestSpecification;
+import jakarta.inject.Inject;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import java.time.Duration;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import static io.restassured.RestAssured.given;
+import static org.awaitility.Awaitility.await;
+import static org.hamcrest.Matchers.anyOf;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.clearInvocations;
+import static org.mockito.Mockito.doCallRealMethod;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+@QuarkusTest
+class CloudFormationSesEmailIdentityIntegrationTest {
+
+ private static final ObjectMapper MAPPER = new ObjectMapper();
+ private static final String CFN_AUTH =
+ "AWS4-HMAC-SHA256 Credential=test/20261001/us-east-1/cloudformation/aws4_request";
+ private static final String SES_AUTH =
+ "AWS4-HMAC-SHA256 Credential=test/20261001/us-east-1/ses/aws4_request";
+ private String stack;
+ private String stackAuth = CFN_AUTH;
+
+ @InjectSpy
+ SesService sesService;
+
+ @InjectSpy
+ SesIdentityService identityService;
+
+ @Inject
+ MutableClock clock;
+
+ @BeforeAll
+ static void configureContentTypes() {
+ RestAssuredJsonUtils.configureAwsContentTypes();
+ }
+
+ @BeforeEach
+ void resetClock() {
+ clock.reset();
+ }
+
+ @AfterEach
+ void cleanUp() {
+ try {
+ if (stack != null) {
+ cfn("DeleteStack", null).then().statusCode(200);
+ await().atMost(Duration.ofSeconds(15)).untilAsserted(() ->
+ assertEquals(400, cfn("DescribeStacks", null).statusCode()));
+ }
+ } finally {
+ clock.reset();
+ }
+ }
+
+ @Test
+ void domainIdentityExposesRealDkimDnsAttributesAndReconcilesUpdates() throws Exception {
+ String first = "ses-" + Long.toString(System.nanoTime(), 36) + ".example.com";
+ stack = "cfn-ses-" + Long.toString(System.nanoTime(), 36);
+ cfn("CreateStack", template(first, false)).then().statusCode(200);
+ String created = awaitStatus("CREATE_COMPLETE");
+ Map<String, String> outputs = XmlParser.extractPairs(created, "Outputs", "OutputKey", "OutputValue");
+ assertEquals(first, outputs.get("IdentityRef"));
+ Response identity = sesIdentity(first);
+ assertEquals(200, identity.statusCode(), identity.asString());
+ List<String> tokens = identity.jsonPath().getList("DkimAttributes.Tokens", String.class);
+ assertEquals(3, tokens.size());
+ for (int index = 1; index <= 3; index++) {
+ String token = tokens.get(index - 1);
+ assertEquals(token + "._domainkey." + first, outputs.get("DkimName" + index));
+ assertEquals(token + ".dkim.amazonses.com", outputs.get("DkimValue" + index));
+ }
+
+ cfn("UpdateStack", template(first, true)).then().statusCode(200);
+ assertEquals(first, XmlParser.extractPairs(awaitStatus("UPDATE_COMPLETE"),
+ "Outputs", "OutputKey", "OutputValue").get("IdentityRef"));
+ identity = sesIdentity(first);
+ assertEquals(false, identity.jsonPath().getBoolean("FeedbackForwardingStatus"));
+ assertEquals(false, identity.jsonPath().getBoolean("DkimAttributes.SigningEnabled"));
+ assertEquals("mail." + first, identity.jsonPath().getString("MailFromAttributes.MailFromDomain"));
+ assertEquals("REJECT_MESSAGE", identity.jsonPath().getString("MailFromAttributes.BehaviorOnMxFailure"));
+ assertEquals("new", identity.jsonPath().getString("Tags.find { it.Key == 'purpose' }.Value"));
+ assertEquals("RSA_1024_BIT", identity.jsonPath().getString("DkimAttributes.NextSigningKeyLength"));
+
+ ObjectNode invalid = (ObjectNode) MAPPER.readTree(template(first, true));
+ invalid.withObject("/Resources/Identity/Properties/DkimSigningAttributes")
+ .put("NextSigningKeyLength", "INVALID");
+ cfn("UpdateStack", invalid.toString()).then().statusCode(200);
+ awaitStatus("UPDATE_ROLLBACK_COMPLETE");
+ identity = sesIdentity(first);
+ assertEquals("RSA_1024_BIT", identity.jsonPath().getString("DkimAttributes.NextSigningKeyLength"));
+ assertEquals(false, identity.jsonPath().getBoolean("DkimAttributes.SigningEnabled"));
+ assertEquals("new", identity.jsonPath().getString("Tags.find { it.Key == 'purpose' }.Value"));
+
+ cfn("DeleteStack", null).then().statusCode(200);
+ await().atMost(Duration.ofSeconds(15)).untilAsserted(() ->
+ assertEquals(404, sesIdentity(first).statusCode()));
+ stack = null;
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ void selectedTagDynamicReferenceDoesNotExpandItsResultBeforeSesValidation(boolean updating) throws Exception {
+ String identityName = "ses-" + Long.toString(System.nanoTime(), 36) + ".example.com";
+ stack = "cfn-ses-" + Long.toString(System.nanoTime(), 36);
+ String source = "/" + stack + "/source";
+ String nested = "/" + stack + "/literal";
+ String literal = "{{resolve:ssm:" + nested + "}}";
+ try {
+ for (Map.Entry<String, String> parameter : Map.of(source, literal, nested, "expanded-again").entrySet()) {
+ given().header("Authorization", auth("000000000000", "ssm"))
+ .contentType("application/x-amz-json-1.1").header("X-Amz-Target", "AmazonSSM.PutParameter")
+ .body(Map.of("Name", parameter.getKey(), "Value", parameter.getValue(), "Type", "String"))
+ .post("/").then().statusCode(200);
+ }
+ if (updating) {
+ cfn("CreateStack", template(identityName, false)).then().statusCode(200);
+ awaitStatus("CREATE_COMPLETE");
+ }
+ ObjectNode template = (ObjectNode) MAPPER.readTree(template(identityName, false));
+ template.withObject("/Conditions").set("IncludeTag",
+ MAPPER.valueToTree(Map.of("Fn::Equals", List.of("included", "included"))));
+ template.withObject("/Resources/Identity/Properties").set("Tags", MAPPER.valueToTree(List.of(Map.of(
+ "Key", "chain",
+ "Value", Map.of("Fn::If", List.of("IncludeTag", "{{resolve:ssm:" + source + "}}",
+ Map.of("Ref", "AWS::NoValue")))))));
+ cfn(updating ? "UpdateStack" : "CreateStack", template.toString()).then().statusCode(200);
+ awaitStatus(updating ? "UPDATE_ROLLBACK_COMPLETE" : "ROLLBACK_COMPLETE");
+ String events = cfn("DescribeStackEvents", null).then().statusCode(200).extract().asString();
+ assertTrue(events.contains("Tags can only contain"), events);
+ if (updating) {
+ assertEquals("old", sesIdentity(identityName).jsonPath().getString(
+ "Tags.find { it.Key == 'purpose' }.Value"));
+ assertEquals(null, sesIdentity(identityName).jsonPath().getString(
+ "Tags.find { it.Key == 'chain' }.Value"));
+ } else {
+ assertEquals(404, sesIdentity(identityName).statusCode());
+ }
+ } finally {
+ given().header("Authorization", auth("000000000000", "ssm"))
+ .contentType("application/x-amz-json-1.1").header("X-Amz-Target", "AmazonSSM.DeleteParameters")
+ .body(Map.of("Names", List.of(source, nested))).post("/").then().statusCode(200);
+ }
+ }
+
+ @Test
+ void emailAddressIdentityHasNoDomainDkimRecordsAndDeletesWithStack() throws Exception {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String address = "ses-" + suffix + "@unregistered-" + suffix + ".example.com";
+ stack = "cfn-ses-" + suffix;
+ cfn("CreateStack", template(address, false)).then().statusCode(200);
+ String created = awaitStatus("CREATE_COMPLETE");
+ Map<String, String> outputs = XmlParser.extractPairs(created, "Outputs", "OutputKey", "OutputValue");
+ assertEquals(address, outputs.get("IdentityRef"));
+ for (int index = 1; index <= 3; index++) {
+ assertEquals("", outputs.get("DkimName" + index));
+ assertEquals("", outputs.get("DkimValue" + index));
+ }
+
+ Response identity = sesIdentity(address);
+ assertEquals(200, identity.statusCode(), identity.asString());
+ assertEquals("EMAIL_ADDRESS", identity.jsonPath().getString("IdentityType"));
+ assertEquals(List.of(), identity.jsonPath().getList("DkimAttributes.Tokens", String.class));
+ assertEquals(null, identity.jsonPath().getString("DkimAttributes.SigningHostedZone"));
+
+ cfn("DeleteStack", null).then().statusCode(200);
+ await().atMost(Duration.ofSeconds(15)).untilAsserted(() ->
+ assertEquals(404, sesIdentity(address).statusCode()));
+ stack = null;
+ }
+
+ @Test
+ void domainSigningHostedZoneDrivesGetAttInheritanceRotationAndDnsDetection() throws Exception {
+ String domain = "ses-" + Long.toString(System.nanoTime(), 36) + ".example.com";
+ String signingZone = SesIdentityService.DKIM_SIGNING_HOSTED_ZONE;
+ String email = "user@" + domain;
+ stack = "cfn-ses-" + Long.toString(System.nanoTime(), 36);
+ createIdentityStack(domain);
+ List<String> originalTokens = sesIdentity(domain).jsonPath().getList("DkimAttributes.Tokens", String.class);
+ cfn("UpdateStack", template(domain, true)).then().statusCode(200);
+ String updated = awaitStatus("UPDATE_COMPLETE");
+ Response identity = sesIdentity(domain);
+ assertEquals(signingZone, identity.jsonPath().getString("DkimAttributes.SigningHostedZone"));
+ List<String> tokens = identity.jsonPath().getList("DkimAttributes.Tokens", String.class);
+ assertNotEquals(originalTokens, tokens);
+ Map<String, String> outputs = XmlParser.extractPairs(updated, "Outputs", "OutputKey", "OutputValue");
+ StringBuilder changes = new StringBuilder();
+ for (int index = 1; index <= 3; index++) {
+ String token = tokens.get(index - 1);
+ assertEquals(token + "._domainkey." + domain, outputs.get("DkimName" + index));
+ assertEquals(token + "." + signingZone, outputs.get("DkimValue" + index));
+ changes.append("<Change><Action>CREATE</Action><ResourceRecordSet><Name>")
+ .append(outputs.get("DkimName" + index)).append("</Name><Type>CNAME</Type><TTL>60</TTL>")
+ .append("<ResourceRecords><ResourceRecord><Value>").append(outputs.get("DkimValue" + index))
+ .append("</Value></ResourceRecord></ResourceRecords></ResourceRecordSet></Change>");
+ }
+ String zoneId = null;
+ boolean recordsCreated = false;
+ try {
+ given().header("Authorization", SES_AUTH).contentType("application/json")
+ .body(MAPPER.writeValueAsString(Map.of("EmailIdentity", email)))
+ .post("/v2/email/identities").then().statusCode(200)
+ .body("DkimAttributes.SigningHostedZone", is(signingZone));
+ String zone = given().contentType("application/xml").body("""
+ <CreateHostedZoneRequest xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
+ <Name>%s</Name><CallerReference>%s</CallerReference></CreateHostedZoneRequest>
+ """.formatted(domain, stack)).post("/2013-04-01/hostedzone")
+ .then().statusCode(201).extract().asString();
+ zoneId = XmlParser.extractFirst(zone, "Id", null);
+ String recordPath = "/2013-04-01" + zoneId + "/rrset";
+ given().contentType("application/xml").body(dnsChanges(changes.toString()))
+ .post(recordPath).then().statusCode(200);
+ recordsCreated = true;
+ String published = given().get(recordPath).then().statusCode(200).extract().asString();
+ for (int index = 1; index <= 3; index++) {
+ assertTrue(XmlParser.containsValue(published, "Name", outputs.get("DkimName" + index) + "."));
+ assertTrue(XmlParser.containsValue(published, "Value", outputs.get("DkimValue" + index)));
+ }
+ clock.advance(Duration.ofSeconds(5));
+ await().atMost(Duration.ofSeconds(15)).untilAsserted(() ->
+ assertEquals("SUCCESS", sesIdentity(domain).jsonPath().getString("DkimAttributes.Status")));
+ assertEquals(signingZone, sesIdentity(email).jsonPath().getString("DkimAttributes.SigningHostedZone"));
+ } finally {
+ if (zoneId != null) {
+ if (recordsCreated) {
+ given().contentType("application/xml")
+ .body(dnsChanges(changes.toString().replace("<Action>CREATE</Action>", "<Action>DELETE</Action>")))
+ .post("/2013-04-01" + zoneId + "/rrset").then().statusCode(200);
+ }
+ given().delete("/2013-04-01" + zoneId).then().statusCode(200);
+ }
+ given().header("Authorization", SES_AUTH).delete("/v2/email/identities/{identity}", email)
+ .then().statusCode(anyOf(is(200), is(404)));
+ }
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ void conditionalOptionsCanBeRemovedAndRestoredWithoutReplacingTheIdentity(boolean members) throws Exception {
+ String identityName = "ses-" + Long.toString(System.nanoTime(), 36) + ".example.com";
+ stack = "cfn-ses-" + Long.toString(System.nanoTime(), 36);
+ cfn("CreateStack", conditionalOptionsTemplate(identityName, true, members)).then().statusCode(200);
+ awaitStatus("CREATE_COMPLETE");
+ assertConditionalOptions(identityName, true, members);
+ clearInvocations(sesService);
+
+ cfn("UpdateStack", conditionalOptionsTemplate(identityName, false, members)).then().statusCode(200);
+ String omitted = await().atMost(Duration.ofSeconds(15)).until(
+ () -> cfn("DescribeStacks", null).then().statusCode(200).extract().asString(),
+ body -> List.of("UPDATE_COMPLETE", "UPDATE_ROLLBACK_COMPLETE", "UPDATE_ROLLBACK_FAILED")
+ .contains(XmlParser.extractFirst(body, "StackStatus", null)));
+ assertEquals("UPDATE_COMPLETE", XmlParser.extractFirst(omitted, "StackStatus", null), omitted);
+ assertEquals(identityName, XmlParser.extractPairs(omitted,
+ "Outputs", "OutputKey", "OutputValue").get("IdentityRef"));
+ assertConditionalOptions(identityName, false, members);
+
+ cfn("UpdateStack", conditionalOptionsTemplate(identityName, true, members)).then().statusCode(200);
+ String restored = awaitStatus("UPDATE_COMPLETE");
+ Map<String, String> outputs = XmlParser.extractPairs(restored, "Outputs", "OutputKey", "OutputValue");
+ assertEquals(identityName, outputs.get("IdentityRef"));
+ assertConditionalOptions(identityName, true, members);
+ List<String> tokens = sesIdentity(identityName).jsonPath().getList("DkimAttributes.Tokens", String.class);
+ assertEquals(3, tokens.size());
+ for (int index = 1; index <= 3; index++) {
+ assertEquals(tokens.get(index - 1) + "._domainkey." + identityName, outputs.get("DkimName" + index));
+ assertEquals(tokens.get(index - 1) + ".dkim.amazonses.com", outputs.get("DkimValue" + index));
+ }
+ verify(sesService, never()).createEmailIdentity(eq(identityName), any(), any(), eq("us-east-1"));
+ verify(sesService, never()).deleteIdentity(identityName, "us-east-1");
+ }
+
+ @Test
+ void optionalSettingsSelectedAsNoValueCanCreateAnIdentity() throws Exception {
+ String identityName = "ses-" + Long.toString(System.nanoTime(), 36) + ".example.com";
+ stack = "cfn-ses-" + Long.toString(System.nanoTime(), 36);
+ ObjectNode template = (ObjectNode) MAPPER.readTree(conditionalOptionsTemplate(identityName, false, false));
+ template.withObject("/Resources/Identity/Properties").set("ConfigurationSetAttributes",
+ optionalValue(MAPPER.createObjectNode().put("ConfigurationSetName", "unused")));
+ cfn("CreateStack", template.toString()).then().statusCode(200);
+ String created = await().atMost(Duration.ofSeconds(15)).until(
+ () -> cfn("DescribeStacks", null).then().statusCode(200).extract().asString(),
+ body -> List.of("CREATE_COMPLETE", "ROLLBACK_COMPLETE", "ROLLBACK_FAILED")
+ .contains(XmlParser.extractFirst(body, "StackStatus", null)));
+ assertEquals("CREATE_COMPLETE", XmlParser.extractFirst(created, "StackStatus", null), created);
+ assertConditionalOptions(identityName, false, false);
+ assertEquals(null, sesIdentity(identityName).jsonPath().getString("ConfigurationSetName"));
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"DkimAttributes", "Tags", "Tag.Key", "Tag.Value"})
+ void noValueSupportDoesNotAcceptInvalidShapesOrMissingRequiredTagFields(String invalid) throws Exception {
+ String identityName = "ses-" + Long.toString(System.nanoTime(), 36) + ".example.com";
+ stack = "cfn-ses-" + Long.toString(System.nanoTime(), 36);
+ ObjectNode template = (ObjectNode) MAPPER.readTree(conditionalOptionsTemplate(identityName, false, false));
+ ObjectNode properties = template.withObject("/Resources/Identity/Properties");
+ if (invalid.startsWith("Tag.")) {
+ ObjectNode tag = properties.putArray("Tags").addObject().put("Key", "purpose").put("Value", "");
+ tag.set(invalid.substring(4), optionalValue(MAPPER.createObjectNode().put("Ref", "AWS::Region")));
+ } else {
+ properties.put(invalid, "");
+ }
+ cfn("CreateStack", template.toString()).then().statusCode(200);
+ String failed = await().atMost(Duration.ofSeconds(15)).until(
+ () -> cfn("DescribeStacks", null).then().statusCode(200).extract().asString(),
+ body -> List.of("CREATE_COMPLETE", "ROLLBACK_COMPLETE", "ROLLBACK_FAILED")
+ .contains(XmlParser.extractFirst(body, "StackStatus", null)));
+ assertEquals("ROLLBACK_COMPLETE", XmlParser.extractFirst(failed, "StackStatus", null), failed);
+ sesIdentity(identityName).then().statusCode(404);
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ void failedCreateOrReplacementCannotDeleteAnExistingIdentity(boolean replacing) throws Exception {
+ String existing = "existing-ses-" + Long.toString(System.nanoTime(), 36) + ".example.com";
+ String original = "original-" + existing;
+ stack = "cfn-ses-collision-" + Long.toString(System.nanoTime(), 36);
+ given().header("Authorization", SES_AUTH).contentType("application/json")
+ .body(Map.of("EmailIdentity", existing, "Tags", List.of(Map.of("Key", "owner", "Value", "external"))))
+ .post("/v2/email/identities").then().statusCode(200);
+ List<String> tokens = sesIdentity(existing).jsonPath().getList("DkimAttributes.Tokens", String.class);
+ try {
+ if (replacing) {
+ cfn("CreateStack", template(original, false)).then().statusCode(200);
+ awaitStatus("CREATE_COMPLETE");
+ }
+ cfn(replacing ? "UpdateStack" : "CreateStack", template(existing, true)).then().statusCode(200);
+ String rolledBack = awaitStatus(replacing ? "UPDATE_ROLLBACK_COMPLETE" : "ROLLBACK_COMPLETE");
+ if (replacing) {
+ assertEquals(original, XmlParser.extractPairs(rolledBack, "Outputs", "OutputKey", "OutputValue")
+ .get("IdentityRef"));
+ sesIdentity(original).then().statusCode(200);
+ }
+ cfn("DeleteStack", null).then().statusCode(200);
+ await().atMost(Duration.ofSeconds(15)).untilAsserted(() ->
+ assertEquals(400, cfn("DescribeStacks", null).statusCode()));
+ stack = null;
+ sesIdentity(original).then().statusCode(404);
+ Response retained = sesIdentity(existing);
+ assertEquals(200, retained.statusCode(), retained.asString());
+ assertEquals(tokens, retained.jsonPath().getList("DkimAttributes.Tokens", String.class));
+ assertEquals(List.of(Map.of("Key", "owner", "Value", "external")), retained.jsonPath().getList("Tags"));
+ } finally {
+ given().header("Authorization", SES_AUTH).delete("/v2/email/identities/{identity}", existing)
+ .then().statusCode(200);
+ }
+ }
+
+ @Test
+ void changingIdentityReplacesBackingSesResource() throws Exception {
+ String first = "ses-" + Long.toString(System.nanoTime(), 36) + ".example.com";
+ String second = "other-" + first;
+ stack = "cfn-ses-" + Long.toString(System.nanoTime(), 36);
+ cfn("CreateStack", template(first, false)).then().statusCode(200);
+ awaitStatus("CREATE_COMPLETE");
+
+ cfn("UpdateStack", template(second, false)).then().statusCode(200);
+ String updated = awaitStatus("UPDATE_COMPLETE");
+
+ assertEquals(second, XmlParser.extractPairs(updated, "Outputs", "OutputKey", "OutputValue")
+ .get("IdentityRef"));
+ assertEquals(404, sesIdentity(first).statusCode());
+ assertEquals(200, sesIdentity(second).statusCode());
+ }
+
+ @Test
+ void changeSetPreviewMatchesReplacementAndInPlaceExecution() throws Exception {
+ String first = "ses-" + Long.toString(System.nanoTime(), 36) + ".example.com";
+ String second = "other-" + first;
+ stack = "cfn-ses-" + Long.toString(System.nanoTime(), 36);
+ cfn("CreateStack", dependentTemplate(first, false)).then().statusCode(200);
+ awaitStatus("CREATE_COMPLETE");
+ assertEquals(first, dependentValue());
+
+ changeSet("CreateChangeSet", "replace-identity", dependentTemplate(second, false))
+ .then().statusCode(200);
+ String replacementPreview = changeSet("DescribeChangeSet", "replace-identity", null)
+ .then().statusCode(200).extract().asString();
+ Map<String, String> identityChange = XmlParser.extractGroups(replacementPreview, "ResourceChange")
+ .stream().filter(change -> "Identity".equals(change.get("LogicalResourceId")))
+ .findFirst().orElseThrow();
+ assertEquals("Modify", identityChange.get("Action"));
+ assertEquals("True", identityChange.get("Replacement"));
+ Map<String, String> dependentChange = XmlParser.extractGroups(replacementPreview, "ResourceChange")
+ .stream().filter(change -> "DependentParameter".equals(change.get("LogicalResourceId")))
+ .findFirst().orElseThrow();
+ assertEquals("Modify", dependentChange.get("Action"));
+ assertEquals("False", dependentChange.get("Replacement"));
+ assertEquals(200, sesIdentity(first).statusCode(), "Preview must leave the deployed identity unchanged");
+ assertEquals(404, sesIdentity(second).statusCode());
+ assertEquals(first, dependentValue());
+
+ changeSet("ExecuteChangeSet", "replace-identity", null).then().statusCode(200);
+ String replaced = awaitStatus("UPDATE_COMPLETE");
+ assertEquals(second, XmlParser.extractPairs(replaced,
+ "Outputs", "OutputKey", "OutputValue").get("IdentityRef"));
+ assertEquals(404, sesIdentity(first).statusCode());
+ assertEquals(200, sesIdentity(second).statusCode());
+ assertEquals(second, dependentValue());
+
+ changeSet("CreateChangeSet", "update-options", dependentTemplate(second, true)).then().statusCode(200);
+ String optionPreview = changeSet("DescribeChangeSet", "update-options", null)
+ .then().statusCode(200).extract().asString();
+ Map<String, String> optionChange = XmlParser.extractGroups(optionPreview, "ResourceChange")
+ .stream().filter(change -> "Identity".equals(change.get("LogicalResourceId")))
+ .findFirst().orElseThrow();
+ assertEquals("False", optionChange.get("Replacement"));
+ assertEquals(true, sesIdentity(second).jsonPath().getBoolean("FeedbackForwardingStatus"));
+ changeSet("ExecuteChangeSet", "update-options", null).then().statusCode(200);
+ awaitStatus("UPDATE_COMPLETE");
+ assertEquals(false, sesIdentity(second).jsonPath().getBoolean("FeedbackForwardingStatus"));
+ assertEquals(second, dependentValue());
+ }
+
+ @Test
+ void replacementRetainKeepsThePriorIdentityAfterAnotherUpdateAndStackDeletion() throws Exception {
+ String first = "ses-" + Long.toString(System.nanoTime(), 36) + ".example.com";
+ String second = "retained-" + first;
+ stack = "cfn-ses-" + Long.toString(System.nanoTime(), 36);
+ cfn("CreateStack", retainedTemplate(first, false)).then().statusCode(200);
+ awaitStatus("CREATE_COMPLETE");
+ List<String> originalTokens = sesIdentity(first).jsonPath()
+ .getList("DkimAttributes.Tokens", String.class);
+
+ try {
+ cfn("UpdateStack", retainedTemplate(second, false)).then().statusCode(200);
+ String replaced = awaitStatus("UPDATE_COMPLETE");
+ assertEquals(second, XmlParser.extractPairs(replaced,
+ "Outputs", "OutputKey", "OutputValue").get("IdentityRef"));
+ assertEquals(originalTokens, sesIdentity(first).jsonPath()
+ .getList("DkimAttributes.Tokens", String.class));
+
+ cfn("UpdateStack", retainedTemplate(second, true)).then().statusCode(200);
+ awaitStatus("UPDATE_COMPLETE");
+ assertEquals("old", sesIdentity(first).jsonPath()
+ .getString("Tags.find { it.Key == 'purpose' }.Value"));
+ assertEquals("new", sesIdentity(second).jsonPath()
+ .getString("Tags.find { it.Key == 'purpose' }.Value"));
+
+ cfn("DeleteStack", null).then().statusCode(200);
+ await().atMost(Duration.ofSeconds(15)).untilAsserted(() ->
+ assertEquals(404, sesIdentity(second).statusCode()));
+ assertEquals(200, sesIdentity(first).statusCode(),
+ "UpdateReplacePolicy Retain must release the displaced identity from stack ownership");
+ stack = null;
+ } finally {
+ given().header("Authorization", SES_AUTH)
+ .delete("/v2/email/identities/{identity}", first).then().statusCode(anyOf(is(200), is(404)));
+ }
+ }
+
+ @Test
+ void failedUpdateRestoresOnlyTheOwningAccountIdentity() throws Exception {
+ String owner = "000000000001";
+ String other = "000000000002";
+ String identityName = "ses-" + Long.toString(System.nanoTime(), 36) + ".example.com";
+ stack = "cfn-ses-" + Long.toString(System.nanoTime(), 36);
+ stackAuth = auth(owner, "cloudformation");
+ given().header("Authorization", auth(other, "ses")).contentType("application/json")
+ .body(MAPPER.writeValueAsString(Map.of("EmailIdentity", identityName,
+ "Tags", List.of(Map.of("Key", "purpose", "Value", "external")))))
+ .post("/v2/email/identities").then().statusCode(200);
+
+ try {
+ cfn("CreateStack", template(identityName, false)).then().statusCode(200);
+ awaitStatus("CREATE_COMPLETE");
+ List<String> ownerTokens = sesIdentity(identityName, owner).jsonPath()
+ .getList("DkimAttributes.Tokens", String.class);
+ List<String> otherTokens = sesIdentity(identityName, other).jsonPath()
+ .getList("DkimAttributes.Tokens", String.class);
+ ObjectNode attempted = (ObjectNode) MAPPER.readTree(template(identityName, true));
+ attempted.withObject("/Resources").set("BadSecret", MAPPER.valueToTree(Map.of(
+ "Type", "AWS::SecretsManager::Secret", "DependsOn", "Identity",
+ "Properties", Map.of("SecretString", "explicit",
+ "GenerateSecretString", Map.of("PasswordLength", 32)))));
+
+ cfn("UpdateStack", attempted.toString()).then().statusCode(200);
+ awaitStatus("UPDATE_ROLLBACK_COMPLETE");
+ Response restored = sesIdentity(identityName, owner);
+ assertEquals(200, restored.statusCode(), restored.asString());
+ assertEquals(ownerTokens, restored.jsonPath().getList("DkimAttributes.Tokens", String.class));
+ assertEquals(true, restored.jsonPath().getBoolean("FeedbackForwardingStatus"));
+ assertEquals("old", restored.jsonPath().getString("Tags.find { it.Key == 'purpose' }.Value"));
+ Response untouched = sesIdentity(identityName, other);
+ assertEquals(otherTokens, untouched.jsonPath().getList("DkimAttributes.Tokens", String.class));
+ assertEquals("external", untouched.jsonPath().getString("Tags.find { it.Key == 'purpose' }.Value"));
+
+ cfn("UpdateStack", template(identityName, true)).then().statusCode(200);
+ awaitStatus("UPDATE_COMPLETE");
+ cfn("DeleteStack", null).then().statusCode(200);
+ await().atMost(Duration.ofSeconds(15)).untilAsserted(() ->
+ assertEquals(404, sesIdentity(identityName, owner).statusCode()));
+ assertEquals(200, sesIdentity(identityName, other).statusCode(),
+ "Deleting the owner stack must not delete another account's same-named identity");
+ assertEquals("external", sesIdentity(identityName, other).jsonPath()
+ .getString("Tags.find { it.Key == 'purpose' }.Value"));
+ stack = null;
+ } finally {
+ given().header("Authorization", auth(other, "ses"))
+ .delete("/v2/email/identities/{identity}", identityName)
+ .then().statusCode(anyOf(is(200), is(404)));
+ }
+ }
+
+ @Test
+ void failedLaterResourceRestoresIdentitySettingsAndTags() throws Exception {
+ String identityName = "ses-" + Long.toString(System.nanoTime(), 36) + ".example.com";
+ stack = "cfn-ses-" + Long.toString(System.nanoTime(), 36);
+ String signingZone = SesIdentityService.DKIM_SIGNING_HOSTED_ZONE;
+ createIdentityStack(identityName);
+ List<String> originalTokens = sesIdentity(identityName).jsonPath()
+ .getList("DkimAttributes.Tokens", String.class);
+
+ ObjectNode attempted = (ObjectNode) MAPPER.readTree(template(identityName, true));
+ attempted.withObject("/Resources").set("BadSecret", MAPPER.valueToTree(Map.of(
+ "Type", "AWS::SecretsManager::Secret", "DependsOn", "Identity",
+ "Properties", Map.of("SecretString", "explicit",
+ "GenerateSecretString", Map.of("PasswordLength", 32)))));
+ cfn("UpdateStack", attempted.toString()).then().statusCode(200);
+ awaitStatus("UPDATE_ROLLBACK_COMPLETE");
+
+ Response identity = sesIdentity(identityName);
+ assertEquals(200, identity.statusCode(), identity.asString());
+ assertEquals(true, identity.jsonPath().getBoolean("FeedbackForwardingStatus"));
+ assertEquals(true, identity.jsonPath().getBoolean("DkimAttributes.SigningEnabled"));
+ assertEquals("RSA_2048_BIT", identity.jsonPath().getString("DkimAttributes.NextSigningKeyLength"));
+ assertEquals(originalTokens, identity.jsonPath().getList("DkimAttributes.Tokens", String.class));
+ assertEquals(signingZone, identity.jsonPath().getString("DkimAttributes.SigningHostedZone"));
+ assertEquals(null, identity.jsonPath().getString("MailFromAttributes.MailFromDomain"));
+ assertEquals("old", identity.jsonPath().getString("Tags.find { it.Key == 'purpose' }.Value"));
+ String restored = cfn("DescribeStacks", null).then().statusCode(200).extract().asString();
+ assertEquals(originalTokens.getFirst() + "." + signingZone,
+ XmlParser.extractPairs(restored, "Outputs", "OutputKey", "OutputValue").get("DkimValue1"));
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ void continuedRollbackRestoresTheLastSuccessfulIdentitySnapshotBeforeAnotherUpdate(boolean replacing)
+ throws Exception {
+ String identityName = "ses-" + Long.toString(System.nanoTime(), 36) + ".example.com";
+ stack = "cfn-ses-" + Long.toString(System.nanoTime(), 36);
+ cfn("CreateStack", template(identityName, false)).then().statusCode(200);
+ awaitStatus("CREATE_COMPLETE");
+ List<String> originalTokens = sesIdentity(identityName).jsonPath()
+ .getList("DkimAttributes.Tokens", String.class);
+ ObjectNode firstAttempt = (ObjectNode) MAPPER.readTree(template(identityName, true));
+ firstAttempt.withObject("/Resources").set("BadSecret", MAPPER.valueToTree(Map.of(
+ "Type", "AWS::SecretsManager::Secret", "DependsOn", "Identity",
+ "Properties", Map.of("SecretString", "first-attempt",
+ "GenerateSecretString", Map.of("PasswordLength", 32)))));
+ clearInvocations(identityService);
+ doThrow(new AwsException("ServiceUnavailableException", "temporary restore failure", 503))
+ .doCallRealMethod().when(identityService).save(
+ argThat(identity -> isOriginalSnapshot(identity, identityName, originalTokens)),
+ eq("us-east-1"));
+
+ try {
+ cfn("UpdateStack", firstAttempt.toString()).then().statusCode(200);
+ awaitStatus("UPDATE_ROLLBACK_FAILED");
+ assertEquals(false, sesIdentity(identityName).jsonPath().getBoolean("FeedbackForwardingStatus"));
+ assertEquals("new", sesIdentity(identityName).jsonPath()
+ .getString("Tags.find { it.Key == 'purpose' }.Value"));
+ cfn("ContinueUpdateRollback", null).then().statusCode(200);
+ awaitStatus("UPDATE_ROLLBACK_COMPLETE");
+ String nextIdentity = replacing ? "replacement-" + identityName : identityName;
+ ObjectNode secondAttempt = (ObjectNode) MAPPER.readTree(template(nextIdentity, false));
+ secondAttempt.withObject("/Resources/Identity/Properties").set("Tags",
+ MAPPER.valueToTree(List.of(Map.of("Key", "purpose", "Value", "second-attempt"))));
+ secondAttempt.withObject("/Resources").set("BadSecret", MAPPER.valueToTree(Map.of(
+ "Type", "AWS::SecretsManager::Secret", "DependsOn", "Identity",
+ "Properties", Map.of("SecretString", "second-attempt",
+ "GenerateSecretString", Map.of("PasswordLength", 64)))));
+
+ cfn("UpdateStack", secondAttempt.toString()).then().statusCode(200);
+ awaitStatus("UPDATE_ROLLBACK_COMPLETE");
+
+ Response recovered = sesIdentity(identityName);
+ assertEquals(200, recovered.statusCode(), recovered.asString());
+ assertEquals(originalTokens, recovered.jsonPath().getList("DkimAttributes.Tokens", String.class));
+ assertEquals(true, recovered.jsonPath().getBoolean("FeedbackForwardingStatus"));
+ assertEquals(true, recovered.jsonPath().getBoolean("DkimAttributes.SigningEnabled"));
+ assertEquals("RSA_2048_BIT", recovered.jsonPath().getString("DkimAttributes.NextSigningKeyLength"));
+ assertEquals(null, recovered.jsonPath().getString("MailFromAttributes.MailFromDomain"));
+ assertEquals("old", recovered.jsonPath().getString("Tags.find { it.Key == 'purpose' }.Value"));
+ if (replacing) {
+ assertEquals(404, sesIdentity(nextIdentity).statusCode());
+ String restored = cfn("DescribeStacks", null).then().statusCode(200).extract().asString();
+ assertEquals(identityName, XmlParser.extractPairs(restored,
+ "Outputs", "OutputKey", "OutputValue").get("IdentityRef"));
+ }
+ } finally {
+ doCallRealMethod().when(identityService).save(any(Identity.class), eq("us-east-1"));
+ }
+ }
+
+ @Test
+ void failedReplacementAfterRecoveryDoesNotReplayConsumedIdentitySnapshot() throws Exception {
+ String identityName = "ses-" + Long.toString(System.nanoTime(), 36) + ".example.com";
+ String replacement = "replacement-" + identityName;
+ stack = "cfn-ses-" + Long.toString(System.nanoTime(), 36);
+ createIdentityStack(identityName);
+ List<String> originalTokens = sesIdentity(identityName).jsonPath()
+ .getList("DkimAttributes.Tokens", String.class);
+ ObjectNode failed = (ObjectNode) MAPPER.readTree(template(identityName, true));
+ failed.withObject("/Resources").set("BadSecret", MAPPER.valueToTree(Map.of(
+ "Type", "AWS::SecretsManager::Secret", "DependsOn", "Identity",
+ "Properties", Map.of("SecretString", "first-attempt",
+ "GenerateSecretString", Map.of("PasswordLength", 32)))));
+ doThrow(new AwsException("ServiceUnavailableException", "temporary restore failure", 503))
+ .doCallRealMethod().when(identityService).save(
+ argThat(identity -> isOriginalSnapshot(identity, identityName, originalTokens)),
+ eq("us-east-1"));
+
+ try {
+ cfn("UpdateStack", failed.toString()).then().statusCode(200);
+ awaitStatus("UPDATE_ROLLBACK_FAILED");
+ continueUpdateRollbackSkipping("Identity").then().statusCode(200);
+ awaitStatus("UPDATE_ROLLBACK_COMPLETE");
+ doThrow(new AwsException("ServiceUnavailableException", "replacement unavailable", 503))
+ .when(sesService).createEmailIdentity(eq(replacement), isNull(), any(), eq("us-east-1"));
+
+ cfn("UpdateStack", template(replacement, false)).then().statusCode(200);
+ awaitStatus("UPDATE_ROLLBACK_COMPLETE");
+ assertEquals(originalTokens, sesIdentity(identityName).jsonPath()
+ .getList("DkimAttributes.Tokens", String.class));
+ assertEquals("old", sesIdentity(identityName).jsonPath()
+ .getString("Tags.find { it.Key == 'purpose' }.Value"));
+ assertEquals(404, sesIdentity(replacement).statusCode());
+
+ clearInvocations(identityService);
+ ObjectNode finalAttempt = (ObjectNode) MAPPER.readTree(template(identityName, true));
+ finalAttempt.withObject("/Resources/Identity/Properties").set("Tags",
+ MAPPER.valueToTree(List.of(Map.of("Key", "purpose", "Value", "final-attempt"))));
+ cfn("UpdateStack", finalAttempt.toString()).then().statusCode(200);
+ awaitStatus("UPDATE_COMPLETE");
+ verify(identityService, never()).save(any(Identity.class), eq("us-east-1"));
+ } finally {
+ doCallRealMethod().when(identityService).save(any(Identity.class), eq("us-east-1"));
+ doCallRealMethod().when(sesService).createEmailIdentity(eq(replacement), isNull(), any(),
+ eq("us-east-1"));
+ }
+ }
+
+ @Test
+ void failedIdentityRollbackRejectsUpdateAndRemainsRetryable() throws Exception {
+ String identityName = "ses-" + Long.toString(System.nanoTime(), 36) + ".example.com";
+ String otherAccount = "000000000002";
+ stack = "cfn-ses-" + Long.toString(System.nanoTime(), 36);
+ given().header("Authorization", auth(otherAccount, "ses")).contentType("application/json")
+ .body(MAPPER.writeValueAsString(Map.of("EmailIdentity", identityName,
+ "Tags", List.of(Map.of("Key", "purpose", "Value", "external")))))
+ .post("/v2/email/identities").then().statusCode(200);
+ cfn("CreateStack", template(identityName, false)).then().statusCode(200);
+ awaitStatus("CREATE_COMPLETE");
+ List<String> originalTokens = sesIdentity(identityName).jsonPath()
+ .getList("DkimAttributes.Tokens", String.class);
+ List<String> otherTokens = sesIdentity(identityName, otherAccount).jsonPath()
+ .getList("DkimAttributes.Tokens", String.class);
+ ObjectNode failed = (ObjectNode) MAPPER.readTree(template(identityName, true));
+ failed.withObject("/Resources").set("BadSecret", MAPPER.valueToTree(Map.of(
+ "Type", "AWS::SecretsManager::Secret", "DependsOn", "Identity",
+ "Properties", Map.of("SecretString", "explicit",
+ "GenerateSecretString", Map.of("PasswordLength", 32)))));
+ clearInvocations(identityService);
+ doThrow(new AwsException("ServiceUnavailableException", "restore unavailable", 503))
+ .when(identityService).save(
+ argThat(identity -> isOriginalSnapshot(identity, identityName, originalTokens)),
+ eq("us-east-1"));
+
+ try {
+ cfn("UpdateStack", failed.toString()).then().statusCode(200);
+ awaitStatus("UPDATE_ROLLBACK_FAILED");
+ ObjectNode outputsOnly = (ObjectNode) MAPPER.readTree(template(identityName, true));
+ outputsOnly.withObject("/Outputs").set("Marker", MAPPER.valueToTree(Map.of("Value", "cleanup")));
+ cfn("UpdateStack", outputsOnly.toString()).then().statusCode(400);
+ cfn("ContinueUpdateRollback", null).then().statusCode(200);
+ String failedAgain = awaitStatus("UPDATE_ROLLBACK_FAILED");
+ assertEquals(null, XmlParser.extractPairs(failedAgain,
+ "Outputs", "OutputKey", "OutputValue").get("Marker"));
+ assertEquals(false, sesIdentity(identityName).jsonPath().getBoolean("FeedbackForwardingStatus"));
+ assertEquals("new", sesIdentity(identityName).jsonPath()
+ .getString("Tags.find { it.Key == 'purpose' }.Value"));
+ verify(identityService, times(2)).save(
+ argThat(identity -> isOriginalSnapshot(identity, identityName, originalTokens)),
+ eq("us-east-1"));
+
+ doThrow(new AwsException("ServiceUnavailableException", "temporary delete failure", 503))
+ .doCallRealMethod().when(sesService).deleteIdentity(identityName, "us-east-1");
+ cfn("DeleteStack", null).then().statusCode(200);
+ awaitStatus("DELETE_FAILED");
+ assertEquals(200, sesIdentity(identityName).statusCode());
+ cfn("DeleteStack", null).then().statusCode(200);
+ await().atMost(Duration.ofSeconds(15)).untilAsserted(() ->
+ assertEquals(404, sesIdentity(identityName).statusCode()));
+ assertEquals(otherTokens, sesIdentity(identityName, otherAccount).jsonPath()
+ .getList("DkimAttributes.Tokens", String.class));
+ assertEquals("external", sesIdentity(identityName, otherAccount).jsonPath()
+ .getString("Tags.find { it.Key == 'purpose' }.Value"));
+ stack = null;
+ } finally {
+ doCallRealMethod().when(identityService).save(any(Identity.class), eq("us-east-1"));
+ doCallRealMethod().when(sesService).deleteIdentity(identityName, "us-east-1");
+ given().header("Authorization", auth(otherAccount, "ses"))
+ .delete("/v2/email/identities/{identity}", identityName)
+ .then().statusCode(anyOf(is(200), is(404)));
+ }
+ }
+
+ @Test
+ void outputsOnlyUpdateCommitsAfterContinuedIdentityRollback() throws Exception {
+ String identityName = "ses-" + Long.toString(System.nanoTime(), 36) + ".example.com";
+ stack = "cfn-ses-" + Long.toString(System.nanoTime(), 36);
+ createIdentityStack(identityName);
+ List<String> originalTokens = sesIdentity(identityName).jsonPath()
+ .getList("DkimAttributes.Tokens", String.class);
+ ObjectNode failed = (ObjectNode) MAPPER.readTree(template(identityName, true));
+ failed.withObject("/Resources").set("BadSecret", MAPPER.valueToTree(Map.of(
+ "Type", "AWS::SecretsManager::Secret", "DependsOn", "Identity",
+ "Properties", Map.of("SecretString", "explicit",
+ "GenerateSecretString", Map.of("PasswordLength", 32)))));
+ doThrow(new AwsException("ServiceUnavailableException", "temporary restore failure", 503))
+ .doCallRealMethod().when(identityService).save(
+ argThat(identity -> isOriginalSnapshot(identity, identityName, originalTokens)),
+ eq("us-east-1"));
+
+ try {
+ cfn("UpdateStack", failed.toString()).then().statusCode(200);
+ awaitStatus("UPDATE_ROLLBACK_FAILED");
+ cfn("ContinueUpdateRollback", null).then().statusCode(200);
+ awaitStatus("UPDATE_ROLLBACK_COMPLETE");
+
+ ObjectNode outputsOnly = (ObjectNode) MAPPER.readTree(template(identityName, true));
+ outputsOnly.withObject("/Outputs").set("Marker", MAPPER.valueToTree(Map.of("Value", "recovered")));
+ cfn("UpdateStack", outputsOnly.toString()).then().statusCode(200);
+ String completed = awaitStatus("UPDATE_COMPLETE");
+ assertEquals("recovered", XmlParser.extractPairs(completed,
+ "Outputs", "OutputKey", "OutputValue").get("Marker"));
+ assertEquals("new", sesIdentity(identityName).jsonPath()
+ .getString("Tags.find { it.Key == 'purpose' }.Value"));
+ verify(identityService, atLeastOnce()).save(
+ argThat(identity -> isOriginalSnapshot(identity, identityName, originalTokens)),
+ eq("us-east-1"));
+ } finally {
+ doCallRealMethod().when(identityService).save(any(Identity.class), eq("us-east-1"));
+ }
+ }
+
+ @Test
+ void continuedIdentityRollbackRemainsCompleteWhenLaterResourceFails() throws Exception {
+ String identityName = "ses-" + Long.toString(System.nanoTime(), 36) + ".example.com";
+ stack = "cfn-ses-" + Long.toString(System.nanoTime(), 36);
+ createIdentityStack(identityName);
+ List<String> originalTokens = sesIdentity(identityName).jsonPath()
+ .getList("DkimAttributes.Tokens", String.class);
+ ObjectNode firstFailure = (ObjectNode) MAPPER.readTree(template(identityName, true));
+ firstFailure.withObject("/Resources").set("BadSecret", MAPPER.valueToTree(Map.of(
+ "Type", "AWS::SecretsManager::Secret", "DependsOn", "Identity",
+ "Properties", Map.of("SecretString", "explicit",
+ "GenerateSecretString", Map.of("PasswordLength", 32)))));
+ doThrow(new AwsException("ServiceUnavailableException", "temporary restore failure", 503))
+ .doCallRealMethod().when(identityService).save(
+ argThat(identity -> isOriginalSnapshot(identity, identityName, originalTokens)),
+ eq("us-east-1"));
+
+ try {
+ cfn("UpdateStack", firstFailure.toString()).then().statusCode(200);
+ awaitStatus("UPDATE_ROLLBACK_FAILED");
+ cfn("ContinueUpdateRollback", null).then().statusCode(200);
+ awaitStatus("UPDATE_ROLLBACK_COMPLETE");
+
+ ObjectNode secondFailure = (ObjectNode) MAPPER.readTree(template(identityName, true));
+ secondFailure.withObject("/Resources").set("AnotherBadSecret", MAPPER.valueToTree(Map.of(
+ "Type", "AWS::SecretsManager::Secret", "DependsOn", "Identity",
+ "Properties", Map.of("SecretString", "explicit",
+ "GenerateSecretString", Map.of("PasswordLength", 32)))));
+ secondFailure.withObject("/Outputs").set("Marker", MAPPER.valueToTree(Map.of("Value", "attempted")));
+ cfn("UpdateStack", secondFailure.toString()).then().statusCode(200);
+ String rolledBack = awaitStatus("UPDATE_ROLLBACK_COMPLETE");
+ assertEquals(null, XmlParser.extractPairs(rolledBack,
+ "Outputs", "OutputKey", "OutputValue").get("Marker"));
+ assertEquals("CREATE_COMPLETE", XmlParser.extractPairs(
+ cfn("DescribeStackResources", null).then().statusCode(200).extract().asString(),
+ "StackResources", "LogicalResourceId", "ResourceStatus").get("Identity"));
+ Response restored = sesIdentity(identityName);
+ assertEquals(originalTokens, restored.jsonPath().getList("DkimAttributes.Tokens", String.class));
+ assertEquals("old", restored.jsonPath().getString("Tags.find { it.Key == 'purpose' }.Value"));
+ verify(identityService, atLeastOnce()).save(
+ argThat(identity -> isOriginalSnapshot(identity, identityName, originalTokens)),
+ eq("us-east-1"));
+ } finally {
+ doCallRealMethod().when(identityService).save(any(Identity.class), eq("us-east-1"));
+ }
+ }
+
+ @Test
+ void failedPostCreateCleanupIsRetriedByStackRollback() throws Exception {
+ String identityName = "ses-" + Long.toString(System.nanoTime(), 36) + ".example.com";
+ String mailFromDomain = "mail." + identityName;
+ stack = "cfn-ses-" + Long.toString(System.nanoTime(), 36);
+ ObjectNode attempted = (ObjectNode) MAPPER.readTree(template(identityName, false));
+ attempted.withObject("/Resources/Identity/Properties").set("MailFromAttributes",
+ MAPPER.valueToTree(Map.of("MailFromDomain", mailFromDomain)));
+ doThrow(new AwsException("BadRequestException", "MAIL FROM rejected", 400))
+ .when(identityService).setMailFromDomain(identityName, mailFromDomain,
+ "UseDefaultValue", "us-east-1");
+ doThrow(new AwsException("ServiceUnavailableException", "temporary delete failure", 503))
+ .doCallRealMethod().when(sesService).deleteIdentity(identityName, "us-east-1");
+
+ try {
+ cfn("CreateStack", attempted.toString()).then().statusCode(200);
+ awaitStatus("ROLLBACK_COMPLETE");
+ assertEquals(404, sesIdentity(identityName).statusCode(),
+ "Stack rollback must delete an identity left by failed post-create cleanup");
+ verify(sesService, times(2)).deleteIdentity(identityName, "us-east-1");
+ } finally {
+ doCallRealMethod().when(identityService).setMailFromDomain(identityName, mailFromDomain,
+ "UseDefaultValue", "us-east-1");
+ doCallRealMethod().when(sesService).deleteIdentity(identityName, "us-east-1");
+ }
+ }
+
+ @Test
+ void failedReplacementCleanupSurvivesRejectedUpdatesUntilSuccessfulUpdate() throws Exception {
+ String original = "ses-" + Long.toString(System.nanoTime(), 36) + ".example.com";
+ String replacement = "replacement-" + original;
+ String mailFromDomain = "mail." + replacement;
+ stack = "cfn-ses-" + Long.toString(System.nanoTime(), 36);
+ cfn("CreateStack", retainedTemplate(original, false)).then().statusCode(200);
+ awaitStatus("CREATE_COMPLETE");
+ List<String> originalTokens = sesIdentity(original).jsonPath()
+ .getList("DkimAttributes.Tokens", String.class);
+ doThrow(new AwsException("BadRequestException", "MAIL FROM rejected", 400))
+ .when(identityService).setMailFromDomain(replacement, mailFromDomain,
+ "RejectMessage", "us-east-1");
+ doThrow(new AwsException("ServiceUnavailableException", "temporary delete failure", 503))
+ .doCallRealMethod().when(sesService).deleteIdentity(replacement, "us-east-1");
+
+ try {
+ cfn("UpdateStack", retainedTemplate(replacement, true)).then().statusCode(200);
+ String rolledBack = awaitStatus("UPDATE_ROLLBACK_COMPLETE");
+ assertEquals(original, XmlParser.extractPairs(rolledBack,
+ "Outputs", "OutputKey", "OutputValue").get("IdentityRef"));
+ assertEquals(200, sesIdentity(replacement).statusCode(),
+ "The failed cleanup must retain the replacement for a later retry");
+ Response identity = sesIdentity(original);
+ assertEquals(200, identity.statusCode(), identity.asString());
+ assertEquals(originalTokens, identity.jsonPath().getList("DkimAttributes.Tokens", String.class));
+ assertEquals("old", identity.jsonPath().getString("Tags.find { it.Key == 'purpose' }.Value"));
+
+ ObjectNode rejected = (ObjectNode) MAPPER.readTree(retainedTemplate(original, false));
+ rejected.withObject("/Resources/Identity/Properties").set("DkimSigningAttributes",
+ MAPPER.valueToTree(Map.of("NextSigningKeyLength", "INVALID")));
+ cfn("UpdateStack", rejected.toString()).then().statusCode(200);
+ awaitStatus("UPDATE_ROLLBACK_COMPLETE");
+ assertEquals(originalTokens,
+ sesIdentity(original).jsonPath().getList("DkimAttributes.Tokens", String.class));
+ assertEquals(200, sesIdentity(replacement).statusCode());
+ verify(sesService, times(1)).deleteIdentity(replacement, "us-east-1");
+
+ ObjectNode outputsOnly = (ObjectNode) MAPPER.readTree(retainedTemplate(original, false));
+ outputsOnly.withObject("/Outputs").set("Marker", MAPPER.valueToTree(Map.of("Value", "cleanup")));
+ cfn("UpdateStack", outputsOnly.toString()).then().statusCode(200);
+ awaitStatus("UPDATE_COMPLETE");
+ assertEquals(404, sesIdentity(replacement).statusCode(),
+ "An outputs-only update must retry cleanup even when SES provisioning is skipped");
+ assertEquals(originalTokens,
+ sesIdentity(original).jsonPath().getList("DkimAttributes.Tokens", String.class));
+ assertEquals("old", sesIdentity(original).jsonPath()
+ .getString("Tags.find { it.Key == 'purpose' }.Value"));
+ verify(sesService, times(2)).deleteIdentity(replacement, "us-east-1");
+
+ cfn("DeleteStack", null).then().statusCode(200);
+ await().atMost(Duration.ofSeconds(15)).untilAsserted(() ->
+ assertEquals(404, sesIdentity(original).statusCode()));
+ stack = null;
+ } finally {
+ doCallRealMethod().when(identityService).setMailFromDomain(replacement, mailFromDomain,
+ "RejectMessage", "us-east-1");
+ doCallRealMethod().when(sesService).deleteIdentity(replacement, "us-east-1");
+ given().header("Authorization", SES_AUTH)
+ .delete("/v2/email/identities/{identity}", replacement).then().statusCode(anyOf(is(200), is(404)));
+ }
+ }
+
+ @Test
+ void stackDeletionRetriesCleanupOfFailedReplacement() throws Exception {
+ String original = "ses-" + Long.toString(System.nanoTime(), 36) + ".example.com";
+ String replacement = "replacement-" + original;
+ stack = "cfn-ses-" + Long.toString(System.nanoTime(), 36);
+ cfn("CreateStack", template(original, false)).then().statusCode(200);
+ awaitStatus("CREATE_COMPLETE");
+ doThrow(new AwsException("ServiceUnavailableException", "temporary read failure", 503))
+ .when(identityService).getIdentityVerificationAttributes(replacement, "us-east-1");
+ doThrow(new AwsException("ServiceUnavailableException", "temporary delete failure", 503))
+ .doCallRealMethod().when(sesService).deleteIdentity(replacement, "us-east-1");
+
+ try {
+ cfn("UpdateStack", template(replacement, false)).then().statusCode(200);
+ awaitStatus("UPDATE_ROLLBACK_COMPLETE");
+ doCallRealMethod().when(identityService).getIdentityVerificationAttributes(replacement, "us-east-1");
+ assertEquals(200, sesIdentity(original).statusCode());
+ assertEquals(200, sesIdentity(replacement).statusCode());
+
+ cfn("DeleteStack", null).then().statusCode(200);
+ await().atMost(Duration.ofSeconds(15)).untilAsserted(() -> {
+ assertEquals(404, sesIdentity(original).statusCode());
+ assertEquals(404, sesIdentity(replacement).statusCode());
+ });
+ verify(sesService, times(2)).deleteIdentity(replacement, "us-east-1");
+ stack = null;
+ } finally {
+ doCallRealMethod().when(identityService).getIdentityVerificationAttributes(replacement, "us-east-1");
+ doCallRealMethod().when(sesService).deleteIdentity(replacement, "us-east-1");
+ given().header("Authorization", SES_AUTH)
+ .delete("/v2/email/identities/{identity}", replacement).then().statusCode(anyOf(is(200), is(404)));
+ }
+ }
+
+ @Test
+ void stackDeletionAbandonsFailedReplacementAfterCleanupExhaustion() throws Exception {
+ String original = "ses-" + Long.toString(System.nanoTime(), 36) + ".example.com";
+ String replacement = "replacement-" + original;
+ stack = "cfn-ses-" + Long.toString(System.nanoTime(), 36);
+ cfn("CreateStack", template(original, false)).then().statusCode(200);
+ awaitStatus("CREATE_COMPLETE");
+ doThrow(new AwsException("ServiceUnavailableException", "temporary read failure", 503))
+ .when(identityService).getIdentityVerificationAttributes(replacement, "us-east-1");
+ doThrow(new AwsException("ServiceUnavailableException", "delete unavailable", 503))
+ .when(sesService).deleteIdentity(replacement, "us-east-1");
+
+ try {
+ cfn("UpdateStack", template(replacement, false)).then().statusCode(200);
+ awaitStatus("UPDATE_ROLLBACK_COMPLETE");
+ doCallRealMethod().when(identityService).getIdentityVerificationAttributes(replacement, "us-east-1");
+ assertEquals(200, sesIdentity(original).statusCode());
+ assertEquals(200, sesIdentity(replacement).statusCode());
+ clearInvocations(sesService);
+
+ cfn("DeleteStack", null).then().statusCode(200);
+ awaitStatus("DELETE_FAILED");
+ assertEquals(404, sesIdentity(original).statusCode());
+ assertEquals(200, sesIdentity(replacement).statusCode());
+ verify(sesService, times(3)).deleteIdentity(replacement, "us-east-1");
+
+ cfn("DeleteStack", null).then().statusCode(200);
+ await().atMost(Duration.ofSeconds(15)).untilAsserted(() ->
+ assertEquals(400, cfn("DescribeStacks", null).statusCode()));
+ assertEquals(200, sesIdentity(replacement).statusCode());
+ verify(sesService, times(3)).deleteIdentity(replacement, "us-east-1");
+ stack = null;
+ } finally {
+ doCallRealMethod().when(identityService).getIdentityVerificationAttributes(replacement, "us-east-1");
+ doCallRealMethod().when(sesService).deleteIdentity(replacement, "us-east-1");
+ given().header("Authorization", SES_AUTH)
+ .delete("/v2/email/identities/{identity}", replacement).then().statusCode(anyOf(is(200), is(404)));
+ }
+ }
+
+ @Test
+ void deletionRetryPreservesAnotherStacksIdentityAtAnExhaustedOrphanAddress() throws Exception {
+ String original = "ses-" + Long.toString(System.nanoTime(), 36) + ".example.com";
+ String replacement = "replacement-" + original;
+ stack = "cfn-ses-history-" + Long.toString(System.nanoTime(), 36);
+ String originalStack = stack;
+ String adoptingStack = "cfn-ses-adopting-" + Long.toString(System.nanoTime(), 36);
+ cfn("CreateStack", template(original, false)).then().statusCode(200);
+ awaitStatus("CREATE_COMPLETE");
+ doThrow(new AwsException("ServiceUnavailableException", "temporary read failure", 503))
+ .when(identityService).getIdentityVerificationAttributes(replacement, "us-east-1");
+ doThrow(new AwsException("ServiceUnavailableException", "delete unavailable", 503))
+ .when(sesService).deleteIdentity(replacement, "us-east-1");
+
+ try {
+ cfn("UpdateStack", template(replacement, false)).then().statusCode(200);
+ awaitStatus("UPDATE_ROLLBACK_COMPLETE");
+ doCallRealMethod().when(identityService).getIdentityVerificationAttributes(replacement, "us-east-1");
+ assertEquals(200, sesIdentity(replacement).statusCode());
+ cfn("DeleteStack", null).then().statusCode(200);
+ awaitStatus("DELETE_FAILED");
+ assertEquals(200, sesIdentity(replacement).statusCode());
+
+ doCallRealMethod().when(sesService).deleteIdentity(replacement, "us-east-1");
+ given().header("Authorization", SES_AUTH)
+ .delete("/v2/email/identities/{identity}", replacement).then().statusCode(200);
+ sesIdentity(replacement).then().statusCode(404);
+
+ stack = adoptingStack;
+ cfn("CreateStack", template(replacement, true)).then().statusCode(200);
+ String adopted = awaitStatus("CREATE_COMPLETE");
+ assertEquals(replacement, XmlParser.extractPairs(adopted,
+ "Outputs", "OutputKey", "OutputValue").get("IdentityRef"));
+ Response claimed = sesIdentity(replacement);
+ claimed.then().statusCode(200);
+ List<String> adoptedTokens = claimed.jsonPath().getList("DkimAttributes.Tokens", String.class);
+ assertEquals("new", claimed.jsonPath().getString("Tags.find { it.Key == 'purpose' }.Value"));
+ clearInvocations(sesService);
+
+ stack = originalStack;
+ cfn("DeleteStack", null).then().statusCode(200);
+ await().atMost(Duration.ofSeconds(15)).untilAsserted(() ->
+ assertEquals(400, cfn("DescribeStacks", null).statusCode()));
+ assertEquals(404, sesIdentity(original).statusCode());
+ stack = adoptingStack;
+ Response stillClaimed = cfn("DescribeStacks", null);
+ stillClaimed.then().statusCode(200);
+ assertEquals("CREATE_COMPLETE", XmlParser.extractFirst(stillClaimed.asString(), "StackStatus", null));
+ Response remaining = sesIdentity(replacement);
+ assertEquals(200, remaining.statusCode(),
+ "An exhausted cleanup must not delete Stack B's replacement identity: " + remaining.asString());
+ assertEquals(adoptedTokens, remaining.jsonPath().getList("DkimAttributes.Tokens", String.class));
+ verify(sesService, never()).deleteIdentity(replacement, "us-east-1");
+ } finally {
+ doCallRealMethod().when(identityService).getIdentityVerificationAttributes(replacement, "us-east-1");
+ doCallRealMethod().when(sesService).deleteIdentity(replacement, "us-east-1");
+ for (String ownedStack : List.of(adoptingStack, originalStack)) {
+ stack = ownedStack;
+ if (cfn("DescribeStacks", null).statusCode() == 200) {
+ cfn("DeleteStack", null).then().statusCode(200);
+ await().atMost(Duration.ofSeconds(15)).untilAsserted(() ->
+ assertEquals(400, cfn("DescribeStacks", null).statusCode()));
+ }
+ }
+ sesIdentity(original).then().statusCode(404);
+ sesIdentity(replacement).then().statusCode(404);
+ stack = null;
+ }
+ }
+
+ private String awaitStatus(String expected) {
+ return await().atMost(Duration.ofSeconds(15)).until(
+ () -> cfn("DescribeStacks", null).then().statusCode(200).extract().asString(),
+ body -> expected.equals(XmlParser.extractFirst(body, "StackStatus", null)));
+ }
+
+ private boolean isOriginalSnapshot(Identity identity, String identityName, List<String> originalTokens) {
+ return identity != null && identityName.equals(identity.getIdentity())
+ && originalTokens.equals(identity.getDkimTokens())
+ && identity.isFeedbackForwardingEnabled() && identity.isDkimEnabled()
+ && "RSA_2048_BIT".equals(identity.getDkimNextSigningKeyLength())
+ && identity.getMailFromDomain() == null
+ && identity.getTags().stream().anyMatch(tag -> "purpose".equals(tag.key()) && "old".equals(tag.value()));
+ }
+
+ private Response cfn(String action, String template) {
+ RequestSpecification request = given().contentType("application/x-www-form-urlencoded")
+ .header("Authorization", stackAuth).formParam("Action", action).formParam("StackName", stack);
+ if (template != null) {
+ request.formParam("TemplateBody", template);
+ }
+ return request.post("/");
+ }
+
+ private Response continueUpdateRollbackSkipping(String logicalId) {
+ return given().contentType("application/x-www-form-urlencoded")
+ .header("Authorization", stackAuth)
+ .formParam("Action", "ContinueUpdateRollback")
+ .formParam("StackName", stack)
+ .formParam("ResourcesToSkip.member.1", logicalId)
+ .post("/");
+ }
+
+ private Response sesIdentity(String identity) {
+ return given().header("Authorization", SES_AUTH).get("/v2/email/identities/{identity}", identity);
+ }
+
+ private Response changeSet(String action, String name, String template) {
+ RequestSpecification request = given().contentType("application/x-www-form-urlencoded")
+ .header("Authorization", stackAuth).formParam("Action", action).formParam("StackName", stack)
+ .formParam("ChangeSetName", name);
+ if (template != null) {
+ request.formParam("ChangeSetType", "UPDATE").formParam("TemplateBody", template);
+ }
+ return request.post("/");
+ }
+
+ private String dependentValue() {
+ return given().header("Authorization", auth("000000000000", "ssm"))
+ .contentType("application/x-amz-json-1.1").header("X-Amz-Target", "AmazonSSM.GetParameter")
+ .body(Map.of("Name", stack + "-identity")).post("/").then().statusCode(200)
+ .extract().jsonPath().getString("Parameter.Value");
+ }
+
+ private String dependentTemplate(String identity, boolean updated) throws Exception {
+ ObjectNode dependent = (ObjectNode) MAPPER.readTree(template(identity, updated));
+ dependent.withObject("/Resources").set("DependentParameter", MAPPER.valueToTree(Map.of(
+ "Type", "AWS::SSM::Parameter", "Properties", Map.of("Name", stack + "-identity",
+ "Type", "String", "Value", Map.of("Ref", "Identity")))));
+ return dependent.toString();
+ }
+
+ private Response sesIdentity(String identity, String account) {
+ return given().header("Authorization", auth(account, "ses"))
+ .get("/v2/email/identities/{identity}", identity);
+ }
+
+ private String auth(String account, String service) {
+ return "AWS4-HMAC-SHA256 Credential=" + account + "/20261002/us-east-1/" + service
+ + "/aws4_request, SignedHeaders=host, Signature=abc";
+ }
+
+ private String retainedTemplate(String identity, boolean updated) throws Exception {
+ ObjectNode retained = (ObjectNode) MAPPER.readTree(template(identity, updated));
+ retained.withObject("/Resources/Identity").put("UpdateReplacePolicy", "Retain");
+ return retained.toString();
+ }
+
+ private String conditionalOptionsTemplate(String identity, boolean include, boolean members) throws Exception {
+ ObjectNode conditional = (ObjectNode) MAPPER.readTree(template(identity, true));
+ conditional.putObject("Conditions").putObject("IncludeOptions")
+ .putArray("Fn::Equals").add("enabled").add(include ? "enabled" : "disabled");
+ ObjectNode properties = conditional.withObject("/Resources/Identity/Properties");
+ for (String name : List.of("DkimSigningAttributes", "DkimAttributes", "MailFromAttributes", "FeedbackAttributes")) {
+ JsonNode value = properties.get(name);
+ if (members) {
+ ObjectNode object = MAPPER.createObjectNode();
+ value.fields().forEachRemaining(field -> object.set(field.getKey(), optionalValue(field.getValue())));
+ properties.set(name, object);
+ } else {
+ properties.set(name, optionalValue(value));
+ }
+ }
+ if (members) {
+ properties.putArray("Tags").addObject().put("Key", "stable").put("Value", "");
+ properties.withArray("Tags").add(optionalValue(MAPPER.valueToTree(Map.of("Key", "purpose", "Value", "new"))));
+ } else {
+ properties.set("Tags", optionalValue(properties.get("Tags")));
+ }
+ return conditional.toString();
+ }
+
+ private ObjectNode optionalValue(JsonNode value) {
+ ObjectNode conditional = MAPPER.createObjectNode();
+ conditional.putArray("Fn::If").add("IncludeOptions").add(value)
+ .add(MAPPER.createObjectNode().put("Ref", "AWS::NoValue"));
+ return conditional;
+ }
+
+ private void assertConditionalOptions(String identityName, boolean include, boolean members) {
+ Response identity = sesIdentity(identityName);
+ assertEquals(200, identity.statusCode(), identity.asString());
+ assertEquals(!include, identity.jsonPath().getBoolean("FeedbackForwardingStatus"));
+ assertEquals(!include, identity.jsonPath().getBoolean("DkimAttributes.SigningEnabled"));
+ assertEquals(include ? "RSA_1024_BIT" : "RSA_2048_BIT",
+ identity.jsonPath().getString("DkimAttributes.NextSigningKeyLength"));
+ assertEquals(include ? "mail." + identityName : null,
+ identity.jsonPath().getString("MailFromAttributes.MailFromDomain"));
+ assertEquals(include ? "REJECT_MESSAGE" : "USE_DEFAULT_VALUE",
+ identity.jsonPath().getString("MailFromAttributes.BehaviorOnMxFailure"));
+ List<Map<String, String>> tags = identity.jsonPath().getList("Tags");
+ assertEquals((include ? 1 : 0) + (members ? 1 : 0), tags.size());
+ if (include) {
+ assertEquals("new", identity.jsonPath().getString("Tags.find { it.Key == 'purpose' }.Value"));
+ }
+ if (members) {
+ assertEquals("", identity.jsonPath().getString("Tags.find { it.Key == 'stable' }.Value"));
+ }
+ }
+
+ private void createIdentityStack(String identityName) throws Exception {
+ cfn("CreateStack", template(identityName, false)).then().statusCode(200);
+ awaitStatus("CREATE_COMPLETE");
+ }
+
+ private String dnsChanges(String changes) {
+ return "<ChangeResourceRecordSetsRequest xmlns=\"https://route53.amazonaws.com/doc/2013-04-01/\">"
+ + "<ChangeBatch><Changes>" + changes
+ + "</Changes></ChangeBatch></ChangeResourceRecordSetsRequest>";
+ }
+
+ private String template(String identity, boolean updated) throws Exception {
+ 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> 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))));
+ }
+ return MAPPER.writeValueAsString(Map.of("Resources",
+ Map.of("Identity", Map.of("Type", "AWS::SES::EmailIdentity", "Properties", properties)),
+ "Outputs", outputs));
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SesCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SesCfnProvisionerTest.java
new file mode 100644
index 0000000000..d0f3a51a72
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SesCfnProvisionerTest.java
@@ -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);
+ }
+}
diff --git a/src/test/resources/cloudformation/supported-resource-types.tsv b/src/test/resources/cloudformation/supported-resource-types.tsv
index 47ae84a79e..386b381781 100644
--- a/src/test/resources/cloudformation/supported-resource-types.tsv
+++ b/src/test/resources/cloudformation/supported-resource-types.tsv
@@ -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
diff --git a/tools/docs/cfn_resource_types.yaml b/tools/docs/cfn_resource_types.yaml
index 647d625b5b..7d1aeb959c 100644
--- a/tools/docs/cfn_resource_types.yaml
+++ b/tools/docs/cfn_resource_types.yaml
@@ -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'