mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -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
@@ -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