Skip to content

armgit / floci

Public Imported from GitHub
0

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

feat(cloudformation): support UpdateTerminationProtection (#1692)

Alex Banna4a16266f4868
diff --git a/docs/services/cloudformation.md b/docs/services/cloudformation.md
index 5060f04d34..e6767b3d26 100644
--- a/docs/services/cloudformation.md
+++ b/docs/services/cloudformation.md
@@ -12,6 +12,7 @@
| `CreateStack` | Deploy a CloudFormation template |
| `UpdateStack` | Update an existing stack |
| `DeleteStack` | Delete a stack and its resources |
+| `UpdateTerminationProtection` | - |
| `CreateChangeSet` | Create a change set |
| `DescribeChangeSet` | Get change set details (no computed diff/preview) |
| `ExecuteChangeSet` | Apply a change set |
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java
index d7a2fa5ac3..e354883a9a 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java
@@ -351,7 +351,7 @@ public class AwsQueryController {
);
private static final Set<String> CLOUDFORMATION_ACTIONS = Set.of(
- "CreateStack", "DeleteStack", "UpdateStack", "DescribeStacks",
+ "CreateStack", "DeleteStack", "UpdateStack", "DescribeStacks", "UpdateTerminationProtection",
"ListStacks", "ListExports", "GetTemplate", "ValidateTemplate",
"CreateChangeSet", "DeleteChangeSet", "DescribeChangeSet", "ExecuteChangeSet", "ListChangeSets",
"DescribeStackEvents", "DescribeStackResources", "ListStackResources", "DescribeStackResource",
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationQueryHandler.java
index 4f44a83411..095ab337bc 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationQueryHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationQueryHandler.java
@@ -49,6 +49,7 @@ public class CloudFormationQueryHandler {
case "CreateStack" -> createStack(params, region);
case "UpdateStack" -> updateStack(params, region);
case "DeleteStack" -> deleteStack(params, region);
+ case "UpdateTerminationProtection" -> updateTerminationProtection(params, region);
case "CreateChangeSet" -> createChangeSet(params, region);
case "DescribeChangeSet" -> describeChangeSet(params, region);
case "ExecuteChangeSet" -> executeChangeSet(params, region);
@@ -115,6 +116,11 @@ public class CloudFormationQueryHandler {
templateBody, templateUrl, parameters, capabilities, tags, region);
awaitExecution(cfnService.executeChangeSet(stackName, "initial-create", region));
+ if (Boolean.parseBoolean(params.getFirst("EnableTerminationProtection"))) {
+ // CreateStackInput carries the same member; honor it so the stack is created protected.
+ cfnService.updateTerminationProtection(stackName, true, region);
+ }
+
Stack stack = cfnService.describeStacks(stackName, region).get(0);
String xml = new XmlBuilder()
.start("CreateStackResponse", CF_NS)
@@ -156,13 +162,38 @@ public class CloudFormationQueryHandler {
private Response deleteStack(MultivaluedMap<String, String> params, String region) {
String stackName = params.getFirst("StackName");
- cfnService.deleteStack(stackName, region);
- String xml = new XmlBuilder()
- .start("DeleteStackResponse", CF_NS)
- .raw(AwsQueryResponse.responseMetadata())
- .end("DeleteStackResponse")
- .build();
- return Response.ok(xml).type("text/xml").build();
+ try {
+ cfnService.deleteStack(stackName, region);
+ String xml = new XmlBuilder()
+ .start("DeleteStackResponse", CF_NS)
+ .raw(AwsQueryResponse.responseMetadata())
+ .end("DeleteStackResponse")
+ .build();
+ return Response.ok(xml).type("text/xml").build();
+ } catch (AwsException e) {
+ return xmlError(e.getErrorCode(), e.getMessage(), e.getHttpStatus());
+ }
+ }
+
+ // ── UpdateTerminationProtection ─────────────────────────────────────────────
+
+ private Response updateTerminationProtection(MultivaluedMap<String, String> params, String region) {
+ String stackName = params.getFirst("StackName");
+ boolean enabled = Boolean.parseBoolean(params.getFirst("EnableTerminationProtection"));
+ try {
+ Stack stack = cfnService.updateTerminationProtection(stackName, enabled, region);
+ String xml = new XmlBuilder()
+ .start("UpdateTerminationProtectionResponse", CF_NS)
+ .start("UpdateTerminationProtectionResult")
+ .elem("StackId", stack.getStackId())
+ .end("UpdateTerminationProtectionResult")
+ .raw(AwsQueryResponse.responseMetadata())
+ .end("UpdateTerminationProtectionResponse")
+ .build();
+ return Response.ok(xml).type("text/xml").build();
+ } catch (AwsException e) {
+ return xmlError(e.getErrorCode(), e.getMessage(), e.getHttpStatus());
+ }
}
// ── CreateChangeSet ───────────────────────────────────────────────────────
@@ -471,6 +502,7 @@ public class CloudFormationQueryHandler {
.elem("StackId", s.getStackId())
.elem("StackName", s.getStackName())
.elem("StackStatus", s.getStatus())
+ .elem("EnableTerminationProtection", String.valueOf(s.isEnableTerminationProtection()))
.elem("CreationTime", ISO.format(s.getCreationTime()));
if (s.getLastUpdatedTime() != null) {
xml.elem("LastUpdatedTime", ISO.format(s.getLastUpdatedTime()));
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 461b7b8aac..ac724f8cd9 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
@@ -112,6 +112,17 @@ public class CloudFormationService {
stackBackend.deleteForAccount(storageAccount, key(stackName, region));
}
+ /**
+ * Sets a stack's termination protection (CloudFormation {@code UpdateTerminationProtection}).
+ * Returns the stack so the caller can echo its {@code StackId}.
+ */
+ public Stack updateTerminationProtection(String stackName, boolean enabled, String region) {
+ Stack stack = getStackOrThrow(stackName, region);
+ stack.setEnableTerminationProtection(enabled);
+ persistStack(stack);
+ return stack;
+ }
+
// ── DescribeStacks ────────────────────────────────────────────────────────
public List<Stack> describeStacks(String stackName, String region) {
@@ -290,6 +301,12 @@ public class CloudFormationService {
if (stack == null) {
return CompletableFuture.completedFuture(null); // Already gone — no-op
}
+ if (stack.isEnableTerminationProtection()) {
+ // Real AWS rejects deletion of a protected stack and leaves it unchanged.
+ throw new AwsException("ValidationError",
+ "Stack [" + stack.getStackId()
+ + "] cannot be deleted while TerminationProtection is enabled", 400);
+ }
stack.setStatus("DELETE_IN_PROGRESS");
addEvent(stack, stack.getStackName(), stack.getStackId(),
"AWS::CloudFormation::Stack", "DELETE_IN_PROGRESS", null);
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/model/Stack.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/model/Stack.java
index 6751dda543..b9c81ab795 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/model/Stack.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/model/Stack.java
@@ -27,6 +27,7 @@ public class Stack {
private List<StackEvent> events = new ArrayList<>();
private Map<String, ChangeSet> changeSets = new LinkedHashMap<>();
private Map<String, String> tags = new LinkedHashMap<>();
+ private boolean enableTerminationProtection = false;
public String getStackId() { return stackId; }
public void setStackId(String stackId) { this.stackId = stackId; }
@@ -62,4 +63,6 @@ public class Stack {
public void setChangeSets(Map<String, ChangeSet> changeSets) { this.changeSets = changeSets; }
public Map<String, String> getTags() { return tags; }
public void setTags(Map<String, String> tags) { this.tags = tags; }
+ public boolean isEnableTerminationProtection() { return enableTerminationProtection; }
+ public void setEnableTerminationProtection(boolean enableTerminationProtection) { this.enableTerminationProtection = enableTerminationProtection; }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java
index c167e336ab..f220291f9a 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java
@@ -172,6 +172,124 @@ class CloudFormationIntegrationTest {
.body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"));
}
+ @Test
+ void updateTerminationProtection_togglesFlagAndReflectsInDescribeStacks() {
+ String template = """
+ { "Resources": { "Q": { "Type": "AWS::SQS::Queue",
+ "Properties": { "QueueName": "cf-tp-queue" } } } }
+ """;
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "CreateStack")
+ .formParam("StackName", "tp-stack")
+ .formParam("TemplateBody", template)
+ .when().post("/")
+ .then().statusCode(200).body(containsString("<StackId>"));
+
+ // DescribeStacks reports protection off by default.
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", "tp-stack")
+ .when().post("/")
+ .then().statusCode(200)
+ .body(containsString("<EnableTerminationProtection>false</EnableTerminationProtection>"));
+
+ // Enable termination protection — returns the StackId.
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "UpdateTerminationProtection")
+ .formParam("StackName", "tp-stack")
+ .formParam("EnableTerminationProtection", "true")
+ .when().post("/")
+ .then().statusCode(200)
+ .body(containsString("<StackId>"))
+ .body(containsString("</UpdateTerminationProtectionResult>"));
+
+ // DescribeStacks now reflects it.
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", "tp-stack")
+ .when().post("/")
+ .then().statusCode(200)
+ .body(containsString("<EnableTerminationProtection>true</EnableTerminationProtection>"));
+ }
+
+ @Test
+ void deleteStack_protectedStackIsRejectedAndStackRemains() {
+ String template = """
+ { "Resources": { "Q": { "Type": "AWS::SQS::Queue",
+ "Properties": { "QueueName": "cf-tp-del-queue" } } } }
+ """;
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "CreateStack")
+ .formParam("StackName", "tp-del-stack")
+ .formParam("TemplateBody", template)
+ .when().post("/")
+ .then().statusCode(200).body(containsString("<StackId>"));
+
+ // Enable termination protection.
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "UpdateTerminationProtection")
+ .formParam("StackName", "tp-del-stack")
+ .formParam("EnableTerminationProtection", "true")
+ .when().post("/")
+ .then().statusCode(200);
+
+ // DeleteStack is rejected with a 400 ValidationError (XML error body, Query protocol).
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DeleteStack")
+ .formParam("StackName", "tp-del-stack")
+ .when().post("/")
+ .then().statusCode(400)
+ .contentType(containsString("xml"))
+ .body(containsString("<Code>ValidationError</Code>"))
+ .body(containsString("cannot be deleted while TerminationProtection is enabled"));
+
+ // The stack still exists and is not in a DELETE state.
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", "tp-del-stack")
+ .when().post("/")
+ .then().statusCode(200)
+ .body(containsString("<StackName>tp-del-stack</StackName>"))
+ .body(not(containsString("DELETE_IN_PROGRESS")))
+ .body(not(containsString("DELETE_COMPLETE")));
+ }
+
+ @Test
+ void createStack_withTerminationProtectionEnabled() {
+ String template = """
+ { "Resources": { "Q": { "Type": "AWS::SQS::Queue",
+ "Properties": { "QueueName": "cf-tp-create-queue" } } } }
+ """;
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "CreateStack")
+ .formParam("StackName", "tp-create-stack")
+ .formParam("TemplateBody", template)
+ .formParam("EnableTerminationProtection", "true")
+ .when().post("/")
+ .then().statusCode(200).body(containsString("<StackId>"));
+
+ // The CreateStackInput flag is honored — DescribeStacks reports protection on.
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", "tp-create-stack")
+ .when().post("/")
+ .then().statusCode(200)
+ .body(containsString("<EnableTerminationProtection>true</EnableTerminationProtection>"));
+ }
+
@Test
void createStack_s3BucketWithCorsConfiguration() {
String template = """