feat(codeartifact): support publishing and reading generic-format package versions (#3839)

Adds PublishPackageVersion, DescribePackageVersion, and GetPackageVersionAsset
for CodeArtifact's generic package format, matching AWS's own wire restriction
that PublishPackageVersion accepts only the generic format. Includes real
asset hashing (MD5, SHA-1, SHA-256, SHA-512), SHA-256 integrity verification
against the caller's x-amz-content-sha256 header, correct Unfinished/Published
status transitions, and the documented AWS quotas for this action (350 assets
per version, 5 GB max asset size), all returning ServiceQuotaExceededException
when exceeded.

Asset bytes are kept out of the JSON/WAL-backed package-version store
(@JsonIgnore on PackageAsset.content) and instead go through a dedicated
asset store, a disk file in persistent/hybrid/wal mode or a side map in
memory mode, the same split S3Object/S3Service already uses for object
bytes. Without this, a publish rewrote every asset of every package version
on disk each time, growing quadratically under this action's own
350-asset/5GB quotas. The asset name is also validated against the wire
model's AssetName pattern (no control characters), not just its length,
since it is echoed back verbatim in the X-AssetName response header on
GetPackageVersionAsset.

Verified against the real botocore service-2.json wire model and the AWS
CodeArtifact User Guide (including its PDF for the quotas table, since the
HTML quotas page is JS-rendered). Live-verified with the real AWS CLI and
the AWS SDK for Java v2 against a running Floci instance, including an exact
byte-for-byte asset round-trip and the real ConflictException when
republishing an already-Published version.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-18 23:49:21 -05:00
1 parent e190f7d340
commit bbaf3568c5
10 files changed
+1158 -19

No files matched your search

@@ -0,0 +1,130 @@
package io.github.hectorvent.floci.services.codeartifact;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.PersistentStorage;
import io.github.hectorvent.floci.services.codeartifact.CodeArtifactService.PackageVersionAssetResult;
import io.github.hectorvent.floci.services.codeartifact.CodeArtifactService.PublishPackageVersionResult;
import io.github.hectorvent.floci.services.codeartifact.model.CodeArtifactDomain;
import io.github.hectorvent.floci.services.codeartifact.model.CodeArtifactPackageVersion;
import io.github.hectorvent.floci.services.codeartifact.model.CodeArtifactRepository;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Asset bytes must not ride along in the JSON-backed {@code codeartifact-package-versions.json}
* store: that file is rewritten whole on every publish, so embedding a growing set of assets
* there would make every later publish pay to re-serialize every earlier one. This exercises
* {@link CodeArtifactService} against a real {@link PersistentStorage}, the same backend
* {@code persistent} storage mode uses in production.
*/
class CodeArtifactAssetPersistenceTest {
private static final String REGION = "us-east-1";
private static final String ACCOUNT_ID = "123456789012";
@Test
void assetBytesAreNotEmbeddedInThePersistedPackageVersionJson(@TempDir Path dir) throws IOException {
CodeArtifactService service = newService(dir);
service.createDomain(REGION, "dom", null, Map.of());
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
byte[] content = "hello world, this is the asset payload".getBytes(StandardCharsets.UTF_8);
service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "a.txt",
sha256Hex(content), "false", content);
String json = Files.readString(dir.resolve("codeartifact-package-versions.json"));
String base64Content = Base64.getEncoder().encodeToString(content);
assertFalse(json.contains(base64Content),
"persisted package-version JSON must not embed asset bytes: " + json);
}
@Test
void assetContentSurvivesRestart(@TempDir Path dir) {
CodeArtifactService first = newService(dir);
first.createDomain(REGION, "dom", null, Map.of());
first.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
byte[] content = "hello world".getBytes(StandardCharsets.UTF_8);
PublishPackageVersionResult published = first.publishPackageVersion(REGION, "dom", null, "repo", "generic",
null, "my-pkg", "1.0.0", "a.txt", sha256Hex(content), "false", content);
CodeArtifactService restarted = newService(dir);
PackageVersionAssetResult result = restarted.getPackageVersionAsset(REGION, "dom", null, "repo", "generic",
null, "my-pkg", "1.0.0", "a.txt", null);
assertEquals("hello world", new String(result.asset().getContent(), StandardCharsets.UTF_8));
assertEquals(published.packageVersion().getRevision(), result.packageVersionRevision());
}
@Test
void distinctAssetNamesNeverCollideOnDisk(@TempDir Path dir) {
CodeArtifactService service = newService(dir);
service.createDomain(REGION, "dom", null, Map.of());
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
String longToken = "p".repeat(255);
List<String> names = List.of("b", "x/../b", ".", "..", "b/c", "/b", "b/");
for (String name : names) {
byte[] content = ("content of " + name).getBytes(StandardCharsets.UTF_8);
service.publishPackageVersion(REGION, "dom", null, "repo", "generic", longToken, longToken, longToken,
name, sha256Hex(content), "true", content);
}
CodeArtifactService restarted = newService(dir);
for (String name : names) {
PackageVersionAssetResult result = restarted.getPackageVersionAsset(REGION, "dom", null, "repo",
"generic", longToken, longToken, longToken, name, null);
assertEquals("content of " + name, new String(result.asset().getContent(), StandardCharsets.UTF_8));
}
}
private CodeArtifactService newService(Path dir) {
AccountAwareStorageBackend<CodeArtifactDomain> domainStore = accountAware(dir, "codeartifact-domains.json",
new TypeReference<Map<String, CodeArtifactDomain>>() {});
AccountAwareStorageBackend<CodeArtifactRepository> repoStore = accountAware(dir,
"codeartifact-repositories.json", new TypeReference<Map<String, CodeArtifactRepository>>() {});
AccountAwareStorageBackend<CodeArtifactPackageVersion> packageVersionStore = accountAware(dir,
"codeartifact-package-versions.json", new TypeReference<Map<String, CodeArtifactPackageVersion>>() {});
RegionResolver regionResolver = mock(RegionResolver.class);
when(regionResolver.getAccountId()).thenReturn(ACCOUNT_ID);
when(regionResolver.buildArn(anyString(), anyString(), anyString())).thenAnswer(invocation ->
"arn:aws:" + invocation.getArgument(0, String.class) + ":" + invocation.getArgument(1, String.class)
+ ":" + ACCOUNT_ID + ":" + invocation.getArgument(2, String.class));
EmulatorConfig config = mock(EmulatorConfig.class);
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
return new CodeArtifactService(domainStore, repoStore, packageVersionStore, regionResolver, config,
false, dir.resolve("codeartifact-assets"));
}
private <V> AccountAwareStorageBackend<V> accountAware(Path dir, String fileName, TypeReference<Map<String, V>> type) {
PersistentStorage<String, V> backend = new PersistentStorage<>(dir.resolve(fileName), type);
backend.load();
return new AccountAwareStorageBackend<>(backend, null, ACCOUNT_ID);
}
private static String sha256Hex(byte[] content) {
try {
return io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator.sha256Hex(content);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
@@ -1,15 +1,19 @@
package io.github.hectorvent.floci.services.codeartifact;
import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.emptyIterable;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
@QuarkusTest
class CodeArtifactIntegrationTest {
@@ -129,4 +133,77 @@ class CodeArtifactIntegrationTest {
.body("{}").post("/v1/domain?domain=NOT-VALID")
.then().statusCode(400).body("__type", equalTo("ValidationException"));
}
@Test
void publishDescribeAndGetPackageVersionAssetRoundTripExactBytes() {
given().contentType("application/json").header("Authorization", AUTH)
.body("{}").post("/v1/domain?domain=pkg-domain").then().statusCode(200);
given().contentType("application/json").header("Authorization", AUTH)
.body("{}").post("/v1/repository?domain=pkg-domain&repository=pkg-repo").then().statusCode(200);
byte[] content = "hello codeartifact".getBytes(StandardCharsets.UTF_8);
String sha256 = sha256Hex(content);
given().header("Authorization", AUTH).header("x-amz-content-sha256", sha256)
.contentType("application/octet-stream").body(content)
.post("/v1/package/version/publish?domain=pkg-domain&repository=pkg-repo&format=generic"
+ "&package=my-pkg&version=1.0.0&asset=asset.txt")
.then().statusCode(200)
.body("status", equalTo("Published"))
.body("versionRevision", notNullValue())
.body("asset.name", equalTo("asset.txt"))
.body("asset.size", equalTo(content.length))
.body("asset.hashes.'SHA-256'", equalTo(sha256));
given().header("Authorization", AUTH)
.get("/v1/package/version?domain=pkg-domain&repository=pkg-repo&format=generic"
+ "&package=my-pkg&version=1.0.0")
.then().statusCode(200)
.body("packageVersion.status", equalTo("Published"))
.body("packageVersion.origin.originType", equalTo("INTERNAL"));
byte[] downloaded = given().header("Authorization", AUTH)
.get("/v1/package/version/asset?domain=pkg-domain&repository=pkg-repo&format=generic"
+ "&package=my-pkg&version=1.0.0&asset=asset.txt")
.then().statusCode(200)
.header("X-AssetName", equalTo("asset.txt"))
.extract().asByteArray();
assertArrayEquals(content, downloaded);
given().header("Authorization", AUTH).header("x-amz-content-sha256", sha256)
.contentType("application/octet-stream").body(content)
.post("/v1/package/version/publish?domain=pkg-domain&repository=pkg-repo&format=generic"
+ "&package=my-pkg&version=1.0.0&asset=another.txt")
.then().statusCode(409).body("__type", equalTo("ConflictException"));
}
@Test
void unfinishedPublishKeepsVersionOpenForMoreAssets() {
given().contentType("application/json").header("Authorization", AUTH)
.body("{}").post("/v1/domain?domain=unfinished-domain").then().statusCode(200);
given().contentType("application/json").header("Authorization", AUTH)
.body("{}").post("/v1/repository?domain=unfinished-domain&repository=repo").then().statusCode(200);
byte[] content = "partial".getBytes(StandardCharsets.UTF_8);
String sha256 = sha256Hex(content);
given().header("Authorization", AUTH).header("x-amz-content-sha256", sha256)
.contentType("application/octet-stream").body(content)
.post("/v1/package/version/publish?domain=unfinished-domain&repository=repo&format=generic"
+ "&package=my-pkg&version=1.0.0&asset=a.txt&unfinished=true")
.then().statusCode(200).body("status", equalTo("Unfinished"));
given().header("Authorization", AUTH)
.get("/v1/package/version?domain=unfinished-domain&repository=repo&format=generic"
+ "&package=my-pkg&version=1.0.0")
.then().statusCode(200).body("packageVersion.status", equalTo("Unfinished"));
}
private static String sha256Hex(byte[] content) {
try {
return SigV4RequestValidator.sha256Hex(content);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
@@ -1,19 +1,22 @@
package io.github.hectorvent.floci.services.codeartifact;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.codeartifact.CodeArtifactService.DomainView;
import io.github.hectorvent.floci.services.codeartifact.CodeArtifactService.ResourcePolicy;
import io.github.hectorvent.floci.services.codeartifact.CodeArtifactService.PackageVersionAssetResult;
import io.github.hectorvent.floci.services.codeartifact.CodeArtifactService.PublishPackageVersionResult;
import io.github.hectorvent.floci.services.codeartifact.model.CodeArtifactDomain;
import io.github.hectorvent.floci.services.codeartifact.model.CodeArtifactPackageVersion;
import io.github.hectorvent.floci.services.codeartifact.model.CodeArtifactRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -26,9 +29,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -40,15 +41,11 @@ class CodeArtifactServiceTest {
private CodeArtifactService service;
@BeforeEach
@SuppressWarnings({"unchecked", "rawtypes"})
void setUp() {
StorageFactory storageFactory = mock(StorageFactory.class);
AccountAwareStorageBackend<CodeArtifactDomain> domainStore = AccountAwareStorageBackend.inMemory(ACCOUNT_ID);
AccountAwareStorageBackend<CodeArtifactRepository> repoStore = AccountAwareStorageBackend.inMemory(ACCOUNT_ID);
when(storageFactory.create(eq("codeartifact"), eq("codeartifact-domains.json"), any(TypeReference.class)))
.thenReturn((AccountAwareStorageBackend) domainStore);
when(storageFactory.create(eq("codeartifact"), eq("codeartifact-repositories.json"), any(TypeReference.class)))
.thenReturn((AccountAwareStorageBackend) repoStore);
AccountAwareStorageBackend<CodeArtifactPackageVersion> packageVersionStore =
AccountAwareStorageBackend.inMemory(ACCOUNT_ID);
RegionResolver regionResolver = mock(RegionResolver.class);
when(regionResolver.getAccountId()).thenReturn(ACCOUNT_ID);
@@ -59,7 +56,8 @@ class CodeArtifactServiceTest {
EmulatorConfig config = mock(EmulatorConfig.class);
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
service = new CodeArtifactService(storageFactory, regionResolver, config);
service = new CodeArtifactService(domainStore, repoStore, packageVersionStore, regionResolver, config,
true, null);
}
// -------------------------------------------------------------- domains
@@ -352,4 +350,236 @@ class CodeArtifactServiceTest {
() -> service.describeRepository(REGION, "dom", null, "repo"));
assertEquals("ResourceNotFoundException", e.getErrorCode());
}
// -------------------------------------------------------- package versions
@Test
void publishPackageVersionComputesRealHashesAndPublishesByDefault() {
service.createDomain(REGION, "dom", null, Map.of());
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
byte[] content = "hello world".getBytes(StandardCharsets.UTF_8);
String sha256 = sha256Hex(content);
PublishPackageVersionResult result = service.publishPackageVersion(REGION, "dom", null, "repo", "generic",
"my-ns", "my-pkg", "1.0.0", "asset.txt", sha256, "false", content);
assertEquals("Published", result.packageVersion().getStatus());
assertEquals("asset.txt", result.asset().getName());
assertEquals(content.length, result.asset().getSize());
assertEquals(sha256, result.asset().getHashes().get("SHA-256"));
assertTrue(result.asset().getHashes().containsKey("MD5"));
assertTrue(result.asset().getHashes().containsKey("SHA-1"));
assertTrue(result.asset().getHashes().containsKey("SHA-512"));
}
@Test
void publishPackageVersionRejectsMismatchedSha256() {
service.createDomain(REGION, "dom", null, Map.of());
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
byte[] content = "hello world".getBytes(StandardCharsets.UTF_8);
AwsException e = assertThrows(AwsException.class, () -> service.publishPackageVersion(REGION, "dom", null,
"repo", "generic", null, "my-pkg", "1.0.0", "asset.txt", "0".repeat(64), "false", content));
assertEquals("ValidationException", e.getErrorCode());
}
@Test
void publishPackageVersionRequiresAssetSha256() {
service.createDomain(REGION, "dom", null, Map.of());
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
byte[] content = "hello world".getBytes(StandardCharsets.UTF_8);
AwsException missing = assertThrows(AwsException.class, () -> service.publishPackageVersion(REGION, "dom",
null, "repo", "generic", null, "my-pkg", "1.0.0", "asset.txt", null, "false", content));
assertEquals("ValidationException", missing.getErrorCode());
AwsException malformed = assertThrows(AwsException.class, () -> service.publishPackageVersion(REGION, "dom",
null, "repo", "generic", null, "my-pkg", "1.0.0", "asset.txt", "not-hex", "false", content));
assertEquals("ValidationException", malformed.getErrorCode());
}
@Test
void publishPackageVersionRejectsAssetNameWithControlCharacters() {
service.createDomain(REGION, "dom", null, Map.of());
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
byte[] content = "hello world".getBytes(StandardCharsets.UTF_8);
String sha256 = sha256Hex(content);
// A newline here would land verbatim in the X-AssetName response header on GetPackageVersionAsset.
AwsException e = assertThrows(AwsException.class, () -> service.publishPackageVersion(REGION, "dom", null,
"repo", "generic", null, "my-pkg", "1.0.0", "asset.txt\r\nX-Injected: true", sha256, "false",
content));
assertEquals("ValidationException", e.getErrorCode());
}
@Test
void publishPackageVersionRejectsMalformedUnfinishedFlag() {
service.createDomain(REGION, "dom", null, Map.of());
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
byte[] content = "hello world".getBytes(StandardCharsets.UTF_8);
AwsException e = assertThrows(AwsException.class, () -> service.publishPackageVersion(REGION, "dom", null,
"repo", "generic", null, "my-pkg", "1.0.0", "asset.txt", sha256Hex(content), "yes", content));
assertEquals("ValidationException", e.getErrorCode());
PublishPackageVersionResult omitted = service.publishPackageVersion(REGION, "dom", null, "repo", "generic",
null, "my-pkg", "1.0.0", "asset.txt", sha256Hex(content), null, content);
assertEquals("Published", omitted.packageVersion().getStatus());
}
@Test
void publishPackageVersionRejectsNonGenericFormat() {
service.createDomain(REGION, "dom", null, Map.of());
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
byte[] content = "x".getBytes(StandardCharsets.UTF_8);
AwsException e = assertThrows(AwsException.class, () -> service.publishPackageVersion(REGION, "dom", null,
"repo", "npm", null, "my-pkg", "1.0.0", "asset.txt", sha256Hex(content), "false", content));
assertEquals("ValidationException", e.getErrorCode());
}
@Test
void publishPackageVersionRequiresExistingRepository() {
service.createDomain(REGION, "dom", null, Map.of());
byte[] content = "x".getBytes(StandardCharsets.UTF_8);
AwsException e = assertThrows(AwsException.class, () -> service.publishPackageVersion(REGION, "dom", null,
"no-such-repo", "generic", null, "my-pkg", "1.0.0", "asset.txt", sha256Hex(content), "false", content));
assertEquals("ResourceNotFoundException", e.getErrorCode());
}
@Test
void unfinishedPublishAllowsMultipleAssetsThenFinalizes() {
service.createDomain(REGION, "dom", null, Map.of());
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
byte[] first = "first".getBytes(StandardCharsets.UTF_8);
byte[] second = "second".getBytes(StandardCharsets.UTF_8);
PublishPackageVersionResult r1 = service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null,
"my-pkg", "1.0.0", "a.txt", sha256Hex(first), "true", first);
assertEquals("Unfinished", r1.packageVersion().getStatus());
PublishPackageVersionResult r2 = service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null,
"my-pkg", "1.0.0", "b.txt", sha256Hex(second), "false", second);
assertEquals("Published", r2.packageVersion().getStatus());
assertEquals(2, r2.packageVersion().getAssets().size());
assertTrue(r2.packageVersion().getAssets().containsKey("a.txt"));
assertTrue(r2.packageVersion().getAssets().containsKey("b.txt"));
}
@Test
void publishingToAnAlreadyPublishedVersionConflicts() {
service.createDomain(REGION, "dom", null, Map.of());
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
byte[] content = "x".getBytes(StandardCharsets.UTF_8);
service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "a.txt",
sha256Hex(content), "false", content);
AwsException e = assertThrows(AwsException.class, () -> service.publishPackageVersion(REGION, "dom", null,
"repo", "generic", null, "my-pkg", "1.0.0", "b.txt", sha256Hex(content), "false", content));
assertEquals("ConflictException", e.getErrorCode());
}
@Test
void publishPackageVersionCapsAssetsPerVersionAtThreeHundredFifty() {
service.createDomain(REGION, "dom", null, Map.of());
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
for (int i = 0; i < 350; i++) {
byte[] content = ("content-" + i).getBytes(StandardCharsets.UTF_8);
service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0",
"asset-" + i + ".txt", sha256Hex(content), "true", content);
}
byte[] oneMore = "one-more".getBytes(StandardCharsets.UTF_8);
AwsException e = assertThrows(AwsException.class, () -> service.publishPackageVersion(REGION, "dom", null,
"repo", "generic", null, "my-pkg", "1.0.0", "asset-350.txt", sha256Hex(oneMore), "true", oneMore));
assertEquals("ServiceQuotaExceededException", e.getErrorCode());
// Re-publishing an asset name already on the version must not itself trip the cap.
byte[] resend = "content-0".getBytes(StandardCharsets.UTF_8);
service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "asset-0.txt",
sha256Hex(resend), "true", resend);
}
@Test
void describePackageVersionRoundTripsAndReportsNotFound() {
service.createDomain(REGION, "dom", null, Map.of());
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
byte[] content = "x".getBytes(StandardCharsets.UTF_8);
service.publishPackageVersion(REGION, "dom", null, "repo", "generic", "ns", "my-pkg", "1.0.0", "a.txt",
sha256Hex(content), "false", content);
CodeArtifactPackageVersion pv = service.describePackageVersion(REGION, "dom", null, "repo", "generic", "ns",
"my-pkg", "1.0.0");
assertEquals("Published", pv.getStatus());
assertEquals("ns", pv.getNamespace());
AwsException e = assertThrows(AwsException.class, () -> service.describePackageVersion(REGION, "dom", null,
"repo", "generic", "ns", "my-pkg", "2.0.0"));
assertEquals("ResourceNotFoundException", e.getErrorCode());
}
@Test
void getPackageVersionAssetReturnsExactBytesAndValidatesRevision() {
service.createDomain(REGION, "dom", null, Map.of());
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
byte[] content = "hello world".getBytes(StandardCharsets.UTF_8);
PublishPackageVersionResult published = service.publishPackageVersion(REGION, "dom", null, "repo", "generic",
null, "my-pkg", "1.0.0", "a.txt", sha256Hex(content), "false", content);
PackageVersionAssetResult result = service.getPackageVersionAsset(REGION, "dom", null, "repo", "generic",
null, "my-pkg", "1.0.0", "a.txt", null);
assertEquals(content.length, result.asset().getContent().length);
assertEquals("hello world", new String(result.asset().getContent(), StandardCharsets.UTF_8));
assertEquals(published.packageVersion().getRevision(), result.packageVersionRevision());
AwsException wrongAsset = assertThrows(AwsException.class, () -> service.getPackageVersionAsset(REGION,
"dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "missing.txt", null));
assertEquals("ResourceNotFoundException", wrongAsset.getErrorCode());
AwsException wrongRevision = assertThrows(AwsException.class, () -> service.getPackageVersionAsset(REGION,
"dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "a.txt", "not-the-current-revision"));
assertEquals("ResourceNotFoundException", wrongRevision.getErrorCode());
}
@Test
void concurrentUnfinishedPublishesOfDifferentAssetsBothPersist() throws InterruptedException {
service.createDomain(REGION, "dom", null, Map.of());
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
int assetCount = 8;
ExecutorService pool = Executors.newFixedThreadPool(assetCount);
CountDownLatch ready = new CountDownLatch(assetCount);
CountDownLatch go = new CountDownLatch(1);
try {
for (int i = 0; i < assetCount; i++) {
int index = i;
pool.submit(() -> {
ready.countDown();
try {
go.await();
byte[] content = ("content-" + index).getBytes(StandardCharsets.UTF_8);
service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg",
"1.0.0", "asset-" + index + ".txt", sha256Hex(content), "true", content);
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
}
});
}
ready.await(5, TimeUnit.SECONDS);
go.countDown();
} finally {
pool.shutdown();
assertTrue(pool.awaitTermination(10, TimeUnit.SECONDS));
}
CodeArtifactPackageVersion pv = service.describePackageVersion(REGION, "dom", null, "repo", "generic", null,
"my-pkg", "1.0.0");
assertEquals(assetCount, pv.getAssets().size());
}
private static String sha256Hex(byte[] content) {
try {
return SigV4RequestValidator.sha256Hex(content);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}