mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ec2): enforce VPC IPv4 CIDR block association restrictions (#5510)
* test(ec2): keep subnet range test secondary block in 10.0.0.0/8 * test(ec2): add same-range secondary blocks in subnet containment tests * fix(ec2): enforce VPC IPv4 CIDR block association restrictions * refactor(ec2): add Cidr4.rfc1918Range for the containing private range * fix(ec2): classify VPC CIDR ranges by containment, not overlap * test(ec2): drop the /0 primary case now that CreateVpc refuses it
This commit is contained in:
1 parent
4da5143456
commit
ac5f0bd95f
6 files changed
+183
-13
No files matched your search
@@ -65,6 +65,7 @@ import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -904,14 +905,83 @@ class Ec2ServiceTest {
|
||||
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
|
||||
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory());
|
||||
String vpcId = service.createVpc("us-east-1", "10.0.0.0/16", false).getVpcId();
|
||||
String associationId = service.associateVpcCidrBlock("us-east-1", vpcId, "192.168.0.0/16").getAssociationId();
|
||||
String associationId = service.associateVpcCidrBlock("us-east-1", vpcId, "10.1.0.0/16").getAssociationId();
|
||||
assertEquals("10.0.255.0/24",
|
||||
service.createSubnet("us-east-1", vpcId, "10.0.255.9/24", "us-east-1a").getCidrBlock());
|
||||
assertEquals("192.168.1.0/24",
|
||||
service.createSubnet("us-east-1", vpcId, "192.168.1.9/24", "us-east-1a").getCidrBlock());
|
||||
assertEquals("10.1.1.0/24",
|
||||
service.createSubnet("us-east-1", vpcId, "10.1.1.9/24", "us-east-1a").getCidrBlock());
|
||||
service.disassociateVpcCidrBlock("us-east-1", associationId);
|
||||
assertEquals("InvalidSubnet.Range", assertThrows(AwsException.class,
|
||||
() -> service.createSubnet("us-east-1", vpcId, "192.168.2.0/24", "us-east-1a")).getErrorCode());
|
||||
() -> service.createSubnet("us-east-1", vpcId, "10.1.2.0/24", "us-east-1a")).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void associateVpcCidrBlockRejectsTheRestrictedRangesForThePrimaryBlock() {
|
||||
// VPC User Guide, "IPv4 CIDR block association restrictions": the primary block's range
|
||||
// decides which secondary blocks are refused.
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
|
||||
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory());
|
||||
Map<String, List<String>> restricted = new LinkedHashMap<>();
|
||||
restricted.put("10.0.0.0/16", List.of("172.16.0.0/16", "192.168.0.0/16", "198.19.0.0/24"));
|
||||
restricted.put("10.1.0.0/16", List.of("10.0.0.0/16", "10.0.4.0/24"));
|
||||
restricted.put("172.16.0.0/16", List.of("10.1.0.0/16", "192.168.0.0/16", "172.31.0.0/16", "198.19.0.0/24"));
|
||||
restricted.put("192.168.0.0/20", List.of("10.1.0.0/16", "172.16.0.0/16", "198.19.0.0/24"));
|
||||
restricted.put("198.19.0.0/16", List.of("10.1.0.0/16", "172.16.0.0/16", "192.168.0.0/16"));
|
||||
restricted.put("100.64.0.0/16", List.of("10.1.0.0/16", "172.16.0.0/16", "192.168.0.0/16", "198.19.0.0/24"));
|
||||
restricted.put("52.0.0.0/16", List.of("10.1.0.0/16", "172.16.0.0/16", "192.168.0.0/16", "198.19.0.0/24"));
|
||||
|
||||
restricted.forEach((primary, blocks) -> {
|
||||
String vpcId = service.createVpc("us-east-1", primary, false).getVpcId();
|
||||
for (String cidr : blocks) {
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.associateVpcCidrBlock("us-east-1", vpcId, cidr), primary + " + " + cidr);
|
||||
assertEquals("InvalidVpc.Range", error.getErrorCode());
|
||||
assertEquals("The CIDR '" + cidr + "' is restricted. Use a CIDR from the same private address "
|
||||
+ "range as the current VPC CIDR, or use a publicly-routable CIDR. For additional "
|
||||
+ "restrictions, see https://docs.aws.amazon.com/vpc/latest/userguide/vpc-cidr-blocks.html",
|
||||
error.getMessage());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
}
|
||||
assertEquals(1, service.describeVpcs("us-east-1", List.of(vpcId), Map.of()).getFirst()
|
||||
.getCidrBlockAssociationSet().size());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void associateVpcCidrBlockAcceptsThePermittedRangesForThePrimaryBlock() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
|
||||
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory());
|
||||
Map<String, List<String>> permitted = new LinkedHashMap<>();
|
||||
permitted.put("10.0.0.0/16", List.of("10.2.0.0/16", "100.64.0.0/16", "52.0.0.0/16"));
|
||||
permitted.put("10.2.0.0/16", List.of("10.0.0.0/16"));
|
||||
permitted.put("172.16.0.0/16", List.of("172.20.0.0/16", "100.64.0.0/16", "52.0.0.0/16"));
|
||||
permitted.put("192.168.0.0/20", List.of("192.168.16.0/20", "100.64.0.0/16", "52.0.0.0/16"));
|
||||
permitted.put("198.19.0.0/16", List.of("100.64.0.0/16", "52.0.0.0/16"));
|
||||
permitted.put("100.64.0.0/16", List.of("100.65.0.0/16", "52.0.0.0/16"));
|
||||
permitted.put("52.0.0.0/16", List.of("52.1.0.0/16", "100.64.0.0/16"));
|
||||
|
||||
permitted.forEach((primary, blocks) -> {
|
||||
String vpcId = service.createVpc("us-east-1", primary, false).getVpcId();
|
||||
for (String cidr : blocks) {
|
||||
assertEquals(cidr, service.associateVpcCidrBlock("us-east-1", vpcId, cidr).getCidrBlock(),
|
||||
primary + " + " + cidr);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void associateVpcCidrBlockRefuses172Dot31ForA172Dot16Vpc() {
|
||||
// A 172.16.0.0/12 VPC cannot add from 172.31.0.0/16, its own primary's /16 included.
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
|
||||
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory());
|
||||
String vpcId = service.createVpc("us-east-1", "172.31.104.0/21", false).getVpcId();
|
||||
assertEquals("InvalidVpc.Range", assertThrows(AwsException.class,
|
||||
() -> service.associateVpcCidrBlock("us-east-1", vpcId, "172.31.120.0/21")).getErrorCode());
|
||||
assertEquals("172.30.120.0/21",
|
||||
service.associateVpcCidrBlock("us-east-1", vpcId, "172.30.120.0/21").getCidrBlock());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -1002,7 +1072,7 @@ class Ec2ServiceTest {
|
||||
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);
|
||||
String associationId = service.associateVpcCidrBlock("us-east-1", vpc.getVpcId(), "192.168.0.0/16")
|
||||
String associationId = service.associateVpcCidrBlock("us-east-1", vpc.getVpcId(), "10.1.0.0/16")
|
||||
.getAssociationId();
|
||||
CountDownLatch scanning = new CountDownLatch(1);
|
||||
CountDownLatch resume = new CountDownLatch(1);
|
||||
@@ -1017,7 +1087,7 @@ class Ec2ServiceTest {
|
||||
ExecutorService workers = Executors.newFixedThreadPool(2);
|
||||
try {
|
||||
Future<Subnet> creating = workers.submit(() ->
|
||||
service.createSubnet("us-east-1", vpc.getVpcId(), "192.168.1.0/24", "us-east-1a"));
|
||||
service.createSubnet("us-east-1", vpc.getVpcId(), "10.1.1.0/24", "us-east-1a"));
|
||||
assertTrue(scanning.await(5, TimeUnit.SECONDS));
|
||||
Future<?> editing = workers.submit(() -> {
|
||||
editor.set(Thread.currentThread());
|
||||
@@ -1031,7 +1101,7 @@ class Ec2ServiceTest {
|
||||
|| (editor.get() != null && editor.get().getState() == Thread.State.BLOCKED));
|
||||
assertFalse(editing.isDone(), "CIDR edits must not mutate the list while subnet validation scans it");
|
||||
resume.countDown();
|
||||
assertEquals("192.168.1.0/24", creating.get(5, TimeUnit.SECONDS).getCidrBlock());
|
||||
assertEquals("10.1.1.0/24", creating.get(5, TimeUnit.SECONDS).getCidrBlock());
|
||||
editing.get(5, TimeUnit.SECONDS);
|
||||
} finally {
|
||||
resume.countDown();
|
||||
|
||||
+4
-4
@@ -18,12 +18,12 @@ class Ec2SubnetVpcRangeIntegrationTest {
|
||||
void subnetRangeMustFitPrimaryOrAssociatedSecondaryVpcBlock() {
|
||||
String vpcId = ec2("CreateVpc").formParam("CidrBlock", "10.0.0.0/16").post("/").then()
|
||||
.statusCode(200).extract().path("CreateVpcResponse.vpc.vpcId");
|
||||
ec2("CreateSubnet").formParam("VpcId", vpcId).formParam("CidrBlock", "192.168.1.0/24")
|
||||
ec2("CreateSubnet").formParam("VpcId", vpcId).formParam("CidrBlock", "10.1.1.0/24")
|
||||
.post("/").then().statusCode(400).body("Response.Errors.Error.Code", equalTo("InvalidSubnet.Range"));
|
||||
ec2("AssociateVpcCidrBlock").formParam("VpcId", vpcId).formParam("CidrBlock", "192.168.0.0/16")
|
||||
ec2("AssociateVpcCidrBlock").formParam("VpcId", vpcId).formParam("CidrBlock", "10.1.0.0/16")
|
||||
.post("/").then().statusCode(200);
|
||||
String subnetId = ec2("CreateSubnet").formParam("VpcId", vpcId).formParam("CidrBlock", "192.168.1.9/24")
|
||||
.post("/").then().statusCode(200).body("CreateSubnetResponse.subnet.cidrBlock", equalTo("192.168.1.0/24"))
|
||||
String subnetId = ec2("CreateSubnet").formParam("VpcId", vpcId).formParam("CidrBlock", "10.1.1.9/24")
|
||||
.post("/").then().statusCode(200).body("CreateSubnetResponse.subnet.cidrBlock", equalTo("10.1.1.0/24"))
|
||||
.extract().path("CreateSubnetResponse.subnet.subnetId");
|
||||
ec2("DeleteSubnet").formParam("SubnetId", subnetId).post("/").then().statusCode(200);
|
||||
ec2("DeleteVpc").formParam("VpcId", vpcId).post("/").then().statusCode(200);
|
||||
|
||||
+26
@@ -5,6 +5,7 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
|
||||
/**
|
||||
* AssociateVpcCidrBlock returned cidrBlockState as a bare string
|
||||
@@ -61,4 +62,29 @@ class Ec2VpcCidrBlockAssociationIntegrationTest {
|
||||
+ "it.associationId == '" + associationId + "' }.cidrBlockState.state",
|
||||
equalTo("associated"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void associateVpcCidrBlockRefusesABlockFromAnotherRfc1918Range() {
|
||||
String vpcId = given()
|
||||
.formParam("Action", "CreateVpc")
|
||||
.formParam("CidrBlock", "10.93.0.0/24")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("CreateVpcResponse.vpc.vpcId");
|
||||
|
||||
given()
|
||||
.formParam("Action", "AssociateVpcCidrBlock")
|
||||
.formParam("VpcId", vpcId)
|
||||
.formParam("CidrBlock", "192.168.0.0/16")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidVpc.Range"))
|
||||
.body("Response.Errors.Error.Message", startsWith("The CIDR '192.168.0.0/16' is restricted."));
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,21 @@ class Cidr4Test {
|
||||
assertFalse(Cidr4.parse("0.0.0.0/0").orElseThrow().isRfc1918());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rfc1918RangeIsTheRangeThatWhollyContainsTheBlock() {
|
||||
assertEquals(Optional.of(Cidr4.parse("10.0.0.0/8").orElseThrow()),
|
||||
Cidr4.parse("10.2.0.0/16").orElseThrow().rfc1918Range());
|
||||
assertEquals(Optional.of(Cidr4.parse("172.16.0.0/12").orElseThrow()),
|
||||
Cidr4.parse("172.31.104.0/21").orElseThrow().rfc1918Range());
|
||||
assertEquals(Optional.of(Cidr4.parse("192.168.0.0/16").orElseThrow()),
|
||||
Cidr4.parse("192.168.0.0/16").orElseThrow().rfc1918Range());
|
||||
|
||||
assertEquals(Optional.empty(), Cidr4.parse("0.0.0.0/0").orElseThrow().rfc1918Range(),
|
||||
"overlapping a range is not being in it");
|
||||
assertEquals(Optional.empty(), Cidr4.parse("8.0.0.0/6").orElseThrow().rfc1918Range());
|
||||
assertEquals(Optional.empty(), Cidr4.parse("52.0.0.0/16").orElseThrow().rfc1918Range());
|
||||
}
|
||||
|
||||
@Test
|
||||
void containmentAndOverlapAreExactAtTheBoundaries() {
|
||||
Cidr4 vpc = Cidr4.parse("10.0.0.0/16").orElseThrow();
|
||||
|
||||
Reference in new issue
Block a user