Light, fluffy, and always free - The AWS Local Emulator alternative
feat(ec2): back each VPC with a real Docker network in RFC 1918 space (#3272)
Allen Sanborncf1aabce39a6
diff --git a/docs/services/ec2.md b/docs/services/ec2.mdindex fa692e9bad..65cb24c8e4 100644--- a/docs/services/ec2.md+++ b/docs/services/ec2.md@@ -686,6 +686,45 @@ State is reported settled rather than transitional, as elsewhere in this service| `FLOCI_SERVICES_EC2_AWS_FAITHFUL_PRIVATE_IP` | `false` | Report the CFN/subnet-allocated private IP instead of the container bridge IP; routing and IMDS are unaffected || `FLOCI_SERVICES_EC2_CONTAINER_IPS_ROUTABLE` | auto-detect | Whether an instance's container IP is reachable from the machines consuming Floci's API (Terraform, Terratest, your shell). When it is, DescribeInstances and DescribeAddresses report the container IP, so port 22 really is port 22; when it is not, they report `127.0.0.1` and reachability goes through the published high host ports. Detected by a throwaway TCP connect; set explicitly when Floci itself runs as a container || `FLOCI_SERVICES_EC2_RECONCILE_CONTAINERS_ON_STARTUP` | `true` | On startup, remove instance containers this Floci left on the daemon whose record did not survive the restart or came back terminated; stopped instances are never swept |+| `FLOCI_SERVICES_EC2_VPC_NETWORKS_ENABLED` | `true` | Back each VPC with a real Docker network (see [VPC Docker networks](#vpc-docker-networks)) |+| `FLOCI_SERVICES_EC2_VPC_NETWORKS_FALLBACK_POOL` | `10.240.0.0/12` | RFC 1918 pool that substituted CIDRs are drawn from |+| `FLOCI_SERVICES_EC2_VPC_NETWORKS_FALLBACK_PREFIX_LENGTH` | `16` | Prefix length of each block handed out of that pool |+| `FLOCI_SERVICES_EC2_VPC_NETWORKS_RECONCILE_ON_STARTUP` | `true` | Remove VPC networks left behind by a previous run of this same emulator |+| `FLOCI_SERVICES_EC2_VPC_NETWORKS_DRIVER` | `bridge` | Docker network driver used for VPC networks |++## VPC Docker networks++Each VPC is backed by a real Docker network, created lazily when the first instance in that VPC+launches. An instance's reported private IP is then an address its container actually holds, drawn+from the subnet CIDR the caller declared, not a plausible-looking number. Instances in the same+VPC reach each other at those addresses; instances in different VPCs sit on different bridges.++One network per **VPC**, not per subnet: subnets inside a VPC route to each other in AWS, so a+network per subnet would manufacture a partition AWS does not have. Per-subnet addressing is kept+anyway: the network's IPAM pool is the whole VPC CIDR and each subnet allocates static addresses+out of its own slice of it.++The declared CIDR is used verbatim whenever it can be. It cannot be when it is absent, malformed,+outside RFC 1918, or already claimed on the Docker daemon, including by another Floci VPC, since+two VPCs may legally declare the same CIDR in AWS but one daemon cannot route two identical+ranges. Only then is an equivalent block taken from `fallback-pool`, and the substitution is+**logged at WARN**: a reported private IP that does not mean what the caller declared is either+true or it is in the log.++Limits worth knowing before reading a passing test as evidence:++- **Security groups and NACLs are not enforced by this.** Within one Docker network every+ container reaches every other on every port.+- **Between-VPC isolation is the daemon's, not Floci's.** It comes from Docker's own+ `DOCKER-ISOLATION-STAGE` rules. OrbStack does not apply them: measured on OrbStack 29.4.0,+ two containers on separate networks reach each other in both directions, `--internal` included.+ On such a host the VPC boundary is an addressing boundary only.+- **On Docker Desktop for macOS and Windows container addresses do not answer from the host.**+ The address is real and reachable container-to-container, but a host-side client cannot dial+ it. This is why SSH keeps its published host port.++Set `FLOCI_SERVICES_EC2_VPC_NETWORKS_ENABLED=false` to go back to synthesised private addresses+and the shared default bridge. It is also off whenever `mock` is on.## Requirementsdiff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.javaindex c0856a9a98..e4ee656d08 100644--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java@@ -2238,6 +2238,43 @@ public interface EmulatorConfig {/** When true, instances go straight to RUNNING without launching Docker containers. */@WithDefault("false")boolean mock();++ /** Docker-network backing for VPCs and subnets. */+ VpcNetworksConfig vpcNetworks();+ }++ /**+ * Backs each VPC with a real Docker network so instances get private addresses drawn from+ * the CIDR the caller declared, and instances in different VPCs cannot route to each other.+ */+ interface VpcNetworksConfig {+ @WithDefault("true")+ boolean enabled();++ /**+ * Private range that substituted CIDRs are allocated from, used when a declared VPC CIDR+ * is absent, malformed, outside RFC 1918, or already claimed on the Docker daemon. Must+ * itself be RFC 1918. The default sits high in 10/8, away from both Docker's default+ * pools (172.17-172.31, 192.168) and the low 10.x ranges corporate VPNs favour.+ */+ @WithDefault("10.240.0.0/12")+ String fallbackPool();++ /** Prefix length of each block handed out of {@link #fallbackPool}. */+ @WithDefault("16")+ int fallbackPrefixLength();++ /**+ * When true, VPC networks left behind by a previous run of this same Floci instance are+ * removed at startup. Scoped by the emulator's API port, so instances sharing a Docker+ * daemon never reconcile each other's networks.+ */+ @WithDefault("true")+ boolean reconcileOnStartup();++ /** Docker network driver for VPC networks. */+ @WithDefault("bridge")+ String driver();}interface AppConfigServiceConfig {diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.javaindex 3bd93296e7..c2091135a8 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.java@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;import io.github.hectorvent.floci.core.common.docker.PortAllocator;import io.github.hectorvent.floci.services.ec2.model.Instance;import io.github.hectorvent.floci.services.ec2.model.InstanceState;+import io.github.hectorvent.floci.services.ec2.net.VpcNetworkManager;import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager;import com.github.dockerjava.api.DockerClient;import com.github.dockerjava.api.exception.NotFoundException;@@ -165,6 +166,7 @@ public class Ec2ContainerManager {private final Ec2PortForwardManager portForwardManager;private final RegionResolver regionResolver;private final ContainerNetworkReachability containerNetworkReachability;+ private final VpcNetworkManager vpcNetworkManager;private final ExecutorService executor;private final Duration userDataExecutionTimeout;private final Set<ResultCallback<Frame>> activeUserDataCallbacks = ConcurrentHashMap.newKeySet();@@ -183,10 +185,11 @@ public class Ec2ContainerManager {Ec2MetadataServer metadataServer,Ec2PortForwardManager portForwardManager,RegionResolver regionResolver,- ContainerNetworkReachability containerNetworkReachability) {+ ContainerNetworkReachability containerNetworkReachability,+ VpcNetworkManager vpcNetworkManager) {this(containerBuilder, lifecycleManager, logStreamer, containerDetector, dockerHostResolver, dockerClient,portAllocator, config, metadataServer, portForwardManager, regionResolver,- containerNetworkReachability, createLaunchExecutor(),+ containerNetworkReachability, vpcNetworkManager, createLaunchExecutor(),Duration.ofMinutes(USER_DATA_EXECUTION_TIMEOUT_MINUTES));}@@ -202,6 +205,7 @@ public class Ec2ContainerManager {Ec2PortForwardManager portForwardManager,RegionResolver regionResolver,ContainerNetworkReachability containerNetworkReachability,+ VpcNetworkManager vpcNetworkManager,ExecutorService executor,Duration userDataExecutionTimeout) {this.containerBuilder = containerBuilder;@@ -216,6 +220,7 @@ public class Ec2ContainerManager {this.metadataServer = metadataServer;this.portForwardManager = portForwardManager;this.containerNetworkReachability = containerNetworkReachability;+ this.vpcNetworkManager = vpcNetworkManager;this.executor = executor;this.userDataExecutionTimeout = userDataExecutionTimeout;}@@ -278,6 +283,11 @@ public class Ec2ContainerManager {return;}+ // Captured before anything can overwrite it: on a launch that never attaches to the VPC+ // network, exposeReachablePrivateAddress replaces the reported private IP with the bridge+ // one, and the address actually leased would otherwise be unrecoverable on the paths below.+ String leasedPrivateIp = instance.getPrivateIpAddress();+try {executor.execute(() -> {try {@@ -285,15 +295,23 @@ public class Ec2ContainerManager {// IMDS endpoint that this container should useString flociHost = dockerHostResolver.resolve();int imdsPort = config.services().ec2().imdsPort();- StartedContainer started = createAndStartContainer(instance, image, region, flociHost, imdsPort);+ StartedContainer started = createAndStartContainer(instance, image, region, flociHost, imdsPort,+ leasedPrivateIp);if (started == null) {return;}int sshHostPort = started.sshHostPort();String containerId = started.containerId();+ String vpcAddress = started.vpcAddress();+ if (vpcAddress == null) {+ // Nothing holds the address, and moments from now this instance will be+ // reporting its bridge address instead, so the lease would no longer be+ // findable from the instance at terminate time. Give it back here.+ vpcNetworkManager.releasePrivateIp(region, instance.getSubnetId(), leasedPrivateIp);+ }if (isLaunchCancelled(instance)) {- failLaunch(instance);+ failLaunch(instance, leasedPrivateIp);return;}@@ -301,7 +319,7 @@ public class Ec2ContainerManager {boolean running = false;for (int i = 0; i < 30 && !running; i++) {if (isLaunchCancelled(instance)) {- failLaunch(instance);+ failLaunch(instance, leasedPrivateIp);return;}running = lifecycleManager.isContainerRunning(containerId);@@ -312,12 +330,12 @@ public class Ec2ContainerManager {if (!running) {LOG.warnv("EC2 instance {0} container {1} did not reach running state", instanceId, containerId);- failLaunch(instance);+ failLaunch(instance, leasedPrivateIp);return;}if (isLaunchCancelled(instance)) {- failLaunch(instance);+ failLaunch(instance, leasedPrivateIp);return;}@@ -326,6 +344,17 @@ public class Ec2ContainerManager {// settings are populated; wait here so IMDS is registered// before link-local metadata validation and UserData run.String containerIp = waitForContainerBridgeIp(containerId, instanceId, instance);+ if (vpcAddress != null) {+ // The VPC address is the one Floci reports and the one peers in the same VPC+ // dial. The bridge address still identifies this container to IMDS, because+ // the default route, and so the source address of its metadata requests, is+ // the bridge.+ if (containerIp != null && !containerIp.equals(vpcAddress)) {+ instance.setImdsSourceIp(containerIp);+ metadataServer.registerContainer(containerIp, instanceId, instance);+ }+ containerIp = vpcAddress;+ }if (containerIp != null && !containerIp.isBlank()) {instance.setContainerBridgeIp(containerIp);exposeReachablePrivateAddress(instance, containerIp, config.services().ec2().awsFaithfulPrivateIp());@@ -334,12 +363,12 @@ public class Ec2ContainerManager {else {LOG.warnv("EC2 instance {0} container {1} did not receive a usable bridge IP for IMDS",instanceId, containerId);- failLaunch(instance);+ failLaunch(instance, leasedPrivateIp);return;}if (!markRunning(instance)) {- failLaunch(instance);+ failLaunch(instance, leasedPrivateIp);return;}@@ -381,14 +410,14 @@ public class Ec2ContainerManager {} catch (InterruptedException e) {Thread.currentThread().interrupt();- failLaunch(instance);+ failLaunch(instance, leasedPrivateIp);} catch (Exception e) {LOG.warnv("Failed to launch EC2 instance {0}: {1}", instance.getInstanceId(), e.getMessage());// The daemon can disappear between the probe above and any of the calls in// this block. Losing Docker is not the instance's fault, so degrade to a// metadata-only instance; a genuine container failure still fails the launch.if (isDockerAvailable()) {- failLaunch(instance);+ failLaunch(instance, leasedPrivateIp);} else {markContainerlessRunning(instance);}@@ -397,12 +426,12 @@ public class Ec2ContainerManager {} catch (RejectedExecutionException e) {LOG.warnv("Could not schedule EC2 instance {0} launch because the launch executor is saturated or stopping",instance.getInstanceId());- failLaunch(instance);+ failLaunch(instance, leasedPrivateIp);}}private StartedContainer createAndStartContainer(Instance instance, ResolvedAmiImage image, String region,- String flociHost, int imdsPort) {+ String flociHost, int imdsPort, String leasedPrivateIp) {String instanceId = instance.getInstanceId();String containerName = ContainerStorageHelper.resourceName(config, "ec2", null, instanceId);String imdsEndpoint = "http://" + flociHost + ":" + imdsPort;@@ -429,8 +458,16 @@ public class Ec2ContainerManager {return null;}recorded = true;+ // Join the VPC's Docker network at the address the subnet allocated, before the+ // container starts, so the guest comes up already holding its private IP. The+ // default bridge attachment stays: it is what carries the published SSH host+ // port, which a network mode set at creation time would suppress.+ String vpcAddress = vpcNetworkManager.attach(region, instance.getVpcId(), instance.getSubnetId(),+ containerId, leasedPrivateIp)+ .map(network -> leasedPrivateIp)+ .orElse(null);lifecycleManager.startCreated(containerId, spec);- return new StartedContainer(containerId, sshHostPort);+ return new StartedContainer(containerId, sshHostPort, vpcAddress);} catch (Exception e) {boolean ownsCleanup = !recorded || clearRecordedContainer(instance, containerId, sshHostPort);if (!ownsCleanup) {@@ -486,6 +523,19 @@ public class Ec2ContainerManager {}private void failLaunch(Instance instance) {+ failLaunch(instance, instance.getPrivateIpAddress());+ }++ /**+ * Ends a launch that never reached RUNNING.+ *+ * <p>The private address was leased before the container existed, and {@link #terminate}, the+ * only other place it is given back, is not on this path: an instance that fails here is+ * already terminated, so nothing terminates it again and the lease would be held for the life+ * of the process. Detaching first is what makes the release safe; Docker keeps the address+ * reserved while the endpoint stands, and would refuse the next launch handed the same one.+ */+ private void failLaunch(Instance instance, String leasedPrivateIp) {String containerId;int sshHostPort;String containerIp;@@ -506,6 +556,13 @@ public class Ec2ContainerManager {if (alreadyCleaned) {return;}+ try {+ vpcNetworkManager.detach(instance.getRegion(), instance.getVpcId(), containerId);+ vpcNetworkManager.releasePrivateIp(instance.getRegion(), instance.getSubnetId(), leasedPrivateIp);+ } catch (Exception e) {+ LOG.warnv("Error releasing the VPC address of failed EC2 launch {0}: {1}",+ instance.getInstanceId(), e.getMessage());+ }try {portForwardManager.unpublishAll(instance);} catch (Exception e) {@@ -608,7 +665,7 @@ public class Ec2ContainerManager {return false;}- private record StartedContainer(String containerId, int sshHostPort) {+ private record StartedContainer(String containerId, int sshHostPort, String vpcAddress) {}/**@@ -724,6 +781,7 @@ public class Ec2ContainerManager {exposeReachablePublicAddress(instance);}metadataServer.registerContainer(containerIp, instanceId, instance);+ refreshImdsSourceRegistration(instance, containerId, containerIp);}} catch (InterruptedException e) {Thread.currentThread().interrupt();@@ -823,9 +881,85 @@ public class Ec2ContainerManager {exposeReachablePublicAddress(instance);}metadataServer.registerContainer(containerIp, instance.getInstanceId(), instance);+ refreshImdsSourceRegistration(instance, containerId, containerIp);return true;}+ /**+ * Keeps the IMDS registration of a VPC-attached instance's bridge address current.+ *+ * <p>An instance on a VPC network has two addresses, and only one of them is the one IMDS+ * sees. {@code Ec2MetadataServer} resolves an instance from the source address of the+ * request, and the container's default route is the bridge, so its metadata requests arrive+ * from the bridge address. The address Floci reports, and the one+ * {@link #getContainerBridgeIp} prefers, is the VPC address, so registering only that one+ * leaves IMDS with no entry for the address the requests actually come from.+ *+ * <p>{@code launch} gets this right. {@link #start} and {@link #restoreMetadataRegistration}+ * did not, and both are exactly where it goes wrong: Docker hands out a fresh bridge address+ * when a stopped container starts again, and an emulator restart rebuilds the registration+ * map empty. Either way the instance came back with its bridge address unregistered and+ * {@code imdsSourceIp} still naming the address of a previous run, which then also survived+ * as a stale entry pointing at this instance.+ *+ * @param reportedIp the address the instance reports, already registered by the caller; when+ * the bridge address is the same one, there is no second address to track+ */+ private void refreshImdsSourceRegistration(Instance instance, String containerId, String reportedIp) {+ String bridgeIp;+ try {+ bridgeIp = bridgeNetworkIp(containerId);+ } catch (RuntimeException e) {+ // An inspect that failed says nothing about where the container is attached, and the+ // unregister below is only correct for a container that definitely has no separate+ // bridge address. Reading "I could not find out" as "there is none" would tear down a+ // healthy instance's IMDS source registration over a transient Docker hiccup and put+ // nothing in its place, leaving its metadata requests unresolvable until some later+ // start or restore happened to succeed. Leave the registration exactly as it is; the+ // next start or restore refreshes it once inspect works again.+ LOG.warnv("Could not inspect container {0} for its bridge IP, leaving the IMDS source "+ + "registration of EC2 instance {1} unchanged: {2}",+ containerId, instance.getInstanceId(), e.getMessage());+ return;+ }+ // Nothing to track separately when the reported address is the bridge address: that is a+ // plain bridge-only instance, and the caller has already registered it.+ String current = bridgeIp != null && !bridgeIp.isBlank() && !bridgeIp.equals(reportedIp) ? bridgeIp : null;+ String previous = instance.getImdsSourceIp();++ if (previous != null && !previous.isBlank() && !previous.equals(current)) {+ metadataServer.unregisterContainer(previous, instance);+ instance.setImdsSourceIp(null);+ }+ if (current != null) {+ instance.setImdsSourceIp(current);+ metadataServer.registerContainer(current, instance.getInstanceId(), instance);+ }+ }++ /**+ * The container's address on Docker's default bridge, which is where its default route, and+ * so the source address of its IMDS requests, lives. Distinct from+ * {@link #getContainerBridgeIp}, which despite the name prefers the VPC network's address.+ *+ * <p>Returning null has to mean one thing only, "this container has no bridge address",+ * because callers act on that answer. A failed inspect is a different answer, "I could not+ * find out", so it propagates instead of being folded into the same null.+ *+ * @return the address, or null when the container is not on the default bridge at all+ * @throws RuntimeException if the container could not be inspected, leaving its bridge+ * attachment unknown rather than known to be absent+ */+ private String bridgeNetworkIp(String containerId) {+ var inspect = dockerClient.inspectContainerCmd(containerId).exec();+ if (inspect.getNetworkSettings() == null || inspect.getNetworkSettings().getNetworks() == null) {+ return null;+ }+ ContainerNetwork bridge = inspect.getNetworkSettings().getNetworks().get("bridge");+ return bridge == null || bridge.getIpAddress() == null || bridge.getIpAddress().isBlank()+ ? null : bridge.getIpAddress();+ }+/*** The address Floci is willing to hand out as this instance's public address — chosen so* that whatever dials it reaches the guest on the service's own port.@@ -901,10 +1035,12 @@ public class Ec2ContainerManager {public void terminate(Instance instance) {String containerId;String containerIp;+ String imdsSourceIp;int sshHostPort;synchronized (instance) {containerId = instance.getDockerContainerId();containerIp = instance.getContainerBridgeIp();+ imdsSourceIp = instance.getImdsSourceIp();sshHostPort = instance.getSshHostPort();instance.setState(InstanceState.shuttingDown());}@@ -929,6 +1065,12 @@ public class Ec2ContainerManager {portAllocator.release(sshHostPort);}metadataServer.unregisterContainer(containerIp, instance);+ metadataServer.unregisterContainer(imdsSourceIp, instance);+ // Give the address back only now that the container is gone: releasing it while+ // Docker still holds the endpoint would hand the same IP to the next launch and+ // have Docker refuse it.+ vpcNetworkManager.releasePrivateIp(instance.getRegion(), instance.getSubnetId(),+ instance.getPrivateIpAddress());instance.setState(InstanceState.terminated());instance.setTerminatedAt(System.currentTimeMillis());});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 1360b6a77d..4c20b5e0b1 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@@ -46,6 +46,7 @@ import io.github.hectorvent.floci.services.ec2.model.BlockDeviceMapping;import io.github.hectorvent.floci.services.ec2.model.CapacityReservation;import io.github.hectorvent.floci.services.ec2.model.EbsBlockDevice;import io.github.hectorvent.floci.services.ec2.model.GroupIdentifier;+import io.github.hectorvent.floci.services.ec2.net.VpcNetworkManager;import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager;import io.github.hectorvent.floci.services.ec2.model.Image;import io.github.hectorvent.floci.services.ec2.model.Instance;@@ -185,6 +186,15 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {// subnetId → counter for IP assignment (runtime-only, not persisted)private final Map<String, AtomicInteger> subnetIpCounters = new ConcurrentHashMap<>();+ /**+ * Null in the hermetic unit tests, which reach the constructors that do not take it; CDI always+ * supplies one. Every use goes through a null guard for that reason. Not final: the constructor+ * chain below is four deep and threading one more parameter through all of it would touch every+ * existing test fixture's arity, so the two entry points that receive it assign it after+ * delegating.+ */+ private VpcNetworkManager vpcNetworkManager;+// Public, no request context — for callers (and tests) that construct this service directly// without CDI. Caller-identity resolution falls back to the configured default account.public Ec2Service(EmulatorConfig config, Ec2ContainerManager containerManager,@@ -192,10 +202,32 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {AmiImageResolver amiImageResolver, Ec2ImageCatalog imageCatalog,Ec2InstanceTypeCatalog instanceTypeCatalog, StorageFactory storageFactory) {this(config, containerManager, portForwardManager, amiImageResolver, imageCatalog,- instanceTypeCatalog, storageFactory, null);+ instanceTypeCatalog, storageFactory, (jakarta.enterprise.inject.Instance<RequestContext>) null);+ }++ // Package-private for tests that need the Docker-backed VPC networking but not a request context.+ Ec2Service(EmulatorConfig config, Ec2ContainerManager containerManager,+ Ec2PortForwardManager portForwardManager,+ AmiImageResolver amiImageResolver, Ec2ImageCatalog imageCatalog,+ Ec2InstanceTypeCatalog instanceTypeCatalog, StorageFactory storageFactory,+ VpcNetworkManager vpcNetworkManager) {+ this(config, containerManager, portForwardManager, amiImageResolver, imageCatalog,+ instanceTypeCatalog, storageFactory, (jakarta.enterprise.inject.Instance<RequestContext>) null);+ this.vpcNetworkManager = vpcNetworkManager;}@Inject+ public Ec2Service(EmulatorConfig config, Ec2ContainerManager containerManager,+ Ec2PortForwardManager portForwardManager,+ AmiImageResolver amiImageResolver, Ec2ImageCatalog imageCatalog,+ Ec2InstanceTypeCatalog instanceTypeCatalog, StorageFactory storageFactory,+ jakarta.enterprise.inject.Instance<RequestContext> requestContextInstance,+ VpcNetworkManager vpcNetworkManager) {+ this(config, containerManager, portForwardManager, amiImageResolver, imageCatalog,+ instanceTypeCatalog, storageFactory, requestContextInstance);+ this.vpcNetworkManager = vpcNetworkManager;+ }+public Ec2Service(EmulatorConfig config, Ec2ContainerManager containerManager,Ec2PortForwardManager portForwardManager,AmiImageResolver amiImageResolver, Ec2ImageCatalog imageCatalog,@@ -396,6 +428,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {return;}+ restoreVpcNetworks();+int restored = 0;for (String key : instances.keys()) {Instance instance = instances.get(key).orElse(null);@@ -433,6 +467,79 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {return !"terminated".equals(state) && !"shutting-down".equals(state);}+ /**+ * Rebuilds the in-memory VPC address plan from persisted VPCs and subnets, after clearing+ * out any Docker networks a previous run of this same emulator left behind.+ *+ * <p>Order matters and is the whole point. Orphaned networks still hold their IPAM+ * reservations, so re-planning first would read a dead run's own bridges as collisions and+ * quietly substitute a CIDR for every VPC that used to work.+ */+ private void restoreVpcNetworks() {+ if (vpcNetworkManager == null || !vpcNetworkManager.enabled()) {+ return;+ }+ vpcNetworkManager.reconcileOrphans((region, vpcId) -> vpcs.get(key(region, vpcId)).isPresent());+ for (String storageKey : vpcs.keys()) {+ vpcs.get(storageKey).ifPresent(vpc ->+ vpcNetworkManager.declareVpc(vpc.getRegion(), vpc.getVpcId(), vpc.getCidrBlock()));+ }+ for (String storageKey : subnets.keys()) {+ subnets.get(storageKey).ifPresent(subnet -> vpcNetworkManager.declareSubnet(+ subnet.getRegion(), subnet.getVpcId(), subnet.getSubnetId(), subnet.getCidrBlock()));+ }+ reserveRestoredPrivateIps();+ }++ /**+ * Re-claims the private addresses that persisted, non-terminated instances still hold.+ *+ * <p>The lease table is in-memory only, so a restart rebuilds it empty while the containers of+ * those instances go on holding their addresses. Without this the next RunInstances is handed+ * {@code .10} again, an address a live container already answers on, and Docker refuses the+ * attach, leaving the new instance on the bridge with a reported IP that is someone else's.+ *+ * <p>Every refusal is tolerated and logged rather than thrown: an address may now fall outside+ * a subnet whose effective range was substituted this run, and two persisted instances may name+ * the same address. Startup is not the place to fail over state that is already on disk.+ */+ private void reserveRestoredPrivateIps() {+ int reserved = 0;+ int skipped = 0;+ for (String storageKey : instances.keys()) {+ Instance instance = instances.get(storageKey).orElse(null);+ if (instance == null || instance.getSubnetId() == null+ || instance.getPrivateIpAddress() == null+ || instance.getState() == null+ || "terminated".equals(instance.getState().getName())) {+ continue;+ }+ if (vpcNetworkManager.reservePrivateIp(instance.getRegion(), instance.getSubnetId(),+ instance.getPrivateIpAddress())) {+ reserved++;+ }+ else {+ skipped++;+ }+ }+ if (reserved > 0 || skipped > 0) {+ LOG.debugv("Re-reserved {0} private address(es) held by restored instances; {1} could not be "+ + "reserved", String.valueOf(reserved), String.valueOf(skipped));+ }+ }++ private void declareVpcNetwork(String region, String vpcId, String cidrBlock) {+ if (vpcNetworkManager != null) {+ vpcNetworkManager.declareVpc(region, vpcId, cidrBlock);+ }+ }++ private void declareSubnetNetwork(String region, String vpcId, String subnetId, String cidrBlock) {+ if (vpcNetworkManager != null) {+ vpcNetworkManager.declareSubnet(region, vpcId, subnetId, cidrBlock);+ }+ }+private static boolean needsMetadataRegistration(Instance instance) {if (instance == null || instance.getDockerContainerId() == null) {return false;@@ -469,6 +576,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {defaultVpc.getCidrBlockAssociationSet().add(new VpcCidrBlockAssociation("vpc-cidr-assoc-default", "172.31.0.0/16"));vpcs.put(key(region, vpcId), defaultVpc);+ declareVpcNetwork(region, vpcId, "172.31.0.0/16");// Default subnets (a/b/c)String[] azSuffixes = MODELLED_ZONE_SUFFIXES;@@ -492,6 +600,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {subnet.setRegion(region);subnet.setSubnetArn(AwsArnUtils.Arn.of("ec2", region, ownerAccountId, "subnet/" + subnetIds[i]).toString());subnets.put(key(region, subnetIds[i]), subnet);+ declareSubnetNetwork(region, vpcId, subnetIds[i], cidrBlocks[i]);}createDefaultSecurityGroup(region, vpcId, defaultSecurityGroupId(region));@@ -2652,6 +2761,15 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {}private String assignPrivateIp(String region, String subnetId) {+ // A Docker-backed subnet allocates the real thing: an address on the network the+ // instance's container will actually hold. Only when there is no such network does+ // this fall back to the synthesised address below, which nothing can connect to.+ if (vpcNetworkManager != null) {+ Optional<String> allocated = vpcNetworkManager.allocatePrivateIp(region, subnetId);+ if (allocated.isPresent()) {+ return allocated.get();+ }+ }if (subnetId == null) {return "172.31.0." + (10 + new Random().nextInt(200));}@@ -2979,6 +3097,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {vpc.getIpv6CidrBlockAssociationSet().add(amazonProvidedIpv6Association(region));}vpcs.put(key(region, vpcId), vpc);+ declareVpcNetwork(region, vpcId, cidrBlock);createDefaultSecurityGroup(region, vpcId, "sg-" + randomHex(17));createMainRouteTable(region, vpc, "rtb-" + randomHex(17), "rtbassoc-" + randomHex(17));@@ -3009,6 +3128,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {"The vpc '" + vpcId + "' has dependencies and cannot be deleted.");vpcs.delete(key(region, vpcId));}+ if (vpcNetworkManager != null) {+ vpcNetworkManager.deleteVpcNetwork(region, vpcId);+ }}public void modifyVpcAttribute(String region, String vpcId, String attribute, String value) {@@ -3393,6 +3515,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {rejectConflictingSubnetCidr(region, vpcId, cidrBlock);subnets.put(key(region, subnetId), subnet);}+ declareSubnetNetwork(region, vpcId, subnetId, cidrBlock);// Every subnet starts associated with its VPC's default NACL. ReplaceNetworkAclAssociation// later moves it onto a custom NACL, so this association must exist for that lookup to work.@@ -3448,6 +3571,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {"The subnet '" + subnetId + "' has dependencies and cannot be deleted.");subnets.delete(key(region, subnetId));}+ if (vpcNetworkManager != null) {+ vpcNetworkManager.forgetSubnet(region, subnetId);+ }}/**diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/model/Instance.java b/src/main/java/io/github/hectorvent/floci/services/ec2/model/Instance.javaindex 80faff9139..a8d8acb09e 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/model/Instance.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/model/Instance.java@@ -64,6 +64,14 @@ public class Instance {// Docker backing fields (not serialised to AWS wire format)private String dockerContainerId;private String containerBridgeIp;+ /**+ * The container's default-bridge address, kept alongside {@code containerBridgeIp} when the+ * instance is also attached to its VPC's Docker network. IMDS identifies a caller by the+ * source address of its request, and that is whichever interface carries the container's+ * default route, the bridge, while the address Floci reports is the VPC one. Both are+ * registered so metadata answers either way.+ */+ private String imdsSourceIp;private String userData;private int sshHostPort;private long terminatedAt;@@ -187,6 +195,9 @@ public class Instance {public String getContainerBridgeIp() { return containerBridgeIp; }public void setContainerBridgeIp(String containerBridgeIp) { this.containerBridgeIp = containerBridgeIp; }+ public String getImdsSourceIp() { return imdsSourceIp; }+ public void setImdsSourceIp(String imdsSourceIp) { this.imdsSourceIp = imdsSourceIp; }+public Map<Integer, Integer> getPublishedPorts() {if (publishedPorts == null) {publishedPorts = new LinkedHashMap<>();diff --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.javanew file mode 100644index 0000000000..61c2d9fad5--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/net/Cidr4.java@@ -0,0 +1,153 @@+package io.github.hectorvent.floci.services.ec2.net;++import java.util.Objects;+import java.util.Optional;+import java.util.regex.Matcher;+import java.util.regex.Pattern;++/**+ * An IPv4 CIDR block, as arithmetic rather than text.+ *+ * <p>Floci needs to answer questions about declared VPC and subnet CIDRs that string+ * handling cannot: does this subnet actually sit inside its VPC, does this block collide+ * with a range Docker has already handed to some other network, is it inside RFC 1918,+ * and what is the Nth usable address in it. Every one of those is an integer comparison+ * once the block is a (network, prefix) pair, and every one of them is a guess when it is+ * a string that gets split on dots.+ *+ * <p>Addresses are held as unsigned values widened into a {@code long}, so the whole IPv4+ * space compares and sorts correctly without sign trouble at 128.0.0.0.+ */+public final class Cidr4 {++ private static final Pattern CIDR = Pattern.compile(+ "^\\s*(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})\\s*$");++ /** The three RFC 1918 private ranges, the only space Floci will put a VPC network in. */+ private static final Cidr4[] RFC_1918 = {+ new Cidr4(toLong(10, 0, 0, 0), 8),+ new Cidr4(toLong(172, 16, 0, 0), 12),+ new Cidr4(toLong(192, 168, 0, 0), 16)+ };++ private final long network;+ private final int prefix;++ Cidr4(long network, int prefix) {+ this.prefix = prefix;+ this.network = network & maskFor(prefix);+ }++ /**+ * @return the block, or empty when {@code text} is null, malformed, or has an octet or+ * prefix length out of range. Callers treat empty as "the declaration is unusable",+ * which is a fallback trigger, never a failure.+ */+ public static Optional<Cidr4> parse(String text) {+ if (text == null) {+ return Optional.empty();+ }+ Matcher m = CIDR.matcher(text);+ if (!m.matches()) {+ return Optional.empty();+ }+ long[] octets = new long[4];+ for (int i = 0; i < 4; i++) {+ octets[i] = Long.parseLong(m.group(i + 1));+ if (octets[i] > 255) {+ return Optional.empty();+ }+ }+ int prefix = Integer.parseInt(m.group(5));+ if (prefix > 32) {+ return Optional.empty();+ }+ return Optional.of(new Cidr4(+ (octets[0] << 24) | (octets[1] << 16) | (octets[2] << 8) | octets[3], prefix));+ }++ private static long maskFor(int prefix) {+ return prefix == 0 ? 0L : ((0xFFFFFFFFL << (32 - prefix)) & 0xFFFFFFFFL);+ }++ private static long toLong(int a, int b, int c, int d) {+ return ((long) a << 24) | ((long) b << 16) | ((long) c << 8) | d;+ }++ public long network() {+ return network;+ }++ public int prefix() {+ return prefix;+ }++ /** Number of addresses in the block, network and broadcast included. */+ public long size() {+ return 1L << (32 - prefix);+ }++ public long broadcast() {+ return network + size() - 1;+ }++ public boolean containsAddress(long address) {+ return address >= network && address <= broadcast();+ }++ public boolean contains(Cidr4 other) {+ return other.network >= network && other.broadcast() <= broadcast();+ }++ public boolean overlaps(Cidr4 other) {+ return network <= other.broadcast() && other.network <= broadcast();+ }++ public boolean isRfc1918() {+ for (Cidr4 range : RFC_1918) {+ if (range.contains(this)) {+ return true;+ }+ }+ return false;+ }++ /**+ * The address {@code offset} positions above the network address, as dotted quad.+ *+ * @return the address, or empty when the offset falls outside the block. Offset 0 is the+ * network address itself and offset 1 is conventionally the gateway, so instance+ * allocation starts well above both.+ */+ public Optional<String> addressAt(long offset) {+ if (offset < 0 || offset >= size()) {+ return Optional.empty();+ }+ return Optional.of(format(network + offset));+ }++ public static String format(long address) {+ return ((address >> 24) & 0xFF) + "." + ((address >> 16) & 0xFF) + "."+ + ((address >> 8) & 0xFF) + "." + (address & 0xFF);+ }++ /** A block of the same prefix length starting {@code index} blocks further along. */+ Cidr4 shifted(long index) {+ return new Cidr4(network + index * size(), prefix);+ }++ @Override+ public String toString() {+ return format(network) + "/" + prefix;+ }++ @Override+ public boolean equals(Object o) {+ return o instanceof Cidr4 other && other.network == network && other.prefix == prefix;+ }++ @Override+ public int hashCode() {+ return Objects.hash(network, prefix);+ }+}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.javanew file mode 100644index 0000000000..37435dab3f--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManager.java@@ -0,0 +1,910 @@+package io.github.hectorvent.floci.services.ec2.net;++import io.github.hectorvent.floci.config.EmulatorConfig;+import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;+import com.github.dockerjava.api.DockerClient;+import com.github.dockerjava.api.exception.NotFoundException;+import com.github.dockerjava.api.model.ContainerNetwork;+import com.github.dockerjava.api.model.Network;+import jakarta.annotation.PreDestroy;+import jakarta.enterprise.context.ApplicationScoped;+import jakarta.inject.Inject;+import org.jboss.logging.Logger;++import java.util.ArrayList;+import java.util.Collection;+import java.util.LinkedHashMap;+import java.util.List;+import java.util.Map;+import java.util.Optional;+import java.util.Set;+import java.util.concurrent.ConcurrentHashMap;+import java.util.concurrent.Executors;+import java.util.concurrent.ScheduledExecutorService;+import java.util.concurrent.TimeUnit;+import java.util.function.BiPredicate;++/**+ * Gives every VPC a real Docker network, so an EC2 instance's private address is an address+ * something can actually connect to rather than a number invented to look plausible.+ *+ * <h2>One network per VPC, not per subnet</h2>+ *+ * <p>A Docker network is an isolation domain: containers on one can reach each other, and+ * cannot reach containers on another. That is exactly a VPC. It is <em>not</em> a subnet:+ * subnets inside a VPC route to each other by default in AWS, so giving each subnet its own+ * Docker network would manufacture a partition that AWS does not have, and would break the+ * very common two-tier "app subnet talks to database subnet" topology in this corpus.+ *+ * <p>The per-subnet addressing that AWS gets from separate subnets is preserved anyway: the+ * network is created with the whole VPC CIDR as its IPAM pool, and each subnet allocates+ * static addresses from its own slice of that pool. A subnet CIDR is by definition inside its+ * VPC CIDR, so every such address is valid on the network. The result is one bridge per VPC+ * instead of one per subnet: the correct semantics and, incidentally, a third of the Linux+ * bridges.+ *+ * <h2>Declared CIDRs are used where they can be, substituted loudly where they cannot</h2>+ *+ * <p>The declared CIDR is used verbatim whenever it is usable, which for this corpus is+ * almost always. It is not usable when it is absent, malformed, outside RFC 1918, or already+ * claimed by another Docker network, including one of Floci's own, which is what a second+ * VPC declaring the same CIDR looks like (legal in AWS, impossible on one Docker daemon).+ * Then, and only then, an equivalent block is allocated out of a configured private pool and+ * the substitution is logged at WARN. It is never silent: a reported private IP that does not+ * mean what the caller declared is exactly the class of quiet lie this whole change exists to+ * remove, so it is either true or it is in the log.+ *+ * <h2>What this does not do</h2>+ *+ * <p>Security groups and network ACLs are untouched. Docker networks isolate between+ * networks; within one, every container can reach every other on every port. Nothing here+ * makes an assertion that traffic is <em>blocked</em> meaningful.+ *+ * <p>Between-VPC isolation is likewise only as good as the daemon underneath. It comes from+ * Docker's own {@code DOCKER-ISOLATION-STAGE} rules, which drop forwarded traffic between+ * bridge networks: Floci asks for separate networks and gets whatever the daemon does with+ * them. <strong>OrbStack does not do that.</strong> Measured on OrbStack 29.4.0, 2026-08-22:+ * two containers each attached to exactly one Docker network, on different subnets, created+ * with nothing but {@code docker network create} and {@code docker run --network}, reach each+ * other's ports in both directions. Marking the networks {@code --internal} does not change+ * it. That is a property of the host's Docker implementation and there is nothing this class+ * can do about it, so on such a host the VPC boundary is an addressing boundary only. Do not+ * read a passing "different VPCs cannot reach each other" test as evidence without checking+ * which daemon it ran on.+ *+ * <h2>Hosts where container IPs are not routable</h2>+ *+ * <p>On Docker Desktop for macOS and Windows the containers live behind a VM and none of their+ * addresses answer from the host. The private address stays correct and reachable+ * container-to-container, and the guest really holds it, but a host-side client (Terratest,+ * a shell) cannot dial it. That is the same constraint {@code ContainerNetworkReachability}+ * detects for the public address on the ec2-public-address-reachability branch, and it is why+ * SSH keeps its published host port here.+ */+@ApplicationScoped+public class VpcNetworkManager {++ private static final Logger LOG = Logger.getLogger(VpcNetworkManager.class);++ /** Marks a Docker network as a Floci VPC network, for discovery and orphan reconciliation. */+ public static final String LABEL_COMPONENT = "floci_component";+ public static final String COMPONENT_VALUE = "ec2-vpc";+ public static final String LABEL_VPC_ID = "floci_vpc_id";+ public static final String LABEL_VPC_REGION = "floci_vpc_region";+ /**+ * The API port of the Floci process that created the network. Several Floci instances+ * routinely share one Docker daemon, and reconciliation deletes things; scoping it by+ * owner is what stops one instance's startup from tearing down another's live networks.+ */+ public static final String LABEL_OWNER_PORT = "floci_vpc_owner_port";++ /**+ * Instance addresses start here inside each subnet slice. AWS reserves the first four+ * addresses of a subnet and Docker takes the first as the gateway, so starting at .10+ * clears both without arithmetic that has to stay in sync with either.+ *+ * <p>Ten addresses of headroom is cheap in a /24 and impossible in a /29, which holds eight+ * addresses in total. {@link #firstHostOffset} scales the offset down for such blocks rather+ * than leaving them with nothing at all to hand out.+ */+ static final int FIRST_HOST_OFFSET = 10;++ /**+ * The smallest offset any block can start allocating at: past the network address, and past+ * the address Docker takes for the gateway.+ */+ static final int MIN_HOST_OFFSET = 2;++ /**+ * The longest prefix this manager will ever allocate a block at. A /28 is the smallest block+ * AWS itself accepts as a subnet, and about the smallest that is worth handing to a VPC:+ * past it the network, gateway and broadcast addresses are most of the block.+ */+ static final int MAX_ALLOCATABLE_PREFIX = 28;++ private final EmulatorConfig config;+ private final DockerClient dockerClient;++ /** region::vpcId -> binding. Rebuilt from persisted VPCs at startup. */+ private final Map<String, VpcBinding> bindings = new ConcurrentHashMap<>();+ /** region::subnetId -> region::vpcId, so subnet-keyed calls need no VPC lookup. */+ private final Map<String, String> subnetOwner = new ConcurrentHashMap<>();++ private final Object planLock = new Object();++ /** So a misconfigured fallback prefix is reported once, not once per VPC. */+ private final java.util.concurrent.atomic.AtomicBoolean warnedPrefixClamp =+ new java.util.concurrent.atomic.AtomicBoolean();++ private final ScheduledExecutorService retries = Executors.newSingleThreadScheduledExecutor(r -> {+ Thread t = new Thread(r, "ec2-vpc-network-teardown");+ t.setDaemon(true);+ return t;+ });++ @Inject+ public VpcNetworkManager(EmulatorConfig config, DockerClient dockerClient) {+ this.config = config;+ this.dockerClient = dockerClient;+ }++ @PreDestroy+ void shutdown() {+ retries.shutdownNow();+ }++ public boolean enabled() {+ return config.services().ec2().vpcNetworks().enabled() && !config.services().ec2().mock();+ }++ // ─── Declaration: CreateVpc / CreateSubnet ────────────────────────────────++ /**+ * Records the address plan for a VPC. Called from CreateVpc and from default-resource+ * seeding; the Docker network itself is only created when the first instance needs it+ * (see {@link #attach}), because most roots in this corpus create VPCs they never launch+ * anything into and a Linux bridge per unused VPC is pure daemon churn.+ */+ public void declareVpc(String region, String vpcId, String declaredCidr) {+ if (!enabled() || region == null || vpcId == null) {+ return;+ }+ synchronized (planLock) {+ // Deliberately not computeIfAbsent: planVpc reads the whole binding map to detect+ // collisions, and ConcurrentHashMap forbids that from inside a mapping function.+ String vpcKey = key(region, vpcId);+ if (!bindings.containsKey(vpcKey)) {+ bindings.put(vpcKey, planVpc(region, vpcId, declaredCidr));+ }+ }+ }++ /** Records the address plan for a subnet inside an already-declared VPC. */+ public void declareSubnet(String region, String vpcId, String subnetId, String declaredCidr) {+ if (!enabled() || region == null || vpcId == null || subnetId == null) {+ return;+ }+ synchronized (planLock) {+ VpcBinding vpc = bindings.get(key(region, vpcId));+ if (vpc == null) {+ return;+ }+ if (!vpc.subnets.containsKey(subnetId)) {+ vpc.subnets.put(subnetId, planSubnet(vpc, subnetId, declaredCidr));+ }+ subnetOwner.put(key(region, subnetId), key(region, vpcId));+ }+ }++ private VpcBinding planVpc(String region, String vpcId, String declaredCidr) {+ Optional<Cidr4> declared = Cidr4.parse(declaredCidr);+ String rejection = rejectionReason(vpcId, declared, declaredCidr, List.of());+ Cidr4 effective;+ if (rejection == null) {+ effective = declared.orElseThrow();+ }+ else {+ effective = allocateFromFallbackPool(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 "+ + "synthesised private addresses that nothing can connect to.",+ vpcId, region, String.valueOf(declaredCidr), rejection,+ config.services().ec2().vpcNetworks().fallbackPool());+ }+ else {+ LOG.warnv("VPC {0} in {1}: declared CIDR {2} is unusable ({3}). Substituting {4} from the "+ + "fallback pool. Reported private IPs for this VPC will NOT match the "+ + "declared CIDR.",+ vpcId, region, String.valueOf(declaredCidr), rejection, effective);+ }+ }+ return new VpcBinding(region, vpcId, declared.orElse(null), effective, rejection != null,+ networkName(region, vpcId));+ }++ /**+ * @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<Cidr4> declared, String declaredText,+ Collection<Cidr4> extraTaken) {+ if (declared.isEmpty()) {+ return declaredText == null || declaredText.isBlank()+ ? "no CIDR was declared" : "not a parseable IPv4 CIDR";+ }+ Cidr4 cidr = declared.get();+ if (!cidr.isRfc1918()) {+ return "outside RFC 1918 private space (10/8, 172.16/12, 192.168/16)";+ }+ Optional<String> clash = firstClash(vpcId, cidr, extraTaken);+ return clash.orElse(null);+ }++ private Optional<String> firstClash(String vpcId, Cidr4 candidate, Collection<Cidr4> extraTaken) {+ for (Cidr4 taken : extraTaken) {+ if (candidate.overlaps(taken)) {+ return Optional.of("overlaps an address range already in use (" + taken + ")");+ }+ }+ for (VpcBinding other : bindings.values()) {+ if (other.effective != null && candidate.overlaps(other.effective)) {+ return Optional.of("already used by VPC " + other.vpcId + " (" + other.effective+ + "): two VPCs may declare the same CIDR in AWS, but one Docker daemon "+ + "cannot route two identical ranges");+ }+ }+ for (Cidr4 taken : dockerNetworkSubnets(vpcId)) {+ if (candidate.overlaps(taken)) {+ return Optional.of("overlaps an existing Docker network (" + taken + ")");+ }+ }+ return Optional.empty();+ }++ private SubnetBinding planSubnet(VpcBinding vpc, String subnetId, String declaredCidr) {+ Optional<Cidr4> declared = Cidr4.parse(declaredCidr);+ List<Cidr4> siblings = vpc.subnets.values().stream().map(s -> s.effective).filter(c -> c != null).toList();++ if (vpc.effective == null) {+ return new SubnetBinding(subnetId, declared.orElse(null), null, true);+ }+ // The common, quiet path: the VPC kept its declared CIDR and the subnet sits inside it.+ if (!vpc.substituted && declared.isPresent()+ && vpc.effective.contains(declared.get())+ && siblings.stream().noneMatch(s -> s.overlaps(declared.get()))) {+ return bindSubnet(vpc, subnetId, declared.get(), declared.get(), false);+ }++ int desiredPrefix = declared.map(Cidr4::prefix).orElse(24);+ Cidr4 slice = allocateSubBlock(vpc.effective, desiredPrefix, siblings);+ if (slice == null) {+ LOG.warnv("Subnet {0} in VPC {1}: no free /{2} slice remains inside {3}; instances in this "+ + "subnet fall back to synthesised private addresses.",+ subnetId, vpc.vpcId, String.valueOf(desiredPrefix), vpc.effective);+ return new SubnetBinding(subnetId, declared.orElse(null), null, true);+ }+ LOG.warnv("Subnet {0} in VPC {1}: declared CIDR {2} cannot be used ({3}). Substituting {4}: "+ + "reported private IPs for this subnet will NOT match the declared CIDR.",+ subnetId, vpc.vpcId, String.valueOf(declaredCidr),+ vpc.substituted ? "its VPC's CIDR was itself substituted for " + vpc.effective+ : "it does not sit inside the VPC range " + vpc.effective + " or collides with a sibling subnet",+ slice);+ return bindSubnet(vpc, subnetId, declared.orElse(null), slice, true);+ }++ /**+ * Binds a subnet to a range it can actually allocate out of.+ *+ * <p>A block too small to hold one host address is refused here rather than accepted and then+ * quietly allocating nothing: a subnet with no Docker-backed range is an ordinary, handled+ * state (its instances keep synthesised addresses), whereas a subnet that claims a range and+ * then returns empty from every {@link #allocatePrivateIp} looks like exhaustion and is not.+ */+ private SubnetBinding bindSubnet(VpcBinding vpc, String subnetId, Cidr4 declared,+ Cidr4 effective, boolean substituted) {+ if (effective != null && usableAddressCount(effective) == 0) {+ LOG.warnv("Subnet {0} in VPC {1}: {2} is too small to hold a usable address ({3} address(es) "+ + "in total, of which the network address, the Docker gateway and the "+ + "broadcast address are already spoken for). Instances in this subnet fall "+ + "back to synthesised private addresses.",+ subnetId, vpc.vpcId, effective, String.valueOf(effective.size()));+ return new SubnetBinding(subnetId, declared, null, true);+ }+ return new SubnetBinding(subnetId, declared, effective, substituted);+ }++ /**+ * Where allocation starts inside {@code block}: the usual {@link #FIRST_HOST_OFFSET} of+ * headroom when the block can spare it, and {@link #MIN_HOST_OFFSET} when it cannot.+ */+ static long firstHostOffset(Cidr4 block) {+ return block.size() >= FIRST_HOST_OFFSET + 2 ? FIRST_HOST_OFFSET : MIN_HOST_OFFSET;+ }++ /** How many addresses {@code block} can hand to instances, once reserved ones are removed. */+ static long usableAddressCount(Cidr4 block) {+ return Math.max(0, block.size() - 1 - firstHostOffset(block));+ }++ /** First block of {@code desiredPrefix} inside {@code parent} that no member of {@code taken} overlaps. */+ static Cidr4 allocateSubBlock(Cidr4 parent, int desiredPrefix, Collection<Cidr4> taken) {+ int prefix = Math.max(desiredPrefix, parent.prefix());+ Cidr4 candidate = new Cidr4(parent.network(), prefix);+ long blocks = 1L << (prefix - parent.prefix());+ for (long i = 0; i < blocks; i++) {+ Cidr4 block = candidate.shifted(i);+ boolean clash = taken.stream().anyMatch(block::overlaps);+ if (!clash) {+ return block;+ }+ }+ return null;+ }++ private Cidr4 allocateFromFallbackPool(String vpcId, Integer declaredPrefix) {+ EmulatorConfig.VpcNetworksConfig cfg = config.services().ec2().vpcNetworks();+ Optional<Cidr4> pool = Cidr4.parse(cfg.fallbackPool());+ if (pool.isEmpty() || !pool.get().isRfc1918()) {+ LOG.errorv("floci.services.ec2.vpc-networks.fallback-pool ({0}) is not a valid RFC 1918 CIDR; "+ + "no substitution is possible.", cfg.fallbackPool());+ return null;+ }+ int prefix = clampAllocatablePrefix(cfg.fallbackPrefixLength(), pool.get());+ if (declaredPrefix != null && declaredPrefix > prefix) {+ // A VPC that declared a /24 gets a /24: the substitute should be the size that was+ // asked for, not a /16 that wastes fifteen sixteenths of the pool per VPC.+ prefix = Math.min(declaredPrefix, MAX_ALLOCATABLE_PREFIX);+ }+ List<Cidr4> taken = new ArrayList<>(dockerNetworkSubnets(vpcId));+ bindings.values().stream().map(b -> b.effective).filter(c -> c != null).forEach(taken::add);+ return allocateSubBlock(pool.get(), prefix, taken);+ }++ /**+ * {@code floci.services.ec2.vpc-networks.fallback-prefix-length} is a plain int with no bounds+ * declared on it, and both ends of its range are useless in different ways: shorter than the+ * pool's own prefix asks for a block the pool does not contain, and longer than+ * {@link #MAX_ALLOCATABLE_PREFIX} asks for a block with no room for a host, which used to mean+ * every substituted VPC allocated exactly nothing and said nothing about it.+ *+ * <p>Neither is worth refusing to start over, since the whole fallback path is already a+ * degraded one; the value is clamped into the range that can work, and the clamp is logged.+ */+ private int clampAllocatablePrefix(int configured, Cidr4 pool) {+ int clamped = Math.min(Math.max(configured, pool.prefix()), MAX_ALLOCATABLE_PREFIX);+ if (clamped != configured && warnedPrefixClamp.compareAndSet(false, true)) {+ LOG.warnv("floci.services.ec2.vpc-networks.fallback-prefix-length is /{0}, which cannot serve "+ + "instance addresses out of the pool {1}. Using /{2} instead; valid values run "+ + "from the pool''s own prefix (/{3}) to /{4}.",+ String.valueOf(configured), pool, String.valueOf(clamped),+ String.valueOf(pool.prefix()), String.valueOf(MAX_ALLOCATABLE_PREFIX));+ }+ return clamped;+ }++ // ─── Address allocation ──────────────────────────────────────────────────++ /**+ * The next free address inside the subnet's effective range.+ *+ * @return the address, or empty when the subnet has no Docker-backed range, in which case+ * the caller keeps its existing synthesised address rather than reporting nothing.+ */+ public Optional<String> allocatePrivateIp(String region, String subnetId) {+ if (!enabled() || subnetId == null) {+ return Optional.empty();+ }+ SubnetBinding subnet = subnetBinding(region, subnetId);+ if (subnet == null || subnet.effective == null) {+ return Optional.empty();+ }+ synchronized (subnet) {+ long limit = subnet.effective.size() - 1;+ // Sweep forward from the last hand-out first, so a fresh subnet allocates in the+ // readable order .10, .11, .12; only once the range is walked does it come back for+ // addresses released by terminated instances, which keeps a long-running survey from+ // exhausting a /24 after 245 launches.+ for (long pass = 0; pass < 2; pass++) {+ long from = pass == 0 ? subnet.nextOffset : subnet.firstOffset;+ long to = pass == 0 ? limit : Math.min(subnet.nextOffset, limit);+ for (long offset = from; offset < to; offset++) {+ Optional<String> address = subnet.effective.addressAt(offset);+ if (address.isPresent() && !subnet.leased.contains(address.get())) {+ subnet.leased.add(address.get());+ subnet.nextOffset = offset + 1;+ return address;+ }+ }+ }+ }+ LOG.warnv("Subnet {0} range {1} is exhausted; further instances get no Docker-backed address.",+ subnetId, subnet.effective);+ return Optional.empty();+ }++ /**+ * Claims one specific address in a subnet's range, rather than picking the next free one.+ *+ * <p>This exists for restart. The lease table lives only in memory, so a restarted emulator+ * rebuilds it empty while the containers of persisted, still-running instances go on holding+ * the addresses a previous run handed them. Re-reserving those addresses is what stops the+ * next {@link #allocatePrivateIp} from handing a live container's address to a new instance.+ *+ * <p>Refusal is normal and never exceptional: a persisted instance may sit in a subnet whose+ * effective range has since been substituted, or two persisted instances may claim the same+ * address. Both return false and leave the caller's other reservations intact: the+ * alternative is a restart that aborts on inherited state it cannot change.+ *+ * @return true when the address is now leased; false when the subnet has no Docker-backed+ * range, the address is unparseable or outside that range, or it is already leased+ */+ public boolean reservePrivateIp(String region, String subnetId, String address) {+ if (!enabled() || subnetId == null || address == null) {+ return false;+ }+ SubnetBinding subnet = subnetBinding(region, subnetId);+ if (subnet == null || subnet.effective == null) {+ return false;+ }+ Optional<Cidr4> parsed = Cidr4.parse(address + "/32");+ if (parsed.isEmpty() || !subnet.effective.containsAddress(parsed.get().network())) {+ LOG.warnv("Cannot reserve {0} in subnet {1}: it is outside the subnet''s effective range {2}. "+ + "Its holder keeps it, and this manager will not hand it out either, since "+ + "it is not an address it allocates from.",+ String.valueOf(address), subnetId, String.valueOf(subnet.effective));+ return false;+ }+ synchronized (subnet) {+ if (!subnet.leased.add(address)) {+ LOG.warnv("Address {0} in subnet {1} is claimed by more than one instance; the later "+ + "claim is ignored.", address, subnetId);+ return false;+ }+ long offset = parsed.get().network() - subnet.effective.network();+ if (offset >= subnet.nextOffset) {+ subnet.nextOffset = offset + 1;+ }+ return true;+ }+ }++ public void releasePrivateIp(String region, String subnetId, String address) {+ SubnetBinding subnet = subnetBinding(region, subnetId);+ if (subnet != null && address != null) {+ subnet.leased.remove(address);+ }+ }++ // ─── Materialisation: attaching a container ──────────────────────────────++ /**+ * Creates the VPC's Docker network if it does not exist yet, and attaches the container to+ * it at {@code address}.+ *+ * <p>The container keeps its default bridge attachment as well. That is deliberate: EC2+ * containers publish SSH on a host port, and on macOS Docker Desktop setting a network mode+ * alongside port bindings suppresses publishing entirely (see ContainerLifecycleManager).+ * Attaching as a second network keeps published ports working while making the VPC address+ * the one Floci reports, and the one instances in the same VPC use to reach each other.+ *+ * @return the network name on success, empty when the instance stays on the bridge only+ */+ public Optional<String> attach(String region, String vpcId, String subnetId,+ String containerId, String address) {+ if (!enabled() || containerId == null || address == null) {+ return Optional.empty();+ }+ VpcBinding vpc = bindings.get(key(region, vpcId));+ if (vpc == null || vpc.effective == null) {+ return Optional.empty();+ }+ // Only attach an address this manager actually planned. A synthesised address from+ // Ec2Service's fallback would be outside the network's IPAM pool, and Docker would+ // reject it, noisily, and after the container is already up.+ SubnetBinding subnet = subnetBinding(region, subnetId);+ Optional<Cidr4> parsed = Cidr4.parse(address + "/32");+ if (subnet == null || subnet.effective == null || parsed.isEmpty()+ || !subnet.effective.containsAddress(parsed.get().network())) {+ return Optional.empty();+ }+ String networkName = materialise(vpc);+ if (networkName == null) {+ return Optional.empty();+ }+ try {+ dockerClient.connectToNetworkCmd()+ .withContainerId(containerId)+ .withNetworkId(networkName)+ .withContainerNetwork(new ContainerNetwork()+ .withIpamConfig(new ContainerNetwork.Ipam().withIpv4Address(address)))+ .exec();+ LOG.infov("Attached container {0} to VPC network {1} at {2}", containerId, networkName, address);+ return Optional.of(networkName);+ } catch (Exception e) {+ LOG.warnv("Could not attach container {0} to VPC network {1} at {2}: {3}. The instance keeps "+ + "its bridge address and its reported private IP will not be reachable.",+ containerId, networkName, address, e.getMessage());+ return Optional.empty();+ }+ }++ /**+ * Disconnects a container from its VPC network, for a launch that failed after {@link #attach}+ * succeeded. Docker holds the endpoint, and so the address, until the container is removed+ * or disconnected, so the lease cannot be returned to the pool before this runs: the next+ * launch would be handed the same address and Docker would refuse it.+ *+ * <p>A container that was never attached, or whose network is already gone, is not an error+ * here; both are the ordinary shape of a launch that failed early.+ */+ public void detach(String region, String vpcId, String containerId) {+ if (!enabled() || containerId == null) {+ return;+ }+ VpcBinding vpc = bindings.get(key(region, vpcId));+ if (vpc == null || !vpc.created) {+ return;+ }+ try {+ dockerClient.disconnectFromNetworkCmd()+ .withNetworkId(vpc.networkName)+ .withContainerId(containerId)+ .withForce(true)+ .exec();+ } catch (Exception e) {+ LOG.debugv("Could not disconnect {0} from VPC network {1}: {2}",+ containerId, vpc.networkName, e.getMessage());+ }+ }++ /** @return the network name, or null when creation failed */+ private String materialise(VpcBinding vpc) {+ synchronized (vpc) {+ if (vpc.created) {+ return vpc.networkName;+ }+ Network existing = null;+ try {+ existing = dockerClient.inspectNetworkCmd().withNetworkId(vpc.networkName).exec();+ } catch (NotFoundException notThereYet) {+ // expected: first instance in this VPC+ } catch (Exception e) {+ LOG.debugv("Could not inspect VPC network {0}: {1}", vpc.networkName, e.getMessage());+ }+ if (existing != null) {+ List<Cidr4> actual = ipamSubnets(existing);+ if (actual.contains(vpc.effective)) {+ vpc.created = true;+ return vpc.networkName;+ }+ // A leftover under this VPC's own name, routing a range this VPC does not plan+ // from. Adopting it would hand out addresses from vpc.effective on a bridge that+ // routes something else: the exact quiet lie this class exists to remove.+ //+ // It is removed and recreated rather than re-planned onto the leftover's own+ // subnet, for two reasons. Addresses have already been allocated from+ // vpc.effective by the time materialise runs (attach validates the address+ // against the subnet plan before calling it), and after a restart some of them+ // are leases held by live containers, so the plan cannot be moved out from under+ // them. And the name carries this emulator's port and region, so the network is+ // by construction Floci's own: recreating it cannot destroy a user's network.+ LOG.warnv("Docker network {0} for VPC {1} already exists with subnet {2}, but this VPC "+ + "allocates addresses from {3}. Removing the stale network and "+ + "recreating it on the planned range; anything still attached to it is "+ + "disconnected first.",+ vpc.networkName, vpc.vpcId,+ actual.isEmpty() ? "no readable IPAM configuration" : actual.toString(),+ vpc.effective);+ if (!removeStaleNetwork(existing, vpc)) {+ return null;+ }+ }+ try {+ dockerClient.createNetworkCmd()+ .withName(vpc.networkName)+ .withDriver(config.services().ec2().vpcNetworks().driver())+ .withIpam(new Network.Ipam().withConfig(+ new Network.Ipam.Config().withSubnet(vpc.effective.toString())))+ .withLabels(networkLabels(vpc))+ .exec();+ vpc.created = true;+ LOG.infov("Created Docker network {0} for VPC {1} ({2}){3}",+ vpc.networkName, vpc.vpcId, vpc.effective,+ vpc.substituted ? " [substituted; declared " + vpc.declared + "]" : "");+ return vpc.networkName;+ } catch (Exception e) {+ LOG.warnv("Could not create Docker network {0} for VPC {1} ({2}): {3}. Instances in this VPC "+ + "stay on the shared bridge and are neither isolated nor addressed from the "+ + "declared CIDR.",+ vpc.networkName, vpc.vpcId, vpc.effective, e.getMessage());+ return null;+ }+ }+ }++ /**+ * @return false when the stale network is still there afterwards, in which case the caller+ * must not adopt it: an instance on the shared bridge with an unreachable private IP+ * is a documented degradation, whereas an instance holding an address the network+ * does not route is a wrong answer.+ */+ private boolean removeStaleNetwork(Network existing, VpcBinding vpc) {+ String id = existing.getId() == null ? vpc.networkName : existing.getId();+ try {+ disconnectAll(existing);+ dockerClient.removeNetworkCmd(id).exec();+ return true;+ } catch (NotFoundException alreadyGone) {+ return true;+ } catch (Exception e) {+ LOG.warnv("Could not remove the stale Docker network {0} for VPC {1}: {2}. Instances in this "+ + "VPC stay on the shared bridge rather than being given addresses the "+ + "network does not route.",+ vpc.networkName, vpc.vpcId, e.getMessage());+ return false;+ }+ }++ /** Every IPv4 range the network's IPAM configuration declares, in declaration order. */+ private static List<Cidr4> ipamSubnets(Network network) {+ if (network.getIpam() == null || network.getIpam().getConfig() == null) {+ return List.of();+ }+ List<Cidr4> subnets = new ArrayList<>();+ for (Network.Ipam.Config ipam : network.getIpam().getConfig()) {+ Cidr4.parse(ipam.getSubnet()).ifPresent(subnets::add);+ }+ return subnets;+ }++ private Map<String, String> networkLabels(VpcBinding vpc) {+ Map<String, String> labels = new LinkedHashMap<>(ContainerStorageHelper.defaultLabels(config));+ labels.put(LABEL_COMPONENT, COMPONENT_VALUE);+ labels.put(LABEL_VPC_ID, vpc.vpcId);+ labels.put(LABEL_VPC_REGION, vpc.region);+ labels.put(LABEL_OWNER_PORT, String.valueOf(config.port()));+ return labels;+ }++ // ─── Teardown ────────────────────────────────────────────────────────────++ public void forgetSubnet(String region, String subnetId) {+ String vpcKey = subnetOwner.remove(key(region, subnetId));+ if (vpcKey == null) {+ return;+ }+ VpcBinding vpc = bindings.get(vpcKey);+ if (vpc != null) {+ vpc.subnets.remove(subnetId);+ }+ }++ /** Drops the VPC's plan and removes its Docker network, retrying while endpoints drain. */+ public void deleteVpcNetwork(String region, String vpcId) {+ VpcBinding vpc = bindings.remove(key(region, vpcId));+ if (vpc == null) {+ return;+ }+ vpc.subnets.keySet().forEach(subnetId -> subnetOwner.remove(key(region, subnetId)));+ if (!vpc.created) {+ return;+ }+ removeNetworkWithRetry(vpc.networkName, 1);+ }++ private void removeNetworkWithRetry(String networkName, int attempt) {+ try {+ dockerClient.removeNetworkCmd(networkName).exec();+ LOG.infov("Removed Docker network {0}", networkName);+ } catch (NotFoundException alreadyGone) {+ // nothing to do+ } catch (Exception e) {+ // Containers terminated moments earlier still hold endpoints for a beat.+ if (attempt < 5 && !retries.isShutdown()) {+ retries.schedule(() -> removeNetworkWithRetry(networkName, attempt + 1), 2, TimeUnit.SECONDS);+ return;+ }+ LOG.warnv("Could not remove Docker network {0} after {1} attempts: {2}. It will be reconciled "+ + "at the next startup.", networkName, String.valueOf(attempt), e.getMessage());+ }+ }++ // ─── Startup reconciliation ──────────────────────────────────────────────++ /**+ * Removes VPC networks this Floci left behind on a previous run.+ *+ * <p>A crashed or SIGKILLed run leaves its bridges and its IPAM reservations on the daemon,+ * and the next run's identical CIDRs then collide with its own corpses: a repeat failure+ * mode in this project, and one that looks like a genuine collision, so it would silently+ * push every VPC into the fallback pool.+ *+ * <p>Only networks labelled with this process's own API port are considered. Several Floci+ * instances share a daemon here; scoping by owner is what makes the deletion safe.+ *+ * @param stillDeclared answers whether (region, vpcId) exists in the restored state+ */+ public void reconcileOrphans(BiPredicate<String, String> stillDeclared) {+ if (!enabled() || !config.services().ec2().vpcNetworks().reconcileOnStartup()) {+ return;+ }+ String owner = String.valueOf(config.port());+ int removed = 0;+ try {+ for (Network network : dockerClient.listNetworksCmd()+ .withFilter("label", List.of(LABEL_COMPONENT + "=" + COMPONENT_VALUE))+ .exec()) {+ Map<String, String> labels = network.getLabels() == null ? Map.of() : network.getLabels();+ if (!owner.equals(labels.get(LABEL_OWNER_PORT))) {+ continue;+ }+ String vpcId = labels.get(LABEL_VPC_ID);+ String region = labels.get(LABEL_VPC_REGION);+ if (vpcId != null && region != null && stillDeclared.test(region, vpcId)) {+ continue;+ }+ disconnectAll(network);+ try {+ dockerClient.removeNetworkCmd(network.getId()).exec();+ removed++;+ LOG.infov("Reconciled orphaned VPC network {0} (vpc {1}) left by a previous run",+ network.getName(), String.valueOf(vpcId));+ } catch (Exception e) {+ LOG.warnv("Could not remove orphaned VPC network {0}: {1}", network.getName(), e.getMessage());+ }+ }+ } catch (Exception e) {+ LOG.warnv("Could not reconcile orphaned VPC networks: {0}", e.getMessage());+ }+ if (removed > 0) {+ LOG.infov("Removed {0} orphaned VPC network(s)", String.valueOf(removed));+ }+ }++ /**+ * Detaches whatever is still on the network so it can be removed.+ *+ * <p>The container map has to be re-read with an inspect: Docker's network <em>list</em>+ * endpoint leaves it empty, so a crashed run's containers, the exact case this exists for,+ * are invisible in the listing and the removal then fails with "has active endpoints".+ */+ private void disconnectAll(Network listed) {+ Network network;+ try {+ network = dockerClient.inspectNetworkCmd().withNetworkId(listed.getId()).exec();+ } catch (Exception e) {+ LOG.debugv("Could not inspect {0} before removal: {1}", listed.getName(), e.getMessage());+ return;+ }+ Map<String, Network.ContainerNetworkConfig> containers = network.getContainers();+ if (containers == null) {+ return;+ }+ for (String containerId : containers.keySet()) {+ try {+ dockerClient.disconnectFromNetworkCmd()+ .withNetworkId(listed.getId())+ .withContainerId(containerId)+ .withForce(true)+ .exec();+ } catch (Exception e) {+ LOG.debugv("Could not disconnect {0} from {1}: {2}", containerId, listed.getName(), e.getMessage());+ }+ }+ }++ // ─── Introspection, for tests and reporting ──────────────────────────────++ public Optional<String> effectiveVpcCidr(String region, String vpcId) {+ VpcBinding vpc = bindings.get(key(region, vpcId));+ return vpc == null || vpc.effective == null ? Optional.empty() : Optional.of(vpc.effective.toString());+ }++ public Optional<String> effectiveSubnetCidr(String region, String subnetId) {+ SubnetBinding subnet = subnetBinding(region, subnetId);+ return subnet == null || subnet.effective == null+ ? Optional.empty() : Optional.of(subnet.effective.toString());+ }++ public Optional<String> networkNameFor(String region, String vpcId) {+ VpcBinding vpc = bindings.get(key(region, vpcId));+ return vpc == null ? Optional.empty() : Optional.of(vpc.networkName);+ }++ public boolean isSubstituted(String region, String vpcId) {+ VpcBinding vpc = bindings.get(key(region, vpcId));+ return vpc != null && vpc.substituted;+ }++ String networkName(String region, String vpcId) {+ return ContainerStorageHelper.dockerName(config,+ "floci-vpc-" + config.port() + "-" + region + "-" + vpcId);+ }++ private SubnetBinding subnetBinding(String region, String subnetId) {+ String vpcKey = subnetOwner.get(key(region, subnetId));+ if (vpcKey == null) {+ return null;+ }+ VpcBinding vpc = bindings.get(vpcKey);+ return vpc == null ? null : vpc.subnets.get(subnetId);+ }++ /**+ * Every IPv4 range the daemon has already reserved, so a declared CIDR can be checked before use.+ *+ * @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+ */+ private Set<Cidr4> dockerNetworkSubnets(String ownVpcId) {+ Set<Cidr4> taken = new java.util.LinkedHashSet<>();+ String owner = String.valueOf(config.port());+ try {+ for (Network network : dockerClient.listNetworksCmd().exec()) {+ Map<String, String> labels = network.getLabels() == null ? Map.of() : network.getLabels();+ if (ownVpcId != null && ownVpcId.equals(labels.get(LABEL_VPC_ID))+ && owner.equals(labels.get(LABEL_OWNER_PORT))) {+ continue;+ }+ if (network.getIpam() == null || network.getIpam().getConfig() == null) {+ continue;+ }+ for (Network.Ipam.Config ipam : network.getIpam().getConfig()) {+ Cidr4.parse(ipam.getSubnet()).ifPresent(taken::add);+ }+ }+ } catch (Exception e) {+ // A daemon that cannot be listed cannot be collided with either; proceeding with the+ // declared CIDR is the honest choice, and createNetwork still refuses a real overlap.+ LOG.debugv("Could not list Docker networks for CIDR collision check: {0}", e.getMessage());+ }+ return taken;+ }++ private static String key(String region, String id) {+ return region + "::" + id;+ }++ private static final class VpcBinding {+ final String region;+ final String vpcId;+ final Cidr4 declared;+ final Cidr4 effective;+ final boolean substituted;+ final String networkName;+ final Map<String, SubnetBinding> subnets = new ConcurrentHashMap<>();+ volatile boolean created;++ VpcBinding(String region, String vpcId, Cidr4 declared, Cidr4 effective,+ boolean substituted, String networkName) {+ this.region = region;+ this.vpcId = vpcId;+ this.declared = declared;+ this.effective = effective;+ this.substituted = substituted;+ this.networkName = networkName;+ }+ }++ private static final class SubnetBinding {+ final String subnetId;+ final Cidr4 declared;+ final Cidr4 effective;+ final boolean substituted;+ final Set<String> leased = ConcurrentHashMap.newKeySet();+ /** Scaled to the block: a /29 cannot spare the ten addresses a /24 hands over freely. */+ final long firstOffset;+ long nextOffset;++ SubnetBinding(String subnetId, Cidr4 declared, Cidr4 effective, boolean substituted) {+ this.subnetId = subnetId;+ this.declared = declared;+ this.effective = effective;+ this.substituted = substituted;+ this.firstOffset = effective == null ? FIRST_HOST_OFFSET : firstHostOffset(effective);+ this.nextOffset = this.firstOffset;+ }+ }+}diff --git a/src/main/resources/application.yml b/src/main/resources/application.ymlindex a16ce601b7..fe9d9f5c55 100644--- a/src/main/resources/application.yml+++ b/src/main/resources/application.yml@@ -465,6 +465,19 @@ floci:# the container network). Set explicitly when the probe# can't speak for your clients, e.g. Floci itself running# containerized. FLOCI_SERVICES_EC2_CONTAINER_IPS_ROUTABLE+ # Each VPC gets a real Docker network, so an instance's private IP is an address its+ # container actually holds and instances in different VPCs are on different bridges.+ vpc-networks:+ enabled: true # FLOCI_SERVICES_EC2_VPC_NETWORKS_ENABLED+ # Used only when a declared VPC CIDR cannot be honoured: absent, malformed, outside+ # RFC 1918, or already claimed on this Docker daemon. Substitutions are logged at WARN;+ # a reported private IP that does not match the declared CIDR is never silent.+ fallback-pool: 10.240.0.0/12 # FLOCI_SERVICES_EC2_VPC_NETWORKS_FALLBACK_POOL+ fallback-prefix-length: 16 # FLOCI_SERVICES_EC2_VPC_NETWORKS_FALLBACK_PREFIX_LENGTH+ # Remove VPC networks left behind by a previous run of THIS emulator (scoped by API port,+ # so Floci instances sharing a daemon never reconcile each other's networks).+ reconcile-on-startup: true # FLOCI_SERVICES_EC2_VPC_NETWORKS_RECONCILE_ON_STARTUP+ driver: bridge # FLOCI_SERVICES_EC2_VPC_NETWORKS_DRIVERecs:enabled: truemock: falsediff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManagerTest.javaindex cde168843d..1f977bdc1a 100644--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManagerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManagerTest.java@@ -11,6 +11,8 @@ import com.github.dockerjava.api.command.InspectContainerResponse;import com.github.dockerjava.api.command.InspectExecCmd;import com.github.dockerjava.api.command.InspectExecResponse;import com.github.dockerjava.api.command.ListContainersCmd;+import com.github.dockerjava.api.command.StartContainerCmd;+import com.github.dockerjava.api.exception.DockerException;import com.github.dockerjava.api.model.Container;import com.github.dockerjava.api.model.ContainerNetwork;import com.github.dockerjava.api.model.Frame;@@ -18,6 +20,7 @@ import com.github.dockerjava.api.model.StreamType;import java.nio.charset.StandardCharsets;import com.github.dockerjava.api.model.NetworkSettings;import io.github.hectorvent.floci.config.EmulatorConfig;+import io.github.hectorvent.floci.services.ec2.net.VpcNetworkManager;import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager;import io.github.hectorvent.floci.core.common.RegionResolver;import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;@@ -65,6 +68,7 @@ import static org.mockito.Answers.RETURNS_DEEP_STUBS;import static org.mockito.Mockito.after;import static org.mockito.Mockito.doThrow;import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.atLeastOnce;import static org.mockito.Mockito.never;import static org.mockito.Mockito.timeout;import static org.mockito.Mockito.times;@@ -153,7 +157,8 @@ class Ec2ContainerManagerTest {metadataServer,mock(Ec2PortForwardManager.class),mock(RegionResolver.class),- mock(ContainerNetworkReachability.class));+ mock(ContainerNetworkReachability.class),+ mock(VpcNetworkManager.class));Instance instance = new Instance();instance.setInstanceId("i-restored");@@ -168,6 +173,154 @@ class Ec2ContainerManagerTest {verify(metadataServer).registerContainer("192.168.215.42", "i-restored", instance);}+ @Test+ void restoreMetadataRegistrationAlsoRegistersTheAddressImdsRequestsArriveFrom() {+ ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);+ when(lifecycleManager.isContainerRunning(TEST_CONTAINER_ID)).thenReturn(true);++ DockerClient dockerClient = mock(DockerClient.class);+ InspectContainerResponse response = vpcAttachedInspectResponse("10.0.1.10", "172.17.0.4");+ InspectContainerCmd inspect = mock(InspectContainerCmd.class);+ when(dockerClient.inspectContainerCmd(TEST_CONTAINER_ID)).thenReturn(inspect);+ when(inspect.exec()).thenReturn(response);++ Ec2MetadataServer metadataServer = mock(Ec2MetadataServer.class);+ Ec2ContainerManager manager = managerWith(lifecycleManager, dockerClient, metadataServer);++ Instance instance = new Instance();+ instance.setInstanceId("i-vpc-restored");+ instance.setDockerContainerId(TEST_CONTAINER_ID);+ instance.setContainerBridgeIp("10.0.1.10");+ instance.setImdsSourceIp("172.17.0.2");++ assertTrue(manager.restoreMetadataRegistration(instance));++ // The VPC address is what Floci reports, but the container's default route is the bridge,+ // so that is the source address Ec2MetadataServer resolves the instance by.+ verify(metadataServer).registerContainer("10.0.1.10", "i-vpc-restored", instance);+ verify(metadataServer).registerContainer("172.17.0.4", "i-vpc-restored", instance);+ verify(metadataServer).unregisterContainer("172.17.0.2", instance);+ assertEquals("172.17.0.4", instance.getImdsSourceIp());+ }++ @Test+ void restoreMetadataRegistrationLeavesABridgeOnlyInstanceWithNoSeparateImdsSource() {+ ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);+ when(lifecycleManager.isContainerRunning(TEST_CONTAINER_ID)).thenReturn(true);++ DockerClient dockerClient = mock(DockerClient.class);+ InspectContainerResponse response = inspectResponse("172.17.0.4");+ InspectContainerCmd inspect = mock(InspectContainerCmd.class);+ when(dockerClient.inspectContainerCmd(TEST_CONTAINER_ID)).thenReturn(inspect);+ when(inspect.exec()).thenReturn(response);++ Ec2MetadataServer metadataServer = mock(Ec2MetadataServer.class);+ Ec2ContainerManager manager = managerWith(lifecycleManager, dockerClient, metadataServer);++ Instance instance = new Instance();+ instance.setInstanceId("i-bridge-only");+ instance.setDockerContainerId(TEST_CONTAINER_ID);+ instance.setContainerBridgeIp("172.17.0.4");++ assertTrue(manager.restoreMetadataRegistration(instance));++ assertNull(instance.getImdsSourceIp(), "one address means one registration");+ verify(metadataServer).registerContainer("172.17.0.4", "i-bridge-only", instance);+ verify(metadataServer, never()).unregisterContainer(anyString(), any(Instance.class));+ }++ @Test+ void restoreMetadataRegistrationKeepsTheImdsSourceWhenTheContainerCannotBeInspected() {+ ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);+ when(lifecycleManager.isContainerRunning(TEST_CONTAINER_ID)).thenReturn(true);++ DockerClient dockerClient = mock(DockerClient.class);+ InspectContainerCmd inspect = mock(InspectContainerCmd.class);+ when(dockerClient.inspectContainerCmd(TEST_CONTAINER_ID)).thenReturn(inspect);+ when(inspect.exec()).thenThrow(new DockerException("dial unix /var/run/docker.sock: EOF", 500));++ Ec2MetadataServer metadataServer = mock(Ec2MetadataServer.class);+ Ec2ContainerManager manager = managerWith(lifecycleManager, dockerClient, metadataServer);++ Instance instance = new Instance();+ instance.setInstanceId("i-inspect-failed");+ instance.setDockerContainerId(TEST_CONTAINER_ID);+ instance.setContainerBridgeIp("10.0.1.10");+ instance.setImdsSourceIp("172.17.0.4");++ assertTrue(manager.restoreMetadataRegistration(instance));++ // A failed inspect leaves the bridge attachment unknown, not known to be absent. Tearing+ // the registration down here would stop this healthy instance's metadata requests+ // resolving, with nothing registered in place of what was removed.+ verify(metadataServer, never()).unregisterContainer("172.17.0.4", instance);+ assertEquals("172.17.0.4", instance.getImdsSourceIp(),+ "a transient Docker failure must not drop an existing IMDS source registration");+ }++ @Test+ void startRegistersTheNewBridgeAddressDockerHandsOutOnRestart() throws Exception {+ ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);+ when(lifecycleManager.isContainerRunning(TEST_CONTAINER_ID)).thenReturn(true);++ DockerClient dockerClient = mock(DockerClient.class);+ when(dockerClient.startContainerCmd(TEST_CONTAINER_ID))+ .thenReturn(mock(StartContainerCmd.class, RETURNS_SELF));+ // The VPC address is static, so it survives the stop; the bridge address does not.+ InspectContainerResponse response = vpcAttachedInspectResponse("10.0.1.10", "172.17.0.9");+ InspectContainerCmd inspect = mock(InspectContainerCmd.class);+ when(dockerClient.inspectContainerCmd(TEST_CONTAINER_ID)).thenReturn(inspect);+ when(inspect.exec()).thenReturn(response);++ Ec2MetadataServer metadataServer = mock(Ec2MetadataServer.class);+ Ec2ContainerManager manager = managerWith(lifecycleManager, dockerClient, metadataServer);++ Instance instance = new Instance();+ instance.setInstanceId("i-vpc-started");+ instance.setDockerContainerId(TEST_CONTAINER_ID);+ instance.setContainerBridgeIp("10.0.1.10");+ instance.setImdsSourceIp("172.17.0.4");++ manager.start(instance);+ awaitUntil(() -> "running".equals(instance.getState().getName()), Duration.ofSeconds(5));++ verify(metadataServer, timeout(2000)).registerContainer("172.17.0.9", "i-vpc-started", instance);+ verify(metadataServer, timeout(2000)).unregisterContainer("172.17.0.4", instance);+ assertEquals("172.17.0.9", instance.getImdsSourceIp(),+ "the stale address would otherwise stay registered while IMDS requests arrive "+ + "from an address nothing knows about");+ }++ private static Ec2ContainerManager managerWith(ContainerLifecycleManager lifecycleManager,+ DockerClient dockerClient,+ Ec2MetadataServer metadataServer) {+ return new Ec2ContainerManager(+ mock(ContainerBuilder.class),+ lifecycleManager,+ mock(ContainerLogStreamer.class),+ mock(ContainerDetector.class),+ mock(DockerHostResolver.class),+ dockerClient,+ mock(PortAllocator.class),+ mock(EmulatorConfig.class, RETURNS_DEEP_STUBS),+ metadataServer,+ mock(Ec2PortForwardManager.class),+ mock(RegionResolver.class),+ mock(ContainerNetworkReachability.class),+ mock(VpcNetworkManager.class));+ }++ /** A container on both its VPC network and the default bridge, which is what launch leaves. */+ private static InspectContainerResponse vpcAttachedInspectResponse(String vpcIp, String bridgeIp) {+ InspectContainerResponse inspect = mock(InspectContainerResponse.class);+ NetworkSettings networkSettings = mock(NetworkSettings.class);+ when(inspect.getNetworkSettings()).thenReturn(networkSettings);+ when(networkSettings.getNetworks()).thenReturn(Map.of(+ "floci-vpc-4566-us-west-2-vpc-1", new ContainerNetwork().withIpv4Address(vpcIp),+ "bridge", new ContainerNetwork().withIpv4Address(bridgeIp)));+ return inspect;+ }+@Testvoid userDataExecutionCommandRunsScriptDirectlySoShebangIsHonored() {assertArrayEquals(new String[]{"/tmp/user-data.sh"}, Ec2ContainerManager.userDataExecutionCommand());@@ -418,7 +571,8 @@ class Ec2ContainerManagerTest {mock(Ec2MetadataServer.class),mock(Ec2PortForwardManager.class),mock(RegionResolver.class),- reachability);+ reachability,+ mock(VpcNetworkManager.class));}@Test@@ -438,6 +592,53 @@ class Ec2ContainerManagerTest {assertTrue(Ec2ContainerManager.userDataShellScripts(userData).isEmpty());}+ @Test+ void launchReturnsTheLeaseWhenTheContainerNeverJoinsTheVpcNetwork() throws Exception {+ Ec2ContainerManager.containerBridgeIpAttempts = 2;+ Ec2ContainerManager.containerBridgeIpPollMillis = 1;+ LaunchHarness harness = launchHarness();+ InspectContainerResponse noIp = inspectResponse(null);+ InspectContainerCmd inspect = mock(InspectContainerCmd.class);+ when(harness.dockerClient.inspectContainerCmd(TEST_CONTAINER_ID)).thenReturn(inspect);+ when(inspect.exec()).thenReturn(noIp);+ // attach() returns empty: nothing holds the address, and the instance is about to start+ // reporting its bridge address instead, so the lease is no longer findable from it.++ Instance instance = leasedInstance("i-noattach");++ harness.manager.launch(instance, "ubuntu:24.04", null, "us-west-2");++ awaitUntil(() -> "terminated".equals(instance.getState().getName()), Duration.ofSeconds(2));+ verify(harness.vpcNetworkManager, timeout(2000).atLeastOnce())+ .releasePrivateIp("us-west-2", "subnet-lease", "10.0.1.10");+ }++ @Test+ void launchReturnsTheLeaseWhenTheLaunchThrows() throws Exception {+ LaunchHarness harness = launchHarness();+ when(harness.builder.build()).thenThrow(new IllegalStateException("daemon went away"));++ Instance instance = leasedInstance("i-throws");++ harness.manager.launch(instance, "ubuntu:24.04", null, "us-west-2");++ awaitUntil(() -> "terminated".equals(instance.getState().getName()), Duration.ofSeconds(2));+ // Detach before release: Docker holds the address while the endpoint stands, so handing it+ // to the next launch before disconnecting would have the daemon refuse it.+ verify(harness.vpcNetworkManager, timeout(2000)).detach("us-west-2", "vpc-lease", null);+ verify(harness.vpcNetworkManager, timeout(2000))+ .releasePrivateIp("us-west-2", "subnet-lease", "10.0.1.10");+ }++ private static Instance leasedInstance(String instanceId) {+ Instance instance = instance(instanceId);+ instance.setRegion("us-west-2");+ instance.setVpcId("vpc-lease");+ instance.setSubnetId("subnet-lease");+ instance.setPrivateIpAddress("10.0.1.10");+ return instance;+ }+@Testvoid launchInstanceUserDataStreamToCloudWatch() throws Exception {LaunchHarness harness = launchHarness();@@ -1078,6 +1279,7 @@ class Ec2ContainerManagerTest {Ec2MetadataServer metadataServer = mock(Ec2MetadataServer.class);ContainerLogStreamer logStreamer = mock(ContainerLogStreamer.class);Ec2PortForwardManager portForwardManager = mock(Ec2PortForwardManager.class);+ VpcNetworkManager vpcNetworkManager = mock(VpcNetworkManager.class);RegionResolver regionResolver = mock(RegionResolver.class);when(regionResolver.getAccountId()).thenReturn("000000000000");Ec2ContainerManager manager = executor == null@@ -1093,7 +1295,8 @@ class Ec2ContainerManagerTest {metadataServer,portForwardManager,regionResolver,- mock(ContainerNetworkReachability.class))+ mock(ContainerNetworkReachability.class),+ vpcNetworkManager): new Ec2ContainerManager(containerBuilder,lifecycleManager,@@ -1107,10 +1310,11 @@ class Ec2ContainerManagerTest {portForwardManager,regionResolver,mock(ContainerNetworkReachability.class),+ vpcNetworkManager,executor,userDataTimeout);return new LaunchHarness(manager, lifecycleManager, dockerClient, metadataServer, logStreamer, builder,- portAllocator, portForwardManager, config, new CopyOnWriteArrayList<>());+ portAllocator, portForwardManager, config, vpcNetworkManager, new CopyOnWriteArrayList<>());}// ── startup reconciliation of EC2 containers orphaned by a previous run ──────@@ -1401,6 +1605,7 @@ class Ec2ContainerManagerTest {PortAllocator portAllocator,Ec2PortForwardManager portForwardManager,EmulatorConfig config,+ VpcNetworkManager vpcNetworkManager,List<String[]> executedCommands) {void stubSuccessfulExecs(CountDownLatch userDataStarted, CountDownLatch finishUserData) throws Exception {AtomicReference<String[]> currentCommand = new AtomicReference<>();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 ef441add17..7ba6e0d228 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@@ -19,6 +19,7 @@ import io.github.hectorvent.floci.services.ec2.model.KeyPair;import io.github.hectorvent.floci.services.ec2.model.SecurityGroupRule;import io.github.hectorvent.floci.services.ec2.model.UserIdGroupPair;import io.github.hectorvent.floci.services.ec2.model.InstanceState;+import io.github.hectorvent.floci.services.ec2.net.VpcNetworkManager;import io.github.hectorvent.floci.services.ec2.model.LaunchTemplate;import io.github.hectorvent.floci.services.ec2.model.LaunchTemplateData;import io.github.hectorvent.floci.services.ec2.model.ManagedPrefixList;@@ -64,6 +65,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;import static org.junit.jupiter.api.Assertions.assertNull;import static org.junit.jupiter.api.Assertions.assertThrows;import static org.junit.jupiter.api.Assertions.assertTrue;+import static org.mockito.ArgumentMatchers.anyString;import static org.mockito.ArgumentMatchers.argThat;import static org.mockito.Mockito.mock;import static org.mockito.Mockito.never;@@ -1207,6 +1209,59 @@ class Ec2ServiceTest {assertNotNull(attached.getEbs().getSnapshotId());}+ @Test+ void restoreReReservesThePrivateAddressesOfInstancesThatSurvivedTheRestart() {+ // The lease table is in-memory, so a restart rebuilds it empty while the containers of+ // persisted instances still hold their addresses. Without the re-reservation the next+ // RunInstances is handed an address a live container already answers on.+ AccountAwareStorageBackend<Instance> instanceStore = AccountAwareStorageBackend.inMemory("000000000000");+ instanceStore.put("us-east-1::i-alive", persistedInstance("i-alive", "10.0.1.10", InstanceState.running()));+ instanceStore.put("us-east-1::i-gone", persistedInstance("i-gone", "10.0.1.11", InstanceState.terminated()));++ VpcNetworkManager vpcNetworks = mock(VpcNetworkManager.class);+ when(vpcNetworks.enabled()).thenReturn(true);+ Ec2Service service = new Ec2Service(mockConfig(false), mock(Ec2ContainerManager.class),+ mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),+ new Ec2InstanceTypeCatalog(),+ new InMemoryStorageFactory(Map.of("ec2-instances.json", instanceStore)), vpcNetworks);++ service.restoreMetadataRegistrations();++ verify(vpcNetworks).reservePrivateIp("us-east-1", "subnet-a", "10.0.1.10");+ verify(vpcNetworks, never()).reservePrivateIp("us-east-1", "subnet-a", "10.0.1.11");+ }++ @Test+ void restoreSurvivesAnAddressItCannotReserve() {+ AccountAwareStorageBackend<Instance> instanceStore = AccountAwareStorageBackend.inMemory("000000000000");+ instanceStore.put("us-east-1::i-one", persistedInstance("i-one", "10.0.1.10", InstanceState.running()));+ instanceStore.put("us-east-1::i-two", persistedInstance("i-two", "10.0.1.10", InstanceState.running()));++ VpcNetworkManager vpcNetworks = mock(VpcNetworkManager.class);+ when(vpcNetworks.enabled()).thenReturn(true);+ // Second claim on the same address is refused; startup must not abort over it.+ when(vpcNetworks.reservePrivateIp(anyString(), anyString(), anyString()))+ .thenReturn(true).thenReturn(false);+ Ec2Service service = new Ec2Service(mockConfig(false), mock(Ec2ContainerManager.class),+ mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),+ new Ec2InstanceTypeCatalog(),+ new InMemoryStorageFactory(Map.of("ec2-instances.json", instanceStore)), vpcNetworks);++ assertDoesNotThrow(service::restoreMetadataRegistrations);+ verify(vpcNetworks, times(2)).reservePrivateIp("us-east-1", "subnet-a", "10.0.1.10");+ }++ private static Instance persistedInstance(String instanceId, String privateIp, InstanceState state) {+ Instance instance = new Instance();+ instance.setInstanceId(instanceId);+ instance.setRegion("us-east-1");+ instance.setVpcId("vpc-a");+ instance.setSubnetId("subnet-a");+ instance.setPrivateIpAddress(privateIp);+ instance.setState(state);+ return instance;+ }+private static String runOne(Ec2Service service, String imageId) {return service.runInstances("us-east-1", imageId, "t3.micro", 1, 1, null,List.of(), null, null, List.of(), null, null)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.javanew file mode 100644index 0000000000..b103765998--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/net/Cidr4Test.java@@ -0,0 +1,69 @@+package io.github.hectorvent.floci.services.ec2.net;++import org.junit.jupiter.api.Test;++import java.util.Optional;++import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertFalse;+import static org.junit.jupiter.api.Assertions.assertTrue;++class Cidr4Test {++ @Test+ void parsesAndNormalisesToTheNetworkAddress() {+ assertEquals("10.0.1.0/24", Cidr4.parse("10.0.1.37/24").orElseThrow().toString());+ assertEquals(256L, Cidr4.parse("10.0.1.0/24").orElseThrow().size());+ assertEquals("10.0.1.255", Cidr4.format(Cidr4.parse("10.0.1.0/24").orElseThrow().broadcast()));+ }++ @Test+ void rejectsMalformedInput() {+ for (String bad : new String[]{null, "", "10.0.0.0", "10.0.0.0/33", "10.0.0.256/24", "not-a-cidr"}) {+ assertEquals(Optional.empty(), Cidr4.parse(bad), "should reject " + bad);+ }+ }++ @Test+ void recognisesTheThreeRfc1918Ranges() {+ assertTrue(Cidr4.parse("10.0.0.0/16").orElseThrow().isRfc1918());+ assertTrue(Cidr4.parse("172.16.0.0/12").orElseThrow().isRfc1918());+ assertTrue(Cidr4.parse("172.31.0.0/16").orElseThrow().isRfc1918());+ assertTrue(Cidr4.parse("192.168.215.0/24").orElseThrow().isRfc1918());++ assertFalse(Cidr4.parse("172.15.0.0/16").orElseThrow().isRfc1918());+ assertFalse(Cidr4.parse("172.32.0.0/16").orElseThrow().isRfc1918());+ assertFalse(Cidr4.parse("11.0.0.0/8").orElseThrow().isRfc1918());+ assertFalse(Cidr4.parse("100.64.0.0/10").orElseThrow().isRfc1918(), "carrier-grade NAT is not RFC 1918");+ assertFalse(Cidr4.parse("0.0.0.0/0").orElseThrow().isRfc1918());+ }++ @Test+ void containmentAndOverlapAreExactAtTheBoundaries() {+ Cidr4 vpc = Cidr4.parse("10.0.0.0/16").orElseThrow();+ assertTrue(vpc.contains(Cidr4.parse("10.0.255.0/24").orElseThrow()));+ assertFalse(vpc.contains(Cidr4.parse("10.1.0.0/24").orElseThrow()));+ assertFalse(vpc.contains(Cidr4.parse("10.0.0.0/8").orElseThrow()), "a supernet is not contained");++ assertTrue(vpc.overlaps(Cidr4.parse("10.0.0.0/8").orElseThrow()));+ assertFalse(vpc.overlaps(Cidr4.parse("10.1.0.0/16").orElseThrow()));+ }++ @Test+ void comparesCorrectlyAboveTheSignedIntBoundary() {+ // 192.168.x as an int is negative; arithmetic that forgot to widen breaks exactly here.+ Cidr4 orbstack = Cidr4.parse("192.168.215.0/24").orElseThrow();+ assertTrue(orbstack.overlaps(Cidr4.parse("192.168.0.0/16").orElseThrow()));+ assertFalse(orbstack.overlaps(Cidr4.parse("192.168.216.0/24").orElseThrow()));+ assertEquals("192.168.215.10", orbstack.addressAt(10).orElseThrow());+ }++ @Test+ void addressAtStaysInsideTheBlock() {+ Cidr4 subnet = Cidr4.parse("10.0.1.0/24").orElseThrow();+ assertEquals("10.0.1.10", subnet.addressAt(10).orElseThrow());+ assertEquals("10.0.1.255", subnet.addressAt(255).orElseThrow());+ assertEquals(Optional.empty(), subnet.addressAt(256));+ assertEquals(Optional.empty(), subnet.addressAt(-1));+ }+}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.javanew file mode 100644index 0000000000..dbd0784f64--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManagerTest.java@@ -0,0 +1,497 @@+package io.github.hectorvent.floci.services.ec2.net;++import io.github.hectorvent.floci.config.EmulatorConfig;+import com.github.dockerjava.api.DockerClient;+import com.github.dockerjava.api.command.ConnectToNetworkCmd;+import com.github.dockerjava.api.command.CreateNetworkCmd;+import com.github.dockerjava.api.command.CreateNetworkResponse;+import com.github.dockerjava.api.command.DisconnectFromNetworkCmd;+import com.github.dockerjava.api.command.InspectNetworkCmd;+import com.github.dockerjava.api.command.ListNetworksCmd;+import com.github.dockerjava.api.command.RemoveNetworkCmd;+import com.github.dockerjava.api.exception.NotFoundException;+import com.github.dockerjava.api.model.Network;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;+import org.mockito.ArgumentCaptor;++import java.util.ArrayList;+import java.util.LinkedHashMap;+import java.util.List;+import java.util.Map;+import java.util.Optional;++import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertFalse;+import static org.junit.jupiter.api.Assertions.assertNotEquals;+import static org.junit.jupiter.api.Assertions.assertTrue;+import static org.mockito.ArgumentMatchers.any;+import static org.mockito.ArgumentMatchers.anyString;+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;+import static org.mockito.Mockito.RETURNS_SELF;+import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.never;+import static org.mockito.Mockito.verify;+import static org.mockito.Mockito.when;++class VpcNetworkManagerTest {++ private static final String REGION = "us-east-1";++ private DockerClient docker;+ private EmulatorConfig config;+ private VpcNetworkManager manager;+ private List<Network> existingNetworks;+ private List<String> removedNetworks;++ @BeforeEach+ void setUp() {+ existingNetworks = new ArrayList<>();+ removedNetworks = new ArrayList<>();+ docker = mock(DockerClient.class);+ config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);+ when(config.port()).thenReturn(4650);+ when(config.services().ec2().mock()).thenReturn(false);+ when(config.services().ec2().vpcNetworks().enabled()).thenReturn(true);+ when(config.services().ec2().vpcNetworks().fallbackPool()).thenReturn("10.240.0.0/12");+ when(config.services().ec2().vpcNetworks().fallbackPrefixLength()).thenReturn(16);+ when(config.services().ec2().vpcNetworks().reconcileOnStartup()).thenReturn(true);+ when(config.services().ec2().vpcNetworks().driver()).thenReturn("bridge");+ when(config.docker().extraLabels()).thenReturn(List.of());+ when(config.docker().resourceNamespace()).thenReturn(Optional.empty());++ ListNetworksCmd list = mock(ListNetworksCmd.class, RETURNS_SELF);+ when(list.exec()).thenAnswer(invocation -> List.copyOf(existingNetworks));+ when(docker.listNetworksCmd()).thenReturn(list);++ // Inspect resolves against the same fixture list, so a network with live endpoints can be+ // told apart from one that does not exist yet.+ when(docker.inspectNetworkCmd()).thenAnswer(listCall -> {+ InspectNetworkCmd inspect = mock(InspectNetworkCmd.class);+ String[] requested = new String[1];+ when(inspect.withNetworkId(anyString())).thenAnswer(call -> {+ requested[0] = call.getArgument(0);+ return inspect;+ });+ when(inspect.exec()).thenAnswer(call -> existingNetworks.stream()+ .filter(n -> n.getId().equals(requested[0]) || n.getName().equals(requested[0]))+ .findFirst()+ .orElseThrow(() -> new NotFoundException("no such network")));+ return inspect;+ });++ CreateNetworkCmd create = mock(CreateNetworkCmd.class, RETURNS_SELF);+ when(create.exec()).thenReturn(new CreateNetworkResponse());+ when(docker.createNetworkCmd()).thenReturn(create);++ ConnectToNetworkCmd connect = mock(ConnectToNetworkCmd.class, RETURNS_SELF);+ when(docker.connectToNetworkCmd()).thenReturn(connect);++ when(docker.disconnectFromNetworkCmd())+ .thenReturn(mock(DisconnectFromNetworkCmd.class, RETURNS_SELF));++ when(docker.removeNetworkCmd(anyString())).thenAnswer(invocation -> {+ removedNetworks.add(invocation.getArgument(0));+ return mock(RemoveNetworkCmd.class, RETURNS_SELF);+ });++ manager = new VpcNetworkManager(config, docker);+ }++ private void existingNetwork(String name, String subnet, Map<String, String> labels) {+ existingNetwork(name, subnet, labels, Map.of());+ }++ private void existingNetwork(String name, String subnet, Map<String, String> labels,+ Map<String, Network.ContainerNetworkConfig> containers) {+ Network network = mock(Network.class);+ when(network.getContainers()).thenReturn(containers);+ when(network.getName()).thenReturn(name);+ when(network.getId()).thenReturn(name);+ when(network.getLabels()).thenReturn(labels);+ when(network.getIpam()).thenReturn(new Network.Ipam()+ .withConfig(new Network.Ipam.Config().withSubnet(subnet)));+ existingNetworks.add(network);+ }++ // ─── The ordinary case: the declared CIDR is what the instance gets ───────++ @Test+ void usesTheDeclaredCidrAndAllocatesInsideTheDeclaredSubnet() {+ manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");+ manager.declareSubnet(REGION, "vpc-1", "subnet-a", "10.0.1.0/24");++ assertFalse(manager.isSubstituted(REGION, "vpc-1"));+ assertEquals("10.0.0.0/16", manager.effectiveVpcCidr(REGION, "vpc-1").orElseThrow());+ assertEquals("10.0.1.0/24", manager.effectiveSubnetCidr(REGION, "subnet-a").orElseThrow());++ Cidr4 declared = Cidr4.parse("10.0.1.0/24").orElseThrow();+ for (int i = 0; i < 5; i++) {+ String address = manager.allocatePrivateIp(REGION, "subnet-a").orElseThrow();+ long value = Cidr4.parse(address + "/32").orElseThrow().network();+ assertTrue(declared.containsAddress(value), address + " should be inside 10.0.1.0/24");+ assertNotEquals("10.0.1.0", address, "never the network address");+ assertNotEquals("10.0.1.1", address, "never the Docker gateway");+ }+ }++ @Test+ void handsOutADistinctAddressEachTime() {+ manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");+ manager.declareSubnet(REGION, "vpc-1", "subnet-a", "10.0.1.0/24");+ String first = manager.allocatePrivateIp(REGION, "subnet-a").orElseThrow();+ String second = manager.allocatePrivateIp(REGION, "subnet-a").orElseThrow();+ assertNotEquals(first, second);+ }++ @Test+ void aReleasedAddressIsHandedOutAgainRatherThanExhaustingTheSubnet() {+ manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");+ manager.declareSubnet(REGION, "vpc-1", "subnet-tiny", "10.0.9.0/28");++ List<String> allocated = new ArrayList<>();+ Optional<String> next;+ while ((next = manager.allocatePrivateIp(REGION, "subnet-tiny")).isPresent()) {+ allocated.add(next.get());+ }+ assertTrue(allocated.size() >= 4, "a /28 should yield several usable addresses");+ assertTrue(manager.allocatePrivateIp(REGION, "subnet-tiny").isEmpty(), "and then be exhausted");++ manager.releasePrivateIp(REGION, "subnet-tiny", allocated.get(0));+ assertEquals(allocated.get(0), manager.allocatePrivateIp(REGION, "subnet-tiny").orElseThrow(),+ "a terminated instance's address must come back into circulation");+ }++ // ─── Restart: addresses persisted instances still hold ───────────────────++ @Test+ void aReservedAddressIsNeverHandedOutAgain() {+ manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");+ manager.declareSubnet(REGION, "vpc-1", "subnet-a", "10.0.1.0/24");++ // What a restart looks like: the lease table is empty, but a live container holds .10.+ assertTrue(manager.reservePrivateIp(REGION, "subnet-a", "10.0.1.10"));++ for (int i = 0; i < 20; i++) {+ assertNotEquals("10.0.1.10", manager.allocatePrivateIp(REGION, "subnet-a").orElseThrow(),+ "the address a restored instance still holds must not be re-allocated");+ }+ }++ @Test+ void reservingTheSameAddressTwiceRefusesTheSecondClaimWithoutThrowing() {+ manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");+ manager.declareSubnet(REGION, "vpc-1", "subnet-a", "10.0.1.0/24");++ assertTrue(manager.reservePrivateIp(REGION, "subnet-a", "10.0.1.11"));+ assertFalse(manager.reservePrivateIp(REGION, "subnet-a", "10.0.1.11"),+ "two instances claiming one address is reported, not thrown");+ }++ @Test+ void reservingAnAddressOutsideTheSubnetRangeRefusesWithoutThrowing() {+ manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");+ manager.declareSubnet(REGION, "vpc-1", "subnet-a", "10.0.1.0/24");++ assertFalse(manager.reservePrivateIp(REGION, "subnet-a", "172.31.4.9"),+ "an address from a range this subnet no longer uses cannot be reserved");+ assertFalse(manager.reservePrivateIp(REGION, "subnet-a", "not-an-address"));+ assertFalse(manager.reservePrivateIp(REGION, "subnet-unknown", "10.0.1.10"),+ "a subnet with no Docker-backed range has nothing to reserve in");+ // None of that poisoned the pool.+ assertTrue(manager.allocatePrivateIp(REGION, "subnet-a").isPresent());+ }++ @Test+ void aReservedAddressComesBackAfterRelease() {+ manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");+ manager.declareSubnet(REGION, "vpc-1", "subnet-tiny", "10.0.9.0/28");++ assertTrue(manager.reservePrivateIp(REGION, "subnet-tiny", "10.0.9.10"));+ manager.releasePrivateIp(REGION, "subnet-tiny", "10.0.9.10");++ List<String> allocated = new ArrayList<>();+ Optional<String> next;+ while ((next = manager.allocatePrivateIp(REGION, "subnet-tiny")).isPresent()) {+ allocated.add(next.get());+ }+ assertTrue(allocated.contains("10.0.9.10"),+ "a terminated restored instance's address must return to circulation");+ }++ @Test+ void subnetsInTheSameVpcGetDisjointRangesOnOneNetwork() {+ manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");+ manager.declareSubnet(REGION, "vpc-1", "subnet-a", "10.0.1.0/24");+ manager.declareSubnet(REGION, "vpc-1", "subnet-b", "10.0.2.0/24");++ assertEquals("10.0.1.0/24", manager.effectiveSubnetCidr(REGION, "subnet-a").orElseThrow());+ assertEquals("10.0.2.0/24", manager.effectiveSubnetCidr(REGION, "subnet-b").orElseThrow());+ // Same VPC, so the same Docker network: subnets inside a VPC route to each other in AWS.+ assertEquals(manager.networkNameFor(REGION, "vpc-1"), manager.networkNameFor(REGION, "vpc-1"));+ }++ // ─── The three substitution triggers ─────────────────────────────────────++ @Test+ void substitutesWhenTheDeclaredCidrIsOutsideRfc1918() {+ manager.declareVpc(REGION, "vpc-public", "54.0.0.0/16");+ assertTrue(manager.isSubstituted(REGION, "vpc-public"));+ Cidr4 effective = Cidr4.parse(manager.effectiveVpcCidr(REGION, "vpc-public").orElseThrow()).orElseThrow();+ assertTrue(effective.isRfc1918());+ assertTrue(Cidr4.parse("10.240.0.0/12").orElseThrow().contains(effective));+ }++ @Test+ void substitutesWhenNoCidrIsDeclared() {+ manager.declareVpc(REGION, "vpc-blank", null);+ assertTrue(manager.isSubstituted(REGION, "vpc-blank"));+ assertTrue(manager.effectiveVpcCidr(REGION, "vpc-blank").isPresent());+ }++ @Test+ void substitutesWhenTheDeclaredCidrCollidesWithAnExistingDockerNetwork() {+ // OrbStack's default bridge on this machine.+ existingNetwork("orbstack", "192.168.215.0/24", Map.of());++ manager.declareVpc(REGION, "vpc-clash", "192.168.215.0/24");++ assertTrue(manager.isSubstituted(REGION, "vpc-clash"));+ Cidr4 effective = Cidr4.parse(manager.effectiveVpcCidr(REGION, "vpc-clash").orElseThrow()).orElseThrow();+ assertFalse(effective.overlaps(Cidr4.parse("192.168.215.0/24").orElseThrow()));+ }++ @Test+ void twoVpcsMayDeclareTheSameCidrAndBothKeepWorking() {+ manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");+ manager.declareSubnet(REGION, "vpc-1", "subnet-1", "10.0.1.0/24");+ manager.declareVpc(REGION, "vpc-2", "10.0.0.0/16");+ manager.declareSubnet(REGION, "vpc-2", "subnet-2", "10.0.1.0/24");++ assertFalse(manager.isSubstituted(REGION, "vpc-1"), "the first claim keeps the declared range");+ assertTrue(manager.isSubstituted(REGION, "vpc-2"), "the second is substituted, not rejected");++ Cidr4 first = Cidr4.parse(manager.effectiveVpcCidr(REGION, "vpc-1").orElseThrow()).orElseThrow();+ Cidr4 second = Cidr4.parse(manager.effectiveVpcCidr(REGION, "vpc-2").orElseThrow()).orElseThrow();+ assertFalse(first.overlaps(second));++ // Both still allocate: a duplicate CIDR must not leave a VPC unusable.+ String a = manager.allocatePrivateIp(REGION, "subnet-1").orElseThrow();+ String b = manager.allocatePrivateIp(REGION, "subnet-2").orElseThrow();+ assertTrue(first.containsAddress(Cidr4.parse(a + "/32").orElseThrow().network()));+ assertTrue(second.containsAddress(Cidr4.parse(b + "/32").orElseThrow().network()));+ }++ @Test+ void aSubnetOutsideItsVpcRangeIsRemappedInsideIt() {+ manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");+ manager.declareSubnet(REGION, "vpc-1", "subnet-stray", "172.16.5.0/24");++ Cidr4 effective = Cidr4.parse(manager.effectiveSubnetCidr(REGION, "subnet-stray").orElseThrow()).orElseThrow();+ assertTrue(Cidr4.parse("10.0.0.0/16").orElseThrow().contains(effective),+ "an address must be routable on the VPC's own network to mean anything");+ }++ // ─── Materialisation and attachment ──────────────────────────────────────++ @Test+ void createsTheNetworkWithTheEffectiveCidrOnFirstAttach() {+ manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");+ manager.declareSubnet(REGION, "vpc-1", "subnet-a", "10.0.1.0/24");+ String address = manager.allocatePrivateIp(REGION, "subnet-a").orElseThrow();++ assertTrue(manager.attach(REGION, "vpc-1", "subnet-a", "container-1", address).isPresent());++ ArgumentCaptor<Network.Ipam> ipam = ArgumentCaptor.forClass(Network.Ipam.class);+ verify(docker.createNetworkCmd()).withIpam(ipam.capture());+ assertEquals("10.0.0.0/16", ipam.getValue().getConfig().get(0).getSubnet());+ verify(docker.connectToNetworkCmd()).withContainerId("container-1");+ }++ @Test+ void refusesToAttachAnAddressItDidNotPlan() {+ manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");+ manager.declareSubnet(REGION, "vpc-1", "subnet-a", "10.0.1.0/24");++ // The synthesised address Ec2Service falls back to: outside the network's IPAM pool.+ assertTrue(manager.attach(REGION, "vpc-1", "subnet-a", "container-1", "172.31.0.11").isEmpty());+ verify(docker, never()).connectToNetworkCmd();+ }++ @Test+ void adoptsALeftoverNetworkOnlyWhenItsSubnetIsTheOneThisVpcAllocatesFrom() {+ // A Floci restart: the bridge for vpc-1 survived, on the range vpc-1 still plans from.+ existingNetwork(manager.networkName(REGION, "vpc-1"), "10.0.0.0/16", ourLabels("vpc-1", "4650"));+ manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");+ manager.declareSubnet(REGION, "vpc-1", "subnet-a", "10.0.1.0/24");+ String address = manager.allocatePrivateIp(REGION, "subnet-a").orElseThrow();++ assertTrue(manager.attach(REGION, "vpc-1", "subnet-a", "container-1", address).isPresent());++ verify(docker, never()).createNetworkCmd();+ assertTrue(removedNetworks.isEmpty(), "a matching network is reused, not churned");+ }++ @Test+ void recreatesALeftoverNetworkWhoseSubnetIsNotTheRangeThisVpcAllocatesFrom() {+ // What a previous run with a different declaration leaves behind: this VPC's own network+ // name, carrying a range that has nothing to do with the addresses now being handed out.+ String networkName = manager.networkName(REGION, "vpc-1");+ existingNetwork(networkName, "172.20.0.0/16", ourLabels("vpc-1", "4650"));++ manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");+ manager.declareSubnet(REGION, "vpc-1", "subnet-a", "10.0.1.0/24");+ String address = manager.allocatePrivateIp(REGION, "subnet-a").orElseThrow();+ assertTrue(Cidr4.parse("10.0.1.0/24").orElseThrow()+ .containsAddress(Cidr4.parse(address + "/32").orElseThrow().network()));++ assertTrue(manager.attach(REGION, "vpc-1", "subnet-a", "container-1", address).isPresent());++ assertEquals(List.of(networkName), removedNetworks,+ "a network routing 172.20/16 cannot carry a 10.0.1.x address, so it is not adopted");+ ArgumentCaptor<Network.Ipam> ipam = ArgumentCaptor.forClass(Network.Ipam.class);+ verify(docker.createNetworkCmd()).withIpam(ipam.capture());+ assertEquals("10.0.0.0/16", ipam.getValue().getConfig().get(0).getSubnet(),+ "the network is recreated on the range the addresses actually come from");+ }++ @Test+ void keepsInstancesOnTheBridgeWhenAStaleNetworkCannotBeRemoved() {+ String networkName = manager.networkName(REGION, "vpc-1");+ existingNetwork(networkName, "172.20.0.0/16", ourLabels("vpc-1", "4650"));+ when(docker.removeNetworkCmd(anyString())).thenThrow(new RuntimeException("has active endpoints"));++ manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");+ manager.declareSubnet(REGION, "vpc-1", "subnet-a", "10.0.1.0/24");+ String address = manager.allocatePrivateIp(REGION, "subnet-a").orElseThrow();++ assertTrue(manager.attach(REGION, "vpc-1", "subnet-a", "container-1", address).isEmpty(),+ "an unreachable private IP is a documented degradation; an address the network "+ + "does not route is a wrong answer");+ verify(docker, never()).connectToNetworkCmd();+ }++ // ─── Small blocks: usable, or refused out loud ───────────────────────────++ @Test+ void aSubnetTooSmallForTenAddressesOfHeadroomStillAllocates() {+ manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");+ // A /29 holds eight addresses in total, so the usual .10 offset falls off the end of it.+ manager.declareSubnet(REGION, "vpc-1", "subnet-tiny", "10.0.9.0/29");++ assertEquals("10.0.9.0/29", manager.effectiveSubnetCidr(REGION, "subnet-tiny").orElseThrow());+ List<String> allocated = new ArrayList<>();+ Optional<String> next;+ while ((next = manager.allocatePrivateIp(REGION, "subnet-tiny")).isPresent()) {+ allocated.add(next.get());+ }+ assertEquals(List.of("10.0.9.2", "10.0.9.3", "10.0.9.4", "10.0.9.5", "10.0.9.6"), allocated,+ "past the network address and the Docker gateway, and short of the broadcast address");+ }++ @Test+ void aSubnetWithNoRoomForAnyHostIsRefusedRatherThanAllocatingNothing() {+ manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");+ manager.declareSubnet(REGION, "vpc-1", "subnet-nothing", "10.0.9.0/31");++ assertTrue(manager.effectiveSubnetCidr(REGION, "subnet-nothing").isEmpty(),+ "a subnet that can never hand out an address must not claim a Docker-backed range: "+ + "the caller has to fall back rather than read empty as exhaustion");+ assertTrue(manager.allocatePrivateIp(REGION, "subnet-nothing").isEmpty());+ }++ @Test+ void anUnusableFallbackPrefixLengthIsClampedToOneThatCanServeHosts() {+ // Nothing bounds this config value, and a /30 block has no room for an instance at all.+ when(config.services().ec2().vpcNetworks().fallbackPrefixLength()).thenReturn(30);++ manager.declareVpc(REGION, "vpc-public", "54.0.0.0/16");+ manager.declareSubnet(REGION, "vpc-public", "subnet-a", "54.0.1.0/24");++ Cidr4 effective = Cidr4.parse(manager.effectiveVpcCidr(REGION, "vpc-public").orElseThrow()).orElseThrow();+ assertTrue(effective.prefix() <= VpcNetworkManager.MAX_ALLOCATABLE_PREFIX,+ "a configured-but-unusable prefix must not leave every substituted VPC empty");+ assertTrue(manager.allocatePrivateIp(REGION, "subnet-a").isPresent(),+ "the substituted VPC has to be able to hand out an address");+ }++ @Test+ void doesNothingWhenDisabled() {+ when(config.services().ec2().vpcNetworks().enabled()).thenReturn(false);+ manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");+ manager.declareSubnet(REGION, "vpc-1", "subnet-a", "10.0.1.0/24");+ assertTrue(manager.allocatePrivateIp(REGION, "subnet-a").isEmpty());+ assertTrue(manager.effectiveVpcCidr(REGION, "vpc-1").isEmpty());+ }++ // ─── Startup reconciliation ──────────────────────────────────────────────++ @Test+ void reconcileRemovesOnlyThisEmulatorsOwnOrphans() {+ existingNetwork("floci-vpc-4650-us-east-1-vpc-dead", "10.7.0.0/16", ourLabels("vpc-dead", "4650"));+ existingNetwork("floci-vpc-4650-us-east-1-vpc-live", "10.8.0.0/16", ourLabels("vpc-live", "4650"));+ existingNetwork("floci-vpc-4620-us-east-1-vpc-other", "10.9.0.0/16", ourLabels("vpc-other", "4620"));+ existingNetwork("some-users-network", "10.10.0.0/16", Map.of());++ manager.reconcileOrphans((region, vpcId) -> "vpc-live".equals(vpcId));++ assertEquals(List.of("floci-vpc-4650-us-east-1-vpc-dead"), removedNetworks,+ "a live VPC keeps its network, another emulator's networks are never touched, "+ + "and networks Floci did not create are out of scope entirely");+ }++ @Test+ void reconcileDetachesAContainerTheCrashedRunLeftOnTheNetwork() {+ Map<String, Network.ContainerNetworkConfig> attached =+ Map.of("container-from-a-dead-run", new Network.ContainerNetworkConfig());+ existingNetwork("floci-vpc-4650-us-east-1-vpc-dead", "10.7.0.0/16",+ ourLabels("vpc-dead", "4650"), attached);++ manager.reconcileOrphans((region, vpcId) -> false);++ // Without the disconnect Docker refuses removal with "has active endpoints", and the+ // stale IPAM reservation then pushes the next run's identical CIDR into the fallback pool.+ verify(docker).disconnectFromNetworkCmd();+ assertEquals(List.of("floci-vpc-4650-us-east-1-vpc-dead"), removedNetworks);+ }++ @Test+ void reconcileIsSkippedWhenTurnedOff() {+ when(config.services().ec2().vpcNetworks().reconcileOnStartup()).thenReturn(false);+ existingNetwork("floci-vpc-4650-us-east-1-vpc-dead", "10.7.0.0/16", ourLabels("vpc-dead", "4650"));+ manager.reconcileOrphans((region, vpcId) -> false);+ assertTrue(removedNetworks.isEmpty());+ }++ @Test+ void aSurvivingNetworkForTheSameVpcIsNotReadAsACollision() {+ // What a Floci restart sees: its own bridge for vpc-1 is still up.+ existingNetwork(manager.networkName(REGION, "vpc-1"), "10.0.0.0/16", ourLabels("vpc-1", "4650"));++ manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");++ assertFalse(manager.isSubstituted(REGION, "vpc-1"),+ "re-planning a VPC must not collide with that VPC's own surviving network");+ assertEquals("10.0.0.0/16", manager.effectiveVpcCidr(REGION, "vpc-1").orElseThrow());+ }++ private static Map<String, String> ourLabels(String vpcId, String ownerPort) {+ Map<String, String> labels = new LinkedHashMap<>();+ labels.put(VpcNetworkManager.LABEL_COMPONENT, VpcNetworkManager.COMPONENT_VALUE);+ labels.put(VpcNetworkManager.LABEL_VPC_ID, vpcId);+ labels.put(VpcNetworkManager.LABEL_VPC_REGION, REGION);+ labels.put(VpcNetworkManager.LABEL_OWNER_PORT, ownerPort);+ return labels;+ }++ @Test+ void subBlockAllocationSkipsTakenRanges() {+ Cidr4 parent = Cidr4.parse("10.0.0.0/16").orElseThrow();+ Cidr4 taken = Cidr4.parse("10.0.0.0/24").orElseThrow();+ assertEquals("10.0.1.0/24",+ VpcNetworkManager.allocateSubBlock(parent, 24, List.of(taken)).toString());+ assertEquals(null,+ VpcNetworkManager.allocateSubBlock(parent, 16, List.of(parent)),+ "a fully claimed parent yields nothing rather than an overlapping block");+ }+}diff --git a/src/test/resources/application.yml b/src/test/resources/application.ymlindex 40dda81462..ed7d8b546c 100644--- a/src/test/resources/application.yml+++ b/src/test/resources/application.yml@@ -259,6 +259,10 @@ floci:mock: trueaws-faithful-private-ip: false# container-ips-routable: true # see src/main/resources/application.yml+ # Off explicitly, not merely by implication of mock: true above. Tests must not create or+ # reconcile Docker networks on whatever daemon CI happens to have.+ vpc-networks:+ enabled: falseecs:enabled: truemock: true