fix(ecr): pull rewritten ECS and Lambda images from localhost (#5882)

Floci rewrites an AWS-shaped ECR image of an ECS task or an image-backed
Lambda to its loopback registry and hands that address to the Docker
daemon, as <account>.dkr.ecr.<region>.localhost:<port>/<repo> in the
hostname style. A daemon with the containerd image store, the default
from Docker Engine 29, cannot pull from it. Measured on Docker 29.9.1:
against a plain HTTP registry:2, localhost:<port> and 127.0.0.1:<port>
are reached over HTTP, while any *.localhost name is tried over HTTPS
only ("server gave HTTP response to HTTPS client"), the 127.0.0.0/8
insecure CIDR notwithstanding. With Floci's TLS on, the same names reach
Floci's TLS port and fail on its certificate instead (unknown authority,
and *.localhost names are not in it). The containers never start: ECS
reports TaskFailedToStart.

So the rewrite now always gives localhost:<port>/<account>/<region>/<repo>,
path style, in either URI style and with or without TLS: the address
every daemon, classic or containerd store, pulls from over plain HTTP.
The data plane claims path-style requests on a loopback host (localhost,
127.0.0.0/8, ::1) in the hostname style too, so those pulls still go
through it and its repository name mapping. Nothing else changes: the
advertised repositoryUri, the login endpoint and pushes keep their style.
This commit is contained in:
Alessandro Lattao authored and GitHub committed 2026-10-17 14:11:08 -04:00
1 parent 720f95bebe
commit 553411bc70
5 files changed
+59 -15

No files matched your search

@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.ecr.registry;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.config.NetworkExposureGuard;
import io.github.hectorvent.floci.services.ecr.EcrService;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
@@ -10,6 +11,7 @@ import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.RequestOptions;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.HostAndPort;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import jakarta.enterprise.context.ApplicationScoped;
@@ -245,7 +247,9 @@ public class EcrRegistryDataPlane {
storageRepositoryName, repository, backendUri,
tag(path)));
}
if (!"path".equalsIgnoreCase(uriStyle)) {
// A loopback host is the address Floci itself hands the Docker daemon for the ECS and
// Lambda images it pulls, path style in either URI style (see rewriteImageUri).
if (!"path".equalsIgnoreCase(uriStyle) && !isLoopbackHost(host)) {
return Optional.empty();
}
@@ -265,6 +269,11 @@ public class EcrRegistryDataPlane {
registryPath + querySuffix(query, true), tag(registryPath)));
}
private static boolean isLoopbackHost(String host) {
HostAndPort authority = host == null ? null : HostAndPort.parseAuthority(host, -1);
return authority != null && NetworkExposureGuard.isLoopback(authority.host());
}
private static String querySuffix(String query, boolean removeNamespace) {
if (query == null || query.isEmpty()) {
return "";
@@ -139,7 +139,12 @@ public class EcrRegistryManager {
String region = m.group(2);
String repoAndTag = m.group(3);
ensureStarted();
String rewritten = repositoryUri(account, region, repoAndTag, "localhost");
// Path style on localhost whatever style the API advertises. A daemon with the
// containerd image store, the default from Docker Engine 29, reaches only localhost and
// 127.0.0.1 over plain HTTP and takes every other name, *.localhost included, for an
// HTTPS registry: Floci's plain port cannot answer that, and its TLS port cannot prove
// itself to a daemon that does not trust Floci's CA.
String rewritten = "localhost:" + advertisedPort() + "/" + internalRepoName(account, region, repoAndTag);
LOG.infov("Rewriting ECR image URI {0} -> {1}", image, rewritten);
return rewritten;
}