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 80c499bb09..4e4e065a5c 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 @@ -412,8 +412,7 @@ public class CloudFormationResourceProvisioner { case "AWS::KMS::Key" -> { } // KMS keys can't be immediately deleted; skip case "AWS::KMS::Alias" -> kmsService.deleteAlias(physicalId, region); - case "AWS::SecretsManager::Secret" -> - secretsManagerService.deleteSecret(physicalId, null, true, region); + case "AWS::SecretsManager::Secret" -> deleteSecretSafe(physicalId, region); case "AWS::Events::Rule" -> deleteEventBridgeRuleSafe(physicalId, region); case "AWS::ApiGateway::RestApi" -> apiGatewayService.deleteRestApi(region, physicalId); case "AWS::ApiGatewayV2::Api" -> apiGatewayV2Service.deleteApi(region, physicalId); @@ -3769,6 +3768,17 @@ public class CloudFormationResourceProvisioner { } } + private void deleteSecretSafe(String secretId, String region) { + try { + secretsManagerService.deleteSecret(secretId, null, true, region); + } catch (AwsException e) { + if (!"ResourceNotFoundException".equals(e.getErrorCode())) { + throw e; + } + LOG.debugv("Secret already gone, treating as deleted: {0}", secretId); + } + } + /** * Generate an AWS-like physical name: {stackName}-{logicalId}-{randomSuffix}. * Mirrors the naming pattern AWS CloudFormation uses when no explicit name is provided. 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 b7d50b52d6..b50b3dbaf1 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 @@ -8,6 +8,8 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; @@ -1227,6 +1229,95 @@ class CloudFormationIntegrationTest { .statusCode(200); } + // Regression: issue #1668. Deleting a stack that owns an AWS::SecretsManager::Secret which + // no longer exists (e.g. dropped by a persistent-state restore) must reach DELETE_COMPLETE, + // not DELETE_FAILED. A missing secret is already gone — AWS treats it as deleted. + @Test + void deleteStack_withAlreadyDeletedSecret_reachesDeleteComplete() throws Exception { + String secretName = "cfn-1668-already-deleted-secret"; + String template = """ + { + "Resources": { + "TestSecret": { + "Type": "AWS::SecretsManager::Secret", + "Properties": { + "Name": "%s", + "SecretString": "{\\"key\\":\\"value\\"}" + } + } + } + } + """.formatted(secretName); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "CreateStack") + .formParam("StackName", "cfn-1668-delete-stack") + .formParam("TemplateBody", template) + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("")); + + // Wait for CREATE_COMPLETE before deleting the secret out of band. + long createDeadline = System.currentTimeMillis() + 10_000; + String createStatus = ""; + while (System.currentTimeMillis() < createDeadline) { + String xml = given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "DescribeStacks") + .formParam("StackName", "cfn-1668-delete-stack") + .when().post("/").then().statusCode(200).extract().asString(); + if (xml.contains("CREATE_COMPLETE")) { + createStatus = "CREATE_COMPLETE"; + break; + } + Thread.sleep(200); + } + assertEquals("CREATE_COMPLETE", createStatus, "Stack did not reach CREATE_COMPLETE within timeout"); + + // Simulate a persistent-state restore dropping the secret while the stack still tracks it. + given() + .contentType(SM_CONTENT_TYPE) + .header("X-Amz-Target", "secretsmanager.DeleteSecret") + .body("{\"SecretId\":\"" + secretName + "\",\"ForceDeleteWithoutRecovery\":true}") + .when() + .post("/") + .then() + .statusCode(200); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "DeleteStack") + .formParam("StackName", "cfn-1668-delete-stack") + .when() + .post("/") + .then() + .statusCode(200); + + long deadline = System.currentTimeMillis() + 10_000; + while (System.currentTimeMillis() < deadline) { + String statusXml = given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "DescribeStacks") + .formParam("StackName", "cfn-1668-delete-stack") + .when() + .post("/") + .then() + .extract().body().asString(); + + assertThat(statusXml, not(containsString("DELETE_FAILED"))); + + if (statusXml.contains("DELETE_COMPLETE") + || statusXml.contains("does not exist")) { + return; + } + Thread.sleep(200); + } + throw new AssertionError("Stack did not reach DELETE_COMPLETE within timeout"); + } + @Test void describeDeletedStack_byArn_expiresAfterRetentionWindow() throws Exception { String template = """