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.
This commit is contained in:
Mario Toffia authored and GitHub committed 2026-10-08 20:11:30 -04:00
1 parent e22bd2c59a
commit c1cd5af2bd
8 files changed
+169 -32

No files matched your search

@@ -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<>();
@@ -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);
@@ -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);