From e44df51a79b0ebcaf0755d0fd3fac73c6106932c Mon Sep 17 00:00:00 2001 From: Matan Baruch <36934912+matanbaruch@users.noreply.github.com> Date: Wed, 2 Sep 2026 17:15:13 +0300 Subject: [PATCH] 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), 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) 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. --- .../floci/services/ec2/Ec2Service.java | 3 ++- .../floci/services/ec2/Ec2ServiceTest.java | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) 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 8e82b83471..f83bcfc9fb 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 @@ -5732,8 +5732,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider { } private boolean matchesValue(List 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> filters, String region) { 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 ab693248e2..6668d35fe5 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 @@ -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 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),