mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
feat(cloudformation): honor resource-level DeletionPolicy (#2167)
A resource's DeletionPolicy attribute was never read off the template, so every managed resource was deleted regardless of what it declared: deleteStackResources and rollbackCreatedResources both called provisioner.delete unconditionally. Read DeletionPolicy alongside Type in executeTemplate and apply it at both delete sites. Retain keeps the resource on every stack operation. RetainExceptOnCreate keeps it too, except when the create that made it is rolled back — deleting it there is the entire meaning of that value, and rollbackCreatedResources runs only for a failed create. A retained resource is reported as DELETE_SKIPPED, on the resource and as a stack event, and is not counted as a failed delete, so the stack still reaches DELETE_COMPLETE. That also lets a stack owning a non-empty S3 bucket be deleted, which otherwise fails on the objects. Any other value falls through to the existing delete. That includes Snapshot: floci cannot take the snapshots AWS supports for those types, and AWS does delete the resource under Snapshot, so Delete is the faithful half. The docs record that and the other divergences. The policy is set on the resource after the provisioner returns, because provision() and executeNestedStack() both return a fresh StackResource that replaces the map entry. Closes #1555
This commit is contained in:
1 parent
1795e131a2
commit
7d9b9aedd2
4 files changed
+309
No files matched your search
@@ -118,6 +118,36 @@ Resources provisioned by `CreateStack` / `UpdateStack` land in the **caller's ac
|
||||
(determined from the request's access key — see [Multi-Account Isolation](../configuration/multi-account.md)).
|
||||
Deleting the stack removes them from that same account.
|
||||
|
||||
## Deletion Policies
|
||||
|
||||
A resource's [`DeletionPolicy`](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-attribute-deletionpolicy.html)
|
||||
attribute is honored on `DeleteStack` and on the rollback of a failed `CreateStack`:
|
||||
|
||||
| Value | `DeleteStack` | Rollback of the create that made the resource |
|
||||
|---|---|---|
|
||||
| `Delete` (default) | deleted | deleted |
|
||||
| `Retain` | kept | kept |
|
||||
| `RetainExceptOnCreate` | kept | deleted |
|
||||
|
||||
A kept resource is reported as `DELETE_SKIPPED` in `DescribeStackEvents` and does not fail the
|
||||
deletion — the stack still reaches `DELETE_COMPLETE` while the resource keeps existing. This also
|
||||
lets a stack owning a non-empty S3 bucket be deleted, since the bucket is never touched.
|
||||
|
||||
Deviations from AWS to be aware of:
|
||||
|
||||
- `Snapshot` deletes the resource without taking a snapshot; floci has no snapshot support for the
|
||||
types AWS allows it on.
|
||||
- Every resource defaults to `Delete`. AWS instead defaults `AWS::RDS::DBCluster`, and
|
||||
`AWS::RDS::DBInstance` without a `DBClusterIdentifier`, to `Snapshot`.
|
||||
- Unrecognized values are treated as `Delete`.
|
||||
- An update that removes a resource from the template does not delete it (or consult its policy);
|
||||
only the new template's resources are provisioned. AWS applies `DeletionPolicy` to update-time
|
||||
removals as well.
|
||||
- After `DeleteStack` completes, a retained resource's `DELETE_SKIPPED` record is only visible
|
||||
through `DescribeStackEvents` for the deleted stack, not `DescribeStackResources`.
|
||||
- `UpdateReplacePolicy`, and the `RetainExceptOnCreate` request parameter of `CreateStack` /
|
||||
`UpdateStack`, are not implemented.
|
||||
|
||||
## StackSets
|
||||
|
||||
StackSets deploy a single template into many target accounts and regions:
|
||||
|
||||
+33
@@ -454,6 +454,7 @@ public class CloudFormationService {
|
||||
for (String logicalId : sortedLogicalIds) {
|
||||
JsonNode resDef = resources.get(logicalId);
|
||||
String type = resDef.path("Type").asText();
|
||||
String deletionPolicy = resDef.path("DeletionPolicy").asText(null);
|
||||
JsonNode props = resDef.path("Properties");
|
||||
|
||||
CloudFormationTemplateEngine engine = new CloudFormationTemplateEngine(
|
||||
@@ -479,6 +480,9 @@ public class CloudFormationService {
|
||||
engine, region, accountId, stack.getStackName(),
|
||||
resource.getPhysicalId(), resource.getAttributes());
|
||||
}
|
||||
// Both branches return a fresh StackResource, so the policy is carried over here
|
||||
// rather than on the instance the loop started with.
|
||||
resource.setDeletionPolicy(deletionPolicy);
|
||||
stack.getResources().put(logicalId, resource);
|
||||
|
||||
physicalIds.put(logicalId, resource.getPhysicalId());
|
||||
@@ -632,6 +636,9 @@ public class CloudFormationService {
|
||||
if (resource.getPhysicalId() == null || (!completed && !ownedFailedResource)) {
|
||||
continue;
|
||||
}
|
||||
if (skipRetainedResource(stack, resource, true)) {
|
||||
continue;
|
||||
}
|
||||
addEvent(stack, resource.getLogicalId(), resource.getPhysicalId(),
|
||||
resource.getResourceType(), "DELETE_IN_PROGRESS", null);
|
||||
try {
|
||||
@@ -667,6 +674,9 @@ public class CloudFormationService {
|
||||
if (resource.getPhysicalId() == null || !deletable) {
|
||||
continue;
|
||||
}
|
||||
if (skipRetainedResource(stack, resource, false)) {
|
||||
continue;
|
||||
}
|
||||
addEvent(stack, resource.getLogicalId(), resource.getPhysicalId(),
|
||||
resource.getResourceType(), "DELETE_IN_PROGRESS", null);
|
||||
try {
|
||||
@@ -719,6 +729,29 @@ public class CloudFormationService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies a resource's {@code DeletionPolicy}. {@code Retain} keeps the resource on every stack
|
||||
* operation; {@code RetainExceptOnCreate} keeps it too, except when the create that made it is
|
||||
* rolled back. Every other value — including {@code Snapshot}, which floci cannot snapshot —
|
||||
* falls through to a normal delete, matching the default.
|
||||
*
|
||||
* @return {@code true} when the resource was kept and reported as {@code DELETE_SKIPPED}
|
||||
*/
|
||||
private boolean skipRetainedResource(Stack stack, StackResource resource, boolean createRollback) {
|
||||
String policy = resource.getDeletionPolicy();
|
||||
boolean retained = "Retain".equals(policy)
|
||||
|| (!createRollback && "RetainExceptOnCreate".equals(policy));
|
||||
if (!retained) {
|
||||
return false;
|
||||
}
|
||||
resource.setStatus("DELETE_SKIPPED");
|
||||
addEvent(stack, resource.getLogicalId(), resource.getPhysicalId(),
|
||||
resource.getResourceType(), "DELETE_SKIPPED", null);
|
||||
LOG.infov("Retained {0} ({1}) in stack {2}: DeletionPolicy {3}",
|
||||
resource.getResourceType(), resource.getPhysicalId(), stack.getStackName(), policy);
|
||||
return true;
|
||||
}
|
||||
|
||||
private Map<String, Boolean> resolveConditions(JsonNode template, Map<String, String> params,
|
||||
Stack stack, String region, String accountId) {
|
||||
Map<String, Boolean> conditions = new HashMap<>();
|
||||
|
||||
+3
@@ -13,6 +13,7 @@ public class StackResource {
|
||||
private String logicalId;
|
||||
private String physicalId;
|
||||
private String resourceType;
|
||||
private String deletionPolicy;
|
||||
private String status = "CREATE_IN_PROGRESS";
|
||||
private String statusReason;
|
||||
private Instant timestamp = Instant.now();
|
||||
@@ -24,6 +25,8 @@ public class StackResource {
|
||||
public void setPhysicalId(String physicalId) { this.physicalId = physicalId; }
|
||||
public String getResourceType() { return resourceType; }
|
||||
public void setResourceType(String resourceType) { this.resourceType = resourceType; }
|
||||
public String getDeletionPolicy() { return deletionPolicy; }
|
||||
public void setDeletionPolicy(String deletionPolicy) { this.deletionPolicy = deletionPolicy; }
|
||||
public String getStatus() { return status; }
|
||||
public void setStatus(String status) { this.status = status; }
|
||||
public String getStatusReason() { return statusReason; }
|
||||
|
||||
+243
@@ -0,0 +1,243 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.Response;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
* Verifies the resource-level {@code DeletionPolicy} attribute (issue #1555): {@code Retain} keeps a
|
||||
* resource on every stack operation, {@code RetainExceptOnCreate} keeps it everywhere except the
|
||||
* rollback of the create that made it, and any other value falls through to the default delete.
|
||||
*
|
||||
* <p>Exercised through S3 buckets — the case the attribute is most used for, and one whose survival
|
||||
* is directly observable without Docker.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class CloudFormationDeletionPolicyIntegrationTest {
|
||||
|
||||
@Test
|
||||
void retainKeepsANonEmptyBucketAndTheStackStillCompletesTheDelete() throws InterruptedException {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String bucket = "cfn-retain-bucket-" + suffix;
|
||||
String stackName = "cfn-retain-stack-" + suffix;
|
||||
|
||||
String template = """
|
||||
{
|
||||
"Resources": {
|
||||
"MyBucket": {
|
||||
"Type": "AWS::S3::Bucket",
|
||||
"DeletionPolicy": "Retain",
|
||||
"Properties": { "BucketName": "%s" }
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(bucket);
|
||||
|
||||
String stackId = createStack(stackName, template);
|
||||
|
||||
given()
|
||||
.contentType("text/plain")
|
||||
.body("kept")
|
||||
.when()
|
||||
.put("/" + bucket + "/object.txt")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
deleteStack(stackName);
|
||||
|
||||
// A deleted bucket would have failed on its objects instead (issue #1539), so reaching
|
||||
// DELETE_COMPLETE is itself evidence that the bucket was never touched.
|
||||
awaitStackStatus(stackId, "DELETE_COMPLETE");
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/" + bucket + "/object.txt")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("kept"));
|
||||
|
||||
assertThat(describeStackEvents(stackId),
|
||||
containsString("<ResourceStatus>DELETE_SKIPPED</ResourceStatus>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void retainExceptOnCreateKeepsTheBucketWhenTheStackIsDeleted() throws InterruptedException {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String bucket = "cfn-reoc-delete-bucket-" + suffix;
|
||||
String stackName = "cfn-reoc-delete-stack-" + suffix;
|
||||
|
||||
String template = """
|
||||
{
|
||||
"Resources": {
|
||||
"MyBucket": {
|
||||
"Type": "AWS::S3::Bucket",
|
||||
"DeletionPolicy": "RetainExceptOnCreate",
|
||||
"Properties": { "BucketName": "%s" }
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(bucket);
|
||||
|
||||
String stackId = createStack(stackName, template);
|
||||
deleteStack(stackName);
|
||||
awaitStackStatus(stackId, "DELETE_COMPLETE");
|
||||
|
||||
assertBucketExists(bucket);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rollingBackAFailedCreateDeletesRetainExceptOnCreateButKeepsRetain() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String keptBucket = "cfn-rollback-retain-bucket-" + suffix;
|
||||
String rolledBackBucket = "cfn-rollback-reoc-bucket-" + suffix;
|
||||
String stackName = "cfn-rollback-policy-stack-" + suffix;
|
||||
|
||||
// DependsOn forces both buckets to provision before BadSecret fails (setting SecretString
|
||||
// and GenerateSecretString together is invalid), which triggers the create rollback.
|
||||
String template = """
|
||||
{
|
||||
"Resources": {
|
||||
"KeptBucket": {
|
||||
"Type": "AWS::S3::Bucket",
|
||||
"DeletionPolicy": "Retain",
|
||||
"Properties": { "BucketName": "%s" }
|
||||
},
|
||||
"RolledBackBucket": {
|
||||
"Type": "AWS::S3::Bucket",
|
||||
"DeletionPolicy": "RetainExceptOnCreate",
|
||||
"DependsOn": "KeptBucket",
|
||||
"Properties": { "BucketName": "%s" }
|
||||
},
|
||||
"BadSecret": {
|
||||
"Type": "AWS::SecretsManager::Secret",
|
||||
"DependsOn": "RolledBackBucket",
|
||||
"Properties": {
|
||||
"Name": "cfn-rollback-policy-secret-%s",
|
||||
"SecretString": "explicit",
|
||||
"GenerateSecretString": { "PasswordLength": 32 }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(keptBucket, rolledBackBucket, suffix);
|
||||
|
||||
String stackId = createStack(stackName, template);
|
||||
|
||||
assertThat(describeStacks(stackId), containsString("<StackStatus>ROLLBACK_COMPLETE</StackStatus>"));
|
||||
assertBucketExists(keptBucket);
|
||||
assertBucketDeleted(rolledBackBucket);
|
||||
}
|
||||
|
||||
@Test
|
||||
void bucketsWithoutARetainingPolicyAreStillDeleted() throws InterruptedException {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String defaulted = "cfn-policy-default-bucket-" + suffix;
|
||||
String explicitDelete = "cfn-policy-delete-bucket-" + suffix;
|
||||
String unrecognized = "cfn-policy-unknown-bucket-" + suffix;
|
||||
String stackName = "cfn-policy-delete-stack-" + suffix;
|
||||
|
||||
String template = """
|
||||
{
|
||||
"Resources": {
|
||||
"Defaulted": {
|
||||
"Type": "AWS::S3::Bucket",
|
||||
"Properties": { "BucketName": "%s" }
|
||||
},
|
||||
"ExplicitDelete": {
|
||||
"Type": "AWS::S3::Bucket",
|
||||
"DeletionPolicy": "Delete",
|
||||
"Properties": { "BucketName": "%s" }
|
||||
},
|
||||
"Unrecognized": {
|
||||
"Type": "AWS::S3::Bucket",
|
||||
"DeletionPolicy": "Keep",
|
||||
"Properties": { "BucketName": "%s" }
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(defaulted, explicitDelete, unrecognized);
|
||||
|
||||
String stackId = createStack(stackName, template);
|
||||
deleteStack(stackName);
|
||||
awaitStackStatus(stackId, "DELETE_COMPLETE");
|
||||
|
||||
assertBucketDeleted(defaulted);
|
||||
assertBucketDeleted(explicitDelete);
|
||||
assertBucketDeleted(unrecognized);
|
||||
}
|
||||
|
||||
private static String createStack(String stackName, String template) {
|
||||
String xml = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStack")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().asString();
|
||||
|
||||
int start = xml.indexOf("<StackId>") + "<StackId>".length();
|
||||
return xml.substring(start, xml.indexOf("</StackId>", start));
|
||||
}
|
||||
|
||||
private static void deleteStack(String stackName) {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DeleteStack")
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
/** DeleteStack runs asynchronously, and a deleted stack stays describable by its stack ID. */
|
||||
private static void awaitStackStatus(String stackId, String status) throws InterruptedException {
|
||||
String xml = "";
|
||||
long deadline = System.currentTimeMillis() + 10_000;
|
||||
while (System.currentTimeMillis() < deadline) {
|
||||
xml = describeStacks(stackId);
|
||||
if (xml.contains("<StackStatus>" + status + "</StackStatus>")) {
|
||||
return;
|
||||
}
|
||||
Thread.sleep(50);
|
||||
}
|
||||
fail("stack " + stackId + " never reached " + status + ": " + xml);
|
||||
}
|
||||
|
||||
private static String describeStacks(String stackId) {
|
||||
return cfnQuery("DescribeStacks", stackId).then().statusCode(200).extract().asString();
|
||||
}
|
||||
|
||||
private static String describeStackEvents(String stackId) {
|
||||
return cfnQuery("DescribeStackEvents", stackId).then().statusCode(200).extract().asString();
|
||||
}
|
||||
|
||||
private static Response cfnQuery(String action, String stackId) {
|
||||
return given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", action)
|
||||
.formParam("StackName", stackId)
|
||||
.when()
|
||||
.post("/");
|
||||
}
|
||||
|
||||
private static void assertBucketExists(String bucket) {
|
||||
bucketRequest(bucket).then().statusCode(200);
|
||||
}
|
||||
|
||||
private static void assertBucketDeleted(String bucket) {
|
||||
bucketRequest(bucket).then().statusCode(404);
|
||||
}
|
||||
|
||||
private static Response bucketRequest(String bucket) {
|
||||
return given().header("Host", bucket + ".localhost").when().get("/");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user