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.
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-10-29 00:32:59 -04:00
1 parent 1356cbbc84
commit d68876edcc
4 files changed
+52 -5

No files matched your search

+1 -1
View File
@@ -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. |
@@ -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) {
@@ -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;
}
@@ -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<String> 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),