mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(ec2): round-trip create-time tags and route/SG-rule attributes (#1763)
Create-time values were dropped, causing perpetual terraform plan diffs: - Inline TagSpecification tags now round-trip on create for subnets, route tables, internet gateways, elastic IPs, security groups, and security group rules (each rule gets its own tag copy). - CreateRoute stores and returns natGatewayId (was only gatewayId), in both the query handler and the CloudFormation provisioner. - DescribeSecurityGroupRules supports the security-group-rule-id filter and multiple group-id values; the SecurityGroupRuleId parameter and the filter are intersected (conjunctive), matching AWS semantics. Closes #1463 Supersedes #1491 Co-authored-by: seyi idowu <160403633+awsvigilante@users.noreply.github.com>
This commit is contained in:
1 parent
ebf5a2e847
commit
5cce73da45
5 files changed
+224
-19
No files matched your search
+2
-4
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
@@ -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<String, String> p, String region, String resourceType, String resourceId) {
|
||||
List<Tag> 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<String, String> p, String region,
|
||||
List<SecurityGroupRule> rules) {
|
||||
List<Tag> 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<IpPermission> perms = parseIpPermissions(p, "IpPermissions");
|
||||
List<SecurityGroupRule> 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<IpPermission> perms = parseIpPermissions(p, "IpPermissions");
|
||||
List<SecurityGroupRule> 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<String, String> p, String region) {
|
||||
Map<String, List<String>> filters = getFilters(p);
|
||||
// The AWS SDK sends the security group id as a filter with name "group-id"
|
||||
String groupId = "";
|
||||
List<String> groupIdFilter = filters.get("group-id");
|
||||
if (groupIdFilter != null && !groupIdFilter.isEmpty()) {
|
||||
groupId = groupIdFilter.get(0);
|
||||
}
|
||||
List<String> ruleIds = getList(p, "SecurityGroupRuleId");
|
||||
List<SecurityGroupRule> 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<String> paramRuleIds = getList(p, "SecurityGroupRuleId");
|
||||
List<String> filterRuleIds = filters.getOrDefault("security-group-rule-id", List.of());
|
||||
List<String> ruleIds = paramRuleIds.isEmpty() || filterRuleIds.isEmpty()
|
||||
? (paramRuleIds.isEmpty() ? filterRuleIds : paramRuleIds)
|
||||
: paramRuleIds.stream().filter(filterRuleIds::contains).toList();
|
||||
boolean unsatisfiable = !paramRuleIds.isEmpty() && !filterRuleIds.isEmpty() && ruleIds.isEmpty();
|
||||
List<String> groupIds = filters.getOrDefault("group-id", List.of());
|
||||
List<SecurityGroupRule> 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<String, String> 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<String, String> 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<String, String> 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");
|
||||
|
||||
@@ -1420,11 +1420,11 @@ public class Ec2Service {
|
||||
return false;
|
||||
}
|
||||
|
||||
public List<SecurityGroupRule> describeSecurityGroupRules(String region, String groupId, List<String> ruleIds) {
|
||||
public List<SecurityGroupRule> describeSecurityGroupRules(String region, List<String> groupIds, List<String> 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<Map<String, String>> describeTags(String region, Map<String, List<String>> 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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user