Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

feat(codeartifact): add NuGet/BaGetter sidecar for the nuget format (#5790)

Priamo Germosen3db18d58a5fe
diff --git a/docs/services/codeartifact.md b/docs/services/codeartifact.md
index b55a13aed2..5a032ccef9 100644
--- a/docs/services/codeartifact.md
+++ b/docs/services/codeartifact.md
@@ -7,10 +7,11 @@
Floci supports the CodeArtifact control plane: domains, repositories, resource policies, tags,
and public upstream (external) connections. Package publish/fetch through the CodeArtifact API
itself is implemented for the `generic` format only, matching AWS's own restriction that
-`PublishPackageVersion` accepts only `generic`. The `maven`, `npm`, and `pypi` formats are each
+`PublishPackageVersion` accepts only `generic`. The `maven`, `npm`, `pypi`, and `nuget` formats are each
served through their own real package-manager-protocol proxy (`mvn`/Gradle, `npm`/`yarn`/`pnpm`,
-and `pip`/`twine` publish and resolve all work against the URL `GetRepositoryEndpoint` returns);
-the remaining formats (NuGet, etc.) have no real proxy behind them yet.
+`pip`/`twine`, and `nuget`/`dotnet` publish and resolve all work against the URL
+`GetRepositoryEndpoint` returns); the remaining formats (Ruby, Swift, Cargo) have no real proxy
+behind them yet.
## Supported Actions
@@ -19,9 +20,9 @@ the remaining formats (NuGet, etc.) have no real proxy behind them yet.
| --- | --- |
| `CreateDomain` | Creates a domain (max 10 per account per Region), optionally with a KMS encryption key and initial tags. |
| `DeleteDomain` | Deletes a domain; fails with `ConflictException` while it still contains repositories, and with `ResourceNotFoundException` for a missing domain (as AWS does, although the API reference does not list it). |
-| `DescribeDomain` | Returns a domain's full description, including its repository count and the total size of its assets (generic, Maven, npm, and pypi; see Known limitations). |
+| `DescribeDomain` | Returns a domain's full description, including its repository count and the total size of its assets (generic, Maven, npm, pypi, and NuGet; see Known limitations). |
| `ListDomains` | Lists domain summaries for the account and Region, paginated. |
-| `GetAuthorizationToken` | Issues a bearer token scoped to a domain, valid for 0 (12 hours) or 900-43200 seconds, required by the `maven` and `npm` repository endpoints. |
+| `GetAuthorizationToken` | Issues a bearer token scoped to a domain, valid for 0 (12 hours) or 900-43200 seconds, required by the `maven`, `npm`, `pypi`, and `nuget` repository endpoints. |
| `PutDomainPermissionsPolicy` | Attaches or replaces a domain's resource policy, versioned by `policyRevision`. |
| `GetDomainPermissionsPolicy` | Returns a domain's current resource policy and revision. |
| `DeleteDomainPermissionsPolicy` | Removes a domain's resource policy, optionally checked against `policyRevision`. |
@@ -184,6 +185,43 @@ response is never a spurious conflict), and only a name collision with genuinely
gets rejected with 409. One config knob, `FLOCI_SERVICES_CODEARTIFACT_PYPI_IMAGE`, pins the image
version.
+## The nuget repository endpoint
+
+`GetRepositoryEndpoint` for `format=nuget` returns `http://localhost:4566/codeartifact/nuget/<domain>/<repository>/`.
+Real `nuget`/`dotnet` clients can publish to and resolve from that URL once `v3/index.json` is
+appended to it, AWS's own documented requirement ("The source URL must end in /v3/index.json");
+Floci authors that index itself rather than proxying one from the backing
+[BaGetter](https://github.com/bagetter/bagetter) container, advertising only the `PackageBaseAddress/
+3.0.0` (install) and `PackagePublish/2.0.0` (push) resources BaGetter's own flat container and
+publish endpoints implement. Every request needs a token from `GetAuthorizationToken` scoped to
+the domain being accessed, sent as HTTP Basic with the token as the password (any username),
+the way AWS's own NuGet CLI configuration doc has the client authenticate
+([configure the nuget or dotnet CLI](https://docs.aws.amazon.com/codeartifact/latest/ug/nuget-cli.html)),
+the same Basic convention Maven's HTTP wagon and pip/twine use; a Bearer token is also accepted,
+matching npm. A missing, invalid, expired, or wrong-domain token gets a 401 challenging `Basic`.
+
+Like Verdaccio and pypiserver, BaGetter has no native concept of multiple named feeds inside one
+instance, so each CodeArtifact repository gets its own BaGetter container instead of sharing one,
+started lazily on first use and identified internally by a fresh id generated at
+`CreateRepository` time, so a repository deleted and recreated under the same name never inherits
+the previous one's packages. `DeleteRepository` stops and removes that repository's container
+immediately, the same as npm and pypi.
+
+A `nuget push`/`dotnet nuget push` has no separate `name`/`version` form field the way twine's
+upload does; Floci reads both from the uploaded `.nupkg`'s own embedded `.nuspec` manifest (a
+`.nupkg` is a plain ZIP, parsed with the JDK's own `java.util.zip`, matching the same no-new-
+dependency approach already used elsewhere here). BaGetter's own `AllowPackageOverwrites: false`
+default rejects even a byte-identical re-push, so Floci's proxy applies the same idempotent-retry
+check as the pypi proxy: a re-push with content identical to what BaGetter already holds succeeds
+without forwarding anything, and only a genuine content mismatch is rejected with `409`. One
+config knob, `FLOCI_SERVICES_CODEARTIFACT_NUGET_IMAGE`, pins the image version.
+
+`GetPackageVersionReadme` for `nuget` is answered live against the BaGetter container rather than
+from a captured record: BaGetter keeps every version's own embedded readme file (if its nuspec
+declared one with `<readme>`) addressable by exact version for as long as that version exists,
+falling back to the nuspec's own `<description>` field when no readme file was embedded, matching
+AWS's documented "the readme file or descriptive text" wording for this action exactly.
+
## AWS-compatible failures
Domain and repository names, tags, pagination, duplicate names, missing upstreams, policy-revision
@@ -218,16 +256,16 @@ state.
`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` and the Maven and pypi
- repository endpoints all receive the request body as a single byte array before Floci ever
+- **The 5 GB asset size quota is nominal.** `PublishPackageVersion` and the Maven, pypi, and
+ nuget repository endpoints all receive 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 rejection (`ServiceQuotaExceededException` from `PublishPackageVersion`,
- HTTP 413 from the Maven and pypi endpoints) is correct for anything that does fit in memory; it
- is not itself a streaming size limit.
-- **Maven artifacts, npm packages, and pypi packages do not survive a Floci restart, even under
- persistent storage.** The Reposilite instance and every per-repository Verdaccio and pypiserver
- container have no volume attached and are removed on shutdown along with everything published to
- them. CodeArtifact repository/domain metadata (including each format's stored sidecar container
+ HTTP 413 from the Maven, pypi, and nuget endpoints) is correct for anything that does fit in
+ memory; it is not itself a streaming size limit.
+- **Maven artifacts, npm packages, pypi packages, and NuGet packages do not survive a Floci
+ restart, even under persistent storage.** The Reposilite instance and every per-repository
+ Verdaccio, pypiserver, and BaGetter container have no volume attached and are removed on shutdown
+ along with everything published to them. CodeArtifact repository/domain metadata (including each format's stored sidecar container
id) survives a restart the same way any other Floci state does under persistent storage mode; the
artifacts and packages themselves do not, so the first request after a restart re-provisions
empty storage and returns 404 for anything published before the restart.
@@ -235,27 +273,30 @@ state.
CodeArtifact tokens are scoped to the calling principal's permissions; Floci's are scoped only to
the domain named in the request; anyone who obtains one keeps the same domain-scoped access for
its full lifetime.
-- **The Maven, npm, and pypi sidecar containers have no authentication of their own, and anything
- else sharing their Docker network can reach them directly on their container port.** Real
- CodeArtifact's token check happens once, at Floci's own proxy layer (`CodeArtifactMavenController`/
- the npm data plane/`CodeArtifactPypiController`), before a request ever reaches Reposilite,
- Verdaccio, or pypiserver; those containers' own auth is deliberately disabled since the proxy is
- meant to be the only path in. Binding the published host port to loopback only keeps an outside
- host off that port; it does not stop another container already on the same configured Docker
- network (for example a Lambda, Batch, or ECS workload container) from reaching the sidecar on its
+- **The Maven, npm, pypi, and nuget sidecar containers have no authentication of their own, and
+ anything else sharing their Docker network can reach them directly on their container port.**
+ Real CodeArtifact's token check happens once, at Floci's own proxy layer
+ (`CodeArtifactMavenController`/the npm data plane/`CodeArtifactPypiController`/
+ `CodeArtifactNuGetController`), before a request ever reaches Reposilite, Verdaccio, pypiserver,
+ or BaGetter; those containers' own auth is deliberately disabled since the proxy is meant to be the
+ only path in. Binding the published host port to loopback only keeps an outside host off that
+ port; it does not stop another container already on the same configured Docker network (for
+ example a Lambda, Batch, or ECS workload container) from reaching the sidecar on its
container-internal address, bypassing the proxy's token check entirely. Closing this for real
would mean isolating each sidecar on its own Docker network reachable only from Floci's own
- process, a change that would need to apply uniformly across all three sidecar-backed formats, not
+ process, a change that would need to apply uniformly across all four sidecar-backed formats, not
a one-off fix to any single proxy.
-- **Neither the Maven, npm, nor pypi repository endpoint resolves through upstream repositories or
- external connections.** On real CodeArtifact, a repository with another repository configured as
- an upstream (`UpdateRepository`'s `upstreams`) or with an `AssociateExternalConnection` to a
- public repository (`public:maven-central`, `public:npmjs`, `public:pypi`, etc.) serves packages
- from those sources too, not just its own. Floci's proxies only ever look up the repository's own
- backing storage: a package that exists solely in an upstream, or only through an external
- connection, returns 404 through a repository that has it configured as one; the pypi proxy's
- pypiserver container is also started with `--disable-fallback` specifically so it never silently
- redirects a missing package to the real, public PyPI.
+- **None of the Maven, npm, pypi, or nuget repository endpoints resolve through upstream
+ repositories or external connections.** On real CodeArtifact, a repository with another
+ repository configured as an upstream (`UpdateRepository`'s `upstreams`) or with an
+ `AssociateExternalConnection` to a public repository (`public:maven-central`, `public:npmjs`,
+ `public:pypi`, `public:nuget-org`, etc.) serves packages from those sources too, not just its
+ own. Floci's proxies only ever look up the repository's own backing storage: a package that
+ exists solely in an upstream, or only through an external connection, returns 404 through a
+ repository that has it configured as one; the pypi proxy's pypiserver container is started with
+ `--disable-fallback`, and the nuget proxy's BaGetter container with `Mirror__Enabled=false`,
+ specifically so neither ever silently redirects a missing package to the real, public PyPI or
+ nuget.org.
- **The npm repository endpoint does not enforce the 5 GB asset size quota.** Unlike
`PublishPackageVersion` and the Maven endpoint, it streams the request straight through to the
backing Verdaccio container rather than buffering it first, so there is nowhere in the request
@@ -269,43 +310,51 @@ state.
requires clients to accept HTML as a fallback and pip/uv both do, but it is a real gap against
AWS's documented surface, not just a quirk of the sidecar.
- **`DescribePackageVersion` reports only format/namespace/packageName/version/revision/status/origin
- for Maven, npm, and pypi, same as it does for generic.** Real CodeArtifact's `PackageVersionDescription`
+ for Maven, npm, pypi, and nuget, same as it does for generic.** Real CodeArtifact's `PackageVersionDescription`
can also carry `displayName`, `summary`, `homePage`, `sourceCodeRepository`, `licenses`, and
`publishedTime`, extracted from the package's own metadata; none of those are populated for any
- format today, container-backed or generic. For Maven, npm, and pypi, the version's existence is
+ format today, container-backed or generic. For all four, the version's existence is
confirmed directly against its sidecar (`RepositorySidecarManager.packageVersionExists`) rather
than through the generic-format-only `packageVersions` record `GetPackageVersionAsset`'s own
bridge also moved past, and the revision is synthesized the same deterministic way
`GetPackageVersionAsset`'s bridge already does, so describing a version and then fetching one of
its assets see the same revision. Every sidecar-backed version reports `Published`: none of
- these three formats' native publish paths (`mvn deploy`, `npm publish`, `twine upload`) can leave
- a version in any other status the way a generic publish deliberately can with `unfinished=true`.
+ these four formats' native publish paths (`mvn deploy`, `npm publish`, `twine upload`, `nuget
+ push`) can leave a version in any other status the way a generic publish deliberately can with
+ `unfinished=true`.
+- **The nuget repository endpoint advertises no `RegistrationsBaseUrl` or `SearchQueryService`
+ resource.** `nuget install`/`dotnet add package`/`dotnet restore` resolve a package's versions
+ and dependencies from `PackageBaseAddress` alone (each version's own `.nuspec`, fetched directly,
+ carries its dependency groups), which is what Floci implements, so this doesn't affect the
+ install/restore path; it does mean `nuget list` has no resource to work against, which matches
+ AWS's own documented restriction that only NuGet's V3 protocol works against CodeArtifact at all
+ and that `nuget list` specifically isn't supported.
- **`DescribePackage`'s origin controls default to `publish: ALLOW`/`upstream: BLOCK`** (the
default for a package whose first version was published directly, since Floci never ingests
packages from an upstream source) **until a caller sets them explicitly with
- `PutPackageOriginConfiguration`.** For Maven, npm, and pypi, `DescribePackage` asks the
+ `PutPackageOriginConfiguration`.** For Maven, npm, pypi, and nuget, `DescribePackage` asks the
repository's sidecar whether the package exists, which starts that sidecar if it is not already
running; a package that exists only because `PutPackageOriginConfiguration` created it (no
versions yet) is found without touching any sidecar.
- **`PutPackageOriginConfiguration`'s `publish: BLOCK` is only enforced for the `generic`
format.** `PublishPackageVersion`, the one path it actually gates, accepts only `generic` to
- begin with; Maven, npm, and pypi publishes go through their own native wire protocol (`mvn
- deploy`, `npm publish`, `twine upload`) directly against the repository endpoint, which does not
- yet check this setting. `upstream` is stored and reported faithfully for every format but is
+ begin with; Maven, npm, pypi, and nuget publishes go through their own native wire protocol (`mvn
+ deploy`, `npm publish`, `twine upload`, `nuget push`) directly against the repository endpoint,
+ which does not yet check this setting. `upstream` is stored and reported faithfully for every format but is
never enforced for any of them, since Floci has no upstream ingestion path to block in the first
place.
- **`DescribeDomain`'s `assetSizeBytes` only counts what was published after this field started
being computed.** Generic-format bytes are summed live from the still-existing
- `PublishPackageVersion` records, so they're always current. Maven, npm, and pypi have no such
- record to sum (the same absent generic-format record `DescribePackageVersion`'s own bridge works
+ `PublishPackageVersion` records, so they're always current. Maven, npm, pypi, and nuget have no
+ such record to sum (the same absent generic-format record `DescribePackageVersion`'s own bridge works
around by asking the sidecar directly instead), so each proxy reports its own successfully-written
- byte count directly; a package published through one of those three formats
+ byte count directly; a package published through one of those four formats
before this accounting existed was never reported and is not retroactively counted. A genuine
- conflict or an idempotent no-op retry (any of the three formats' own duplicate-publish
- protection) correctly adds nothing either way. Both `DeleteRepository` and `DeletePackage` clean
+ conflict or an idempotent no-op retry (any of the four formats' own duplicate-publish protection)
+ correctly adds nothing either way. Both `DeleteRepository` and `DeletePackage` clean
up every format's own records and byte counts (and, for `DeleteRepository`, the per-version
asset content underneath them), so a repository or package recreated under the same name never
- inherits a predecessor's bytes. Maven/npm/pypi byte counts live only in memory, the same as the
+ inherits a predecessor's bytes. Maven/npm/pypi/nuget byte counts live only in memory, the same as the
sidecar content they describe (see the restart limitation below), not through `StorageFactory`
like every durable store here: the content these bytes count does not survive a restart either,
so neither does the count.
@@ -337,3 +386,4 @@ See the [CodeArtifact API Reference](https://docs.aws.amazon.com/codeartifact/la
| `FLOCI_SERVICES_CODEARTIFACT_MAVEN_TOKEN` | unset | `name:secret` access token for a pre-configured `MAVEN_URL` |
| `FLOCI_SERVICES_CODEARTIFACT_NPM_IMAGE` | `verdaccio/verdaccio:6.10.4` | Verdaccio image used to serve the `npm` format |
| `FLOCI_SERVICES_CODEARTIFACT_PYPI_IMAGE` | `pypiserver/pypiserver:v2.4.2` | pypiserver image used to serve the `pypi` format |
+| `FLOCI_SERVICES_CODEARTIFACT_NUGET_IMAGE` | `bagetter/bagetter:1.6.5` | BaGetter image used to serve the `nuget` format |
diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
index 008782a764..1fdab8a3b7 100644
--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
@@ -887,6 +887,15 @@ public interface EmulatorConfig {
*/
@WithDefault("pypiserver/pypiserver:v2.4.2")
String pypiImage();
+
+ /**
+ * Image used for the per-repository BaGetter container backing the {@code nuget} format.
+ * Same reasoning as {@link #npmImage()}: BaGetter has no native concept of multiple named
+ * feeds inside one instance either, so this gets one container per CodeArtifact
+ * repository too, and there is no single external instance a URL override could name.
+ */
+ @WithDefault("bagetter/bagetter:1.6.5")
+ String nugetImage();
}
interface ConnectServiceConfig {
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/PerKeyContainerPool.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/PerKeyContainerPool.java
index bed495191b..0897eb3c01 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/docker/PerKeyContainerPool.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/PerKeyContainerPool.java
@@ -11,8 +11,9 @@ import java.util.concurrent.atomic.AtomicLong;
* that lifecycle logic (map of running containers, per-key start lock so concurrent first-use of
* two different keys never blocks on each other, health poll, restart-on-unhealthy, stop-all on
* shutdown) has nothing Verdaccio-specific in it; only how to actually build and start a container
- * for a given key does. A future per-repository sidecar (pypiserver, BaGet) can reuse this
- * directly instead of re-deriving the same map/lock/poll skeleton.
+ * for a given key does. {@code PypiserverSidecarManager} and {@code BagetterSidecarManager}
+ * already reuse this directly instead of re-deriving the same map/lock/poll skeleton; a future
+ * per-repository sidecar for a new format can do the same.
*
* <p>Callers own the health-check path and the actual container creation (image, config, env),
* supplied per call since those are exactly the format-specific parts.
diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/BagetterSidecarClient.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/BagetterSidecarClient.java
new file mode 100644
index 0000000000..26ae7332b3
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/BagetterSidecarClient.java
@@ -0,0 +1,276 @@
+package io.github.hectorvent.floci.services.codeartifact;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.net.http.HttpResponse.BodyHandlers;
+import java.nio.charset.StandardCharsets;
+import java.time.Duration;
+import java.util.List;
+import java.util.Locale;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * The HTTP wire protocol for a BaGetter sidecar: the NuGet V3 Package Content resource (flat
+ * container) and the V2-shaped publish resource BaGetter's push/delete endpoints still use.
+ * Container lifecycle (start, readiness, stop) is {@link BagetterSidecarManager}'s job; this class
+ * asks it for a ready base URL and speaks BaGetter's own protocol against it.
+ *
+ * <p>Deliberately does not implement the NuGet V3 Registration or Search resources:
+ * {@code nuget install}/{@code dotnet add package} both resolve a package's versions and
+ * dependencies from the Package Content resource alone (confirmed directly: NuGet's own V3
+ * client falls back to the flat container's {@code index.json} plus each version's {@code
+ * .nuspec} when a source advertises no {@code RegistrationsBaseUrl} resource), and AWS's own
+ * CodeArtifact User Guide documents {@code nuget list} itself as unsupported against CodeArtifact
+ * regardless (only V3 of the protocol works at all), which is exactly what Search would back.
+ * {@code CodeArtifactNuGetController} authors its own service index rather than proxying BaGetter's,
+ * so this never needs to rewrite a self-referential URL out of a BaGetter response the way
+ * {@code PypiserverSidecarClient} rewrites pypiserver's simple-index HTML.
+ */
+@ApplicationScoped
+public class BagetterSidecarClient implements RepositorySidecarManager {
+
+ private final BagetterSidecarManager manager;
+ private final ObjectMapper mapper;
+ private final HttpClient httpClient;
+ // Shared with CodeArtifactNuGetController's own push handler via lockFor(), keyed by
+ // repository container id: without one lock both paths take, a native push that lists a
+ // not-yet-deleted version and a DeletePackage's own version-list snapshot can interleave,
+ // letting a version pushed in that window survive a DeletePackage that already "succeeded".
+ private final ConcurrentHashMap<String, Object> repositoryLocks = new ConcurrentHashMap<>();
+
+ @Inject
+ public BagetterSidecarClient(BagetterSidecarManager manager, ObjectMapper mapper) {
+ this(manager, mapper, HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build());
+ }
+
+ BagetterSidecarClient(BagetterSidecarManager manager, ObjectMapper mapper, HttpClient httpClient) {
+ this.manager = manager;
+ this.mapper = mapper;
+ this.httpClient = httpClient;
+ }
+
+ @Override
+ public String format() {
+ return "nuget";
+ }
+
+ @Override
+ public String ensureReady(String repositoryContainerId, String publicUrl) {
+ return manager.ensureReady(repositoryContainerId, publicUrl);
+ }
+
+ @Override
+ public void release(String repositoryContainerId) {
+ manager.release(repositoryContainerId);
+ }
+
+ /**
+ * NuGet packages have no namespace (AWS: "Python, NuGet, Ruby, and Cargo package versions do
+ * not contain a corresponding component") and exactly one real asset per version, the
+ * {@code .nupkg} file itself; AWS's own NuGet asset-name-normalization doc says this name is
+ * constructed from the normalized package name and version, so {@code assetName} is checked
+ * against that exact, deterministic value rather than fetched unconditionally: a caller asking
+ * for any other name is asking for an asset that was never, and could never be, published.
+ */
+ @Override
+ public Optional<byte[]> fetchPackageVersionAsset(String repositoryContainerId, String domain, String repository,
+ String namespace, String packageName, String version, String assetName) {
+ String lowerId = packageName.toLowerCase(Locale.ROOT);
+ Optional<String> normalizedVersion = matchVersion(repositoryContainerId, lowerId, version);
+ if (normalizedVersion.isEmpty()) {
+ return Optional.empty();
+ }
+ String expectedAssetName = lowerId + "." + normalizedVersion.get() + ".nupkg";
+ if (!expectedAssetName.equals(assetName)) {
+ return Optional.empty();
+ }
+ return getBytes(repositoryContainerId, "/v3/package/" + SidecarUriUtils.encodeSegment(lowerId) + "/"
+ + SidecarUriUtils.encodeSegment(normalizedVersion.get()) + "/"
+ + SidecarUriUtils.encodeSegment(expectedAssetName));
+ }
+
+ @Override
+ public boolean packageExists(String repositoryContainerId, String domain, String repository, String namespace,
+ String packageName) {
+ return !listVersions(repositoryContainerId, packageName.toLowerCase(Locale.ROOT)).isEmpty();
+ }
+
+ /**
+ * Bridges {@code DescribePackageVersion}: reuses {@link #matchVersion}, the same normalized
+ * lookup {@link #fetchPackageVersionAsset} and {@link #fetchPackageVersionReadme} already use
+ * against BaGetter's own flat container listing, so a caller-supplied version in any form
+ * BaGetter itself would accept (leading zeroes, a trailing zero fourth segment, build
+ * metadata) resolves the same way here as it does for those two.
+ */
+ @Override
+ public boolean packageVersionExists(String repositoryContainerId, String domain, String repository,
+ String namespace, String packageName, String version) {
+ return matchVersion(repositoryContainerId, packageName.toLowerCase(Locale.ROOT), version).isPresent();
+ }
+
+ /**
+ * Deletes every version of the named package, confirmed against a live container.
+ *
+ * <p>Runs inside {@link #lockFor}: without it, a native push that lists the (not yet
+ * updated) version set after this method's own {@link #listVersions} snapshot, but finishes
+ * before this method's delete loop does, would add a version this call never knew to delete,
+ * leaving it behind even though {@code DeletePackage} already reported success.
+ */
+ @Override
+ public void deletePackage(String repositoryContainerId, String domain, String repository, String namespace,
+ String packageName) {
+ synchronized (lockFor(repositoryContainerId)) {
+ String lowerId = packageName.toLowerCase(Locale.ROOT);
+ String baseUrl = ensureReady(repositoryContainerId, null);
+ for (String version : listVersions(repositoryContainerId, lowerId)) {
+ URI uri = SidecarUriUtils.combine(URI.create(baseUrl), "/api/v2/package/"
+ + SidecarUriUtils.encodeSegment(lowerId) + "/" + SidecarUriUtils.encodeSegment(version));
+ HttpRequest request = HttpRequest.newBuilder(uri).timeout(Duration.ofSeconds(10)).DELETE().build();
+ HttpResponse<Void> response;
+ try {
+ response = httpClient.send(request, BodyHandlers.discarding());
+ } catch (Exception e) {
+ throw new IllegalStateException("Could not reach the BaGetter sidecar to delete " + packageName
+ + " " + version, e);
+ }
+ // 404 means this exact version is already gone: a sibling file deleted in an
+ // earlier iteration can't cause this (NuGet has one asset per version, unlike
+ // pypi), and a concurrent delete of the same package can't either now that both
+ // paths share this lock.
+ if (response.statusCode() != 204 && response.statusCode() != 404) {
+ throw new IllegalStateException("Could not delete " + packageName + " " + version + " from the "
+ + "BaGetter sidecar: upstream returned " + response.statusCode());
+ }
+ }
+ }
+ }
+
+ /**
+ * The lock {@link #deletePackage} and {@code CodeArtifactNuGetController}'s own push handler
+ * both take for one repository, so a push and a delete for the same repository can never
+ * interleave; different repositories never block each other. Package-private so the
+ * controller can synchronize its own push-or-conflict check and forwarded write on the exact
+ * same lock this method does, the only way either one closes the race against the other.
+ */
+ Object lockFor(String repositoryContainerId) {
+ return repositoryLocks.computeIfAbsent(repositoryContainerId, k -> new Object());
+ }
+
+ /**
+ * {@code empty()} only when {@code version} genuinely was never published; a published version
+ * with no readme file embedded answers {@code Optional.of("")} rather than {@code empty()}, the
+ * same empty-string parity already established for npm and pypi (a version republished without
+ * a readme records nothing to look up otherwise, which would 404 a version that genuinely
+ * exists). Unlike npm and pypi, this is answered live against the sidecar rather than from a
+ * record captured at publish time: BaGetter, unlike Verdaccio and pypiserver, keeps every
+ * version's own readme (if the nuspec declared one via {@code <readme>}) addressable by exact
+ * version for as long as that version exists, confirmed directly against BaGetter's own package
+ * content controller, so there is nothing to lose by not capturing it eagerly.
+ *
+ * <p>Falls back to the nuspec {@code <description>} element, a mandatory field in every valid
+ * nuspec, when the version exists but embeds no separate readme file: AWS documents this
+ * action as returning "the readme file or descriptive text for a package version", exactly
+ * this two-tier fallback.
+ */
+ @Override
+ public Optional<String> fetchPackageVersionReadme(String repositoryContainerId, String domain, String repository,
+ String namespace, String packageName, String version) {
+ String lowerId = packageName.toLowerCase(Locale.ROOT);
+ Optional<String> normalizedVersion = matchVersion(repositoryContainerId, lowerId, version);
+ if (normalizedVersion.isEmpty()) {
+ return Optional.empty();
+ }
+ Optional<byte[]> readme = getBytes(repositoryContainerId, "/v3/package/"
+ + SidecarUriUtils.encodeSegment(lowerId) + "/" + SidecarUriUtils.encodeSegment(normalizedVersion.get())
+ + "/readme");
+ if (readme.isPresent()) {
+ return Optional.of(new String(readme.get(), StandardCharsets.UTF_8));
+ }
+ Optional<byte[]> nuspec = getBytes(repositoryContainerId, "/v3/package/"
+ + SidecarUriUtils.encodeSegment(lowerId) + "/" + SidecarUriUtils.encodeSegment(normalizedVersion.get())
+ + "/" + SidecarUriUtils.encodeSegment(lowerId) + ".nuspec");
+ if (nuspec.isEmpty()) {
+ // The version was just confirmed to exist via matchVersion above; a nuspec that
+ // vanishes in the narrow window between that check and this fetch is a genuine
+ // mid-flight race (a concurrent DeletePackage), not a version that never existed.
+ return Optional.of("");
+ }
+ return Optional.of(NuspecXml.parse(nuspec.get()).description().orElse(""));
+ }
+
+ /**
+ * {@code empty()} when {@code version} doesn't match any version BaGetter actually holds for this
+ * package, its own normalized form otherwise. BaGetter lists each version it holds already
+ * normalized and lowercased (confirmed directly against its own {@code PackageContentController
+ * .GetPackageVersionsAsync}), so {@code version} is matched against that list under the same
+ * normalization applied to both sides rather than trusted as already being in that exact form:
+ * a caller is free to ask for {@code 1.00.0} and get the version BaGetter itself calls {@code
+ * 1.0.0}, the same leading-zero and trailing-zero normalization AWS documents for NuGet.
+ */
+ private Optional<String> matchVersion(String repositoryContainerId, String lowerId, String version) {
+ String wanted = NuGetVersions.normalize(version);
+ for (String candidate : listVersions(repositoryContainerId, lowerId)) {
+ if (candidate.equals(wanted)) {
+ return Optional.of(candidate);
+ }
+ }
+ return Optional.empty();
+ }
+
+ private List<String> listVersions(String repositoryContainerId, String lowerId) {
+ String baseUrl = ensureReady(repositoryContainerId, null);
+ URI uri = SidecarUriUtils.combine(URI.create(baseUrl), "/v3/package/" + SidecarUriUtils.encodeSegment(lowerId)
+ + "/index.json");
+ HttpRequest request = HttpRequest.newBuilder(uri).timeout(Duration.ofSeconds(10)).GET().build();
+ HttpResponse<String> response;
+ try {
+ response = httpClient.send(request, BodyHandlers.ofString());
+ } catch (Exception e) {
+ throw new IllegalStateException("Could not reach the BaGetter sidecar to list versions of " + lowerId, e);
+ }
+ if (response.statusCode() == 404) {
+ return List.of();
+ }
+ if (response.statusCode() != 200) {
+ throw new IllegalStateException("Could not list versions of " + lowerId + " on the BaGetter sidecar: "
+ + "upstream returned " + response.statusCode());
+ }
+ JsonNode versions;
+ try {
+ versions = mapper.readTree(response.body()).path("versions");
+ } catch (Exception e) {
+ throw new IllegalStateException("Could not parse BaGetter's version listing for " + lowerId, e);
+ }
+ return versions.isArray()
+ ? versions.valueStream().map(JsonNode::asText).toList()
+ : List.of();
+ }
+
+ private Optional<byte[]> getBytes(String repositoryContainerId, String encodedPath) {
+ String baseUrl = ensureReady(repositoryContainerId, null);
+ HttpRequest request = HttpRequest.newBuilder(SidecarUriUtils.combine(URI.create(baseUrl), encodedPath))
+ .timeout(Duration.ofSeconds(10)).GET().build();
+ HttpResponse<byte[]> response;
+ try {
+ response = httpClient.send(request, BodyHandlers.ofByteArray());
+ } catch (Exception e) {
+ throw new IllegalStateException("Could not reach the BaGetter sidecar at " + encodedPath, e);
+ }
+ if (response.statusCode() == 404) {
+ return Optional.empty();
+ }
+ if (response.statusCode() != 200) {
+ throw new IllegalStateException("Could not fetch " + encodedPath + " from the BaGetter sidecar: upstream "
+ + "returned " + response.statusCode());
+ }
+ return Optional.of(response.body());
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/BagetterSidecarManager.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/BagetterSidecarManager.java
new file mode 100644
index 0000000000..8ef75e45d2
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/BagetterSidecarManager.java
@@ -0,0 +1,122 @@
+package io.github.hectorvent.floci.services.codeartifact;
+
+import io.github.hectorvent.floci.config.EmulatorConfig;
+import io.github.hectorvent.floci.core.common.ContainerTeardown;
+import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
+import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
+import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;
+import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo;
+import io.github.hectorvent.floci.core.common.docker.ContainerSpec;
+import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;
+import io.github.hectorvent.floci.core.common.docker.PerKeyContainerPool;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import org.jboss.logging.Logger;
+
+/**
+ * Starts and stops one BaGetter container per CodeArtifact repository backing the {@code nuget}
+ * format, the same per-repository shape as Verdaccio (npm) and pypiserver (pypi) rather than
+ * Reposilite's one shared instance. The HTTP protocol lives in {@link BagetterSidecarClient}.
+ * Implements {@link ContainerTeardown} so reset, nuke, and shutdown stop every container this
+ * manager started.
+ *
+ * <p>Configured with {@code ApiKey} left empty, matching the image's own shipped default: BaGetter
+ * treats an empty configured key as "no key configured" and authenticates every request
+ * unconditionally in that case (traced directly in BaGetter's own {@code ApiKeyAuthenticationService}
+ * before relying on it). Real CodeArtifact authorization already happens once at Floci's proxy
+ * layer, {@code CodeArtifactNuGetController}, before a request ever reaches this container, the
+ * same reasoning Verdaccio's open {@code $all} access and pypiserver's {@code -a . -P .} already
+ * rely on. {@code PackageDeletionBehavior} is overridden to {@code HardDelete}: the image's own
+ * default, {@code Unlist}, only hides a package from listings while leaving its content in place,
+ * which would leave {@code DeletePackage} unable to make a later republish of the same version
+ * succeed and would leave {@code GetPackageVersionAsset} still able to fetch a "deleted" asset.
+ */
+@ApplicationScoped
+public class BagetterSidecarManager implements ContainerTeardown {
+
+ private static final Logger LOG = Logger.getLogger(BagetterSidecarManager.class);
+ // The pinned image's base (mcr.microsoft.com/dotnet/aspnet:9.0-alpine) sets
+ // ASPNETCORE_HTTP_PORTS=8080 itself (confirmed directly against the image's own env, the
+ // documented .NET 8+ convention of listening on 8080 rather than 80 in a non-root container),
+ // and BagetterEndpointBuilder maps a dedicated /health route Floci uses as the readiness check.
+ private static final int BAGETTER_PORT = 8080;
+ private static final String HEALTH_PATH = "/health";
+
+ private final ContainerBuilder containerBuilder;
+ private final ContainerLifecycleManager lifecycleManager;
+ private final EmulatorConfig config;
+ private final PerKeyContainerPool pool;
+
+ @Inject
+ public BagetterSidecarManager(ContainerBuilder containerBuilder, ContainerLifecycleManager lifecycleManager,
+ EmulatorConfig config) {
+ this.containerBuilder = containerBuilder;
+ this.lifecycleManager = lifecycleManager;
+ this.config = config;
+ this.pool = new PerKeyContainerPool(lifecycleManager, HEALTH_PATH);
+ }
+
+ /**
+ * Base URL of a ready BaGetter instance for this nuget repository, starting its container if this
+ * is the first use. {@code publicUrl} is unused: BaGetter's own responses that could otherwise
+ * embed a self-referential URL (its service index, registration, and search resources) are
+ * never proxied verbatim by {@link CodeArtifactNuGetController}, which authors the service
+ * index itself and does not implement registration or search, so there is nothing here for an
+ * internal, client-unreachable address to leak into.
+ */
+ public String ensureReady(String nugetRepositoryId, String publicUrl) {
+ return pool.ensureReady(nugetRepositoryId, () -> startContainer(nugetRepositoryId));
+ }
+
+ /** Stops and removes the container for one nuget repository, if one was ever started. */
+ public void release(String nugetRepositoryId) {
+ pool.stopContainer(nugetRepositoryId);
+ }
+
+ private PerKeyContainerPool.StartedContainer startContainer(String nugetRepositoryId) {
+ String image = config.services().codeartifact().nugetImage();
+ String containerName = ContainerStorageHelper.dockerName(config, "floci-bagetter-" + nugetRepositoryId);
+ lifecycleManager.removeIfExists(containerName);
+
+ // Loopback-only: this container authenticates nothing on its own (ApiKey left empty
+ // below), so the Bearer/Basic check in CodeArtifactNuGetController is the only thing
+ // standing between a client and the backing storage. Publishing this to every interface
+ // would let anyone who can reach the host bypass that check entirely, the same reasoning
+ // as Verdaccio's and pypiserver's own loopback binding.
+ // Storage/Database are left unconfigured here: the pinned image's own Dockerfile already
+ // sets Storage__Path=/data, Database__Type=Sqlite, and Database__ConnectionString to a
+ // writable path under /data (confirmed directly against the image's build), so there is
+ // no empty or unwritable default here to guard against the way pypiserver's "-P ." needs
+ // an explicit path.
+ ContainerSpec spec = containerBuilder.newContainer(image)
+ .withName(containerName)
+ .withEnv("ApiKey", "")
+ // Already the image's own shipped default; set explicitly so a future image
+ // default change can't quietly start resolving packages from the real nuget.org,
+ // matching the Maven, npm, and pypi proxies' own deliberate choice not to resolve
+ // upstream repositories or external connections.
+ .withEnv("Mirror__Enabled", "false")
+ .withEnv("PackageDeletionBehavior", "HardDelete")
+ .withLoopbackPortBinding(BAGETTER_PORT, 0)
+ .withDockerNetwork(config.services().dockerNetwork())
+ .withEmbeddedDns()
+ .withLogRotation()
+ .build();
+ String containerId = lifecycleManager.create(spec);
+ try {
+ ContainerInfo info = lifecycleManager.startCreated(containerId, spec);
+ EndpointInfo endpoint = info.getEndpoint(BAGETTER_PORT);
+ String url = "http://" + endpoint;
+ LOG.infov("BaGetter sidecar for nuget repository {0} is ready at {1}", nugetRepositoryId, url);
+ return new PerKeyContainerPool.StartedContainer(containerId, url);
+ } catch (RuntimeException e) {
+ lifecycleManager.stopAndRemove(containerId, null);
+ throw e;
+ }
+ }
+
+ @Override
+ public void stopManagedContainers() {
+ pool.stopAll();
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNuGetController.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNuGetController.java
new file mode 100644
index 0000000000..f3331d5ca5
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNuGetController.java
@@ -0,0 +1,322 @@
+package io.github.hectorvent.floci.services.codeartifact;
+
+import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.core.common.MultipartFormParser;
+import io.github.hectorvent.floci.services.codeartifact.CodeArtifactService.AuthorizationTokenScope;
+import jakarta.inject.Inject;
+import jakarta.ws.rs.Consumes;
+import jakarta.ws.rs.GET;
+import jakarta.ws.rs.NotAuthorizedException;
+import jakarta.ws.rs.NotFoundException;
+import jakarta.ws.rs.PUT;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.PathParam;
+import jakarta.ws.rs.Produces;
+import jakarta.ws.rs.core.Context;
+import jakarta.ws.rs.core.HttpHeaders;
+import jakarta.ws.rs.core.MediaType;
+import jakarta.ws.rs.core.Response;
+
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpRequest.BodyPublishers;
+import java.net.http.HttpResponse;
+import java.net.http.HttpResponse.BodyHandlers;
+import java.time.Duration;
+import java.util.Arrays;
+import java.util.Locale;
+import java.util.Optional;
+
+/**
+ * Serves the real NuGet V3 wire protocol behind the URL {@code GetRepositoryEndpoint} returns for
+ * the {@code nuget} format: the same source a real {@code nuget}/{@code dotnet} client speaks
+ * against once {@code v3/index.json} is appended to it (AWS's own documented requirement, "The
+ * source URL must end in /v3/index.json"), not a CodeArtifact API action. Backed by one BaGetter
+ * container per repository, provisioned on first use.
+ *
+ * <p>Authors its own service index rather than proxying BaGetter's own: BaGetter's real {@code
+ * v3/index.json} response embeds self-referential absolute URLs for every resource (its own base
+ * address, exactly the problem {@link RepositorySidecarManager#ensureReady}'s {@code publicUrl}
+ * parameter exists for), and this only ever needs two of those resources (see below), so
+ * authoring them directly avoids rewriting BaGetter's response the way {@code
+ * CodeArtifactPypiController} has to rewrite pypiserver's. Advertises only {@code
+ * PackageBaseAddress/3.0.0} (install) and {@code PackagePublish/2.0.0} (push): deliberately omits
+ * {@code RegistrationsBaseUrl} and {@code SearchQueryService} (see {@code BagetterSidecarClient}'s
+ * own javadoc for why neither is needed for {@code nuget install}/{@code restore} to work, and
+ * AWS's own docs call {@code nuget list}, the one command that would need search, unsupported
+ * against CodeArtifact regardless).
+ *
+ * <p>Requires the bearer token real CodeArtifact requires here, obtained from {@code
+ * GetAuthorizationToken}: AWS's own NuGet CLI configuration doc has the client authenticate with
+ * HTTP Basic ({@code username=aws, password=<token>}), the same convention Maven's HTTP wagon and
+ * pip/twine use, not npm's Bearer-only {@code _authToken}; {@link CodeArtifactTokenExtractor}
+ * accepts either scheme.
+ *
+ * <p>Unlike Maven and npm, and like pypi, this rejects a re-push of a package version BaGetter has
+ * already indexed itself, since BaGetter's own {@code AllowPackageOverwrites: false} default rejects
+ * even a byte-identical retry (confirmed directly against its push controller): a pre-upload
+ * check against BaGetter's own flat container and the existing version's own bytes is what gives
+ * CodeArtifact's documented idempotent-republish exception ("Overwriting package assets": a
+ * same-coordinate upload with identical content succeeds) to a sidecar that otherwise has none.
+ */
+@Path("/codeartifact/nuget/{domain}/{repository}")
+public class CodeArtifactNuGetController {
+
+ private final CodeArtifactService service;
+ private final BagetterSidecarClient bagetter;
+ private final HttpClient httpClient;
+
+ @Inject
+ public CodeArtifactNuGetController(CodeArtifactService service, BagetterSidecarClient bagetter) {
+ this(service, bagetter, HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build());
+ }
+
+ CodeArtifactNuGetController(CodeArtifactService service, BagetterSidecarClient bagetter, HttpClient httpClient) {
+ this.service = service;
+ this.bagetter = bagetter;
+ this.httpClient = httpClient;
+ }
+
+ private record RepositoryHandle(String repoId, String baseUrl, String publicBaseUrl, String region,
+ String owner) {
+ }
+
+ @GET
+ @Path("v3/index.json")
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response serviceIndex(@Context HttpHeaders headers, @PathParam("domain") String domain,
+ @PathParam("repository") String repository) {
+ RepositoryHandle handle = requireRepository(headers, domain, repository);
+ // domain and repository are free to embed here unescaped: requireRepository above only
+ // returns once CodeArtifactService has confirmed a repository by this exact name already
+ // exists, which it could only do if that name already passed CreateRepository's own
+ // DOMAIN_NAME/REPOSITORY_NAME validation, a character set with no '"', '\', or control
+ // character either pattern allows through.
+ String body = """
+ {
+ "version": "3.0.0",
+ "resources": [
+ { "@id": "%1$s/v3/package/", "@type": "PackageBaseAddress/3.0.0" },
+ { "@id": "%1$s/api/v2/package", "@type": "PackagePublish/2.0.0" }
+ ]
+ }
+ """.formatted(handle.publicBaseUrl());
+ return Response.status(200).entity(body).type(MediaType.APPLICATION_JSON).build();
+ }
+
+ @GET
+ @Path("v3/package/{id}/index.json")
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response packageVersions(@Context HttpHeaders headers, @PathParam("domain") String domain,
+ @PathParam("repository") String repository, @PathParam("id") String id) {
+ String baseUrl = requireRepository(headers, domain, repository).baseUrl();
+ return proxyGet(baseUrl, "/v3/package/" + SidecarUriUtils.encodeSegment(id) + "/index.json",
+ MediaType.APPLICATION_JSON);
+ }
+
+ /**
+ * One route for the nupkg, nuspec, and readme downloads: all three share the same {@code
+ * {id}/{version}/<file>} shape, and BaGetter itself doesn't validate the literal file name
+ * segment against the real one either (confirmed directly against its package content
+ * controller, which only binds {@code id} and {@code version} from the route; the AWS NuGet
+ * API spec's push side documents the same disregard, "The file name ... [is] ignored"),
+ * so dispatching on {@code filename}'s suffix here is exactly as faithful as BaGetter's own
+ * routing.
+ */
+ @GET
+ @Path("v3/package/{id}/{version}/{filename}")
+ @Produces(MediaType.WILDCARD)
+ public Response packageContent(@Context HttpHeaders headers, @PathParam("domain") String domain,
+ @PathParam("repository") String repository, @PathParam("id") String id,
+ @PathParam("version") String version, @PathParam("filename") String filename) {
+ String baseUrl = requireRepository(headers, domain, repository).baseUrl();
+ String lowerId = SidecarUriUtils.encodeSegment(id.toLowerCase(Locale.ROOT));
+ String encodedVersion = SidecarUriUtils.encodeSegment(version);
+ String lowerFilename = filename.toLowerCase(Locale.ROOT);
+ if ("readme".equals(lowerFilename)) {
+ return proxyGet(baseUrl, "/v3/package/" + lowerId + "/" + encodedVersion + "/readme",
+ "text/markdown");
+ }
+ if (lowerFilename.endsWith(".nuspec")) {
+ return proxyGet(baseUrl, "/v3/package/" + lowerId + "/" + encodedVersion + "/"
+ + lowerId + ".nuspec", "text/xml");
+ }
+ if (lowerFilename.endsWith(".nupkg")) {
+ return proxyGet(baseUrl, "/v3/package/" + lowerId + "/" + encodedVersion + "/"
+ + lowerId + "." + SidecarUriUtils.encodeSegment(version.toLowerCase(Locale.ROOT)) + ".nupkg",
+ MediaType.APPLICATION_OCTET_STREAM);
+ }
+ return Response.status(404).build();
+ }
+
+ /**
+ * Handles a {@code nuget push}/{@code dotnet nuget push}: {@code PUT} to the repository's
+ * {@code api/v2/package}, {@code multipart/form-data} whose first part is the raw bytes of
+ * the {@code .nupkg} (the real NuGet push protocol's own documented shape; unlike twine's
+ * upload, there is no separate {@code name}/{@code version} form field, so those are read
+ * from the uploaded package's own embedded {@code .nuspec} instead).
+ *
+ * <p>The existence check and the forwarded push run inside a lock keyed by this repository's
+ * id, the same reason {@code CodeArtifactPypiController#upload} does: without it, two pushes
+ * racing the same new id/version could both observe "not published yet" before either one's
+ * write actually lands.
+ */
+ @PUT
+ @Path("api/v2/package")
+ @Consumes(MediaType.WILDCARD)
+ public Response push(@Context HttpHeaders headers, @PathParam("domain") String domain,
+ @PathParam("repository") String repository, byte[] body) {
+ byte[] content = body != null ? body : new byte[0];
+ // Same 5 GB AWS quota PublishPackageVersion enforces, and just as nominal: RESTEasy
+ // Reactive has already buffered the full body into this byte[] before this method runs,
+ // so this rejects anything that made it this far rather than bounding memory use upfront.
+ if (content.length > CodeArtifactService.MAX_ASSET_FILE_SIZE_BYTES) {
+ return Response.status(413).build();
+ }
+ RepositoryHandle handle = requireRepository(headers, domain, repository);
+
+ String contentType = headers.getHeaderString(HttpHeaders.CONTENT_TYPE);
+ String boundary = MultipartFormParser.extractBoundary(contentType).orElse(null);
+ if (boundary == null) {
+ return Response.status(400).entity("Could not determine multipart boundary from Content-Type.").build();
+ }
+ MultipartFormParser.ParsedForm form = MultipartFormParser.parse(content, boundary);
+ Optional<MultipartFormParser.FilePart> file = form.file();
+ if (file.isEmpty()) {
+ return Response.status(400).entity("Upload must include the package file.").build();
+ }
+ byte[] nupkgBytes = file.get().content();
+
+ NuspecXml.Metadata nuspec;
+ try {
+ nuspec = NuspecXml.extractFromNupkg(nupkgBytes);
+ } catch (RuntimeException e) {
+ return Response.status(400).entity("Could not read the uploaded package: " + e.getMessage()).build();
+ }
+ String packageName = nuspec.id().toLowerCase(Locale.ROOT);
+ String version = NuGetVersions.normalize(nuspec.version());
+
+ // The exact same lock BagetterSidecarClient#deletePackage takes for this repository, not
+ // a private one: without sharing it, a DeletePackage racing this push could still list
+ // its versions before this push lands and miss it, the one case a private lock here
+ // could never close no matter how it synchronized with itself.
+ synchronized (bagetter.lockFor(handle.repoId())) {
+ Optional<byte[]> existing = existingNupkgContent(handle.baseUrl(), packageName, version);
+ if (existing.isPresent()) {
+ if (Arrays.equals(existing.get(), nupkgBytes)) {
+ return Response.status(200).build();
+ }
+ return Response.status(409)
+ .entity("A package with this id and version already exists.")
+ .build();
+ }
+
+ // Snapshotted before the real write, not after: see
+ // CodeArtifactService#currentContainerBackedPackageFence's own javadoc for why the
+ // order matters.
+ long fenceSnapshot = service.currentContainerBackedPackageFence(handle.region(), domain, handle.owner(),
+ repository, "nuget", null, packageName);
+ Response response = forwardPush(handle.baseUrl(), contentType, content);
+ boolean pushSucceeded = response.getStatus() == 200 || response.getStatus() == 201
+ || response.getStatus() == 202;
+ if (pushSucceeded) {
+ service.recordContainerBackedAssetBytes(handle.region(), domain, handle.owner(), repository,
+ "nuget", null, packageName, nupkgBytes.length, fenceSnapshot);
+ }
+ return response;
+ }
+ }
+
+ /**
+ * {@code empty()} when {@code packageName}/{@code version} isn't published on BaGetter yet, its
+ * actual bytes otherwise. A connectivity failure here throws rather than returning {@code
+ * empty()}, the same fail-safe reasoning as {@code CodeArtifactPypiController
+ * #existingFileContent}: letting a push with different content through to BaGetter during
+ * exactly the moment this check is unavailable would be the opposite of fail-safe, and BaGetter's
+ * own {@code AllowPackageOverwrites: false} would reject it anyway, just with a response this
+ * proxy no longer controls.
+ */
+ private Optional<byte[]> existingNupkgContent(String baseUrl, String packageName, String version) {
+ URI uri = SidecarUriUtils.combine(URI.create(baseUrl), "/v3/package/"
+ + SidecarUriUtils.encodeSegment(packageName) + "/" + SidecarUriUtils.encodeSegment(version) + "/"
+ + SidecarUriUtils.encodeSegment(packageName) + "." + SidecarUriUtils.encodeSegment(version)
+ + ".nupkg");
+ HttpRequest request = HttpRequest.newBuilder(uri).timeout(Duration.ofSeconds(10)).GET().build();
+ HttpResponse<byte[]> response;
+ try {
+ response = httpClient.send(request, BodyHandlers.ofByteArray());
+ } catch (Exception e) {
+ throw new IllegalStateException(
+ "Could not check nuget package " + packageName + " " + version + " for an existing push", e);
+ }
+ if (response.statusCode() == 404) {
+ return Optional.empty();
+ }
+ if (response.statusCode() != 200) {
+ throw new IllegalStateException(
+ "Could not check nuget package " + packageName + " " + version + ": upstream returned "
+ + response.statusCode());
+ }
+ return Optional.of(response.body());
+ }
+
+ private Response forwardPush(String baseUrl, String contentType, byte[] content) {
+ HttpRequest request = HttpRequest.newBuilder(SidecarUriUtils.combine(URI.create(baseUrl), "/api/v2/package"))
+ .timeout(Duration.ofSeconds(30))
+ .header("Content-Type", contentType)
+ .PUT(BodyPublishers.ofByteArray(content))
+ .build();
+ HttpResponse<Void> response;
+ try {
+ response = httpClient.send(request, BodyHandlers.discarding());
+ } catch (Exception e) {
+ throw new IllegalStateException("Could not reach the BaGetter sidecar to push this package", e);
+ }
+ return Response.status(response.statusCode()).build();
+ }
+
+ /**
+ * {@code encodedPath} must already be fully percent-encoded: see {@link SidecarUriUtils
+ * #combine} for why a raw {@code URI.create(baseUrl + path)} concatenation isn't safe here.
+ */
+ private Response proxyGet(String baseUrl, String encodedPath, String fallbackContentType) {
+ HttpRequest request = HttpRequest.newBuilder(SidecarUriUtils.combine(URI.create(baseUrl), encodedPath))
+ .timeout(Duration.ofSeconds(10)).GET().build();
+ HttpResponse<byte[]> response;
+ try {
+ response = httpClient.send(request, BodyHandlers.ofByteArray());
+ } catch (Exception e) {
+ throw new IllegalStateException("Could not reach the BaGetter sidecar at " + encodedPath, e);
+ }
+ if (response.statusCode() == 404) {
+ return Response.status(404).build();
+ }
+ String contentType = response.headers().firstValue("Content-Type").orElse(fallbackContentType);
+ return Response.status(response.statusCode()).entity(response.body()).type(contentType).build();
+ }
+
+ private RepositoryHandle requireRepository(HttpHeaders headers, String domain, String repository) {
+ String token = CodeArtifactTokenExtractor.extractToken(headers);
+ AuthorizationTokenScope scope = service.resolveAuthorizationToken(token, domain)
+ .orElseThrow(() -> new NotAuthorizedException("Basic realm=\"floci-codeartifact\""));
+ String repoId;
+ try {
+ repoId = service.ensureFormatContainerId("nuget", scope.region(), domain, scope.owner(), repository);
+ } catch (AwsException e) {
+ throw new NotFoundException();
+ }
+ String baseUrl = bagetter.ensureReady(repoId, null);
+ // The real GetRepositoryEndpoint value, not just this controller's own path: a NuGet
+ // client's own service-index parser only keeps a resource whose @id parses as an
+ // absolute URI (confirmed directly against NuGet.Protocol's own ServiceIndexResourceV3,
+ // and the official NuGet API docs' own wording, "each resource's @id must be an absolute
+ // URL with an HTTP or HTTPS scheme"), so the index this controller authors has to embed
+ // the same scheme/host (and any configured path prefix) real clients already reach this
+ // controller through, not a bare path with neither.
+ String endpoint = service.getRepositoryEndpoint(scope.region(), domain, scope.owner(), repository, "nuget",
+ null);
+ String publicBaseUrl = endpoint.endsWith("/") ? endpoint.substring(0, endpoint.length() - 1) : endpoint;
+ return new RepositoryHandle(repoId, baseUrl, publicBaseUrl, scope.region(), scope.owner());
+ }
+}
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 ece89136e1..37516b4001 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
@@ -77,7 +77,7 @@ public class CodeArtifactService implements Resettable {
* and the backfill in {@link #ensureFormatContainerId} already handle it, including for
* repositories created before that format's proxy existed.
*/
- private static final List<String> CONTAINER_BACKED_FORMATS = List.of("maven", "npm", "pypi");
+ private static final List<String> CONTAINER_BACKED_FORMATS = List.of("maven", "npm", "pypi", "nuget");
private static final Set<String> ORIGIN_RESTRICTION_VALUES = Set.of("ALLOW", "BLOCK");
/**
* Formats AWS documents as valid for {@code GetPackageVersionReadme} specifically: the
@@ -143,12 +143,12 @@ public class CodeArtifactService implements Resettable {
// currentContainerBackedPackageFence's own javadoc for what this protects against.
private final ConcurrentHashMap<String, Long> containerBackedPackageDeletionFence = new ConcurrentHashMap<>();
private final AtomicLong containerBackedPackageDeletionCounter = new AtomicLong();
- // Maven, npm, and pypi publishes go straight to their own sidecar and never create a
+ // Maven, npm, pypi, and nuget publishes go straight to their own sidecar and never create a
// packageVersions record (see CONTAINER_BACKED_FORMATS), so DescribeDomain's assetSizeBytes
// can't see their bytes by summing packageVersions the way it does for generic. Each format's
// own proxy reports a successful, non-idempotent publish's size here instead, keyed exactly
// like packageOriginConfigs (region/domain/repository/format/namespace/package, no version).
- // In memory only, like the fence above, not through StorageFactory: the Maven/npm/pypi
+ // In memory only, like the fence above, not through StorageFactory: the Maven/npm/pypi/nuget
// content these bytes count lives in the sidecar containers themselves, which do not survive
// a Floci restart either (no volume attached, removed on shutdown; see this service's own
// Known limitations doc). Persisting the count but not the content it describes would leave
@@ -385,7 +385,7 @@ public class CodeArtifactService implements Resettable {
.filter(k -> k.startsWith(originPrefix))
.forEach(k -> packageOriginConfigs.deleteForAccount(owner, k));
// Keyed the same way: without this, a repository recreated under the same name would
- // inherit the previous one's Maven/npm/pypi byte counts into DescribeDomain's total.
+ // inherit the previous one's Maven/npm/pypi/nuget byte counts into DescribeDomain's total.
String ownerScopedOriginPrefix = ownerScopedKey(owner, originPrefix);
containerBackedAssetBytes.keySet().stream()
.filter(k -> k.startsWith(ownerScopedOriginPrefix))
@@ -854,7 +854,7 @@ public class CodeArtifactService implements Resettable {
// it with no versions (its own documented behavior): a stored config, even with zero
// versions behind it, already proves the package exists, and the && below must short-circuit
// on originConfig.isEmpty() before packageExistsViaVersionsOrSidecar ever runs. For a
- // container-backed format that call would otherwise start the real Maven/npm/pypi sidecar
+ // container-backed format that call would otherwise start the real Maven/npm/pypi/nuget sidecar
// container (its own ensureReady lazily provisions one) just to answer a DescribePackage for
// a package nothing was ever actually published to.
if (originConfig.isEmpty()
@@ -1038,10 +1038,11 @@ public class CodeArtifactService implements Resettable {
/**
* Each sidecar's delete capability differs enough that there is no honest shared implementation:
- * Verdaccio and Reposilite remove a whole package in one call, while pypiserver's own management
- * endpoint only deletes one exact name/version pair per call, so {@code PypiserverSidecarClient}
- * lists every file first and removes each one in turn; all three are confirmed against live
- * containers. {@link RepositorySidecarManager#deletePackage}'s default still throws
+ * Verdaccio and Reposilite remove a whole package in one call, while pypiserver's and BaGetter's
+ * own management endpoints only delete one exact name/version pair per call, so {@code
+ * PypiserverSidecarClient} and {@code BagetterSidecarClient} each list every file or version first
+ * and remove each one in turn; all four are confirmed against live containers.
+ * {@link RepositorySidecarManager#deletePackage}'s default still throws
* {@link UnsupportedOperationException} for a sidecar that genuinely cannot do this, rather than
* silently leaving the package in place or claiming a success it cannot back up; that becomes a
* real, if deliberately blunt, AWS error here instead of an unhandled 500.
@@ -1109,8 +1110,8 @@ public class CodeArtifactService implements Resettable {
}
String owner = effectiveOwner(domainOwner);
- // Maven, npm, and pypi are metadata-free passthroughs straight to their own sidecar
- // (ReposiliteSidecarClient/VerdaccioSidecarClient/PypiserverSidecarClient): publish for
+ // Maven, npm, pypi, and nuget are metadata-free passthroughs straight to their own sidecar
+ // (ReposiliteSidecarClient/VerdaccioSidecarClient/PypiserverSidecarClient/BagetterSidecarClient): publish for
// these formats never goes through publishPackageVersion (it only ever accepts "generic"),
// so packageVersions can never hold a record for them. Bridge straight to the sidecar
// that actually has the asset instead of always 404ing here.
@@ -1179,7 +1180,7 @@ public class CodeArtifactService implements Resettable {
* identical coordinates) from ever synthesizing the same revision.
*
* <p>Shared by {@link #getContainerBackedPackageVersionAsset}, {@link
- * #describeContainerBackedPackageVersion}, and {@link #getPackageVersionReadme}'s npm/pypi
+ * #describeContainerBackedPackageVersion}, and {@link #getPackageVersionReadme}'s npm/pypi/nuget
* answers, so a client calling any two of them for the same version coordinate always sees one
* consistent revision rather than independently-computed stand-ins that happen to differ.
* Doesn't change when a new asset is published to the version afterward, unlike a real stored
@@ -1217,10 +1218,11 @@ public class CodeArtifactService implements Resettable {
* action); see {@link #recordPublishedPackageVersionReadme}, the only place either is ever
* captured.
*
- * <p>nuget is accepted, since AWS documents it as valid, but never finds anything: nothing in
- * Floci today implements the real NuGet wire protocol, so no nuget package version can ever
- * actually exist here. This 404s for it exactly the same way as any npm/pypi version nothing
- * ever published, rather than special-casing a format with no data behind it either way.
+ * <p>nuget differs from both: BaGetter keeps every version's own readme (and nuspec {@code
+ * description} fallback) addressable by exact version for as long as that version exists
+ * (confirmed directly against its package content controller), so this answers nuget live
+ * against the sidecar, through {@link RepositorySidecarManager#fetchPackageVersionReadme},
+ * rather than from a record captured at publish time the way npm and pypi need to.
*/
public PackageVersionReadmeResult getPackageVersionReadme(String region, String domain, String domainOwner,
String repository, String format, String namespace, String packageName, String version) {
@@ -1231,25 +1233,31 @@ public class CodeArtifactService implements Resettable {
requireNonBlank(repository, "repository");
requireNonBlank(packageName, "package");
requireNonBlank(version, "packageVersion");
- validatePackageToken("package", packageName);
validatePackageToken("packageVersion", version);
- if (namespace != null) {
- validatePackageToken("namespace", namespace);
- }
+ // Found by Qodo review: this used to only check namespace's token syntax, never whether
+ // pypi or nuget (neither of which has one) was even given one; a caller-supplied
+ // namespace for an existing pypi/nuget version could resolve a readme under a coordinate
+ // that format can't actually have, instead of a ValidationException.
+ validatePackageCoordinates(format, namespace, packageName, "reading the readme of");
String owner = effectiveOwner(domainOwner);
requireRepository(owner, repositoryKey(region, domain, repository), repository);
- // npm, pypi, and nuget alike: none of the three can be answered live (see this method's
- // own javadoc), so a version that was never captured here means it was never published,
- // including every nuget version, since nothing ever publishes one. Checked before
- // ensureFormatContainerId below so a nuget request, which can only ever 404, never mints
- // and persists a sidecar container id for a format with no sidecar at all.
+ String repoId = ensureFormatContainerId(format, region, domain, owner, repository);
+ String revision = synthesizeContainerBackedRevision(repoId, namespace, packageName, version);
+ if ("nuget".equals(format)) {
+ String readme = sidecarRegistry.forFormat(format)
+ .flatMap(manager -> manager.fetchPackageVersionReadme(repoId, domain, repository, namespace,
+ packageName, version))
+ .orElseThrow(() -> notFound("Package version '" + version + "' of package '" + packageName
+ + "' was not found.", version, "package-version"));
+ return new PackageVersionReadmeResult(format, namespace, packageName, version, revision, readme);
+ }
+ // npm and pypi: neither can be answered live (see this method's own javadoc), so a
+ // version that was never captured here means it was never published.
String key = packageVersionKey(region, domain, repository, format, namespace,
accountingPackageName(format, packageName), version);
String readme = Optional.ofNullable(packageVersionReadmes.get(ownerScopedKey(owner, key)))
.orElseThrow(() -> notFound("Package version '" + version + "' of package '" + packageName
+ "' was not found.", version, "package-version"));
- String repoId = ensureFormatContainerId(format, region, domain, owner, repository);
- String revision = synthesizeContainerBackedRevision(repoId, namespace, packageName, version);
return new PackageVersionReadmeResult(format, namespace, packageName, version, revision, readme);
}
@@ -1474,7 +1482,7 @@ public class CodeArtifactService implements Resettable {
/**
* Sums every stored {@link PackageAsset}'s size across the domain's repositories, plus
- * whatever each repository's Maven/npm/pypi proxy has reported through
+ * whatever each repository's Maven/npm/pypi/nuget proxy has reported through
* {@link #recordContainerBackedAssetBytes}, for {@code DescribeDomain}'s {@code
* assetSizeBytes}.
*
@@ -1541,9 +1549,14 @@ public class CodeArtifactService implements Resettable {
/**
* Adds {@code bytes} to the running total CodeArtifact's own storage tracks for one
- * Maven/npm/pypi package, independent of whatever that sidecar could report back later: none
- * of the three expose an enumeration cheap enough for {@link #assetSizeBytesForDomain} to
- * query live (the same reason {@link #describePackageVersion} can't bridge to them either).
+ * Maven/npm/pypi/nuget package, independent of whatever that sidecar could report back later:
+ * Maven, npm, and pypi expose no enumeration cheap enough for {@link #assetSizeBytesForDomain}
+ * to query live (the same reason {@link #describePackageVersion} can't bridge to any of the
+ * three either). nuget's BaGetter sidecar actually could answer a per-package byte total cheaply
+ * through its own flat container listing, but this stays the uniform mechanism across every
+ * container-backed format rather than special-casing the one sidecar that happens to support
+ * it, the same reasoning {@link #deleteContainerBackedPackageIfPresent} already applies by
+ * always asking the sidecar instead of trusting a stored shortcut.
* Called by each format's own proxy only once it has already decided, in its own terms, that a
* real write just happened: never for an idempotent short-circuit (nothing new was written) or
* a rejected conflict (Reposilite's {@code redeployment: false}, Verdaccio's native reject, or
@@ -1676,7 +1689,16 @@ public class CodeArtifactService implements Resettable {
* that mismatch is pre-existing and unrelated to the byte accounting this method guards.
*/
private static String accountingPackageName(String format, String packageName) {
- return "pypi".equals(format) ? packageName.toLowerCase(Locale.ROOT).replaceAll("[-_.]+", "-") : packageName;
+ if ("pypi".equals(format)) {
+ return packageName.toLowerCase(Locale.ROOT).replaceAll("[-_.]+", "-");
+ }
+ // AWS's own NuGet normalization doc: "CodeArtifact normalizes NuGet package names by
+ // converting all letters to lowercase," with no further collapsing the way pypi's PEP 503
+ // normalization does, so this never needs anything beyond a case change.
+ if ("nuget".equals(format)) {
+ return packageName.toLowerCase(Locale.ROOT);
+ }
+ return packageName;
}
private int domainCountForAccount(String owner, String region) {
@@ -1835,8 +1857,8 @@ public class CodeArtifactService implements Resettable {
if (("maven".equals(format) || "generic".equals(format)) && (namespace == null || namespace.isBlank())) {
throw validation("namespace is required when " + action + " a " + format + " package.");
}
- if ("pypi".equals(format) && namespace != null) {
- throw validation("pypi packages do not have a namespace.");
+ if (("pypi".equals(format) || "nuget".equals(format)) && namespace != null) {
+ throw validation(format + " packages do not have a namespace.");
}
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/NuGetVersions.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/NuGetVersions.java
new file mode 100644
index 0000000000..099655dc0c
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/NuGetVersions.java
@@ -0,0 +1,70 @@
+package io.github.hectorvent.floci.services.codeartifact;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+
+/**
+ * NuGet's own version normalization, exactly as AWS's CodeArtifact User Guide documents it for
+ * the {@code nuget} format ("NuGet package name, version, and asset name normalization"):
+ * leading zeroes removed from each numeric segment, a trailing zero fourth segment omitted, and
+ * SemVer 2.0.0 build metadata removed. Used to match a caller-supplied version against the
+ * already-normalized, lowercased version strings BaGetter itself returns (its own package content
+ * controller normalizes the same way, via {@code NuGetVersion.ToNormalizedString()}), not to
+ * reimplement NuGet's full version model: a version this cannot parse as a plain dotted-numeric
+ * release, with an optional prerelease label, is left exactly as given rather than guessed at,
+ * so it simply fails to match anything BaGetter lists instead of silently normalizing it wrong.
+ *
+ * <p>Always pads a short release up to at least three segments (AWS's own worked example, "1.00
+ * is treated as 1.0", doesn't; real {@code NuGetVersion.ToNormalizedString()} does, always
+ * emitting at least Major.Minor.Patch), since matching what BaGetter's version list actually
+ * contains is the entire point here, not reproducing AWS's own doc text byte-for-byte.
+ */
+final class NuGetVersions {
+
+ private NuGetVersions() {
+ }
+
+ static String normalize(String version) {
+ String withoutMetadata = version.split("\\+", 2)[0];
+ int dash = withoutMetadata.indexOf('-');
+ String release = dash < 0 ? withoutMetadata : withoutMetadata.substring(0, dash);
+ String prerelease = dash < 0 ? null : withoutMetadata.substring(dash + 1);
+
+ // -1: a plain split() silently drops a trailing empty string, so "1.0.0." (a real
+ // version has no empty segment) would otherwise normalize identically to "1.0.0" and
+ // match a genuinely published version under a caller-supplied, malformed one.
+ String[] rawSegments = release.split("\\.", -1);
+ List<String> segments = new ArrayList<>();
+ for (String rawSegment : rawSegments) {
+ if (!rawSegment.matches("[0-9]+")) {
+ // Not a plain numeric segment (or not numeric at all, or empty, see above): give
+ // up on normalizing and let the caller's own text through unchanged, so this only
+ // ever actively changes a string it is sure it understood correctly.
+ return version;
+ }
+ try {
+ segments.add(String.valueOf(Long.parseLong(rawSegment)));
+ } catch (NumberFormatException e) {
+ // A segment with more digits than fit in a long: NuGetVersion itself has no such
+ // limit, but nothing this talks to needs one either, so this is treated the same
+ // as any other segment it isn't confident it understood correctly.
+ return version;
+ }
+ }
+ while (segments.size() < 3) {
+ segments.add("0");
+ }
+ if (segments.size() > 4) {
+ return version;
+ }
+ if (segments.size() == 4 && "0".equals(segments.get(3))) {
+ segments.remove(3);
+ }
+ String normalized = String.join(".", segments);
+ if (prerelease != null && !prerelease.isEmpty()) {
+ normalized = normalized + "-" + prerelease;
+ }
+ return normalized.toLowerCase(Locale.ROOT);
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/NuspecXml.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/NuspecXml.java
new file mode 100644
index 0000000000..8888a996ed
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/NuspecXml.java
@@ -0,0 +1,122 @@
+package io.github.hectorvent.floci.services.codeartifact;
+
+import io.github.hectorvent.floci.core.common.XmlParser;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.charset.StandardCharsets;
+import java.util.Locale;
+import java.util.Optional;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+/**
+ * Pulls {@code id}, {@code version}, and {@code description} out of one {@code .nuspec} file's
+ * XML, the three elements every valid nuspec's {@code metadata} must declare, using the shared,
+ * hardened {@link XmlParser#parseDocument}. A real nuspec declares its elements under a default
+ * {@code xmlns} (the exact URI varies by schema version) and never prefixes them, so {@link
+ * Document#getElementsByTagName} still matches on the literal tag text even though that parser
+ * is namespace-aware: an unprefixed element's qualified name is just its local name.
+ */
+final class NuspecXml {
+
+ // A real nuspec is at most a few KB; this is generous headroom, not a realistic size, so a
+ // crafted entry that decompresses past it is rejected outright rather than exhausting the
+ // heap on a request that already passed the overall upload size check (that check measures
+ // the compressed request body, not what a ZIP entry inside it can expand to).
+ private static final int MAX_NUSPEC_BYTES = 1024 * 1024;
+
+ private NuspecXml() {
+ }
+
+ record Metadata(String id, String version, Optional<String> description) {
+ }
+
+ /**
+ * {@code content} is the raw bytes of a {@code .nuspec} file taken from inside a caller-supplied
+ * {@code .nupkg} upload, so this hardens the parser against XXE the same way any other
+ * caller-controlled XML parsed in this codebase must: no DOCTYPE at all ({@code
+ * disallow-doctype-decl}), which also rules out external entities and external DTDs without
+ * needing to disable each individually.
+ */
+ static Metadata parse(byte[] content) {
+ Document document;
+ try {
+ document = XmlParser.parseDocument(new String(content, StandardCharsets.UTF_8));
+ } catch (Exception e) {
+ throw new IllegalStateException("Could not parse the uploaded package's .nuspec file.", e);
+ }
+ Element root = document.getDocumentElement();
+ NodeList metadataNodes = root.getElementsByTagName("metadata");
+ if (metadataNodes.getLength() == 0) {
+ throw new IllegalStateException("nuspec has no <metadata> element.");
+ }
+ Element metadata = (Element) metadataNodes.item(0);
+ String id = requireText(metadata, "id");
+ String version = requireText(metadata, "version");
+ Optional<String> description = optionalText(metadata, "description");
+ return new Metadata(id, version, description);
+ }
+
+ private static String requireText(Element metadata, String tagName) {
+ return optionalText(metadata, tagName)
+ .orElseThrow(() -> new IllegalStateException("nuspec is missing a <" + tagName + "> element."));
+ }
+
+ /**
+ * A {@code .nupkg} is a plain ZIP archive (not an OPC package; {@code NuGet.Packaging}'s own
+ * reader finds the manifest by extension, not by any OPC relationship), so this scans its
+ * entries with the JDK's own {@link ZipInputStream} for the one whose name both ends in
+ * {@code .nuspec} and has no {@code /} in it: the manifest always sits at the archive root,
+ * never nested, which keeps this from matching an unrelated {@code .nuspec}-suffixed entry a
+ * crafted upload could otherwise plant inside a subdirectory.
+ */
+ static Metadata extractFromNupkg(byte[] nupkgBytes) {
+ try (ZipInputStream zip = new ZipInputStream(new ByteArrayInputStream(nupkgBytes))) {
+ ZipEntry entry;
+ while ((entry = zip.getNextEntry()) != null) {
+ String name = entry.getName();
+ if (!entry.isDirectory() && name.toLowerCase(Locale.ROOT).endsWith(".nuspec") && !name.contains("/")) {
+ return parse(readBounded(zip));
+ }
+ }
+ } catch (IOException e) {
+ throw new UncheckedIOException("Could not read the uploaded package as a .nupkg archive.", e);
+ }
+ throw new IllegalStateException("The uploaded package has no .nuspec manifest at its root.");
+ }
+
+ /**
+ * Reads the current entry of {@code zip} up to {@link #MAX_NUSPEC_BYTES}, throwing as soon as
+ * that limit is crossed rather than after buffering the whole thing: a highly-compressed
+ * entry can decompress to far more than the request's own, already-checked, compressed byte
+ * count, so the bound has to apply to the decompressed stream itself, not just the upload.
+ */
+ private static byte[] readBounded(ZipInputStream zip) throws IOException {
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ byte[] chunk = new byte[8192];
+ int read;
+ while ((read = zip.read(chunk)) != -1) {
+ if (buffer.size() + read > MAX_NUSPEC_BYTES) {
+ throw new IllegalStateException(
+ "The uploaded package's .nuspec manifest exceeds " + MAX_NUSPEC_BYTES + " bytes decompressed.");
+ }
+ buffer.write(chunk, 0, read);
+ }
+ return buffer.toByteArray();
+ }
+
+ private static Optional<String> optionalText(Element metadata, String tagName) {
+ NodeList nodes = metadata.getElementsByTagName(tagName);
+ if (nodes.getLength() == 0) {
+ return Optional.empty();
+ }
+ String text = nodes.item(0).getTextContent();
+ return text == null || text.isBlank() ? Optional.empty() : Optional.of(text.trim());
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/RepositorySidecarManager.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/RepositorySidecarManager.java
index 0e85378386..240afc62fe 100644
--- a/src/main/java/io/github/hectorvent/floci/services/codeartifact/RepositorySidecarManager.java
+++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/RepositorySidecarManager.java
@@ -91,4 +91,26 @@ public interface RepositorySidecarManager {
String packageName) {
throw new UnsupportedOperationException(format() + " packages cannot be deleted through this sidecar");
}
+
+ /**
+ * Live readme/descriptive-text lookup for one package version, for a sidecar capable of
+ * answering this for a specific, possibly historical version after the fact. {@code empty()}
+ * by default: Verdaccio (npm) only ever exposes the most-recently-published version's readme,
+ * and pypiserver exposes none at all, so neither can implement this and {@code
+ * CodeArtifactService#getPackageVersionReadme} instead answers npm and pypi from a record
+ * captured at publish time ({@link CodeArtifactService#recordPublishedPackageVersionReadme}).
+ * {@code BagetterSidecarClient} overrides this: BaGetter keeps every version's own readme
+ * addressable by exact version for as long as that version exists, so nuget needs no such
+ * capture step.
+ *
+ * <p>Returning {@code empty()} here is read by the caller as "this sidecar cannot answer
+ * live", not "this version has no readme": an override distinguishes the two with {@code
+ * Optional.of("")} for a published version with nothing to show versus {@code empty()} for a
+ * version that was never published, the same convention {@link #fetchPackageVersionAsset}
+ * already uses for "found, but empty" versus "not found".
+ */
+ default Optional<String> fetchPackageVersionReadme(String repositoryContainerId, String domain, String repository,
+ String namespace, String packageName, String version) {
+ return Optional.empty();
+ }
}
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index 684028830d..5d8a6109ef 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -999,6 +999,7 @@ floci:
maven-image: "dzikoysk/reposilite:3.6.3"
npm-image: "verdaccio/verdaccio:6.10.4"
pypi-image: "pypiserver/pypiserver:v2.4.2"
+ nuget-image: "bagetter/bagetter:1.6.5"
marketplace:
enabled: true
dms:
diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/BagetterSidecarClientTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/BagetterSidecarClientTest.java
new file mode 100644
index 0000000000..d4b34ed4d4
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/BagetterSidecarClientTest.java
@@ -0,0 +1,309 @@
+package io.github.hectorvent.floci.services.codeartifact;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.sun.net.httpserver.HttpServer;
+import io.github.hectorvent.floci.config.EmulatorConfig;
+import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
+import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
+import io.github.hectorvent.floci.core.common.docker.PerKeyContainerPool;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Field;
+import java.net.InetSocketAddress;
+import java.net.http.HttpClient;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+
+/**
+ * Wire-contract tests for {@link BagetterSidecarClient} against a JDK {@code HttpServer} standing in
+ * for a running BaGetter container. The container is registered in the manager's pool directly, so
+ * readiness resolves to the fake server without any Docker daemon.
+ */
+class BagetterSidecarClientTest {
+
+ private final ContainerBuilder containerBuilder = mock(ContainerBuilder.class);
+ private final ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
+ private final EmulatorConfig config = mock(EmulatorConfig.class);
+
+ private HttpServer backend;
+ private String backendUrl;
+
+ @BeforeEach
+ void setUpBackend() throws Exception {
+ backend = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
+ backend.createContext("/health", exchange -> {
+ exchange.sendResponseHeaders(200, -1);
+ exchange.close();
+ });
+ backend.start();
+ backendUrl = "http://127.0.0.1:" + backend.getAddress().getPort();
+ }
+
+ @AfterEach
+ void tearDownBackend() {
+ backend.stop(0);
+ }
+
+ @Test
+ void fetchPackageVersionAssetGetsTheFlatContainerNupkgPath() throws Exception {
+ BagetterSidecarManager manager = manager();
+ seedPooledContainer(manager, "nuget-repo-1", "tracked-bagetter-1", backendUrl);
+ respondWithVersions("demo-pkg", "1.0.0");
+ backend.createContext("/v3/package/demo-pkg/1.0.0/demo-pkg.1.0.0.nupkg", exchange -> {
+ byte[] body = "nupkg bytes".getBytes(StandardCharsets.UTF_8);
+ exchange.sendResponseHeaders(200, body.length);
+ exchange.getResponseBody().write(body);
+ exchange.close();
+ });
+
+ Optional<byte[]> result = client(manager).fetchPackageVersionAsset("nuget-repo-1", "dom", "repo", null,
+ "demo-pkg", "1.0.0", "demo-pkg.1.0.0.nupkg");
+
+ assertTrue(result.isPresent());
+ assertEquals("nupkg bytes", new String(result.get(), StandardCharsets.UTF_8));
+ }
+
+ /**
+ * AWS normalizes NuGet versions (leading zeroes removed): a caller asking for {@code 1.00.0}
+ * must still find the version BaGetter itself lists as {@code 1.0.0}.
+ */
+ @Test
+ void fetchPackageVersionAssetMatchesANonNormalizedRequestedVersion() throws Exception {
+ BagetterSidecarManager manager = manager();
+ seedPooledContainer(manager, "nuget-repo-1", "tracked-bagetter-1", backendUrl);
+ respondWithVersions("demo-pkg", "1.0.0");
+ backend.createContext("/v3/package/demo-pkg/1.0.0/demo-pkg.1.0.0.nupkg", exchange -> {
+ byte[] body = "nupkg bytes".getBytes(StandardCharsets.UTF_8);
+ exchange.sendResponseHeaders(200, body.length);
+ exchange.getResponseBody().write(body);
+ exchange.close();
+ });
+
+ Optional<byte[]> result = client(manager).fetchPackageVersionAsset("nuget-repo-1", "dom", "repo", null,
+ "demo-pkg", "1.00.0", "demo-pkg.1.0.0.nupkg");
+
+ assertTrue(result.isPresent());
+ }
+
+ /**
+ * The asset name must equal the one deterministic name AWS documents for a given package and
+ * version; any other requested name (even a real file that happens to exist under a different
+ * name) is empty.
+ */
+ @Test
+ void fetchPackageVersionAssetReturnsEmptyWhenTheAssetNameDoesNotMatchTheNormalizedForm() throws Exception {
+ BagetterSidecarManager manager = manager();
+ seedPooledContainer(manager, "nuget-repo-1", "tracked-bagetter-1", backendUrl);
+ respondWithVersions("demo-pkg", "1.0.0");
+
+ Optional<byte[]> result = client(manager).fetchPackageVersionAsset("nuget-repo-1", "dom", "repo", null,
+ "demo-pkg", "1.0.0", "demo-pkg.nupkg");
+
+ assertTrue(result.isEmpty());
+ }
+
+ @Test
+ void fetchPackageVersionAssetReturnsEmptyWhenTheVersionWasNeverPublished() throws Exception {
+ BagetterSidecarManager manager = manager();
+ seedPooledContainer(manager, "nuget-repo-1", "tracked-bagetter-1", backendUrl);
+ respondWithVersions("demo-pkg", "1.0.0");
+
+ Optional<byte[]> result = client(manager).fetchPackageVersionAsset("nuget-repo-1", "dom", "repo", null,
+ "demo-pkg", "9.9.9", "demo-pkg.9.9.9.nupkg");
+
+ assertTrue(result.isEmpty());
+ }
+
+ @Test
+ void packageExistsTrueWhenTheFlatContainerListsAtLeastOneVersion() throws Exception {
+ BagetterSidecarManager manager = manager();
+ seedPooledContainer(manager, "nuget-repo-1", "tracked-bagetter-1", backendUrl);
+ respondWithVersions("demo-pkg", "1.0.0");
+
+ assertTrue(client(manager).packageExists("nuget-repo-1", "dom", "repo", null, "demo-pkg"));
+ }
+
+ @Test
+ void packageExistsFalseWhenTheFlatContainerHasNeverHeardOfIt() throws Exception {
+ BagetterSidecarManager manager = manager();
+ seedPooledContainer(manager, "nuget-repo-1", "tracked-bagetter-1", backendUrl);
+ backend.createContext("/v3/package/never-published/index.json", exchange -> {
+ exchange.sendResponseHeaders(404, -1);
+ exchange.close();
+ });
+
+ assertFalse(client(manager).packageExists("nuget-repo-1", "dom", "repo", null, "never-published"));
+ }
+
+ @Test
+ void packageVersionExistsMatchesANonNormalizedRequestedVersion() throws Exception {
+ BagetterSidecarManager manager = manager();
+ seedPooledContainer(manager, "nuget-repo-1", "tracked-bagetter-1", backendUrl);
+ respondWithVersions("demo-pkg", "1.0.0");
+
+ assertTrue(client(manager).packageVersionExists("nuget-repo-1", "dom", "repo", null, "demo-pkg", "1.00.0"));
+ }
+
+ @Test
+ void packageVersionExistsFalseForAVersionThatWasNeverPublished() throws Exception {
+ BagetterSidecarManager manager = manager();
+ seedPooledContainer(manager, "nuget-repo-1", "tracked-bagetter-1", backendUrl);
+ respondWithVersions("demo-pkg", "1.0.0");
+
+ assertFalse(client(manager).packageVersionExists("nuget-repo-1", "dom", "repo", null, "demo-pkg", "9.9.9"));
+ }
+
+ /**
+ * {@code CodeArtifactNuGetController}'s push handler synchronizes on this exact object for a
+ * repository, the same one {@link BagetterSidecarClient#deletePackage} itself takes, so a
+ * push and a delete for the same repository can never interleave; a different repository
+ * must still get its own lock rather than blocking behind an unrelated one.
+ */
+ @Test
+ void lockForReturnsTheSameObjectForOneRepositoryAndDifferentObjectsForDifferentOnes() {
+ BagetterSidecarClient client = client(manager());
+
+ Object repo1Again = client.lockFor("nuget-repo-1");
+
+ assertSame(client.lockFor("nuget-repo-1"), repo1Again);
+ assertNotSame(client.lockFor("nuget-repo-1"), client.lockFor("nuget-repo-2"));
+ }
+
+ @Test
+ void deletePackageDeletesEveryVersionTheFlatContainerLists() throws Exception {
+ BagetterSidecarManager manager = manager();
+ seedPooledContainer(manager, "nuget-repo-1", "tracked-bagetter-1", backendUrl);
+ respondWithVersions("demo-pkg", "1.0.0", "2.0.0");
+ List<String> deletedPaths = new CopyOnWriteArrayList<>();
+ backend.createContext("/api/v2/package/demo-pkg", exchange -> {
+ deletedPaths.add(exchange.getRequestURI().getPath());
+ exchange.sendResponseHeaders(204, -1);
+ exchange.close();
+ });
+
+ client(manager).deletePackage("nuget-repo-1", "dom", "repo", null, "demo-pkg");
+
+ assertEquals(List.of("/api/v2/package/demo-pkg/1.0.0", "/api/v2/package/demo-pkg/2.0.0"),
+ deletedPaths.stream().sorted().toList());
+ }
+
+ @Test
+ void fetchPackageVersionReadmeReturnsTheEmbeddedReadmeFileWhenOneExists() throws Exception {
+ BagetterSidecarManager manager = manager();
+ seedPooledContainer(manager, "nuget-repo-1", "tracked-bagetter-1", backendUrl);
+ respondWithVersions("demo-pkg", "1.0.0");
+ backend.createContext("/v3/package/demo-pkg/1.0.0/readme", exchange -> {
+ byte[] body = "# Real readme".getBytes(StandardCharsets.UTF_8);
+ exchange.sendResponseHeaders(200, body.length);
+ exchange.getResponseBody().write(body);
+ exchange.close();
+ });
+
+ Optional<String> result = client(manager).fetchPackageVersionReadme("nuget-repo-1", "dom", "repo", null,
+ "demo-pkg", "1.0.0");
+
+ assertEquals(Optional.of("# Real readme"), result);
+ }
+
+ @Test
+ void fetchPackageVersionReadmeFallsBackToTheNuspecDescriptionWhenNoReadmeFileIsEmbedded() throws Exception {
+ BagetterSidecarManager manager = manager();
+ seedPooledContainer(manager, "nuget-repo-1", "tracked-bagetter-1", backendUrl);
+ respondWithVersions("demo-pkg", "1.0.0");
+ backend.createContext("/v3/package/demo-pkg/1.0.0/readme", exchange -> {
+ exchange.sendResponseHeaders(404, -1);
+ exchange.close();
+ });
+ String nuspec = "<?xml version=\"1.0\"?><package><metadata><id>demo-pkg</id><version>1.0.0</version>"
+ + "<description>A long description.</description></metadata></package>";
+ backend.createContext("/v3/package/demo-pkg/1.0.0/demo-pkg.nuspec", exchange -> {
+ byte[] body = nuspec.getBytes(StandardCharsets.UTF_8);
+ exchange.sendResponseHeaders(200, body.length);
+ exchange.getResponseBody().write(body);
+ exchange.close();
+ });
+
+ Optional<String> result = client(manager).fetchPackageVersionReadme("nuget-repo-1", "dom", "repo", null,
+ "demo-pkg", "1.0.0");
+
+ assertEquals(Optional.of("A long description."), result);
+ }
+
+ @Test
+ void fetchPackageVersionReadmeReturnsEmptyStringWhenNeitherReadmeNorNuspecAreReachable() throws Exception {
+ BagetterSidecarManager manager = manager();
+ seedPooledContainer(manager, "nuget-repo-1", "tracked-bagetter-1", backendUrl);
+ respondWithVersions("demo-pkg", "1.0.0");
+ backend.createContext("/v3/package/demo-pkg/1.0.0/readme", exchange -> {
+ exchange.sendResponseHeaders(404, -1);
+ exchange.close();
+ });
+ backend.createContext("/v3/package/demo-pkg/1.0.0/demo-pkg.nuspec", exchange -> {
+ exchange.sendResponseHeaders(404, -1);
+ exchange.close();
+ });
+
+ Optional<String> result = client(manager).fetchPackageVersionReadme("nuget-repo-1", "dom", "repo", null,
+ "demo-pkg", "1.0.0");
+
+ assertEquals(Optional.of(""), result);
+ }
+
+ @Test
+ void fetchPackageVersionReadmeReturnsEmptyWhenTheVersionWasNeverPublished() throws Exception {
+ BagetterSidecarManager manager = manager();
+ seedPooledContainer(manager, "nuget-repo-1", "tracked-bagetter-1", backendUrl);
+ respondWithVersions("demo-pkg", "1.0.0");
+
+ Optional<String> result = client(manager).fetchPackageVersionReadme("nuget-repo-1", "dom", "repo", null,
+ "demo-pkg", "9.9.9");
+
+ assertTrue(result.isEmpty());
+ }
+
+ private void respondWithVersions(String packageName, String... versions) {
+ String json = "{\"versions\":[" + String.join(",",
+ java.util.Arrays.stream(versions).map(v -> "\"" + v + "\"").toArray(String[]::new)) + "]}";
+ backend.createContext("/v3/package/" + packageName + "/index.json", exchange -> {
+ byte[] bytes = json.getBytes(StandardCharsets.UTF_8);
+ exchange.getResponseHeaders().set("Content-Type", "application/json");
+ exchange.sendResponseHeaders(200, bytes.length);
+ exchange.getResponseBody().write(bytes);
+ exchange.close();
+ });
+ }
+
+ private BagetterSidecarManager manager() {
+ return new BagetterSidecarManager(containerBuilder, lifecycleManager, config);
+ }
+
+ private BagetterSidecarClient client(BagetterSidecarManager manager) {
+ return new BagetterSidecarClient(manager, new ObjectMapper(), HttpClient.newHttpClient());
+ }
+
+ @SuppressWarnings("unchecked")
+ private static void seedPooledContainer(BagetterSidecarManager manager, String key, String containerId,
+ String url) throws Exception {
+ Field poolField = BagetterSidecarManager.class.getDeclaredField("pool");
+ poolField.setAccessible(true);
+ PerKeyContainerPool pool = (PerKeyContainerPool) poolField.get(manager);
+
+ Field containersField = PerKeyContainerPool.class.getDeclaredField("containers");
+ containersField.setAccessible(true);
+ ConcurrentHashMap<String, PerKeyContainerPool.StartedContainer> containers =
+ (ConcurrentHashMap<String, PerKeyContainerPool.StartedContainer>) containersField.get(pool);
+ containers.put(key, new PerKeyContainerPool.StartedContainer(containerId, url));
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/BagetterSidecarManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/BagetterSidecarManagerTest.java
new file mode 100644
index 0000000000..7f29190f12
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/BagetterSidecarManagerTest.java
@@ -0,0 +1,75 @@
+package io.github.hectorvent.floci.services.codeartifact;
+
+import io.github.hectorvent.floci.config.EmulatorConfig;
+import io.github.hectorvent.floci.core.common.ContainerTeardown;
+import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
+import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
+import io.github.hectorvent.floci.core.common.docker.PerKeyContainerPool;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Field;
+import java.util.concurrent.ConcurrentHashMap;
+
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+
+/**
+ * {@link BagetterSidecarManager#stopManagedContainers()} is a thin delegation to
+ * {@link PerKeyContainerPool#stopAll()}, whose own behavior is covered by {@code
+ * PerKeyContainerPoolTest}. What matters here is the wiring: that the manager implements
+ * {@link ContainerTeardown} (so {@code ContainerTeardowns.stopAll} finds it on reset, nuke, and
+ * shutdown) and that it stops the containers it actually started. A container is seeded straight
+ * into the pool by reflection, so this needs no Docker daemon.
+ */
+class BagetterSidecarManagerTest {
+
+ private final ContainerBuilder containerBuilder = mock(ContainerBuilder.class);
+ private final ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
+ private final EmulatorConfig config = mock(EmulatorConfig.class);
+
+ @Test
+ void implementsContainerTeardownSoStateResetAndNukeCanFindIt() {
+ assertInstanceOf(ContainerTeardown.class, manager());
+ }
+
+ @Test
+ void stopManagedContainersStopsEveryContainerThePoolIsTracking() throws Exception {
+ BagetterSidecarManager manager = manager();
+ seedPooledContainer(manager, "nuget-repo-1", "tracked-bagetter-1", "http://127.0.0.1:1");
+ seedPooledContainer(manager, "nuget-repo-2", "tracked-bagetter-2", "http://127.0.0.1:1");
+
+ manager.stopManagedContainers();
+
+ verify(lifecycleManager).stopAndRemove("tracked-bagetter-1", null);
+ verify(lifecycleManager).stopAndRemove("tracked-bagetter-2", null);
+ }
+
+ @Test
+ void stopManagedContainersIsANoOpWhenNothingWasEverStarted() {
+ BagetterSidecarManager manager = manager();
+
+ manager.stopManagedContainers();
+
+ verifyNoInteractions(lifecycleManager);
+ }
+
+ private BagetterSidecarManager manager() {
+ return new BagetterSidecarManager(containerBuilder, lifecycleManager, config);
+ }
+
+ @SuppressWarnings("unchecked")
+ private static void seedPooledContainer(BagetterSidecarManager manager, String key, String containerId,
+ String url) throws Exception {
+ Field poolField = BagetterSidecarManager.class.getDeclaredField("pool");
+ poolField.setAccessible(true);
+ PerKeyContainerPool pool = (PerKeyContainerPool) poolField.get(manager);
+
+ Field containersField = PerKeyContainerPool.class.getDeclaredField("containers");
+ containersField.setAccessible(true);
+ ConcurrentHashMap<String, PerKeyContainerPool.StartedContainer> containers =
+ (ConcurrentHashMap<String, PerKeyContainerPool.StartedContainer>) containersField.get(pool);
+ containers.put(key, new PerKeyContainerPool.StartedContainer(containerId, url));
+ }
+}
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
new file mode 100644
index 0000000000..e0a9635a4e
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNuGetDockerIntegrationTest.java
@@ -0,0 +1,402 @@
+package io.github.hectorvent.floci.services.codeartifact;
+
+import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
+import io.github.hectorvent.floci.testing.SidecarContainersProfile;
+import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.TestProfile;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Order;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestMethodOrder;
+
+import java.io.ByteArrayOutputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.startsWith;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Runs a real NuGet V3 push/install round trip against a live, per-repository BaGetter container,
+ * started by {@link BagetterSidecarManager} exactly as in production. The pushed {@code .nupkg}
+ * files are hand-built with the JDK's own {@code java.util.zip} (a real nuspec manifest at the
+ * archive root plus, for one test, an embedded readme file), not a captured fixture: what this
+ * class needs to prove is the real container's behavior, not a client library's fidelity to the
+ * wire format.
+ */
+@QuarkusTest
+@TestProfile(SidecarContainersProfile.class)
+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
+class CodeArtifactNuGetDockerIntegrationTest {
+
+ private static final String AUTH = "AWS4-HMAC-SHA256 Credential=AKID/20260904/us-east-1/codeartifact/aws4_request";
+ private static final String DOMAIN = "nuget-sidecar-domain";
+ private static final String REPO = "nuget-sidecar-repo";
+ private static final String PACKAGE_ID = "Demo.Package";
+ private static final String LOWER_ID = "demo.package";
+ private static final String VERSION = "1.0.0";
+
+ private static String bearerToken;
+ // A fresh ZipEntry carries the current wall-clock time in its DOS-format timestamp field
+ // whenever one isn't set explicitly, so two separate buildNupkg(...) calls with otherwise
+ // identical inputs are never byte-identical; every "same bytes" assertion below reuses this
+ // one build from the original push rather than reconstructing it.
+ private static byte[] originalNupkg;
+
+ @BeforeAll
+ static void setUp() {
+ SidecarContainersProfile.requireDockerAndImage("floci.services.codeartifact.nuget-image");
+ RestAssuredJsonUtils.configureAwsContentTypes();
+ }
+
+ @Test
+ @Order(0)
+ void createDomainRepositoryAndAuthorizationToken() {
+ given().contentType("application/json").header("Authorization", AUTH).body("{}")
+ .post("/v1/domain?domain=" + DOMAIN)
+ .then().statusCode(200);
+ given().contentType("application/json").header("Authorization", AUTH).body("{}")
+ .post("/v1/repository?domain=" + DOMAIN + "&repository=" + REPO)
+ .then().statusCode(200);
+
+ bearerToken = given().header("Authorization", AUTH)
+ .post("/v1/authorization-token?domain=" + DOMAIN)
+ .then().statusCode(200)
+ .extract().jsonPath().getString("authorizationToken");
+ }
+
+ @Test
+ @Order(1)
+ void pushThenInstallRoundTripsTheExactBytesThroughTheFlatContainer() {
+ originalNupkg = buildNupkg(PACKAGE_ID, VERSION, "A description.", null, null);
+ long assetSizeBytesBeforePush = describeDomainAssetSizeBytes();
+
+ given().header("Authorization", "Bearer " + bearerToken)
+ .contentType("multipart/form-data; boundary=boundary123")
+ .body(pushBody("boundary123", originalNupkg))
+ .put("/codeartifact/nuget/" + DOMAIN + "/" + REPO + "/api/v2/package")
+ .then().statusCode(201);
+
+ // assetSizeBytes used to have no bridge for nuget at all; this is the real round trip
+ // proving a real push now counts, the same way the other three formats already do.
+ assertEquals(assetSizeBytesBeforePush + originalNupkg.length, describeDomainAssetSizeBytes());
+
+ // Found by Qodo review: the index used to advertise bare paths instead of absolute
+ // URLs, which a real NuGet client's own service-index parser silently drops (it only
+ // keeps a resource whose @id parses as an absolute URI). Both resources must be rooted
+ // at the exact endpoint GetRepositoryEndpoint itself returns.
+ String repositoryEndpoint = given().header("Authorization", AUTH)
+ .get("/v1/repository/endpoint?domain=" + DOMAIN + "&repository=" + REPO + "&format=nuget")
+ .then().statusCode(200)
+ .extract().jsonPath().getString("repositoryEndpoint");
+ String endpointNoTrailingSlash = repositoryEndpoint.substring(0, repositoryEndpoint.length() - 1);
+ given().header("Authorization", "Bearer " + bearerToken)
+ .get("/codeartifact/nuget/" + DOMAIN + "/" + REPO + "/v3/index.json")
+ .then().statusCode(200)
+ .body(containsString(endpointNoTrailingSlash + "/v3/package/"))
+ .body(containsString(endpointNoTrailingSlash + "/api/v2/package"));
+
+ given().header("Authorization", "Bearer " + bearerToken)
+ .get("/codeartifact/nuget/" + DOMAIN + "/" + REPO + "/v3/package/" + LOWER_ID + "/index.json")
+ .then().statusCode(200)
+ .body("versions", org.hamcrest.Matchers.hasItem(VERSION));
+
+ byte[] fetchedNupkg = given().header("Authorization", "Bearer " + bearerToken)
+ .get("/codeartifact/nuget/" + DOMAIN + "/" + REPO + "/v3/package/" + LOWER_ID + "/" + VERSION + "/"
+ + LOWER_ID + "." + VERSION + ".nupkg")
+ .then().statusCode(200)
+ .extract().asByteArray();
+ assertArrayEquals(originalNupkg, fetchedNupkg);
+
+ given().header("Authorization", "Bearer " + bearerToken)
+ .get("/codeartifact/nuget/" + DOMAIN + "/" + REPO + "/v3/package/" + LOWER_ID + "/" + VERSION + "/"
+ + LOWER_ID + ".nuspec")
+ .then().statusCode(200)
+ .body(containsString(PACKAGE_ID));
+
+ given().header("Authorization", AUTH)
+ .get("/v1/package?domain=" + DOMAIN + "&repository=" + REPO + "&format=nuget&package=" + PACKAGE_ID)
+ .then().statusCode(200).body("package.name", equalTo(PACKAGE_ID))
+ .body("package.originConfiguration.restrictions.publish", equalTo("ALLOW"));
+ }
+
+ /**
+ * The real-world gap this closes: {@code GetPackageVersionAsset} used to 404 for every
+ * nuget-published file since there was no nuget sidecar at all. Proves the JSON API bridges
+ * to the same file {@link #pushThenInstallRoundTripsTheExactBytesThroughTheFlatContainer}
+ * already confirmed is really there, through the real sidecar, not a mock. No namespace:
+ * NuGet packages don't have one.
+ */
+ @Test
+ @Order(2)
+ void getPackageVersionAssetBridgesToTheRealBagetterSidecar() {
+ String assetName = LOWER_ID + "." + VERSION + ".nupkg";
+ byte[] fetched = given().header("Authorization", AUTH)
+ .get("/v1/package/version/asset?domain=" + DOMAIN + "&repository=" + REPO + "&format=nuget"
+ + "&package=" + PACKAGE_ID + "&version=" + VERSION + "&asset=" + assetName)
+ .then().statusCode(200)
+ .header("X-AssetName", equalTo(assetName))
+ .extract().asByteArray();
+ assertArrayEquals(originalNupkg, fetched);
+ }
+
+ @Test
+ @Order(3)
+ void getPackageVersionAssetReturns404ForAnAssetNameThatIsNotTheNormalizedForm() {
+ given().header("Authorization", AUTH)
+ .get("/v1/package/version/asset?domain=" + DOMAIN + "&repository=" + REPO + "&format=nuget"
+ + "&package=" + PACKAGE_ID + "&version=" + VERSION + "&asset=" + PACKAGE_ID + ".nupkg")
+ .then().statusCode(404);
+ }
+
+ /**
+ * The real-world gap this closes: {@code DescribePackageVersion} used to 404 for every
+ * nuget-uploaded version the same reason {@code GetPackageVersionAsset} did before it was
+ * bridged. Confirms the bridge reads the real BaGetter container's own flat-container
+ * version listing (not a mock) to confirm the version exists, and 404s for one that was
+ * never pushed.
+ */
+ @Test
+ @Order(4)
+ void describePackageVersionBridgesToTheRealBagetterSidecar() {
+ given().header("Authorization", AUTH)
+ .get("/v1/package/version?domain=" + DOMAIN + "&repository=" + REPO + "&format=nuget"
+ + "&package=" + PACKAGE_ID + "&version=" + VERSION)
+ .then().statusCode(200)
+ .body("packageVersion.status", equalTo("Published"))
+ .body("packageVersion.packageName", equalTo(PACKAGE_ID))
+ .body("packageVersion.version", equalTo(VERSION));
+
+ given().header("Authorization", AUTH)
+ .get("/v1/package/version?domain=" + DOMAIN + "&repository=" + REPO + "&format=nuget"
+ + "&package=" + PACKAGE_ID + "&version=9.9.9")
+ .then().statusCode(404);
+ }
+
+ /**
+ * No readme file was embedded in the pushed package, so this must fall back to the nuspec's
+ * own {@code <description>} rather than 404ing for a version that genuinely exists.
+ */
+ @Test
+ @Order(5)
+ void getPackageVersionReadmeFallsBackToTheNuspecDescription() {
+ given().header("Authorization", AUTH)
+ .get("/v1/package/version/readme?domain=" + DOMAIN + "&repository=" + REPO + "&format=nuget&package="
+ + PACKAGE_ID + "&version=" + VERSION)
+ .then().statusCode(200)
+ .body("readme", equalTo("A description."))
+ .body("package", equalTo(PACKAGE_ID))
+ .body("version", equalTo(VERSION));
+ }
+
+ /**
+ * AWS's own documented idempotent-republish exception ("Overwriting package assets"): a
+ * byte-identical re-push succeeds without BaGetter's own {@code AllowPackageOverwrites: false}
+ * ever rejecting it, since Floci's proxy short-circuits before forwarding anything.
+ */
+ @Test
+ @Order(6)
+ void repushingTheExactSameBytesSucceedsIdempotently() {
+ long assetSizeBytesBeforeRepush = describeDomainAssetSizeBytes();
+
+ given().header("Authorization", "Bearer " + bearerToken)
+ .contentType("multipart/form-data; boundary=boundary123")
+ .body(pushBody("boundary123", originalNupkg))
+ .put("/codeartifact/nuget/" + DOMAIN + "/" + REPO + "/api/v2/package")
+ .then().statusCode(200);
+
+ // The idempotent-match short-circuit never forwards to BaGetter, so no new bytes were
+ // written; assetSizeBytes must not count this push a second time.
+ assertEquals(assetSizeBytesBeforeRepush, describeDomainAssetSizeBytes());
+ }
+
+ @Test
+ @Order(7)
+ void repushingTheSameCoordinatesWithDifferentContentIsRejectedAndTheOriginalSurvives() {
+ byte[] differentNupkg = buildNupkg(PACKAGE_ID, VERSION, "A different description.", null, null);
+
+ given().header("Authorization", "Bearer " + bearerToken)
+ .contentType("multipart/form-data; boundary=boundary123")
+ .body(pushBody("boundary123", differentNupkg))
+ .put("/codeartifact/nuget/" + DOMAIN + "/" + REPO + "/api/v2/package")
+ .then().statusCode(409);
+
+ byte[] fetched = given().header("Authorization", "Bearer " + bearerToken)
+ .get("/codeartifact/nuget/" + DOMAIN + "/" + REPO + "/v3/package/" + LOWER_ID + "/" + VERSION + "/"
+ + LOWER_ID + "." + VERSION + ".nupkg")
+ .then().statusCode(200)
+ .extract().asByteArray();
+ assertArrayEquals(originalNupkg, fetched);
+ }
+
+ @Test
+ @Order(8)
+ void missingPackageAndMissingRepositoryAreNotFound() {
+ given().header("Authorization", "Bearer " + bearerToken)
+ .get("/codeartifact/nuget/" + DOMAIN + "/" + REPO + "/v3/package/does-not-exist/index.json")
+ .then().statusCode(404);
+
+ given().header("Authorization", "Bearer " + bearerToken)
+ .get("/codeartifact/nuget/" + DOMAIN + "/no-such-repo/v3/index.json")
+ .then().statusCode(404);
+
+ given().header("Authorization", AUTH)
+ .get("/v1/package?domain=" + DOMAIN + "&repository=" + REPO + "&format=nuget&package=does-not-exist")
+ .then().statusCode(404).body("__type", equalTo("ResourceNotFoundException"));
+ }
+
+ @Test
+ @Order(9)
+ void repositoriesAreIsolatedFromEachOther() {
+ given().contentType("application/json").header("Authorization", AUTH).body("{}")
+ .post("/v1/repository?domain=" + DOMAIN + "&repository=other-nuget-repo")
+ .then().statusCode(200);
+
+ given().header("Authorization", "Bearer " + bearerToken)
+ .get("/codeartifact/nuget/" + DOMAIN + "/other-nuget-repo/v3/package/" + LOWER_ID + "/index.json")
+ .then().statusCode(404);
+ }
+
+ @Test
+ @Order(10)
+ void missingOrWrongDomainTokensAreUnauthorized() {
+ // Challenges as Basic, not Bearer: AWS's own NuGet CLI configuration doc has the client
+ // authenticate with Basic (username=aws, password=<token>), so this is the scheme a
+ // retry would actually honor.
+ given().get("/codeartifact/nuget/" + DOMAIN + "/" + REPO + "/v3/index.json")
+ .then().statusCode(401)
+ .header("WWW-Authenticate", startsWith("Basic"));
+
+ given().header("Authorization", "Bearer not-a-real-token")
+ .get("/codeartifact/nuget/" + DOMAIN + "/" + REPO + "/v3/index.json")
+ .then().statusCode(401);
+
+ given().contentType("application/json").header("Authorization", AUTH).body("{}")
+ .post("/v1/domain?domain=other-nuget-token-domain")
+ .then().statusCode(200);
+ String otherDomainToken = given().header("Authorization", AUTH)
+ .post("/v1/authorization-token?domain=other-nuget-token-domain")
+ .then().statusCode(200)
+ .extract().jsonPath().getString("authorizationToken");
+
+ given().header("Authorization", "Bearer " + otherDomainToken)
+ .get("/codeartifact/nuget/" + DOMAIN + "/" + REPO + "/v3/index.json")
+ .then().statusCode(401);
+ }
+
+ /**
+ * A separate package, with a {@code <readme>} element and its referenced file both packed
+ * in: the readme file must win over the description, proving the two-tier fallback really
+ * prefers the embedded file when one exists rather than always falling back.
+ */
+ @Test
+ @Order(11)
+ void getPackageVersionReadmePrefersTheEmbeddedReadmeFileOverTheDescription() {
+ String packageId = "Readme.Spike";
+ String readmeText = "# Real Readme\n\nPacked alongside the nuspec.";
+ byte[] nupkg = buildNupkg(packageId, "1.0.0", "A description nobody should see.", "README.md", readmeText);
+
+ given().header("Authorization", "Bearer " + bearerToken)
+ .contentType("multipart/form-data; boundary=boundary123")
+ .body(pushBody("boundary123", nupkg))
+ .put("/codeartifact/nuget/" + DOMAIN + "/" + REPO + "/api/v2/package")
+ .then().statusCode(201);
+
+ given().header("Authorization", AUTH)
+ .get("/v1/package/version/readme?domain=" + DOMAIN + "&repository=" + REPO + "&format=nuget&package="
+ + packageId + "&version=1.0.0")
+ .then().statusCode(200)
+ .body("readme", equalTo(readmeText));
+ }
+
+ @Test
+ @Order(12)
+ void deletePackageRemovesTheFileFromTheRealBagetterSidecarAndAllowsARepublish() {
+ given().header("Authorization", AUTH)
+ .delete("/v1/package?domain=" + DOMAIN + "&repository=" + REPO + "&format=nuget&package="
+ + PACKAGE_ID)
+ .then().statusCode(200).body("deletedPackage.package", equalTo(PACKAGE_ID));
+
+ given().header("Authorization", "Bearer " + bearerToken)
+ .get("/codeartifact/nuget/" + DOMAIN + "/" + REPO + "/v3/package/" + LOWER_ID + "/index.json")
+ .then().statusCode(404);
+
+ given().header("Authorization", AUTH)
+ .get("/v1/package?domain=" + DOMAIN + "&repository=" + REPO + "&format=nuget&package=" + PACKAGE_ID)
+ .then().statusCode(404).body("__type", equalTo("ResourceNotFoundException"));
+
+ // BaGetter's own default (PackageDeletionBehavior: Unlist) would leave the old content in
+ // place and reject this as a duplicate; Floci overrides it to HardDelete specifically so
+ // a deleted package's coordinates are genuinely free again, matching every other
+ // container-backed format's own DeletePackage semantics.
+ byte[] nupkg = buildNupkg(PACKAGE_ID, VERSION, "A description.", null, null);
+ given().header("Authorization", "Bearer " + bearerToken)
+ .contentType("multipart/form-data; boundary=boundary123")
+ .body(pushBody("boundary123", nupkg))
+ .put("/codeartifact/nuget/" + DOMAIN + "/" + REPO + "/api/v2/package")
+ .then().statusCode(201);
+ }
+
+ private static long describeDomainAssetSizeBytes() {
+ return given().header("Authorization", AUTH)
+ .get("/v1/domain?domain=" + DOMAIN)
+ .then().statusCode(200)
+ .extract().jsonPath().getLong("domain.assetSizeBytes");
+ }
+
+ /**
+ * The real NuGet push protocol's own documented shape: {@code multipart/form-data} whose
+ * first item is the raw bytes of the {@code .nupkg}; the part name and filename are ignored
+ * by both the real protocol and {@code CodeArtifactNuGetController}, so {@code "package"}/
+ * {@code "p.nupkg"} here are arbitrary.
+ */
+ private static byte[] pushBody(String boundary, byte[] nupkg) {
+ String header = "--" + boundary + "\r\n"
+ + "Content-Disposition: form-data; name=\"package\"; filename=\"p.nupkg\"\r\n"
+ + "Content-Type: application/octet-stream\r\n\r\n";
+ String footer = "\r\n--" + boundary + "--\r\n";
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ out.writeBytes(header.getBytes(StandardCharsets.UTF_8));
+ out.writeBytes(nupkg);
+ out.writeBytes(footer.getBytes(StandardCharsets.UTF_8));
+ return out.toByteArray();
+ }
+
+ /**
+ * A real {@code .nupkg} is a plain ZIP with a {@code .nuspec} manifest at its root (built with
+ * the JDK's own {@code java.util.zip}, matching {@code NuspecXml.extractFromNupkg}'s own
+ * expectations); {@code readmePathOrNull}/{@code readmeTextOrNull} additionally pack a second
+ * entry and reference it with a nuspec {@code <readme>} element, the real NuGet readme-file
+ * feature.
+ */
+ private static byte[] buildNupkg(String id, String version, String description, String readmePathOrNull,
+ String readmeTextOrNull) {
+ String readmeElement = readmePathOrNull == null ? "" : "<readme>" + readmePathOrNull + "</readme>";
+ String nuspec = "<?xml version=\"1.0\"?>"
+ + "<package xmlns=\"http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd\">"
+ + "<metadata><id>" + id + "</id><version>" + version + "</version><authors>floci</authors>"
+ + readmeElement + "<description>" + description + "</description></metadata></package>";
+ try {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ try (ZipOutputStream zip = new ZipOutputStream(out)) {
+ zip.putNextEntry(new ZipEntry(id + ".nuspec"));
+ zip.write(nuspec.getBytes(StandardCharsets.UTF_8));
+ zip.closeEntry();
+ if (readmePathOrNull != null) {
+ zip.putNextEntry(new ZipEntry(readmePathOrNull));
+ zip.write(readmeTextOrNull.getBytes(StandardCharsets.UTF_8));
+ zip.closeEntry();
+ }
+ }
+ return out.toByteArray();
+ } catch (Exception e) {
+ throw new RuntimeException(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 5fd4fffd10..d5259e0229 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
@@ -58,6 +58,7 @@ class CodeArtifactServiceTest {
private VerdaccioSidecarClient verdaccioClient;
private ReposiliteSidecarClient reposiliteClient;
private PypiserverSidecarClient pypiserverClient;
+ private BagetterSidecarClient bagetterClient;
@BeforeEach
void setUp() {
@@ -82,9 +83,11 @@ class CodeArtifactServiceTest {
when(reposiliteClient.format()).thenReturn("maven");
pypiserverClient = mock(PypiserverSidecarClient.class);
when(pypiserverClient.format()).thenReturn("pypi");
+ bagetterClient = mock(BagetterSidecarClient.class);
+ when(bagetterClient.format()).thenReturn("nuget");
service = new CodeArtifactService(domainStore, repoStore, packageVersionStore, packageOriginConfigStore,
regionResolver, config, true, null, new CodeArtifactSidecarRegistry(
- List.of(verdaccioClient, reposiliteClient, pypiserverClient)));
+ List.of(verdaccioClient, reposiliteClient, pypiserverClient, bagetterClient)));
}
// -------------------------------------------------------------- domains
@@ -549,6 +552,26 @@ class CodeArtifactServiceTest {
assertEquals(0L, service.describeDomain(REGION, "dom", null).assetSizeBytes());
}
+ /**
+ * AWS's own NuGet normalization doc: package names are lowercased only, no further collapsing
+ * the way pypi's PEP 503 normalization does, so an upload spelled with mixed case and a {@code
+ * DeletePackage} spelled already-lowercase still address the one byte count BaGetter itself would
+ * treat as a single package.
+ */
+ @Test
+ void nugetAccountingNormalizesThePackageNameToLowercase() {
+ service.createDomain(REGION, "dom", null, Map.of());
+ service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
+ recordBytes("dom", "repo", "nuget", null, "My.Package", 42L);
+ assertEquals(42L, service.describeDomain(REGION, "dom", null).assetSizeBytes());
+ when(bagetterClient.packageExists(anyString(), eq("dom"), eq("repo"), isNull(), eq("my.package")))
+ .thenReturn(true);
+
+ service.deletePackage(REGION, "dom", null, "repo", "nuget", null, "my.package");
+
+ assertEquals(0L, service.describeDomain(REGION, "dom", null).assetSizeBytes());
+ }
+
@Test
void concurrentCreateDomainWithSameNameOnlyOneWins() throws InterruptedException {
int attempts = 8;
@@ -650,7 +673,7 @@ class CodeArtifactServiceTest {
service.createDomain(REGION, "dom", null, Map.of());
CodeArtifactRepository created = service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
- assertEquals(Set.of("maven", "npm", "pypi"), created.getSidecarContainerIds().keySet());
+ assertEquals(Set.of("maven", "npm", "pypi", "nuget"), created.getSidecarContainerIds().keySet());
created.getSidecarContainerIds().values()
.forEach(id -> assertTrue(id != null && !id.isBlank()));
}
@@ -1480,6 +1503,31 @@ class CodeArtifactServiceTest {
verify(pypiserverClient, never()).packageVersionExists(any(), any(), any(), any(), any(), any());
}
+ @Test
+ void describePackageVersionBridgesToTheNugetSidecar() {
+ service.createDomain(REGION, "dom", null, Map.of());
+ service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
+ when(bagetterClient.packageVersionExists(anyString(), eq("dom"), eq("repo"), isNull(), eq("my-pkg"),
+ eq("1.0.0"))).thenReturn(true);
+
+ CodeArtifactPackageVersion pv = service.describePackageVersion(REGION, "dom", null, "repo", "nuget", null,
+ "my-pkg", "1.0.0");
+
+ assertEquals("Published", pv.getStatus());
+ assertEquals("my-pkg", pv.getPackageName());
+ }
+
+ @Test
+ void describePackageVersionRejectsANamespaceForNuget() {
+ service.createDomain(REGION, "dom", null, Map.of());
+ service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
+
+ AwsException e = assertThrows(AwsException.class, () -> service.describePackageVersion(REGION, "dom", null,
+ "repo", "nuget", "ns", "my-pkg", "1.0.0"));
+ assertEquals("ValidationException", e.getErrorCode());
+ verify(bagetterClient, never()).packageVersionExists(any(), any(), any(), any(), any(), any());
+ }
+
@Test
void describePackageReportsTheDirectPublishDefaultsAndMatchesExactly() {
service.createDomain(REGION, "dom", null, Map.of());
@@ -1674,6 +1722,11 @@ class CodeArtifactServiceTest {
"ALLOW", "BLOCK"));
assertEquals("ValidationException", namespaceForPypi.getErrorCode());
+ AwsException namespaceForNuget = assertThrows(AwsException.class, () -> service
+ .putPackageOriginConfiguration(REGION, "dom", null, "repo", "nuget", "bogus-namespace", "my-pkg",
+ "ALLOW", "BLOCK"));
+ assertEquals("ValidationException", namespaceForNuget.getErrorCode());
+
// None of the rejections above may have created the package or changed anything about it:
// every one of them must still 404 just like before any of these calls were attempted.
AwsException stillAbsent = assertThrows(AwsException.class, () -> service.describePackage(REGION, "dom",
@@ -2294,20 +2347,79 @@ class CodeArtifactServiceTest {
}
/**
- * nuget is a documented valid format for this action, but nothing in Floci implements the
- * real NuGet wire protocol, so no nuget package version can ever actually exist here; this
- * must 404 honestly rather than throwing some other error for a format it otherwise accepts.
+ * Unlike npm and pypi, nuget is answered live against its sidecar (see {@code
+ * BagetterSidecarClient#fetchPackageVersionReadme}'s own javadoc for why BaGetter can support this
+ * where Verdaccio and pypiserver can't), so this never goes through {@code
+ * recordPublishedPackageVersionReadme}/{@code packageVersionReadmes} the way npm/pypi do.
*/
@Test
- void getPackageVersionReadme404sForNugetSinceNothingCanEverPublishOne() {
+ void getPackageVersionReadmeReturnsWhatTheNugetSidecarAnswersLive() {
+ service.createDomain(REGION, "dom", null, Map.of());
+ service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
+ when(bagetterClient.fetchPackageVersionReadme(anyString(), eq("dom"), eq("repo"), isNull(), eq("my-pkg"),
+ eq("1.0.0"))).thenReturn(Optional.of("# My Package"));
+
+ CodeArtifactService.PackageVersionReadmeResult result = service.getPackageVersionReadme(REGION, "dom", null,
+ "repo", "nuget", null, "my-pkg", "1.0.0");
+
+ assertEquals("# My Package", result.readme());
+ assertEquals("my-pkg", result.packageName());
+ assertEquals("1.0.0", result.version());
+ }
+
+ /** A published nuget version with no embedded readme or description answers "", not 404. */
+ @Test
+ void getPackageVersionReadmeReturnsEmptyStringForANugetVersionWithNoReadmeOrDescription() {
service.createDomain(REGION, "dom", null, Map.of());
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
+ when(bagetterClient.fetchPackageVersionReadme(anyString(), eq("dom"), eq("repo"), isNull(), eq("my-pkg"),
+ eq("1.0.0"))).thenReturn(Optional.of(""));
+
+ CodeArtifactService.PackageVersionReadmeResult result = service.getPackageVersionReadme(REGION, "dom", null,
+ "repo", "nuget", null, "my-pkg", "1.0.0");
+
+ assertEquals("", result.readme());
+ }
+
+ @Test
+ void getPackageVersionReadme404sForNugetWhenTheSidecarHasNeverPublishedIt() {
+ service.createDomain(REGION, "dom", null, Map.of());
+ service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
+ when(bagetterClient.fetchPackageVersionReadme(anyString(), eq("dom"), eq("repo"), isNull(), eq("my-pkg"),
+ eq("1.0.0"))).thenReturn(Optional.empty());
AwsException e = assertThrows(AwsException.class, () -> service.getPackageVersionReadme(REGION, "dom", null,
"repo", "nuget", null, "my-pkg", "1.0.0"));
assertEquals("ResourceNotFoundException", e.getErrorCode());
}
+ /**
+ * Found by Qodo review: a caller-supplied namespace for pypi or nuget, neither of which has
+ * one, used to pass this method's own validation (it only checked namespace's token syntax)
+ * and reach the sidecar lookup, which ignores the namespace entirely and would echo it back
+ * in the response as though it belonged to the package.
+ */
+ @Test
+ void getPackageVersionReadmeRejectsANamespaceForNuget() {
+ service.createDomain(REGION, "dom", null, Map.of());
+ service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
+
+ AwsException e = assertThrows(AwsException.class, () -> service.getPackageVersionReadme(REGION, "dom", null,
+ "repo", "nuget", "ns", "my-pkg", "1.0.0"));
+ assertEquals("ValidationException", e.getErrorCode());
+ verify(bagetterClient, never()).fetchPackageVersionReadme(any(), any(), any(), any(), any(), any());
+ }
+
+ @Test
+ void getPackageVersionReadmeRejectsANamespaceForPypi() {
+ service.createDomain(REGION, "dom", null, Map.of());
+ service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
+
+ AwsException e = assertThrows(AwsException.class, () -> service.getPackageVersionReadme(REGION, "dom", null,
+ "repo", "pypi", "ns", "my-pkg", "1.0.0"));
+ assertEquals("ValidationException", e.getErrorCode());
+ }
+
@Test
void recordPublishedPackageVersionReadmeSkipsAPackageNameContainingTheEscapeCharacterWithoutCorruptingOthers() {
service.createDomain(REGION, "dom", null, Map.of());
diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/NuGetVersionsTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/NuGetVersionsTest.java
new file mode 100644
index 0000000000..9b633436cd
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/NuGetVersionsTest.java
@@ -0,0 +1,73 @@
+package io.github.hectorvent.floci.services.codeartifact;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/** Each case is one of the three transformations AWS's own NuGet normalization doc documents. */
+class NuGetVersionsTest {
+
+ /**
+ * AWS's own worked example text is "1.00 is treated as 1.0", a 2-segment result; this
+ * intentionally returns {@code 1.0.0} instead, matching what the real {@code
+ * NuGetVersion.ToNormalizedString()} BaGetter itself calls actually produces (always at least
+ * Major.Minor.Patch) rather than AWS's own apparently-simplified doc example. This class
+ * exists only to match what BaGetter's own version list already contains well enough to find
+ * a caller's version in it, so matching the real sidecar's output is what matters here, not
+ * reproducing AWS's doc text byte-for-byte.
+ */
+ @Test
+ void stripsLeadingZeroesFromEachSegment() {
+ assertEquals("1.0.0", NuGetVersions.normalize("1.00"));
+ assertEquals("1.1.1", NuGetVersions.normalize("1.01.1"));
+ assertEquals("1.0.0.1", NuGetVersions.normalize("1.00.0.1"));
+ }
+
+ @Test
+ void omitsAZeroFourthSegment() {
+ assertEquals("1.0.0", NuGetVersions.normalize("1.0.0.0"));
+ assertEquals("1.0.1", NuGetVersions.normalize("1.0.01.0"));
+ }
+
+ @Test
+ void removesSemVerBuildMetadata() {
+ assertEquals("1.0.7", NuGetVersions.normalize("1.0.7+r3456"));
+ }
+
+ @Test
+ void padsAShortReleaseUpToThreeSegments() {
+ assertEquals("1.0.0", NuGetVersions.normalize("1.0"));
+ assertEquals("1.0.0", NuGetVersions.normalize("1"));
+ }
+
+ @Test
+ void keepsAPrereleaseLabelAndLowercasesIt() {
+ assertEquals("1.0.0-beta.1", NuGetVersions.normalize("1.0.0-Beta.1"));
+ }
+
+ @Test
+ void leavesAnythingItDoesNotConfidentlyUnderstandUnchanged() {
+ assertEquals("not-a-version", NuGetVersions.normalize("not-a-version"));
+ }
+
+ /**
+ * Found by Qodo review: a plain {@code split} silently drops a trailing empty string, so
+ * {@code "1.0.0."} would otherwise normalize identically to the real version {@code "1.0.0"}
+ * and let a malformed caller-supplied version resolve a published one.
+ */
+ @Test
+ void leavesATrailingDotUnchangedRatherThanNormalizingItAwayAsAnEmptySegment() {
+ assertEquals("1.0.0.", NuGetVersions.normalize("1.0.0."));
+ }
+
+ /**
+ * Found by Qodo review: an oversized numeric segment used to throw {@code
+ * NumberFormatException} out of {@code normalize} instead of falling back like any other
+ * segment it isn't confident it understood.
+ */
+ @Test
+ void leavesASegmentTooLargeForALongUnchangedRatherThanThrowing() {
+ String tooLarge = "99999999999999999999.0.0";
+ assertEquals(tooLarge, NuGetVersions.normalize(tooLarge));
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/NuspecXmlTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/NuspecXmlTest.java
new file mode 100644
index 0000000000..5f94e5fc93
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/NuspecXmlTest.java
@@ -0,0 +1,105 @@
+package io.github.hectorvent.floci.services.codeartifact;
+
+import org.junit.jupiter.api.Test;
+
+import java.io.ByteArrayOutputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.Optional;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class NuspecXmlTest {
+
+ private static final String NUSPEC = "<?xml version=\"1.0\"?>"
+ + "<package xmlns=\"http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd\">"
+ + "<metadata><id>Demo.Package</id><version>1.0.0</version><authors>me</authors>"
+ + "<description>A description.</description></metadata></package>";
+
+ @Test
+ void parseReadsIdVersionAndDescriptionThroughTheDefaultNamespace() {
+ NuspecXml.Metadata metadata = NuspecXml.parse(NUSPEC.getBytes(StandardCharsets.UTF_8));
+
+ assertEquals("Demo.Package", metadata.id());
+ assertEquals("1.0.0", metadata.version());
+ assertEquals(Optional.of("A description."), metadata.description());
+ }
+
+ @Test
+ void parseRejectsAMissingRequiredElement() {
+ String nuspec = "<?xml version=\"1.0\"?><package><metadata><id>Demo</id></metadata></package>";
+
+ assertThrows(IllegalStateException.class, () -> NuspecXml.parse(nuspec.getBytes(StandardCharsets.UTF_8)));
+ }
+
+ /**
+ * A DOCTYPE declaration is rejected outright rather than silently resolved: this is the one
+ * observable way to confirm external entity expansion is actually disabled, since a
+ * successful parse of a benign payload alone wouldn't distinguish "XXE is disabled" from
+ * "this particular payload didn't need an external entity anyway."
+ */
+ @Test
+ void parseRejectsADoctypeDeclarationRatherThanResolvingIt() {
+ String maliciousNuspec = "<?xml version=\"1.0\"?><!DOCTYPE package [<!ENTITY xxe SYSTEM \"file:///etc/"
+ + "hostname\">]><package><metadata><id>&xxe;</id><version>1.0.0</version></metadata></package>";
+
+ assertThrows(IllegalStateException.class,
+ () -> NuspecXml.parse(maliciousNuspec.getBytes(StandardCharsets.UTF_8)));
+ }
+
+ @Test
+ void extractFromNupkgFindsTheRootNuspecEntryInsideTheZip() throws Exception {
+ byte[] nupkg = buildNupkg(NUSPEC);
+
+ NuspecXml.Metadata metadata = NuspecXml.extractFromNupkg(nupkg);
+
+ assertEquals("Demo.Package", metadata.id());
+ }
+
+ /** The manifest must sit at the archive root; a same-suffix entry nested in a subdirectory doesn't count. */
+ @Test
+ void extractFromNupkgIgnoresANestedNuspecSuffixedEntry() {
+ byte[] nupkg = buildNupkg(null);
+
+ assertThrows(IllegalStateException.class, () -> NuspecXml.extractFromNupkg(nupkg));
+ }
+
+ /**
+ * Found by Qodo review: a highly-compressible entry can expand to far more than the overall
+ * upload's own, already-checked, compressed byte count, so the bound has to apply to the
+ * decompressed stream, not just the request body. The repeated character below compresses to
+ * a tiny fraction of its real size, matching exactly that shape.
+ */
+ @Test
+ void extractFromNupkgRejectsANuspecEntryThatDecompressesPastTheSizeLimit() {
+ String oversizedDescription = "A".repeat(2 * 1024 * 1024);
+ String nuspec = "<?xml version=\"1.0\"?><package><metadata><id>Demo</id><version>1.0.0</version>"
+ + "<description>" + oversizedDescription + "</description></metadata></package>";
+ byte[] nupkg = buildNupkg(nuspec);
+
+ assertThrows(IllegalStateException.class, () -> NuspecXml.extractFromNupkg(nupkg));
+ }
+
+ private static byte[] buildNupkg(String rootNuspecOrNull) {
+ try {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ try (ZipOutputStream zip = new ZipOutputStream(out)) {
+ if (rootNuspecOrNull != null) {
+ zip.putNextEntry(new ZipEntry("Demo.Package.nuspec"));
+ zip.write(rootNuspecOrNull.getBytes(StandardCharsets.UTF_8));
+ zip.closeEntry();
+ } else {
+ zip.putNextEntry(new ZipEntry("package/services/metadata/core-properties/fake.nuspec"));
+ zip.write("not a real manifest".getBytes(StandardCharsets.UTF_8));
+ zip.closeEntry();
+ }
+ }
+ return out.toByteArray();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+}