fix(cloudformation): fill change set Scope and Details for modified resources (#9137)

DescribeChangeSet returned empty Scope and Details for every change, so a
change set preview such as cdk diff could not show a property difference that
only resolves at deploy time. A Modify the template diff finds now lists one
ResourceChangeDetail per changed property (Tags as its own target) and per
changed Metadata, DeletionPolicy, UpdateReplacePolicy or UpdatePolicy: a
written edit is a Static DirectModification, a change that shows only once
parameters or conditions apply a Dynamic one, and each changed parameter the
value reads adds a Static ParameterReference. RequiresRecreation follows the
replacement decision for the create-only pointers at and under each property.

Details are computed for DescribeChangeSet only, never during execution. Add
and Remove changes, an unedited nested stack and a resource changed only
through a replaced resource keep empty Scope and Details.
This commit is contained in:
Oleksandr Nikolaichuk authored and GitHub committed 2026-11-21 10:15:37 -04:00
1 parent 347e2abd89
commit c2640aaa22
3 files changed
+314 -32

No files matched your search

@@ -5,6 +5,7 @@ import io.floci.aws.services.ses.SesReceiptRuleService;
import io.floci.aws.testing.CfnStackWaits;
import io.floci.aws.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.specification.RequestSpecification;
import jakarta.inject.Inject;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
@@ -15,6 +16,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static io.floci.aws.testing.CfnRequests.cfn;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
@@ -1101,33 +1103,152 @@ class CloudFormationChangeSetDiffIntegrationTest {
/** Creates an UPDATE change set and returns the Replacement value per logical id. */
private Map<String, String> previewReplacement(String stackName, String changeSetName, String template) {
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateChangeSet")
.formParam("StackName", stackName)
.formParam("ChangeSetName", changeSetName)
.formParam("ChangeSetType", "UPDATE")
.formParam("TemplateBody", template)
.when()
.post("/")
.then()
.statusCode(200);
String desc = given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DescribeChangeSet")
.formParam("StackName", stackName)
.formParam("ChangeSetName", changeSetName)
.when()
.post("/")
.then()
.statusCode(200)
.extract().body().asString();
Map<String, String> result = new HashMap<>();
for (Map<String, String> change : XmlParser.extractGroups(desc, "ResourceChange")) {
for (Map<String, String> change : XmlParser.extractGroups(
describeChangeSet(stackName, changeSetName, template, null), "ResourceChange")) {
result.put(change.get("LogicalResourceId"), change.get("Replacement"));
}
return result;
}
/** Creates an UPDATE change set (with one "Name=value" parameter when given) and returns the DescribeChangeSet body. */
private String describeChangeSet(String stackName, String changeSetName, String template, String param) {
RequestSpecification create = cfn("CreateChangeSet", stackName)
.formParam("ChangeSetName", changeSetName)
.formParam("ChangeSetType", "UPDATE")
.formParam("TemplateBody", template);
if (param != null) {
create.formParam("Parameters.member.1.ParameterKey", param.split("=")[0])
.formParam("Parameters.member.1.ParameterValue", param.split("=")[1]);
}
create.when().post("/").then().statusCode(200);
return cfn("DescribeChangeSet", stackName)
.formParam("ChangeSetName", changeSetName)
.when().post("/")
.then().statusCode(200)
.extract().body().asString();
}
private record DetailsRow(String name, String type, String oldProps, String newProps, String newParam,
List<String> scope, List<String> details, String replacement, boolean exact) { }
/** Details are "Attribute|Name|RequiresRecreation|ChangeSource|Evaluation|CausingEntity", empty when absent. */
private static String template(String type, String props, String... extraResources) {
return "{\"Parameters\":{\"P\":{\"Type\":\"String\",\"Default\":\"v1\"},\"Env\":{\"Type\":\"String\",\"Default\":\"prod\"}},\"Conditions\":{\"C\":{\"Fn::Equals\":[{\"Ref\":\"Env\"},\"prod\"]}},\"Resources\":{\"R\":{\"Type\":\""
+ type + "\",\"Properties\":{" + props + "}}" + String.join("", extraResources) + "}}";
}
private static String healthCheck(String type) {
return "\"HealthCheckConfig\":{\"Type\":\"" + type + "\",\"IPAddress\":\"192.0.2.10\",\"Port\":80}";
}
private static String text(XmlParser.XmlElement parent, String child) {
XmlParser.XmlElement e = parent == null ? null : parent.child(child);
return e == null ? "" : e.text();
}
private static XmlParser.XmlElement resourceChange(String body, String logicalId) {
for (XmlParser.XmlElement member : XmlParser.parseDocumentElementTree(body)
.child("DescribeChangeSetResult").child("Changes").children()) {
XmlParser.XmlElement rc = member.child("ResourceChange");
if (logicalId.equals(text(rc, "LogicalResourceId"))) {
return rc;
}
}
return null;
}
private static List<String> detailsOf(XmlParser.XmlElement rc) {
List<String> result = new ArrayList<>();
for (XmlParser.XmlElement d : rc.child("Details").children()) {
XmlParser.XmlElement t = d.child("Target");
result.add(String.join("|", text(t, "Attribute"), text(t, "Name"), text(t, "RequiresRecreation"),
text(d, "ChangeSource"), text(d, "Evaluation"), text(d, "CausingEntity")));
}
return result.stream().sorted().toList();
}
/**
* Expected values: AWS DescribeChangeSet samples (direct Tags edit, parameter change giving a
* DirectModification/Dynamic and a ParameterReference/Static detail) and the ResourceChangeDetail
* reference (Name only for Properties; Replacement is the most intrusive RequiresRecreation).
*/
@Test
void modifiedResource_reportsScopeAndDetails() {
String direct = "|DirectModification|Static|";
String ifSet = "{\"Fn::If\":[\"C\",5,{\"Ref\":\"AWS::NoValue\"}]}";
List<DetailsRow> rows = List.of(
new DetailsRow("changed", "AWS::SNS::Topic", "\"DisplayName\":\"a\"", "\"DisplayName\":\"b\"", null,
List.of("Properties"), List.of("Properties|DisplayName|Never" + direct), "False", true),
new DetailsRow("added", "AWS::SNS::Topic", "\"TopicName\":\"cs-det-added\"",
"\"TopicName\":\"cs-det-added\",\"DisplayName\":\"b\"", null,
List.of("Properties"), List.of("Properties|DisplayName|Never" + direct), "False", true),
new DetailsRow("dropped", "AWS::SNS::Topic", "\"TopicName\":\"cs-det-dropped\",\"DisplayName\":\"a\"",
"\"TopicName\":\"cs-det-dropped\"", null,
List.of("Properties"), List.of("Properties|DisplayName|Never" + direct), "False", true),
new DetailsRow("create-only", "AWS::SQS::Queue", "\"QueueName\":\"cs-det-q-a\"", "\"QueueName\":\"cs-det-q-b\"",
null, List.of("Properties"), List.of("Properties|QueueName|Always" + direct), "True", true),
new DetailsRow("tags", "AWS::SQS::Queue",
"\"QueueName\":\"cs-det-tags\",\"Tags\":[{\"Key\":\"k\",\"Value\":\"v1\"}]",
"\"QueueName\":\"cs-det-tags\",\"Tags\":[{\"Key\":\"k\",\"Value\":\"v2\"}]", null,
List.of("Tags"), List.of("Tags||Never" + direct), "False", true),
new DetailsRow("parameter", "AWS::SNS::Topic", "\"DisplayName\":{\"Ref\":\"P\"}",
"\"DisplayName\":{\"Ref\":\"P\"}", "P=v2", List.of("Properties"),
List.of("Properties|DisplayName|Never|DirectModification|Dynamic|",
"Properties|DisplayName|Never|ParameterReference|Static|P"), "False", true),
new DetailsRow("no-value", "AWS::SNS::Topic", "\"DisplayName\":\"a\"",
"\"DisplayName\":\"b\",\"KmsMasterKeyId\":{\"Ref\":\"AWS::NoValue\"}", null,
List.of("Properties"), List.of("Properties|DisplayName|Never" + direct), "False", true),
new DetailsRow("edited-and-parameter", "AWS::SQS::Queue",
"\"QueueName\":\"cs-det-both\",\"Tags\":[{\"Key\":\"k\",\"Value\":{\"Ref\":\"P\"}}]",
"\"QueueName\":\"cs-det-both\",\"Tags\":[{\"Key\":\"k2\",\"Value\":{\"Ref\":\"P\"}}]", "P=v2",
List.of("Tags"), List.of("Tags||Never" + direct, "Tags||Never|ParameterReference|Static|P"), "False", true),
new DetailsRow("nested-create-only", "AWS::Route53::HealthCheck", healthCheck("HTTP"), healthCheck("HTTPS"), null,
List.of("Properties"), List.of("Properties|HealthCheckConfig|Always" + direct), "True", true),
new DetailsRow("condition", "AWS::SQS::Queue", "\"QueueName\":\"cs-det-cond\",\"DelaySeconds\":" + ifSet,
"\"QueueName\":\"cs-det-cond\",\"DelaySeconds\":" + ifSet, "Env=dev", List.of("Properties"),
List.of("Properties|DelaySeconds|Never|DirectModification|Dynamic|"), "False", false));
List<String> failures = new ArrayList<>();
for (DetailsRow row : rows) {
String stackName = "cs-details-" + row.name();
stacksToDelete.add(stackName);
createStack(stackName, template(row.type(), row.oldProps()));
XmlParser.XmlElement rc = resourceChange(describeChangeSet(stackName, row.name() + "-cs",
template(row.type(), row.newProps()), row.newParam()), "R");
List<String> scope = rc.child("Scope").children().stream().map(XmlParser.XmlElement::text).sorted().toList();
List<String> details = detailsOf(rc);
boolean recreates = row.details().stream().anyMatch(d -> d.contains("|Always|"));
boolean staticDirect = details.stream().anyMatch(d -> d.endsWith("|DirectModification|Static|"));
boolean detailsOk = row.exact() ? details.equals(row.details().stream().sorted().toList())
: details.containsAll(row.details()) && !staticDirect;
if (!scope.equals(row.scope()) || !detailsOk
|| !row.replacement().equals(text(rc, "Replacement")) || recreates != "True".equals(row.replacement())) {
failures.add(row.name() + ": scope=" + scope + " details=" + details + " replacement=" + text(rc, "Replacement"));
}
}
assertEquals(List.of(), failures);
}
/** ResourceChange.Scope and Details are documented "for the Modify action" only. */
@Test
void addedAndRemovedResources_haveNoScopeAndNoDetails() {
String stackName = "cs-details-add-remove";
stacksToDelete.add(stackName);
String gone = ",\"Gone\":{\"Type\":\"AWS::SNS::Topic\",\"Properties\":{\"TopicName\":\"cs-det-gone\"}}";
String fresh = ",\"Fresh\":{\"Type\":\"AWS::SNS::Topic\",\"Properties\":{\"TopicName\":\"cs-det-fresh\"}}";
createStack(stackName, template("AWS::SNS::Topic", "\"TopicName\":\"cs-det-keep\"", gone));
String body = describeChangeSet(stackName, "add-remove-cs",
template("AWS::SNS::Topic", "\"TopicName\":\"cs-det-keep\"", fresh), null);
XmlParser.XmlElement removed = resourceChange(body, "Gone");
XmlParser.XmlElement added = resourceChange(body, "Fresh");
assertEquals("Remove", text(removed, "Action"));
assertEquals("Add", text(added, "Action"));
for (XmlParser.XmlElement rc : List.of(removed, added)) {
assertEquals(0, rc.child("Scope") == null ? 0 : rc.child("Scope").children().size());
assertEquals(0, rc.child("Details") == null ? 0 : rc.child("Details").children().size());
}
}
}