Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(ec2): canonicalize VPC and subnet CIDR blocks on create (#5214)

Allen Sanborn4eae79d546d6
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/CidrCanonicalizer.java b/src/main/java/io/github/hectorvent/floci/core/common/CidrCanonicalizer.java
index c86fd85246..b91e06dfb1 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/CidrCanonicalizer.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/CidrCanonicalizer.java
@@ -4,6 +4,7 @@ import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.util.Arrays;
+import java.util.Objects;
import java.util.Optional;
/**
@@ -50,6 +51,15 @@ public final class CidrCanonicalizer {
});
}
+ /**
+ * Reports whether two CIDR blocks name the same network once both are canonical, so
+ * {@code 10.0.1.9/24} and {@code 10.0.1.0/24} are the same block. A side that cannot be
+ * parsed is compared as its raw string, and two nulls are the same block.
+ */
+ public static boolean sameBlock(String a, String b) {
+ return Objects.equals(canonicalize(a).orElse(a), canonicalize(b).orElse(b));
+ }
+
/**
* Reports whether {@code cidr} is both parseable and already in canonical form,
* i.e. {@code canonicalize(cidr)} would return the identical string.
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkCfnProvisioner.java
index c17d53f4ad..9b2f9dec61 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkCfnProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkCfnProvisioner.java
@@ -403,7 +403,8 @@ public class Ec2NetworkCfnProvisioner implements CfnResourceProvisioner {
// only MapPublicIpOnLaunch is applied; changed, a new subnet replaces it.
Subnet existing = ctx.isUpdate() ? findSubnet(ctx.priorPhysicalId(), ctx.region()) : null;
boolean reuse = existing != null && Objects.equals(vpcId, existing.getVpcId())
- && Objects.equals(cidr, existing.getCidrBlock())
+ // Both sides canonical: a subnet saved before canonicalization may still hold host bits.
+ && CidrCanonicalizer.sameBlock(cidr, existing.getCidrBlock())
&& (az == null || az.equals(existing.getAvailabilityZone()));
Subnet subnet = reuse ? existing : ec2Service.createSubnet(ctx.region(), vpcId, cidr, az);
r.setPhysicalId(subnet.getSubnetId());
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2VpcCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2VpcCfnProvisioner.java
index 3407c9a633..26b52c0d47 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2VpcCfnProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2VpcCfnProvisioner.java
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.cloudformation.provisioners;
import com.fasterxml.jackson.databind.JsonNode;
+import io.github.hectorvent.floci.core.common.CidrCanonicalizer;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.ec2.model.Vpc;
@@ -70,8 +71,11 @@ public class Ec2VpcCfnProvisioner implements CfnResourceProvisioner {
if (existing == null) {
return null;
}
- // A changed CidrBlock is a replacement on AWS, so let the caller create a new VPC.
- if (cidr != null && !cidr.isBlank() && !cidr.equals(existing.getCidrBlock())) {
+ // A changed CidrBlock is a replacement on AWS, so let the caller create a new VPC. Compare both
+ // sides canonically (10.0.0.5/16 is 10.0.0.0/16): a VPC saved before canonicalization may
+ // still hold host bits.
+ if (cidr != null && !cidr.isBlank()
+ && !CidrCanonicalizer.sameBlock(cidr, existing.getCidrBlock())) {
return null;
}
return existing;
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.java
index 2b0bbe5acb..0e09be9ffc 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
@@ -4337,8 +4337,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
return createVpc(region, cidrBlock, isDefault, false);
}
- public Vpc createVpc(String region, String cidrBlock, boolean isDefault,
+ public Vpc createVpc(String region, String requestedCidrBlock, boolean isDefault,
boolean amazonProvidedIpv6CidrBlock) {
+ // AWS stores the CIDR in canonical form: "100.68.0.18/18" becomes "100.68.0.0/18".
+ String cidrBlock = canonicalizeIpv4Cidr(requestedCidrBlock);
ensureDefaultResources(region);
String vpcId = "vpc-" + randomHex(8);
Vpc vpc = new Vpc();
@@ -5136,8 +5138,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
return createSubnet(region, vpcId, cidrBlock, availabilityZone, availabilityZoneId, null);
}
- public Subnet createSubnet(String region, String vpcId, String cidrBlock, String availabilityZone,
+ public Subnet createSubnet(String region, String vpcId, String requestedCidrBlock, String availabilityZone,
String availabilityZoneId, String ipv6CidrBlock) {
+ // Canonical form, as AWS stores it; null (an IPv6-only subnet) stays null.
+ String cidrBlock = canonicalizeIpv4Cidr(requestedCidrBlock);
if (vpcId == null || vpcId.isBlank()) {
throw new AwsException("MissingParameter", "The request must contain the parameter VpcId", 400);
}
@@ -7885,33 +7889,36 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
}
/**
- * AWS canonicalizes an IPv4 destination CIDR on input: "We modify the specified CIDR block to
- * its canonical form; for example, if you specify 100.68.0.18/18, we modify it to
- * 100.68.0.0/18." Running every DestinationCidrBlock through this once, here, is what lets
- * CreateRoute, ReplaceRoute and DeleteRoute agree on "same destination" — two spellings of the
- * same network now collide as duplicates instead of coexisting as two routes with undefined
- * ReplaceRoute/DeleteRoute behaviour — and it is also why DescribeRouteTables echoes back the
- * canonical form: the stored Route never holds anything else.
+ * AWS stores an IPv4 CIDR block in canonical form, with every host bit zeroed. The CreateRoute
+ * reference states it for DestinationCidrBlock: "We modify the specified CIDR block to its
+ * canonical form; for example, if you specify 100.68.0.18/18, we modify it to 100.68.0.0/18."
+ * CreateVpc and CreateSubnet store their CidrBlock the same way, so all three callers run the
+ * requested block through this once, before anything is stored or compared.
+ *
+ * <p>For routes, that is what lets CreateRoute, ReplaceRoute and DeleteRoute agree on "same
+ * destination": two spellings of the same network collide as duplicates instead of coexisting
+ * as two routes with undefined ReplaceRoute/DeleteRoute behaviour. For subnets, it is what
+ * makes the CreateSubnet overlap check see the real network. In every case it is also why the
+ * Describe calls echo back the canonical form: the stored resource never holds anything else.
*
- * <p>DestinationIpv6CidrBlock has no equivalent sentence in the CreateRoute/ReplaceRoute model
- * and is left untouched. DestinationPrefixListId is an opaque ID, not a CIDR, and is likewise
- * untouched.
+ * <p>Only IPv4 blocks are rewritten. DestinationIpv6CidrBlock and the IPv6 blocks of VPCs and
+ * subnets are left untouched, as is DestinationPrefixListId, which is an opaque ID, not a CIDR.
+ * A null block (an IPv6-only subnet) is returned as is.
*
- * <p>A block that is not a well-formed "IPv4/prefix" is returned unchanged: canonicalizing is
- * not this method's job to validate the request, only to reduce what is already well-formed.
- * The unmodified value fails downstream exactly as it did before this change.
+ * <p>A block that is not a well-formed "IPv4/prefix" is returned unchanged: this method does
+ * not validate the request, it only reduces what is already well-formed. The unmodified value
+ * fails downstream exactly as it did before canonicalization was added.
*
* <p>Delegates the actual bit-twiddling to {@link CidrCanonicalizer}, which also understands
- * IPv6. That is deliberately not used here: DestinationCidrBlock is AWS's IPv4-only field —
- * the API reference's canonicalization sentence appears only under it, never under
- * DestinationIpv6CidrBlock — so a value that parses as an IPv6 literal is left untouched
- * rather than canonicalized, the same as any other malformed-for-this-field input.
+ * IPv6. That is deliberately not used here: every caller passes an IPv4-only field, so a value
+ * that parses as an IPv6 literal is left untouched rather than canonicalized, the same as any
+ * other input malformed for the field.
*/
- private static String canonicalizeIpv4Cidr(String destinationCidrBlock) {
- if (!isSet(destinationCidrBlock) || destinationCidrBlock.contains(":")) {
- return destinationCidrBlock;
+ private static String canonicalizeIpv4Cidr(String ipv4CidrBlock) {
+ if (!isSet(ipv4CidrBlock) || ipv4CidrBlock.contains(":")) {
+ return ipv4CidrBlock;
}
- return CidrCanonicalizer.canonicalize(destinationCidrBlock).orElse(destinationCidrBlock);
+ return CidrCanonicalizer.canonicalize(ipv4CidrBlock).orElse(ipv4CidrBlock);
}
/**
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/CidrCanonicalizerTest.java b/src/test/java/io/github/hectorvent/floci/core/common/CidrCanonicalizerTest.java
index ca69d18b71..dd0c785456 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/CidrCanonicalizerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/CidrCanonicalizerTest.java
@@ -161,4 +161,39 @@ class CidrCanonicalizerTest {
assertEquals(Optional.empty(), CidrCanonicalizer.canonicalize(" "));
assertFalse(CidrCanonicalizer.isCanonical(" "));
}
+
+ // -----------------------------------------------------------------
+ // sameBlock: the comparison the CloudFormation provisioners run on update.
+ // -----------------------------------------------------------------
+
+ @Test
+ void sameBlock_hostBitsOnEitherSideAreTheSameNetwork() {
+ assertTrue(CidrCanonicalizer.sameBlock("10.0.1.9/24", "10.0.1.0/24"));
+ assertTrue(CidrCanonicalizer.sameBlock("10.0.1.0/24", "10.0.1.9/24"));
+ assertTrue(CidrCanonicalizer.sameBlock("10.0.1.9/24", "10.0.1.77/24"));
+ }
+
+ @Test
+ void sameBlock_equalBlocksMatch() {
+ assertTrue(CidrCanonicalizer.sameBlock("10.0.0.0/16", "10.0.0.0/16"));
+ }
+
+ @Test
+ void sameBlock_differentNetworksOrPrefixesDoNotMatch() {
+ assertFalse(CidrCanonicalizer.sameBlock("10.0.1.0/24", "10.0.2.0/24"));
+ assertFalse(CidrCanonicalizer.sameBlock("10.0.0.0/16", "10.0.0.0/24"));
+ }
+
+ @Test
+ void sameBlock_nullsMatchOnlyEachOther() {
+ assertTrue(CidrCanonicalizer.sameBlock(null, null));
+ assertFalse(CidrCanonicalizer.sameBlock(null, "10.0.0.0/16"));
+ assertFalse(CidrCanonicalizer.sameBlock("10.0.0.0/16", null));
+ }
+
+ @Test
+ void sameBlock_malformedSideIsComparedRaw() {
+ assertTrue(CidrCanonicalizer.sameBlock("not-a-cidr", "not-a-cidr"));
+ assertFalse(CidrCanonicalizer.sameBlock("not-a-cidr", "10.0.0.0/16"));
+ }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkCfnProvisionerTest.java
index ffa425cd2c..63346f29a4 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkCfnProvisionerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkCfnProvisionerTest.java
@@ -296,6 +296,38 @@ class Ec2NetworkCfnProvisionerTest {
assertFalse(r.getAttributes().containsKey(Ec2NetworkCfnProvisioner.IN_PLACE_PRIOR_ATTR));
}
+ @Test
+ void anUnchangedSubnetWhoseTemplateCidrHasHostBitsIsKept() {
+ Subnet current = subnet();
+ current.setCidrBlock("10.0.1.0/24");
+ when(ec2.describeSubnets(REGION, List.of(SUBNET_ID), Map.of())).thenReturn(List.of(current));
+ StackResource r = prior("AWS::EC2::Subnet", "Subnet", SUBNET_ID, Map.of("SubnetId", SUBNET_ID));
+ ObjectNode props = mapper.createObjectNode().put("VpcId", VPC_ID).put("CidrBlock", "10.0.1.9/24");
+
+ provisioner.provision(r, props, ctx(SUBNET_ID));
+
+ verify(ec2, never()).createSubnet(any(), any(), any(), any());
+ assertEquals(SUBNET_ID, r.getPhysicalId());
+ assertFalse(provisioner.hasReplacementUpdate(r));
+ }
+
+ @Test
+ void aSubnetSavedWithHostBitsBeforeCanonicalizationIsKept() {
+ // State persisted by an older build can still hold the template's spelling, host bits and all.
+ Subnet current = subnet();
+ current.setCidrBlock("10.0.1.9/24");
+ when(ec2.describeSubnets(REGION, List.of(SUBNET_ID), Map.of())).thenReturn(List.of(current));
+ StackResource r = prior("AWS::EC2::Subnet", "Subnet", SUBNET_ID, Map.of("SubnetId", SUBNET_ID));
+ ObjectNode props = mapper.createObjectNode().put("VpcId", VPC_ID).put("CidrBlock", "10.0.1.9/24")
+ .put("MapPublicIpOnLaunch", true);
+
+ provisioner.provision(r, props, ctx(SUBNET_ID));
+
+ verify(ec2, never()).createSubnet(any(), any(), any(), any());
+ assertEquals(SUBNET_ID, r.getPhysicalId());
+ assertFalse(provisioner.hasReplacementUpdate(r));
+ }
+
@Test
void aSubnetWithAChangedCidrIsReplacedAndThePriorOneIsOwedToCleanup() {
Subnet current = subnet();
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2VpcCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2VpcCfnProvisionerTest.java
index 046942bd10..b765b864a0 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2VpcCfnProvisionerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2VpcCfnProvisionerTest.java
@@ -111,6 +111,33 @@ class Ec2VpcCfnProvisionerTest {
"reusing must keep the id every subnet and route table already references");
}
+ @Test
+ void anUnchangedVpcWhoseTemplateCidrHasHostBitsIsReused() {
+ // The backend stores the canonical block, so the template's spelling must be compared in
+ // that form or every update would replace the VPC.
+ when(ec2.describeVpcs(REGION, List.of("vpc-existing"), Map.of()))
+ .thenReturn(List.of(vpc("vpc-existing", "10.0.0.0/16")));
+
+ StackResource r = provision("vpc-existing", """
+ {"CidrBlock": "10.0.0.5/16"}""");
+
+ verify(ec2, never()).createVpc(anyString(), anyString(), anyBoolean());
+ assertEquals("vpc-existing", r.getPhysicalId());
+ }
+
+ @Test
+ void aVpcSavedWithHostBitsBeforeCanonicalizationIsReused() {
+ // State persisted by an older build can still hold the template's spelling, host bits and all.
+ when(ec2.describeVpcs(REGION, List.of("vpc-existing"), Map.of()))
+ .thenReturn(List.of(vpc("vpc-existing", "10.0.0.5/16")));
+
+ StackResource r = provision("vpc-existing", """
+ {"CidrBlock": "10.0.0.5/16"}""");
+
+ verify(ec2, never()).createVpc(anyString(), anyString(), anyBoolean());
+ assertEquals("vpc-existing", r.getPhysicalId());
+ }
+
@Test
void aChangedCidrBlockCreatesAReplacement() {
when(ec2.describeVpcs(REGION, List.of("vpc-existing"), Map.of()))
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.java
index f8d6276df1..f5b0675ee0 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
@@ -689,6 +689,48 @@ class Ec2ServiceTest {
.filter(s -> vpc.getVpcId().equals(s.getVpcId())).count());
}
+ @Test
+ void createVpcAndCreateSubnetStoreTheCidrBlockInCanonicalForm() {
+ // AWS CreateVpc/CreateSubnet: "We modify the specified CIDR block to its canonical form; for
+ // example, if you specify 100.68.0.18/18, we modify it to 100.68.0.0/18."
+ 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", "10.20.0.5/16", false).getVpcId();
+ String subnetId = service.createSubnet("us-east-1", vpcId, "10.20.1.9/24", "us-east-1a").getSubnetId();
+ String canonicalSubnetId = service.createSubnet("us-east-1", vpcId, "10.20.2.0/24", "us-east-1a").getSubnetId();
+
+ Vpc vpc = service.describeVpcs("us-east-1", List.of(vpcId), Map.of()).get(0);
+ assertEquals("10.20.0.0/16", vpc.getCidrBlock());
+ assertEquals(List.of("10.20.0.0/16"), vpc.getCidrBlockAssociationSet().stream()
+ .map(a -> a.getCidrBlock()).toList());
+ assertEquals("10.20.1.0/24",
+ service.describeSubnets("us-east-1", List.of(subnetId), Map.of()).get(0).getCidrBlock());
+ assertEquals("10.20.2.0/24",
+ service.describeSubnets("us-east-1", List.of(canonicalSubnetId), Map.of()).get(0).getCidrBlock());
+
+ // The conflict check runs on the canonical form, and reports it.
+ AwsException error = assertThrows(AwsException.class, () -> service.createSubnet(
+ "us-east-1", vpcId, "10.20.1.0/24", "us-east-1a"));
+ assertEquals("InvalidSubnet.Conflict", error.getErrorCode());
+ AwsException hostBits = assertThrows(AwsException.class, () -> service.createSubnet(
+ "us-east-1", vpcId, "10.20.2.77/24", "us-east-1a"));
+ assertEquals("The CIDR '10.20.2.0/24' conflicts with another subnet", hostBits.getMessage());
+ }
+
+ @Test
+ void createVpcKeepsAnAlreadyCanonicalCidrBlockUnchanged() {
+ 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", "10.30.0.0/16", false).getVpcId();
+
+ assertEquals("10.30.0.0/16",
+ service.describeVpcs("us-east-1", List.of(vpcId), Map.of()).get(0).getCidrBlock());
+ }
+
@Test
void createSubnetRejectsCidrThatPartiallyOverlapsAnExistingSubnet() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),