fix(ecr): enforce immutable image tags (#3009)

Route Docker Distribution traffic through Floci so repository mutability applies to manifest writes. Keep legacy registry storage reachable while moving public ECR endpoints to the guarded listener.
This commit is contained in:
Simone Primarosa authored and GitHub committed 2026-09-14 12:07:40 -04:00
1 parent a03c4d6160
commit 95079965a8
26 files changed
+909 -156

No files matched your search

@@ -196,7 +196,7 @@ class ContainerLifecycleManagerLabelsTest {
private static ContainerSpec specWithLabels(Map<String, String> labels) {
return new ContainerSpec(
"busybox:stable", null, List.of(), null, null, null, Map.of(), List.of(), null,
"busybox:stable", null, List.of(), null, null, null, Map.of(), List.of(), List.of(), null,
List.of(), List.of(), List.of(), labels, null, false, null, List.of(), null,
null, List.of());
}
@@ -8186,7 +8186,7 @@ class CloudFormationIntegrationTest {
"Type": "AWS::Serverless::Function",
"Properties": {
"PackageType": "Image",
"ImageUri": "000000000000.dkr.ecr.us-east-1.localhost:5100/my-repo:latest"
"ImageUri": "000000000000.dkr.ecr.us-east-1.localhost:4566/my-repo:latest"
}
}
}
@@ -8223,7 +8223,7 @@ class CloudFormationIntegrationTest {
.then()
.statusCode(200)
.body("Configuration.PackageType", equalTo("Image"))
.body("Code.ImageUri", equalTo("000000000000.dkr.ecr.us-east-1.localhost:5100/my-repo:latest"));
.body("Code.ImageUri", equalTo("000000000000.dkr.ecr.us-east-1.localhost:4566/my-repo:latest"));
}
@Test
@@ -8240,7 +8240,7 @@ class CloudFormationIntegrationTest {
"Type": "AWS::Serverless::Function",
"Properties": {
"PackageType": "Image",
"ImageUri": "000000000000.dkr.ecr.us-east-1.localhost:5100/my-repo:latest",
"ImageUri": "000000000000.dkr.ecr.us-east-1.localhost:4566/my-repo:latest",
"ImageConfig": {
"EntryPoint": ["/bootstrap"],
"Command": ["handler.main"],
@@ -262,7 +262,7 @@ class SamTransformProcessorTest {
"Type": "AWS::Serverless::Function",
"Properties": {
"PackageType": "Image",
"ImageUri": "000000000000.dkr.ecr.us-east-1.localhost:5100/my-repo:latest"
"ImageUri": "000000000000.dkr.ecr.us-east-1.localhost:4566/my-repo:latest"
}
}
}
@@ -273,7 +273,7 @@ class SamTransformProcessorTest {
JsonNode lambdaProps = expanded.path("Resources").path("MyFunc").path("Properties");
assertEquals("Image", lambdaProps.path("PackageType").asText());
assertEquals("000000000000.dkr.ecr.us-east-1.localhost:5100/my-repo:latest",
assertEquals("000000000000.dkr.ecr.us-east-1.localhost:4566/my-repo:latest",
lambdaProps.path("Code").path("ImageUri").asText());
// No Handler/Runtime were declared and none should be synthesized by the transform itself —
// CloudFormationResourceProvisioner is responsible for not defaulting them once it sees
@@ -337,7 +337,7 @@ class SamTransformProcessorTest {
"Type": "AWS::Serverless::Function",
"Properties": {
"PackageType": "Image",
"ImageUri": "000000000000.dkr.ecr.us-east-1.localhost:5100/my-repo:latest",
"ImageUri": "000000000000.dkr.ecr.us-east-1.localhost:4566/my-repo:latest",
"ImageConfig": {
"EntryPoint": ["/bootstrap"],
"Command": ["handler.main"],
@@ -51,8 +51,8 @@ class EcrServiceTest {
registryManager = Mockito.mock(EcrRegistryManager.class);
when(registryManager.getRepositoryUri(anyString(), anyString(), anyString()))
.thenAnswer(inv -> inv.getArgument(0) + ".dkr.ecr." + inv.getArgument(1)
+ ".localhost:5000/" + inv.getArgument(2));
when(registryManager.getProxyEndpoint()).thenReturn("http://localhost:5000");
+ ".localhost:4566/" + inv.getArgument(2));
when(registryManager.getProxyEndpoint()).thenReturn("http://localhost:4566");
when(registryManager.internalRepoName(anyString(), anyString(), anyString()))
.thenAnswer(inv -> inv.getArgument(0) + "/" + inv.getArgument(1) + "/" + inv.getArgument(2));
// The mock never talks to Docker; report the backing registry as available so the
@@ -117,6 +117,44 @@ class EcrServiceTest {
assertEquals("platform", repo.getTags().get("team"));
}
@Test
void immutableRepositoryIsMarkedForDataPlaneTagProtection() {
service.createRepository(REPO, null, "IMMUTABLE", null, null, null, null, REGION);
assertTrue(service.isImageTagImmutable(REPO, ACCOUNT, REGION));
}
@Test
void mutableRepositoryIsNotMarkedForDataPlaneTagProtection() {
service.createRepository(REPO, null, "MUTABLE", null, null, null, null, REGION);
assertFalse(service.isImageTagImmutable(REPO, ACCOUNT, REGION));
}
@Test
void reconcilePreProxyRegistryNamespacePreservesItsStoragePath() {
String repositoryName = "legacy/service";
service.reconcileFromCatalog(List.of(repositoryName));
Repository repository = service.describeRepositories(List.of(repositoryName), null, REGION).getFirst();
assertEquals(repositoryName, repository.getRepositoryName());
assertEquals(repositoryName, service.registryRepositoryName(repositoryName, ACCOUNT, REGION));
}
@Test
void reconcilePreProxyRepositoryWithNamespaceShapedNamePreservesItsIdentity() {
String repositoryName = ACCOUNT + "/" + REGION + "/legacy";
service.createRepository(repositoryName, null, null, null, null, null, null, REGION);
service.reconcileFromCatalog(List.of(repositoryName));
assertEquals(repositoryName,
service.describeRepositories(List.of(repositoryName), null, REGION).getFirst().getRepositoryName());
assertEquals(repositoryName, service.registryRepositoryName(repositoryName, ACCOUNT, REGION));
assertThrows(AwsException.class, () -> service.describeRepositories(List.of("legacy"), null, REGION));
}
// ------------------------------------------------------------
// Registry unavailable (Floci inside Docker with no Docker daemon)
// ------------------------------------------------------------
@@ -231,7 +269,7 @@ class EcrServiceTest {
service.deleteRepository(REPO, null, true, REGION);
verify(registryManager).deleteRepositoryStorage(ACCOUNT, REGION, REPO);
verify(registryManager).deleteRepositoryStorage(ACCOUNT + "/" + REGION + "/" + REPO);
verify(registryManager).pruneStorage();
}
@@ -248,7 +286,7 @@ class EcrServiceTest {
@Test
void deleteRepository_forceRetainsMetadataWhenRegistryCleanupFails() {
Mockito.doThrow(new IllegalStateException("registry unavailable"))
.when(registryManager).deleteRepositoryStorage(ACCOUNT, REGION, REPO);
.when(registryManager).deleteRepositoryStorage(ACCOUNT + "/" + REGION + "/" + REPO);
service.createRepository(REPO, null, null, null, null, null, null, REGION);
AwsException ex = assertThrows(AwsException.class,
@@ -439,11 +477,12 @@ class EcrServiceTest {
service.reconcileFromCatalog(List.of(
ACCOUNT + "/" + REGION + "/recovered/one",
ACCOUNT + "/" + REGION + "/recovered/two",
"malformed-no-slashes"));
"legacy/repository"));
List<Repository> repos = service.describeRepositories(null, null, REGION);
assertEquals(2, repos.size());
assertEquals(3, repos.size());
assertTrue(repos.stream().anyMatch(r -> "recovered/one".equals(r.getRepositoryName())));
assertTrue(repos.stream().anyMatch(r -> "recovered/two".equals(r.getRepositoryName())));
assertTrue(repos.stream().anyMatch(r -> "legacy/repository".equals(r.getRepositoryName())));
}
@Test
@@ -0,0 +1,209 @@
package io.github.hectorvent.floci.services.ecr.registry;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.services.ecr.EcrService;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.RequestOptions;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
/** Unit tests for routing AWS-shaped ECR repository URIs through Floci's data plane. */
class EcrRegistryDataPlaneTest {
private Vertx vertx;
@BeforeEach
void setUp() {
vertx = Vertx.vertx();
}
@AfterEach
void tearDown() throws Exception {
vertx.close().toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
}
@Test
void hostnameStyleManifestWriteUsesInternalRegistryNamespace() {
var request = EcrRegistryDataPlane.requestFor(
"000000000000.dkr.ecr.us-east-1.localhost:4566",
"/v2/platform/api/manifests/v1", null, "hostname").orElseThrow();
assertEquals("000000000000", request.accountId());
assertEquals("us-east-1", request.region());
assertEquals("platform/api", request.repositoryName());
assertEquals("000000000000/us-east-1/platform/api", request.storageRepositoryName());
assertEquals("v1", request.tag());
assertEquals("/v2/000000000000/us-east-1/platform/api/manifests/v1", request.backendUri());
}
@Test
void digestManifestWriteDoesNotAcquireAnImmutableTagLock() {
var request = EcrRegistryDataPlane.requestFor(
"000000000000.dkr.ecr.us-east-1.localhost:4566",
"/v2/platform/api/manifests/sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
null, "hostname").orElseThrow();
assertNull(request.tag());
}
@Test
void pathStyleRequestRetainsItsExistingInternalNamespace() {
var request = EcrRegistryDataPlane.requestFor("localhost:4566",
"/v2/000000000000/us-east-1/platform/api/blobs/uploads/", "mount=sha256:abc", "path").orElseThrow();
assertEquals("platform/api", request.repositoryName());
assertEquals("/v2/000000000000/us-east-1/platform/api/blobs/uploads/?mount=sha256:abc",
request.backendUri());
}
@Test
void mirrorNamespaceRoutesAnInNetworkK3sPullThroughTheSameDataPlane() {
var request = EcrRegistryDataPlane.requestFor("floci:4566", "/v2/platform/api/manifests/v1",
"ns=000000000000.dkr.ecr.us-east-1.localhost%3A4566", "hostname").orElseThrow();
assertEquals("000000000000", request.accountId());
assertEquals("us-east-1", request.region());
assertEquals("platform/api", request.repositoryName());
assertEquals("/v2/000000000000/us-east-1/platform/api/manifests/v1", request.backendUri());
}
@Test
void unrelatedV2RouteDoesNotClaimTheRequest() {
assertFalse(EcrRegistryDataPlane.requestFor("localhost:4566", "/v2/apis", null, "hostname").isPresent());
assertTrue(EcrRegistryDataPlane.requestFor("000000000000.dkr.ecr.us-east-1.localhost:4566", "/v2/", null,
"hostname").isPresent());
}
@Test
void registryPingDoesNotAcquireARepositoryNamespace() {
var hostnameRequest = EcrRegistryDataPlane.requestFor(
"000000000000.dkr.ecr.us-east-1.localhost:4566", "/v2/", null, "hostname").orElseThrow();
var pathRequest = EcrRegistryDataPlane.requestFor("localhost:4566", "/v2/", null, "path").orElseThrow();
assertEquals("/v2/", hostnameRequest.backendUri());
assertEquals("/v2/", pathRequest.backendUri());
}
@Test
void uploadContinuationRetainsTheClientRepositoryNamespace() {
var request = EcrRegistryDataPlane.requestFor(
"000000000000.dkr.ecr.us-east-1.localhost:4566",
"/v2/platform/api/blobs/uploads/", null, "hostname").orElseThrow();
assertEquals("/v2/platform/api/blobs/uploads/upload-id",
EcrRegistryDataPlane.externalLocation(
"/v2/000000000000/us-east-1/platform/api/blobs/uploads/upload-id", request));
}
@Test
void immutableTagRejectsReplacementBeforeItReachesTheRegistry() throws Exception {
AtomicBoolean tagExists = new AtomicBoolean();
AtomicInteger manifestWrites = new AtomicInteger();
AtomicReference<String> headPath = new AtomicReference<>();
AtomicReference<String> manifestBody = new AtomicReference<>();
AtomicReference<String> manifestPath = new AtomicReference<>();
HttpServer registry = vertx.createHttpServer()
.requestHandler(request -> {
if (request.method() == HttpMethod.HEAD) {
headPath.set(request.path());
request.response().setStatusCode(tagExists.get() ? 200 : 404)
.putHeader("Docker-Content-Digest", "sha256:existing")
.end();
} else if (request.method() == HttpMethod.PUT) {
request.bodyHandler(body -> {
manifestPath.set(request.path());
manifestBody.set(body.toString());
manifestWrites.incrementAndGet();
tagExists.set(true);
request.response().setStatusCode(201).end();
});
} else {
request.response().setStatusCode(405).end();
}
})
.listen(0, "127.0.0.1")
.toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
HttpServer dataPlane = null;
try {
EcrRegistryManager registryManager = Mockito.mock(EcrRegistryManager.class);
EcrService ecrService = Mockito.mock(EcrService.class);
EmulatorConfig config = Mockito.mock(EmulatorConfig.class, Mockito.RETURNS_DEEP_STUBS);
when(config.services().ecr().enabled()).thenReturn(true);
when(config.services().ecr().uriStyle()).thenReturn("hostname");
when(registryManager.httpClient())
.thenReturn(new RegistryHttpClient("http://127.0.0.1:" + registry.actualPort()));
when(ecrService.isImageTagImmutable("platform/api", "000000000000", "us-east-1"))
.thenReturn(true);
when(ecrService.registryRepositoryName("platform/api", "000000000000", "us-east-1"))
.thenReturn("legacy/platform-api");
Router router = Router.router(vertx);
new EcrRegistryDataPlane(registryManager, ecrService, config, vertx).register(router);
dataPlane = vertx.createHttpServer().requestHandler(router)
.listen(0, "127.0.0.1")
.toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
assertEquals(201, putManifest(dataPlane.actualPort()).statusCode());
var rejected = putManifest(dataPlane.actualPort());
assertEquals(400, rejected.statusCode());
String responseBody = rejected.body()
.toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS).toString();
JsonObject json = new JsonObject(responseBody);
JsonArray errors = json.getJsonArray("errors");
assertNotNull(errors);
assertEquals(1, errors.size());
JsonObject error = errors.getJsonObject(0);
assertEquals("TAG_INVALID", error.getString("code"));
assertEquals("The image tag v1 already exists in the platform/api repository and cannot be overwritten because the tag is immutable.",
error.getString("message"));
assertEquals(1, manifestWrites.get());
assertEquals("/v2/legacy/platform-api/manifests/v1", headPath.get());
assertEquals("/v2/legacy/platform-api/manifests/v1", manifestPath.get());
assertEquals("{}", manifestBody.get());
} finally {
if (dataPlane != null) {
dataPlane.close().toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
}
registry.close().toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
}
}
private io.vertx.core.http.HttpClientResponse putManifest(int port) throws Exception {
HttpClient client = vertx.createHttpClient();
return client.request(new RequestOptions()
.setHost("127.0.0.1")
.setPort(port)
.setMethod(HttpMethod.PUT)
.setURI("/v2/platform/api/manifests/v1"))
.compose(request -> {
request.putHeader("Host", "000000000000.dkr.ecr.us-east-1.localhost:4566");
request.setChunked(true);
return request.send(Buffer.buffer("{}"));
})
.toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
}
}
@@ -22,6 +22,7 @@ import com.github.dockerjava.api.command.InspectImageResponse;
import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.ContainerPort;
import com.github.dockerjava.api.model.Frame;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -38,7 +39,9 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -99,6 +102,7 @@ class EcrRegistryManagerTest {
storage = Mockito.mock(EmulatorConfig.StorageConfig.class);
when(config.services()).thenReturn(Mockito.mock(EmulatorConfig.ServicesConfig.class));
when(config.services().ecr()).thenReturn(ecr);
when(config.port()).thenReturn(4566);
when(config.docker()).thenReturn(docker);
when(config.storage()).thenReturn(storage);
when(docker.resourceNamespace()).thenReturn(Optional.empty());
@@ -130,6 +134,16 @@ class EcrRegistryManagerTest {
"io.floci.region", "us-east-1"));
}
@Test
void ensureStartedBindsTheBackingRegistryToLoopbackOnly() {
when(lifecycleManager.createAndStart(any())).thenReturn(
new ContainerLifecycleManager.ContainerInfo("container-id", Map.of()));
manager.ensureStarted();
verify(builder).withLoopbackPortBinding(eq(5000), anyInt());
}
@Test
void ensureStarted_releasesPortWhenDockerStartFails_soPoolIsNotExhausted() {
when(lifecycleManager.createAndStart(any()))
@@ -186,7 +200,7 @@ class EcrRegistryManagerTest {
assertTrue(manager.tryEnsureStarted());
verify(lifecycleManager, Mockito.times(2)).createAndStart(any());
verify(builder, Mockito.times(2)).withPortBinding(5000, BASE_PORT);
verify(builder, Mockito.times(2)).withLoopbackPortBinding(5000, BASE_PORT);
verify(firstLogStream).close();
}
@@ -198,6 +212,9 @@ class EcrRegistryManagerTest {
Container existing = Mockito.mock(Container.class);
when(existing.getId()).thenReturn("container-id");
when(existing.getPorts()).thenReturn(new ContainerPort[] {
new ContainerPort().withIp("127.0.0.1").withPrivatePort(5000).withPublicPort(BASE_PORT)
});
when(lifecycleManager.presenceOf("container-id")).thenReturn(ContainerPresence.STOPPED);
when(lifecycleManager.findByName(REGISTRY_NAME)).thenReturn(Optional.of(existing));
when(lifecycleManager.adopt("container-id", List.of(5000)))
@@ -232,13 +249,15 @@ class EcrRegistryManagerTest {
}
@Test
void adoptUsesPublishedHostPortEvenWhenRunningInsideDocker() {
// Regression: in container mode adopt()'s endpoint resolves to the registry's
// internal port (5000); the advertised proxy endpoint must use the published
// host binding instead, or docker login from the host daemon fails.
void adoptTracksPrivateBackingPortWhenRunningInsideDocker() {
// In container mode httpClient() uses the registry's in-network port. The adopted
// published binding remains the host-mode fallback for control-plane image operations.
when(containerDetector.isRunningInContainer()).thenReturn(true);
Container existing = Mockito.mock(Container.class);
when(existing.getId()).thenReturn("0123456789abcdef");
when(existing.getPorts()).thenReturn(new ContainerPort[] {
new ContainerPort().withIp("127.0.0.1").withPrivatePort(5000).withPublicPort(BASE_PORT + 1)
});
when(lifecycleManager.findByName(REGISTRY_NAME)).thenReturn(Optional.of(existing));
when(lifecycleManager.adopt("0123456789abcdef", List.of(5000)))
.thenReturn(new ContainerLifecycleManager.ContainerInfo("0123456789abcdef",
@@ -248,13 +267,16 @@ class EcrRegistryManagerTest {
manager.ensureStarted();
assertEquals(BASE_PORT + 1, manager.effectivePort());
assertEquals("http://localhost:" + (BASE_PORT + 1), manager.getProxyEndpoint());
assertEquals("http://000000000000.dkr.ecr.us-east-1.localhost:4566", manager.getProxyEndpoint());
}
@Test
void adoptKeepsConfiguredPortWhenNoPublishedBindingExists() {
Container existing = Mockito.mock(Container.class);
when(existing.getId()).thenReturn("0123456789abcdef");
when(existing.getPorts()).thenReturn(new ContainerPort[] {
new ContainerPort().withIp("127.0.0.1").withPrivatePort(5000).withPublicPort(BASE_PORT)
});
when(lifecycleManager.findByName(REGISTRY_NAME)).thenReturn(Optional.of(existing));
when(lifecycleManager.adopt("0123456789abcdef", List.of(5000)))
.thenReturn(new ContainerLifecycleManager.ContainerInfo("0123456789abcdef", Map.of()));
@@ -264,6 +286,23 @@ class EcrRegistryManagerTest {
assertEquals(BASE_PORT, manager.effectivePort());
}
@Test
void legacyPublicBackingRegistryIsRecreatedWithLoopbackBinding() {
Container existing = Mockito.mock(Container.class);
when(existing.getId()).thenReturn("0123456789abcdef");
when(existing.getPorts()).thenReturn(new ContainerPort[] {
new ContainerPort().withIp("0.0.0.0").withPrivatePort(5000).withPublicPort(BASE_PORT)
});
when(lifecycleManager.findByName(REGISTRY_NAME)).thenReturn(Optional.of(existing));
when(lifecycleManager.createAndStart(any())).thenReturn(
new ContainerLifecycleManager.ContainerInfo("container-id", Map.of()));
manager.ensureStarted();
verify(lifecycleManager).stopAndRemove("0123456789abcdef", null);
verify(builder).withLoopbackPortBinding(eq(5000), anyInt());
}
@Test
void httpClient_usesNamespacedRegistryContainerDnsWhenConfigured() {
when(containerDetector.isRunningInContainer()).thenReturn(true);
@@ -279,7 +318,7 @@ class EcrRegistryManagerTest {
String rewritten = manager.rewriteImageUri("123456789012.dkr.ecr.us-east-1.amazonaws.com/backend-user:1");
assertEquals("123456789012.dkr.ecr.us-east-1.localhost:" + BASE_PORT + "/backend-user:1", rewritten);
assertEquals("123456789012.dkr.ecr.us-east-1.localhost:4566/backend-user:1", rewritten);
verify(lifecycleManager).createAndStart(any());
}
@@ -291,7 +330,7 @@ class EcrRegistryManagerTest {
String rewritten = manager.rewriteImageUri("123456789012.dkr.ecr.us-east-1.amazonaws.com/backend-user:1");
assertEquals("localhost:" + BASE_PORT + "/123456789012/us-east-1/backend-user:1", rewritten);
assertEquals("localhost:4566/123456789012/us-east-1/backend-user:1", rewritten);
}
@Test
@@ -327,7 +366,7 @@ class EcrRegistryManagerTest {
String rewritten = manager.rewriteImageUri(AWS_ECR_IMAGE);
assertEquals("123456789012.dkr.ecr.us-east-1.localhost:" + BASE_PORT + "/backend-user:1", rewritten);
assertEquals("123456789012.dkr.ecr.us-east-1.localhost:4566/backend-user:1", rewritten);
verify(dockerClient, Mockito.never()).inspectImageCmd(anyString());
}
@@ -357,7 +396,7 @@ class EcrRegistryManagerTest {
String rewritten = manager.rewriteImageUri(AWS_ECR_IMAGE);
assertEquals("123456789012.dkr.ecr.us-east-1.localhost:" + BASE_PORT + "/backend-user:1", rewritten);
assertEquals("123456789012.dkr.ecr.us-east-1.localhost:4566/backend-user:1", rewritten);
verify(dockerClient).inspectImageCmd(mirrored);
verify(lifecycleManager).createAndStart(any());
}
@@ -370,7 +409,7 @@ class EcrRegistryManagerTest {
String rewritten = manager.rewriteImageUri(AWS_ECR_IMAGE);
assertEquals("123456789012.dkr.ecr.us-east-1.localhost:" + BASE_PORT + "/backend-user:1", rewritten);
assertEquals("123456789012.dkr.ecr.us-east-1.localhost:4566/backend-user:1", rewritten);
verify(lifecycleManager).createAndStart(any());
}
@@ -48,7 +48,7 @@ import static org.mockito.Mockito.when;
class EcsContainerManagerEcrRewriteTest {
private static final String ECR_IMAGE = "123456789012.dkr.ecr.us-east-1.amazonaws.com/backend-user:1";
private static final String REWRITTEN_IMAGE = "123456789012.dkr.ecr.us-east-1.localhost:5100/backend-user:1";
private static final String REWRITTEN_IMAGE = "123456789012.dkr.ecr.us-east-1.localhost:4566/backend-user:1";
private ContainerBuilder containerBuilder;
private ContainerLifecycleManager lifecycleManager;
@@ -94,28 +94,28 @@ class EksClusterManagerTest {
@Test
void registriesYamlMirrorsEveryRegionHostnameAndThePathStyleForm() {
String yaml = EksClusterManager.buildRegistriesYaml(
"000000000000", AwsRegions.ALL, 5100, "http://floci-ecr-registry:5000");
"000000000000", AwsRegions.ALL, 4566, "http://floci:4566");
assertTrue(yaml.startsWith("mirrors:\n"));
for (String region : AwsRegions.ALL) {
assertTrue(yaml.contains("\"000000000000.dkr.ecr." + region + ".localhost:5100\":"),
assertTrue(yaml.contains("\"000000000000.dkr.ecr." + region + ".localhost:4566\":"),
"should mirror the " + region + " hostname");
}
assertTrue(yaml.contains("\"localhost:5100\":"), "should mirror the path-style form");
assertTrue(yaml.contains("\"localhost:4566\":"), "should mirror the path-style form");
assertFalse(yaml.contains("\"*\""), "must not catch-all public registries");
long endpoints = yaml.lines().filter(l -> l.contains("- \"http://floci-ecr-registry:5000\"")).count();
long endpoints = yaml.lines().filter(l -> l.contains("- \"http://floci:4566\"")).count();
assertEquals(AwsRegions.ALL.size() + 1, endpoints,
"every mirror should point at the registry's in-network endpoint");
"every mirror should point at Floci's in-network data plane");
}
@Test
void registriesYamlUsesTheActualRegistryPortAndEndpoint() {
String yaml = EksClusterManager.buildRegistriesYaml(
"111122223333", List.of("eu-central-1"), 5142, "http://my-registry:5000");
"111122223333", List.of("eu-central-1"), 4566, "http://floci:4566");
assertTrue(yaml.contains("\"111122223333.dkr.ecr.eu-central-1.localhost:5142\":"));
assertTrue(yaml.contains("\"localhost:5142\":"));
assertTrue(yaml.contains("- \"http://my-registry:5000\""));
assertTrue(yaml.contains("\"111122223333.dkr.ecr.eu-central-1.localhost:4566\":"));
assertTrue(yaml.contains("\"localhost:4566\":"));
assertTrue(yaml.contains("- \"http://floci:4566\""));
}
@Test
@@ -528,8 +528,6 @@ class EksClusterManagerTest {
when(dockerClient.copyArchiveToContainerCmd(anyString())).thenReturn(copyCmd);
registryManager = Mockito.mock(EcrRegistryManager.class);
when(registryManager.effectivePort()).thenReturn(5100);
when(registryManager.internalEndpoint()).thenReturn("http://floci-ecr-registry:5000");
EmulatorConfig config = Mockito.mock(EmulatorConfig.class);
eks = Mockito.mock(EmulatorConfig.EksServiceConfig.class);
@@ -537,16 +535,19 @@ class EksClusterManagerTest {
when(config.services()).thenReturn(Mockito.mock(EmulatorConfig.ServicesConfig.class));
when(config.services().eks()).thenReturn(eks);
when(config.services().ecr()).thenReturn(ecr);
when(config.port()).thenReturn(4566);
when(config.defaultRegion()).thenReturn("us-east-1");
when(config.defaultAccountId()).thenReturn("000000000000");
when(eks.ecrRegistryMirror()).thenReturn(true);
when(eks.dataPath()).thenReturn(tempDir.toString());
when(ecr.enabled()).thenReturn(true);
DockerHostResolver dockerHostResolver = Mockito.mock(DockerHostResolver.class);
when(dockerHostResolver.resolve()).thenReturn("floci");
manager = new EksClusterManager(
Mockito.mock(ContainerBuilder.class), lifecycleManager,
Mockito.mock(ContainerDetector.class), Mockito.mock(PortAllocator.class),
Mockito.mock(DockerHostResolver.class), registryManager, config,
dockerHostResolver, registryManager, config,
Mockito.mock(RegionResolver.class));
}
@@ -623,13 +623,13 @@ class ContainerLauncherTest {
fn.setImageUri("123456789012.dkr.ecr.us-east-1.amazonaws.com/backend-user:1");
when(ecrRegistryManager.rewriteImageUri("123456789012.dkr.ecr.us-east-1.amazonaws.com/backend-user:1"))
.thenReturn("123456789012.dkr.ecr.us-east-1.localhost:5100/backend-user:1");
.thenReturn("123456789012.dkr.ecr.us-east-1.localhost:4566/backend-user:1");
launcher.launch(fn);
ContainerSpec spec = captureRealContainerSpec();
verify(ecrRegistryManager).rewriteImageUri("123456789012.dkr.ecr.us-east-1.amazonaws.com/backend-user:1");
assertEquals("123456789012.dkr.ecr.us-east-1.localhost:5100/backend-user:1",
assertEquals("123456789012.dkr.ecr.us-east-1.localhost:4566/backend-user:1",
spec.image());
}
@@ -641,13 +641,13 @@ class ContainerLauncherTest {
fn.setImageUri("123456789012.dkr.ecr.us-east-1.amazonaws.com/backend-user:1");
when(ecrRegistryManager.rewriteImageUri("123456789012.dkr.ecr.us-east-1.amazonaws.com/backend-user:1"))
.thenReturn("localhost:5100/123456789012/us-east-1/backend-user:1");
.thenReturn("localhost:4566/123456789012/us-east-1/backend-user:1");
launcher.launch(fn);
ContainerSpec spec = captureRealContainerSpec();
verify(ecrRegistryManager).rewriteImageUri("123456789012.dkr.ecr.us-east-1.amazonaws.com/backend-user:1");
assertEquals("localhost:5100/123456789012/us-east-1/backend-user:1",
assertEquals("localhost:4566/123456789012/us-east-1/backend-user:1",
spec.image());
}
@@ -549,7 +549,7 @@ class S3VirtualHostFilterTest {
@CsvSource({
// ECR registry, as EcrRegistryManager emits it
"123456789012.dkr.ecr.us-east-1.localhost, localhost",
"123456789012.dkr.ecr.us-east-1.localhost:5100, localhost",
"123456789012.dkr.ecr.us-east-1.localhost:4566, localhost",
"123456789012.dkr.ecr.eu-central-1.localhost.floci.io, localhost",
// OpenSearch / Elasticsearch domain endpoints
"search-mydomain-abc.us-east-1.es.localhost, localhost",