mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(cloudformation): resolve Fn::GetAtt [Vpc, DefaultSecurityGroup] (#1977)
* fix(cloudformation): resolve Fn::GetAtt [Vpc, DefaultSecurityGroup] provisionVpc now records the VPC's default security group id as the DefaultSecurityGroup attribute, so GetAtt resolves to the real sg-* id instead of passing the literal 'LogicalId.DefaultSecurityGroup' string to downstream resources (breaks CDK's Custom::VpcRestrictDefaultSG handler). Fixes #1976 * refactor(cloudformation): extract AWS::EC2::VPC into a per-service provisioner
This commit is contained in:
1 parent
4e0a815f20
commit
bdad066694
3 files changed
+116
-10
No files matched your search
-10
@@ -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);
|
||||
|
||||
+52
@@ -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.
|
||||
}
|
||||
+64
@@ -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")));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user