From 732e009207f91a3e790fe3e75da2eeb0716963a9 Mon Sep 17 00:00:00 2001 From: Simone Primarosa Date: Thu, 3 Sep 2026 14:14:56 +0200 Subject: [PATCH] fix(ecr): clean up registry storage (#2982) Remove every manifest for a forced repository deletion and reclaim the shared registry volume only when storage pruning is configured. --- docs/services/ecr.md | 1 + .../floci/services/ecr/EcrService.java | 37 ++++++-- .../ecr/registry/EcrRegistryManager.java | 95 ++++++++++++++++++- .../floci/services/ecr/EcrServiceTest.java | 54 +++++++++++ .../ecr/registry/EcrRegistryManagerTest.java | 83 +++++++++++++++- 5 files changed, 257 insertions(+), 13 deletions(-) diff --git a/docs/services/ecr.md b/docs/services/ecr.md index 3b87820537..9d143b6461 100644 --- a/docs/services/ecr.md +++ b/docs/services/ecr.md @@ -30,6 +30,7 @@ ## Emulation Behavior - **Real OCI registry backing.** A single shared `registry:2` container per Floci instance serves all repositories. The container is started lazily on the first ECR API call and reused across Floci restarts (`keep-running-on-shutdown: true` by default), so pushed image bytes survive restarts. +- **Storage cleanup.** `DeleteRepository --force` removes repository manifests, including untagged manifests. When it deletes the final repository, or when a non-retained runtime stops, Floci removes the named registry volume only in `memory` mode or when `prune-volumes-on-delete: true`. - **Loopback URI scheme.** Repository URIs follow `.dkr.ecr..localhost:/`. RFC 6761 reserves `*.localhost` to resolve to the loopback address, and the docker daemon auto-trusts loopback as an insecure registry, so **no daemon configuration changes are required** — `docker push` and `docker pull` work out of the box. A `path` URI style fallback (`localhost:///`) is available via `floci.services.ecr.uri-style: path` for environments where `*.localhost` resolution misbehaves. - **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. - **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. 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.java index 476362f4b1..99586ceb7c 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 @@ -7,6 +7,7 @@ import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.resource.ExplorerResource; import io.github.hectorvent.floci.core.resource.ResourceProvider; import io.github.hectorvent.floci.core.resource.SupportedResourceType; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.services.ecr.model.AuthorizationData; @@ -189,21 +190,16 @@ public class EcrService implements ResourceProvider { String key = key(region, account, repositoryName); Repository repo = repoStore.get(key).orElseThrow(() -> notFound(repositoryName, account)); - // Check whether the registry has any tagged images for this repo. If - // ensureStarted() can't talk to docker (no daemon), assume the repo is - // empty — this allows control-plane unit tests to delete without docker. List tags = listTagsBestEffort(account, region, repositoryName); - if (!tags.isEmpty() && !force) { + if (!force && !tags.isEmpty()) { throw new AwsException("RepositoryNotEmptyException", "The repository with name '" + repositoryName + "' in registry with id '" + account + "' cannot be deleted because it still contains images", 400); } - if (force && !tags.isEmpty()) { - // Phase 5 will issue real DELETE /v2//manifests/ calls. - LOG.infov("Force-deleting ECR repository {0} containing {1} tag(s) (manifest deletion deferred)", - repositoryName, tags.size()); + if (force) { + deleteRepositoryStorage(account, region, repositoryName); } repoStore.delete(key); @@ -212,6 +208,9 @@ public class EcrService implements ResourceProvider { for (ImageMetadata meta : imageMetaStore.scan(k -> k.startsWith(metaPrefix))) { imageMetaStore.delete(metaPrefix + meta.getDigest()); } + if (!hasRepositories()) { + registryManager.pruneStorage(); + } LOG.infov("Deleted ECR repository {0}/{1}/{2}", region, account, repositoryName); return repo; } @@ -544,6 +543,28 @@ public class EcrService implements ResourceProvider { } } + private void deleteRepositoryStorage(String account, String region, String repositoryName) { + try { + registryManager.deleteRepositoryStorage(account, region, repositoryName); + } catch (Exception e) { + throw registryFailure(repositoryName, e); + } + } + + private boolean hasRepositories() { + if (repoStore instanceof AccountAwareStorageBackend accountAware) { + return !accountAware.scanAllAccounts().isEmpty(); + } + return !repoStore.scan(k -> true).isEmpty(); + } + + private AwsException registryFailure(String repositoryName, Exception cause) { + LOG.warnv("Could not delete ECR repository {0} from the backing registry: {1}", + repositoryName, cause.getMessage()); + return new AwsException("ServerException", + "Could not delete images from repository '" + repositoryName + "'", 500); + } + private static String key(String region, String account, String repoName) { return region + "::" + account + "::" + repoName; } 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 55018515ea..bb471e0b0a 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 @@ -45,7 +45,10 @@ public class EcrRegistryManager { private static final Logger LOG = Logger.getLogger(EcrRegistryManager.class); private static final int CONTAINER_INTERNAL_PORT = 5000; + private static final int REPOSITORY_DELETION_TIMEOUT_SECONDS = 10; + private static final int REPOSITORY_DELETION_KILL_GRACE_SECONDS = 1; private static final String NAMED_VOLUME = "floci-ecr-registry-data"; + private static final String REPOSITORIES_PATH = "/var/lib/registry/docker/registry/v2/repositories/"; /** Matches an AWS-shaped ECR image URI: {@code .dkr.ecr..amazonaws.com/[:tag]}. */ private static final java.util.regex.Pattern AWS_ECR_URI = @@ -363,16 +366,100 @@ public class EcrRegistryManager { return new GcResult(output.toString(), durationMs); } + /** Removes repository manifest storage without removing descendant repository directories. */ + public synchronized void deleteRepositoryStorage(String accountId, String region, String repositoryName) { + ensureStarted(); + if (!started || containerId == null) { + throw new IllegalStateException("ECR registry is not started"); + } + String repository = internalRepoName(accountId, region, repositoryName); + String path = repositoryStoragePath(repository); + StringBuilder output = new StringBuilder(); + DockerClient dockerClient = lifecycleManager.getDockerClient(); + ExecCreateCmdResponse exec = dockerClient + .execCreateCmd(containerId) + .withCmd("timeout", "-k", String.valueOf(REPOSITORY_DELETION_KILL_GRACE_SECONDS), + String.valueOf(REPOSITORY_DELETION_TIMEOUT_SECONDS), "rm", "-rf", + path + "/_layers", path + "/_manifests", path + "/_uploads") + .withAttachStdout(true) + .withAttachStderr(true) + .exec(); + try { + boolean completed = dockerClient.execStartCmd(exec.getId()) + .exec(new ResultCallback.Adapter() { + @Override + public void onNext(Frame frame) { + output.append(new String(frame.getPayload(), StandardCharsets.UTF_8)); + } + }) + .awaitCompletion(REPOSITORY_DELETION_TIMEOUT_SECONDS + + REPOSITORY_DELETION_KILL_GRACE_SECONDS + 1, TimeUnit.SECONDS); + if (!completed) { + throw new RuntimeException("repository deletion timed out"); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new RuntimeException("repository deletion interrupted", e); + } + Long exitCode = dockerClient.inspectExecCmd(exec.getId()).exec().getExitCodeLong(); + if (exitCode == null || exitCode != 0) { + throw new RuntimeException("repository deletion failed: " + output); + } + } + /** Stops the container if {@code keepRunningOnShutdown=false}. Called from EmulatorLifecycle hooks. */ - public void shutdown() { + public synchronized void shutdown() { + if (config.services().ecr().keepRunningOnShutdown()) { + if (started && containerId != null) { + LOG.infov("Leaving ECR backing registry container {0} running for next start-up", containerId); + } + return; + } + stopRegistry(); + removeStorageIfConfigured(); + } + + /** Removes the shared registry storage after the final ECR repository is deleted. */ + public synchronized void pruneStorage() { + if (!shouldPruneStorage()) { + return; + } + stopRegistry(); + removeStorageIfConfigured(); + } + + private void stopRegistry() { if (!started || containerId == null) { return; } - if (config.services().ecr().keepRunningOnShutdown()) { - LOG.infov("Leaving ECR backing registry container {0} running for next start-up", containerId); + lifecycleManager.stopAndRemove(containerId, logStream); + portAllocator.release(hostPort); + containerId = null; + logStream = null; + started = false; + reconciled = false; + hostPort = config.services().ecr().registryBasePort(); + } + + private void removeStorageIfConfigured() { + if (!shouldPruneStorage() || !ContainerStorageHelper.isNamedVolumeMode(config)) { return; } - lifecycleManager.stopAndRemove(containerId, logStream); + ContainerStorageHelper.removeNamedVolume( + config, lifecycleManager, ContainerStorageHelper.dockerName(config, NAMED_VOLUME)); + } + + private boolean shouldPruneStorage() { + return "memory".equals(config.storage().mode()) || config.storage().pruneVolumesOnDelete(); + } + + private static String repositoryStoragePath(String repository) { + for (String segment : repository.split("/")) { + if (segment.isEmpty() || ".".equals(segment) || "..".equals(segment)) { + throw new IllegalArgumentException("Invalid registry repository path"); + } + } + return REPOSITORIES_PATH + repository; } private void adoptExisting(Container existing) { 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.java index 20d39a7c30..5ab296f869 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 @@ -3,6 +3,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.RegionResolver; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; import io.github.hectorvent.floci.core.storage.InMemoryStorage; import io.github.hectorvent.floci.services.ecr.model.ImageDetail; import io.github.hectorvent.floci.services.ecr.model.ImageIdentifier; @@ -28,6 +29,8 @@ import java.util.Map; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; /** @@ -161,6 +164,57 @@ class EcrServiceTest { assertEquals("RepositoryNotFoundException", ex.getErrorCode()); } + @Test + void deleteRepository_forceRemovesRepositoryStorageAndPrunesFinalRepositoryStorage() { + service.createRepository(REPO, null, null, null, null, null, null, REGION); + + service.deleteRepository(REPO, null, true, REGION); + + verify(registryManager).deleteRepositoryStorage(ACCOUNT, REGION, REPO); + verify(registryManager).pruneStorage(); + } + + @Test + void deleteRepository_keepsSharedRegistryStorageForRemainingRepositories() { + service.createRepository(REPO, null, null, null, null, null, null, REGION); + service.createRepository("other", null, null, null, null, null, null, REGION); + + service.deleteRepository(REPO, null, true, REGION); + + verify(registryManager, never()).pruneStorage(); + } + + @Test + void deleteRepository_forceRetainsMetadataWhenRegistryCleanupFails() { + Mockito.doThrow(new IllegalStateException("registry unavailable")) + .when(registryManager).deleteRepositoryStorage(ACCOUNT, REGION, REPO); + service.createRepository(REPO, null, null, null, null, null, null, REGION); + + AwsException ex = assertThrows(AwsException.class, + () -> service.deleteRepository(REPO, null, true, REGION)); + + assertEquals("ServerException", ex.getErrorCode()); + assertEquals(REPO, service.describeRepositories(List.of(REPO), null, REGION).getFirst().getRepositoryName()); + verify(registryManager, never()).pruneStorage(); + } + + @Test + void deleteRepository_keepsSharedStorageForRepositoriesOwnedByAnotherAccount() { + AccountAwareStorageBackend repositories = AccountAwareStorageBackend.inMemory(ACCOUNT); + EcrService accountAwareService = new EcrService( + repositories, + new InMemoryStorage<>(), + registryManager, + Mockito.mock(EmulatorConfig.class), + new RegionResolver(REGION, ACCOUNT)); + accountAwareService.createRepository(REPO, null, null, null, null, null, null, REGION); + repositories.putForAccount("111111111111", REGION + "::111111111111::other", new Repository()); + + accountAwareService.deleteRepository(REPO, null, true, REGION); + + verify(registryManager, never()).pruneStorage(); + } + // ------------------------------------------------------------ // GetAuthorizationToken // ------------------------------------------------------------ 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 0a0abb06ff..982c38d76b 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 @@ -9,7 +9,15 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer; import io.github.hectorvent.floci.core.common.docker.CurrentContainerNetworkResolver; import io.github.hectorvent.floci.core.common.docker.ContainerSpec; import io.github.hectorvent.floci.core.common.docker.PortAllocator; +import com.github.dockerjava.api.DockerClient; +import com.github.dockerjava.api.async.ResultCallback; +import com.github.dockerjava.api.command.ExecCreateCmd; +import com.github.dockerjava.api.command.ExecCreateCmdResponse; +import com.github.dockerjava.api.command.ExecStartCmd; +import com.github.dockerjava.api.command.InspectExecCmd; +import com.github.dockerjava.api.command.InspectExecResponse; import com.github.dockerjava.api.model.Container; +import com.github.dockerjava.api.model.Frame; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -45,6 +53,7 @@ class EcrRegistryManagerTest { private CurrentContainerNetworkResolver currentContainerNetworkResolver; private EmulatorConfig.DockerConfig docker; private EmulatorConfig.EcrServiceConfig ecr; + private EmulatorConfig.StorageConfig storage; private ContainerBuilder.Builder builder; private EcrRegistryManager manager; @@ -68,7 +77,7 @@ class EcrRegistryManagerTest { EmulatorConfig config = Mockito.mock(EmulatorConfig.class); ecr = Mockito.mock(EmulatorConfig.EcrServiceConfig.class); docker = Mockito.mock(EmulatorConfig.DockerConfig.class); - EmulatorConfig.StorageConfig storage = Mockito.mock(EmulatorConfig.StorageConfig.class); + storage = Mockito.mock(EmulatorConfig.StorageConfig.class); when(config.services()).thenReturn(Mockito.mock(EmulatorConfig.ServicesConfig.class)); when(config.services().ecr()).thenReturn(ecr); when(config.docker()).thenReturn(docker); @@ -76,6 +85,7 @@ class EcrRegistryManagerTest { when(docker.resourceNamespace()).thenReturn(Optional.empty()); // Empty host-persistent-path selects named-volume mode (no host bind-mount logic). when(storage.hostPersistentPath()).thenReturn(""); + when(storage.mode()).thenReturn("persistent"); when(ecr.registryContainerName()).thenReturn(REGISTRY_NAME); when(ecr.registryImage()).thenReturn("registry:2"); when(ecr.registryBasePort()).thenReturn(BASE_PORT); @@ -200,4 +210,75 @@ class EcrRegistryManagerTest { void rewriteImageUri_nullImage_returnsNull() { assertNull(manager.rewriteImageUri(null)); } + + @Test + void pruneStorageRemovesTheRegistryAndVolumeWhenConfigured() { + when(storage.pruneVolumesOnDelete()).thenReturn(true); + when(lifecycleManager.createAndStart(any())).thenReturn( + new ContainerLifecycleManager.ContainerInfo("container-id", Map.of())); + manager.ensureStarted(); + + manager.pruneStorage(); + + verify(lifecycleManager).stopAndRemove(Mockito.eq("container-id"), Mockito.isNull()); + verify(lifecycleManager).removeVolume("floci-ecr-registry-data"); + } + + @Test + void pruneStorageRetainsTheRegistryWhenVolumePruningIsDisabled() { + when(lifecycleManager.createAndStart(any())).thenReturn( + new ContainerLifecycleManager.ContainerInfo("container-id", Map.of())); + manager.ensureStarted(); + + manager.pruneStorage(); + + verify(lifecycleManager, Mockito.never()).stopAndRemove(anyString(), Mockito.isNull()); + verify(lifecycleManager, Mockito.never()).removeVolume(anyString()); + } + + @Test + void shutdownRemovesTheRegistryVolumeWhenItIsNotRetained() { + when(storage.pruneVolumesOnDelete()).thenReturn(true); + when(lifecycleManager.createAndStart(any())).thenReturn( + new ContainerLifecycleManager.ContainerInfo("container-id", Map.of())); + manager.ensureStarted(); + + manager.shutdown(); + + verify(lifecycleManager).stopAndRemove(Mockito.eq("container-id"), Mockito.isNull()); + verify(lifecycleManager).removeVolume("floci-ecr-registry-data"); + } + + @Test + void deleteRepositoryStorageBoundsCleanupAndPreservesNestedRepositories() { + when(lifecycleManager.createAndStart(any())).thenReturn( + new ContainerLifecycleManager.ContainerInfo("container-id", Map.of())); + DockerClient dockerClient = Mockito.mock(DockerClient.class); + ExecCreateCmd execCreate = Mockito.mock(ExecCreateCmd.class, Mockito.RETURNS_SELF); + ExecCreateCmdResponse exec = Mockito.mock(ExecCreateCmdResponse.class); + when(exec.getId()).thenReturn("exec-id"); + when(execCreate.exec()).thenReturn(exec); + when(dockerClient.execCreateCmd("container-id")).thenReturn(execCreate); + ExecStartCmd execStart = Mockito.mock(ExecStartCmd.class); + when(execStart.exec(any())).thenAnswer(invocation -> { + @SuppressWarnings("unchecked") + ResultCallback callback = invocation.getArgument(0); + callback.onComplete(); + return callback; + }); + when(dockerClient.execStartCmd("exec-id")).thenReturn(execStart); + InspectExecCmd inspect = Mockito.mock(InspectExecCmd.class); + InspectExecResponse result = Mockito.mock(InspectExecResponse.class); + when(result.getExitCodeLong()).thenReturn(0L); + when(inspect.exec()).thenReturn(result); + when(dockerClient.inspectExecCmd("exec-id")).thenReturn(inspect); + when(lifecycleManager.getDockerClient()).thenReturn(dockerClient); + + manager.deleteRepositoryStorage("000000000000", "us-east-1", "team/app"); + + verify(execCreate).withCmd("timeout", "-k", "1", "10", "rm", "-rf", + "/var/lib/registry/docker/registry/v2/repositories/000000000000/us-east-1/team/app/_layers", + "/var/lib/registry/docker/registry/v2/repositories/000000000000/us-east-1/team/app/_manifests", + "/var/lib/registry/docker/registry/v2/repositories/000000000000/us-east-1/team/app/_uploads"); + } }