Light, fluffy, and always free - The AWS Local Emulator alternative
fix(ec2): enforce VPC IPv4 CIDR block association restrictions (#5510)
Bharat Kauravac5f0bd95f92
diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.javaindex 14534dc5f5..e281836b1a 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java@@ -4872,6 +4872,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettabsynchronized (attachmentTopologyLock(region)) {synchronized (lockFor(key(region, vpcId))) {Vpc vpc = getRequiredVpc(region, vpcId);+ rejectRestrictedVpcCidrAssociation(vpc, cidrBlock);VpcCidrBlockAssociation assoc = new VpcCidrBlockAssociation("vpc-cidr-assoc-" + randomHex(8), cidrBlock);vpc.getCidrBlockAssociationSet().add(assoc);@@ -4881,6 +4882,56 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab}}+ /**+ * The VPC User Guide's "IPv4 CIDR block association restrictions" table. What a VPC may add+ * depends on the range its primary block came from: a VPC created in one RFC 1918 range+ * cannot add a block from another RFC 1918 range or from 198.19.0.0/16, and a VPC created with+ * a block from 198.19.0.0/16, from 100.64.0.0/10 or from publicly routable space cannot add an+ * RFC 1918 block, nor (outside 198.19.0.0/16 itself) one from 198.19.0.0/16. Two narrower rows+ * apply within a range: a 172.16.0.0/12 VPC cannot add from 172.31.0.0/16, and a 10.0.0.0/8+ * VPC with any block in 10.0.0.0/15 cannot add from 10.0.0.0/16. AWS answers each with+ * InvalidVpc.Range. Requests whose block does not parse are left to the checks that already+ * handle them.+ */+ private void rejectRestrictedVpcCidrAssociation(Vpc vpc, String cidrBlock) {+ Optional<Cidr4> primary = Cidr4.parse(vpc.getCidrBlock());+ Optional<Cidr4> requested = Cidr4.parse(cidrBlock);+ if (primary.isEmpty() || requested.isEmpty()) {+ return;+ }+ Optional<Cidr4> primaryRange = primary.get().rfc1918Range();+ Optional<Cidr4> requestedRange = requested.get().rfc1918Range();+ boolean benchmarking = Ipv4Cidrs.overlaps(cidrBlock, "198.19.0.0/16");+ boolean restricted;+ if (primaryRange.isPresent()) {+ String range = primaryRange.get().toString();+ restricted = (requestedRange.isPresent() && !requestedRange.equals(primaryRange))+ || benchmarking+ || ("172.16.0.0/12".equals(range) && Ipv4Cidrs.overlaps(cidrBlock, "172.31.0.0/16"))+ || ("10.0.0.0/8".equals(range) && Ipv4Cidrs.overlaps(cidrBlock, "10.0.0.0/16")+ && vpcHasIpv4BlockIn(vpc, "10.0.0.0/15"));+ } else if (Ipv4Cidrs.contains("198.19.0.0/16", vpc.getCidrBlock())) {+ restricted = requestedRange.isPresent();+ } else {+ restricted = requestedRange.isPresent() || benchmarking;+ }+ if (restricted) {+ throw new AwsException("InvalidVpc.Range", "The CIDR '" + cidrBlock + "' 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", 400);+ }+ }++ private static boolean vpcHasIpv4BlockIn(Vpc vpc, String range) {+ return Ipv4Cidrs.overlaps(vpc.getCidrBlock(), range)+ || vpc.getCidrBlockAssociationSet().stream()+ .filter(association -> "associated".equalsIgnoreCase(association.getCidrBlockState()))+ .map(VpcCidrBlockAssociation::getCidrBlock)+ .filter(Ipv4Cidrs::isIpv4)+ .anyMatch(block -> Ipv4Cidrs.overlaps(block, range));+ }+/*** Associates an Amazon-provided IPv6 block with an existing VPC: the AssociateVpcCidrBlock* half of {@code AmazonProvidedIpv6CidrBlock}, which the Terraform provider issues whendiff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/net/Cidr4.java b/src/main/java/io/github/hectorvent/floci/services/ec2/net/Cidr4.javaindex 61c2d9fad5..c2bfd3ec72 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/net/Cidr4.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/net/Cidr4.java@@ -104,12 +104,20 @@ public final class Cidr4 {}public boolean isRfc1918() {+ return rfc1918Range().isPresent();+ }++ /**+ * @return the RFC 1918 range that wholly contains this block, or empty when it sits outside+ * all three. A block that only straddles one, such as 0.0.0.0/0, is not in it.+ */+ public Optional<Cidr4> rfc1918Range() {for (Cidr4 range : RFC_1918) {if (range.contains(this)) {- return true;+ return Optional.of(range);}}- return false;+ return Optional.empty();}/**diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.javaindex 0e8eb8e9db..cdfd6c8671 100644--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java@@ -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();diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2SubnetVpcRangeIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2SubnetVpcRangeIntegrationTest.javaindex 0dfaa28578..b3ad98c952 100644--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2SubnetVpcRangeIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2SubnetVpcRangeIntegrationTest.java@@ -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);diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VpcCidrBlockAssociationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VpcCidrBlockAssociationIntegrationTest.javaindex 0641996013..23b2c34cee 100644--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VpcCidrBlockAssociationIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VpcCidrBlockAssociationIntegrationTest.java@@ -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."));+ }}diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/net/Cidr4Test.java b/src/test/java/io/github/hectorvent/floci/services/ec2/net/Cidr4Test.javaindex b103765998..a414e1b1e4 100644--- a/src/test/java/io/github/hectorvent/floci/services/ec2/net/Cidr4Test.java+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/net/Cidr4Test.java@@ -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());+ }+@Testvoid containmentAndOverlapAreExactAtTheBoundaries() {Cidr4 vpc = Cidr4.parse("10.0.0.0/16").orElseThrow();