fix(ec2): honor network interface selectors in AssociateAddress (#7506)

* fix(ec2): associate elastic addresses with primary and detached interfaces

Signed-off-by: Andre Kurait <akurait@amazon.com>
Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
(cherry picked from commit 28c17835d6e7dc480f5f5716e206fbbe5b81bee0)
Signed-off-by: Jackson Price <jxnprice@amazon.com>

* test(ec2): clean up partial address-association fixtures

Signed-off-by: Jackson Price <jxnprice@amazon.com>

* fix(ec2): serialize interface deletion and resolve managed interfaces

Signed-off-by: Jackson Price <jxnprice@amazon.com>

---------

Signed-off-by: Andre Kurait <akurait@amazon.com>
Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
Signed-off-by: Jackson Price <jxnprice@amazon.com>
Co-authored-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Jackson PriceandAndre Kurait authored and GitHub committed 2026-11-04 08:28:01 -04:00
1 parent 97dc04e963
commit ee78a41f16
5 files changed
+288 -14

No files matched your search

@@ -4855,7 +4855,8 @@ public class Ec2QueryHandler {
private Response handleAssociateAddress(MultivaluedMap<String, String> p, String region) {
String allocationId = p.getFirst("AllocationId");
String instanceId = p.getFirst("InstanceId");
Address addr = service.associateAddress(region, allocationId, instanceId, p.getFirst("PrivateIpAddress"));
Address addr = service.associateAddress(region, allocationId, instanceId,
p.getFirst("NetworkInterfaceId"), p.getFirst("PrivateIpAddress"));
XmlBuilder xml = new XmlBuilder()
.start("AssociateAddressResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
@@ -13193,6 +13193,71 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
return associateAddress(region, allocationId, instanceId, null);
}
public Address associateAddress(String region, String allocationId, String instanceId,
String networkInterfaceId, String privateIpAddress) {
if (!isSet(networkInterfaceId)) {
if (!isSet(instanceId)) {
throw new AwsException("MissingParameter",
"Specify either InstanceId or NetworkInterfaceId.", 400);
}
return associateAddress(region, allocationId, instanceId, privateIpAddress);
}
if (isSet(instanceId)) {
throw new AwsException("InvalidParameterCombination",
"Specify either InstanceId or NetworkInterfaceId, but not both.", 400);
}
ensureDefaultResources(region);
synchronized (privateIpAllocationLock) {
NetworkInterface networkInterface = networkInterfaces.get(key(region, networkInterfaceId)).orElse(null);
if (networkInterface == null) {
networkInterface = describeNetworkInterfaces(region, List.of(networkInterfaceId), Map.of())
.networkInterfaces().getFirst();
}
networkInterface = releaseIfHostIsGone(region, networkInterface);
NetworkInterfaceAttachment attachment = networkInterface.getAttachment();
if (attachment != null) {
if (attachment.getDeviceIndex() != 0) {
throw new AwsException("Unsupported",
"Elastic IP association to a non-primary attached interface is not supported.", 400);
}
return associateAddress(region, allocationId, attachment.getInstanceId(), privateIpAddress);
}
if (!"available".equals(networkInterface.getStatus())) {
throw new AwsException("Unsupported",
"Elastic IP association to a service-managed interface is not supported.", 400);
}
String selectedPrivateIp = isSet(privateIpAddress)
? privateIpAddress : networkInterface.getPrivateIpAddress();
if (networkInterface.getPrivateIpAddresses().stream()
.noneMatch(ip -> selectedPrivateIp.equals(ip.getPrivateIpAddress()))) {
throw new AwsException("InvalidParameterValue",
"The private IP address is not assigned to the selected network interface.", 400);
}
Address address = getRequiredAddress(region, allocationId);
if (isHeldByNatGateway(region, allocationId)) {
throw new AwsException("Resource.AlreadyAssociated",
"The Elastic IP address is already associated with a NAT gateway.", 400);
}
if (address.getAssociationId() != null) {
disassociateAddress(region, address.getAssociationId());
}
for (Address previous : addresses.scan(k -> k.startsWith(region + "::"))) {
if (previous.getAssociationId() != null
&& networkInterfaceId.equals(previous.getNetworkInterfaceId())
&& selectedPrivateIp.equals(previous.getPrivateIpAddress())) {
disassociateAddress(region, previous.getAssociationId());
}
}
address.setInstanceId(null);
address.setNetworkInterfaceId(networkInterfaceId);
address.setPrivateIpAddress(selectedPrivateIp);
address.setAssociationId("eipassoc-" + randomHex(17));
updateStoredAddressAssociation(address);
addresses.put(key(region, allocationId), address);
return address;
}
}
/**
* Associates the Elastic IP with one private address of the instance.
*
@@ -15182,13 +15247,15 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
* behind, so the status is what says the interface is spoken for.
*/
public void deleteNetworkInterface(String region, String networkInterfaceId) {
NetworkInterface ni = requireStandaloneNetworkInterface(region, networkInterfaceId);
if (ni.getAttachment() != null || "in-use".equals(ni.getStatus())) {
throw new AwsException("InvalidParameterValue",
"Network interface '" + networkInterfaceId + "' is currently in use", 400);
synchronized (privateIpAllocationLock) {
NetworkInterface ni = requireStandaloneNetworkInterface(region, networkInterfaceId);
if (ni.getAttachment() != null || "in-use".equals(ni.getStatus())) {
throw new AwsException("InvalidParameterValue",
"Network interface '" + networkInterfaceId + "' is currently in use", 400);
}
releaseInterfaceAddresses(region, networkInterfaceId, true);
networkInterfaces.delete(key(region, networkInterfaceId));
}
releaseInterfaceAddresses(region, networkInterfaceId, true);
networkInterfaces.delete(key(region, networkInterfaceId));
}
/**