Light, fluffy, and always free - The AWS Local Emulator alternative
fix(ecr): advertise the registry at the published address (#4863)
Mario Toffiac1cd5af2bdc6
diff --git a/docs/services/ecr.md b/docs/services/ecr.mdindex 8e66d88116..4b7ef2673a 100644--- a/docs/services/ecr.md+++ b/docs/services/ecr.md@@ -88,6 +88,8 @@ services:`docker login 000000000000.dkr.ecr.us-east-1.localhost:4566` works once Floci starts the registry sidecar.+When Floci runs in a container that publishes its port on another host port (for example `docker run -p 54321:4566`), the `repositoryUri`, the `GetAuthorizationToken` `proxyEndpoint` and the rewritten Lambda and ECS image URIs all name that host port (`000000000000.dkr.ecr.us-east-1.localhost:54321`), because the Docker daemon performs login, push and pull through it. Floci reads the published port from its own container through the mounted Docker socket, and uses `floci.port` (`4566`) when it does not run in a container or the port is not published.+## Examples```bashdiff --git a/docs/services/eks.md b/docs/services/eks.mdindex 934c11f09d..a5db9a08e8 100644--- a/docs/services/eks.md+++ b/docs/services/eks.md@@ -679,6 +679,8 @@ Requirements and limits:EKS service network variable.- Only Floci-mintable hostnames are mirrored; public registries (docker.io, ghcr.io, …)are never touched.+- Mirrored hostnames carry the port the repository URIs advertise, which is the host port+ when Floci's container publishes `4566` on another one (see [ECR](ecr.md#docker-compose-port-mapping)).- Repository URIs using a non-default `registryId` (account) are not covered.- The mirror set is snapshotted when the cluster is created. Clusters created before thisfeature can be fixed manually because the k3s container filesystem survives a restart:diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/CurrentContainerNetworkResolver.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/CurrentContainerNetworkResolver.javaindex ad2679f6ac..242a61b38b 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/docker/CurrentContainerNetworkResolver.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/CurrentContainerNetworkResolver.java@@ -32,7 +32,7 @@ public class CurrentContainerNetworkResolver {private final DockerClient dockerClient;private final ContainerDetector containerDetector;- private final Map<Integer, Integer> cachedPublishedPorts = new ConcurrentHashMap<>();+ private final Map<Integer, OptionalInt> cachedPublishedPorts = new ConcurrentHashMap<>();private volatile Optional<CurrentContainerNetwork> cachedNetwork;@@ -59,15 +59,14 @@ public class CurrentContainerNetworkResolver {}public OptionalInt resolvePublishedPort(int containerPort) {- Integer cachedPublishedPort = cachedPublishedPorts.get(containerPort);+ OptionalInt cachedPublishedPort = cachedPublishedPorts.get(containerPort);if (cachedPublishedPort != null) {- return OptionalInt.of(cachedPublishedPort);+ return cachedPublishedPort;}OptionalInt resolvedPublishedPort = detectPublishedPort(containerPort);- resolvedPublishedPort.ifPresent(port -> cachedPublishedPorts.putIfAbsent(containerPort, port));- Integer publishedPort = cachedPublishedPorts.get(containerPort);- return publishedPort == null ? OptionalInt.empty() : OptionalInt.of(publishedPort);+ OptionalInt publishedPort = cachedPublishedPorts.putIfAbsent(containerPort, resolvedPublishedPort);+ return publishedPort == null ? resolvedPublishedPort : publishedPort;}Optional<CurrentContainerNetwork> resolve() {@@ -79,6 +78,13 @@ public class CurrentContainerNetworkResolver {return cachedNetwork;}+ /**+ * The host port Floci's own container publishes for {@code containerPort}, empty when it is+ * not published, Floci is not in a container, or the container cannot be inspected. Every+ * result is cached, a failure included, as {@link #resolve()} does: a running container's+ * port bindings never change, and an inspect that fails (no Docker access, no container id)+ * fails the same way on the next call.+ */private OptionalInt detectPublishedPort(int containerPort) {if (!containerDetector.isRunningInContainer()) {return OptionalInt.empty();diff --git a/src/main/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManager.java b/src/main/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManager.javaindex 036bf09ac6..56eb77d387 100644--- a/src/main/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManager.java@@ -45,8 +45,10 @@ import java.util.regex.Pattern;* lazily on first use and reused across restarts.** <p>Methods that compute URIs ({@link #getRepositoryUri}, {@link #getProxyEndpoint})- * do not require Docker — they read the configured port and account/region from- * {@link EmulatorConfig}. Only {@link #ensureStarted()} talks to the daemon.+ * never start the registry. They read the account and region from {@link EmulatorConfig}+ * and the port from {@link #advertisedPort()}, which inspects Floci's own container only+ * when Floci runs in one and falls back to the configured port. Only+ * {@link #ensureStarted()} manages the registry container.*/@ApplicationScopedpublic class EcrRegistryManager {@@ -161,7 +163,7 @@ public class EcrRegistryManager {}private String repositoryUri(String accountId, String region, String repoName, String domain) {- int port = config.port();+ int port = advertisedPort();String style = config.services().ecr().uriStyle();if ("path".equalsIgnoreCase(style)) {return domain + ":" + port + "/" + accountId + "/" + region + "/" + repoName;@@ -187,11 +189,22 @@ public class EcrRegistryManager {? "localhost.floci.io": regionResolver.getAccountId() + ".dkr.ecr."+ regionResolver.getRegion() + ".localhost.floci.io";- return "https://" + host + ":" + config.port();+ return "https://" + host + ":" + advertisedPort();}String scheme = config.services().ecr().tlsEnabled() ? "https" : "http";return scheme + "://" + regionResolver.getAccountId() + ".dkr.ecr."- + regionResolver.getRegion() + ".localhost:" + config.port();+ + regionResolver.getRegion() + ".localhost:" + advertisedPort();+ }++ /**+ * The port in every registry address Floci hands out. The Docker daemon performs login, push+ * and pull, and reaches Floci through the host port Floci's own container publishes for+ * {@code floci.port}. Outside a container, or when that port is not published, it is+ * {@code floci.port} itself.+ */+ public int advertisedPort() {+ int port = config.port();+ return currentContainerNetworkResolver.resolvePublishedPort(port).orElse(port);}/** Returns the effective registry port. Stable across calls once {@link #ensureStarted} runs. */diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.javaindex 5715e9196f..899fd417c5 100644--- a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java@@ -1977,7 +1977,8 @@ public class EksClusterManager}String endpoint = "http://" + dockerHostResolver.resolve() + ":" + config.port();boolean tlsUri = config.services().ecr().tlsUri() && config.tls().enabled();- String content = buildRegistriesYaml(config.defaultAccountId(), regions, config.port(), endpoint, tlsUri);+ String content = buildRegistriesYaml(config.defaultAccountId(), regions,+ ecrRegistryManager.advertisedPort(), endpoint, tlsUri);writeLocalCopy(Paths.get(config.services().eks().dataPath(), "registries", clusterName,"registries.yaml"), content, clusterName);try {@@ -2160,27 +2161,28 @@ public class EksClusterManager/*** Builds the k3s registries.yaml content. One mirror entry per hostname-style repository URI* ({@code <account>.dkr.ecr.<region>.localhost:<port>}) plus one for the path-style form- * ({@code localhost:<port>}), all pointing at Floci's in-network data plane. The TLS URI- * mode adds the corresponding {@code localhost.floci.io} aliases. k3s supports- * no partial wildcards and a {@code "*"} catch-all would also intercept public registries,- * so the hostnames are enumerated explicitly.+ * ({@code localhost:<port>}), all pointing at Floci's in-network data plane. The port is the+ * one the repository URIs advertise, which differs from the endpoint's port when Floci's+ * container publishes its port on another host port. The TLS URI mode adds the corresponding+ * {@code localhost.floci.io} aliases. k3s supports no partial wildcards and a {@code "*"}+ * catch-all would also intercept public registries, so the hostnames are enumerated explicitly.*/- static String buildRegistriesYaml(String accountId, List<String> regions, int dataPlanePort, String endpoint) {- return buildRegistriesYaml(accountId, regions, dataPlanePort, endpoint, false);+ static String buildRegistriesYaml(String accountId, List<String> regions, int advertisedPort, String endpoint) {+ return buildRegistriesYaml(accountId, regions, advertisedPort, endpoint, false);}- static String buildRegistriesYaml(String accountId, List<String> regions, int dataPlanePort,+ static String buildRegistriesYaml(String accountId, List<String> regions, int advertisedPort,String endpoint, boolean tlsUri) {StringBuilder yaml = new StringBuilder("mirrors:\n");for (String region : regions) {- appendMirror(yaml, accountId + ".dkr.ecr." + region + ".localhost:" + dataPlanePort, endpoint);+ appendMirror(yaml, accountId + ".dkr.ecr." + region + ".localhost:" + advertisedPort, endpoint);if (tlsUri) {- appendMirror(yaml, accountId + ".dkr.ecr." + region + ".localhost.floci.io:" + dataPlanePort, endpoint);+ appendMirror(yaml, accountId + ".dkr.ecr." + region + ".localhost.floci.io:" + advertisedPort, endpoint);}}- appendMirror(yaml, "localhost:" + dataPlanePort, endpoint);+ appendMirror(yaml, "localhost:" + advertisedPort, endpoint);if (tlsUri) {- appendMirror(yaml, "localhost.floci.io:" + dataPlanePort, endpoint);+ appendMirror(yaml, "localhost.floci.io:" + advertisedPort, endpoint);}return yaml.toString();}diff --git a/src/test/java/io/github/hectorvent/floci/core/common/docker/CurrentContainerNetworkResolverTest.java b/src/test/java/io/github/hectorvent/floci/core/common/docker/CurrentContainerNetworkResolverTest.javaindex 34f4187aba..49674bf900 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/docker/CurrentContainerNetworkResolverTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/docker/CurrentContainerNetworkResolverTest.java@@ -17,6 +17,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertTrue;import static org.mockito.Mockito.RETURNS_DEEP_STUBS;import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.times;+import static org.mockito.Mockito.verify;import static org.mockito.Mockito.when;class CurrentContainerNetworkResolverTest {@@ -80,7 +82,7 @@ class CurrentContainerNetworkResolverTest {}@Test- void resolvePublishedPort_retriesUntilSuccessfulAndCachesResult() {+ void resolvePublishedPort_skipsUnboundEntryAndCachesResult() {DockerClient dockerClient = mock(DockerClient.class);ContainerDetector containerDetector = mock(ContainerDetector.class);when(containerDetector.isRunningInContainer()).thenReturn(true);@@ -88,7 +90,10 @@ class CurrentContainerNetworkResolverTest {InspectContainerCmd inspectCmd = mock(InspectContainerCmd.class);InspectContainerResponse inspect = mock(InspectContainerResponse.class, RETURNS_DEEP_STUBS);Ports ports = new Ports();- ports.getBindings().put(ExposedPort.tcp(7000), new Ports.Binding[] {null});+ ports.getBindings().put(ExposedPort.tcp(7000), new Ports.Binding[] {+ null,+ Ports.Binding.bindPort(49173)+ });when(dockerClient.inspectContainerCmd("floci-container")).thenReturn(inspectCmd);when(inspectCmd.exec()).thenReturn(inspect);when(inspect.getNetworkSettings().getPorts()).thenReturn(ports);@@ -96,18 +101,56 @@ class CurrentContainerNetworkResolverTest {CurrentContainerNetworkResolver resolver =new TestResolver(dockerClient, containerDetector, "floci-container");- assertTrue(resolver.resolvePublishedPort(7000).isEmpty());-- ports.getBindings().put(ExposedPort.tcp(7000), new Ports.Binding[] {- null,- Ports.Binding.bindPort(49173)- });assertEquals(OptionalInt.of(49173), resolver.resolvePublishedPort(7000));ports.getBindings().put(ExposedPort.tcp(7000), new Ports.Binding[] {Ports.Binding.bindPort(49174)});assertEquals(OptionalInt.of(49173), resolver.resolvePublishedPort(7000));}+ @Test+ void resolvePublishedPort_portNotPublished_cachesTheEmptyResult() {+ DockerClient dockerClient = mock(DockerClient.class);+ ContainerDetector containerDetector = mock(ContainerDetector.class);+ when(containerDetector.isRunningInContainer()).thenReturn(true);+ // EXPOSE 4566 without -p: the port is listed with no host binding.+ Ports ports = new Ports();+ ports.getBindings().put(ExposedPort.tcp(4566), null);+ InspectContainerCmd inspectCmd = inspectReturning(dockerClient, ports);++ CurrentContainerNetworkResolver resolver =+ new TestResolver(dockerClient, containerDetector, "floci-container");++ assertTrue(resolver.resolvePublishedPort(4566).isEmpty());+ assertTrue(resolver.resolvePublishedPort(4566).isEmpty());+ verify(inspectCmd, times(1)).exec();+ }++ @Test+ void resolvePublishedPort_inspectFails_cachesTheEmptyResult() {+ DockerClient dockerClient = mock(DockerClient.class);+ ContainerDetector containerDetector = mock(ContainerDetector.class);+ when(containerDetector.isRunningInContainer()).thenReturn(true);+ InspectContainerCmd inspectCmd = mock(InspectContainerCmd.class);+ when(dockerClient.inspectContainerCmd("floci-container")).thenReturn(inspectCmd);+ when(inspectCmd.exec()).thenThrow(new RuntimeException("Docker daemon unreachable"));++ CurrentContainerNetworkResolver resolver =+ new TestResolver(dockerClient, containerDetector, "floci-container");++ assertTrue(resolver.resolvePublishedPort(4566).isEmpty());+ assertTrue(resolver.resolvePublishedPort(4566).isEmpty());+ verify(inspectCmd, times(1)).exec();+ }++ private static InspectContainerCmd inspectReturning(DockerClient dockerClient, Ports ports) {+ InspectContainerCmd inspectCmd = mock(InspectContainerCmd.class);+ InspectContainerResponse inspect = mock(InspectContainerResponse.class, RETURNS_DEEP_STUBS);+ when(dockerClient.inspectContainerCmd("floci-container")).thenReturn(inspectCmd);+ when(inspectCmd.exec()).thenReturn(inspect);+ when(inspect.getNetworkSettings().getPorts()).thenReturn(ports);+ return inspectCmd;+ }+private static Map<String, ContainerNetwork> networks(String firstName, String firstIp,String secondName, String secondIp) {Map<String, ContainerNetwork> networks = new LinkedHashMap<>();diff --git a/src/test/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManagerTest.javaindex 7fbece1263..35ba7879cf 100644--- a/src/test/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManagerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManagerTest.java@@ -35,6 +35,7 @@ import java.io.Closeable;import java.util.List;import java.util.Map;import java.util.Optional;+import java.util.OptionalInt;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertFalse;@@ -464,6 +465,58 @@ class EcrRegistryManagerTest {verify(regionResolver, never()).getDefaultRegion();}+ @ParameterizedTest+ @CsvSource({+ "hostname, false, 000000000000.dkr.ecr.us-east-1.localhost:54321/app, http://000000000000.dkr.ecr.us-east-1.localhost:54321",+ "path, false, localhost:54321/000000000000/us-east-1/app, http://000000000000.dkr.ecr.us-east-1.localhost:54321",+ "hostname, true, 000000000000.dkr.ecr.us-east-1.localhost.floci.io:54321/app, https://000000000000.dkr.ecr.us-east-1.localhost.floci.io:54321",+ "path, true, localhost.floci.io:54321/000000000000/us-east-1/app, https://localhost.floci.io:54321"+ })+ void advertisedUrisUseTheHostPortTheFlociContainerPublishes(String style, boolean tlsUris,+ String repositoryUri, String proxyEndpoint) {+ EmulatorConfig.TlsConfig tls = Mockito.mock(EmulatorConfig.TlsConfig.class);+ when(config.tls()).thenReturn(tls);+ when(tls.enabled()).thenReturn(tlsUris);+ when(ecr.uriStyle()).thenReturn(style);+ when(ecr.tlsUri()).thenReturn(tlsUris);+ // docker run -p 54321:4566: the Docker daemon logs in, pushes and pulls through the host port.+ when(currentContainerNetworkResolver.resolvePublishedPort(4566)).thenReturn(OptionalInt.of(54321));++ assertEquals(repositoryUri, manager.getRepositoryUri("000000000000", "us-east-1", "app"));+ assertEquals(proxyEndpoint, manager.getProxyEndpoint());+ verify(lifecycleManager, never()).createAndStart(any());+ }++ @ParameterizedTest+ @CsvSource({+ "hostname, 123456789012.dkr.ecr.us-east-1.localhost:54321/backend-user:1",+ "path, localhost:54321/123456789012/us-east-1/backend-user:1"+ })+ void rewriteImageUri_flociPublishedOnAnotherHostPort_pointsTheDaemonAtThePublishedPort(String style,+ String expectedImage) {+ when(ecr.uriStyle()).thenReturn(style);+ when(currentContainerNetworkResolver.resolvePublishedPort(4566)).thenReturn(OptionalInt.of(54321));+ when(lifecycleManager.createAndStart(any())).thenReturn(+ new ContainerLifecycleManager.ContainerInfo("container-id", Map.of()));++ assertEquals(expectedImage, manager.rewriteImageUri(AWS_ECR_IMAGE));+ }++ @Test+ void advertisedUrisKeepFlociPortWhenItIsNotPublishedFromAContainer() {+ when(config.port()).thenReturn(4577);+ when(currentContainerNetworkResolver.resolvePublishedPort(4577)).thenReturn(OptionalInt.empty());+ when(lifecycleManager.createAndStart(any())).thenReturn(+ new ContainerLifecycleManager.ContainerInfo("container-id", Map.of()));++ assertEquals("000000000000.dkr.ecr.us-east-1.localhost:4577/app",+ manager.getRepositoryUri("000000000000", "us-east-1", "app"));+ assertEquals("http://000000000000.dkr.ecr.us-east-1.localhost:4577", manager.getProxyEndpoint());+ assertEquals("123456789012.dkr.ecr.us-east-1.localhost:4577/backend-user:1",+ manager.rewriteImageUri(AWS_ECR_IMAGE));+ verify(currentContainerNetworkResolver, Mockito.atLeastOnce()).resolvePublishedPort(4577);+ }+@Testvoid tlsUriOptInDoesNotReplaceTheLegacyEcrTlsSetting() {when(ecr.tlsEnabled()).thenReturn(true);diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.javaindex 3c3934f77c..db45d4b2af 100644--- a/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java@@ -1060,7 +1060,7 @@ class EksClusterManagerTest {when(dockerClient.copyArchiveToContainerCmd(anyString())).thenReturn(copyCmd);registryManager = Mockito.mock(EcrRegistryManager.class);-+ when(registryManager.advertisedPort()).thenReturn(4566);config = Mockito.mock(EmulatorConfig.class);eks = Mockito.mock(EmulatorConfig.EksServiceConfig.class);ecr = Mockito.mock(EmulatorConfig.EcrServiceConfig.class);@@ -1095,6 +1095,22 @@ class EksClusterManagerTest {assertFalse(yaml.contains("localhost.floci.io"));}+ @Test+ void mirrorsTheAdvertisedRegistryPortToTheInNetworkDataPlane() throws Exception {+ // Floci published as -p 54321:4566: pods name the advertised repository URI, while+ // k3s still reaches Floci on its own port inside the Docker network.+ when(registryManager.advertisedPort()).thenReturn(54321);++ manager.injectEcrRegistryMirror("container-1", "demo");++ String yaml = Files.readString(tempDir.resolve("registries/demo/registries.yaml"));+ assertTrue(yaml.contains("\"000000000000.dkr.ecr.us-east-1.localhost:54321\":"));+ assertTrue(yaml.contains("\"localhost:54321\":"));+ assertFalse(yaml.contains("localhost:4566\":"));+ assertTrue(yaml.contains("- \"http://floci:4566\""));+ verify(copyCmd).exec();+ }+@Testvoid injectsTlsAliasesWhenTlsAndTlsUrisAreEnabled() throws Exception {EmulatorConfig.TlsConfig tls = Mockito.mock(EmulatorConfig.TlsConfig.class);