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 de445114af..5c69c22752 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 @@ -160,7 +160,14 @@ public class EcrRegistryManager { return config.services().ecr().tlsUri() && config.tls().enabled(); } - /** Returns the proxy endpoint a docker daemon should log into for any ECR repo. */ + /** + * Returns the proxy endpoint a docker daemon should log into for any ECR repo. + * + *

An ECR registry is regional, so the region in the endpoint is the region of the + * calling request, matching the one in the {@link #getRepositoryUri} of every repository + * in that registry. A client logs in to this endpoint and then pushes to those URIs, so + * the two must name the same host. + */ public String getProxyEndpoint() { if (tlsUriEnabled()) { String host = "path".equalsIgnoreCase(config.services().ecr().uriStyle()) @@ -171,7 +178,7 @@ public class EcrRegistryManager { } String scheme = config.services().ecr().tlsEnabled() ? "https" : "http"; return scheme + "://" + regionResolver.getAccountId() + ".dkr.ecr." - + regionResolver.getDefaultRegion() + ".localhost:" + config.port(); + + regionResolver.getRegion() + ".localhost:" + config.port(); } /** Returns the effective registry port. Stable across calls once {@link #ensureStarted} runs. */ diff --git a/src/test/java/io/github/hectorvent/floci/services/ecr/EcrIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ecr/EcrIntegrationTest.java index 43287183dd..86c1bafd14 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ecr/EcrIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ecr/EcrIntegrationTest.java @@ -9,10 +9,12 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestMethodOrder; +import java.net.URI; import java.util.Base64; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.*; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * In-tree control-plane integration test for ECR. Does not require Docker — @@ -27,6 +29,9 @@ class EcrIntegrationTest { private static final String CT = "application/x-amz-json-1.1"; private static final String PREFIX = "AmazonEC2ContainerRegistry_V20150921."; private static final String REPO = "floci-it/integration"; + private static final String WEST_REGION = "us-west-2"; + private static final String WEST_CREDENTIAL = + "AWS4-HMAC-SHA256 Credential=000000000000/20260923/" + WEST_REGION + "/ecr/aws4_request"; @BeforeAll static void configureRestAssured() { @@ -202,4 +207,57 @@ class EcrIntegrationTest { .statusCode(400) .body("__type", equalTo("RepositoryNotFoundException")); } + + /** + * An ECR registry is regional: the proxyEndpoint a client is told to docker login to must + * name the same host the repositoryUri pushes to, so both have to carry the region of the + * call rather than the emulator's configured default. + */ + @Test + @Order(10) + void getAuthorizationToken_nonDefaultRegionRequest_returnsProxyEndpointForThatRegion() { + String repository = "floci-it/region-scoped"; + String repositoryUri = given() + .header("X-Amz-Target", PREFIX + "CreateRepository") + .header("Authorization", WEST_CREDENTIAL) + .contentType(CT) + .body(""" + { "repositoryName": "%s" } + """.formatted(repository)) + .when() + .post("/") + .then() + .statusCode(200) + .body("repository.repositoryUri", containsString(".dkr.ecr." + WEST_REGION + ".")) + .extract().jsonPath().getString("repository.repositoryUri"); + + String proxyEndpoint = given() + .header("X-Amz-Target", PREFIX + "GetAuthorizationToken") + .header("Authorization", WEST_CREDENTIAL) + .contentType(CT) + .body("{}") + .when() + .post("/") + .then() + .statusCode(200) + .body("authorizationData[0].proxyEndpoint", containsString(".dkr.ecr." + WEST_REGION + ".")) + .extract().jsonPath().getString("authorizationData[0].proxyEndpoint"); + + assertEquals( + repositoryUri.substring(0, repositoryUri.indexOf('/')), + URI.create(proxyEndpoint).getAuthority(), + "docker login target must be the registry host the repository URI pushes to"); + + given() + .header("X-Amz-Target", PREFIX + "DeleteRepository") + .header("Authorization", WEST_CREDENTIAL) + .contentType(CT) + .body(""" + { "repositoryName": "%s", "force": true } + """.formatted(repository)) + .when() + .post("/") + .then() + .statusCode(200); + } } 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 80d2c3f2ac..2078c6ccfa 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 @@ -73,6 +73,7 @@ class EcrRegistryManagerTest { private ContainerBuilder.Builder builder; private DockerClient dockerClient; private InspectImageCmd inspectImage; + private RegionResolver regionResolver; private EcrRegistryManager manager; @BeforeEach @@ -98,7 +99,7 @@ class EcrRegistryManagerTest { when(logStreamer.generateLogStreamName(anyString())).thenReturn("registry-log-stream"); containerDetector = Mockito.mock(ContainerDetector.class); currentContainerNetworkResolver = Mockito.mock(CurrentContainerNetworkResolver.class); - RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000"); + regionResolver = Mockito.spy(new RegionResolver("us-east-1", "000000000000")); config = Mockito.mock(EmulatorConfig.class); ecr = Mockito.mock(EmulatorConfig.EcrServiceConfig.class); @@ -399,6 +400,27 @@ class EcrRegistryManagerTest { assertEquals(expectedImage, manager.rewriteImageUri(AWS_ECR_IMAGE)); } + @ParameterizedTest + @CsvSource({ + "false, http://000000000000.dkr.ecr.eu-central-1.localhost:4566", + "true, https://000000000000.dkr.ecr.eu-central-1.localhost.floci.io:4566" + }) + void getProxyEndpoint_nonDefaultRequestRegion_followsTheRequestRegion(boolean tlsUris, + String proxyEndpoint) { + EmulatorConfig.TlsConfig tls = Mockito.mock(EmulatorConfig.TlsConfig.class); + when(config.tls()).thenReturn(tls); + when(tls.enabled()).thenReturn(tlsUris); + when(ecr.uriStyle()).thenReturn("hostname"); + when(ecr.tlsUri()).thenReturn(tlsUris); + Mockito.doReturn("eu-central-1").when(regionResolver).getRegion(); + + // A docker login target that named another region than the push target would be unusable. + assertEquals(proxyEndpoint, manager.getProxyEndpoint()); + assertTrue(manager.getRepositoryUri("000000000000", "eu-central-1", "app") + .startsWith(proxyEndpoint.substring(proxyEndpoint.indexOf("://") + 3) + "/")); + verify(regionResolver, never()).getDefaultRegion(); + } + @Test void tlsUriOptInDoesNotReplaceTheLegacyEcrTlsSetting() { when(ecr.tlsEnabled()).thenReturn(true);