Light, fluffy, and always free - The AWS Local Emulator alternative
fix(cloudformation): resolve Fn::GetAtt [Vpc, DefaultSecurityGroup] (#1977)
lex00bdad0666947e
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.javaindex 31e14c1000..23b62ba4cb 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@@ -338,7 +338,6 @@ public class CloudFormationResourceProvisioner {// EC2 networking. These delegate to Ec2Service so the resources actually exist// (describe-subnets, ELBv2, etc. can find them) instead of being stubbed with a// fake physical id. Topological ordering guarantees parents are provisioned first.- case "AWS::EC2::VPC" -> provisionVpc(resource, properties, engine, region);case "AWS::EC2::Subnet" -> provisionSubnet(resource, properties, engine, region);case "AWS::EC2::SecurityGroup" -> provisionSecurityGroup(resource, properties, engine, region, stackName);case "AWS::EC2::InternetGateway" -> provisionInternetGateway(resource, region);@@ -586,15 +585,6 @@ public class CloudFormationResourceProvisioner {// ELBv2 create-load-balancer, etc. resolve it). physicalId is set to the real EC2 id so// Ref/exports resolve to a real vpc-/subnet-/... id rather than a stub.- private void provisionVpc(StackResource r, JsonNode props, CloudFormationTemplateEngine engine, String region) {- String cidr = resolveOptional(props, "CidrBlock", engine);- var vpc = ec2Service.createVpc(region, cidr, false);- r.setPhysicalId(vpc.getVpcId());- r.getAttributes().put("VpcId", vpc.getVpcId());- if (vpc.getCidrBlock() != null) {- r.getAttributes().put("CidrBlock", vpc.getCidrBlock());- }- }private void provisionSubnet(StackResource r, JsonNode props, CloudFormationTemplateEngine engine, String region) {String vpcId = resolveOptional(props, "VpcId", engine);diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2VpcCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2VpcCfnProvisioner.javanew file mode 100644index 0000000000..7448ad68c0--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2VpcCfnProvisioner.java@@ -0,0 +1,52 @@+package io.github.hectorvent.floci.services.cloudformation.provisioners;++import com.fasterxml.jackson.databind.JsonNode;+import io.github.hectorvent.floci.services.cloudformation.model.StackResource;+import io.github.hectorvent.floci.services.ec2.Ec2Service;+import jakarta.enterprise.context.ApplicationScoped;+import jakarta.inject.Inject;++import java.util.List;+import java.util.Map;+import java.util.Set;++/**+ * CloudFormation provisioning for {@code AWS::EC2::VPC}. Extracted verbatim from+ * {@code CloudFormationResourceProvisioner} as part of the per-service decomposition.+ */+@ApplicationScoped+public class Ec2VpcCfnProvisioner implements CfnResourceProvisioner {++ private final Ec2Service ec2Service;++ @Inject+ public Ec2VpcCfnProvisioner(Ec2Service ec2Service) {+ this.ec2Service = ec2Service;+ }++ @Override+ public Set<String> resourceTypes() {+ return Set.of("AWS::EC2::VPC");+ }++ @Override+ public void provision(StackResource r, JsonNode props, ProvisionContext ctx) {+ String cidr = ctx.resolveOptional(props, "CidrBlock");+ var vpc = ec2Service.createVpc(ctx.region(), cidr, false);+ r.setPhysicalId(vpc.getVpcId());+ r.getAttributes().put("VpcId", vpc.getVpcId());+ if (vpc.getCidrBlock() != null) {+ r.getAttributes().put("CidrBlock", vpc.getCidrBlock());+ }+ // Fn::GetAtt DefaultSecurityGroup — CDK's Custom::VpcRestrictDefaultSG handler+ // depends on it resolving to the VPC's default security group id.+ ec2Service.describeSecurityGroups(ctx.region(), List.of(), List.of("default"), Map.of()).stream()+ .filter(sg -> vpc.getVpcId().equals(sg.getVpcId()))+ .findFirst()+ .ifPresent(sg -> r.getAttributes().put("DefaultSecurityGroup", sg.getGroupId()));+ }++ // No delete override: the switch this replaces had no AWS::EC2::VPC delete arm,+ // so stack teardown leaves the VPC alone. Adding one here would change teardown+ // behavior beyond the scope of this extraction.+}diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationVpcDefaultSgAttrIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationVpcDefaultSgAttrIntegrationTest.javanew file mode 100644index 0000000000..1a2cd7574e--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationVpcDefaultSgAttrIntegrationTest.java@@ -0,0 +1,64 @@+package io.github.hectorvent.floci.services.cloudformation;++import io.quarkus.test.junit.QuarkusTest;+import org.junit.jupiter.api.Test;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.containsString;+import static org.hamcrest.Matchers.not;++/**+ * End-to-end check that Fn::GetAtt [Vpc, DefaultSecurityGroup] resolves to the real+ * default security group id of a stack-created VPC (issue #1976) instead of passing+ * through as the literal "LogicalId.DefaultSecurityGroup".+ */+@QuarkusTest+class CloudFormationVpcDefaultSgAttrIntegrationTest {++ private static final String CFN_AUTH =+ "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/cloudformation/aws4_request";++ @Test+ void getAttDefaultSecurityGroupResolvesToRealSgId() {+ String suffix = Long.toString(System.nanoTime(), 36);+ String stackName = "cfn-vpc-defsg-stack-" + suffix;++ String template = """+ {+ "Resources": {+ "Vpc": {+ "Type": "AWS::EC2::VPC",+ "Properties": {"CidrBlock": "10.43.0.0/16"}+ }+ },+ "Outputs": {+ "DefaultSg": {"Value": {"Fn::GetAtt": ["Vpc", "DefaultSecurityGroup"]}}+ }+ }+ """;++ 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);++ given()+ .contentType("application/x-www-form-urlencoded")+ .header("Authorization", CFN_AUTH)+ .formParam("Action", "DescribeStacks")+ .formParam("StackName", stackName)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"))+ .body(containsString("<OutputValue>sg-"))+ .body(not(containsString("Vpc.DefaultSecurityGroup")));+ }+}