From 33d35b2079daefaa4f96db7288ac2a848c237b56 Mon Sep 17 00:00:00 2001 From: lex00 <121451605+lex00@users.noreply.github.com> Date: Sat, 15 Aug 2026 17:48:49 -0600 Subject: [PATCH] feat(ec2): config toggle to report the AWS-faithful private IP (#2024) * feat(ec2): config toggle to report the AWS-faithful private IP (#1983) DescribeInstances reported the Docker container's bridge IP as PrivateIpAddress, disagreeing with CloudFormation's subnet-allocated IP. Rather than force a fidelity-vs-reachability tradeoff, this adds an opt-in: ec2.aws-faithful-private-ip (default false) Default (false) is unchanged: the reported private IP is the reachable container bridge IP, so instances can reach each other at that address on the shared Docker network. When true, the CFN/subnet-allocated private IP set at launch is reported instead (AWS-faithful); routing and IMDS always use the container bridge IP (tracked separately as containerBridgeIp) regardless of the flag. Wired via a new awsFaithful overload of exposeReachablePrivateAddress; the three call sites pass the config flag. Test covers both modes. Fixes #1983. * docs(ec2): list aws-faithful-private-ip in application.yml and the config table Every other floci.services.ec2 knob appears in both application.yml files and the docs configuration table; this one was declared only on EmulatorConfig, so a user-facing flag was undiscoverable. --- docs/services/ec2.md | 1 + .../floci/config/EmulatorConfig.java | 12 ++++++++++ .../services/ec2/Ec2ContainerManager.java | 20 +++++++++++++--- src/main/resources/application.yml | 1 + .../services/ec2/Ec2ContainerManagerTest.java | 24 ++++++++++++++++++- src/test/resources/application.yml | 1 + 6 files changed, 55 insertions(+), 4 deletions(-) diff --git a/docs/services/ec2.md b/docs/services/ec2.md index ba131b6e3c..1a591eefac 100644 --- a/docs/services/ec2.md +++ b/docs/services/ec2.md @@ -420,6 +420,7 @@ Launch templates store versioned launch data. New template versions can be creat | `FLOCI_SERVICES_EC2_MAX_PUBLISHED_PORTS_PER_INSTANCE` | `20` | Max published ports per instance; also the widest single-rule span published | | `FLOCI_SERVICES_EC2_SOCAT_IMAGE` | `alpine/socat` | Image used for the port-forwarding sidecar | | `FLOCI_SERVICES_EC2_MOCK` | `false` | Skip Docker; instances jump directly to final state (useful for tests) | +| `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 | ## Requirements diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java index a4faa4b754..491f1f1686 100644 --- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java +++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java @@ -1545,6 +1545,18 @@ public interface EmulatorConfig { @WithDefault("true") boolean enabled(); + /** + * When true, DescribeInstances and IMDS report each instance's CFN- and + * subnet-allocated private IP (AWS-faithful) instead of the Docker + * container's bridge IP (#1983). Default false keeps the bridge IP as the + * reported private address, which lets instances reach each other at that + * address on the shared Docker network. Routing/IMDS always use the + * container bridge IP regardless of this flag; only the reported + * PrivateIpAddress changes. + */ + @WithDefault("false") + boolean awsFaithfulPrivateIp(); + /** Port on the Floci host for the IMDS HTTP server (169.254.169.254 equivalent). */ @WithDefault("9169") int imdsPort(); 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.java index 51582c855b..1ad94153d6 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 @@ -192,7 +192,7 @@ public class Ec2ContainerManager { String containerIp = waitForContainerBridgeIp(containerId, instanceId); if (containerIp != null && !containerIp.isBlank()) { instance.setContainerBridgeIp(containerIp); - exposeReachablePrivateAddress(instance, containerIp); + exposeReachablePrivateAddress(instance, containerIp, config.services().ec2().awsFaithfulPrivateIp()); metadataServer.registerContainer(containerIp, instanceId, instance); } else { @@ -321,7 +321,7 @@ public class Ec2ContainerManager { String containerIp = waitForContainerBridgeIp(containerId, instanceId); if (containerIp != null && !containerIp.isBlank()) { instance.setContainerBridgeIp(containerIp); - exposeReachablePrivateAddress(instance, containerIp); + exposeReachablePrivateAddress(instance, containerIp, config.services().ec2().awsFaithfulPrivateIp()); metadataServer.registerContainer(containerIp, instanceId, instance); } } catch (InterruptedException e) { @@ -361,15 +361,29 @@ public class Ec2ContainerManager { metadataServer.unregisterContainer(previousContainerIp, instance); } instance.setContainerBridgeIp(containerIp); - exposeReachablePrivateAddress(instance, containerIp); + exposeReachablePrivateAddress(instance, containerIp, config.services().ec2().awsFaithfulPrivateIp()); metadataServer.registerContainer(containerIp, instance.getInstanceId(), instance); return true; } static void exposeReachablePrivateAddress(Instance instance, String privateIp) { + exposeReachablePrivateAddress(instance, privateIp, false); + } + + /** + * Overwrite the instance's reported private address with the container's + * reachable bridge IP — unless {@code awsFaithful} is true (#1983), in which + * case the CFN/subnet-allocated private IP set at launch is left untouched. + * The container bridge IP is tracked separately (setContainerBridgeIp) and + * used for routing/IMDS regardless of this flag. + */ + static void exposeReachablePrivateAddress(Instance instance, String privateIp, boolean awsFaithful) { if (instance == null || privateIp == null || privateIp.isBlank()) { return; } + if (awsFaithful) { + return; + } String privateDnsName = "ip-" + privateIp.replace('.', '-') + ".ec2.internal"; instance.setPrivateIpAddress(privateIp); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 9f5a8eae37..5cff8a47e7 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -363,6 +363,7 @@ floci: max-published-ports-per-instance: 20 socat-image: alpine/socat mock: false + aws-faithful-private-ip: false ecs: enabled: true mock: false diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManagerTest.java index 6229c651ac..8eb0b32cd1 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 @@ -47,6 +47,7 @@ import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Answers.RETURNS_SELF; +import static org.mockito.Answers.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.timeout; @@ -87,6 +88,27 @@ class Ec2ContainerManagerTest { assertEquals("ip-192-168-215-21.ec2.internal", networkInterface.getPrivateDnsName()); } + @Test + void exposeReachablePrivateAddressPreservesAllocatedIpWhenAwsFaithful() { + // #1983: with awsFaithfulPrivateIp=true, the CFN/subnet-allocated private + // IP set at launch is left untouched — the container bridge IP is not + // reported (routing still uses it via containerBridgeIp, tracked elsewhere). + Instance instance = new Instance(); + instance.setPrivateIpAddress("10.82.32.10"); + instance.setPrivateDnsName("ip-10-82-32-10.ec2.internal"); + + InstanceNetworkInterface networkInterface = new InstanceNetworkInterface(); + networkInterface.setPrivateIpAddress("10.82.32.10"); + networkInterface.setPrivateDnsName("ip-10-82-32-10.ec2.internal"); + instance.setNetworkInterfaces(List.of(networkInterface)); + + Ec2ContainerManager.exposeReachablePrivateAddress(instance, "192.168.215.21", true); + + assertEquals("10.82.32.10", instance.getPrivateIpAddress()); + assertEquals("ip-10-82-32-10.ec2.internal", instance.getPrivateDnsName()); + assertEquals("10.82.32.10", networkInterface.getPrivateIpAddress()); + } + @Test void restoreMetadataRegistrationRegistersRunningPersistedContainer() { ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class); @@ -107,7 +129,7 @@ class Ec2ContainerManagerTest { mock(DockerHostResolver.class), dockerClient, mock(PortAllocator.class), - mock(EmulatorConfig.class), + mock(EmulatorConfig.class, RETURNS_DEEP_STUBS), metadataServer, mock(Ec2PortForwardManager.class)); diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index b1090c20c9..698ded0969 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -216,6 +216,7 @@ floci: max-published-ports-per-instance: 20 socat-image: alpine/socat mock: true + aws-faithful-private-ip: false ecs: enabled: true mock: true