Light, fluffy, and always free - The AWS Local Emulator alternative
feat(ecr): update and validate pull-through cache rules (#4342)
XuJianb658c5f02052
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/EcrTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/EcrTest.javaindex b4db72f0fa..0e99ef3911 100644--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/EcrTest.java+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/EcrTest.java@@ -185,6 +185,20 @@ class EcrTest {.extracting(PullThroughCacheRule::ecrRepositoryPrefix).containsExactly(firstPrefix);+ String credentialArn =+ "arn:aws:secretsmanager:us-east-1:000000000000:secret:ecr-pullthroughcache/sdk";+ UpdatePullThroughCacheRuleResponse updated = ecr.updatePullThroughCacheRule(builder -> builder+ .ecrRepositoryPrefix(firstPrefix)+ .credentialArn(credentialArn));+ ValidatePullThroughCacheRuleResponse validated = ecr.validatePullThroughCacheRule(+ builder -> builder.ecrRepositoryPrefix(firstPrefix));++ assertThat(updated.credentialArn()).isEqualTo(credentialArn);+ assertThat(updated.updatedAt()).isNotNull();+ assertThat(validated.isValid()).isTrue();+ assertThat(validated.credentialArn()).isEqualTo(credentialArn);+ assertThat(validated.upstreamRegistryUrl()).isEqualTo("registry-1.docker.io");+DeletePullThroughCacheRuleResponse deleted = ecr.deletePullThroughCacheRule(builder -> builder.ecrRepositoryPrefix(firstPrefix));assertThat(deleted.ecrRepositoryPrefix()).isEqualTo(firstPrefix);diff --git a/docs/services/ecr.md b/docs/services/ecr.mdindex 040a9dceac..1b29f89ba1 100644--- a/docs/services/ecr.md+++ b/docs/services/ecr.md@@ -11,6 +11,8 @@| `CreateRepository` | Create a new repository (lazy-starts the backing registry on first call) || `CreatePullThroughCacheRule` | Create and persist pull-through cache rule configuration || `DescribePullThroughCacheRules` | List or filter pull-through cache rules with pagination |+| `UpdatePullThroughCacheRule` | Update a rule's credential or custom role |+| `ValidatePullThroughCacheRule` | Validate that a configured rule exists and report it as usable || `DeletePullThroughCacheRule` | Delete a pull-through cache rule by repository prefix || `DescribeRepositories` | List repositories or fetch by name || `DeleteRepository` | Delete a repository (with `force=true` semantics for non-empty repos) |@@ -39,7 +41,7 @@- **Loopback URI scheme.** Repository URIs follow `<account>.dkr.ecr.<region>.localhost:<flociPort>/<repoName>`. RFC 6761 reserves `*.localhost` to resolve to the loopback address, and the docker daemon auto-trusts loopback as an insecure registry, so `docker push` and `docker pull` work without daemon configuration. A `path` URI style fallback (`localhost:<flociPort>/<account>/<region>/<repo>`) is available via `floci.services.ecr.uri-style: path` for environments where `*.localhost` resolution misbehaves.- **Image tag mutability.** `IMMUTABLE` repositories allow the first manifest write for a tag and reject every replacement, including a replacement with the same manifest. The proxy forwards blob uploads and digest-addressed manifests unchanged.- **Authorization.** `GetAuthorizationToken` returns `Base64("AWS:floci")` plus a proxy endpoint. The backing `registry:2` runs without auth, so any `aws ecr get-login-password | docker login` succeeds.-- **Pull through cache rule configuration.** `CreatePullThroughCacheRule`, `DescribePullThroughCacheRules`, and `DeletePullThroughCacheRule` persist AWS-compatible rule metadata with account and Region isolation. Rules do not proxy or cache images from the configured upstream registry.+- **Pull through cache rule configuration.** Pull-through cache rule actions persist AWS-compatible rule metadata with account and Region isolation. `UpdatePullThroughCacheRule` preserves fields omitted by the request and refreshes `updatedAt`. `ValidatePullThroughCacheRule` checks that the rule exists and reports it as valid; Floci does not contact the upstream registry or validate credentials. Rules do not proxy or cache images from the configured upstream registry.- **Manifest format negotiation.** `BatchGetImage` forwards the caller's `acceptedMediaTypes` as the upstream `Accept` header. Modern OCI manifests (`application/vnd.oci.image.manifest.v1+json`) and Docker v2 schema 2 are both supported.- **Cross-account / cross-region isolation.** Internally the registry namespaces repositories as `<account>/<region>/<repoName>`, so the same repository name in different accounts or regions cannot collide.- **Reconcile on first start.** When the registry container starts, Floci queries `GET /v2/_catalog` and recreates `Repository` metadata entries for any namespaces present in the registry but missing from local storage. This means image bytes are never orphaned across restarts.diff --git a/src/main/java/io/github/hectorvent/floci/services/ecr/EcrJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/ecr/EcrJsonHandler.javaindex 0afa86f5d6..b4de8954e7 100644--- a/src/main/java/io/github/hectorvent/floci/services/ecr/EcrJsonHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/ecr/EcrJsonHandler.java@@ -46,6 +46,8 @@ public class EcrJsonHandler {case "CreateRepository" -> handleCreateRepository(request, region);case "CreatePullThroughCacheRule" -> handleCreatePullThroughCacheRule(request, region);case "DescribePullThroughCacheRules" -> handleDescribePullThroughCacheRules(request, region);+ case "UpdatePullThroughCacheRule" -> handleUpdatePullThroughCacheRule(request, region);+ case "ValidatePullThroughCacheRule" -> handleValidatePullThroughCacheRule(request, region);case "DeletePullThroughCacheRule" -> handleDeletePullThroughCacheRule(request, region);case "DescribeRepositories" -> handleDescribeRepositories(request, region);case "BatchGetRepositoryScanningConfiguration" ->@@ -136,6 +138,51 @@ public class EcrJsonHandler {return Response.ok(buildPullThroughCacheRule(rule, false, false)).build();}+ private Response handleUpdatePullThroughCacheRule(JsonNode request, String region) {+ PullThroughCacheRule rule = service.updatePullThroughCacheRule(+ request.path("ecrRepositoryPrefix").asText(null),+ request.path("registryId").asText(null),+ request.path("credentialArn").asText(null),+ request.path("customRoleArn").asText(null),+ region);+ ObjectNode response = objectMapper.createObjectNode();+ response.put("ecrRepositoryPrefix", rule.getEcrRepositoryPrefix());+ response.put("registryId", rule.getRegistryId());+ response.put("updatedAt", rule.getUpdatedAt().getEpochSecond());+ if (rule.getCredentialArn() != null) {+ response.put("credentialArn", rule.getCredentialArn());+ }+ if (rule.getCustomRoleArn() != null) {+ response.put("customRoleArn", rule.getCustomRoleArn());+ }+ if (rule.getUpstreamRepositoryPrefix() != null) {+ response.put("upstreamRepositoryPrefix", rule.getUpstreamRepositoryPrefix());+ }+ return Response.ok(response).build();+ }++ private Response handleValidatePullThroughCacheRule(JsonNode request, String region) {+ PullThroughCacheRule rule = service.validatePullThroughCacheRule(+ request.path("ecrRepositoryPrefix").asText(null),+ request.path("registryId").asText(null),+ region);+ ObjectNode response = objectMapper.createObjectNode();+ response.put("ecrRepositoryPrefix", rule.getEcrRepositoryPrefix());+ response.put("registryId", rule.getRegistryId());+ response.put("upstreamRegistryUrl", rule.getUpstreamRegistryUrl());+ if (rule.getCredentialArn() != null) {+ response.put("credentialArn", rule.getCredentialArn());+ }+ if (rule.getCustomRoleArn() != null) {+ response.put("customRoleArn", rule.getCustomRoleArn());+ }+ if (rule.getUpstreamRepositoryPrefix() != null) {+ response.put("upstreamRepositoryPrefix", rule.getUpstreamRepositoryPrefix());+ }+ response.put("isValid", true);+ return Response.ok(response).build();+ }+private Response handleDescribeRepositories(JsonNode request, String region) {List<String> names = parseStringList(request.path("repositoryNames"));String registryId = request.path("registryId").asText(null);diff --git a/src/main/java/io/github/hectorvent/floci/services/ecr/EcrService.java b/src/main/java/io/github/hectorvent/floci/services/ecr/EcrService.javaindex 90ac23310e..3694451955 100644--- a/src/main/java/io/github/hectorvent/floci/services/ecr/EcrService.java+++ b/src/main/java/io/github/hectorvent/floci/services/ecr/EcrService.java@@ -185,6 +185,43 @@ public class EcrService implements ResourceProvider {return rule;}+ public PullThroughCacheRule updatePullThroughCacheRule(String ecrRepositoryPrefix,+ String registryId,+ String credentialArn,+ String customRoleArn,+ String region) {+ String prefix = normalizePullThroughCachePrefix(ecrRepositoryPrefix);+ validatePullThroughCachePrefix(prefix, "ecrRepositoryPrefix");+ String account = effectiveAccount(registryId);+ validateRegistryId(account);+ validateCredentialArn(credentialArn);+ validateCustomRoleArn(customRoleArn);++ String key = pullThroughCacheRuleKey(region, account, prefix);+ PullThroughCacheRule rule = pullThroughCacheRuleStore.get(key)+ .orElseThrow(() -> pullThroughCacheRuleNotFound(prefix, account));+ if (credentialArn != null) {+ rule.setCredentialArn(credentialArn);+ }+ if (customRoleArn != null) {+ rule.setCustomRoleArn(customRoleArn);+ }+ rule.setUpdatedAt(Instant.now());+ pullThroughCacheRuleStore.put(key, rule);+ return rule;+ }++ public PullThroughCacheRule validatePullThroughCacheRule(String ecrRepositoryPrefix,+ String registryId,+ String region) {+ String prefix = normalizePullThroughCachePrefix(ecrRepositoryPrefix);+ validatePullThroughCachePrefix(prefix, "ecrRepositoryPrefix");+ String account = effectiveAccount(registryId);+ validateRegistryId(account);+ return pullThroughCacheRuleStore.get(pullThroughCacheRuleKey(region, account, prefix))+ .orElseThrow(() -> pullThroughCacheRuleNotFound(prefix, account));+ }+/*** Recreates {@link Repository} metadata entries for registry namespaces found in the catalog.* Namespaces created by Floci are {@code <account>/<region>/<repoName>}; pre-proxy registrydiff --git a/src/test/java/io/github/hectorvent/floci/services/ecr/EcrIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ecr/EcrIntegrationTest.javaindex 401e19ceae..c6841bbc92 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@@ -307,6 +307,53 @@ class EcrIntegrationTest {@Test@Order(12)+ void updateAndValidatePullThroughCacheRule() {+ String updatedCredential =+ "arn:aws:secretsmanager:us-east-1:000000000000:secret:ecr-pullthroughcache/updated";+ String customRole = "arn:aws:iam::000000000000:role/EcrPullThroughCache";++ given()+ .header("X-Amz-Target", PREFIX + "UpdatePullThroughCacheRule")+ .contentType(CT)+ .body("""+ {+ "ecrRepositoryPrefix": "%s/",+ "credentialArn": "%s",+ "customRoleArn": "%s"+ }+ """.formatted(CACHE_PREFIX, updatedCredential, customRole))+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("ecrRepositoryPrefix", equalTo(CACHE_PREFIX))+ .body("credentialArn", equalTo(updatedCredential))+ .body("customRoleArn", equalTo(customRole))+ .body("upstreamRepositoryPrefix", equalTo("library"))+ .body("updatedAt", notNullValue())+ .body("upstreamRegistryUrl", nullValue());++ given()+ .header("X-Amz-Target", PREFIX + "ValidatePullThroughCacheRule")+ .contentType(CT)+ .body("""+ { "ecrRepositoryPrefix": "%s" }+ """.formatted(CACHE_PREFIX))+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("ecrRepositoryPrefix", equalTo(CACHE_PREFIX))+ .body("upstreamRegistryUrl", equalTo("registry-1.docker.io"))+ .body("credentialArn", equalTo(updatedCredential))+ .body("customRoleArn", equalTo(customRole))+ .body("upstreamRepositoryPrefix", equalTo("library"))+ .body("isValid", equalTo(true))+ .body("failure", nullValue());+ }++ @Test+ @Order(13)void describePullThroughCacheRulesFiltersAndPaginates() {String nextToken = given().header("X-Amz-Target", PREFIX + "DescribePullThroughCacheRules")@@ -349,7 +396,7 @@ class EcrIntegrationTest {}@Test- @Order(13)+ @Order(14)void createPullThroughCacheRuleDuplicateFails() {given().header("X-Amz-Target", PREFIX + "CreatePullThroughCacheRule")@@ -378,7 +425,7 @@ class EcrIntegrationTest {}@Test- @Order(14)+ @Order(15)void deletePullThroughCacheRules() {for (String prefix : new String[] {CACHE_PREFIX, SECOND_CACHE_PREFIX}) {given()@@ -406,5 +453,17 @@ class EcrIntegrationTest {.then().statusCode(400).body("__type", equalTo("PullThroughCacheRuleNotFoundException"));++ given()+ .header("X-Amz-Target", PREFIX + "ValidatePullThroughCacheRule")+ .contentType(CT)+ .body("""+ { "ecrRepositoryPrefix": "%s" }+ """.formatted(CACHE_PREFIX))+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .body("__type", equalTo("PullThroughCacheRuleNotFoundException"));}}diff --git a/src/test/java/io/github/hectorvent/floci/services/ecr/EcrServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/ecr/EcrServiceTest.javaindex a581db4465..255e4e3c9e 100644--- a/src/test/java/io/github/hectorvent/floci/services/ecr/EcrServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ecr/EcrServiceTest.java@@ -24,6 +24,7 @@ import com.sun.net.httpserver.HttpServer;import java.io.IOException;import java.io.OutputStream;import java.net.InetSocketAddress;+import java.time.Instant;import java.util.Base64;import java.util.Collections;import java.util.List;@@ -179,6 +180,53 @@ class EcrServiceTest {assertEquals("PullThroughCacheRuleNotFoundException", exception.getErrorCode());}+ @Test+ void updatePullThroughCacheRule_updatesProvidedFieldsAndPreservesTheRest() {+ String originalCredential =+ "arn:aws:secretsmanager:us-east-1:000000000000:secret:ecr-pullthroughcache/original";+ String updatedCredential =+ "arn:aws:secretsmanager:us-east-1:000000000000:secret:ecr-pullthroughcache/updated";+ PullThroughCacheRule created = service.createPullThroughCacheRule(+ "update/cache", "registry-1.docker.io", null, null,+ originalCredential, null, "library", REGION);+ Instant createdAt = created.getCreatedAt();++ PullThroughCacheRule updated = service.updatePullThroughCacheRule(+ "update/cache/", null, updatedCredential,+ "arn:aws:iam::000000000000:role/EcrPullThroughCache", REGION);++ assertEquals(updatedCredential, updated.getCredentialArn());+ assertEquals("arn:aws:iam::000000000000:role/EcrPullThroughCache", updated.getCustomRoleArn());+ assertEquals("registry-1.docker.io", updated.getUpstreamRegistryUrl());+ assertEquals("library", updated.getUpstreamRepositoryPrefix());+ assertEquals(createdAt, updated.getCreatedAt());+ assertFalse(updated.getUpdatedAt().isBefore(createdAt));++ PullThroughCacheRule roleOnlyUpdate = service.updatePullThroughCacheRule(+ "update/cache", null, null,+ "arn:aws:iam::000000000000:role/ReplacedRole", REGION);+ assertEquals(updatedCredential, roleOnlyUpdate.getCredentialArn());+ assertEquals("arn:aws:iam::000000000000:role/ReplacedRole", roleOnlyUpdate.getCustomRoleArn());+ }++ @Test+ void validatePullThroughCacheRule_returnsStoredRuleAndMissingRulesFail() {+ PullThroughCacheRule created = service.createPullThroughCacheRule(+ "validate/cache", "quay.io", null, null,+ null, null, null, REGION);++ PullThroughCacheRule validated = service.validatePullThroughCacheRule(+ "validate/cache/", null, REGION);++ assertSame(created, validated);+ assertEquals("quay.io", validated.getUpstreamRegistryUrl());+ assertEquals("PullThroughCacheRuleNotFoundException", assertThrows(AwsException.class,+ () -> service.validatePullThroughCacheRule("missing/cache", null, REGION)).getErrorCode());+ assertEquals("PullThroughCacheRuleNotFoundException", assertThrows(AwsException.class,+ () -> service.updatePullThroughCacheRule(+ "missing/cache", null, null, null, REGION)).getErrorCode());+ }+@Testvoid pullThroughCacheRule_rejectsInvalidInputs() {assertEquals("InvalidParameterException", assertThrows(AwsException.class,