diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactMavenExternalConnectionDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactMavenExternalConnectionDockerIntegrationTest.java index afd8525009..e852ac0011 100644 --- a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactMavenExternalConnectionDockerIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactMavenExternalConnectionDockerIntegrationTest.java @@ -12,6 +12,7 @@ import org.junit.jupiter.api.TestMethodOrder; import java.nio.charset.StandardCharsets; +import static io.github.hectorvent.floci.testing.CodeArtifactRequests.putPackageOriginConfiguration; import static io.restassured.RestAssured.given; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; @@ -144,11 +145,7 @@ class CodeArtifactMavenExternalConnectionDockerIntegrationTest { @Test @Order(4) void upstreamBlockPreventsRetentionEvenThoughTheRealArtifactExists() { - given().contentType("application/json").header("Authorization", AUTH) - .body("{\"restrictions\":{\"publish\":\"ALLOW\",\"upstream\":\"BLOCK\"}}") - .post("/v1/package?domain=" + DOMAIN + "&repository=" + BLOCKED_REPO + "&format=maven" - + "&namespace=junit&package=junit") - .then().statusCode(200); + putPackageOriginConfiguration(AUTH, DOMAIN, BLOCKED_REPO, "maven", "junit", "junit", "ALLOW", "BLOCK"); given().header("Authorization", "Bearer " + bearerToken) .get("/codeartifact/maven/" + DOMAIN + "/" + BLOCKED_REPO + "/" + JUNIT_JAR) 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 49d3a457bd..4722555d47 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 @@ -22,8 +22,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.nio.charset.StandardCharsets; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Base64; import java.util.HashMap; @@ -41,6 +39,8 @@ import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; +import static io.github.hectorvent.floci.testing.CodeArtifactRequests.npmIntegrity; +import static io.github.hectorvent.floci.testing.CodeArtifactRequests.npmPublishEnvelope; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -268,7 +268,7 @@ class CodeArtifactNpmDataPlaneTest { startDataPlane(service, verdaccioClient); HttpResponse response = put("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/my-pkg", "good-token", - publishEnvelope("my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", content)); + npmPublishEnvelope("my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", content)); assertEquals(200, response.statusCode()); assertEquals(false, upstreamReceivedPut.get()); @@ -306,7 +306,7 @@ class CodeArtifactNpmDataPlaneTest { startDataPlane(service, verdaccioClient); HttpResponse response = put("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/my-pkg", "good-token", - publishEnvelope("my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", newContent)); + npmPublishEnvelope("my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", newContent)); assertEquals(409, response.statusCode()); assertEquals(true, upstreamReceivedPut.get()); @@ -340,7 +340,8 @@ class CodeArtifactNpmDataPlaneTest { startDataPlane(service, verdaccioClient); HttpResponse response = put("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/my-pkg", "good-token", - publishEnvelope("my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", "fresh bytes".getBytes(StandardCharsets.UTF_8))); + npmPublishEnvelope("my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", + "fresh bytes".getBytes(StandardCharsets.UTF_8))); assertEquals(201, response.statusCode()); assertEquals(true, upstreamReceivedPut.get()); @@ -369,7 +370,7 @@ class CodeArtifactNpmDataPlaneTest { startDataPlane(service, verdaccioClient); HttpResponse response = put("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/my-pkg", "good-token", - publishEnvelope("my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", + npmPublishEnvelope("my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", "fresh bytes".getBytes(StandardCharsets.UTF_8))); assertEquals(201, response.statusCode()); @@ -411,7 +412,7 @@ class CodeArtifactNpmDataPlaneTest { startDataPlane(service, verdaccioClient); HttpResponse response = put("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/my-pkg", "good-token", - publishEnvelope("my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", + npmPublishEnvelope("my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", "fresh bytes".getBytes(StandardCharsets.UTF_8))); assertEquals(409, response.statusCode()); @@ -439,7 +440,7 @@ class CodeArtifactNpmDataPlaneTest { startDataPlane(service, verdaccioClient); HttpResponse response = put("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/my-pkg", "good-token", - publishEnvelope("my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", content)); + npmPublishEnvelope("my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", content)); assertEquals(409, response.statusCode()); assertEquals(false, upstreamReceivedPut.get()); @@ -483,7 +484,7 @@ class CodeArtifactNpmDataPlaneTest { startDataPlane(service, verdaccioClient); HttpResponse response = put("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/@scope/my-pkg", "good-token", - publishEnvelope("@scope/my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", + npmPublishEnvelope("@scope/my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", "fresh bytes".getBytes(StandardCharsets.UTF_8))); assertEquals(409, response.statusCode()); @@ -512,7 +513,7 @@ class CodeArtifactNpmDataPlaneTest { startDataPlane(service, verdaccioClient); HttpResponse response = put("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/@scope/my-pkg", "good-token", - publishEnvelope("@scope/my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", + npmPublishEnvelope("@scope/my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", "fresh bytes".getBytes(StandardCharsets.UTF_8))); assertEquals(409, response.statusCode()); @@ -546,7 +547,7 @@ class CodeArtifactNpmDataPlaneTest { startDataPlane(service, verdaccioClient); HttpResponse response = put("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/@scope/my-pkg", "good-token", - publishEnvelope("@scope/my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", + npmPublishEnvelope("@scope/my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", "fresh bytes".getBytes(StandardCharsets.UTF_8))); assertEquals(201, response.statusCode()); @@ -640,7 +641,7 @@ class CodeArtifactNpmDataPlaneTest { String oversizedReadme = "x".repeat(1_000_001); String envelope = "{\"name\":\"my-pkg\",\"versions\":{\"1.0.0\":{\"name\":\"my-pkg\",\"version\":\"1.0.0\"," + "\"readme\":\"" + oversizedReadme + "\",\"dist\":{\"tarball\":\"http://ignored/my-pkg-1.0.0.tgz\"," - + "\"integrity\":\"" + integrity(content) + "\"}}},\"_attachments\":{\"my-pkg-1.0.0.tgz\":{" + + "\"integrity\":\"" + npmIntegrity(content) + "\"}}},\"_attachments\":{\"my-pkg-1.0.0.tgz\":{" + "\"content_type\":\"application/octet-stream\",\"data\":\"" + Base64.getEncoder().encodeToString(content) + "\",\"length\":" + content.length + "}}}"; @@ -682,7 +683,7 @@ class CodeArtifactNpmDataPlaneTest { String oversizedReadme = "x".repeat(1_000_001); String envelope = "{\"name\":\"my-pkg\",\"versions\":{\"1.0.0\":{\"name\":\"my-pkg\",\"version\":\"1.0.0\"," + "\"readme\":\"" + oversizedReadme + "\",\"dist\":{\"tarball\":\"http://ignored/my-pkg-1.0.0.tgz\"," - + "\"integrity\":\"" + integrity(content) + "\"}}},\"_attachments\":{\"my-pkg-1.0.0.tgz\":{" + + "\"integrity\":\"" + npmIntegrity(content) + "\"}}},\"_attachments\":{\"my-pkg-1.0.0.tgz\":{" + "\"content_type\":\"application/octet-stream\",\"data\":\"" + Base64.getEncoder().encodeToString(content) + "\",\"length\":" + content.length + "}}}"; @@ -728,7 +729,7 @@ class CodeArtifactNpmDataPlaneTest { startDataPlane(service, verdaccioClient); HttpResponse response = put("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/my-pkg", "good-token", - publishEnvelope("other-pkg", "1.0.0", "other-pkg-1.0.0.tgz", content)); + npmPublishEnvelope("other-pkg", "1.0.0", "other-pkg-1.0.0.tgz", content)); assertEquals(201, response.statusCode()); assertEquals(true, upstreamReceivedPut.get()); @@ -767,7 +768,7 @@ class CodeArtifactNpmDataPlaneTest { startDataPlane(service, verdaccioClient); HttpResponse response = put("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/@myscope%2Fmy-pkg", - "good-token", publishEnvelope("@myscope/my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", content)); + "good-token", npmPublishEnvelope("@myscope/my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", content)); assertEquals(200, response.statusCode()); assertEquals(false, upstreamReceivedPut.get()); @@ -805,7 +806,7 @@ class CodeArtifactNpmDataPlaneTest { startDataPlane(service, verdaccioClient); HttpResponse response = put("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/my-pkg", "good-token", - publishEnvelope("my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", content)); + npmPublishEnvelope("my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", content)); assertEquals(409, response.statusCode()); assertEquals(true, upstreamReceivedPut.get()); @@ -834,7 +835,8 @@ class CodeArtifactNpmDataPlaneTest { @Test void aPublishWhoseSidecarLookupFailsFallsThroughToTheBackend() throws Exception { assertForwardedWhenNotConfirmable( - publishEnvelope("my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", "tarball bytes".getBytes(StandardCharsets.UTF_8)), + npmPublishEnvelope("my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", + "tarball bytes".getBytes(StandardCharsets.UTF_8)), verdaccioClient -> when(verdaccioClient.fetchPackageDocument(anyString(), isNull(), eq("my-pkg"))) .thenThrow(new IllegalStateException("sidecar unreachable"))); } @@ -878,7 +880,7 @@ class CodeArtifactNpmDataPlaneTest { when(verdaccioClient.fetchPackageDocument(anyString(), eq(namespace), eq(name))) .thenReturn(Optional.of(storedDocument(name, version, filename, storedContent, storedTags))); when(verdaccioClient.storedTarballIntegrity(anyString(), eq(namespace), eq(name), eq(filename))) - .thenReturn(Optional.of(integrity(storedContent))); + .thenReturn(Optional.of(npmIntegrity(storedContent))); } /** @@ -912,7 +914,7 @@ class CodeArtifactNpmDataPlaneTest { startDataPlane(service, verdaccioClient); HttpResponse response = put("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/big-pkg", "good-token", - publishEnvelope("big-pkg", "1.0.0", "big-pkg-1.0.0.tgz", content)); + npmPublishEnvelope("big-pkg", "1.0.0", "big-pkg-1.0.0.tgz", content)); assertEquals(200, response.statusCode()); assertEquals(false, upstreamReceivedPut.get()); @@ -926,18 +928,10 @@ class CodeArtifactNpmDataPlaneTest { storedTags.forEach(tags::put); ObjectNode dist = document.putObject("versions").putObject(version).putObject("dist"); dist.put("tarball", "http://ignored/" + filename); - dist.put("integrity", integrity(content)); + dist.put("integrity", npmIntegrity(content)); return document; } - private static String integrity(byte[] content) { - try { - return "sha512-" + Base64.getEncoder().encodeToString(MessageDigest.getInstance("SHA-512").digest(content)); - } catch (NoSuchAlgorithmException e) { - throw new IllegalStateException(e); - } - } - private HttpResponse putUnchecked(String path, String body) { try { return put(path, "good-token", body); @@ -955,7 +949,7 @@ class CodeArtifactNpmDataPlaneTest { @Test void aPublishArrivingWhilePreflightsAreFullWaitsForAPermitInsteadOfBypassingTheCheck() throws Exception { byte[] content = "tarball bytes".getBytes(StandardCharsets.UTF_8); - String envelope = publishEnvelope("my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", content); + String envelope = npmPublishEnvelope("my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", content); AtomicReference upstreamReceivedPut = new AtomicReference<>(false); upstream = vertx.createHttpServer() .requestHandler(request -> { @@ -979,7 +973,7 @@ class CodeArtifactNpmDataPlaneTest { CountDownLatch release = new CountDownLatch(1); ObjectNode document = storedDocument("my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", content, Map.of()); when(verdaccioClient.storedTarballIntegrity(anyString(), isNull(), eq("my-pkg"), eq("my-pkg-1.0.0.tgz"))) - .thenReturn(Optional.of(integrity(content))); + .thenReturn(Optional.of(npmIntegrity(content))); when(verdaccioClient.fetchPackageDocument(anyString(), isNull(), eq("my-pkg"))).thenAnswer(invocation -> { entered.countDown(); release.await(5, TimeUnit.SECONDS); @@ -1015,7 +1009,7 @@ class CodeArtifactNpmDataPlaneTest { @Test void aQueuedPublishReleasesItsUseWhenTheClientDisconnects() throws Exception { byte[] content = "tarball bytes".getBytes(StandardCharsets.UTF_8); - String envelope = publishEnvelope("my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", content); + String envelope = npmPublishEnvelope("my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", content); CodeArtifactService service = mock(CodeArtifactService.class); when(service.resolveAuthorizationToken("good-token", DOMAIN)) .thenReturn(Optional.of(new AuthorizationTokenScope("000000000000", "us-east-1"))); @@ -1033,7 +1027,7 @@ class CodeArtifactNpmDataPlaneTest { return Optional.of(document); }); when(verdaccioClient.storedTarballIntegrity(anyString(), isNull(), eq("my-pkg"), - eq("my-pkg-1.0.0.tgz"))).thenReturn(Optional.of(integrity(content))); + eq("my-pkg-1.0.0.tgz"))).thenReturn(Optional.of(npmIntegrity(content))); startDataPlane(service, verdaccioClient); String path = "/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/my-pkg"; @@ -1066,21 +1060,6 @@ class CodeArtifactNpmDataPlaneTest { } } - private static String publishEnvelope(String name, String version, String filename, byte[] content) { - return publishEnvelopeWithDistTags(name, version, filename, content, null); - } - - private static String publishEnvelopeWithDistTags(String name, String version, String filename, byte[] content, - String distTagsJson) { - String base64Content = Base64.getEncoder().encodeToString(content); - String distTags = distTagsJson == null ? "" : ",\"dist-tags\":" + distTagsJson; - return "{\"name\":\"" + name + "\",\"versions\":{\"" + version + "\":{\"name\":\"" + name - + "\",\"version\":\"" + version + "\",\"dist\":{\"tarball\":\"http://ignored/" + filename - + "\",\"integrity\":\"" + integrity(content) + "\"}}}" + distTags + ",\"_attachments\":{\"" + filename - + "\":{\"content_type\":\"application/octet-stream\"," - + "\"data\":\"" + base64Content + "\",\"length\":" + content.length + "}}}"; - } - /** * Republishes byte-identical content whose envelope sends {@code distTagsJson}, against a * package whose tags Verdaccio already has as {@code storedTags}. The backend always answers 409 @@ -1111,7 +1090,7 @@ class CodeArtifactNpmDataPlaneTest { startDataPlane(service, verdaccioClient); return put("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/my-pkg", "good-token", - publishEnvelopeWithDistTags("my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", content, distTagsJson)); + npmPublishEnvelope("my-pkg", "1.0.0", "my-pkg-1.0.0.tgz", content, distTagsJson)); } @Test 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 198ef4af5e..8363733217 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 @@ -30,6 +30,8 @@ import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; +import static io.github.hectorvent.floci.testing.CodeArtifactRequests.npmPublishEnvelope; +import static io.github.hectorvent.floci.testing.CodeArtifactRequests.putPackageOriginConfiguration; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.containsString; @@ -887,18 +889,20 @@ class CodeArtifactNpmDockerIntegrationTest { void aLiteralSlashScopedPublishIsCountedAndRespectsAPublishBlock() { String scope = "flociscope"; String packageName = "literal-slash-pkg"; - String literalPath = "/codeartifact/npm/" + DOMAIN + "/" + REPO + "/@" + scope + "/" + packageName; + String npmName = "@" + scope + "/" + packageName; + String literalPath = "/codeartifact/npm/" + DOMAIN + "/" + REPO + "/" + npmName; byte[] firstTarball = "literal slash throwaway tarball 1".getBytes(StandardCharsets.UTF_8); long assetSizeBytesBefore = describeDomainAssetSizeBytes(); given().urlEncodingEnabled(false).header("Authorization", "Bearer " + bearerToken) - .contentType("application/json").body(scopedEnvelope(scope, packageName, "1.0.0", firstTarball)) + .contentType("application/json") + .body(npmPublishEnvelope(npmName, "1.0.0", packageName + "-1.0.0.tgz", firstTarball)) .put(literalPath) .then().statusCode(anyOf201Or200()); assertEquals(assetSizeBytesBefore + firstTarball.length, describeDomainAssetSizeBytes()); - setNpmOriginRestrictions(scope, packageName, "BLOCK"); - String secondVersion = scopedEnvelope(scope, packageName, "2.0.0", + putPackageOriginConfiguration(AUTH, DOMAIN, REPO, "npm", scope, packageName, "BLOCK", "BLOCK"); + String secondVersion = npmPublishEnvelope(npmName, "2.0.0", packageName + "-2.0.0.tgz", "literal slash throwaway tarball 2".getBytes(StandardCharsets.UTF_8)); given().urlEncodingEnabled(false).header("Authorization", "Bearer " + bearerToken) .contentType("application/json").body(secondVersion) @@ -911,32 +915,13 @@ class CodeArtifactNpmDockerIntegrationTest { .body("versions", hasKey("1.0.0")) .body("versions", not(hasKey("2.0.0"))); - setNpmOriginRestrictions(scope, packageName, "ALLOW"); + putPackageOriginConfiguration(AUTH, DOMAIN, REPO, "npm", scope, packageName, "ALLOW", "BLOCK"); given().urlEncodingEnabled(false).header("Authorization", "Bearer " + bearerToken) .contentType("application/json").body(secondVersion) .put(literalPath) .then().statusCode(anyOf201Or200()); } - private static String scopedEnvelope(String scope, String packageName, String version, byte[] tarball) { - String npmName = "@" + scope + "/" + packageName; - String assetName = packageName + "-" + version + ".tgz"; - return "{\"name\":\"" + npmName + "\",\"versions\":{\"" + version + "\":{\"name\":\"" + npmName - + "\",\"version\":\"" + version + "\",\"dist\":{\"tarball\":\"http://ignored/" + assetName + "\"}}}," - + "\"_attachments\":{\"" + assetName + "\":{\"content_type\":\"application/octet-stream\"," - + "\"data\":\"" + Base64.getEncoder().encodeToString(tarball) + "\",\"length\":" + tarball.length - + "}}}"; - } - - private static void setNpmOriginRestrictions(String scope, String packageName, String publish) { - given().contentType("application/json").header("Authorization", AUTH) - .body("{\"restrictions\":{\"publish\":\"" + publish + "\",\"upstream\":\"BLOCK\"}}") - .post("/v1/package?domain=" + DOMAIN + "&repository=" + REPO + "&format=npm&namespace=" + scope - + "&package=" + packageName) - .then().statusCode(200) - .body("originConfiguration.restrictions.publish", equalTo(publish)); - } - /** * 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/CodeArtifactNpmExternalConnectionDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNpmExternalConnectionDockerIntegrationTest.java index 4d96f3776a..db4fe53ad5 100644 --- a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNpmExternalConnectionDockerIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNpmExternalConnectionDockerIntegrationTest.java @@ -12,6 +12,7 @@ import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; +import static io.github.hectorvent.floci.testing.CodeArtifactRequests.putPackageOriginConfiguration; import static io.restassured.RestAssured.given; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; @@ -139,11 +140,7 @@ class CodeArtifactNpmExternalConnectionDockerIntegrationTest { @Test @Order(4) void upstreamBlockPreventsBothTheMergeAndTheRetention() { - given().contentType("application/json").header("Authorization", AUTH) - .body("{\"restrictions\":{\"publish\":\"ALLOW\",\"upstream\":\"BLOCK\"}}") - .post("/v1/package?domain=" + DOMAIN + "&repository=" + BLOCKED_REPO + "&format=npm" - + "&package=" + PACKAGE_NAME) - .then().statusCode(200); + putPackageOriginConfiguration(AUTH, DOMAIN, BLOCKED_REPO, "npm", null, PACKAGE_NAME, "ALLOW", "BLOCK"); given().header("Authorization", "Bearer " + bearerToken) .get("/codeartifact/npm/" + DOMAIN + "/" + BLOCKED_REPO + "/" + PACKAGE_NAME) 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 3f9d608e24..220ff051d7 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 @@ -21,6 +21,7 @@ import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; +import static io.github.hectorvent.floci.testing.CodeArtifactRequests.putPackageOriginConfiguration; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -579,7 +580,7 @@ class CodeArtifactNuGetDockerIntegrationTest { .put("/codeartifact/nuget/" + DOMAIN + "/" + REPO + "/api/v2/package") .then().statusCode(201); - setOriginRestrictions(packageId, "BLOCK"); + putPackageOriginConfiguration(AUTH, DOMAIN, REPO, "nuget", null, packageId, "BLOCK", "BLOCK"); given().header("Authorization", "Bearer " + bearerToken) .contentType("multipart/form-data; boundary=boundary123") @@ -599,7 +600,7 @@ class CodeArtifactNuGetDockerIntegrationTest { .then().statusCode(200) .body("versions", equalTo(List.of(VERSION))); - setOriginRestrictions(packageId, "ALLOW"); + putPackageOriginConfiguration(AUTH, DOMAIN, REPO, "nuget", null, packageId, "ALLOW", "BLOCK"); given().header("Authorization", "Bearer " + bearerToken) .contentType("multipart/form-data; boundary=boundary123") @@ -608,14 +609,6 @@ class CodeArtifactNuGetDockerIntegrationTest { .then().statusCode(201); } - private static void setOriginRestrictions(String packageId, String publish) { - given().contentType("application/json").header("Authorization", AUTH) - .body("{\"restrictions\":{\"publish\":\"" + publish + "\",\"upstream\":\"BLOCK\"}}") - .post("/v1/package?domain=" + DOMAIN + "&repository=" + REPO + "&format=nuget&package=" + packageId) - .then().statusCode(200) - .body("originConfiguration.restrictions.publish", equalTo(publish)); - } - 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/CodeArtifactNuGetExternalConnectionDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNuGetExternalConnectionDockerIntegrationTest.java index 94c02eda5c..b8579fe43f 100644 --- a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNuGetExternalConnectionDockerIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNuGetExternalConnectionDockerIntegrationTest.java @@ -10,6 +10,7 @@ import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; +import static io.github.hectorvent.floci.testing.CodeArtifactRequests.putPackageOriginConfiguration; import static io.restassured.RestAssured.given; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; @@ -137,11 +138,7 @@ class CodeArtifactNuGetExternalConnectionDockerIntegrationTest { @Test @Order(3) void upstreamBlockPreventsRetentionEvenThoughTheRealPackageExists() { - given().contentType("application/json").header("Authorization", AUTH) - .body("{\"restrictions\":{\"publish\":\"ALLOW\",\"upstream\":\"BLOCK\"}}") - .post("/v1/package?domain=" + DOMAIN + "&repository=" + BLOCKED_REPO + "&format=nuget" - + "&package=" + PACKAGE_ID) - .then().statusCode(200); + putPackageOriginConfiguration(AUTH, DOMAIN, BLOCKED_REPO, "nuget", null, PACKAGE_ID, "ALLOW", "BLOCK"); given().header("Authorization", "Bearer " + bearerToken) .get("/codeartifact/nuget/" + DOMAIN + "/" + BLOCKED_REPO + "/v3/package/" + LOWER_ID + "/" + VERSION diff --git a/src/test/java/io/github/hectorvent/floci/testing/CodeArtifactRequests.java b/src/test/java/io/github/hectorvent/floci/testing/CodeArtifactRequests.java new file mode 100644 index 0000000000..148443eb39 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/testing/CodeArtifactRequests.java @@ -0,0 +1,60 @@ +package io.github.hectorvent.floci.testing; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Base64; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; + +/** Request bodies and management calls the CodeArtifact tests share across formats. */ +public final class CodeArtifactRequests { + + private CodeArtifactRequests() {} + + /** + * An npm publish envelope for one version: its {@code dist} block, carrying the tarball's real + * sha512 {@code integrity}, and the tarball itself as a base64 {@code _attachments} entry named + * {@code filename}. {@code name} is the full npm name, {@code @scope/name} for a scoped package. + */ + public static String npmPublishEnvelope(String name, String version, String filename, byte[] content) { + return npmPublishEnvelope(name, version, filename, content, null); + } + + /** As above, with {@code distTagsJson} sent as the envelope's {@code dist-tags}; {@code null} leaves it out. */ + public static String npmPublishEnvelope(String name, String version, String filename, byte[] content, + String distTagsJson) { + String distTags = distTagsJson == null ? "" : ",\"dist-tags\":" + distTagsJson; + return "{\"name\":\"" + name + "\",\"versions\":{\"" + version + "\":{\"name\":\"" + name + + "\",\"version\":\"" + version + "\",\"dist\":{\"tarball\":\"http://ignored/" + filename + + "\",\"integrity\":\"" + npmIntegrity(content) + "\"}}}" + distTags + ",\"_attachments\":{\"" + + filename + "\":{\"content_type\":\"application/octet-stream\"," + + "\"data\":\"" + Base64.getEncoder().encodeToString(content) + "\",\"length\":" + content.length + + "}}}"; + } + + /** The {@code sha512-} integrity string npm records for {@code content}. */ + public static String npmIntegrity(byte[] content) { + try { + return "sha512-" + Base64.getEncoder().encodeToString(MessageDigest.getInstance("SHA-512").digest(content)); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException(e); + } + } + + /** + * PutPackageOriginConfiguration for one package, asserting both restrictions came back as sent. + * {@code namespace} is {@code null} for a format or package without one. + */ + public static void putPackageOriginConfiguration(String authorization, String domain, String repository, + String format, String namespace, String packageName, String publish, String upstream) { + String namespaceParam = namespace == null ? "" : "&namespace=" + namespace; + given().contentType("application/json").header("Authorization", authorization) + .body("{\"restrictions\":{\"publish\":\"" + publish + "\",\"upstream\":\"" + upstream + "\"}}") + .post("/v1/package?domain=" + domain + "&repository=" + repository + "&format=" + format + + namespaceParam + "&package=" + packageName) + .then().statusCode(200) + .body("originConfiguration.restrictions.publish", equalTo(publish)) + .body("originConfiguration.restrictions.upstream", equalTo(upstream)); + } +}