fix(cloudformation): apply stack tags during updates (#5031)

* fix(cloudformation): apply stack tags during updates

* test(cloudformation): cover tags-only and failed stack updates

* fix(cloudformation): distinguish empty tags from omitted tags

* fix(cloudformation): reject implicit CREATE with previous template

* test(cloudformation): validate stack tag updates with AWS SDK

* test(cloudformation): fail on SDK cleanup errors

* fix(cloudformation): allow previous template with omitted type
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-10-05 10:20:35 -04:00
1 parent 523003972e
commit a6df1dbb12
6 files changed
+280 -5

No files matched your search

@@ -0,0 +1,104 @@
package com.floci.test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
import software.amazon.awssdk.services.cloudformation.model.ChangeSetType;
import software.amazon.awssdk.services.cloudformation.model.Stack;
import software.amazon.awssdk.services.cloudformation.model.Tag;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
class CloudFormationStackTagsUpdateTest {
private static final String TEMPLATE = """
{"Resources":{"Queue":{"Type":"AWS::SQS::Queue"}}}
""";
private static CloudFormationClient cloudFormation;
private static String stackName;
@BeforeAll
static void setup() {
cloudFormation = TestFixtures.cloudFormationClient();
stackName = TestFixtures.uniqueName("cfn-stack-tags-sdk");
}
@AfterAll
static void cleanup() {
if (cloudFormation != null) {
try {
cloudFormation.deleteStack(request -> request.stackName(stackName));
} finally {
cloudFormation.close();
}
}
}
@Test
void sdkUpdatesStackTags() throws InterruptedException {
cloudFormation.createStack(request -> request.stackName(stackName)
.templateBody(TEMPLATE)
.tags(tag("Env", "before"), tag("Owner", "original")));
assertThat(awaitStatus("CREATE_COMPLETE")).isEqualTo("CREATE_COMPLETE");
assertThat(tags()).containsExactlyInAnyOrderEntriesOf(
Map.of("Env", "before", "Owner", "original"));
cloudFormation.updateStack(request -> request.stackName(stackName)
.usePreviousTemplate(true)
.tags(tag("Env", "after"), tag("Team", "new")));
assertThat(awaitStatus("UPDATE_COMPLETE")).isEqualTo("UPDATE_COMPLETE");
assertThat(tags()).containsExactlyInAnyOrderEntriesOf(
Map.of("Env", "after", "Team", "new"));
cloudFormation.createChangeSet(request -> request.stackName(stackName)
.changeSetName("tags-only")
.changeSetType(ChangeSetType.UPDATE)
.usePreviousTemplate(true)
.tags(tag("Env", "final")));
assertThat(tags()).containsExactlyInAnyOrderEntriesOf(
Map.of("Env", "after", "Team", "new"));
cloudFormation.executeChangeSet(request -> request.stackName(stackName)
.changeSetName("tags-only"));
assertThat(awaitStatus("UPDATE_COMPLETE")).isEqualTo("UPDATE_COMPLETE");
assertThat(tags()).containsExactlyInAnyOrderEntriesOf(Map.of("Env", "final"));
cloudFormation.updateStack(request -> request.stackName(stackName)
.usePreviousTemplate(true));
assertThat(awaitStatus("UPDATE_COMPLETE")).isEqualTo("UPDATE_COMPLETE");
assertThat(tags()).containsExactlyInAnyOrderEntriesOf(Map.of("Env", "final"));
}
private static Tag tag(String key, String value) {
return Tag.builder().key(key).value(value).build();
}
private static Map<String, String> tags() {
return cloudFormation.describeStacks(request -> request.stackName(stackName))
.stacks().get(0).tags().stream()
.collect(Collectors.toMap(Tag::key, Tag::value));
}
private static String awaitStatus(String expected) throws InterruptedException {
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(30);
while (System.nanoTime() < deadline) {
Stack stack = cloudFormation.describeStacks(request -> request.stackName(stackName))
.stacks().get(0);
String status = stack.stackStatusAsString();
if (expected.equals(status)) {
return status;
}
if (status.endsWith("_FAILED") || status.contains("ROLLBACK")) {
throw new AssertionError(stackName + " reached " + status + ": "
+ stack.stackStatusReason());
}
Thread.sleep(100);
}
throw new AssertionError(stackName + " did not reach " + expected + " within 30 seconds");
}
}
@@ -150,13 +150,14 @@ public class CloudFormationQueryHandler {
Map<String, String> parameters =
extractParameters(params, cfnService.currentParameters(stackName, region));
List<String> capabilities = extractList(params, "Capabilities.member.");
Map<String, String> tags = extractTags(params);
String changeSetName = "update-" + UUID.randomUUID().toString().substring(0, 8);
ChangeSet cs = usePreviousTemplate
? cfnService.createChangeSetFromPreviousTemplate(stackName, changeSetName,
parameters, capabilities, Map.of(), region)
parameters, capabilities, tags, region)
: cfnService.createChangeSet(stackName, changeSetName,
"UPDATE", templateBody, templateUrl, parameters, capabilities, Map.of(), region);
"UPDATE", templateBody, templateUrl, parameters, capabilities, tags, region);
awaitExecution(cfnService.executeChangeSet(stackName, cs.getChangeSetName(), region));
Stack stack = cfnService.describeStacks(stackName, region).get(0);
@@ -723,7 +724,9 @@ public class CloudFormationQueryHandler {
result.put(key, value != null ? value : "");
i++;
}
return result;
boolean supplied = params.keySet().stream().anyMatch(name ->
"Tags".equals(name) || "Tags.member".equals(name) || name.startsWith("Tags.member."));
return supplied ? result : null;
}
private List<String> extractList(MultivaluedMap<String, String> params, String prefix) {
@@ -441,6 +441,7 @@ public class CloudFormationService implements ResourceProvider {
cs.setTemplateBody(resolvedTemplate);
cs.setParameters(parameters);
cs.setCapabilities(capabilities);
cs.setTags(tags);
if (samTransformFailureReason != null) {
cs.setStatus("FAILED");
cs.setExecutionStatus("UNAVAILABLE");
@@ -940,7 +941,7 @@ public class CloudFormationService implements ResourceProvider {
Map<String, String> params = cs.getParameters() != null ? cs.getParameters() : Map.of();
return submitOperation(() -> RequestScopes.runAs(accountId, region, () -> {
executeTemplate(stack, templateBody, params, isCreate, region, accountId);
executeTemplate(stack, templateBody, params, cs.getTags(), isCreate, region, accountId);
String status = stack.getStatus();
cs.setExecutionStatus(status != null && (status.contains("ROLLBACK") || status.endsWith("_FAILED"))
? "EXECUTE_FAILED" : "EXECUTE_COMPLETE");
@@ -1404,6 +1405,7 @@ public class CloudFormationService implements ResourceProvider {
}
private void executeTemplate(Stack stack, String templateBody, Map<String, String> params,
Map<String, String> tags,
boolean isCreate, String region, String accountId) {
StackUpdateSnapshot previousState = snapshotForUpdate(stack);
boolean updateCommitted = false;
@@ -1681,6 +1683,9 @@ public class CloudFormationService implements ResourceProvider {
if (!isCreate) {
updateCommitted = true;
if (tags != null) {
stack.replaceTags(tags);
}
if (hasReplacementUpdates(stack) || hasRemovedOrConditionFalseResources(stack, resources, conditions)) {
stack.setStatus("UPDATE_COMPLETE_CLEANUP_IN_PROGRESS");
stack.setLastUpdatedTime(now());
@@ -2803,7 +2808,7 @@ public class CloudFormationService implements ResourceProvider {
childParams.put(e.getKey(), engine.resolve(e.getValue())));
}
executeTemplate(childStack, childTemplate, childParams, childCreate, region, accountId);
executeTemplate(childStack, childTemplate, childParams, null, childCreate, region, accountId);
resource.setPhysicalId(childStack.getStackId());
resource.getAttributes().put("Arn", childStack.getStackId());
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -20,6 +21,7 @@ public class ChangeSet {
private String templateBody;
private Map<String, String> parameters;
private List<String> capabilities;
private Map<String, String> tags;
private String changeSetType; // CREATE or UPDATE
private Instant creationTime = Instant.now();
@@ -43,6 +45,8 @@ public class ChangeSet {
public void setParameters(Map<String, String> parameters) { this.parameters = parameters; }
public List<String> getCapabilities() { return capabilities; }
public void setCapabilities(List<String> capabilities) { this.capabilities = capabilities; }
public Map<String, String> getTags() { return tags; }
public void setTags(Map<String, String> tags) { this.tags = tags == null ? null : new LinkedHashMap<>(tags); }
public String getChangeSetType() { return changeSetType; }
public void setChangeSetType(String changeSetType) { this.changeSetType = changeSetType; }
public Instant getCreationTime() { return creationTime; }
@@ -158,6 +158,7 @@ public class Stack {
public void replaceOutputs(Map<String, String> source) { replace(outputs, source); }
public void replaceExports(Map<String, String> source) { replace(exports, source); }
public void replaceOutputExportNames(Map<String, String> source) { replace(outputExportNames, source); }
public void replaceTags(Map<String, String> source) { replace(tags, source); }
private static <V> void replace(Map<String, V> target, Map<String, V> source) {
synchronized (target) {
@@ -0,0 +1,158 @@
package io.github.hectorvent.floci.services.cloudformation;
import io.github.hectorvent.floci.core.common.XmlParser;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.Test;
import java.util.Map;
import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
@QuarkusTest
class CloudFormationUpdateStackTagsIntegrationTest {
@Test
void tagsOnlyUpdatesWorkForDirectRequestsAndChangeSets() {
String stackName = "cfn-tags-only-" + UUID.randomUUID().toString().substring(0, 8);
String template = """
{"Resources":{"Queue":{"Type":"AWS::SQS::Queue","Properties":{"QueueName":"%s"}}}}
""".formatted(stackName);
try {
withTags(request("CreateStack", stackName).formParam("TemplateBody", template),
"before", "original")
.when().post("/").then().statusCode(200);
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
withTags(request("UpdateStack", stackName).formParam("TemplateBody", template),
"after", "new")
.when().post("/").then().statusCode(200);
assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
assertEquals(Map.of("Env", "after", "Team", "new"), tags(stackName));
withTags(request("CreateChangeSet", stackName)
.formParam("TemplateBody", template)
.formParam("ChangeSetName", "tags-only")
.formParam("ChangeSetType", "UPDATE"), "final", "latest")
.when().post("/").then().statusCode(200);
assertEquals(Map.of("Env", "after", "Team", "new"), tags(stackName));
executeChangeSet(stackName, "tags-only");
assertEquals(Map.of("Env", "final", "Team", "latest"), tags(stackName));
String failingTemplate = """
{"Resources":{
"Queue":{"Type":"AWS::SQS::Queue","Properties":{"QueueName":"%s"}},
"ZFail":{"Type":"AWS::CloudFormation::Stack","Properties":{}}
}}
""".formatted(stackName);
withTags(request("UpdateStack", stackName).formParam("TemplateBody", failingTemplate),
"failed", "attempt")
.when().post("/").then().statusCode(200);
assertEquals("UPDATE_ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
assertEquals(Map.of("Env", "final", "Team", "latest"), tags(stackName));
} finally {
request("DeleteStack", stackName).when().post("/").then().statusCode(200);
CfnStackWaits.awaitStackDeleted(stackName);
}
}
@Test
void suppliedTagsReplaceStackTagsOnlyWhenTheUpdateExecutes() {
String stackName = "cfn-update-tags-" + UUID.randomUUID().toString().substring(0, 8);
String template = """
{"Parameters":{"Timeout":{"Type":"Number","Default":"30"}},
"Resources":{"Queue":{"Type":"AWS::SQS::Queue","Properties":{
"QueueName":"%s","VisibilityTimeout":{"Ref":"Timeout"}}}}}
""".formatted(stackName);
try {
withTags(request("CreateStack", stackName).formParam("TemplateBody", template),
"before", "original")
.when().post("/").then().statusCode(200);
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
assertEquals(Map.of("Env", "before", "Owner", "original"), tags(stackName));
withTags(updateRequest("UpdateStack", stackName, template, "60"), "after", "new")
.when().post("/").then().statusCode(200);
assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
assertEquals(Map.of("Env", "after", "Team", "new"), tags(stackName));
updateRequest("UpdateStack", stackName, template, "70")
.when().post("/").then().statusCode(200);
assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
assertEquals(Map.of("Env", "after", "Team", "new"), tags(stackName));
withTags(updateRequest("CreateChangeSet", stackName, template, "80")
.formParam("ChangeSetName", "tag-update")
.formParam("ChangeSetType", "UPDATE"), "final", "latest")
.when().post("/").then().statusCode(200);
assertEquals(Map.of("Env", "after", "Team", "new"), tags(stackName));
executeChangeSet(stackName, "tag-update");
assertEquals(Map.of("Env", "final", "Team", "latest"), tags(stackName));
updateRequest("CreateChangeSet", stackName, template, "90")
.formParam("ChangeSetName", "untagged-update")
.formParam("ChangeSetType", "UPDATE")
.when().post("/").then().statusCode(200);
executeChangeSet(stackName, "untagged-update");
assertEquals(Map.of("Env", "final", "Team", "latest"), tags(stackName));
updateRequest("UpdateStack", stackName, template, "100")
.formParam("Tags", "")
.when().post("/").then().statusCode(200);
assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
assertEquals(Map.of(), tags(stackName));
withTags(updateRequest("UpdateStack", stackName, template, "110"), "reset", "again")
.when().post("/").then().statusCode(200);
assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
assertEquals(Map.of("Env", "reset", "Team", "again"), tags(stackName));
updateRequest("CreateChangeSet", stackName, template, "120")
.formParam("ChangeSetName", "clear-tags")
.formParam("ChangeSetType", "UPDATE")
.formParam("Tags.member", "")
.when().post("/").then().statusCode(200);
assertEquals(Map.of("Env", "reset", "Team", "again"), tags(stackName));
executeChangeSet(stackName, "clear-tags");
assertEquals(Map.of(), tags(stackName));
} finally {
request("DeleteStack", stackName).when().post("/").then().statusCode(200);
CfnStackWaits.awaitStackDeleted(stackName);
}
}
private static RequestSpecification request(String action, String stackName) {
return given().contentType("application/x-www-form-urlencoded")
.formParam("Action", action).formParam("StackName", stackName);
}
private static RequestSpecification updateRequest(String action, String stackName,
String template, String timeout) {
return request(action, stackName).formParam("TemplateBody", template)
.formParam("Parameters.member.1.ParameterKey", "Timeout")
.formParam("Parameters.member.1.ParameterValue", timeout);
}
private static RequestSpecification withTags(RequestSpecification request,
String env, String secondValue) {
String secondKey = "before".equals(env) ? "Owner" : "Team";
return request.formParam("Tags.member.1.Key", "Env")
.formParam("Tags.member.1.Value", env)
.formParam("Tags.member.2.Key", secondKey)
.formParam("Tags.member.2.Value", secondValue);
}
private static Map<String, String> tags(String stackName) {
String xml = request("DescribeStacks", stackName).when().post("/")
.then().statusCode(200).extract().asString();
return XmlParser.extractPairs(xml, "member", "Key", "Value");
}
private static void executeChangeSet(String stackName, String changeSetName) {
request("ExecuteChangeSet", stackName).formParam("ChangeSetName", changeSetName)
.when().post("/").then().statusCode(200);
assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
}
}