From d68876edccd72fe7a3d699474cd7a1b2f5d0c51e Mon Sep 17 00:00:00 2001 From: Allen Sanborn Date: Wed, 28 Oct 2026 23:32:59 -0500 Subject: [PATCH] fix(ec2): ignore ports when revoking an all-traffic rule (#6895) * fix(ec2): ignore ports when revoking an all-traffic rule Revoking an "all traffic" (-1) rule compared ports literally. Terraform's aws_security_group revokes AWS's default egress rule as -1 with FromPort=0 and ToPort=0, which never matched the stored rule (it has no ports), so every new group kept allowing all egress and the next plan wanted to remove it again. AWS ignores ports for -1. Protocols are now compared after the same normalization the security-group evaluation uses, so a revoke naming protocol 6 also matches a tcp rule, as on AWS. * test(ec2): name the all-traffic revoke test a service test * test(ec2): move the all-traffic revoke tests into Ec2ServiceTest Use Ec2ServiceTest's existing mockModeService() instead of a copied mock-mode harness, as requested in review. --- docs/services/ec2.md | 2 +- .../floci/services/ec2/Ec2Service.java | 11 +++-- .../services/ec2/SecurityGroupPolicy.java | 2 +- .../floci/services/ec2/Ec2ServiceTest.java | 42 +++++++++++++++++++ 4 files changed, 52 insertions(+), 5 deletions(-) diff --git a/docs/services/ec2.md b/docs/services/ec2.md index 342c5f6fe4..9f9fa76caa 100644 --- a/docs/services/ec2.md +++ b/docs/services/ec2.md @@ -334,7 +334,7 @@ private gateway lifecycles are not modelled. | DeleteSecurityGroup | Deletes a security group from the local EC2 store. | | AuthorizeSecurityGroupIngress | Adds inbound permissions. Sources may be IPv4 ranges, IPv6 ranges, or another security group (`UserIdGroupPairs`, sent on the wire as `Groups`); prefix list sources are not stored. One rule is stored per source, each carrying its own description. | | AuthorizeSecurityGroupEgress | Adds outbound permissions, with the same source types as the inbound call. | -| RevokeSecurityGroupIngress | Removes inbound permissions. Matches on protocol and port range only, so it removes every permission on that port regardless of source. | +| RevokeSecurityGroupIngress | Removes inbound permissions. Matches on protocol and port range only, so it removes every permission on that port regardless of source. Protocol numbers and names match each other (`6` is `tcp`), and an all-traffic (`-1`) permission matches whatever ports the request names, as on AWS (Terraform revokes the default egress rule as `-1` with ports `0`). | | RevokeSecurityGroupEgress | Removes outbound permissions, matched the same way as the inbound call. | | GetSecurityGroupsForVpc | Lists the security groups belonging to one VPC, with the same filters as the describe call. | | DescribeSecurityGroupRules | Lists stored security group rules. | 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 c63fffaad5..a10e0047fb 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 @@ -8155,9 +8155,14 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab } private boolean sameProtocolAndPorts(IpPermission a, IpPermission b) { - return Objects.equals(a.getIpProtocol(), b.getIpProtocol()) - && Objects.equals(a.getFromPort(), b.getFromPort()) - && Objects.equals(a.getToPort(), b.getToPort()); + String protocol = SecurityGroupPolicy.normalizeProtocol(a.getIpProtocol()); + if (!Objects.equals(protocol, SecurityGroupPolicy.normalizeProtocol(b.getIpProtocol()))) { + return false; + } + // "All traffic" has no ports, and AWS ignores any a request names: Terraform's + // aws_security_group revokes the default egress rule as -1 with FromPort=0, ToPort=0. + return "-1".equals(protocol) + || (Objects.equals(a.getFromPort(), b.getFromPort()) && Objects.equals(a.getToPort(), b.getToPort())); } private boolean hasSources(IpPermission perm) { diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/SecurityGroupPolicy.java b/src/main/java/io/github/hectorvent/floci/services/ec2/SecurityGroupPolicy.java index 27103f3391..bc9bc9be35 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/SecurityGroupPolicy.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/SecurityGroupPolicy.java @@ -63,7 +63,7 @@ public final class SecurityGroupPolicy { return true; } - private static String normalizeProtocol(String protocol) { + static String normalizeProtocol(String protocol) { if (protocol == null) { return null; } diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java index a9c312a6b0..d8e8a202e5 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java @@ -6388,6 +6388,39 @@ class Ec2ServiceTest { assertEquals(1, rules.stream().filter(r -> "2001:db8:1::/64".equals(r.getCidrIpv6())).count()); } + /** + * Terraform's aws_security_group revokes AWS's default allow-all egress rule as {@code -1} with + * {@code FromPort=0, ToPort=0}. The revoke compared ports literally, never matched the stored rule + * (which has none), and every new group kept allowing all egress. + */ + @Test + void revokingAllTrafficIgnoresThePortsTheRequestNames() { + Ec2Service service = mockModeService(); + String vpcId = service.createVpc("us-east-1", "10.74.0.0/16", false).getVpcId(); + String groupId = service.createSecurityGroup("us-east-1", "tf", "x", vpcId).getGroupId(); + + service.revokeSecurityGroupEgress("us-east-1", groupId, List.of(cidrPermission("-1", 0, 0))); + + assertTrue(service.describeSecurityGroups("us-east-1", List.of(groupId), List.of(), Map.of()) + .getFirst().getIpPermissionsEgress().isEmpty(), "the default allow-all egress is gone"); + } + + @Test + void portsStillMatterForOtherProtocols() { + Ec2Service service = mockModeService(); + String vpcId = service.createVpc("us-east-1", "10.76.0.0/16", false).getVpcId(); + String groupId = service.createSecurityGroup("us-east-1", "tf", "x", vpcId).getGroupId(); + service.authorizeSecurityGroupIngress("us-east-1", groupId, List.of(cidrPermission("tcp", 22, 22))); + + service.revokeSecurityGroupIngress("us-east-1", groupId, List.of(cidrPermission("tcp", 0, 0))); + assertEquals(1, service.describeSecurityGroups("us-east-1", List.of(groupId), List.of(), Map.of()) + .getFirst().getIpPermissions().size(), "port 0 is not port 22"); + + service.revokeSecurityGroupIngress("us-east-1", groupId, List.of(cidrPermission("6", 22, 22))); + assertTrue(service.describeSecurityGroups("us-east-1", List.of(groupId), List.of(), Map.of()) + .getFirst().getIpPermissions().isEmpty(), "protocol 6 is tcp"); + } + /** Ingress only: createSecurityGroup seeds a default allow-all egress rule. */ private static List ingressRuleIds(Ec2Service service, String groupId) { return service.describeSecurityGroupRules("us-east-1", List.of(groupId), List.of()) @@ -6424,6 +6457,15 @@ class Ec2ServiceTest { return perm; } + private static IpPermission cidrPermission(String protocol, int fromPort, int toPort) { + IpPermission perm = new IpPermission(); + perm.setIpProtocol(protocol); + perm.setFromPort(fromPort); + perm.setToPort(toPort); + perm.getIpRanges().add(new IpRange("0.0.0.0/0", null)); + return perm; + } + private static Ec2Service sgService() { return new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class), mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class),