mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(cloudformation): compute accurate changeset resource diffs
computeChangeSetChanges never read cs.getParameters(), so an update that only changed parameter values reported an empty diff and CDK skipped execution, leaving the old parameter-derived config deployed. It also diffed a SAM change set's raw template against the stack's expanded deployed template, reporting spurious Modify/Remove on every SAM-generated resource for a no-op update, and hardcoded Replacement=False even when a resource's Type changed. Now the diff expands a SAM change set's template before comparing, marks a resource Modify when it references a changed parameter, and reports Replacement=True when a resource's Type changes.
This commit is contained in:
2 files changed
+265
-4
No files matched your search
+40
-4
@@ -311,22 +311,44 @@ public class CloudFormationService implements ResourceProvider {
|
||||
public List<ResourceChange> computeChangeSetChanges(ChangeSet cs, String region) {
|
||||
Stack stack = getStackOrThrow(cs.getStackName(), region);
|
||||
try {
|
||||
JsonNode newResources = parseTemplate(cs.getTemplateBody()).path("Resources");
|
||||
JsonNode newTemplate = parseTemplate(cs.getTemplateBody());
|
||||
if (samTransformProcessor.hasSamTransform(newTemplate)) {
|
||||
// The deployed stack's template is always the SAM-expanded form (see
|
||||
// executeTemplate); comparing the change set's raw SAM source against it would
|
||||
// report every SAM-generated resource as Add/Remove even on a no-op update.
|
||||
newTemplate = samTransformProcessor.expandSamTemplate(newTemplate);
|
||||
}
|
||||
JsonNode newResources = newTemplate.path("Resources");
|
||||
boolean createType = "CREATE".equalsIgnoreCase(cs.getChangeSetType())
|
||||
|| stack.getTemplateBody() == null;
|
||||
JsonNode oldResources = createType
|
||||
? objectMapper.createObjectNode()
|
||||
: parseTemplate(stack.getTemplateBody()).path("Resources");
|
||||
|
||||
Map<String, String> oldParams = stack.getParameters() != null
|
||||
? stack.getParameters() : Map.of();
|
||||
Map<String, String> newParams = cs.getParameters() != null
|
||||
? cs.getParameters() : Map.of();
|
||||
Set<String> changedParams = new HashSet<>();
|
||||
newParams.forEach((k, v) -> {
|
||||
if (!Objects.equals(v, oldParams.get(k))) {
|
||||
changedParams.add(k);
|
||||
}
|
||||
});
|
||||
|
||||
List<ResourceChange> changes = new ArrayList<>();
|
||||
newResources.fields().forEachRemaining(e -> {
|
||||
String logicalId = e.getKey();
|
||||
String resourceType = e.getValue().path("Type").asText();
|
||||
JsonNode newDef = e.getValue();
|
||||
String resourceType = newDef.path("Type").asText();
|
||||
JsonNode oldDef = oldResources.get(logicalId);
|
||||
if (oldDef == null) {
|
||||
changes.add(new ResourceChange("Add", logicalId, null, resourceType, null));
|
||||
} else if (!oldDef.equals(e.getValue())) {
|
||||
} else if (!oldDef.equals(newDef) || referencesAnyParameter(newDef, changedParams)) {
|
||||
boolean typeChanged = !oldDef.path("Type").asText().equals(resourceType);
|
||||
changes.add(new ResourceChange("Modify", logicalId,
|
||||
resourcePhysicalId(stack, logicalId), resourceType, "False"));
|
||||
resourcePhysicalId(stack, logicalId), resourceType,
|
||||
typeChanged ? "True" : "False"));
|
||||
}
|
||||
});
|
||||
oldResources.fields().forEachRemaining(e -> {
|
||||
@@ -346,6 +368,20 @@ public class CloudFormationService implements ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
/** True if a resource's definition references any of the given (changed) parameter names. */
|
||||
private boolean referencesAnyParameter(JsonNode resourceDef, Set<String> parameterNames) {
|
||||
if (parameterNames.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
String json = resourceDef.toString();
|
||||
for (String name : parameterNames) {
|
||||
if (json.contains("\"Ref\":\"" + name + "\"") || json.contains("${" + name + "}")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public record ResourceChange(String action, String logicalResourceId, String physicalResourceId,
|
||||
String resourceType, String replacement) {}
|
||||
|
||||
|
||||
+225
@@ -0,0 +1,225 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
/**
|
||||
* Covers three ways {@code computeChangeSetChanges} previously misreported a change set's diff:
|
||||
* ignoring parameter-only updates, comparing a SAM stack's raw template against its expanded
|
||||
* deployed template, and hardcoding {@code Replacement=False} even when a Type change forces a
|
||||
* replacement.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class CloudFormationChangeSetDiffIntegrationTest {
|
||||
|
||||
private final List<String> stacksToDelete = new ArrayList<>();
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void deleteStacks() {
|
||||
for (String stackName : stacksToDelete) {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DeleteStack")
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/");
|
||||
}
|
||||
stacksToDelete.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
void parameterOnlyUpdate_isReportedAsAChange() {
|
||||
String stackName = "cs-diff-param-only-stack";
|
||||
stacksToDelete.add(stackName);
|
||||
|
||||
String template = """
|
||||
{
|
||||
"Parameters": {
|
||||
"Suffix": {"Type": "String"}
|
||||
},
|
||||
"Resources": {
|
||||
"Q": {
|
||||
"Type": "AWS::SQS::Queue",
|
||||
"Properties": {
|
||||
"QueueName": {"Fn::Sub": "cs-diff-${Suffix}"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStack")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template)
|
||||
.formParam("Parameters.member.1.ParameterKey", "Suffix")
|
||||
.formParam("Parameters.member.1.ParameterValue", "one")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
// Same template, only the parameter value changes.
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateChangeSet")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("ChangeSetName", "param-only-cs")
|
||||
.formParam("ChangeSetType", "UPDATE")
|
||||
.formParam("TemplateBody", template)
|
||||
.formParam("Parameters.member.1.ParameterKey", "Suffix")
|
||||
.formParam("Parameters.member.1.ParameterValue", "two")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeChangeSet")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("ChangeSetName", "param-only-cs")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<LogicalResourceId>Q</LogicalResourceId>"))
|
||||
.body(containsString("<Action>Modify</Action>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void samStackNoOpUpdate_doesNotReportSpuriousChanges() {
|
||||
String stackName = "cs-diff-sam-noop-stack";
|
||||
stacksToDelete.add(stackName);
|
||||
|
||||
String template = """
|
||||
AWSTemplateFormatVersion: '2010-09-09'
|
||||
Transform: AWS::Serverless-2016-10-31
|
||||
Resources:
|
||||
HelloFunction:
|
||||
Type: AWS::Serverless::Function
|
||||
Properties:
|
||||
FunctionName: cs-diff-sam-func
|
||||
Handler: index.handler
|
||||
Runtime: nodejs22.x
|
||||
InlineCode: |
|
||||
exports.handler = async () => ({ statusCode: 200, body: 'ok' });
|
||||
""";
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStack")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template)
|
||||
.formParam("Capabilities.member.1", "CAPABILITY_IAM")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
// Byte-identical re-submission of the same SAM source: the deployed template is the
|
||||
// *expanded* form, so diffing the raw SAM source against it must not report the
|
||||
// SAM-generated IAM role as removed or the function as modified.
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateChangeSet")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("ChangeSetName", "sam-noop-cs")
|
||||
.formParam("ChangeSetType", "UPDATE")
|
||||
.formParam("TemplateBody", template)
|
||||
.formParam("Capabilities.member.1", "CAPABILITY_IAM")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeChangeSet")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("ChangeSetName", "sam-noop-cs")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(not(containsString("<Action>Remove</Action>")))
|
||||
.body(not(containsString("<Action>Modify</Action>")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resourceTypeChange_reportsReplacementTrue() {
|
||||
String stackName = "cs-diff-replacement-stack";
|
||||
stacksToDelete.add(stackName);
|
||||
|
||||
String initialTemplate = """
|
||||
{
|
||||
"Resources": {
|
||||
"R": {
|
||||
"Type": "AWS::SNS::Topic",
|
||||
"Properties": {"TopicName": "cs-diff-repl-topic"}
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
// Same logical id, different resource Type: AWS always forces a replacement here.
|
||||
String replacedTemplate = """
|
||||
{
|
||||
"Resources": {
|
||||
"R": {
|
||||
"Type": "AWS::SQS::Queue",
|
||||
"Properties": {"QueueName": "cs-diff-repl-queue"}
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStack")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", initialTemplate)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateChangeSet")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("ChangeSetName", "replacement-cs")
|
||||
.formParam("ChangeSetType", "UPDATE")
|
||||
.formParam("TemplateBody", replacedTemplate)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeChangeSet")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("ChangeSetName", "replacement-cs")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<Replacement>True</Replacement>"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user