From 22ff93365e0844b01cba0aa0799b863d8afa8d80 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Mon, 5 Oct 2026 18:37:51 -0500 Subject: [PATCH] fix(ec2): plan a VPC's CIDR against another Region's network for the same VPC id (#5134) When planning a VPC's CIDR, Floci skips its own surviving Docker network for that VPC so a restart does not read it as a collision. The match was on the VPC id and owner only, but two Regions can hold a VPC with the same id (the legacy vpc-default). Planning that id in one Region then skipped the other Region's network, accepted an overlapping CIDR, and the Docker network create failed, leaving instances without VPC connectivity. The skip now also requires the network's Region label to match; every VPC network has carried one since VPC networks were introduced. --- .../services/ec2/net/VpcNetworkManager.java | 24 ++++++++++------- .../ec2/net/VpcNetworkManagerTest.java | 27 +++++++++++++++++++ 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManager.java b/src/main/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManager.java index e244415fee..6fd03f5a35 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManager.java @@ -208,13 +208,13 @@ public class VpcNetworkManager { private VpcBinding planVpc(String region, String vpcId, String declaredCidr) { Optional declared = Cidr4.parse(declaredCidr); - String rejection = rejectionReason(vpcId, declared, declaredCidr, List.of()); + String rejection = rejectionReason(region, vpcId, declared, declaredCidr, List.of()); Cidr4 effective; if (rejection == null) { effective = declared.orElseThrow(); } else { - effective = allocateFromFallbackPool(vpcId, declared.map(Cidr4::prefix).orElse(null)); + effective = allocateFromFallbackPool(region, vpcId, declared.map(Cidr4::prefix).orElse(null)); if (effective == null) { LOG.warnv("VPC {0} in {1}: declared CIDR {2} is unusable ({3}) and the fallback pool {4} " + "is exhausted. This VPC gets no Docker network; its instances keep " @@ -237,7 +237,7 @@ public class VpcNetworkManager { * @return null when the block can be used as declared, otherwise a human-readable reason * it cannot, which goes verbatim into the substitution WARN. */ - private String rejectionReason(String vpcId, Optional declared, String declaredText, + private String rejectionReason(String region, String vpcId, Optional declared, String declaredText, Collection extraTaken) { if (declared.isEmpty()) { return declaredText == null || declaredText.isBlank() @@ -247,11 +247,11 @@ public class VpcNetworkManager { if (!cidr.isRfc1918()) { return "outside RFC 1918 private space (10/8, 172.16/12, 192.168/16)"; } - Optional clash = firstClash(vpcId, cidr, extraTaken); + Optional clash = firstClash(region, vpcId, cidr, extraTaken); return clash.orElse(null); } - private Optional firstClash(String vpcId, Cidr4 candidate, Collection extraTaken) { + private Optional firstClash(String region, String vpcId, Cidr4 candidate, Collection extraTaken) { for (Cidr4 taken : extraTaken) { if (candidate.overlaps(taken)) { return Optional.of("overlaps an address range already in use (" + taken + ")"); @@ -264,7 +264,7 @@ public class VpcNetworkManager { + "cannot route two identical ranges"); } } - for (Cidr4 taken : dockerNetworkSubnets(vpcId)) { + for (Cidr4 taken : dockerNetworkSubnets(region, vpcId)) { if (candidate.overlaps(taken)) { return Optional.of("overlaps an existing Docker network (" + taken + ")"); } @@ -352,7 +352,7 @@ public class VpcNetworkManager { return null; } - private Cidr4 allocateFromFallbackPool(String vpcId, Integer declaredPrefix) { + private Cidr4 allocateFromFallbackPool(String region, String vpcId, Integer declaredPrefix) { EmulatorConfig.VpcNetworksConfig cfg = config.services().ec2().vpcNetworks(); Optional pool = Cidr4.parse(cfg.fallbackPool()); if (pool.isEmpty() || !pool.get().isRfc1918()) { @@ -366,7 +366,7 @@ public class VpcNetworkManager { // asked for, not a /16 that wastes fifteen sixteenths of the pool per VPC. prefix = Math.min(declaredPrefix, MAX_ALLOCATABLE_PREFIX); } - List taken = new ArrayList<>(dockerNetworkSubnets(vpcId)); + List taken = new ArrayList<>(dockerNetworkSubnets(region, vpcId)); bindings.values().stream().map(b -> b.effective).filter(c -> c != null).forEach(taken::add); return allocateSubBlock(pool.get(), prefix, taken); } @@ -938,16 +938,20 @@ public class VpcNetworkManager { /** * Every IPv4 range the daemon has already reserved, so a declared CIDR can be checked before use. * + * @param ownRegion the Region of the VPC being planned * @param ownVpcId the VPC being planned; this instance's surviving network for that same VPC is * excluded, so a Floci restart re-planning a VPC whose bridge is still up does - * not read its own network as a collision and substitute a CIDR it already has + * not read its own network as a collision and substitute a CIDR it already has. + * Two Regions can hold a VPC with the same id (the legacy {@code vpc-default}), + * so the network must be for that VPC in the same Region */ - private Set dockerNetworkSubnets(String ownVpcId) { + private Set dockerNetworkSubnets(String ownRegion, String ownVpcId) { Set taken = new java.util.LinkedHashSet<>(); try { for (Network network : dockerClient.listNetworksCmd().exec()) { Map labels = network.getLabels() == null ? Map.of() : network.getLabels(); if (ownVpcId != null && ALIASES.matches(labels, ContainerStorageHelper.RESOURCE_ID_LABEL, ownVpcId) + && ALIASES.matches(labels, ContainerStorageHelper.REGION_LABEL, ownRegion) && ownedByThisDeployment(labels)) { continue; } diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManagerTest.java index 5ecdd38d23..9982813967 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManagerTest.java @@ -521,6 +521,33 @@ class VpcNetworkManagerTest { assertEquals("10.0.0.0/16", manager.effectiveVpcCidr(REGION, "vpc-1").orElseThrow()); } + // Catches: planning a VPC skipping another Region's surviving network for a VPC with the same id, + // which then accepts an overlapping CIDR and fails when its own network is created. + @Test + void anotherRegionsNetworkForTheSameVpcIdIsStillACollision() { + Map otherRegion = new LinkedHashMap<>(newLabels("vpc-default", "4650")); + otherRegion.put("io.floci.region", "eu-west-1"); + existingNetwork(manager.networkName("eu-west-1", "vpc-default"), "10.0.0.0/16", otherRegion); + + manager.declareVpc(REGION, "vpc-default", "10.0.0.0/16"); + + assertTrue(manager.isSubstituted(REGION, "vpc-default"), + "eu-west-1's network for vpc-default holds 10.0.0.0/16; us-east-1's vpc-default cannot reuse it"); + assertNotEquals("10.0.0.0/16", manager.effectiveVpcCidr(REGION, "vpc-default").orElseThrow()); + } + + // The same rule through the labels a network carried before the io.floci.* keys. + @Test + void anotherRegionsLegacyNetworkForTheSameVpcIdIsStillACollision() { + Map otherRegion = new LinkedHashMap<>(ourLabels("vpc-default", "4650")); + otherRegion.put("floci_vpc_region", "eu-west-1"); + existingNetwork("floci-aws-vpc-4650-eu-west-1-vpc-default", "10.0.0.0/16", otherRegion); + + manager.declareVpc(REGION, "vpc-default", "10.0.0.0/16"); + + assertTrue(manager.isSubstituted(REGION, "vpc-default")); + } + @Test void createdNetworkCarriesTheIdentityLabelsAndTheirLegacyAliases() { manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");