mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
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:
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);
|
||||
|
||||
|
||||
+177
@@ -1,12 +1,19 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.path.xml.XmlPath;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
@@ -634,4 +641,174 @@ class Ec2NetworkInterfaceIntegrationTest {
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
// A PrivateIpAddresses list is what the Terraform AWS provider sends for private_ips: the primary
|
||||
// is the entry marked Primary, never an address the subnet allocator picked on its own.
|
||||
private static String addressInSubnet(int lastOctet) {
|
||||
String held = given()
|
||||
.formParam("Action", "DescribeSubnets")
|
||||
.formParam("SubnetId.1", subnetId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("DescribeSubnetsResponse.subnetSet.item.cidrBlock");
|
||||
String[] o = held.split("/")[0].split("\\.");
|
||||
return o[0] + "." + o[1] + "." + o[2] + "." + lastOctet;
|
||||
}
|
||||
|
||||
private static ValidatableResponse createWithList(String... params) {
|
||||
RequestSpecification r = given()
|
||||
.formParam("Action", "CreateNetworkInterface")
|
||||
.formParam("SubnetId", subnetId)
|
||||
.header("Authorization", AUTH_HEADER);
|
||||
for (int i = 0; i < params.length; i += 2) {
|
||||
r = r.formParam(params[i], params[i + 1]);
|
||||
}
|
||||
return r.when().post("/").then();
|
||||
}
|
||||
|
||||
/** Reads the interface back through DescribeNetworkInterfaces, not the create response. */
|
||||
private static Map<String, Boolean> describedAddresses(String id) {
|
||||
XmlPath xml = given()
|
||||
.formParam("Action", "DescribeNetworkInterfaces")
|
||||
.formParam("NetworkInterfaceId.1", id)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().xmlPath();
|
||||
String base = "DescribeNetworkInterfacesResponse.networkInterfaceSet.item.privateIpAddressesSet.item.";
|
||||
List<String> ips = xml.getList(base + "privateIpAddress");
|
||||
List<String> primaries = xml.getList(base + "primary");
|
||||
Map<String, Boolean> out = new LinkedHashMap<>();
|
||||
for (int i = 0; i < ips.size(); i++) {
|
||||
out.put(ips.get(i), Boolean.parseBoolean(primaries.get(i)));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private static void deleteEni(String id) {
|
||||
given().formParam("Action", "DeleteNetworkInterface").formParam("NetworkInterfaceId", id)
|
||||
.header("Authorization", AUTH_HEADER).when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(20)
|
||||
void aSinglePrivateIpAddressesEntryMarkedPrimaryIsThePrimary() {
|
||||
String ip = addressInSubnet(201);
|
||||
String id = createWithList(
|
||||
"PrivateIpAddresses.1.PrivateIpAddress", ip,
|
||||
"PrivateIpAddresses.1.Primary", "true")
|
||||
.statusCode(200)
|
||||
.body("CreateNetworkInterfaceResponse.networkInterface.privateIpAddress", equalTo(ip))
|
||||
.extract().path("CreateNetworkInterfaceResponse.networkInterface.networkInterfaceId");
|
||||
|
||||
Assertions.assertEquals(Map.of(ip, true), describedAddresses(id));
|
||||
deleteEni(id);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(21)
|
||||
void theEntryMarkedPrimaryWinsWhateverItsPosition() {
|
||||
String first = addressInSubnet(202);
|
||||
String second = addressInSubnet(203);
|
||||
String id = createWithList(
|
||||
"PrivateIpAddresses.1.PrivateIpAddress", first,
|
||||
"PrivateIpAddresses.1.Primary", "false",
|
||||
"PrivateIpAddresses.2.PrivateIpAddress", second,
|
||||
"PrivateIpAddresses.2.Primary", "true")
|
||||
.statusCode(200)
|
||||
.extract().path("CreateNetworkInterfaceResponse.networkInterface.networkInterfaceId");
|
||||
|
||||
Map<String, Boolean> got = describedAddresses(id);
|
||||
Assertions.assertEquals(2, got.size());
|
||||
Assertions.assertEquals(Boolean.TRUE, got.get(second));
|
||||
Assertions.assertEquals(Boolean.FALSE, got.get(first));
|
||||
deleteEni(id);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(22)
|
||||
void aSecondaryAnotherInterfaceHoldsIsRefused() {
|
||||
String ip = addressInSubnet(204);
|
||||
String holder = createWithList("PrivateIpAddress", ip).statusCode(200)
|
||||
.extract().path("CreateNetworkInterfaceResponse.networkInterface.networkInterfaceId");
|
||||
|
||||
createWithList(
|
||||
"PrivateIpAddresses.1.PrivateIpAddress", addressInSubnet(205),
|
||||
"PrivateIpAddresses.1.Primary", "true",
|
||||
"PrivateIpAddresses.2.PrivateIpAddress", ip)
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidIPAddress.InUse"));
|
||||
deleteEni(holder);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(23)
|
||||
void aPrimaryEntryAlongsidePrivateIpAddressIsRefused() {
|
||||
createWithList(
|
||||
"PrivateIpAddress", addressInSubnet(206),
|
||||
"PrivateIpAddresses.1.PrivateIpAddress", addressInSubnet(207),
|
||||
"PrivateIpAddresses.1.Primary", "true")
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidParameterCombination"));
|
||||
}
|
||||
|
||||
private static int enisInSubnet() {
|
||||
return given()
|
||||
.formParam("Action", "DescribeNetworkInterfaces")
|
||||
.formParam("Filter.1.Name", "subnet-id")
|
||||
.formParam("Filter.1.Value.1", subnetId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().xmlPath()
|
||||
.getList("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.networkInterfaceId").size();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(24)
|
||||
void twoEntriesMarkedPrimaryAreRefusedAndCreateNothing() {
|
||||
int before = enisInSubnet();
|
||||
createWithList(
|
||||
"PrivateIpAddresses.1.PrivateIpAddress", addressInSubnet(208),
|
||||
"PrivateIpAddresses.1.Primary", "true",
|
||||
"PrivateIpAddresses.2.PrivateIpAddress", addressInSubnet(209),
|
||||
"PrivateIpAddresses.2.Primary", "true")
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidParameterValue"));
|
||||
Assertions.assertEquals(before, enisInSubnet());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(25)
|
||||
void aSecondaryEqualToThePrivateIpAddressCollapsesIntoThePrimary() {
|
||||
String ip = addressInSubnet(210);
|
||||
String id = createWithList(
|
||||
"PrivateIpAddress", ip,
|
||||
"PrivateIpAddresses.1.PrivateIpAddress", ip)
|
||||
.statusCode(200)
|
||||
.extract().path("CreateNetworkInterfaceResponse.networkInterface.networkInterfaceId");
|
||||
Assertions.assertEquals(Map.of(ip, true), describedAddresses(id));
|
||||
deleteEni(id);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(26)
|
||||
void aNonPrimaryEntryEqualToThePrimaryEntryCollapsesIntoIt() {
|
||||
String ip = addressInSubnet(211);
|
||||
String id = createWithList(
|
||||
"PrivateIpAddresses.1.PrivateIpAddress", ip,
|
||||
"PrivateIpAddresses.1.Primary", "true",
|
||||
"PrivateIpAddresses.2.PrivateIpAddress", ip)
|
||||
.statusCode(200)
|
||||
.extract().path("CreateNetworkInterfaceResponse.networkInterface.networkInterfaceId");
|
||||
Assertions.assertEquals(Map.of(ip, true), describedAddresses(id));
|
||||
deleteEni(id);
|
||||
}
|
||||
}
|
||||
@@ -4593,6 +4593,60 @@ class Ec2ServiceTest {
|
||||
verify(vpcNetworks, never()).releasePrivateIp("us-east-1", subnetId, "10.50.1.11");
|
||||
}
|
||||
|
||||
@Test
|
||||
void anInterfaceRefusedForAHeldSecondaryLeasesNoPrimaryOnTheVpcNetwork() {
|
||||
VpcNetworkManager vpcNetworks = mock(VpcNetworkManager.class);
|
||||
Ec2Service service = mockedVpcNetworkService(vpcNetworks);
|
||||
String subnetId = privateIpSubnet(service);
|
||||
service.createNetworkInterface("us-east-1", subnetId, "holder", "10.50.1.70",
|
||||
List.of(), List.of(), List.of());
|
||||
when(vpcNetworks.allocatePrivateIp("us-east-1", subnetId)).thenReturn(Optional.of("10.50.1.10"));
|
||||
|
||||
assertEquals("InvalidIPAddress.InUse", assertThrows(AwsException.class, () -> service.createNetworkInterface(
|
||||
"us-east-1", subnetId, "clash", null, List.of("10.50.1.70"), List.of(), List.of())).getErrorCode());
|
||||
verify(vpcNetworks, never()).allocatePrivateIp(anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aZeroPaddedSecondaryIsFoldedBeforeItIsChecked() {
|
||||
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);
|
||||
service.createNetworkInterface("us-east-1", subnetId, "holder", "10.50.1.70",
|
||||
List.of(), List.of(), List.of());
|
||||
|
||||
assertEquals("InvalidIPAddress.InUse", assertThrows(AwsException.class, () -> service.createNetworkInterface(
|
||||
"us-east-1", subnetId, "padded", "10.50.1.80", List.of("10.50.1.070"), List.of(), List.of()))
|
||||
.getErrorCode());
|
||||
|
||||
String eniId = service.createNetworkInterface("us-east-1", subnetId, "folded", "10.50.1.80",
|
||||
List.of("10.50.1.080", "10.50.1.081", "10.50.1.81"), List.of(), List.of()).getNetworkInterfaceId();
|
||||
NetworkInterface stored = service.describeNetworkInterfaces("us-east-1", List.of(eniId), Map.of())
|
||||
.networkInterfaces().getFirst();
|
||||
assertEquals(List.of("10.50.1.80", "10.50.1.81"), stored.getPrivateIpAddresses().stream()
|
||||
.map(NetworkInterfacePrivateIpAddress::getPrivateIpAddress).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAutoAssignedPrimaryEqualToARequestedSecondaryIsListedOnceAsPrimary() {
|
||||
// The secondary is free and not stored yet, so the network may hand the same address out
|
||||
// as the primary; the interface must not then list it a second time as a secondary.
|
||||
VpcNetworkManager vpcNetworks = mock(VpcNetworkManager.class);
|
||||
Ec2Service service = mockedVpcNetworkService(vpcNetworks);
|
||||
String subnetId = privateIpSubnet(service);
|
||||
when(vpcNetworks.allocatePrivateIp("us-east-1", subnetId)).thenReturn(Optional.of("10.50.1.10"));
|
||||
|
||||
String eniId = service.createNetworkInterface("us-east-1", subnetId, "overlap", null,
|
||||
List.of("10.50.1.10"), List.of(), List.of()).getNetworkInterfaceId();
|
||||
|
||||
NetworkInterface stored = service.describeNetworkInterfaces("us-east-1", List.of(eniId), Map.of())
|
||||
.networkInterfaces().getFirst();
|
||||
assertEquals("10.50.1.10", stored.getPrivateIpAddress());
|
||||
assertEquals(List.of(Map.entry("10.50.1.10", true)), stored.getPrivateIpAddresses().stream()
|
||||
.map(a -> Map.entry(a.getPrivateIpAddress(), a.isPrimary())).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAddressAnotherAccountHoldsIsNotHandedOutWhenTheWalkRunsTheNetworkDry() {
|
||||
// The walk skips the only address the shared network still offers, which another account's
|
||||
|
||||
Reference in new issue
Block a user