feat(ecr): manage pull through cache rules (#4304)

Signed-off-by: XuJian <265865031+xujiantop-crypto@users.noreply.github.com>
This commit is contained in:
XuJian authored and GitHub committed 2026-09-24 08:36:06 -04:00
1 parent 34be03b334
commit 1952ba4052
7 files changed
+695 -2

No files matched your search

@@ -29,6 +29,8 @@ 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 CACHE_PREFIX = "docker-hub";
private static final String SECOND_CACHE_PREFIX = "kubernetes";
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";
@@ -260,4 +262,149 @@ class EcrIntegrationTest {
.then()
.statusCode(200);
}
@Test
@Order(11)
void createPullThroughCacheRule() {
given()
.header("X-Amz-Target", PREFIX + "CreatePullThroughCacheRule")
.contentType(CT)
.body("""
{
"ecrRepositoryPrefix": "%s/",
"upstreamRegistryUrl": "registry-1.docker.io",
"credentialArn": "arn:aws:secretsmanager:us-east-1:000000000000:secret:ecr-pullthroughcache/docker-hub",
"upstreamRepositoryPrefix": "library"
}
""".formatted(CACHE_PREFIX))
.when()
.post("/")
.then()
.statusCode(200)
.body("ecrRepositoryPrefix", equalTo(CACHE_PREFIX))
.body("upstreamRegistryUrl", equalTo("registry-1.docker.io"))
.body("upstreamRegistry", equalTo("docker-hub"))
.body("upstreamRepositoryPrefix", equalTo("library"))
.body("registryId", equalTo("000000000000"))
.body("createdAt", notNullValue());
given()
.header("X-Amz-Target", PREFIX + "CreatePullThroughCacheRule")
.contentType(CT)
.body("""
{
"ecrRepositoryPrefix": "%s",
"upstreamRegistryUrl": "registry.k8s.io"
}
""".formatted(SECOND_CACHE_PREFIX))
.when()
.post("/")
.then()
.statusCode(200)
.body("upstreamRegistry", equalTo("k8s"))
.body("upstreamRepositoryPrefix", equalTo("ROOT"));
}
@Test
@Order(12)
void describePullThroughCacheRulesFiltersAndPaginates() {
String nextToken = given()
.header("X-Amz-Target", PREFIX + "DescribePullThroughCacheRules")
.contentType(CT)
.body("{ \"maxResults\": 1 }")
.when()
.post("/")
.then()
.statusCode(200)
.body("pullThroughCacheRules", hasSize(1))
.body("pullThroughCacheRules[0].createdAt", notNullValue())
.body("pullThroughCacheRules[0].updatedAt", notNullValue())
.body("nextToken", not(emptyString()))
.extract().jsonPath().getString("nextToken");
given()
.header("X-Amz-Target", PREFIX + "DescribePullThroughCacheRules")
.contentType(CT)
.body("""
{ "maxResults": 1, "nextToken": "%s" }
""".formatted(nextToken))
.when()
.post("/")
.then()
.statusCode(200)
.body("pullThroughCacheRules", hasSize(1));
given()
.header("X-Amz-Target", PREFIX + "DescribePullThroughCacheRules")
.contentType(CT)
.body("""
{ "ecrRepositoryPrefixes": ["%s/"] }
""".formatted(CACHE_PREFIX))
.when()
.post("/")
.then()
.statusCode(200)
.body("pullThroughCacheRules", hasSize(1))
.body("pullThroughCacheRules[0].ecrRepositoryPrefix", equalTo(CACHE_PREFIX));
}
@Test
@Order(13)
void createPullThroughCacheRuleDuplicateFails() {
given()
.header("X-Amz-Target", PREFIX + "CreatePullThroughCacheRule")
.contentType(CT)
.body("""
{
"ecrRepositoryPrefix": "%s",
"upstreamRegistryUrl": "registry-1.docker.io"
}
""".formatted(CACHE_PREFIX))
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("PullThroughCacheRuleAlreadyExistsException"));
given()
.header("X-Amz-Target", PREFIX + "DescribePullThroughCacheRules")
.contentType(CT)
.body("{ \"ecrRepositoryPrefixes\": [] }")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("InvalidParameterException"));
}
@Test
@Order(14)
void deletePullThroughCacheRules() {
for (String prefix : new String[] {CACHE_PREFIX, SECOND_CACHE_PREFIX}) {
given()
.header("X-Amz-Target", PREFIX + "DeletePullThroughCacheRule")
.contentType(CT)
.body("""
{ "ecrRepositoryPrefix": "%s" }
""".formatted(prefix))
.when()
.post("/")
.then()
.statusCode(200)
.body("ecrRepositoryPrefix", equalTo(prefix))
.body("createdAt", notNullValue());
}
given()
.header("X-Amz-Target", PREFIX + "DeletePullThroughCacheRule")
.contentType(CT)
.body("""
{ "ecrRepositoryPrefix": "%s" }
""".formatted(CACHE_PREFIX))
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("PullThroughCacheRuleNotFoundException"));
}
}
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.ecr;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
@@ -9,6 +10,7 @@ import io.github.hectorvent.floci.services.ecr.model.ImageDetail;
import io.github.hectorvent.floci.services.ecr.model.ImageIdentifier;
import io.github.hectorvent.floci.services.ecr.model.AuthorizationData;
import io.github.hectorvent.floci.services.ecr.model.ImageMetadata;
import io.github.hectorvent.floci.services.ecr.model.PullThroughCacheRule;
import io.github.hectorvent.floci.services.ecr.model.Repository;
import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager;
import io.github.hectorvent.floci.services.ecr.registry.RegistryHttpClient;
@@ -23,6 +25,7 @@ import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -46,6 +49,7 @@ class EcrServiceTest {
private EcrService service;
private EcrRegistryManager registryManager;
private InMemoryStorage<String, PullThroughCacheRule> pullThroughCacheRuleStore;
@BeforeEach
void setUp() {
@@ -62,15 +66,140 @@ class EcrServiceTest {
EmulatorConfig config = Mockito.mock(EmulatorConfig.class);
RegionResolver regionResolver = new RegionResolver(REGION, ACCOUNT);
pullThroughCacheRuleStore = new InMemoryStorage<>();
service = new EcrService(
new InMemoryStorage<>(),
new InMemoryStorage<>(),
pullThroughCacheRuleStore,
registryManager,
config,
regionResolver);
}
// ------------------------------------------------------------
// Pull through cache rules
// ------------------------------------------------------------
@Test
void createPullThroughCacheRule_roundTripsDefaults() {
PullThroughCacheRule rule = service.createPullThroughCacheRule(
"docker-hub/", "registry-1.docker.io", null, null,
null, null, null, REGION);
assertEquals("docker-hub", rule.getEcrRepositoryPrefix());
assertEquals("registry-1.docker.io", rule.getUpstreamRegistryUrl());
assertEquals("docker-hub", rule.getUpstreamRegistry());
assertEquals("ROOT", rule.getUpstreamRepositoryPrefix());
assertEquals(ACCOUNT, rule.getRegistryId());
assertNotNull(rule.getCreatedAt());
assertEquals(rule.getCreatedAt(), rule.getUpdatedAt());
}
@Test
void createPullThroughCacheRule_duplicate_throwsAlreadyExists() {
service.createPullThroughCacheRule("docker-hub", "registry-1.docker.io", null,
null, null, null, null, REGION);
AwsException exception = assertThrows(AwsException.class,
() -> service.createPullThroughCacheRule("docker-hub/", "registry-1.docker.io", null,
null, null, null, null, REGION));
assertEquals("PullThroughCacheRuleAlreadyExistsException", exception.getErrorCode());
}
@Test
void describePullThroughCacheRules_filtersAndPaginates() {
service.createPullThroughCacheRule("cache/alpha", "registry-1.docker.io", null,
"docker-hub", null, null, "library", REGION);
service.createPullThroughCacheRule("cache/beta", "quay.io", null,
null, null, null, null, REGION);
service.createPullThroughCacheRule("cache/gamma", "registry.k8s.io", null,
null, null, null, null, REGION);
PaginatedResult<PullThroughCacheRule> first = service.describePullThroughCacheRules(
null, null, 1, null, REGION);
PaginatedResult<PullThroughCacheRule> second = service.describePullThroughCacheRules(
null, null, 1, first.nextToken(), REGION);
PaginatedResult<PullThroughCacheRule> filtered = service.describePullThroughCacheRules(
null, List.of("cache/gamma/"), null, null, REGION);
assertEquals("cache/alpha", first.items().getFirst().getEcrRepositoryPrefix());
assertNotNull(first.nextToken());
assertEquals("cache/beta", second.items().getFirst().getEcrRepositoryPrefix());
assertEquals("cache/gamma", filtered.items().getFirst().getEcrRepositoryPrefix());
}
@Test
void pullThroughCacheRules_areIsolatedByAccountAndRegion() {
String otherAccount = "111111111111";
service.createPullThroughCacheRule("shared/cache", "quay.io", null,
null, null, null, null, REGION);
service.createPullThroughCacheRule("shared/cache", "registry.k8s.io", otherAccount,
null, null, null, null, REGION);
service.createPullThroughCacheRule("shared/cache", "ghcr.io", null,
null, null, null, null, "eu-west-1");
assertEquals("quay.io", service.describePullThroughCacheRules(
null, null, null, null, REGION).items().getFirst().getUpstreamRegistryUrl());
assertEquals("registry.k8s.io", service.describePullThroughCacheRules(
otherAccount, null, null, null, REGION).items().getFirst().getUpstreamRegistryUrl());
assertEquals("ghcr.io", service.describePullThroughCacheRules(
null, null, null, null, "eu-west-1").items().getFirst().getUpstreamRegistryUrl());
}
@Test
void pullThroughCacheRule_persistsAcrossServiceInstances() {
service.createPullThroughCacheRule("persisted/cache", "quay.io", null,
null, null, null, null, REGION);
EcrService restored = new EcrService(
new InMemoryStorage<>(),
new InMemoryStorage<>(),
pullThroughCacheRuleStore,
registryManager,
Mockito.mock(EmulatorConfig.class),
new RegionResolver(REGION, ACCOUNT));
PullThroughCacheRule rule = restored.describePullThroughCacheRules(
null, List.of("persisted/cache"), null, null, REGION).items().getFirst();
assertEquals("quay.io", rule.getUpstreamRegistryUrl());
}
@Test
void deletePullThroughCacheRule_removesRuleAndMissingFails() {
service.createPullThroughCacheRule("delete/cache", "quay.io", null,
null, null, null, null, REGION);
PullThroughCacheRule deleted = service.deletePullThroughCacheRule("delete/cache/", null, REGION);
assertEquals("delete/cache", deleted.getEcrRepositoryPrefix());
AwsException exception = assertThrows(AwsException.class,
() -> service.deletePullThroughCacheRule("delete/cache", null, REGION));
assertEquals("PullThroughCacheRuleNotFoundException", exception.getErrorCode());
}
@Test
void pullThroughCacheRule_rejectsInvalidInputs() {
assertEquals("InvalidParameterException", assertThrows(AwsException.class,
() -> service.createPullThroughCacheRule("A", "quay.io", null,
null, null, null, null, REGION)).getErrorCode());
assertEquals("UnsupportedUpstreamRegistryException", assertThrows(AwsException.class,
() -> service.createPullThroughCacheRule("invalid/upstream", "example.com", null,
null, null, null, null, REGION)).getErrorCode());
assertEquals("InvalidParameterException", assertThrows(AwsException.class,
() -> service.createPullThroughCacheRule("ROOT", "quay.io", null,
null, null, null, "team", REGION)).getErrorCode());
assertEquals("InvalidParameterException", assertThrows(AwsException.class,
() -> service.describePullThroughCacheRules(
null, List.of(), null, null, REGION)).getErrorCode());
assertEquals("InvalidParameterException", assertThrows(AwsException.class,
() -> service.describePullThroughCacheRules(
null, Collections.nCopies(101, "cache"), null, null, REGION)).getErrorCode());
assertEquals("InvalidParameterException", assertThrows(AwsException.class,
() -> service.describePullThroughCacheRules(null, null, 0, null, REGION)).getErrorCode());
}
// ------------------------------------------------------------
// CreateRepository
// ------------------------------------------------------------
@@ -366,6 +495,7 @@ class EcrServiceTest {
EcrService accountAwareService = new EcrService(
repositories,
new InMemoryStorage<>(),
new InMemoryStorage<>(),
registryManager,
Mockito.mock(EmulatorConfig.class),
new RegionResolver(REGION, ACCOUNT));