fix(ec2): keep network interface pagination stable across storage scans (#9666)

Sort the matching interfaces before applying an offset token so a reordered scan cannot repeat an interface or skip a result.

Co-authored-by: Andre Kurait <AndreKurait@gmail.com>
Signed-off-by: Andre Kurait <akurait@amazon.com>
Signed-off-by: Jackson Price <jxnprice@amazon.com>
This commit is contained in:
Jackson PriceandAndre Kurait authored and GitHub committed 2026-11-28 15:06:56 -04:00
1 parent d44f29cc15
commit 0609698473
2 files changed
+61

No files matched your search

@@ -17105,6 +17105,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
// Phase 5: pagination
if (maxResults > 0) {
// Offset tokens must survive changes in storage scan order.
result.sort(Comparator.comparing(NetworkInterface::getNetworkInterfaceId));
int total = result.size();
int toIndex = Math.min(offset + maxResults, total);
List<NetworkInterface> page = (offset < total)
@@ -1927,6 +1927,65 @@ class Ec2ServiceTest {
assertEquals(1, result.networkInterfaces().size());
}
@ParameterizedTest
@ValueSource(booleans = {false, true})
void describeNetworkInterfacesPaginationSurvivesStorageReordering(boolean standalone) {
AccountAwareStorageBackend<Instance> instances =
spy(AccountAwareStorageBackend.inMemory("000000000000"));
AccountAwareStorageBackend<NetworkInterface> interfaces =
spy(AccountAwareStorageBackend.inMemory("000000000000"));
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
new Ec2InstanceTypeCatalog(),
new InMemoryStorageFactory(Map.of(
"ec2-instances.json", instances, "ec2-network-interfaces.json", interfaces)));
List<Instance> launched = new ArrayList<>();
List<NetworkInterface> created = new ArrayList<>();
if (standalone) {
String subnetId = service.describeSubnets("us-east-1", List.of(), Map.of()).getFirst().getSubnetId();
for (int i = 0; i < 6; i++) {
created.add(service.createNetworkInterface(
"us-east-1", subnetId, null, null, List.of(), List.of(), List.of()));
}
} else {
launched.addAll(service.runInstances("us-east-1", "ami-1234567890abcdef0", "t3.micro",
6, 6, null, List.of(), null, null, List.of(), null, null).getInstances());
}
String vpcId = standalone ? created.getFirst().getVpcId() : launched.getFirst().getVpcId();
Map<String, List<String>> filters = Map.of("vpc-id", List.of(vpcId));
doReturn(launched).when(instances).scanForAccount(eq("000000000000"), any());
doReturn(created).when(interfaces).scanForAccount(eq("000000000000"), any());
NetworkInterfaceListResult first =
service.describeNetworkInterfaces("us-east-1", List.of(), filters, 5, null);
assertEquals(5, first.networkInterfaces().size());
assertNotNull(first.nextToken());
// Another write can reorder a storage scan without changing the matching interfaces.
doReturn(launched.reversed()).when(instances).scanForAccount(eq("000000000000"), any());
doReturn(created.reversed()).when(interfaces).scanForAccount(eq("000000000000"), any());
NetworkInterfaceListResult last =
service.describeNetworkInterfaces("us-east-1", List.of(), filters, 5, first.nextToken());
assertEquals(1, last.networkInterfaces().size());
assertNull(last.nextToken());
List<String> ids = new ArrayList<>(first.networkInterfaces().stream()
.map(NetworkInterface::getNetworkInterfaceId).toList());
ids.addAll(last.networkInterfaces().stream().map(NetworkInterface::getNetworkInterfaceId).toList());
Set<String> expected = standalone
? new HashSet<>(created.stream().map(NetworkInterface::getNetworkInterfaceId).toList())
: new HashSet<>(launched.stream()
.map(instance -> instance.getNetworkInterfaces().getFirst().getNetworkInterfaceId()).toList());
assertEquals(6, new HashSet<>(ids).size(), "Continuation must not repeat an interface");
assertEquals(expected, new HashSet<>(ids));
doReturn(launched).when(instances).scanForAccount(eq("000000000000"), any());
doReturn(created).when(interfaces).scanForAccount(eq("000000000000"), any());
assertEquals(last.networkInterfaces().stream().map(NetworkInterface::getNetworkInterfaceId).toList(),
service.describeNetworkInterfaces("us-east-1", List.of(), filters, 5, first.nextToken())
.networkInterfaces().stream().map(NetworkInterface::getNetworkInterfaceId).toList());
}
@ParameterizedTest
@CsvSource({"false,false", "false,true", "true,false", "true,true"})
void describeNetworkInterfacesFiltersActualAddressAssociations(boolean suppliedInterface, boolean defaultVpc) {