fix(ec2): CreateNetworkInterface honours the PrivateIpAddresses entry marked Primary (#7031)

* fix(ec2): CreateNetworkInterface honours the PrivateIpAddresses entry marked Primary

The Terraform AWS provider sends private_ips as PrivateIpAddresses.N.PrivateIpAddress
plus Primary=true. The handler dropped the Primary flag, so the ENI got an
auto-allocated primary and the requested address became a secondary. The entry
marked Primary is now the primary (checked and reserved like PrivateIpAddress),
other entries are secondaries and are refused with InvalidIPAddress.InUse when
held. PrivateIpAddress together with a Primary entry is InvalidParameterCombination.

* test(ec2): cover duplicate Primary entries and secondaries equal to the primary in CreateNetworkInterface

* fix(ec2): check CreateNetworkInterface secondaries in canonical form before allocating the primary

A refused secondary no longer leaves the auto-assigned primary leased on the
VPC network, and a zero-padded secondary can no longer slip past the in-use
and duplicate checks or be stored padded.

* test(ec2): cover an auto-assigned primary that equals a requested secondary
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-10-30 21:57:10 -04:00
1 parent 1945117bfe
commit 2a4f57321c
4 files changed
+272 -12

No files matched your search

@@ -5142,12 +5142,31 @@ public class Ec2QueryHandler {
String description = p.getFirst("Description");
String privateIpAddress = p.getFirst("PrivateIpAddress");
List<String> privateIpAddresses = new ArrayList<>();
String primaryFromList = null;
for (int i = 1; ; i++) {
String addr = p.getFirst("PrivateIpAddresses." + i + ".PrivateIpAddress");
String entry = "PrivateIpAddresses." + i;
String addr = p.getFirst(entry + ".PrivateIpAddress");
if (addr == null) {
break;
}
privateIpAddresses.add(addr);
if (Boolean.parseBoolean(p.getFirst(entry + ".Primary"))) {
if (primaryFromList != null) {
throw new AwsException("InvalidParameterValue",
"Only one private IP address can be designated as primary", 400);
}
primaryFromList = addr;
} else {
privateIpAddresses.add(addr);
}
}
// AWS: "If you specify an IP address, you cannot indicate any IP addresses specified in
// privateIpAddresses as primary (only one IP address can be designated as primary)."
if (primaryFromList != null) {
if (privateIpAddress != null && !privateIpAddress.isBlank()) {
throw new AwsException("InvalidParameterCombination",
"PrivateIpAddress cannot be combined with a PrivateIpAddresses entry marked Primary", 400);
}
privateIpAddress = primaryFromList;
}
List<String> securityGroupIds = getList(p, "SecurityGroupId");
if (securityGroupIds.isEmpty()) {
@@ -14461,6 +14461,18 @@ 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.
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);
if (!ip.equals(requestedIp) && secondaries.add(ip)) {
requireRequestedPrivateIpFree(region, subnetId, ip);
}
}
String primaryIp = requestedIp != null ? requestedIp : assignPrivateIp(region, subnetId);
String primaryDns = AwsRegions.ec2PrivateIpDnsName(primaryIp, region);
@@ -14486,17 +14498,15 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
primary.setPrivateDnsName(primaryDns);
primary.setPrimary(true);
ipList.add(primary);
if (privateIpAddresses != null) {
for (String extra : privateIpAddresses) {
if (extra == null || extra.isBlank() || extra.equals(primaryIp)) {
continue;
}
NetworkInterfacePrivateIpAddress secondary = new NetworkInterfacePrivateIpAddress();
secondary.setPrivateIpAddress(extra);
secondary.setPrivateDnsName(AwsRegions.ec2PrivateIpDnsName(extra, region));
secondary.setPrimary(false);
ipList.add(secondary);
for (String extra : secondaries) {
if (extra.equals(primaryIp)) {
continue;
}
NetworkInterfacePrivateIpAddress secondary = new NetworkInterfacePrivateIpAddress();
secondary.setPrivateIpAddress(extra);
secondary.setPrivateDnsName(AwsRegions.ec2PrivateIpDnsName(extra, region));
secondary.setPrimary(false);
ipList.add(secondary);
}
ni.setPrivateIpAddresses(ipList);