mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(ec2): reject a CreateSubnet whose CIDR overlaps an existing subnet (#3171)
* fix(ec2): reject a CreateSubnet whose CIDR overlaps an existing subnet AWS refuses a subnet whose IPv4 CIDR overlaps another subnet's in the same VPC with InvalidSubnet.Conflict. CreateSubnet has no idempotency token, so this check is also what stops an SDK transport retry after a lost response from recording a second subnet at the same CIDR. Ported from lex00/floci#191. * fix(ec2): hold the VPC lock across the subnet conflict scan and store Two overlapping CreateSubnet requests in flight together could both pass the scan before either subnet was stored, so the retry case the check targets could still record a duplicate. The scan and the put now run under the VPC's stripe lock, the way the other per-resource mutators do.
This commit is contained in:
1 parent
2541f1b68d
commit
614166fe1f
6 files changed
+160
-1
No files matched your search
@@ -3309,7 +3309,12 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
subnet.setOwnerId(accountId);
|
||||
subnet.setRegion(region);
|
||||
subnet.setSubnetArn(AwsArnUtils.Arn.of("ec2", region, accountId, "subnet/" + subnetId).toString());
|
||||
subnets.put(key(region, subnetId), subnet);
|
||||
// The conflict scan and the store must be one step under the VPC's lock, or two
|
||||
// overlapping creates in flight together both pass the scan before either is stored.
|
||||
synchronized (lockFor(key(region, vpcId))) {
|
||||
rejectConflictingSubnetCidr(region, vpcId, cidrBlock);
|
||||
subnets.put(key(region, subnetId), subnet);
|
||||
}
|
||||
|
||||
// Every subnet starts associated with its VPC's default NACL. ReplaceNetworkAclAssociation
|
||||
// later moves it onto a custom NACL, so this association must exist for that lookup to work.
|
||||
@@ -3325,6 +3330,26 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
return subnet;
|
||||
}
|
||||
|
||||
/**
|
||||
* AWS refuses a subnet whose IPv4 CIDR overlaps another subnet's in the same VPC. CreateSubnet
|
||||
* carries no idempotency token, so this check is also what stops an SDK transport retry after
|
||||
* a lost response from producing a second, unrecorded subnet at the same CIDR. Requests with
|
||||
* no IPv4 CIDR are not checked here.
|
||||
*/
|
||||
private void rejectConflictingSubnetCidr(String region, String vpcId, String cidrBlock) {
|
||||
if (!Ipv4Cidrs.isIpv4(cidrBlock)) {
|
||||
return;
|
||||
}
|
||||
boolean conflict = subnets.scan(k -> true).stream()
|
||||
.filter(s -> region.equals(s.getRegion()) && vpcId.equals(s.getVpcId()))
|
||||
.filter(s -> Ipv4Cidrs.isIpv4(s.getCidrBlock()))
|
||||
.anyMatch(s -> Ipv4Cidrs.overlaps(cidrBlock, s.getCidrBlock()));
|
||||
if (conflict) {
|
||||
throw new AwsException("InvalidSubnet.Conflict",
|
||||
"The CIDR '" + cidrBlock + "' conflicts with another subnet", 400);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Subnet> describeSubnets(String region, List<String> subnetIds, Map<String, List<String>> filters) {
|
||||
ensureDefaultResources(region);
|
||||
return subnets.scan(k -> true).stream()
|
||||
|
||||
@@ -26,6 +26,16 @@ final class Ipv4Cidrs {
|
||||
return x[0] <= y[1] && y[0] <= x[1];
|
||||
}
|
||||
|
||||
/** True when {@code cidr} parses as an IPv4 block, so the other checks can be applied to it. */
|
||||
static boolean isIpv4(String cidr) {
|
||||
try {
|
||||
parse(cidr);
|
||||
return true;
|
||||
} catch (AwsException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The first /{@code netmask} block inside any of the {@code provisioned}
|
||||
* CIDRs (in order) that overlaps none of the {@code occupied} CIDRs, or
|
||||
|
||||
@@ -1103,6 +1103,24 @@ class Ec2IntegrationTest {
|
||||
.body("Response.Errors.Error.Message", equalTo("The request must contain the parameter VpcId"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(21)
|
||||
void createSubnetWithConflictingCidrReturnsInvalidSubnetConflict() {
|
||||
given()
|
||||
.formParam("Action", "CreateSubnet")
|
||||
.formParam("VpcId", vpcId)
|
||||
.formParam("CidrBlock", "10.0.1.128/25")
|
||||
.formParam("AvailabilityZone", "us-east-1a")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidSubnet.Conflict"))
|
||||
.body("Response.Errors.Error.Message",
|
||||
equalTo("The CIDR '10.0.1.128/25' conflicts with another subnet"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(21)
|
||||
void describeSubnetById() {
|
||||
|
||||
@@ -200,6 +200,34 @@ class Ec2ServiceConcurrencyTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void concurrentOverlappingCreateSubnetsRecordExactlyOne() throws Exception {
|
||||
for (int trial = 0; trial < TRIALS; trial++) {
|
||||
String region = "race-subnet-" + trial;
|
||||
Ec2Service service = newService();
|
||||
String vpcId = service.createVpc(region, "10.0.0.0/16", false).getVpcId();
|
||||
|
||||
AtomicInteger created = new AtomicInteger();
|
||||
AtomicInteger conflicts = new AtomicInteger();
|
||||
runRaceAllowing(i -> {
|
||||
try {
|
||||
service.createSubnet(region, vpcId, "10.0.1.0/24", null);
|
||||
created.incrementAndGet();
|
||||
} catch (AwsException e) {
|
||||
assertEquals("InvalidSubnet.Conflict", e.getErrorCode());
|
||||
conflicts.incrementAndGet();
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals(1, created.get(), "trial " + trial + ": exactly one create may win the CIDR");
|
||||
assertEquals(N - 1, conflicts.get(), "trial " + trial + ": losers must see InvalidSubnet.Conflict");
|
||||
long stored = service.describeSubnets(region, List.of(), Map.of()).stream()
|
||||
.filter(s -> vpcId.equals(s.getVpcId()))
|
||||
.count();
|
||||
assertEquals(1, stored, "trial " + trial + ": one subnet stored at the CIDR");
|
||||
}
|
||||
}
|
||||
|
||||
/** Runs N concurrent invocations of the same op; the op absorbs its own expected failures. */
|
||||
private static void runRaceAllowing(java.util.function.IntConsumer op) throws Exception {
|
||||
ExecutorService pool = Executors.newFixedThreadPool(THREADS);
|
||||
|
||||
@@ -256,6 +256,75 @@ class Ec2ServiceTest {
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createSubnetRejectsCidrThatConflictsWithAnExistingSubnetInTheSameVpc() {
|
||||
// CreateSubnet has no idempotency token, so an SDK transport retry after a lost response
|
||||
// resends the identical request. AWS's CIDR conflict check is what turns that resend into
|
||||
// an error instead of a second, unrecorded subnet.
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class),
|
||||
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory());
|
||||
Vpc vpc = service.createVpc("us-east-1", "10.0.0.0/16", false);
|
||||
service.createSubnet("us-east-1", vpc.getVpcId(), "10.0.1.0/24", "us-east-1a");
|
||||
|
||||
AwsException error = assertThrows(AwsException.class, () -> service.createSubnet(
|
||||
"us-east-1", vpc.getVpcId(), "10.0.1.0/24", "us-east-1a"));
|
||||
|
||||
assertEquals("InvalidSubnet.Conflict", error.getErrorCode());
|
||||
assertEquals("The CIDR '10.0.1.0/24' conflicts with another subnet", error.getMessage());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
assertEquals(1, service.describeSubnets("us-east-1", List.of(), Map.of()).stream()
|
||||
.filter(s -> vpc.getVpcId().equals(s.getVpcId())).count());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createSubnetRejectsCidrThatPartiallyOverlapsAnExistingSubnet() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class),
|
||||
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory());
|
||||
Vpc vpc = service.createVpc("us-east-1", "10.0.0.0/16", false);
|
||||
service.createSubnet("us-east-1", vpc.getVpcId(), "10.0.1.0/24", "us-east-1a");
|
||||
|
||||
// 10.0.1.128/25 lies inside 10.0.1.0/24, an overlap rather than a duplicate.
|
||||
AwsException error = assertThrows(AwsException.class, () -> service.createSubnet(
|
||||
"us-east-1", vpc.getVpcId(), "10.0.1.128/25", "us-east-1a"));
|
||||
|
||||
assertEquals("InvalidSubnet.Conflict", error.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createSubnetAllowsNonOverlappingCidrsInTheSameVpc() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class),
|
||||
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory());
|
||||
Vpc vpc = service.createVpc("us-east-1", "10.0.0.0/16", false);
|
||||
service.createSubnet("us-east-1", vpc.getVpcId(), "10.0.1.0/24", "us-east-1a");
|
||||
|
||||
Subnet second = service.createSubnet("us-east-1", vpc.getVpcId(), "10.0.2.0/24", "us-east-1b");
|
||||
|
||||
assertEquals("10.0.2.0/24", second.getCidrBlock());
|
||||
assertEquals(2, service.describeSubnets("us-east-1", List.of(), Map.of()).stream()
|
||||
.filter(s -> vpc.getVpcId().equals(s.getVpcId())).count());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createSubnetAllowsConflictingCidrsInDifferentVpcs() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class),
|
||||
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory());
|
||||
Vpc vpcA = service.createVpc("us-east-1", "10.0.0.0/16", false);
|
||||
Vpc vpcB = service.createVpc("us-east-1", "10.0.0.0/16", false);
|
||||
service.createSubnet("us-east-1", vpcA.getVpcId(), "10.0.1.0/24", "us-east-1a");
|
||||
|
||||
Subnet subnetB = service.createSubnet("us-east-1", vpcB.getVpcId(), "10.0.1.0/24", "us-east-1a");
|
||||
|
||||
assertEquals("10.0.1.0/24", subnetB.getCidrBlock());
|
||||
}
|
||||
|
||||
@Test
|
||||
void runInstancesStoresArchitectureFromImageCatalog() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
|
||||
@@ -27,6 +27,15 @@ class Ipv4CidrsTest {
|
||||
assertFalse(Ipv4Cidrs.overlaps("10.0.0.0/24", "10.0.1.0/24"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isIpv4AcceptsParseableBlocksAndRejectsTheRest() {
|
||||
assertTrue(Ipv4Cidrs.isIpv4("10.0.1.0/24"));
|
||||
assertFalse(Ipv4Cidrs.isIpv4(null));
|
||||
assertFalse(Ipv4Cidrs.isIpv4(""));
|
||||
assertFalse(Ipv4Cidrs.isIpv4("10.0.1.0"));
|
||||
assertFalse(Ipv4Cidrs.isIpv4("2600:1f18::/56"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void firstFreeBlockSkipsOccupiedSpaceInOrder() {
|
||||
assertEquals("10.0.1.0/24",
|
||||
|
||||
Reference in new issue
Block a user