mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(ec2): refuse an explicit CreateNetworkInterface address that is already in use (#6908)
This commit is contained in:
1 parent
772b444850
commit
146ba44c92
3 files changed
+224
-13
No files matched your search
@@ -4662,22 +4662,37 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
* case the instance keeps it the way it keeps a synthesised address.
|
||||
*/
|
||||
private void claimRequestedPrivateIp(String region, String subnetId, String address) {
|
||||
if (requireRequestedPrivateIpFree(region, subnetId, address)) {
|
||||
vpcNetworkManager.claimPrivateIp(region, subnetId, address);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The in-use half of {@link #claimRequestedPrivateIp}, without the lease. CreateNetworkInterface
|
||||
* uses it alone: an interface has no container to hold the address on the VPC network, and
|
||||
* DeleteNetworkInterface returns no lease, so one taken here would keep the address from
|
||||
* automatic allocation after the interface is gone. {@link #assignPrivateIp} already steps
|
||||
* around addresses stored interfaces hold. Callers must hold {@link #privateIpAllocationLock}.
|
||||
*
|
||||
* @return whether the address lies in a Docker-backed range, where a lease could be taken
|
||||
*/
|
||||
private boolean requireRequestedPrivateIpFree(String region, String subnetId, String address) {
|
||||
if (privateIpsTaken(region, subnetId).contains(address)) {
|
||||
throw new AwsException("InvalidIPAddress.InUse", "Address " + address + " is in use.", 400);
|
||||
}
|
||||
if (vpcNetworkManager == null || subnetId == null) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
boolean leasable = vpcNetworkManager.effectiveSubnetCidr(region, subnetId)
|
||||
.filter(cidr -> Ipv4Cidrs.contains(cidr, address + "/32"))
|
||||
.isPresent();
|
||||
if (!leasable) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
if (privateIpsInUseInEveryAccount(region, subnetId).contains(address)) {
|
||||
throw new AwsException("InvalidIPAddress.InUse", "Address " + address + " is in use.", 400);
|
||||
}
|
||||
vpcNetworkManager.claimPrivateIp(region, subnetId, address);
|
||||
return true;
|
||||
}
|
||||
|
||||
/** {@link #privateIpsInUse}, across every account rather than only the caller's. */
|
||||
@@ -4777,8 +4792,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
|
||||
private static void addIfSet(Set<String> addresses, String address) {
|
||||
if (address != null && !address.isBlank()) {
|
||||
// CreateNetworkInterface keeps its address as given, so a zero-padded octet has to be
|
||||
// folded here for the in-use checks to see one address rather than two.
|
||||
// An interface persisted before CreateNetworkInterface canonicalised its address may
|
||||
// still hold a zero-padded octet, so it is folded here for the in-use checks to see
|
||||
// one address rather than two.
|
||||
addresses.add(canonicalIpv4(address).orElse(address));
|
||||
}
|
||||
}
|
||||
@@ -14430,10 +14446,17 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
}
|
||||
}
|
||||
|
||||
// An explicit address is checked the way RunInstances checks its PrivateIpAddress: it must be
|
||||
// one the subnet can assign, in canonical form, and no other resource may hold it. Unlike a
|
||||
// launch it takes no lease on the VPC network (see requireRequestedPrivateIpFree).
|
||||
String requestedIp = (privateIpAddress != null && !privateIpAddress.isBlank())
|
||||
? validateRequestedPrivateIp(privateIpAddress, subnet, null, 1) : null;
|
||||
synchronized (privateIpAllocationLock) {
|
||||
String eniId = "eni-" + randomHex(17);
|
||||
String primaryIp = (privateIpAddress != null && !privateIpAddress.isBlank())
|
||||
? privateIpAddress : assignPrivateIp(region, subnetId);
|
||||
if (requestedIp != null) {
|
||||
requireRequestedPrivateIpFree(region, subnetId, requestedIp);
|
||||
}
|
||||
String primaryIp = requestedIp != null ? requestedIp : assignPrivateIp(region, subnetId);
|
||||
String primaryDns = AwsRegions.ec2PrivateIpDnsName(primaryIp, region);
|
||||
|
||||
NetworkInterface ni = new NetworkInterface();
|
||||
|
||||
Reference in new issue
Block a user