diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CodeArtifactTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CodeArtifactTest.java index 4771494786..c1b607bfca 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CodeArtifactTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CodeArtifactTest.java @@ -2,11 +2,16 @@ package com.floci.test; import org.jboss.logging.Logger; import org.junit.jupiter.api.*; +import software.amazon.awssdk.core.ResponseBytes; +import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.services.codeartifact.CodeartifactClient; import software.amazon.awssdk.services.codeartifact.model.*; // Explicit import: this file's Tag usage is the CodeArtifact model type, not JUnit's @Tag. import software.amazon.awssdk.services.codeartifact.model.Tag; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; @@ -219,6 +224,76 @@ class CodeArtifactTest { assertThat(disassoc.repository().externalConnections()).isEmpty(); } + @Test + @Order(70) + @DisplayName("PublishPackageVersion - publishes a generic asset and verifies its hashes") + void publishPackageVersion() { + byte[] content = "hello from the compat suite".getBytes(StandardCharsets.UTF_8); + String sha256 = sha256Hex(content); + + PublishPackageVersionResponse resp = codeArtifact.publishPackageVersion(r -> r + .domain(DOMAIN) + .repository(REPO) + .format(PackageFormat.GENERIC) + .namespace("compat-ns") + .packageValue("compat-pkg") + .packageVersion("1.0.0") + .assetName("asset.txt") + .assetSHA256(sha256), + RequestBody.fromBytes(content)); + + assertThat(resp.status()).isEqualTo(PackageVersionStatus.PUBLISHED); + assertThat(resp.asset().name()).isEqualTo("asset.txt"); + assertThat(resp.asset().size()).isEqualTo(content.length); + assertThat(resp.asset().hashes().get(HashAlgorithm.SHA_256)).isEqualTo(sha256); + } + + // No SDK-level bad-assetSHA256 test: the Java SDK overwrites that header during SigV4 signing; see CodeArtifactServiceTest instead. + + @Test + @Order(72) + @DisplayName("DescribePackageVersion / GetPackageVersionAsset - round-trip exact bytes") + void describeAndDownloadPackageVersionAsset() { + DescribePackageVersionResponse described = codeArtifact.describePackageVersion(r -> r + .domain(DOMAIN) + .repository(REPO) + .format(PackageFormat.GENERIC) + .namespace("compat-ns") + .packageValue("compat-pkg") + .packageVersion("1.0.0")); + assertThat(described.packageVersion().status()).isEqualTo(PackageVersionStatus.PUBLISHED); + assertThat(described.packageVersion().origin().originType()).isEqualTo(PackageVersionOriginType.INTERNAL); + + ResponseBytes downloaded = codeArtifact.getPackageVersionAssetAsBytes(r -> r + .domain(DOMAIN) + .repository(REPO) + .format(PackageFormat.GENERIC) + .namespace("compat-ns") + .packageValue("compat-pkg") + .packageVersion("1.0.0") + .asset("asset.txt")); + assertThat(downloaded.response().assetName()).isEqualTo("asset.txt"); + assertThat(downloaded.asByteArray()).isEqualTo("hello from the compat suite".getBytes(StandardCharsets.UTF_8)); + } + + @Test + @Order(73) + @DisplayName("PublishPackageVersion - a second publish to a Published version conflicts") + void republishingAPublishedVersionConflicts() { + byte[] content = "won't land".getBytes(StandardCharsets.UTF_8); + assertThatThrownBy(() -> codeArtifact.publishPackageVersion(r -> r + .domain(DOMAIN) + .repository(REPO) + .format(PackageFormat.GENERIC) + .namespace("compat-ns") + .packageValue("compat-pkg") + .packageVersion("1.0.0") + .assetName("another.txt") + .assetSHA256(sha256Hex(content)), + RequestBody.fromBytes(content))) + .isInstanceOf(ConflictException.class); + } + @Test @Order(90) @DisplayName("DeleteDomain - fails while repositories still exist") @@ -226,4 +301,17 @@ class CodeArtifactTest { assertThatThrownBy(() -> codeArtifact.deleteDomain(r -> r.domain(DOMAIN))) .isInstanceOf(ConflictException.class); } + + private static String sha256Hex(byte[] content) { + try { + byte[] digest = MessageDigest.getInstance("SHA-256").digest(content); + StringBuilder sb = new StringBuilder(digest.length * 2); + for (byte b : digest) { + sb.append(String.format("%02x", b)); + } + return sb.toString(); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException(e); + } + } } diff --git a/docs/services/codeartifact.md b/docs/services/codeartifact.md index f786cd3895..4ce56551b6 100644 --- a/docs/services/codeartifact.md +++ b/docs/services/codeartifact.md @@ -5,8 +5,10 @@ **Endpoint:** `http://localhost:4566` Floci supports the CodeArtifact control plane: domains, repositories, resource policies, tags, -and public upstream (external) connections. Package publish/fetch endpoints for each format -(npm, PyPI, Maven, NuGet, generic, etc.) are not implemented yet. +and public upstream (external) connections. Package publish/fetch is implemented for the +`generic` format only, matching AWS's own restriction that `PublishPackageVersion` accepts only +`generic`; the other formats (npm, PyPI, Maven, NuGet, etc.) have no real package-manager-protocol +proxy behind them yet. ## Supported Actions @@ -32,6 +34,9 @@ and public upstream (external) connections. Package publish/fetch endpoints for | `DeleteRepositoryPermissionsPolicy` | Removes a repository's resource policy, optionally checked against `policyRevision`. | | `AssociateExternalConnection` | Attaches a fixed-catalog public upstream (e.g. `public:npmjs`) to a repository; mutually exclusive with repository upstreams. | | `DisassociateExternalConnection` | Removes a repository's external connection. | +| `PublishPackageVersion` | Uploads a generic-format asset, creating or extending a package version; requires `x-amz-content-sha256` and verifies it against the real hash of the bytes received. | +| `DescribePackageVersion` | Returns a package version's status, revision, and origin. | +| `GetPackageVersionAsset` | Downloads one asset from a package version by name, optionally pinned to a specific revision. | | `TagResource` | Adds or updates tags on a domain or repository ARN. | | `UntagResource` | Removes tags by key from a domain or repository ARN. | | `ListTagsForResource` | Lists the tags on a domain or repository ARN. | @@ -48,6 +53,15 @@ enforces the one-external-connection-per-repository limit AWS enforces. A reposi upstream repositories or an external connection, but not both, matching AWS; `CreateRepository` and `UpdateRepository` also cap direct upstreams at 10, AWS's own repository limit. +`PublishPackageVersion` creates a package version in the `Unfinished` state when the `unfinished` +flag is set, and `Published` otherwise; once `Published`, a repeat publish to the same +domain/repository/package/version fails with `ConflictException`, matching AWS's real rule that a +published version cannot accept additional assets. Every publish returns a fresh +`versionRevision`, and each asset's hashes (`MD5`, `SHA-1`, `SHA-256`, `SHA-512`) are computed from +the bytes Floci actually received, not echoed from the request. Floci enforces AWS's own published +quotas for this action: a 5 GB max asset file size and a 350-asset cap per package version, both +returning `ServiceQuotaExceededException`. + ## AWS-compatible failures Domain and repository names, tags, pagination, duplicate names, missing upstreams, policy-revision @@ -71,6 +85,16 @@ state. that is not the caller's own account looks up that account's domain/repository with no trust-policy or permissions-policy enforcement, consistent with Floci's IAM enforcement being opt-in elsewhere, but worth knowing if you rely on domain-sharing semantics. +- **Package management beyond publish/describe/get-asset isn't implemented.** `ListPackages`, + `ListPackageVersions`, `ListPackageVersionAssets`, `DeletePackageVersions`, + `DisposePackageVersions`, and `UpdatePackageVersionsStatus` don't exist yet; the only way to + move a version from `Unfinished` to `Published` today is a follow-up `PublishPackageVersion` + call that omits the `unfinished` flag. +- **The 5 GB asset size quota is nominal.** `PublishPackageVersion` receives the request body as + a single byte array before Floci ever checks its length, so a request already large enough to + exhaust available heap fails before the quota check runs. The `ServiceQuotaExceededException` + behavior is correct for anything that does fit in memory; it is not itself a streaming size + limit. See the [CodeArtifact API Reference](https://docs.aws.amazon.com/codeartifact/latest/APIReference/Welcome.html). diff --git a/docs/services/index.md b/docs/services/index.md index 95e2ed4a91..5be2607342 100644 --- a/docs/services/index.md +++ b/docs/services/index.md @@ -118,7 +118,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac | [AWS Backup](backup.md) | `/backup-vaults/*`, `/backup/plans/*`, `/backup-jobs/*`, `/supported-resource-types` | REST JSON | 20 | | [AWS FIS](fis.md) | `/experimentTemplates/*`, `/experiments/*`, `/actions/*`, `/targetResourceTypes/*`, `/safetyLevers/*`, `/tags/*` | REST JSON | 26 | | [CodeGuru Reviewer](codegurureviewer.md) | `/associations`, `/associations/{associationArn}`, `/tags/*` | REST JSON | 7 | -| [CodeArtifact](codeartifact.md) | `/v1/domain*`, `/v1/repository*`, `/v1/tag*` | REST JSON | 22 | +| [CodeArtifact](codeartifact.md) | `/v1/domain*`, `/v1/repository*`, `/v1/package/version*`, `/v1/tag*` | REST JSON | 25 | | [CloudFront](cloudfront.md) | `/2020-05-31/distribution/*`, `/2020-05-31/cache-policy/*`, `/2020-05-31/function/*` | REST XML | 50 | | [Route53](route53.md) | `/2013-04-01/hostedzone/*`, `/2013-04-01/healthcheck/*`, `/2013-04-01/change/*` | REST XML | 25 | | [Route 53 Resolver](route53resolver.md) | `POST /` + `X-Amz-Target: Route53Resolver.*` | JSON 1.1 | 18 | diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactController.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactController.java index 204457d4f3..eba721b01f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactController.java +++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactController.java @@ -11,13 +11,18 @@ import io.github.hectorvent.floci.core.common.PaginatedResult; import io.github.hectorvent.floci.core.common.Pagination; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.services.codeartifact.CodeArtifactService.DomainView; +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.CodeArtifactService.ResourcePolicy; +import io.github.hectorvent.floci.services.codeartifact.model.CodeArtifactPackageVersion; import io.github.hectorvent.floci.services.codeartifact.model.CodeArtifactRepository; import io.github.hectorvent.floci.services.codeartifact.model.ExternalConnection; +import io.github.hectorvent.floci.services.codeartifact.model.PackageAsset; import jakarta.inject.Inject; import jakarta.ws.rs.Consumes; import jakarta.ws.rs.DELETE; import jakarta.ws.rs.GET; +import jakarta.ws.rs.HeaderParam; import jakarta.ws.rs.POST; import jakarta.ws.rs.PUT; import jakarta.ws.rs.Path; @@ -272,6 +277,64 @@ public class CodeArtifactController { return ok(single("repository", repositoryDescription(r))); } + // ------------------------------------------------------- package versions + + @POST + @Path("/v1/package/version/publish") + @Consumes(MediaType.WILDCARD) + public Response publishPackageVersion(@Context HttpHeaders headers, @QueryParam("domain") String domain, + @QueryParam("domain-owner") String domainOwner, + @QueryParam("repository") String repository, + @QueryParam("format") String format, + @QueryParam("namespace") String namespace, + @QueryParam("package") String packageName, + @QueryParam("version") String version, + @QueryParam("asset") String assetName, + @QueryParam("unfinished") String unfinished, + @HeaderParam("x-amz-content-sha256") String assetSha256, + byte[] body) { + String region = regionResolver.resolveRegion(headers); + PublishPackageVersionResult result = service.publishPackageVersion(region, domain, domainOwner, repository, + format, namespace, packageName, version, assetName, assetSha256, unfinished, body); + return ok(publishPackageVersionResponse(result)); + } + + @GET + @Path("/v1/package/version") + public Response describePackageVersion(@Context HttpHeaders headers, @QueryParam("domain") String domain, + @QueryParam("domain-owner") String domainOwner, + @QueryParam("repository") String repository, + @QueryParam("format") String format, + @QueryParam("namespace") String namespace, + @QueryParam("package") String packageName, + @QueryParam("version") String version) { + String region = regionResolver.resolveRegion(headers); + CodeArtifactPackageVersion pv = service.describePackageVersion(region, domain, domainOwner, repository, + format, namespace, packageName, version); + return ok(single("packageVersion", packageVersionDescription(pv))); + } + + @GET + @Path("/v1/package/version/asset") + public Response getPackageVersionAsset(@Context HttpHeaders headers, @QueryParam("domain") String domain, + @QueryParam("domain-owner") String domainOwner, + @QueryParam("repository") String repository, + @QueryParam("format") String format, + @QueryParam("namespace") String namespace, + @QueryParam("package") String packageName, + @QueryParam("version") String version, + @QueryParam("asset") String assetName, + @QueryParam("revision") String revision) { + String region = regionResolver.resolveRegion(headers); + PackageVersionAssetResult result = service.getPackageVersionAsset(region, domain, domainOwner, repository, + format, namespace, packageName, version, assetName, revision); + return Response.ok(result.asset().getContent(), MediaType.APPLICATION_OCTET_STREAM) + .header("X-AssetName", result.asset().getName()) + .header("X-PackageVersion", version) + .header("X-PackageVersionRevision", result.packageVersionRevision()) + .build(); + } + // -------------------------------------------------------------------- tags @POST @@ -377,6 +440,48 @@ public class CodeArtifactController { return response; } + private ObjectNode publishPackageVersionResponse(PublishPackageVersionResult result) { + ObjectNode node = objectMapper.createObjectNode(); + node.put("format", result.packageVersion().getFormat()); + if (result.packageVersion().getNamespace() != null) { + node.put("namespace", result.packageVersion().getNamespace()); + } + node.put("package", result.packageVersion().getPackageName()); + node.put("version", result.packageVersion().getVersion()); + node.put("versionRevision", result.packageVersion().getRevision()); + node.put("status", result.packageVersion().getStatus()); + node.set("asset", assetSummary(result.asset())); + return node; + } + + private ObjectNode packageVersionDescription(CodeArtifactPackageVersion pv) { + ObjectNode node = objectMapper.createObjectNode(); + node.put("format", pv.getFormat()); + if (pv.getNamespace() != null) { + node.put("namespace", pv.getNamespace()); + } + node.put("packageName", pv.getPackageName()); + node.put("version", pv.getVersion()); + node.put("revision", pv.getRevision()); + node.put("status", pv.getStatus()); + if (pv.getPublishedTime() != null) { + node.put("publishedTime", pv.getPublishedTime()); + } + ObjectNode origin = node.putObject("origin"); + origin.put("originType", "INTERNAL"); + node.putArray("licenses"); + return node; + } + + private ObjectNode assetSummary(PackageAsset asset) { + ObjectNode node = objectMapper.createObjectNode(); + node.put("name", asset.getName()); + node.put("size", asset.getSize()); + ObjectNode hashes = node.putObject("hashes"); + asset.getHashes().forEach(hashes::put); + return node; + } + private ObjectNode resourcePolicy(ResourcePolicy policy) { ObjectNode node = objectMapper.createObjectNode(); node.put("resourceArn", policy.resourceArn()); 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 9887efa1bf..56c86b68de 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 @@ -8,23 +8,40 @@ import io.github.hectorvent.floci.core.common.PaginatedResult; import io.github.hectorvent.floci.core.common.Pagination; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.common.Resettable; +import io.github.hectorvent.floci.core.common.ServiceConfigAccess; +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.model.CodeArtifactDomain; +import io.github.hectorvent.floci.services.codeartifact.model.CodeArtifactPackageVersion; import io.github.hectorvent.floci.services.codeartifact.model.CodeArtifactRepository; import io.github.hectorvent.floci.services.codeartifact.model.ExternalConnection; +import io.github.hectorvent.floci.services.codeartifact.model.PackageAsset; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; +import org.jboss.logging.Logger; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.AtomicMoveNotSupportedException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.time.Instant; import java.util.ArrayList; +import java.util.Comparator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Pattern; +import java.util.stream.Stream; @ApplicationScoped public class CodeArtifactService implements Resettable { @@ -46,26 +63,73 @@ public class CodeArtifactService implements Resettable { Set.of("npm", "pypi", "maven", "nuget", "generic", "ruby", "swift", "cargo"); private static final Set ENDPOINT_TYPES = Set.of("dualstack", "ipv4"); + private static final Logger LOG = Logger.getLogger(CodeArtifactService.class); + private static final Pattern DOMAIN_NAME = Pattern.compile("[a-z][a-z0-9\\-]{0,48}[a-z0-9]"); private static final Pattern REPOSITORY_NAME = Pattern.compile("[A-Za-z0-9][A-Za-z0-9._\\-]{1,99}"); private static final Pattern ACCOUNT_ID = Pattern.compile("[0-9]{12}"); + private static final Pattern PACKAGE_TOKEN = Pattern.compile("[^#/\\s]+"); + // Matches the wire model's AssetName pattern exactly: any non-empty string with no + // control/format/unassigned/surrogate/private-use characters. Without this, an asset name + // containing '\r'/'\n' would be echoed back verbatim in the X-AssetName response header. + private static final Pattern ASSET_NAME = Pattern.compile("\\P{C}+"); + private static final List ASSET_HASH_ALGORITHMS = List.of("MD5", "SHA-1", "SHA-256", "SHA-512"); + private static final long MAX_ASSET_FILE_SIZE_BYTES = 5L * 1024 * 1024 * 1024; + private static final int MAX_ASSETS_PER_PACKAGE_VERSION = 350; + private static final String ASSET_STORAGE_DIR = "codeartifact-assets"; public record DomainView(CodeArtifactDomain domain, int repositoryCount) {} public record ResourcePolicy(String resourceArn, String revision, String document) {} + public record PublishPackageVersionResult(CodeArtifactPackageVersion packageVersion, PackageAsset asset) {} + public record PackageVersionAssetResult(PackageAsset asset, String packageVersionRevision) {} private final AccountAwareStorageBackend domains; private final AccountAwareStorageBackend repositories; + private final AccountAwareStorageBackend packageVersions; private final RegionResolver regionResolver; private final EmulatorConfig config; + // Asset bytes never go through the JSON/WAL-backed `packageVersions` store (see PackageAsset's + // @JsonIgnore content field): they're kept here instead, on disk in persistent/hybrid/wal mode + // or in this map in memory mode, so a publish only ever rewrites metadata. + private final boolean inMemory; + private final Path assetRoot; + private final ConcurrentHashMap memoryAssetStore = new ConcurrentHashMap<>(); @Inject - public CodeArtifactService(StorageFactory storageFactory, RegionResolver regionResolver, EmulatorConfig config) { - this.domains = storageFactory.create("codeartifact", "codeartifact-domains.json", - new TypeReference>() {}); - this.repositories = storageFactory.create("codeartifact", "codeartifact-repositories.json", - new TypeReference>() {}); + public CodeArtifactService(StorageFactory storageFactory, RegionResolver regionResolver, EmulatorConfig config, + ServiceConfigAccess serviceConfigAccess) { + this( + storageFactory.create("codeartifact", "codeartifact-domains.json", + new TypeReference>() {}), + storageFactory.create("codeartifact", "codeartifact-repositories.json", + new TypeReference>() {}), + storageFactory.create("codeartifact", "codeartifact-package-versions.json", + new TypeReference>() {}), + regionResolver, config, + "memory".equals(serviceConfigAccess.storageMode("codeartifact")), + Path.of(config.storage().persistentPath()).resolve(ASSET_STORAGE_DIR)); + } + + /** Package-private constructor for testing. */ + CodeArtifactService(AccountAwareStorageBackend domains, + AccountAwareStorageBackend repositories, + AccountAwareStorageBackend packageVersions, + RegionResolver regionResolver, EmulatorConfig config, + boolean inMemory, Path assetRoot) { + this.domains = domains; + this.repositories = repositories; + this.packageVersions = packageVersions; this.regionResolver = regionResolver; this.config = config; + this.inMemory = inMemory; + this.assetRoot = assetRoot; + if (!inMemory) { + try { + Files.createDirectories(assetRoot); + } catch (IOException e) { + throw new UncheckedIOException("Failed to create CodeArtifact asset data directory: " + assetRoot, e); + } + } } // ---------------------------------------------------------------- domains @@ -350,6 +414,120 @@ public class CodeArtifactService implements Resettable { return r; } + // ------------------------------------------------------- package versions + + public synchronized PublishPackageVersionResult publishPackageVersion(String region, String domain, + String domainOwner, String repository, String format, String namespace, String packageName, + String version, String assetName, String assetSha256, String unfinishedParam, byte[] content) { + if (!"generic".equals(format)) { + throw validation("format must be 'generic'; PublishPackageVersion only supports the generic " + + "package format."); + } + boolean unfinished = parseBoolean(unfinishedParam); + validatePackageToken("package", packageName); + validatePackageToken("packageVersion", version); + if (namespace != null) { + validatePackageToken("namespace", namespace); + } + validateAssetName(assetName); + if (assetSha256 == null || !SigV4RequestValidator.isSha256Hex(assetSha256)) { + throw validation("assetSHA256 is required and must be a 64-character SHA-256 hex digest."); + } + byte[] assetContent = content != null ? content : new byte[0]; + if (assetContent.length > MAX_ASSET_FILE_SIZE_BYTES) { + throw new AwsException("ServiceQuotaExceededException", + "The maximum asset file size is 5 Gigabytes.", 402); + } + String actualSha256 = sha256Hex(assetContent); + if (!assetSha256.equalsIgnoreCase(actualSha256)) { + throw validation("assetSHA256 does not match the SHA-256 hash of the uploaded content."); + } + + String owner = effectiveOwner(domainOwner); + requireRepository(owner, repositoryKey(region, domain, repository)); + String key = packageVersionKey(region, domain, repository, format, namespace, packageName, version); + + CodeArtifactPackageVersion pv = packageVersions.getForAccount(owner, key).orElse(null); + if (pv != null && "Published".equals(pv.getStatus())) { + throw conflict("Package version '" + version + "' of package '" + packageName + "' is already " + + "Published; no additional assets can be uploaded to it."); + } + if (pv == null) { + pv = new CodeArtifactPackageVersion(); + pv.setDomainName(domain); + pv.setDomainOwner(owner); + pv.setRegion(region); + pv.setRepositoryName(repository); + pv.setFormat(format); + pv.setNamespace(namespace); + pv.setPackageName(packageName); + pv.setVersion(version); + } + + Map assets = new LinkedHashMap<>(pv.getAssets()); + if (!assets.containsKey(assetName) && assets.size() >= MAX_ASSETS_PER_PACKAGE_VERSION) { + throw new AwsException("ServiceQuotaExceededException", + "A package version can have a maximum of " + MAX_ASSETS_PER_PACKAGE_VERSION + " assets.", 402); + } + + PackageAsset asset = new PackageAsset(); + asset.setName(assetName); + asset.setSize(assetContent.length); + asset.setContent(assetContent); + asset.setHashes(computeHashes(assetContent)); + writeAssetContent(owner, key, assetName, assetContent); + assets.put(assetName, asset); + pv.setAssets(assets); + + pv.setStatus(unfinished ? "Unfinished" : "Published"); + if (!unfinished && pv.getPublishedTime() == null) { + pv.setPublishedTime(Instant.now().getEpochSecond()); + } + pv.setRevision(newRevision()); + + packageVersions.putForAccount(owner, key, pv); + return new PublishPackageVersionResult(pv, asset); + } + + public CodeArtifactPackageVersion describePackageVersion(String region, String domain, String domainOwner, + String repository, String format, String namespace, String packageName, String version) { + if (format == null || !PACKAGE_FORMATS.contains(format)) { + throw validation("format must be one of " + PACKAGE_FORMATS + "."); + } + String owner = effectiveOwner(domainOwner); + String key = packageVersionKey(region, domain, repository, format, namespace, packageName, version); + return packageVersions.getForAccount(owner, key) + .orElseThrow(() -> notFound("Package version '" + version + "' of package '" + packageName + + "' was not found.")); + } + + public PackageVersionAssetResult getPackageVersionAsset(String region, String domain, String domainOwner, + String repository, String format, String namespace, String packageName, String version, + String assetName, String packageVersionRevision) { + if (format == null || !PACKAGE_FORMATS.contains(format)) { + throw validation("format must be one of " + PACKAGE_FORMATS + "."); + } + String owner = effectiveOwner(domainOwner); + String key = packageVersionKey(region, domain, repository, format, namespace, packageName, version); + CodeArtifactPackageVersion pv = packageVersions.getForAccount(owner, key) + .orElseThrow(() -> notFound("Package version '" + version + "' of package '" + packageName + + "' was not found.")); + if (packageVersionRevision != null && !packageVersionRevision.equals(pv.getRevision())) { + throw notFound("Package version '" + version + "' was not found at revision '" + + packageVersionRevision + "'."); + } + PackageAsset asset = pv.getAssets().get(assetName); + if (asset == null) { + throw notFound("Asset '" + assetName + "' was not found on package version '" + version + "'."); + } + // The live object's content may already be populated (same JVM as the publish that set + // it), but it isn't guaranteed to be: a reload from the persisted store never restores it, + // since it's excluded from that JSON/WAL. Always re-read from the asset store itself + // rather than trusting whatever happens to already be on the object. + asset.setContent(readAssetContent(owner, key, assetName)); + return new PackageVersionAssetResult(asset, pv.getRevision()); + } + // -------------------------------------------------------------------- tags public synchronized void tagResource(String resourceArn, Map newTags) { @@ -398,6 +576,12 @@ public class CodeArtifactService implements Resettable { public void clear() { domains.clear(); repositories.clear(); + packageVersions.clear(); + if (inMemory) { + memoryAssetStore.clear(); + } else { + deleteAssetRoot(); + } } // ----------------------------------------------------------------- helpers @@ -522,6 +706,125 @@ public class CodeArtifactService implements Resettable { return merged; } + private static void validatePackageToken(String field, String value) { + if (value == null || value.isEmpty() || value.length() > 255 || !PACKAGE_TOKEN.matcher(value).matches()) { + throw validation(field + " must be 1-255 characters with no '#', '/', or whitespace."); + } + } + + private static void validateAssetName(String assetName) { + if (assetName == null || assetName.isEmpty() || assetName.length() > 255 + || !ASSET_NAME.matcher(assetName).matches()) { + throw validation("asset must be 1-255 characters with no control characters."); + } + } + + // ----------------------------------------------------------- asset bytes + + private void writeAssetContent(String owner, String packageVersionKey, String assetName, byte[] content) { + if (inMemory) { + memoryAssetStore.put(assetStoreKey(owner, packageVersionKey, assetName), content); + return; + } + Path filePath = resolveAssetPath(owner, packageVersionKey, assetName); + try { + Files.createDirectories(filePath.getParent()); + Path tmp = filePath.resolveSibling(filePath.getFileName() + ".tmp-" + UUID.randomUUID()); + try { + Files.write(tmp, content); + try { + Files.move(tmp, filePath, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); + } catch (AtomicMoveNotSupportedException e) { + Files.move(tmp, filePath, StandardCopyOption.REPLACE_EXISTING); + } + } finally { + Files.deleteIfExists(tmp); + } + } catch (IOException e) { + throw new UncheckedIOException("Failed to write CodeArtifact asset file: " + filePath, e); + } + } + + private byte[] readAssetContent(String owner, String packageVersionKey, String assetName) { + if (inMemory) { + byte[] content = memoryAssetStore.get(assetStoreKey(owner, packageVersionKey, assetName)); + return content != null ? content : new byte[0]; + } + Path filePath = resolveAssetPath(owner, packageVersionKey, assetName); + try { + return Files.readAllBytes(filePath); + } catch (IOException e) { + throw new UncheckedIOException("Failed to read CodeArtifact asset file: " + filePath, e); + } + } + + private static String assetStoreKey(String owner, String packageVersionKey, String assetName) { + return owner + "::" + packageVersionKey + "::" + assetName; + } + + /** + * Both path segments are SHA-256 hex digests, never the raw names: an asset name may contain + * '/', '.', '..' or run long, and the version key can exceed a filesystem's filename limit, so + * using either directly would let distinct legal names collide or fail. Digests are fixed + * length, path-safe, and cannot escape the directory. + */ + private Path resolveAssetPath(String owner, String packageVersionKey, String assetName) { + return assetRoot.resolve(owner) + .resolve(sha256Hex(packageVersionKey.getBytes(StandardCharsets.UTF_8))) + .resolve(sha256Hex(assetName.getBytes(StandardCharsets.UTF_8))); + } + + private void deleteAssetRoot() { + if (!Files.isDirectory(assetRoot)) { + return; + } + try (Stream paths = Files.walk(assetRoot)) { + paths.sorted(Comparator.reverseOrder()).forEach(p -> { + try { + Files.deleteIfExists(p); + } catch (IOException e) { + LOG.errorv(e, "Failed to delete CodeArtifact asset file: {0}", p); + } + }); + } catch (IOException e) { + LOG.errorv(e, "Failed to reset CodeArtifact asset storage under {0}", assetRoot); + } + } + + private static boolean parseBoolean(String value) { + if (value == null || value.isBlank()) { + return false; + } + if ("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value)) { + return Boolean.parseBoolean(value); + } + throw validation("unfinished must be 'true' or 'false'."); + } + + private static Map computeHashes(byte[] content) { + Map hashes = new LinkedHashMap<>(); + for (String algorithm : ASSET_HASH_ALGORITHMS) { + hashes.put(algorithm, SigV4RequestValidator.hexEncode(digest(algorithm, content))); + } + return hashes; + } + + private static String sha256Hex(byte[] content) { + try { + return SigV4RequestValidator.sha256Hex(content); + } catch (Exception e) { + throw new IllegalStateException("JVM does not support SHA-256", e); + } + } + + private static byte[] digest(String algorithm, byte[] content) { + try { + return MessageDigest.getInstance(algorithm).digest(content); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException("JVM does not support " + algorithm, e); + } + } + private static String domainKey(String region, String domain) { return region + "::" + domain; } @@ -530,6 +833,12 @@ public class CodeArtifactService implements Resettable { return region + "::" + domain + "::" + repository; } + private static String packageVersionKey(String region, String domain, String repository, String format, + String namespace, String packageName, String version) { + return region + "::" + domain + "::" + repository + "::" + format + "::" + + (namespace == null ? "" : namespace) + "::" + packageName + "::" + version; + } + private static AwsException validation(String message) { return new AwsException("ValidationException", message, 400); } diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/model/CodeArtifactPackageVersion.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/model/CodeArtifactPackageVersion.java new file mode 100644 index 0000000000..5dd005df57 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/model/CodeArtifactPackageVersion.java @@ -0,0 +1,121 @@ +package io.github.hectorvent.floci.services.codeartifact.model; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import io.quarkus.runtime.annotations.RegisterForReflection; + +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; + +@RegisterForReflection +@JsonIgnoreProperties(ignoreUnknown = true) +public class CodeArtifactPackageVersion { + private String domainName; + private String domainOwner; + private String region; + private String repositoryName; + private String format; + private String namespace; + private String packageName; + private String version; + private String status; + private String revision; + private Long publishedTime; + private Map assets = new LinkedHashMap<>(); + + public String getDomainName() { + return domainName; + } + + public void setDomainName(String domainName) { + this.domainName = domainName; + } + + public String getDomainOwner() { + return domainOwner; + } + + public void setDomainOwner(String domainOwner) { + this.domainOwner = domainOwner; + } + + public String getRegion() { + return region; + } + + public void setRegion(String region) { + this.region = region; + } + + public String getRepositoryName() { + return repositoryName; + } + + public void setRepositoryName(String repositoryName) { + this.repositoryName = repositoryName; + } + + public String getFormat() { + return format; + } + + public void setFormat(String format) { + this.format = format; + } + + public String getNamespace() { + return namespace; + } + + public void setNamespace(String namespace) { + this.namespace = namespace; + } + + public String getPackageName() { + return packageName; + } + + public void setPackageName(String packageName) { + this.packageName = packageName; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getRevision() { + return revision; + } + + public void setRevision(String revision) { + this.revision = revision; + } + + public Long getPublishedTime() { + return publishedTime; + } + + public void setPublishedTime(Long publishedTime) { + this.publishedTime = publishedTime; + } + + public Map getAssets() { + return Collections.unmodifiableMap(assets); + } + + public void setAssets(Map assets) { + this.assets = assets == null ? new LinkedHashMap<>() : new LinkedHashMap<>(assets); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/model/PackageAsset.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/model/PackageAsset.java new file mode 100644 index 0000000000..28c6b85727 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/model/PackageAsset.java @@ -0,0 +1,55 @@ +package io.github.hectorvent.floci.services.codeartifact.model; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import io.quarkus.runtime.annotations.RegisterForReflection; + +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; + +@RegisterForReflection +@JsonIgnoreProperties(ignoreUnknown = true) +public class PackageAsset { + private String name; + private long size; + // Kept out of the persisted package-version JSON/WAL: CodeArtifactService writes and reads + // these bytes through its own asset store (disk file in persistent/hybrid/wal mode, a side + // map in memory mode) instead, so a publish never rewrites every asset of every package + // version just to append one more. + @JsonIgnore + private byte[] content = new byte[0]; + private Map hashes = new LinkedHashMap<>(); + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public long getSize() { + return size; + } + + public void setSize(long size) { + this.size = size; + } + + public byte[] getContent() { + return content.clone(); + } + + public void setContent(byte[] content) { + this.content = content == null ? new byte[0] : content.clone(); + } + + public Map getHashes() { + return Collections.unmodifiableMap(hashes); + } + + public void setHashes(Map hashes) { + this.hashes = hashes == null ? new LinkedHashMap<>() : new LinkedHashMap<>(hashes); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactAssetPersistenceTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactAssetPersistenceTest.java new file mode 100644 index 0000000000..aabd7c252e --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactAssetPersistenceTest.java @@ -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 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 domainStore = accountAware(dir, "codeartifact-domains.json", + new TypeReference>() {}); + AccountAwareStorageBackend repoStore = accountAware(dir, + "codeartifact-repositories.json", new TypeReference>() {}); + AccountAwareStorageBackend packageVersionStore = accountAware(dir, + "codeartifact-package-versions.json", new TypeReference>() {}); + + 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 AccountAwareStorageBackend accountAware(Path dir, String fileName, TypeReference> type) { + PersistentStorage 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); + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactIntegrationTest.java index ea3d82ffbc..5977cc2128 100644 --- a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactIntegrationTest.java @@ -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); + } + } } 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 cccce4072d..9c97414a22 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 @@ -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 domainStore = AccountAwareStorageBackend.inMemory(ACCOUNT_ID); AccountAwareStorageBackend 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 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); + } + } }