fix(ec2): guard null group entries in DescribeNetworkInterfaces group-id filter (#2887)

The group-id filter dereferenced each of an interface's group entries with no
null check, unlike the sibling filters in the same switch (attachment.instance-id
already guards getAttachment() != null). When an interface's group list carried a
null entry, matchesFilter threw a NullPointerException and DescribeNetworkInterfaces
returned a 500.

Terraform and Pulumi poll DescribeNetworkInterfaces filtered by group-id while
deleting a security group, so the 500 turned an SG delete into an indefinite retry
loop until the client timed out. Guard each entry, matching the null-tolerant style
of the adjacent cases.
This commit is contained in:
Matan Baruch authored and GitHub committed 2026-09-02 10:16:59 -04:00
1 parent e44df51a79
commit 5c71713cad
2 files changed
+24 -1

No files matched your search

@@ -5968,7 +5968,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
case "subnet-id" -> matchesValue(values, ni.getSubnetId());
case "vpc-id" -> matchesValue(values, ni.getVpcId());
case "group-id" -> ni.getGroups().stream()
.anyMatch(g -> matchesValue(values, g.getGroupId()));
.anyMatch(g -> g != null && matchesValue(values, g.getGroupId()));
case "status" -> matchesValue(values, ni.getStatus());
case "attachment.instance-id" -> ni.getAttachment() != null
&& matchesValue(values, ni.getAttachment().getInstanceId());
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.services.ec2.model.EbsBlockDevice;
import io.github.hectorvent.floci.services.ec2.model.GroupIdentifier;
import io.github.hectorvent.floci.services.ec2.model.Image;
import io.github.hectorvent.floci.services.ec2.model.Instance;
import io.github.hectorvent.floci.services.ec2.model.NetworkInterfaceListResult;
import io.github.hectorvent.floci.services.ec2.model.IpPermission;
import io.github.hectorvent.floci.services.ec2.model.Ipv6Range;
import io.github.hectorvent.floci.services.ec2.model.SecurityGroupRule;
@@ -83,6 +84,28 @@ class Ec2ServiceTest {
verifyNoInteractions(containerManager);
}
@Test
void describeNetworkInterfacesGroupIdFilterSkipsNullGroupEntry() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class),
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
new InMemoryStorageFactory());
Reservation reservation = service.runInstances("us-east-1", "ami-1234567890abcdef0", "t3.micro",
1, 1, null, List.of(), null, null, List.of(), null, null);
Instance inst = reservation.getInstances().getFirst();
String groupId = inst.getNetworkInterfaces().getFirst().getGroups().getFirst().getGroupId();
// A null entry in an interface's group list must not crash the group-id filter: the interface
// is still matched on its real group and the null is skipped. Regression: matchesFilter
// dereferenced the entry with no null-guard, so DescribeNetworkInterfaces returned a 500 that
// hung a Terraform/Pulumi security-group delete (it polls this call until the group is gone).
inst.getNetworkInterfaces().getFirst().getGroups().add(null);
NetworkInterfaceListResult result = service.describeNetworkInterfaces("us-east-1", List.of(),
Map.of("group-id", List.of(groupId)), 0, null);
assertEquals(1, result.networkInterfaces().size());
}
@Test
void awaitContainerLaunchReportsTerminatedContainer() {
Ec2Service service = new Ec2Service(mockConfig(false), mock(Ec2ContainerManager.class),