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),