fix(ec2): validate explicit secondary private IPs (#8836)

* fix(ec2): validate explicit secondary private IPs
This commit is contained in:
XuX authored and GitHub committed 2026-11-18 08:49:18 -04:00
1 parent 4292bca198
commit da3429cc06
2 files changed
+23 -5

No files matched your search

@@ -16423,14 +16423,14 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
if (requestedIp != null) {
requireRequestedPrivateIpFree(region, subnetId, requestedIp);
}
// Secondaries are folded and checked before the primary is allocated, so a refused one
// leaves no lease behind on the VPC network. Their range is not checked, as before.
// Secondaries are folded and validated before the primary is allocated, so a refused one
// leaves no lease behind on the VPC network.
Set<String> secondaries = new LinkedHashSet<>();
for (String extra : privateIpAddresses == null ? List.<String>of() : privateIpAddresses) {
if (extra == null || extra.isBlank()) {
continue;
}
String ip = canonicalIpv4(extra).orElse(extra);
String ip = validateRequestedPrivateIp(extra, subnet, null, 1);
if (!ip.equals(requestedIp) && secondaries.add(ip)) {
requireRequestedPrivateIpFree(region, subnetId, ip);
}
@@ -696,9 +696,12 @@ class Ec2ServiceTest {
// CreateNetworkInterface now refuses an out-of-subnet primary, so the record is persisted
// the way an older build left it.
NetworkInterface legacy = service.createNetworkInterface(region, small.getSubnetId(), "legacy-addresses",
null, List.of("10.97.0.0", "10.97.0.15"), List.of(), List.of());
null, List.of("10.97.0.4", "10.97.0.5"), List.of(), List.of());
legacy.setPrivateIpAddress("10.98.0.4");
legacy.getPrivateIpAddresses().getFirst().setPrivateIpAddress("10.98.0.4");
for (int index = 0; index < legacy.getPrivateIpAddresses().size(); index++) {
legacy.getPrivateIpAddresses().get(index).setPrivateIpAddress(
index % 2 == 0 ? "10.97.0.0" : "10.97.0.15");
}
interfaceStore.put(region + "::" + legacy.getNetworkInterfaceId(), legacy);
assertEquals(11, service.describeSubnets(region, List.of(small.getSubnetId()), Map.of())
.getFirst().getAvailableIpAddressCount());
@@ -5017,6 +5020,21 @@ class Ec2ServiceTest {
}
}
@Test
void createNetworkInterfaceRefusesAnExplicitSecondaryTheSubnetCannotAssign() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory());
String subnetId = privateIpSubnet(service);
for (String unassignable : List.of("10.50.2.10", "10.50.1.1", "10.50.1.255", "not-an-ip")) {
AwsException error = assertThrows(AwsException.class, () -> service.createNetworkInterface(
"us-east-1", subnetId, "bad-secondary", "10.50.1.70", List.of(unassignable),
List.of(), List.of()));
assertEquals("InvalidParameterValue", error.getErrorCode(), unassignable);
}
}
@Test
void anExplicitAddressADeletedInterfaceHeldCanBeTakenAgain() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),