From c1cd5af2bdc6c0c77b654f1cfb9cd9a8bb1237df Mon Sep 17 00:00:00 2001 From: Mario Toffia Date: Fri, 9 Oct 2026 02:11:30 +0200 Subject: [PATCH] fix(ecr): advertise the registry at the published address (#4863) ECR built repositoryUri, the GetAuthorizationToken proxyEndpoint and the Lambda and ECS image rewrites from floci.port, the port Floci listens on inside its own container. The Docker daemon performs login, push and pull, and reaches Floci through the host port that container publishes. When Floci ran as docker run -p 54321:4566, every advertised address named port 4566 on the host, where nothing answers, so docker login, CDK container asset publishing and ECS or Lambda image pulls failed. EcrRegistryManager now has advertisedPort(): the host port Floci's container publishes for floci.port, read through the existing CurrentContainerNetworkResolver.resolvePublishedPort as RDS does for its proxy endpoint, or floci.port when Floci is not in a container or the port is not published. repositoryUri (hostname and path style, plain and TLS URI mode), proxyEndpoint and rewriteImageUri all use it. The EKS registry mirror keys must match the URIs pods name, so they use the same advertised port. The mirror endpoint stays Floci's in-network address on floci.port. The resolver now caches every published-port answer, "not published" and a failed inspect included, as it already caches the network: a running container's port bindings cannot change, and an inspect that fails fails the same way next time. ECR requests therefore do not inspect the container each time. --- docs/services/ecr.md | 2 + docs/services/eks.md | 2 + .../CurrentContainerNetworkResolver.java | 18 ++++-- .../ecr/registry/EcrRegistryManager.java | 23 ++++++-- .../floci/services/eks/EksClusterManager.java | 26 ++++---- .../CurrentContainerNetworkResolverTest.java | 59 ++++++++++++++++--- .../ecr/registry/EcrRegistryManagerTest.java | 53 +++++++++++++++++ .../services/eks/EksClusterManagerTest.java | 18 +++++- 8 files changed, 169 insertions(+), 32 deletions(-) diff --git a/docs/services/ecr.md b/docs/services/ecr.md index 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 ```bash diff --git a/docs/services/eks.md b/docs/services/eks.md index 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 this feature 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.java index 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 cachedPublishedPorts = new ConcurrentHashMap<>(); + private final Map cachedPublishedPorts = new ConcurrentHashMap<>(); private volatile Optional 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 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.java index 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. * *

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. */ @ApplicationScoped public 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.java index 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 .dkr.ecr..localhost:}) plus one for the path-style form - * ({@code localhost:}), 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:}), 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 regions, int dataPlanePort, String endpoint) { - return buildRegistriesYaml(accountId, regions, dataPlanePort, endpoint, false); + static String buildRegistriesYaml(String accountId, List regions, int advertisedPort, String endpoint) { + return buildRegistriesYaml(accountId, regions, advertisedPort, endpoint, false); } - static String buildRegistriesYaml(String accountId, List regions, int dataPlanePort, + static String buildRegistriesYaml(String accountId, List 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.java index 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 networks(String firstName, String firstIp, String secondName, String secondIp) { Map 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.java index 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); + } + @Test void 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.java index 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(); + } + @Test void injectsTlsAliasesWhenTlsAndTlsUrisAreEnabled() throws Exception { EmulatorConfig.TlsConfig tls = Mockito.mock(EmulatorConfig.TlsConfig.class);