diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java index e57b41ed09..952a1e651e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java @@ -455,8 +455,8 @@ public class CloudFormationResourceProvisioner { case "AWS::S3::Bucket" -> s3Service.deleteBucket(physicalId); case "AWS::SNS::Topic" -> snsService.deleteTopic(physicalId, region); case "AWS::SNS::Subscription" -> snsService.unsubscribe(physicalId, region); - case "AWS::DynamoDB::Table" -> dynamoDbService.deleteTable(physicalId, region); - case "AWS::Lambda::Function" -> lambdaService.deleteFunction(region, physicalId); + case "AWS::DynamoDB::Table" -> deleteDynamoTableSafe(physicalId, region); + case "AWS::Lambda::Function" -> deleteLambdaFunctionSafe(physicalId, region); case "AWS::IAM::Role" -> deleteRoleSafe(physicalId); // AWS::IAM::Policy is inline: it is removed together with its owning principal (see // deleteRoleSafe), or precisely via the StackResource-aware delete path. Nothing to do @@ -4261,6 +4261,28 @@ public class CloudFormationResourceProvisioner { } } + private void deleteDynamoTableSafe(String tableName, String region) { + try { + dynamoDbService.deleteTable(tableName, region); + } catch (AwsException e) { + if (!"ResourceNotFoundException".equals(e.getErrorCode())) { + throw e; + } + LOG.debugv("DynamoDB table already gone, treating as deleted: {0}", tableName); + } + } + + private void deleteLambdaFunctionSafe(String functionName, String region) { + try { + lambdaService.deleteFunction(region, functionName); + } catch (AwsException e) { + if (!"ResourceNotFoundException".equals(e.getErrorCode())) { + throw e; + } + LOG.debugv("Lambda function already gone, treating as deleted: {0}", functionName); + } + } + private void deleteManagedPolicy(StackResource resource) { String policyArn = resource.getPhysicalId(); for (String roleName : managedPolicyRoleTargets(resource)) { diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDeleteIdempotencyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDeleteIdempotencyIntegrationTest.java new file mode 100644 index 0000000000..05d15fb1cf --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDeleteIdempotencyIntegrationTest.java @@ -0,0 +1,124 @@ +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.BeforeAll; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.containsString; +import static org.junit.jupiter.api.Assertions.fail; + +/** + * Verifies deleting a stack whose underlying resource was already removed out-of-band completes + * (DELETE_COMPLETE) rather than failing. AWS CloudFormation treats deleting an absent resource as an + * idempotent no-op — this matters because a rolled-back stack has already deleted some of its + * resources, so tearing that stack down re-issues DeleteTable/DeleteFunction against resources that + * no longer exist. Without the {@code *Safe} wrappers those "not found" errors flipped the stack to + * DELETE_FAILED, wedging the stack name against a later create. + * + *
Exercised end-to-end through the DynamoDB path (metadata-only, so Docker-free). Hermetic unit
+ * coverage verifies both the DynamoDB and Lambda wrappers, including propagation of other errors.
+ */
+@QuarkusTest
+class CloudFormationDeleteIdempotencyIntegrationTest {
+
+ private static final String CFN_AUTH =
+ "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/cloudformation/aws4_request";
+ private static final String DDB_AUTH =
+ "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/dynamodb/aws4_request";
+ private static final String DDB_CT = "application/x-amz-json-1.0";
+
+ @BeforeAll
+ static void configureRestAssured() {
+ RestAssuredJsonUtils.configureAwsContentTypes();
+ }
+
+ @Test
+ void deletingStackWhoseTableWasRemovedOutOfBandStillCompletes() throws InterruptedException {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String tableName = "idem-delete-table-" + suffix;
+ String stackName = "idem-delete-stack-" + suffix;
+
+ String template = """
+ {
+ "Resources": {
+ "T": {
+ "Type": "AWS::DynamoDB::Table",
+ "Properties": {
+ "TableName": "%s",
+ "AttributeDefinitions": [{"AttributeName":"pk","AttributeType":"S"}],
+ "KeySchema": [{"AttributeName":"pk","KeyType":"HASH"}],
+ "BillingMode": "PAY_PER_REQUEST"
+ }
+ }
+ }
+ }
+ """.formatted(tableName);
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .header("Authorization", CFN_AUTH)
+ .formParam("Action", "CreateStack")
+ .formParam("StackName", stackName)
+ .formParam("TemplateBody", template)
+ .when().post("/").then().statusCode(200);
+
+ // CloudFormationQueryHandler awaits executeChangeSet before CreateStack returns, so this
+ // one-shot assertion verifies the handler's synchronous completion contract.
+ assertStackStatus(stackName, "CREATE_COMPLETE");
+
+ // Remove the table out-of-band — mimics a rollback having already deleted it.
+ given()
+ .contentType(DDB_CT)
+ .header("Authorization", DDB_AUTH)
+ .header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")
+ .body("{\"TableName\":\"" + tableName + "\"}")
+ .when().post("/").then().statusCode(200);
+
+ // Deleting the stack must still succeed (was DELETE_FAILED: "Requested resource not found").
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .header("Authorization", CFN_AUTH)
+ .formParam("Action", "DeleteStack")
+ .formParam("StackName", stackName)
+ .when().post("/").then().statusCode(200);
+
+ // DeleteStack runs asynchronously: poll until the stack is gone (a successful delete removes it
+ // entirely). A failed delete would instead leave it queryable as DELETE_FAILED — fail fast on that.
+ awaitStackDeleted(stackName);
+ }
+
+ private void awaitStackDeleted(String stackName) throws InterruptedException {
+ for (int i = 0; i < 100; i++) {
+ String body = given()
+ .contentType("application/x-www-form-urlencoded")
+ .header("Authorization", CFN_AUTH)
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", stackName)
+ .when().post("/").then().extract().asString();
+
+ if (body.contains("does not exist")) {
+ return;
+ }
+ if (body.contains("