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 9e8d83546f..ebc779bf04 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 @@ -622,10 +622,8 @@ public class CloudFormationResourceProvisioner { String routeTableId = resolveOptional(props, "RouteTableId", engine); String destinationCidr = resolveOptional(props, "DestinationCidrBlock", engine); String gatewayId = resolveOptional(props, "GatewayId", engine); - if (gatewayId == null || gatewayId.isBlank()) { - gatewayId = resolveOptional(props, "NatGatewayId", engine); - } - ec2Service.createRoute(region, routeTableId, destinationCidr, gatewayId); + String natGatewayId = resolveOptional(props, "NatGatewayId", engine); + ec2Service.createRoute(region, routeTableId, destinationCidr, gatewayId, natGatewayId); r.setPhysicalId(r.getLogicalId() + "-" + UUID.randomUUID().toString().substring(0, 8)); } diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java index ad72beac54..03ecb3ab78 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java @@ -357,6 +357,29 @@ public class Ec2QueryHandler { return tags; } + // Apply tags supplied inline on a create call (TagSpecification) to the resource, so + // they round-trip on the next Describe* — otherwise the provider sees phantom tag drift. + private void applyResourceTags(MultivaluedMap p, String region, String resourceType, String resourceId) { + List tagList = parseTagsForResource(p, resourceType); + if (!tagList.isEmpty()) { + service.createTags(region, List.of(resourceId), tagList); + } + } + + // Each rule gets its own copy so mutating one rule's tag list can never leak into the + // others authorized in the same batch, and the copy also feeds the response XML. + private void applySecurityGroupRuleTags(MultivaluedMap p, String region, + List rules) { + List ruleTags = parseTagsForResource(p, "security-group-rule"); + if (ruleTags.isEmpty()) { + return; + } + for (SecurityGroupRule rule : rules) { + service.createTags(region, List.of(rule.getSecurityGroupRuleId()), ruleTags); + rule.setTags(new ArrayList<>(ruleTags)); + } + } + private Response xmlResponse(String xml) { return Response.ok(xml).type(MediaType.APPLICATION_XML).build(); } @@ -993,6 +1016,7 @@ public class Ec2QueryHandler { String cidrBlock = p.getFirst("CidrBlock"); String az = p.getFirst("AvailabilityZone"); Subnet subnet = service.createSubnet(region, vpcId, cidrBlock, az); + applyResourceTags(p, region, "subnet", subnet.getSubnetId()); XmlBuilder xml = new XmlBuilder() .start("CreateSubnetResponse", AwsNamespaces.EC2) .elem("requestId", UUID.randomUUID().toString()) @@ -1045,6 +1069,7 @@ public class Ec2QueryHandler { String description = p.getFirst("GroupDescription"); String vpcId = p.getFirst("VpcId"); SecurityGroup sg = service.createSecurityGroup(region, groupName, description, vpcId); + applyResourceTags(p, region, "security-group", sg.getGroupId()); XmlBuilder xml = new XmlBuilder() .start("CreateSecurityGroupResponse", AwsNamespaces.EC2) .elem("requestId", UUID.randomUUID().toString()) @@ -1081,6 +1106,7 @@ public class Ec2QueryHandler { String groupId = p.getFirst("GroupId"); List perms = parseIpPermissions(p, "IpPermissions"); List rules = service.authorizeSecurityGroupIngress(region, groupId, perms); + applySecurityGroupRuleTags(p, region, rules); XmlBuilder xml = new XmlBuilder() .start("AuthorizeSecurityGroupIngressResponse", AwsNamespaces.EC2) .elem("requestId", UUID.randomUUID().toString()) @@ -1097,6 +1123,7 @@ public class Ec2QueryHandler { String groupId = p.getFirst("GroupId"); List perms = parseIpPermissions(p, "IpPermissions"); List rules = service.authorizeSecurityGroupEgress(region, groupId, perms); + applySecurityGroupRuleTags(p, region, rules); XmlBuilder xml = new XmlBuilder() .start("AuthorizeSecurityGroupEgressResponse", AwsNamespaces.EC2) .elem("requestId", UUID.randomUUID().toString()) @@ -1125,14 +1152,19 @@ public class Ec2QueryHandler { private Response handleDescribeSecurityGroupRules(MultivaluedMap p, String region) { Map> filters = getFilters(p); - // The AWS SDK sends the security group id as a filter with name "group-id" - String groupId = ""; - List groupIdFilter = filters.get("group-id"); - if (groupIdFilter != null && !groupIdFilter.isEmpty()) { - groupId = groupIdFilter.get(0); - } - List ruleIds = getList(p, "SecurityGroupRuleId"); - List rules = service.describeSecurityGroupRules(region, groupId, ruleIds); + // The AWS SDK sends the security group id as a filter with name "group-id". Rule ids can + // arrive as the SecurityGroupRuleId.N parameter or the "security-group-rule-id" filter; + // filters are conjunctive with parameters, so intersect rather than union when both appear. + List paramRuleIds = getList(p, "SecurityGroupRuleId"); + List filterRuleIds = filters.getOrDefault("security-group-rule-id", List.of()); + List ruleIds = paramRuleIds.isEmpty() || filterRuleIds.isEmpty() + ? (paramRuleIds.isEmpty() ? filterRuleIds : paramRuleIds) + : paramRuleIds.stream().filter(filterRuleIds::contains).toList(); + boolean unsatisfiable = !paramRuleIds.isEmpty() && !filterRuleIds.isEmpty() && ruleIds.isEmpty(); + List groupIds = filters.getOrDefault("group-id", List.of()); + List rules = unsatisfiable + ? List.of() + : service.describeSecurityGroupRules(region, groupIds, ruleIds); XmlBuilder xml = new XmlBuilder() .start("DescribeSecurityGroupRulesResponse", AwsNamespaces.EC2) .elem("requestId", UUID.randomUUID().toString()) @@ -1348,6 +1380,7 @@ public class Ec2QueryHandler { private Response handleCreateInternetGateway(MultivaluedMap p, String region) { InternetGateway igw = service.createInternetGateway(region); + applyResourceTags(p, region, "internet-gateway", igw.getInternetGatewayId()); XmlBuilder xml = new XmlBuilder() .start("CreateInternetGatewayResponse", AwsNamespaces.EC2) .elem("requestId", UUID.randomUUID().toString()) @@ -1391,6 +1424,7 @@ public class Ec2QueryHandler { private Response handleCreateRouteTable(MultivaluedMap p, String region) { String vpcId = p.getFirst("VpcId"); RouteTable rt = service.createRouteTable(region, vpcId); + applyResourceTags(p, region, "route-table", rt.getRouteTableId()); XmlBuilder xml = new XmlBuilder() .start("CreateRouteTableResponse", AwsNamespaces.EC2) .elem("requestId", UUID.randomUUID().toString()) @@ -1443,7 +1477,8 @@ public class Ec2QueryHandler { String rtId = p.getFirst("RouteTableId"); String dest = p.getFirst("DestinationCidrBlock"); String gwId = p.getFirst("GatewayId"); - service.createRoute(region, rtId, dest, gwId); + String natGwId = p.getFirst("NatGatewayId"); + service.createRoute(region, rtId, dest, gwId, natGwId); return booleanResponse("CreateRoute"); } @@ -1567,6 +1602,7 @@ public class Ec2QueryHandler { private Response handleAllocateAddress(MultivaluedMap p, String region) { Address addr = service.allocateAddress(region); + applyResourceTags(p, region, "elastic-ip", addr.getAllocationId()); XmlBuilder xml = new XmlBuilder() .start("AllocateAddressResponse", AwsNamespaces.EC2) .elem("requestId", UUID.randomUUID().toString()) @@ -2162,7 +2198,8 @@ public class Ec2QueryHandler { if (rule.getToPort() != null) xml.elem("toPort", String.valueOf(rule.getToPort())); xml.elem("cidrIpv4", rule.getCidrIpv4()) .elem("cidrIpv6", rule.getCidrIpv6()) - .elem("description", rule.getDescription()); + .elem("description", rule.getDescription()) + .raw(tagSetXml(rule.getTags())); return xml.build(); } @@ -2192,6 +2229,7 @@ public class Ec2QueryHandler { xml.start("item") .elem("destinationCidrBlock", r.getDestinationCidrBlock()) .elem("gatewayId", r.getGatewayId()) + .elem("natGatewayId", r.getNatGatewayId()) .elem("state", r.getState()) .elem("origin", r.getOrigin()) .end("item"); diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java index 05c1652623..2377ac4f88 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java @@ -1420,11 +1420,11 @@ public class Ec2Service { return false; } - public List describeSecurityGroupRules(String region, String groupId, List ruleIds) { + public List describeSecurityGroupRules(String region, List groupIds, List ruleIds) { ensureDefaultResources(region); String regionPrefix = region + "::"; return securityGroupRules.scan(k -> k.startsWith(regionPrefix)).stream() - .filter(r -> groupId.isEmpty() || groupId.equals(r.getGroupId())) + .filter(r -> groupIds.isEmpty() || groupIds.contains(r.getGroupId())) .filter(r -> ruleIds.isEmpty() || ruleIds.contains(r.getSecurityGroupRuleId())) .collect(Collectors.toList()); } @@ -2063,6 +2063,8 @@ public class Ec2Service { if (subnet != null) { subnet.setTags(new ArrayList<>(tagList)); subnets.put(storeKey, subnet); return; } SecurityGroup sg = securityGroups.get(storeKey).orElse(null); if (sg != null) { sg.setTags(new ArrayList<>(tagList)); securityGroups.put(storeKey, sg); return; } + SecurityGroupRule sgRule = securityGroupRules.get(storeKey).orElse(null); + if (sgRule != null) { sgRule.setTags(new ArrayList<>(tagList)); securityGroupRules.put(storeKey, sgRule); return; } InternetGateway igw = internetGateways.get(storeKey).orElse(null); if (igw != null) { igw.setTags(new ArrayList<>(tagList)); internetGateways.put(storeKey, igw); return; } RouteTable rt = routeTables.get(storeKey).orElse(null); @@ -2076,7 +2078,9 @@ public class Ec2Service { NatGateway natGateway = natGateways.get(storeKey).orElse(null); if (natGateway != null) { natGateway.setTags(new ArrayList<>(tagList)); natGateways.put(storeKey, natGateway); return; } NetworkAcl networkAcl = networkAcls.get(storeKey).orElse(null); - if (networkAcl != null) { networkAcl.setTags(new ArrayList<>(tagList)); networkAcls.put(storeKey, networkAcl); } + if (networkAcl != null) { networkAcl.setTags(new ArrayList<>(tagList)); networkAcls.put(storeKey, networkAcl); return; } + Address address = addresses.get(storeKey).orElse(null); + if (address != null) { address.setTags(new ArrayList<>(tagList)); addresses.put(storeKey, address); } } public List> describeTags(String region, Map> filters) { @@ -2251,11 +2255,13 @@ public class Ec2Service { } } - public void createRoute(String region, String routeTableId, String destinationCidrBlock, String gatewayId) { + public void createRoute(String region, String routeTableId, String destinationCidrBlock, String gatewayId, String natGatewayId) { ensureDefaultResources(region); RouteTable rt = getRequiredRouteTable(region, routeTableId); - rt.getRoutes().add(new Route(destinationCidrBlock, gatewayId, "CreateRoute")); + Route route = new Route(destinationCidrBlock, gatewayId, "CreateRoute"); + route.setNatGatewayId(natGatewayId); + rt.getRoutes().add(route); routeTables.put(key(region, routeTableId), rt); } diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/model/Route.java b/src/main/java/io/github/hectorvent/floci/services/ec2/model/Route.java index 4ea42d4a31..4bc8b1f1c0 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/model/Route.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/model/Route.java @@ -9,6 +9,7 @@ public class Route { private String destinationCidrBlock; private String gatewayId; + private String natGatewayId; private String state = "active"; private String origin; @@ -26,6 +27,9 @@ public class Route { public String getGatewayId() { return gatewayId; } public void setGatewayId(String gatewayId) { this.gatewayId = gatewayId; } + public String getNatGatewayId() { return natGatewayId; } + public void setNatGatewayId(String natGatewayId) { this.natGatewayId = natGatewayId; } + public String getState() { return state; } public void setState(String state) { this.state = state; } diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java index e331f6031f..65e5ae0761 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java @@ -3054,4 +3054,163 @@ class Ec2IntegrationTest { // otherwise the AWS SDK for Go fails to deserialize interface endpoints. .body("CreateVpcEndpointResponse.vpcEndpoint.subnetIdSet.item", equalTo(subnet)); } + + @Test + @Order(317) + void subnetTagsFromCreateSurviveDescribe() { + String vpc = newVpc("10.37.0.0/16"); + String subnet = given() + .formParam("Action", "CreateSubnet") + .formParam("VpcId", vpc) + .formParam("CidrBlock", "10.37.1.0/24") + .formParam("TagSpecification.1.ResourceType", "subnet") + .formParam("TagSpecification.1.Tag.1.Key", "Name") + .formParam("TagSpecification.1.Tag.1.Value", "tagged-subnet") + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .extract().path("CreateSubnetResponse.subnet.subnetId"); + + given() + .formParam("Action", "DescribeSubnets") + .formParam("SubnetId.1", subnet) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then() + .statusCode(200) + .body("DescribeSubnetsResponse.subnetSet.item.tagSet.item.key", equalTo("Name")) + .body("DescribeSubnetsResponse.subnetSet.item.tagSet.item.value", equalTo("tagged-subnet")); + } + + @Test + @Order(318) + void routeNatGatewayIdRoundTrips() { + String vpc = newVpc("10.38.0.0/16"); + String rt = given() + .formParam("Action", "CreateRouteTable") + .formParam("VpcId", vpc) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .extract().path("CreateRouteTableResponse.routeTable.routeTableId"); + + given() + .formParam("Action", "CreateRoute") + .formParam("RouteTableId", rt) + .formParam("DestinationCidrBlock", "0.0.0.0/0") + .formParam("NatGatewayId", "nat-0123456789abcdef0") + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .body("CreateRouteResponse.return", equalTo("true")); + + given() + .formParam("Action", "DescribeRouteTables") + .formParam("RouteTableId.1", rt) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then() + .statusCode(200) + .body("DescribeRouteTablesResponse.routeTableSet.item.routeSet.item.natGatewayId", + equalTo("nat-0123456789abcdef0")); + } + + @Test + @Order(319) + void securityGroupRuleDescribableByRuleId() { + String vpc = newVpc("10.39.0.0/16"); + String sg = given() + .formParam("Action", "CreateSecurityGroup") + .formParam("GroupName", "rule-test") + .formParam("GroupDescription", "rule test") + .formParam("VpcId", vpc) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .extract().path("CreateSecurityGroupResponse.groupId"); + + String ruleId = given() + .formParam("Action", "AuthorizeSecurityGroupIngress") + .formParam("GroupId", sg) + .formParam("IpPermissions.1.IpProtocol", "tcp") + .formParam("IpPermissions.1.FromPort", "443") + .formParam("IpPermissions.1.ToPort", "443") + .formParam("IpPermissions.1.IpRanges.1.CidrIp", "10.0.0.0/16") + .formParam("TagSpecification.1.ResourceType", "security-group-rule") + .formParam("TagSpecification.1.Tag.1.Key", "Name") + .formParam("TagSpecification.1.Tag.1.Value", "allow-https") + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .extract().path("AuthorizeSecurityGroupIngressResponse.securityGroupRuleSet.item.securityGroupRuleId"); + + // The modern aws_vpc_security_group_ingress_rule reads back by security-group-rule-id, + // and its create-time tags must round-trip or Terraform recreates the rule every plan. + given() + .formParam("Action", "DescribeSecurityGroupRules") + .formParam("Filter.1.Name", "security-group-rule-id") + .formParam("Filter.1.Value.1", ruleId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then() + .statusCode(200) + .body("DescribeSecurityGroupRulesResponse.securityGroupRuleSet.item.securityGroupRuleId", + equalTo(ruleId)) + .body("DescribeSecurityGroupRulesResponse.securityGroupRuleSet.item.tagSet.item.key", + equalTo("Name")) + .body("DescribeSecurityGroupRulesResponse.securityGroupRuleSet.item.tagSet.item.value", + equalTo("allow-https")); + } + + @Test + @Order(320) + void securityGroupRuleIdParamAndFilterAreConjunctive() { + String vpc = newVpc("10.40.0.0/16"); + String sg = given() + .formParam("Action", "CreateSecurityGroup") + .formParam("GroupName", "conjunctive-test") + .formParam("GroupDescription", "conjunctive test") + .formParam("VpcId", vpc) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .extract().path("CreateSecurityGroupResponse.groupId"); + + String ruleId = given() + .formParam("Action", "AuthorizeSecurityGroupIngress") + .formParam("GroupId", sg) + .formParam("IpPermissions.1.IpProtocol", "tcp") + .formParam("IpPermissions.1.FromPort", "80") + .formParam("IpPermissions.1.ToPort", "80") + .formParam("IpPermissions.1.IpRanges.1.CidrIp", "10.0.0.0/16") + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .extract().path("AuthorizeSecurityGroupIngressResponse.securityGroupRuleSet.item.securityGroupRuleId"); + + // Param and filter naming different rules must intersect to nothing, not union to both. + given() + .formParam("Action", "DescribeSecurityGroupRules") + .formParam("SecurityGroupRuleId.1", ruleId) + .formParam("Filter.1.Name", "security-group-rule-id") + .formParam("Filter.1.Value.1", "sgr-00000000000000000") + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then() + .statusCode(200) + .body("DescribeSecurityGroupRulesResponse.securityGroupRuleSet", emptyOrNullString()); + + // Param and filter naming the same rule still match. + given() + .formParam("Action", "DescribeSecurityGroupRules") + .formParam("SecurityGroupRuleId.1", ruleId) + .formParam("Filter.1.Name", "security-group-rule-id") + .formParam("Filter.1.Value.1", ruleId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then() + .statusCode(200) + .body("DescribeSecurityGroupRulesResponse.securityGroupRuleSet.item.securityGroupRuleId", + equalTo(ruleId)); + } }