diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactMavenController.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactMavenController.java index 5ed767262c..4853c2a085 100644 --- a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactMavenController.java +++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactMavenController.java @@ -159,8 +159,11 @@ public class CodeArtifactMavenController { @Produces(MediaType.WILDCARD) public Response fetch(@Context HttpHeaders headers, @PathParam("domain") String domain, @PathParam("repository") String repository, @PathParam("gav") String gav) { - String repoId = requireScope(headers, domain, repository).repoId(); - return reposilite.fetchArtifact(repoId, gav) + MavenRequestScope scope = requireScope(headers, domain, repository); + if (!isDownloadable(scope, domain, repository, gav)) { + return Response.status(Response.Status.NOT_FOUND).build(); + } + return reposilite.fetchArtifact(scope.repoId(), gav) .map(this::toResponse) .orElseGet(() -> Response.status(Response.Status.NOT_FOUND).build()); } @@ -168,12 +171,34 @@ public class CodeArtifactMavenController { @HEAD public Response head(@Context HttpHeaders headers, @PathParam("domain") String domain, @PathParam("repository") String repository, @PathParam("gav") String gav) { - String repoId = requireScope(headers, domain, repository).repoId(); - return reposilite.artifactExists(repoId, gav) + MavenRequestScope scope = requireScope(headers, domain, repository); + if (!isDownloadable(scope, domain, repository, gav)) { + return Response.status(Response.Status.NOT_FOUND).build(); + } + return reposilite.artifactExists(scope.repoId(), gav) ? Response.ok().build() : Response.status(Response.Status.NOT_FOUND).build(); } + /** + * {@code true} unless {@code gav} names an exact version this service knows is {@code + * Archived}: {@link #parseNamespaceAndPackageName}'s own version element is {@code null} for a + * bare {@code maven-metadata.xml} path (no specific version to block), which this treats the + * same as "not a well-formed, versioned GAV at all": both fall through to Reposilite + * unchecked, exactly as {@code deploy} above already does for the identical case. + */ + private boolean isDownloadable(MavenRequestScope scope, String domain, String repository, String gav) { + return parseNamespaceAndPackageName(gav).filter(c -> c[2] != null).map(c -> { + try { + service.requireContainerBackedVersionDownloadable(scope.owner(), scope.region(), domain, repository, + "maven", c[0], c[1], c[2], scope.repoId()); + return true; + } catch (AwsException e) { + return false; + } + }).orElse(true); + } + private Response toResponse(FetchedArtifact artifact) { return Response.ok(artifact.content(), artifact.contentType()).build(); } diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNpmDataPlane.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNpmDataPlane.java index b3b374c8d7..b97200e873 100644 --- a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNpmDataPlane.java +++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNpmDataPlane.java @@ -149,24 +149,28 @@ public class CodeArtifactNpmDataPlane { context.request().pause(); String repoId = npmRepositoryId; AuthorizationTokenScope tokenScope = scope.get(); + Optional tarballTarget = nativeTarballDownloadTarget(context.request().method(), + npmRequest.rest()); Optional unpublishTarget = nativeUnpublishTarget(context.request().method(), npmRequest.rest()); - vertx.executeBlocking(promise -> promise.complete(ensureBackendReady(repoId, npmRequest))) - .onComplete(result -> { - if (result.failed()) { - context.request().resume(); - context.response().setStatusCode(503).end(); - return; - } - proxy(context, result.result(), npmRequest.rest() + querySuffix(context), status -> - unpublishTarget.ifPresent(identity -> { - if (status == 200 || status == 201) { - service.forgetContainerBackedPackage(tokenScope.region(), npmRequest.domain(), - tokenScope.owner(), npmRequest.repository(), "npm", identity.namespace(), - identity.packageName()); - } - })); - }); + vertx.executeBlocking(promise -> { + tarballTarget.ifPresent(target -> blockIfArchived(tokenScope, npmRequest, repoId, target)); + promise.complete(ensureBackendReady(repoId, npmRequest)); + }).onComplete(result -> { + if (result.failed()) { + context.request().resume(); + context.response().setStatusCode(result.cause() instanceof AwsException ? 404 : 503).end(); + return; + } + proxy(context, result.result(), npmRequest.rest() + querySuffix(context), status -> + unpublishTarget.ifPresent(identity -> { + if (status == 200 || status == 201) { + service.forgetContainerBackedPackage(tokenScope.region(), npmRequest.domain(), + tokenScope.owner(), npmRequest.repository(), "npm", identity.namespace(), + identity.packageName()); + } + })); + }); } /** @@ -223,6 +227,69 @@ public class CodeArtifactNpmDataPlane { return Optional.of(packageIdentityFromPath(rest.substring(0, revIndex))); } + /** + * A real tarball download ({@code npm install}, or a direct link from Verdaccio's own web UI) + * is a {@code GET} to {@code /-/}, the exact shape {@code + * VerdaccioSidecarClient}'s own tarball requests already use: detected here the same way + * {@link #nativeUnpublishTarget} detects its own {@code /-rev/} marker. The version is not a + * separate path segment the way Maven's GAV or nuget's content route carry it, only embedded + * in {@code filename} itself, so {@link #blockIfArchived} resolves it for real against + * Verdaccio's own packument rather than this method guessing it from the filename's own text. + * + *

Raised by Greptile's review: an earlier version of this method assumed {@code filename} + * always has the {@code -.tgz} shape this emulator's own publish path + * happens to write, and stripped that known prefix/suffix to read the version straight out of + * it. Nothing in the real npm publish protocol requires a client-chosen {@code dist.tarball} + * name to follow that convention at all (a version {@code 1.0.0} can legitimately name its + * tarball {@code bundle.tgz}), so a filename that didn't fit the assumed shape skipped the + * Archived check entirely, and one that happened to fit it by coincidence (a real tarball + * literally named {@code -release.tgz}) could resolve to the wrong, unrelated + * version {@code release}. + */ + private static Optional nativeTarballDownloadTarget(HttpMethod method, String rest) { + if (method != HttpMethod.GET) { + return Optional.empty(); + } + int marker = rest.indexOf("/-/"); + if (marker < 1) { + return Optional.empty(); + } + PackageIdentity identity = packageIdentityFromPath(rest.substring(0, marker)); + String filename = rest.substring(marker + 3); + return Optional.of(new TarballDownloadRequest(identity, filename)); + } + + private record TarballDownloadRequest(PackageIdentity identity, String filename) {} + + /** + * Blocks (by throwing {@link AwsException}) only when {@code target}'s own filename resolves + * to a version this repository currently has marked {@code Archived}. Checks whether this + * package has any status override at all first, a plain in-memory map read, before ever + * paying for a live Verdaccio packument fetch: the overwhelming common case, a package + * nothing has ever called {@code UpdatePackageVersionsStatus}/{@code DisposePackageVersions} + * on, costs nothing beyond that one check. {@link VerdaccioSidecarClient#versionForTarballFilename} + * resolves the real version, against the packument itself rather than the filename's own + * text, only once there is actually something in this repository worth checking against. + */ + private void blockIfArchived(AuthorizationTokenScope tokenScope, NpmRequest npmRequest, String repoId, + TarballDownloadRequest target) { + Map overrides = service.containerBackedOverridesForPackage(tokenScope.owner(), tokenScope.region(), + npmRequest.domain(), npmRequest.repository(), "npm", target.identity().namespace(), + target.identity().packageName()); + if (overrides.isEmpty()) { + return; + } + Optional version = verdaccioClient.versionForTarballFilename(repoId, npmRequest.domain(), + npmRequest.repository(), target.identity().namespace(), target.identity().packageName(), + target.filename()); + if (version.isEmpty()) { + return; + } + service.requireContainerBackedVersionDownloadable(tokenScope.owner(), tokenScope.region(), + npmRequest.domain(), npmRequest.repository(), "npm", target.identity().namespace(), + target.identity().packageName(), version.get(), repoId); + } + /** * Real npm registries and the pinned Verdaccio image both reject a republish of an existing * version with 409, even for byte-identical content. The AWS CodeArtifact User Guide states the diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNuGetController.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNuGetController.java index 834aea35df..85ade28214 100644 --- a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNuGetController.java +++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNuGetController.java @@ -130,7 +130,14 @@ public class CodeArtifactNuGetController { public Response packageContent(@Context HttpHeaders headers, @PathParam("domain") String domain, @PathParam("repository") String repository, @PathParam("id") String id, @PathParam("version") String version, @PathParam("filename") String filename) { - String baseUrl = requireRepository(headers, domain, repository).baseUrl(); + RepositoryHandle handle = requireRepository(headers, domain, repository); + try { + service.requireContainerBackedVersionDownloadable(handle.owner(), handle.region(), domain, repository, + "nuget", null, id, version, handle.repoId()); + } catch (AwsException e) { + return Response.status(404).build(); + } + String baseUrl = handle.baseUrl(); String lowerId = SidecarUriUtils.encodeSegment(id.toLowerCase(Locale.ROOT)); String encodedVersion = SidecarUriUtils.encodeSegment(version); String lowerFilename = filename.toLowerCase(Locale.ROOT); diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiController.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiController.java index e8acacd91e..500a9ee0ab 100644 --- a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiController.java +++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiController.java @@ -120,14 +120,37 @@ public class CodeArtifactPypiController { .build(); } + /** + * pypi's real download route is a bare filename with no package name or version anywhere in + * the URL, unlike Maven's GAV path or nuget's content route, so there is nothing in a download + * request itself to resolve the coordinate {@link #archivedOrDisposedVersionFromFilename} + * needs. Resolving it against this repository's own status overrides, the same way the upload + * guard above already does, rather than a separate map recorded only at upload time: raised by + * Greptile's review, an earlier version of this check used exactly that kind of map, recorded + * only by a native twine upload, so a version {@code CopyPackageVersions} wrote straight to the + * destination sidecar (never touching this controller's own upload handler at all) had no + * recorded identity, and archiving it afterward never blocked the download. Resolving against + * the override records instead needs no write-time bookkeeping at all: whichever path put the + * real file there, an override written later by {@code UpdatePackageVersionsStatus}/{@code + * DisposePackageVersions} is found the same way regardless. + */ @GET @Path("packages/{filename}") @Produces(MediaType.WILDCARD) public Response downloadFile(@Context HttpHeaders headers, @PathParam("domain") String domain, @PathParam("repository") String repository, @PathParam("filename") String filename) { - String baseUrl = requireRepository(headers, domain, repository).baseUrl(); - return proxyGet(baseUrl, "/packages/" + SidecarUriUtils.encodeSegment(filename)); + RepositoryHandle handle = requireRepository(headers, domain, repository); + ArchivedOrDisposedMatch match = archivedOrDisposedVersionFromFilename(handle, domain, repository, filename); + if (match != null) { + try { + service.requireContainerBackedVersionDownloadable(handle.owner(), handle.region(), domain, + repository, "pypi", null, match.packageName(), match.version(), handle.repoId()); + } catch (AwsException e) { + return Response.status(404).build(); + } + } + return proxyGet(handle.baseUrl(), "/packages/" + SidecarUriUtils.encodeSegment(filename)); } /** diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactService.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactService.java index c2ba109e39..31bf4f6552 100644 --- a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactService.java +++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactService.java @@ -1995,17 +1995,13 @@ public class CodeArtifactService implements Resettable { * one status that still allows a normal download ("if the Unlisted package version is already * referenced in an npm package-lock.json file, it can still be downloaded and installed"). * - *

Known gap, not fixed here (flagged by Qodo review on the PR that added {@code - * DisposePackageVersions}): this only guards {@code GetPackageVersionAsset} itself. The - * native per-format data planes ({@code CodeArtifactMavenController}'s GAV path, {@code - * CodeArtifactNpmDataPlane}'s tarball route, {@code CodeArtifactPypiController}'s raw - * filename-only download route, {@code CodeArtifactNuGetController}'s content route) proxy - * straight to each sidecar and never consult this override at all, for any purpose, so a - * plain {@code pip install}/{@code npm install}/{@code mvn} fetch against an Archived version - * can still download it. Fixing this means threading status awareness into four independently - * shaped native protocols (pypi's own download route does not even carry a version in its - * URL, only a filename, unlike the other three), a feature-family-sized gap closer to Package - * Groups than a quick patch, tracked in the private design memo, not silently dropped. + *

The native per-format data planes ({@code CodeArtifactMavenController}'s GAV path, + * {@code CodeArtifactNpmDataPlane}'s tarball route, {@code CodeArtifactPypiController}'s raw + * filename-only download route, {@code CodeArtifactNuGetController}'s content route) each + * call this same check too, via {@link #requireContainerBackedVersionDownloadable}, before + * proxying a GET straight to their own sidecar: see that method's own javadoc for why only + * {@code Archived} needs an active block there (a {@code Disposed} version's native download + * already 404s against the sidecar directly, since its bytes are genuinely gone). */ private static void requireAssetDownloadable(String status, String version) { if ("Archived".equals(status) || "Disposed".equals(status)) { @@ -2173,6 +2169,37 @@ public class CodeArtifactService implements Resettable { return override == null ? null : override.revision(); } + /** + * The download-side counterpart to {@link #requireContainerBackedVersionNotArchivedOrDisposed} + * above: blocks a native per-format download route (Maven's GAV path, npm's tarball route, + * nuget's content route, pypi's filename route) from serving an {@code Archived} version's + * bytes, the same way {@link #getContainerBackedPackageVersionAsset} already blocks it for + * {@code GetPackageVersionAsset} itself, via this exact same {@link #requireAssetDownloadable}. + * {@code Disposed} needs no real check here: {@link #deleteContainerBackedVersionAssets} + * already deletes the sidecar's own bytes, so a disposed version's native download already + * 404s against the sidecar directly, with nothing left here to block, but this still covers + * it anyway, so a caller never has to reason about which of the two statuses this does or + * doesn't close. + * + *

Unlike {@link #requireContainerBackedVersionNotArchivedOrDisposed}, this takes an + * already-resolved {@code owner} and {@code repoId} rather than a raw {@code domainOwner}: + * every native controller has already resolved both, {@code owner} from its own token scope, + * {@code repoId} from its own {@link #ensureFormatContainerId} call, before it ever reaches + * this check, so re-resolving either here would only repeat work the caller already paid for. + * + *

Not {@code synchronized}, unlike that counterpart: nothing here writes, so there is no + * race to close, the same reasoning that already leaves {@link + * #getContainerBackedPackageVersionAsset}'s own call to {@link + * #currentContainerBackedVersionStatus} unsynchronized. + */ + public void requireContainerBackedVersionDownloadable(String owner, String region, String domain, + String repository, String format, String namespace, String packageName, String version, + String repoId) { + PackageVersionStatusOverride state = currentContainerBackedVersionStatus(owner, region, domain, repository, + format, namespace, packageName, version, repoId); + requireAssetDownloadable(state.status(), version); + } + /** * Raised by Greptile's review of this same PR: a native publish's own check, real sidecar * write, and override clear are three separate calls (necessarily, since the real write sits @@ -2263,8 +2290,15 @@ public class CodeArtifactService implements Resettable { * then looked up individually in {@link #containerBackedPackageVersionStatusOverrides} itself. * Empty for a package nothing has ever called {@link #updatePackageVersionsStatus} or {@link * #disposePackageVersions} on, which is every container-backed package before this existed. + * + *

Public (not just this class's own internal callers) for the same reason {@link + * #containerBackedOverriddenVersionStatusesByRepository} already is: raised by Greptile's + * review, a real npm tarball GET names a package directly (unlike pypi's bare filename), so + * {@code CodeArtifactNpmDataPlane}'s own download-side guard can check this cheaply, in + * memory, before ever paying for a live Verdaccio packument fetch to resolve which version + * (if any) a requested filename actually belongs to. */ - private Map containerBackedOverridesForPackage(String owner, String region, + public Map containerBackedOverridesForPackage(String owner, String region, String domain, String repository, String format, String namespace, String packageName) { String accountingName = accountingPackageName(format, packageName); Set versions = containerBackedOverriddenVersionsByPackage.get(ownerScopedKey(owner, diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/VerdaccioSidecarClient.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/VerdaccioSidecarClient.java index 755e73623b..4660d83738 100644 --- a/src/main/java/io/github/hectorvent/floci/services/codeartifact/VerdaccioSidecarClient.java +++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/VerdaccioSidecarClient.java @@ -23,6 +23,7 @@ import java.time.Instant; import java.time.format.DateTimeParseException; import java.util.ArrayList; import java.util.Base64; +import java.util.Iterator; import java.util.List; import java.util.Optional; @@ -363,6 +364,33 @@ public class VerdaccioSidecarClient implements RepositorySidecarManager { return tarballFilename.isEmpty() ? null : tarballFilename; } + /** + * The exact version whose tarball Verdaccio's own packument records {@code filename} under, + * found by checking every version's own {@link #tarballFilenameFor} rather than assuming + * {@code filename} embeds its version in the {@code -.tgz} shape this + * emulator's own publish path always writes: nothing in the real npm publish protocol + * requires a client-chosen {@code dist.tarball} name to follow that convention, so {@code + * CodeArtifactNpmDataPlane}'s own download-side Archived guard resolves the real coordinate + * here instead of guessing from the filename's own text. Empty when the package doesn't + * exist, or no version's own tarball matches. + */ + public Optional versionForTarballFilename(String repositoryContainerId, String domain, + String repository, String namespace, String packageName, String filename) { + String baseUrl = ensureReady(repositoryContainerId, publicUrl(domain, repository)); + JsonNode document = fetchPackageDocument(baseUrl, namespace, packageName).orElse(null); + if (document == null) { + return Optional.empty(); + } + Iterator versions = document.path("versions").fieldNames(); + while (versions.hasNext()) { + String version = versions.next(); + if (filename.equals(tarballFilenameFor(document, version))) { + return Optional.of(version); + } + } + return Optional.empty(); + } + /** * npm has exactly one asset per version, the tarball: unlike Maven's version directory, there * is no separate checksum sidecar file or secondary artifact to enumerate. Empty when the diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactMavenDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactMavenDockerIntegrationTest.java index d5e40ce136..69c6585b60 100644 --- a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactMavenDockerIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactMavenDockerIntegrationTest.java @@ -695,6 +695,60 @@ class CodeArtifactMavenDockerIntegrationTest { assertEquals("com.example.nested", namespaces.get(names.indexOf("listpkgsprobe"))); } + /** + * Closes 10-NATIVE-STATUS for Maven: a plain {@code mvn dependency:get} fetch of an Archived + * version's real GAV path must 404 against the real Reposilite sidecar, even though the jar's + * own bytes are still sitting there untouched (unlike {@link + * #disposePackageVersionsDeletesTheRealReposiliteAssetsAndKeepsListingTheVersionAsDisposed}'s + * own 404, which comes from Reposilite genuinely no longer having the file). + */ + @Test + @Order(24) + void deployThenArchiveBlocksBothGetAndHeadAgainstTheRealReposiliteSidecarWhileLeavingTheBytesInPlace() { + byte[] content = "archive-me-bytes".getBytes(StandardCharsets.UTF_8); + String gav = "com/example/archiveme/1.0.0/archiveme-1.0.0.jar"; + given().header("Authorization", "Bearer " + bearerToken).body(content) + .put("/codeartifact/maven/" + DOMAIN + "/" + REPO + "/" + gav) + .then().statusCode(200); + + given().contentType("application/json").header("Authorization", AUTH) + .body("{\"versions\":[\"1.0.0\"],\"targetStatus\":\"Archived\"}") + .post("/v1/package/versions/update_status?domain=" + DOMAIN + "&repository=" + REPO + + "&format=maven&namespace=com.example&package=archiveme") + .then().statusCode(200) + .body("successfulVersions['1.0.0'].status", equalTo("Archived")); + + given().header("Authorization", "Bearer " + bearerToken) + .get("/codeartifact/maven/" + DOMAIN + "/" + REPO + "/" + gav) + .then().statusCode(404); + given().header("Authorization", "Bearer " + bearerToken) + .head("/codeartifact/maven/" + DOMAIN + "/" + REPO + "/" + gav) + .then().statusCode(404); + + given().header("Authorization", AUTH) + .post("/v1/package/versions?domain=" + DOMAIN + "&repository=" + REPO + "&format=maven" + + "&namespace=com.example&package=archiveme&status=Archived") + .then().statusCode(200) + .body("versions[0].version", equalTo("1.0.0")) + .body("versions[0].status", equalTo("Archived")); + + // Proves the block above is this check's own override, not Reposilite having genuinely + // lost the file the way the dispose test's 404 does: un-archiving must make the exact + // same bytes fetchable again, with nothing re-deployed in between. + given().contentType("application/json").header("Authorization", AUTH) + .body("{\"versions\":[\"1.0.0\"],\"targetStatus\":\"Published\"}") + .post("/v1/package/versions/update_status?domain=" + DOMAIN + "&repository=" + REPO + + "&format=maven&namespace=com.example&package=archiveme") + .then().statusCode(200) + .body("successfulVersions['1.0.0'].status", equalTo("Published")); + byte[] fetchedAfterUnarchive = given().header("Authorization", "Bearer " + bearerToken) + .get("/codeartifact/maven/" + DOMAIN + "/" + REPO + "/" + gav) + .then().statusCode(200) + .extract().asByteArray(); + assertEquals(new String(content, StandardCharsets.UTF_8), + new String(fetchedAfterUnarchive, StandardCharsets.UTF_8)); + } + /** * A throwaway groupId/artifactId, not {@link #GAV}: deleting it exercises Reposilite's real * directory delete (confirmed live before this was built: a single {@code DELETE} on the diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNpmDataPlaneTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNpmDataPlaneTest.java index 1ec26732cf..9a9a612e99 100644 --- a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNpmDataPlaneTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNpmDataPlaneTest.java @@ -45,6 +45,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -899,6 +900,162 @@ class CodeArtifactNpmDataPlaneTest { assertEquals(true, upstreamReceivedPut.get()); } + /** + * Closes 10-NATIVE-STATUS for npm: a plain {@code npm install} download of a tarball whose + * version this service has Archived must 404 without ever reaching Verdaccio, the same way a + * publish to that coordinate is already refused above. + */ + @Test + void aTarballDownloadOfAnArchivedVersionIsBlockedWithoutForwarding() throws Exception { + AtomicReference upstreamReceivedGet = new AtomicReference<>(false); + upstream = vertx.createHttpServer() + .requestHandler(request -> { + if (request.method() == HttpMethod.GET) { + upstreamReceivedGet.set(true); + } + request.response().setStatusCode(200).end("tarball bytes"); + }) + .listen(0, "127.0.0.1") + .toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS); + + CodeArtifactService service = mock(CodeArtifactService.class); + when(service.resolveAuthorizationToken("good-token", DOMAIN)) + .thenReturn(Optional.of(new AuthorizationTokenScope("000000000000", "us-east-1"))); + when(service.ensureFormatContainerId("npm", "us-east-1", DOMAIN, "000000000000", REPOSITORY)) + .thenReturn(NPM_REPOSITORY_ID); + doReturn(Map.of("1.0.0", new Object())).when(service).containerBackedOverridesForPackage( + eq("000000000000"), eq("us-east-1"), eq(DOMAIN), eq(REPOSITORY), eq("npm"), isNull(), + eq("my-pkg")); + doThrow(new AwsException("ResourceNotFoundException", "Package version '1.0.0' was not found.", 404)) + .when(service).requireContainerBackedVersionDownloadable(eq("000000000000"), eq("us-east-1"), + eq(DOMAIN), eq(REPOSITORY), eq("npm"), isNull(), eq("my-pkg"), eq("1.0.0"), + eq(NPM_REPOSITORY_ID)); + VerdaccioSidecarClient verdaccioClient = mock(VerdaccioSidecarClient.class); + when(verdaccioClient.ensureReady(eq(NPM_REPOSITORY_ID), anyString())) + .thenReturn("http://127.0.0.1:" + upstream.actualPort()); + when(verdaccioClient.versionForTarballFilename(eq(NPM_REPOSITORY_ID), eq(DOMAIN), eq(REPOSITORY), isNull(), + eq("my-pkg"), eq("my-pkg-1.0.0.tgz"))).thenReturn(Optional.of("1.0.0")); + startDataPlane(service, verdaccioClient); + + HttpResponse response = get("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/my-pkg/-/my-pkg-1.0.0.tgz", + "good-token"); + + assertEquals(404, response.statusCode()); + assertEquals(false, upstreamReceivedGet.get()); + } + + /** + * Raised by Greptile's review: a real npm {@code dist.tarball} filename is not required to + * embed its version at all, so this must resolve the real coordinate against Verdaccio's own + * packument, not by stripping a {@code -.tgz} shape out of the filename + * text. A tarball literally named {@code bundle.tgz} for version {@code 1.0.0} must still be + * blocked once that version is Archived. + */ + @Test + void aTarballDownloadWhoseFilenameDoesNotEmbedItsVersionIsStillBlockedWhenArchived() throws Exception { + AtomicReference upstreamReceivedGet = new AtomicReference<>(false); + upstream = vertx.createHttpServer() + .requestHandler(request -> { + if (request.method() == HttpMethod.GET) { + upstreamReceivedGet.set(true); + } + request.response().setStatusCode(200).end("tarball bytes"); + }) + .listen(0, "127.0.0.1") + .toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS); + + CodeArtifactService service = mock(CodeArtifactService.class); + when(service.resolveAuthorizationToken("good-token", DOMAIN)) + .thenReturn(Optional.of(new AuthorizationTokenScope("000000000000", "us-east-1"))); + when(service.ensureFormatContainerId("npm", "us-east-1", DOMAIN, "000000000000", REPOSITORY)) + .thenReturn(NPM_REPOSITORY_ID); + doReturn(Map.of("1.0.0", new Object())).when(service).containerBackedOverridesForPackage( + eq("000000000000"), eq("us-east-1"), eq(DOMAIN), eq(REPOSITORY), eq("npm"), isNull(), + eq("my-pkg")); + doThrow(new AwsException("ResourceNotFoundException", "Package version '1.0.0' was not found.", 404)) + .when(service).requireContainerBackedVersionDownloadable(eq("000000000000"), eq("us-east-1"), + eq(DOMAIN), eq(REPOSITORY), eq("npm"), isNull(), eq("my-pkg"), eq("1.0.0"), + eq(NPM_REPOSITORY_ID)); + VerdaccioSidecarClient verdaccioClient = mock(VerdaccioSidecarClient.class); + when(verdaccioClient.ensureReady(eq(NPM_REPOSITORY_ID), anyString())) + .thenReturn("http://127.0.0.1:" + upstream.actualPort()); + when(verdaccioClient.versionForTarballFilename(eq(NPM_REPOSITORY_ID), eq(DOMAIN), eq(REPOSITORY), isNull(), + eq("my-pkg"), eq("bundle.tgz"))).thenReturn(Optional.of("1.0.0")); + startDataPlane(service, verdaccioClient); + + HttpResponse response = get("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/my-pkg/-/bundle.tgz", + "good-token"); + + assertEquals(404, response.statusCode()); + assertEquals(false, upstreamReceivedGet.get()); + } + + /** + * The scope has to be decoded off the path and passed through as the namespace before the + * check even runs, the same {@code %2F}/{@code %2f} decoding {@link + * CodeArtifactNpmDataPlane#packageIdentityFromPath} already does for a publish; an + * unarchived version still proxies through once the check passes. + */ + @Test + void aTarballDownloadOfAScopedPackageParsesTheNamespaceBeforeCheckingTheOverride() throws Exception { + upstream = vertx.createHttpServer() + .requestHandler(request -> request.response().setStatusCode(200).end("tarball bytes")) + .listen(0, "127.0.0.1") + .toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS); + + CodeArtifactService service = mock(CodeArtifactService.class); + when(service.resolveAuthorizationToken("good-token", DOMAIN)) + .thenReturn(Optional.of(new AuthorizationTokenScope("000000000000", "us-east-1"))); + when(service.ensureFormatContainerId("npm", "us-east-1", DOMAIN, "000000000000", REPOSITORY)) + .thenReturn(NPM_REPOSITORY_ID); + doReturn(Map.of("1.0.0", new Object())).when(service).containerBackedOverridesForPackage( + eq("000000000000"), eq("us-east-1"), eq(DOMAIN), eq(REPOSITORY), eq("npm"), eq("myscope"), + eq("my-pkg")); + VerdaccioSidecarClient verdaccioClient = mock(VerdaccioSidecarClient.class); + when(verdaccioClient.ensureReady(eq(NPM_REPOSITORY_ID), anyString())) + .thenReturn("http://127.0.0.1:" + upstream.actualPort()); + when(verdaccioClient.versionForTarballFilename(eq(NPM_REPOSITORY_ID), eq(DOMAIN), eq(REPOSITORY), + eq("myscope"), eq("my-pkg"), eq("my-pkg-1.0.0.tgz"))).thenReturn(Optional.of("1.0.0")); + startDataPlane(service, verdaccioClient); + + HttpResponse response = get("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + + "/@myscope%2fmy-pkg/-/my-pkg-1.0.0.tgz", "good-token"); + + assertEquals(200, response.statusCode()); + verify(service).requireContainerBackedVersionDownloadable(eq("000000000000"), eq("us-east-1"), eq(DOMAIN), + eq(REPOSITORY), eq("npm"), eq("myscope"), eq("my-pkg"), eq("1.0.0"), eq(NPM_REPOSITORY_ID)); + } + + /** + * Guards against the marker detection over-triggering: a plain packument fetch (no {@code /-/} + * in the path at all) must never reach the download guard, the same way {@link + * CodeArtifactNpmDataPlane#nativeUnpublishTarget} only matches its own {@code /-rev/} marker + * and leaves every other shape alone. + */ + @Test + void aPlainPackageMetadataGetNeverConsultsTheDownloadGuard() throws Exception { + upstream = vertx.createHttpServer() + .requestHandler(request -> request.response().setStatusCode(200).end("{}")) + .listen(0, "127.0.0.1") + .toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS); + + CodeArtifactService service = mock(CodeArtifactService.class); + when(service.resolveAuthorizationToken("good-token", DOMAIN)) + .thenReturn(Optional.of(new AuthorizationTokenScope("000000000000", "us-east-1"))); + when(service.ensureFormatContainerId("npm", "us-east-1", DOMAIN, "000000000000", REPOSITORY)) + .thenReturn(NPM_REPOSITORY_ID); + VerdaccioSidecarClient verdaccioClient = mock(VerdaccioSidecarClient.class); + when(verdaccioClient.ensureReady(eq(NPM_REPOSITORY_ID), anyString())) + .thenReturn("http://127.0.0.1:" + upstream.actualPort()); + startDataPlane(service, verdaccioClient); + + HttpResponse response = get("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/my-pkg", "good-token"); + + assertEquals(200, response.statusCode()); + verify(service, never()).requireContainerBackedVersionDownloadable(any(), any(), any(), any(), any(), any(), + any(), any(), any()); + } + private void startDataPlane(CodeArtifactService service, VerdaccioSidecarClient verdaccioClient) throws Exception { startDataPlane(service, verdaccioClient, true); diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNpmDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNpmDockerIntegrationTest.java index 33699d3964..d10a34771b 100644 --- a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNpmDockerIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNpmDockerIntegrationTest.java @@ -824,6 +824,55 @@ class CodeArtifactNpmDockerIntegrationTest { assertEquals("myscope", namespaces.get(names.indexOf(scopedPackage))); } + /** + * Closes 10-NATIVE-STATUS for npm: a plain {@code npm install} download of an Archived + * version's real tarball must 404 against the live Verdaccio sidecar, even though the tarball + * itself is still sitting there untouched. Its own package, not {@link #PACKAGE_NAME}, so + * archiving it can't disturb any other ordered test in this class. + */ + @Test + @Order(22) + void archivingAVersionBlocksItsRealVerdaccioTarballDownloadWhileLeavingTheBytesInPlace() { + String packageName = "archive-download-pkg"; + byte[] tarball = "archive download throwaway tarball".getBytes(StandardCharsets.UTF_8); + String envelope = "{\"name\":\"" + packageName + "\",\"versions\":{\"1.0.0\":{\"name\":\"" + packageName + + "\",\"version\":\"1.0.0\",\"dist\":{\"tarball\":\"http://ignored/" + packageName + "-1.0.0.tgz\"}}}," + + "\"_attachments\":{\"" + packageName + "-1.0.0.tgz\":{\"content_type\":\"application/octet-stream\"," + + "\"data\":\"" + Base64.getEncoder().encodeToString(tarball) + "\",\"length\":" + tarball.length + + "}}}"; + given().header("Authorization", "Bearer " + bearerToken).contentType("application/json").body(envelope) + .put("/codeartifact/npm/" + DOMAIN + "/" + REPO + "/" + packageName) + .then().statusCode(anyOf201Or200()); + + given().contentType("application/json").header("Authorization", AUTH) + .body("{\"versions\":[\"1.0.0\"],\"targetStatus\":\"Archived\"}") + .post("/v1/package/versions/update_status?domain=" + DOMAIN + "&repository=" + REPO + + "&format=npm&package=" + packageName) + .then().statusCode(200) + .body("successfulVersions['1.0.0'].status", equalTo("Archived")); + + given().header("Authorization", "Bearer " + bearerToken) + .get("/codeartifact/npm/" + DOMAIN + "/" + REPO + "/" + packageName + "/-/" + packageName + + "-1.0.0.tgz") + .then().statusCode(404); + + // Proves the block above is this check's own override, not Verdaccio having genuinely + // lost the tarball: un-archiving must make the exact same bytes fetchable again, with + // nothing re-published in between. + given().contentType("application/json").header("Authorization", AUTH) + .body("{\"versions\":[\"1.0.0\"],\"targetStatus\":\"Published\"}") + .post("/v1/package/versions/update_status?domain=" + DOMAIN + "&repository=" + REPO + + "&format=npm&package=" + packageName) + .then().statusCode(200) + .body("successfulVersions['1.0.0'].status", equalTo("Published")); + byte[] fetchedAfterUnarchive = given().header("Authorization", "Bearer " + bearerToken) + .get("/codeartifact/npm/" + DOMAIN + "/" + REPO + "/" + packageName + "/-/" + packageName + + "-1.0.0.tgz") + .then().statusCode(200) + .extract().asByteArray(); + assertArrayEquals(tarball, fetchedAfterUnarchive); + } + /** * Proves the actual mechanism, not just that {@code VerdaccioSidecarManager} has a method * named right: {@code ContainerTeardowns.stopAll} runs every {@code ContainerTeardown} on diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNuGetDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNuGetDockerIntegrationTest.java index 0ebd651d40..46c2e25631 100644 --- a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNuGetDockerIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNuGetDockerIntegrationTest.java @@ -514,6 +514,53 @@ class CodeArtifactNuGetDockerIntegrationTest { + packageId.toLowerCase(Locale.ROOT) + " among " + names); } + /** + * Closes 10-NATIVE-STATUS for nuget: a plain {@code dotnet restore}/{@code nuget install} + * download of an Archived version's real {@code .nupkg} must 404 against the live BaGetter + * sidecar, even though the file itself is still sitting there untouched. Its own package, not + * {@link #PACKAGE_ID}, so archiving it can't disturb any other ordered test in this class. + */ + @Test + @Order(17) + void archivingAVersionBlocksItsRealBagetterContentDownloadWhileLeavingTheBytesInPlace() { + String packageId = "Archive.Download.Package"; + String lowerId = packageId.toLowerCase(Locale.ROOT); + byte[] nupkg = buildNupkg(packageId, VERSION, "Archive download throwaway.", null, null); + given().header("Authorization", "Bearer " + bearerToken) + .contentType("multipart/form-data; boundary=boundary123") + .body(pushBody("boundary123", nupkg)) + .put("/codeartifact/nuget/" + DOMAIN + "/" + REPO + "/api/v2/package") + .then().statusCode(201); + + given().contentType("application/json").header("Authorization", AUTH) + .body("{\"versions\":[\"" + VERSION + "\"],\"targetStatus\":\"Archived\"}") + .post("/v1/package/versions/update_status?domain=" + DOMAIN + "&repository=" + REPO + + "&format=nuget&package=" + packageId) + .then().statusCode(200) + .body("successfulVersions['" + VERSION + "'].status", equalTo("Archived")); + + given().header("Authorization", "Bearer " + bearerToken) + .get("/codeartifact/nuget/" + DOMAIN + "/" + REPO + "/v3/package/" + lowerId + "/" + VERSION + "/" + + lowerId + "." + VERSION + ".nupkg") + .then().statusCode(404); + + // Proves the block above is this check's own override, not BaGetter having genuinely + // lost the file: un-archiving must make the exact same bytes fetchable again, with + // nothing re-pushed in between. + given().contentType("application/json").header("Authorization", AUTH) + .body("{\"versions\":[\"" + VERSION + "\"],\"targetStatus\":\"Published\"}") + .post("/v1/package/versions/update_status?domain=" + DOMAIN + "&repository=" + REPO + + "&format=nuget&package=" + packageId) + .then().statusCode(200) + .body("successfulVersions['" + VERSION + "'].status", equalTo("Published")); + byte[] fetchedAfterUnarchive = given().header("Authorization", "Bearer " + bearerToken) + .get("/codeartifact/nuget/" + DOMAIN + "/" + REPO + "/v3/package/" + lowerId + "/" + VERSION + "/" + + lowerId + "." + VERSION + ".nupkg") + .then().statusCode(200) + .extract().asByteArray(); + assertArrayEquals(nupkg, fetchedAfterUnarchive); + } + private static long describeDomainAssetSizeBytes() { return given().header("Authorization", AUTH) .get("/v1/domain?domain=" + DOMAIN) diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiControllerTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiControllerTest.java index 1456b0b9ad..c540de0c35 100644 --- a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiControllerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiControllerTest.java @@ -77,6 +77,11 @@ class CodeArtifactPypiControllerTest { .thenReturn(Optional.of(new AuthorizationTokenScope(OWNER, REGION))); when(service.ensureFormatContainerId(eq("pypi"), eq(REGION), eq(DOMAIN), eq(OWNER), eq(REPOSITORY))) .thenReturn("pypi-repo-id"); + // Matches a real repository with no UpdatePackageVersionsStatus/DisposePackageVersions + // call ever made against it: archivedOrDisposedVersionFromFilename's own scan has nothing + // to check, so downloadFile proxies straight through unless a test below overrides this. + when(service.containerBackedOverriddenVersionStatusesByRepository(eq(REGION), eq(DOMAIN), eq(OWNER), + eq(REPOSITORY), eq("pypi"))).thenReturn(Map.of()); when(pypiserver.ensureReady(eq("pypi-repo-id"), any())).thenReturn(backendUrl); HttpClient httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build(); @@ -183,6 +188,62 @@ class CodeArtifactPypiControllerTest { assertEquals("/packages/..%2Fsecret.tar.gz", observedRawPath.get()); } + /** + * Closes 10-NATIVE-STATUS for pypi: {@code downloadFile}'s own route is a bare filename with + * no package name or version anywhere in the URL, unlike Maven's GAV path or nuget's content + * route, so the coordinate {@code requireContainerBackedVersionDownloadable} needs is resolved + * by matching the filename against this repository's own Archived/Disposed status overrides + * (see {@code archivedOrDisposedVersionFromFilename}), not a separate map recorded only at + * upload time: raised by Greptile's review, that earlier mechanism was never populated by a + * version {@code CopyPackageVersions} wrote straight to the destination sidecar, so archiving + * a copied version never blocked its download. Resolving against the override records + * instead works regardless of how the real file got there. + */ + @Test + void downloadFileBlocksAFileWhoseVersionIsArchived() { + // No controller.upload call at all, deliberately: proves this resolves against the + // override records themselves, not anything an upload would have recorded, the exact + // case a version CopyPackageVersions wrote straight to the destination sidecar is in. + when(service.containerBackedOverriddenVersionStatusesByRepository(eq(REGION), eq(DOMAIN), eq(OWNER), + eq(REPOSITORY), eq("pypi"))).thenReturn(Map.of("my-pkg", Map.of("1.0.0", "Archived"))); + + AtomicBoolean downloadReceived = new AtomicBoolean(); + backend.createContext("/packages/my_pkg-1.0.0.tar.gz", exchange -> { + downloadReceived.set(true); + respond(exchange, 200, "application/octet-stream", "new content"); + }); + doThrow(new AwsException("ResourceNotFoundException", "Package version '1.0.0' was not found.", 404)) + .when(service).requireContainerBackedVersionDownloadable(eq(OWNER), eq(REGION), eq(DOMAIN), + eq(REPOSITORY), eq("pypi"), isNull(), eq("my-pkg"), eq("1.0.0"), eq("pypi-repo-id")); + + Response response = controller.downloadFile(headers("Bearer good"), DOMAIN, REPOSITORY, + "my_pkg-1.0.0.tar.gz"); + + assertEquals(404, response.getStatus()); + assertEquals(false, downloadReceived.get()); + } + + /** + * The overwhelmingly common case: a repository nothing has ever called {@code + * UpdatePackageVersionsStatus}/{@code DisposePackageVersions} against has no overrides at all + * (the default this class's own {@code setUp} already stubs), so {@code + * archivedOrDisposedVersionFromFilename} finds nothing to match and the download proxies + * straight through without ever consulting {@code requireContainerBackedVersionDownloadable}. + */ + @Test + void downloadFileOfAnUntrackedFilenameSkipsTheCheckAndStillProxies() { + byte[] content = "package bytes".getBytes(StandardCharsets.UTF_8); + backend.createContext("/packages/never-uploaded-1.0.tar.gz", + exchange -> respond(exchange, 200, "application/octet-stream", content)); + + Response response = controller.downloadFile(headers("Bearer good"), DOMAIN, REPOSITORY, + "never-uploaded-1.0.tar.gz"); + + assertEquals(200, response.getStatus()); + verify(service, never()).requireContainerBackedVersionDownloadable(any(), any(), any(), any(), any(), any(), + any(), any(), any()); + } + /** * Same finding again, via the other wire-protocol route that shares the same {@code proxyGet} * helper: the package name in {@code GET /simple/{package}/} is just as caller-supplied as diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiDockerIntegrationTest.java index bf1ed139f6..aa043a0b2e 100644 --- a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiDockerIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiDockerIntegrationTest.java @@ -798,6 +798,56 @@ class CodeArtifactPypiDockerIntegrationTest { assertTrue(names.contains("list-packages-probe"), "expected list-packages-probe among " + names); } + /** + * Closes 10-NATIVE-STATUS for pypi: a plain {@code pip install} download of an Archived + * version's real file must 404 against the live pypiserver sidecar, even though the file + * itself is still sitting there untouched. This is the one format whose download route is a + * bare filename with no package name or version in the URL at all (see {@code + * CodeArtifactPypiController#fileIdentitiesByRepo}'s own javadoc), so this also proves the + * identity this test's own upload recorded is what the download route resolved the coordinate + * from. Its own package, not {@link #PACKAGE_NAME}, so archiving it can't disturb any other + * ordered test in this class. + */ + @Test + @Order(22) + void archivingAVersionBlocksItsRealPypiserverFileDownloadWhileLeavingTheBytesInPlace() { + String packageName = "archive-download-pypi-pkg"; + String filename = "archive_download_pypi_pkg-1.0.0.tar.gz"; + byte[] content = "archive download throwaway sdist".getBytes(StandardCharsets.UTF_8); + given().header("Authorization", "Bearer " + bearerToken) + .contentType("multipart/form-data; boundary=boundary123") + .body(twineUploadBody("boundary123", packageName, filename, content)) + .post("/codeartifact/pypi/" + DOMAIN + "/" + REPO + "/") + .then().statusCode(200); + + given().contentType("application/json").header("Authorization", AUTH) + .body("{\"versions\":[\"1.0.0\"],\"targetStatus\":\"Archived\"}") + .post("/v1/package/versions/update_status?domain=" + DOMAIN + "&repository=" + REPO + + "&format=pypi&package=" + packageName) + .then().statusCode(200) + .body("successfulVersions['1.0.0'].status", equalTo("Archived")); + + given().header("Authorization", "Bearer " + bearerToken) + .get("/codeartifact/pypi/" + DOMAIN + "/" + REPO + "/packages/" + filename) + .then().statusCode(404); + + // Proves the block above is this check's own override, not pypiserver having genuinely + // lost the file: un-archiving must make the exact same bytes fetchable again, with + // nothing re-uploaded in between. + given().contentType("application/json").header("Authorization", AUTH) + .body("{\"versions\":[\"1.0.0\"],\"targetStatus\":\"Published\"}") + .post("/v1/package/versions/update_status?domain=" + DOMAIN + "&repository=" + REPO + + "&format=pypi&package=" + packageName) + .then().statusCode(200) + .body("successfulVersions['1.0.0'].status", equalTo("Published")); + byte[] fetchedAfterUnarchive = given().header("Authorization", "Bearer " + bearerToken) + .get("/codeartifact/pypi/" + DOMAIN + "/" + REPO + "/packages/" + filename) + .then().statusCode(200) + .extract().asByteArray(); + assertEquals(new String(content, StandardCharsets.UTF_8), + new String(fetchedAfterUnarchive, StandardCharsets.UTF_8)); + } + /** * Proves the actual mechanism, not just that {@code PypiserverSidecarManager} has a method * named right: {@code ContainerTeardowns.stopAll} runs every {@code ContainerTeardown} on diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactServiceTest.java index e3a22e1e27..d6dd291094 100644 --- a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactServiceTest.java @@ -4768,6 +4768,52 @@ class CodeArtifactServiceTest { "repo", "maven", "com.example", "a", "4.0-never-touched")); } + /** + * The download-side counterpart: every native per-format proxy controller (Maven, npm, nuget, + * pypi) calls this before serving bytes straight from its own sidecar, the mechanism that + * closes 10-NATIVE-STATUS. Must block only {@code Archived} (the status whose real sidecar + * bytes are deliberately left in place, unlike {@code Disposed}, which already deletes them) + * and let every other status, including a version never touched at all, through unchanged, + * the exact same four-way split {@link #requireContainerBackedVersionNotArchivedOrDisposedBlocksOnlyThoseTwoStatuses} + * already proves for the upload side, with {@code Disposed} included here too even though a + * real native download of a disposed version would already 404 against the sidecar itself for + * an unrelated reason (its bytes are gone), since this method's own contract blocks both + * statuses regardless of whether the sidecar would have caught it anyway. + */ + @Test + void requireContainerBackedVersionDownloadableBlocksOnlyArchivedAndDisposed() { + service.createDomain(REGION, "dom", null, Map.of()); + CodeArtifactRepository created = service.createRepository(REGION, "dom", null, "repo", null, null, Map.of()); + String repoId = created.getSidecarContainerIds().get("maven"); + when(reposiliteClient.listPackageVersions(eq(repoId), eq("dom"), eq("repo"), eq("com.example"), + eq("a"))).thenReturn(List.of( + new RepositorySidecarManager.PackageVersionTimestamp("1.0", Instant.ofEpochSecond(1000)), + new RepositorySidecarManager.PackageVersionTimestamp("2.0", Instant.ofEpochSecond(1000)), + new RepositorySidecarManager.PackageVersionTimestamp("3.0", Instant.ofEpochSecond(1000)))); + + service.updatePackageVersionsStatus(REGION, "dom", null, "repo", "maven", "com.example", "a", + List.of("1.0"), null, null, "Archived"); + AwsException archivedError = assertThrows(AwsException.class, + () -> service.requireContainerBackedVersionDownloadable(ACCOUNT_ID, REGION, "dom", "repo", "maven", + "com.example", "a", "1.0", repoId)); + assertEquals("ResourceNotFoundException", archivedError.getErrorCode()); + + service.disposePackageVersions(REGION, "dom", null, "repo", "maven", "com.example", "a", List.of("2.0"), + null, null); + AwsException disposedError = assertThrows(AwsException.class, + () -> service.requireContainerBackedVersionDownloadable(ACCOUNT_ID, REGION, "dom", "repo", "maven", + "com.example", "a", "2.0", repoId)); + assertEquals("ResourceNotFoundException", disposedError.getErrorCode()); + + service.updatePackageVersionsStatus(REGION, "dom", null, "repo", "maven", "com.example", "a", + List.of("3.0"), null, null, "Unlisted"); + assertDoesNotThrow(() -> service.requireContainerBackedVersionDownloadable(ACCOUNT_ID, REGION, "dom", "repo", + "maven", "com.example", "a", "3.0", repoId)); + + assertDoesNotThrow(() -> service.requireContainerBackedVersionDownloadable(ACCOUNT_ID, REGION, "dom", "repo", + "maven", "com.example", "a", "4.0-never-touched", repoId)); + } + /** * Raised by Greptile's review: {@code requireContainerBackedVersionNotArchivedOrDisposed}'s * guard, the real sidecar write, and this call are necessarily three separate steps (the diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/VerdaccioSidecarClientTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/VerdaccioSidecarClientTest.java index a1f2660609..4bb3295d38 100644 --- a/src/test/java/io/github/hectorvent/floci/services/codeartifact/VerdaccioSidecarClientTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/VerdaccioSidecarClientTest.java @@ -275,6 +275,38 @@ class VerdaccioSidecarClientTest { assertTrue(packages.isEmpty()); } + /** + * Raised by Greptile's review: a real npm {@code dist.tarball} filename is not required to + * embed its version at all, so resolving a download's archived-status check by guessing a + * {@code -.tgz} shape out of the filename's own text can both miss a + * real tarball named something else entirely and misread an unrelated version out of one + * that happens to coincidentally fit that shape. This resolves against the packument's own + * {@code dist.tarball} instead, confirmed here with a tarball named nothing like its version. + */ + @Test + void versionForTarballFilenameResolvesAFilenameThatDoesNotEmbedItsVersion() throws Exception { + VerdaccioSidecarManager manager = manager(); + seedPooledContainer(manager, "npm-repo-1", "tracked-verdaccio-1", backendUrl); + respondWithMetadata("/demo-pkg", "1.0.0", "/demo-pkg/-/bundle.tgz"); + + Optional version = client(manager).versionForTarballFilename("npm-repo-1", "dom", "repo", null, + "demo-pkg", "bundle.tgz"); + + assertEquals(Optional.of("1.0.0"), version); + } + + @Test + void versionForTarballFilenameIsEmptyWhenNoVersionsTarballMatches() throws Exception { + VerdaccioSidecarManager manager = manager(); + seedPooledContainer(manager, "npm-repo-1", "tracked-verdaccio-1", backendUrl); + respondWithMetadata("/demo-pkg", "1.0.0", "/demo-pkg/-/demo-pkg-1.0.0.tgz"); + + Optional version = client(manager).versionForTarballFilename("npm-repo-1", "dom", "repo", null, + "demo-pkg", "never-published.tgz"); + + assertEquals(Optional.empty(), version); + } + private VerdaccioSidecarManager manager() { return new VerdaccioSidecarManager(containerBuilder, lifecycleManager, config); }