fix(ec2): tolerate a null groupId in the network-interface group-id filter (#2921)

The group-id filter for DescribeNetworkInterfaces resolves to
matchesValue(List<String>, String), which called value.matches(...) with
no null guard. A real GroupIdentifier entry whose groupId is null (a
name-only association) therefore NPE'd the whole request, unlike the
sibling matchesValue(String, List<String>) overload that already
normalizes with Objects.toString. Normalize the value the same way so
every network-interface leaf filter (group-id, subnet-id, status,
private-dns-name, ...) tolerates a null field instead of failing.

Adds a regression test that carries a null-groupId entry ahead of a real
group on the same interface, so the null path is exercised and the real
group still matches.
This commit is contained in:
Matan Baruch authored and GitHub committed 2026-09-02 10:15:13 -04:00
1 parent 5b3c0e01e3
commit e44df51a79
2 files changed
+27 -1

No files matched your search

@@ -5732,8 +5732,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
}
private boolean matchesValue(List<String> patterns, String value) {
String normalizedValue = Objects.toString(value, "");
return patterns.stream()
.anyMatch(pattern -> value.matches(wildcardToRegex(pattern)));
.anyMatch(pattern -> normalizedValue.matches(wildcardToRegex(pattern)));
}
private boolean matchesFilters(Object resource, Map<String, List<String>> filters, String region) {
@@ -162,6 +162,31 @@ class Ec2ServiceTest {
assertEquals(1, retry.getInstances().size());
}
/**
* A NetworkInterface can carry a real GroupIdentifier entry whose groupId is null (a name-only
* association). The group-id filter must skip that entry instead of NPE-ing on
* {@code null.matches(...)} in the shared value matcher, while a real group on the same interface
* still matches. The null entry is evaluated first so the filter actually exercises the null path.
*/
@Test
void describeNetworkInterfacesGroupIdFilterToleratesNullGroupId() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory());
String subnetId = service.describeSubnets("us-east-1", List.of(), Map.of())
.getFirst().getSubnetId();
NetworkInterface eni = service.createNetworkInterface("us-east-1", subnetId, null,
null, List.of(), List.of(), List.of());
eni.getGroups().add(new GroupIdentifier(null, "name-only"));
eni.getGroups().add(new GroupIdentifier("sg-realgroup000000", "real"));
List<NetworkInterface> matched = service.describeNetworkInterfaces("us-east-1", List.of(),
Map.of("group-id", List.of("sg-realgroup000000")), 0, null).networkInterfaces();
assertTrue(matched.stream()
.anyMatch(n -> eni.getNetworkInterfaceId().equals(n.getNetworkInterfaceId())));
}
@Test
void runInstancesRequiresImageIdInsteadOfDefaulting() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),