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

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