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 b83bdf3909..c4b3edd06b 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 @@ -426,6 +426,7 @@ public class CloudFormationResourceProvisioner { case "AWS::ElasticLoadBalancingV2::Listener" -> elbV2Service.deleteListener(region, physicalId); case "AWS::ElasticLoadBalancingV2::ListenerRule" -> elbV2Service.deleteRule(region, physicalId); case "AWS::KinesisFirehose::DeliveryStream" -> firehoseService.deleteDeliveryStream(physicalId); + case "AWS::EC2::SecurityGroup" -> ec2Service.deleteSecurityGroup(region, physicalId); case "AWS::EC2::Instance" -> ec2Service.terminateInstances(region, List.of(physicalId)); case "AWS::RDS::DBInstance" -> rdsService.deleteDbInstance(physicalId); case "AWS::RDS::DBCluster" -> rdsService.deleteDbCluster(physicalId); 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 f3505082b9..10549ced6a 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 @@ -5225,6 +5225,66 @@ class CloudFormationIntegrationTest { .body("services[0].taskDefinition", equalTo("arn:aws:ecs:us-east-1:000000000000:task-definition/cfn-ecs-update-taskdef:2")); } + + @Test + void deleteStack_ec2SecurityGroup_leavesNoOrphans() { + String stackName = "sg-delete-cleanup-stack"; + String groupName = "sg-delete-cleanup-group"; + + String template = """ + { + "Resources": { + "AppSecurityGroup": { + "Type": "AWS::EC2::SecurityGroup", + "Properties": { + "GroupName": "%s", + "GroupDescription": "Security group created by CloudFormation", + "VpcId": "vpc-default" + } + } + } + } + """.formatted(groupName); + + given() + .formParam("Action", "CreateStack") + .formParam("Version", "2010-05-15") + .formParam("StackName", stackName) + .formParam("TemplateBody", template) + .when().post("/") + .then().statusCode(200); + + given() + .formParam("Action", "DescribeSecurityGroups") + .formParam("Version", "2016-11-15") + .formParam("GroupName.1", groupName) + .when().post("/") + .then().statusCode(200) + .body(containsString(groupName)); + + given() + .formParam("Action", "DeleteStack") + .formParam("Version", "2010-05-15") + .formParam("StackName", stackName) + .when().post("/") + .then().statusCode(200); + + given() + .formParam("Action", "DescribeSecurityGroups") + .formParam("Version", "2016-11-15") + .formParam("GroupName.1", groupName) + .when().post("/") + .then().statusCode(200) + .body(not(containsString(groupName))); + + given() + .formParam("Action", "CreateStack") + .formParam("Version", "2010-05-15") + .formParam("StackName", stackName) + .formParam("TemplateBody", template) + .when().post("/") + .then().statusCode(200); + } @Test void deleteStack_ecs_leavesNoOrphans() {