feat(cloudformation): add optional-value and delete cleanup hooks (#5067)

This commit is contained in:
XuJian authored and GitHub committed 2026-10-09 15:04:03 -04:00
1 parent 233d05eab1
commit 07b529e150
6 files changed
+258 -14

No files matched your search

@@ -1863,6 +1863,10 @@ public class CloudFormationService implements ResourceProvider {
}
private List<UpdateCleanupFailure> finishCommittedResourceCleanup(Stack stack, String region) {
return finishCommittedResourceCleanup(stack, region, false);
}
private List<UpdateCleanupFailure> finishCommittedResourceCleanup(Stack stack, String region, boolean deleting) {
List<UpdateCleanupFailure> failures = new ArrayList<>();
// Dependents go before what they depend on, as when the stack is deleted: a displaced
// listener has to go before the displaced target group it still forwards to, or that
@@ -1883,7 +1887,8 @@ public class CloudFormationService implements ResourceProvider {
null);
}
while (true) {
UpdateCleanupResult result = dispatcher.completeUpdate(resource);
UpdateCleanupResult result = deleting ? dispatcher.completeDeleteCleanup(resource)
: dispatcher.completeUpdate(resource);
if (!result.applicable()) {
break;
}
@@ -1897,7 +1902,11 @@ public class CloudFormationService implements ResourceProvider {
"DELETE_COMPLETE",
null);
}
dispatcher.clearUpdate(resource);
if (deleting) {
dispatcher.clearDeleteCleanup(resource);
} else {
dispatcher.clearUpdate(resource);
}
break;
}
if (result.attempts() < 3) {
@@ -1916,7 +1925,11 @@ public class CloudFormationService implements ResourceProvider {
resource.getResourceType(),
"DELETE_FAILED",
reason);
dispatcher.clearUpdate(resource);
if (deleting) {
dispatcher.clearDeleteCleanup(resource);
} else {
dispatcher.clearUpdate(resource);
}
break;
}
}
@@ -2350,7 +2363,7 @@ public class CloudFormationService implements ResourceProvider {
// last update left in place. An entity displaced by a replacement whose cleanup phase
// never ended is named only by the cleanup the resource still carries, so the stack
// deletes that one too: nothing else ever will.
for (UpdateCleanupFailure displacedFailure : finishCommittedResourceCleanup(stack, region)) {
for (UpdateCleanupFailure displacedFailure : finishCommittedResourceCleanup(stack, region, true)) {
failedResources.add(displacedFailure.logicalId());
}
for (StackResource resource : resources) {
@@ -3,6 +3,7 @@ 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.ArrayNode;
import com.fasterxml.jackson.databind.node.MissingNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import io.github.hectorvent.floci.core.common.AwsException;
@@ -207,12 +208,10 @@ public class CloudFormationTemplateEngine {
if (node.has("Fn::Split") || node.has("Fn::GetAZs") || node.has("Fn::Cidr")) {
return objectMapper.valueToTree(resolveList(node));
}
if (node.has("Ref") || node.has("Fn::Sub") || node.has("Fn::Join") ||
node.has("Fn::Select") || node.has("Fn::Base64") ||
node.has("Fn::GetAtt") || node.has("Fn::ImportValue") || node.has("Fn::FindInMap")) {
if (isIntrinsic(node)) {
return TextNode.valueOf(resolve(node));
}
// Plain object — resolve each field
// Plain object: resolve each field.
ObjectNode resolved = objectMapper.createObjectNode();
Iterator<Map.Entry<String, JsonNode>> fields = node.fields();
while (fields.hasNext()) {
@@ -231,6 +230,60 @@ public class CloudFormationTemplateEngine {
return node;
}
/** Resolves optional structured properties, omitting only selected AWS::NoValue expressions. */
public JsonNode resolveNodeOmittingNoValue(JsonNode node) {
return resolveNode(omitNoValue(node));
}
private JsonNode omitNoValue(JsonNode node) {
if (node == null || node.isNull() || node.isMissingNode()) {
return node;
}
if (node.isObject()) {
if (node.size() == 1 && "AWS::NoValue".equals(node.path("Ref").asText())) {
return MissingNode.getInstance();
}
if (node.size() == 1 && node.has("Fn::If")) {
JsonNode branch = selectIfBranch(node.get("Fn::If"));
if (branch != null) {
return omitNoValue(branch);
}
}
if (isIntrinsic(node)) {
return node;
}
ObjectNode resolved = objectMapper.createObjectNode();
Iterator<Map.Entry<String, JsonNode>> fields = node.fields();
while (fields.hasNext()) {
Map.Entry<String, JsonNode> field = fields.next();
JsonNode value = omitNoValue(field.getValue());
if (!value.isMissingNode()) {
resolved.set(field.getKey(), value);
}
}
return resolved;
}
if (node.isArray()) {
ArrayNode resolved = objectMapper.createArrayNode();
for (JsonNode item : node) {
JsonNode value = omitNoValue(item);
if (!value.isMissingNode()) {
resolved.add(value);
}
}
return resolved;
}
return node;
}
private static boolean isIntrinsic(JsonNode node) {
return node.has("Ref") || node.has("Fn::Sub") || node.has("Fn::Join")
|| node.has("Fn::Select") || node.has("Fn::Base64") || node.has("Fn::GetAtt")
|| node.has("Fn::ImportValue") || node.has("Fn::FindInMap")
|| node.has("Fn::Split") || node.has("Fn::GetAZs") || node.has("Fn::Cidr")
|| node.has("Fn::If");
}
/**
* Resolves a value against {@link #dynamicReferenceResolver} when it carries CloudFormation
* dynamic reference syntax ({@code {{resolve:ssm:...}}}, {@code {{resolve:secretsmanager:...}}}).
@@ -238,6 +238,19 @@ public class CfnResourceDispatcher {
.orElseGet(UpdateCleanupResult::notApplicable);
}
/** Cleanup owed by an unfinished update while deleting the enclosing stack. */
public UpdateCleanupResult completeDeleteCleanup(StackResource resource) {
return registry.forType(resource.getResourceType())
.map(owner -> owner.completeDeleteCleanup(resource))
.filter(UpdateCleanupResult::applicable)
.orElseGet(UpdateCleanupResult::notApplicable);
}
/** Clears completed replacement cleanup without assuming a stack update committed. */
public void clearDeleteCleanup(StackResource resource) {
registry.forType(resource.getResourceType()).ifPresent(owner -> owner.clearDeleteCleanup(resource));
}
/**
* The physical id this update displaced, announced as DELETE_IN_PROGRESS before the stack
* update closes. A type whose {@code UpdateReplacePolicy} is {@code Retain} owes no cleanup.
@@ -78,6 +78,15 @@ public interface CfnResourceProvisioner {
return UpdateCleanupResult.notApplicable();
}
/**
* Deletes entities displaced by an unfinished update before the stack itself is deleted.
* The default uses normal replacement cleanup. A provisioner may distinguish this operation
* from committing an update when it still carries configuration needed for rollback.
*/
default UpdateCleanupResult completeDeleteCleanup(StackResource resource) {
return completeUpdate(resource);
}
/**
* Whether this update replaced the physical entity, so the stack has cleanup pending and enters
* UPDATE_COMPLETE_CLEANUP_IN_PROGRESS. The default reports no replacement.
@@ -94,6 +103,15 @@ public interface CfnResourceProvisioner {
// no-op by default: a type with no replacement cleanup records nothing to clear
}
/**
* Drops completed replacement cleanup during stack deletion. The default preserves the normal
* cleanup behavior; an override may keep rollback configuration until the managed entity is
* successfully deleted.
*/
default void clearDeleteCleanup(StackResource resource) {
clearUpdate(resource);
}
/**
* Carries additive ownership tracking a failed update discovered onto the last known-good
* resource metadata CloudFormation restores, so entities the failed attempt created but could
@@ -23,15 +23,19 @@ import java.time.Clock;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
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.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.RETURNS_DEFAULTS;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
@@ -139,11 +143,13 @@ class CloudFormationServiceRollbackTest {
for (StackResource resource : new StackResource[] {role, logGroup, alreadyDeleted, adopted, owned}) {
stack.getResources().put(resource.getLogicalId(), resource);
}
when(provisioner.completeUpdate(any())).thenReturn(UpdateCleanupResult.notApplicable());
when(provisioner.completeDeleteCleanup(any())).thenReturn(UpdateCleanupResult.notApplicable());
service.deleteStackResources(stack, REGION, ACCOUNT);
assertEquals("DELETE_COMPLETE", stack.getStatus());
verify(provisioner).completeDeleteCleanup(role);
verify(provisioner, never()).completeUpdate(any());
verify(provisioner).delete(role, REGION);
verify(provisioner).delete(logGroup, REGION);
verify(provisioner).delete(owned, REGION);
@@ -165,7 +171,7 @@ class CloudFormationServiceRollbackTest {
StackResource bucket = resource("Bucket", "leak-probe-bucket", "AWS::S3::Bucket", "UPDATE_FAILED");
stack.getResources().put(bucket.getLogicalId(), bucket);
when(provisioner.completeUpdate(any())).thenReturn(UpdateCleanupResult.notApplicable());
when(provisioner.completeDeleteCleanup(any())).thenReturn(UpdateCleanupResult.notApplicable());
doThrow(new AwsException("BucketNotEmpty", "The bucket you tried to delete is not empty", 409))
.when(provisioner).delete(eq(bucket), eq(REGION));
@@ -224,7 +230,7 @@ class CloudFormationServiceRollbackTest {
for (StackResource resource : new StackResource[] {listener, firstQueue, secondQueue, targetGroup}) {
stack.getResources().put(resource.getLogicalId(), resource);
}
when(provisioner.completeUpdate(any())).thenReturn(UpdateCleanupResult.notApplicable());
when(provisioner.completeDeleteCleanup(any())).thenReturn(UpdateCleanupResult.notApplicable());
service.deleteStackResources(stack, REGION, ACCOUNT);
@@ -261,7 +267,7 @@ class CloudFormationServiceRollbackTest {
for (StackResource resource : new StackResource[] {listener, firstQueue, secondQueue, targetGroup}) {
stack.getResources().put(resource.getLogicalId(), resource);
}
when(provisioner.completeUpdate(any())).thenReturn(UpdateCleanupResult.notApplicable());
when(provisioner.completeDeleteCleanup(any())).thenReturn(UpdateCleanupResult.notApplicable());
service.deleteStackResources(stack, REGION, ACCOUNT);
@@ -295,7 +301,7 @@ class CloudFormationServiceRollbackTest {
for (StackResource resource : new StackResource[] {consumer, partner, firstQueue, secondQueue}) {
stack.getResources().put(resource.getLogicalId(), resource);
}
when(provisioner.completeUpdate(any())).thenReturn(UpdateCleanupResult.notApplicable());
when(provisioner.completeDeleteCleanup(any())).thenReturn(UpdateCleanupResult.notApplicable());
service.deleteStackResources(stack, REGION, ACCOUNT);
@@ -331,7 +337,7 @@ class CloudFormationServiceRollbackTest {
StackResource secondQueue = resource("SecondQueue", "second-url", "AWS::SQS::Queue", "CREATE_COMPLETE");
stack.getResources().put(firstQueue.getLogicalId(), firstQueue);
stack.getResources().put(secondQueue.getLogicalId(), secondQueue);
when(provisioner.completeUpdate(any())).thenReturn(UpdateCleanupResult.notApplicable());
when(provisioner.completeDeleteCleanup(any())).thenReturn(UpdateCleanupResult.notApplicable());
AtomicReference<Throwable> failure = new AtomicReference<>();
Thread deleter = new Thread(null, () -> {
@@ -352,6 +358,72 @@ class CloudFormationServiceRollbackTest {
deletes.verify(provisioner).delete(firstQueue, REGION);
}
@Test
void failedStackDeletePreservesRollbackMetadataUntilTheResourceDeleteSucceeds() {
Stack stack = new Stack();
stack.setStackName("delete-keeps-rollback-state");
stack.setStackId("stack-id");
stack.setRegion(REGION);
stack.setStatus("UPDATE_ROLLBACK_FAILED");
StackResource resource = resource("Stateful", "current-id", "AWS::Test::Stateful", "UPDATE_FAILED");
resource.getAttributes().put("rollbackSnapshot", "original-configuration");
resource.getAttributes().put("cleanupPending", "true");
stack.getResources().put(resource.getLogicalId(), resource);
CfnResourceDispatcher cleanupDispatcher = mock(CfnResourceDispatcher.class, invocation ->
switch (invocation.getMethod().getName()) {
case "updateCleanupPhysicalId" -> resource.getAttributes().containsKey("cleanupPending")
? "displaced-id" : null;
case "completeUpdate" -> {
resource.getAttributes().remove("rollbackSnapshot");
yield new UpdateCleanupResult(true, true, "displaced-id", 0, null);
}
case "completeDeleteCleanup" -> resource.getAttributes().containsKey("cleanupPending")
? new UpdateCleanupResult(true, true, "displaced-id", 0, null)
: UpdateCleanupResult.notApplicable();
case "clearUpdate" -> {
resource.getAttributes().remove("rollbackSnapshot");
resource.getAttributes().remove("cleanupPending");
yield null;
}
case "clearDeleteCleanup" -> {
resource.getAttributes().remove("cleanupPending");
yield null;
}
default -> RETURNS_DEFAULTS.answer(invocation);
});
AtomicInteger attempts = new AtomicInteger();
doAnswer(invocation -> {
if (attempts.incrementAndGet() == 1) {
throw new AwsException("ServiceUnavailable", "temporary current-resource deletion failure", 503);
}
assertEquals("original-configuration", resource.getAttributes().get("rollbackSnapshot"));
resource.getAttributes().remove("rollbackSnapshot");
return null;
}).when(cleanupDispatcher).delete(resource, REGION);
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
when(config.defaultAccountId()).thenReturn(ACCOUNT);
CloudFormationService deleteService = new CloudFormationService(cleanupDispatcher, mock(S3Service.class),
mock(SsmService.class), mock(CfnDynamicReferences.class), new ObjectMapper(), config,
mock(RegionResolver.class), Clock.systemUTC(), new InMemoryStorageFactory());
assertThrows(IllegalStateException.class, () -> deleteService.deleteStackResources(stack, REGION, ACCOUNT));
assertEquals("DELETE_FAILED", stack.getStatus());
assertEquals("DELETE_FAILED", resource.getStatus());
assertEquals("original-configuration", resource.getAttributes().get("rollbackSnapshot"));
assertNull(resource.getAttributes().get("cleanupPending"));
assertTrue(stack.getEvents().stream().anyMatch(event -> "Stateful".equals(event.getLogicalResourceId())
&& "displaced-id".equals(event.getPhysicalResourceId())
&& "DELETE_COMPLETE".equals(event.getResourceStatus())));
deleteService.deleteStackResources(stack, REGION, ACCOUNT);
assertEquals(2, attempts.get());
assertEquals("DELETE_COMPLETE", stack.getStatus());
assertEquals("DELETE_COMPLETE", resource.getStatus());
assertNull(resource.getAttributes().get("rollbackSnapshot"));
}
private static StackResource resource(String logicalId, String physicalId, String resourceType, String status) {
StackResource resource = new StackResource();
resource.setLogicalId(logicalId);
@@ -37,6 +37,81 @@ class CloudFormationTemplateEngineTest {
}
}
@Test
void selectedNoValueOmitsTheOptionalPropertyWhileKeepingLiteralEmptyText() {
JsonNode source = json("""
{"Scopes":{"Fn::If":["Include",[],{"Ref":"AWS::NoValue"}]},"Name":""}
""");
assertEquals(json("{\"Name\":\"\"}"),
engineWithCondition("Include", false).resolveNodeOmittingNoValue(source));
}
@Test
void selectedNoValueOmitsTheOptionalArrayEntryWhileKeepingLiteralEmptyText() {
JsonNode source = json("""
["first",{"Fn::If":["Include","read",{"Ref":"AWS::NoValue"}]},"","last"]
""");
assertEquals(json("[\"first\",\"\",\"last\"]"),
engineWithCondition("Include", false).resolveNodeOmittingNoValue(source));
}
@Test
void optionalNodeResolutionPreservesTheLegacyNoValueContract() {
JsonNode noValue = json("{\"Ref\":\"AWS::NoValue\"}");
assertEquals("", engine().resolveNode(noValue).asText());
assertTrue(engine().resolveNodeOmittingNoValue(noValue).isMissingNode());
assertEquals(json("\"\""), engine().resolveNodeOmittingNoValue(json("\"\"")));
assertEquals(json("\"AWS::NoValue\""), engine().resolveNodeOmittingNoValue(json("\"AWS::NoValue\"")));
}
@Test
void optionalNodeResolutionDoesNotRewriteOtherIntrinsicArguments() {
JsonNode joined = json("""
{"Fn::Join": ["-", ["read", {"Ref": "AWS::NoValue"}, "all"]]}
""");
assertEquals("read--all", engine().resolveNodeOmittingNoValue(joined).asText());
JsonNode malformedConditional = json("{\"Fn::If\":[\"IncludeScopes\"]}");
assertEquals(engine().resolveNode(malformedConditional),
engine().resolveNodeOmittingNoValue(malformedConditional));
assertEquals(json("{\"keep\":\"\"}"), engine().resolveNodeOmittingNoValue(
json("{\"keep\":{\"Ref\":\"AWS::NoValue\",\"extra\":true}}")));
}
@ParameterizedTest
@ValueSource(booleans = {true, false})
void optionalNodeResolutionOmitsSelectedNestedValuesWithoutMutatingInput(boolean include) {
CloudFormationTemplateEngine e = engineWithCondition("Include", include);
JsonNode source = json("""
{"nested":{"optional":{"Fn::If":["Include",{"list":[0,false,""]},{"Ref":"AWS::NoValue"}]},
"empty":"","literal":"AWS::NoValue","nil":null},
"items":["first",{"Fn::If":["Include",{"Ref":"AWS::Region"},{"Ref":"AWS::NoValue"}]},"last"]}
""");
JsonNode before = source.deepCopy();
JsonNode expected = json(include ? """
{"nested":{"optional":{"list":[0,false,""]},"empty":"","literal":"AWS::NoValue","nil":null},
"items":["first","us-east-1","last"]}
""" : """
{"nested":{"empty":"","literal":"AWS::NoValue","nil":null},"items":["first","last"]}
""");
assertEquals(expected, e.resolveNodeOmittingNoValue(source));
assertEquals(before, source);
}
@Test
void optionalNodeResolutionOnlyResolvesTheSelectedDynamicReferenceOnce() {
List<String> calls = new ArrayList<>();
CloudFormationTemplateEngine e = new CloudFormationTemplateEngine("000000000000",
"us-east-1", "my-stack", "stack/id", Map.of(), Map.of(), Map.of(),
Map.of("Include", true), Map.of(), mapper, name -> null, value -> {
calls.add(value);
return "{{resolve:ssm:/literal-result}}";
});
assertEquals("{{resolve:ssm:/literal-result}}", e.resolveNodeOmittingNoValue(json("""
{"Fn::If":["Include","{{resolve:ssm:/selected}}","{{resolve:ssm:/unselected}}"]}
""")).asText());
assertEquals(List.of("{{resolve:ssm:/selected}}"), calls);
}
@Test
void joinAcceptsSplitAsItsListOfValues() {
assertEquals("x|y|z", engine().resolve(json("""