mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
feat(codeartifact): serve the npm format through a real Verdaccio proxy (#4336)
GetRepositoryEndpoint for format=npm now returns a working registry endpoint: real npm/yarn/pnpm clients can publish to and install from it, proxied straight through to a real Verdaccio instance rather than reimplemented. Every request needs Authorization: Bearer <token> from GetAuthorizationToken, matching npm's own _authToken config and, unlike Maven's HTTP Basic support, Bearer only. Unlike Reposilite, Verdaccio has no native concept of multiple named repositories inside one instance, so each CodeArtifact repository gets its own container instead of sharing one, started lazily on first use and identified by a fresh id assigned at CreateRepository time so a repository deleted and recreated under the same name never inherits the previous one's packages. Each container is started with VERDACCIO_PUBLIC_URL set to that repository's own proxy URL so package metadata it returns (dist.tarball) points back through Floci instead of the container's own internal, client-unreachable address. VerdaccioSidecarManager implements the RepositorySidecarManager interface CodeArtifactService already generalized DeleteRepository's cleanup over, so a deleted repository's Verdaccio container is stopped and removed the same generic way Maven's Reposilite storage is released. The per-key container lifecycle (map of running containers, per-key start lock, health poll, restart-on-unhealthy, stop-all) has nothing Verdaccio-specific in it, so it is pulled out into PerKeyContainerPool for a future per-repository sidecar to reuse directly. CodeArtifactNpmDataPlane is a raw streaming proxy rather than a JAX-RS controller, since npm's wire protocol has no fixed path shape the way Maven's GAV layout does; Verdaccio already speaks that protocol correctly, so this only needs to authenticate the request and forward it. The publish/fetch integration test replays a real request the npm CLI sent during manual verification, captured with a tiny Node.js capture server, so it exercises the exact envelope shape (including a real gzip tarball attachment) npm publish actually produces; the full round trip was additionally verified by hand with the real npm CLI against a live Floci instance.
This commit is contained in:
1 parent
d2c0705ea9
commit
3e6c7a3472
17 files changed
+1554
-71
No files matched your search
@@ -7,10 +7,10 @@
|
||||
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` format is served through its own real
|
||||
package-manager-protocol proxy (`mvn`/Gradle publish and resolve both work against the URL
|
||||
`GetRepositoryEndpoint` returns); the remaining formats (npm, PyPI, NuGet, etc.) have no real
|
||||
proxy behind them yet.
|
||||
`PublishPackageVersion` accepts only `generic`. The `maven` and `npm` formats are each served
|
||||
through their own real package-manager-protocol proxy (`mvn`/Gradle and `npm`/`yarn`/`pnpm`
|
||||
publish and resolve all work against the URL `GetRepositoryEndpoint` returns); the remaining
|
||||
formats (PyPI, NuGet, etc.) have no real proxy behind them yet.
|
||||
|
||||
## Supported Actions
|
||||
|
||||
@@ -21,7 +21,7 @@ proxy behind them yet.
|
||||
| `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. |
|
||||
| `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` repository endpoint. |
|
||||
| `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. |
|
||||
| `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`. |
|
||||
@@ -95,6 +95,31 @@ shared settings list. Two config knobs,
|
||||
image version or point at an already-running instance and skip container management, matching the
|
||||
pattern used elsewhere in Floci for sidecars.
|
||||
|
||||
## The npm repository endpoint
|
||||
|
||||
`GetRepositoryEndpoint` for `format=npm` returns `http://localhost:4566/codeartifact/npm/<domain>/<repository>/`.
|
||||
Real npm clients (`npm publish`, `npm install`, and their `yarn`/`pnpm` equivalents) can publish to
|
||||
and resolve from that URL directly; it speaks the real npm registry protocol (package metadata,
|
||||
tarball fetch, publish), proxied straight through to a real [Verdaccio](https://verdaccio.org)
|
||||
instance rather than reimplemented. Every request needs `Authorization: Bearer <token>`, using a
|
||||
token from `GetAuthorizationToken` scoped to the domain being accessed; npm's own credential
|
||||
configuration (`.npmrc`'s `//<registry-host>/<path>/:_authToken=...`) sets this the same way it
|
||||
would against real AWS, and always as Bearer (unlike Maven's HTTP Basic). A missing, invalid,
|
||||
expired, or wrong-domain token gets a 401 challenging `Bearer`.
|
||||
|
||||
Unlike Reposilite, which has a native concept of multiple named repositories inside one instance,
|
||||
Verdaccio does not: each CodeArtifact repository gets its own Verdaccio 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 Maven proxy releases its own storage the same way, just through Reposilite's
|
||||
settings API instead of a container stop, since Reposilite is one shared instance). Each container is
|
||||
started with `VERDACCIO_PUBLIC_URL` set to that repository's own proxy URL, so package metadata it
|
||||
returns (`dist.tarball`) points back through Floci instead of the container's own internal,
|
||||
client-unreachable address; without this, `npm install` would try to fetch the tarball directly
|
||||
from an address it cannot reach. One config knob, `FLOCI_SERVICES_CODEARTIFACT_NPM_IMAGE`, pins
|
||||
the image version.
|
||||
|
||||
## AWS-compatible failures
|
||||
|
||||
Domain and repository names, tags, pagination, duplicate names, missing upstreams, policy-revision
|
||||
@@ -135,24 +160,28 @@ state.
|
||||
runs. The rejection (`ServiceQuotaExceededException` from `PublishPackageVersion`, HTTP 413 from
|
||||
the Maven endpoint) is correct for anything that does fit in memory; it is not itself a streaming
|
||||
size limit.
|
||||
- **Maven artifacts do not survive a Floci restart, even under persistent storage.** The Reposilite
|
||||
sidecar container has no volume attached and is removed on shutdown along with everything
|
||||
published to it. CodeArtifact repository/domain metadata (including the stored sidecar container
|
||||
id) survives a restart the same way any other Floci state does under persistent storage mode;
|
||||
the artifacts themselves do not, so the first Maven request after a restart
|
||||
re-provisions an empty Reposilite repository and returns 404 for anything published before the
|
||||
restart.
|
||||
- **Maven artifacts and npm packages do not survive a Floci restart, even under persistent
|
||||
storage.** The Reposilite instance and every per-repository Verdaccio 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.
|
||||
- **`GetAuthorizationToken` tokens are not revocable and are not tied to any IAM identity.** Real
|
||||
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 repository endpoint does not 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`, etc.) serves packages from those sources too, not just its
|
||||
own. Floci's Maven proxy only ever looks 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.
|
||||
- **Neither the Maven nor the npm 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`, 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 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
|
||||
path to check a byte count against the quota before forwarding it.
|
||||
|
||||
See the [CodeArtifact API Reference](https://docs.aws.amazon.com/codeartifact/latest/APIReference/Welcome.html).
|
||||
|
||||
@@ -164,3 +193,4 @@ See the [CodeArtifact API Reference](https://docs.aws.amazon.com/codeartifact/la
|
||||
| `FLOCI_SERVICES_CODEARTIFACT_MAVEN_IMAGE` | `dzikoysk/reposilite:3.6.3` | Reposilite image used to serve the `maven` format |
|
||||
| `FLOCI_SERVICES_CODEARTIFACT_MAVEN_URL` | unset | When set, use this URL and skip Reposilite container management |
|
||||
| `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 |
|
||||
@@ -121,7 +121,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac
|
||||
| [AWS Backup](backup.md) | `/backup-vaults/*`, `/backup/plans/*`, `/backup-jobs/*`, `/supported-resource-types` | REST JSON | 20 |
|
||||
| [AWS FIS](fis.md) | `/experimentTemplates/*`, `/experiments/*`, `/actions/*`, `/targetResourceTypes/*`, `/safetyLevers/*`, `/tags/*` | REST JSON | 26 |
|
||||
| [CodeGuru Reviewer](codegurureviewer.md) | `/associations`, `/associations/{associationArn}`, `/tags/*` | REST JSON | 7 |
|
||||
| [CodeArtifact](codeartifact.md) | `/v1/domain*`, `/v1/repository*`, `/v1/package/version*`, `/v1/tag*`, `/v1/authorization-token`, `/codeartifact/maven/*` | REST JSON | 26 |
|
||||
| [CodeArtifact](codeartifact.md) | `/v1/domain*`, `/v1/repository*`, `/v1/package/version*`, `/v1/tag*`, `/v1/authorization-token`, `/codeartifact/maven/*`, `/codeartifact/npm/*` | REST JSON | 26 |
|
||||
| [CloudFront](cloudfront.md) | `/2020-05-31/distribution/*`, `/2020-05-31/cache-policy/*`, `/2020-05-31/function/*` | REST XML | 50 |
|
||||
| [Route53](route53.md) | `/2013-04-01/hostedzone/*`, `/2013-04-01/healthcheck/*`, `/2013-04-01/change/*` | REST XML | 25 |
|
||||
| [Route 53 Resolver](route53resolver.md) | `POST /` + `X-Amz-Target: Route53Resolver.*` | JSON 1.1 | 18 |
|
||||
|
||||
@@ -825,6 +825,15 @@ public interface EmulatorConfig {
|
||||
|
||||
@WithDefault("dzikoysk/reposilite:3.6.3")
|
||||
String mavenImage();
|
||||
|
||||
/**
|
||||
* Image used for the per-repository Verdaccio container backing the {@code npm} format.
|
||||
* No URL/token override like {@link #mavenUrl()}: unlike Reposilite's one shared instance,
|
||||
* npm gets one container per CodeArtifact repository, so there is no single external
|
||||
* instance to point at.
|
||||
*/
|
||||
@WithDefault("verdaccio/verdaccio:6.10.4")
|
||||
String npmImage();
|
||||
}
|
||||
|
||||
interface ConnectServiceConfig {
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
package io.github.hectorvent.floci.core.common.docker;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Lazily starts, health-checks, and reuses one container per arbitrary key. Pulled out of
|
||||
* {@code VerdaccioSidecarManager} (one Verdaccio container per CodeArtifact npm repository) since
|
||||
* 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.
|
||||
*
|
||||
* <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.
|
||||
*/
|
||||
public class PerKeyContainerPool {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(PerKeyContainerPool.class);
|
||||
|
||||
/** What a {@link Starter} hands back once its container is created and started. */
|
||||
public record StartedContainer(String containerId, String baseUrl) {}
|
||||
|
||||
/** Builds and starts a fresh container for the key {@link #ensureReady} was called with. */
|
||||
@FunctionalInterface
|
||||
public interface Starter {
|
||||
StartedContainer start();
|
||||
}
|
||||
|
||||
private final ContainerLifecycleManager lifecycleManager;
|
||||
private final String healthPath;
|
||||
private final ConcurrentHashMap<String, StartedContainer> containers = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<String, Object> startLocks = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* @param healthPath path (e.g. {@code /-/ping}) appended to a container's base URL to probe
|
||||
* readiness; expected to return HTTP 200 once the container can serve
|
||||
*/
|
||||
public PerKeyContainerPool(ContainerLifecycleManager lifecycleManager, String healthPath) {
|
||||
this.lifecycleManager = lifecycleManager;
|
||||
this.healthPath = healthPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Base URL of a ready container for {@code key}, starting one via {@code starter} if this is
|
||||
* the first use or the previous container is no longer healthy. Concurrent calls for
|
||||
* different keys never block on each other; concurrent calls for the same key serialize so
|
||||
* only one container is ever started for it.
|
||||
*/
|
||||
public String ensureReady(String key, Starter starter) {
|
||||
StartedContainer existing = containers.get(key);
|
||||
if (existing != null && SidecarHealthHelper.probeHealth(existing.baseUrl(), healthPath)) {
|
||||
return existing.baseUrl();
|
||||
}
|
||||
Object lock = startLocks.computeIfAbsent(key, k -> new Object());
|
||||
synchronized (lock) {
|
||||
existing = containers.get(key);
|
||||
if (existing != null && SidecarHealthHelper.probeHealth(existing.baseUrl(), healthPath)) {
|
||||
return existing.baseUrl();
|
||||
}
|
||||
if (existing != null) {
|
||||
LOG.warnv("Sidecar container for key {0} is no longer healthy; restarting it", key);
|
||||
lifecycleManager.stopAndRemove(existing.containerId(), null);
|
||||
}
|
||||
StartedContainer started = starter.start();
|
||||
// Tracked before the health wait, not after: a timeout below must not stop
|
||||
// stopContainer()/stopAll() from ever finding this already-running container again.
|
||||
containers.put(key, started);
|
||||
SidecarHealthHelper.waitForHealth(started.baseUrl(), healthPath);
|
||||
return started.baseUrl();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops and removes the container for one key, if one was ever started. No-op otherwise.
|
||||
* Shares {@link #ensureReady}'s per-key lock so a stop racing a concurrent first-use of the
|
||||
* same key serializes against it, instead of running between the moment that start commits to
|
||||
* a container and the moment it becomes visible here: without the shared lock, a stop that
|
||||
* finds nothing to remove could be immediately followed by that start's own container
|
||||
* registration, orphaning it since nothing calls stop for the key again.
|
||||
*/
|
||||
public void stopContainer(String key) {
|
||||
if (key == null) {
|
||||
return;
|
||||
}
|
||||
Object lock = startLocks.computeIfAbsent(key, k -> new Object());
|
||||
synchronized (lock) {
|
||||
StartedContainer container = containers.remove(key);
|
||||
if (container != null) {
|
||||
lifecycleManager.stopAndRemove(container.containerId(), null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Stops and removes every container this pool has started, then forgets them all. */
|
||||
public void stopAll() {
|
||||
if (containers.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
LOG.infov("Stopping {0} pooled sidecar container(s)", containers.size());
|
||||
containers.forEach((key, container) -> {
|
||||
try {
|
||||
lifecycleManager.stopAndRemove(container.containerId(), null);
|
||||
} catch (Exception e) {
|
||||
LOG.debugv(e, "Failed to stop pooled sidecar container for key {0}", key);
|
||||
}
|
||||
});
|
||||
containers.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package io.github.hectorvent.floci.core.common.docker;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
|
||||
/**
|
||||
* A single HTTP GET health probe, and a bounded poll built on it, shared by every sidecar manager
|
||||
* that waits for its own container to come up: {@link PerKeyContainerPool} and
|
||||
* {@code ReposiliteSidecarManager} both had their own copy of this before it moved here.
|
||||
*/
|
||||
public final class SidecarHealthHelper {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(SidecarHealthHelper.class);
|
||||
private static final int HEALTH_POLL_MAX_MS = 30_000;
|
||||
private static final int HEALTH_POLL_INTERVAL_MS = 500;
|
||||
private static final int PROBE_TIMEOUT_MS = 500;
|
||||
|
||||
private SidecarHealthHelper() {
|
||||
}
|
||||
|
||||
/** {@code true} once a GET to {@code baseUrl + healthPath} returns HTTP 200. */
|
||||
public static boolean probeHealth(String baseUrl, String healthPath) {
|
||||
try {
|
||||
HttpURLConnection connection = (HttpURLConnection) URI.create(baseUrl + healthPath).toURL()
|
||||
.openConnection();
|
||||
connection.setConnectTimeout(PROBE_TIMEOUT_MS);
|
||||
connection.setReadTimeout(PROBE_TIMEOUT_MS);
|
||||
return connection.getResponseCode() == 200;
|
||||
} catch (IOException e) {
|
||||
LOG.debugv(e, "Sidecar health probe failed for {0}", baseUrl);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Polls {@link #probeHealth} every {@value #HEALTH_POLL_INTERVAL_MS} ms until it succeeds or
|
||||
* {@value #HEALTH_POLL_MAX_MS} ms have passed, in which case this throws.
|
||||
*/
|
||||
public static void waitForHealth(String baseUrl, String healthPath) {
|
||||
long deadline = System.currentTimeMillis() + HEALTH_POLL_MAX_MS;
|
||||
while (System.currentTimeMillis() < deadline) {
|
||||
if (probeHealth(baseUrl, healthPath)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Thread.sleep(HEALTH_POLL_INTERVAL_MS);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException("Interrupted while waiting for sidecar container", e);
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("Sidecar container at " + baseUrl + " did not become healthy within "
|
||||
+ HEALTH_POLL_MAX_MS + " ms");
|
||||
}
|
||||
}
|
||||
+214
@@ -0,0 +1,214 @@
|
||||
package io.github.hectorvent.floci.services.codeartifact;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.ServiceConfigAccess;
|
||||
import io.github.hectorvent.floci.services.codeartifact.CodeArtifactService.AuthorizationTokenScope;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.http.HttpClient;
|
||||
import io.vertx.core.http.HttpClientOptions;
|
||||
import io.vertx.core.http.HttpClientRequest;
|
||||
import io.vertx.core.http.HttpClientResponse;
|
||||
import io.vertx.core.http.HttpMethod;
|
||||
import io.vertx.core.http.RequestOptions;
|
||||
import io.vertx.ext.web.Router;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.event.Observes;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Proxies the real npm registry protocol through Floci to the per-repository Verdaccio container
|
||||
* backing {@code GetRepositoryEndpoint}'s {@code npm} format
|
||||
* ({@code /codeartifact/npm/<domain>/<repository>/}). A raw streaming proxy, not a JAX-RS
|
||||
* controller, since npm's wire protocol has no fixed path shape (package metadata, scoped
|
||||
* packages with an encoded slash, tarball downloads, publish) the way Maven's GAV layout does;
|
||||
* Verdaccio already speaks that protocol correctly, so this only needs to authenticate the
|
||||
* request and forward it.
|
||||
*
|
||||
* <p>Real CodeArtifact npm auth is {@code Authorization: Bearer <token>} (npm's own
|
||||
* {@code _authToken} config always sends Bearer, unlike Maven's HTTP wagon), so unlike
|
||||
* {@code CodeArtifactMavenController} there is no Basic-auth form to also accept here.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class CodeArtifactNpmDataPlane {
|
||||
|
||||
private static final String PREFIX = "/codeartifact/npm/";
|
||||
private static final String SERVICE_KEY = "codeartifact";
|
||||
private static final String BEARER_PREFIX = "Bearer ";
|
||||
private static final Set<String> HOP_BY_HOP_HEADERS = Set.of(
|
||||
"connection", "keep-alive", "proxy-authenticate", "proxy-authorization",
|
||||
"te", "trailers", "transfer-encoding", "upgrade", "authorization", "host");
|
||||
|
||||
private final CodeArtifactService service;
|
||||
private final VerdaccioSidecarManager verdaccioManager;
|
||||
private final ServiceConfigAccess serviceConfigAccess;
|
||||
private final EmulatorConfig config;
|
||||
private final Vertx vertx;
|
||||
private final HttpClient proxyClient;
|
||||
|
||||
@Inject
|
||||
public CodeArtifactNpmDataPlane(CodeArtifactService service, VerdaccioSidecarManager verdaccioManager,
|
||||
ServiceConfigAccess serviceConfigAccess, EmulatorConfig config, Vertx vertx) {
|
||||
this.service = service;
|
||||
this.verdaccioManager = verdaccioManager;
|
||||
this.serviceConfigAccess = serviceConfigAccess;
|
||||
this.config = config;
|
||||
this.vertx = vertx;
|
||||
this.proxyClient = vertx.createHttpClient(new HttpClientOptions()
|
||||
.setConnectTimeout(5_000)
|
||||
.setKeepAlive(true));
|
||||
}
|
||||
|
||||
void register(@Observes Router router) {
|
||||
router.route(PREFIX + "*").handler(this::handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* A raw Vert.x route, unlike {@code CodeArtifactMavenController}, never passes through
|
||||
* {@code ServiceEnabledFilter} (a JAX-RS {@code ContainerRequestFilter}), so a disabled
|
||||
* {@code codeartifact} service has to be checked here explicitly or this route would keep
|
||||
* serving npm reads and publishes regardless.
|
||||
*/
|
||||
private void handle(RoutingContext context) {
|
||||
if (!serviceConfigAccess.isEnabled(SERVICE_KEY)) {
|
||||
context.next();
|
||||
return;
|
||||
}
|
||||
Optional<NpmRequest> request = requestFor(context.request().path());
|
||||
if (request.isEmpty()) {
|
||||
context.next();
|
||||
return;
|
||||
}
|
||||
NpmRequest npmRequest = request.get();
|
||||
|
||||
String token = extractToken(context);
|
||||
Optional<AuthorizationTokenScope> scope =
|
||||
token == null ? Optional.empty() : service.resolveAuthorizationToken(token, npmRequest.domain());
|
||||
if (scope.isEmpty()) {
|
||||
context.response().putHeader("WWW-Authenticate", "Bearer").setStatusCode(401).end();
|
||||
return;
|
||||
}
|
||||
|
||||
String npmRepositoryId;
|
||||
try {
|
||||
npmRepositoryId = service.ensureFormatContainerId("npm", scope.get().region(), npmRequest.domain(),
|
||||
scope.get().owner(), npmRequest.repository());
|
||||
} catch (AwsException e) {
|
||||
context.response().setStatusCode(404).end();
|
||||
return;
|
||||
}
|
||||
|
||||
context.request().pause();
|
||||
String publicUrl = config.effectiveBaseUrl() + PREFIX + npmRequest.domain() + "/" + npmRequest.repository()
|
||||
+ "/";
|
||||
String repoId = npmRepositoryId;
|
||||
vertx.<String>executeBlocking(promise -> promise.complete(verdaccioManager.ensureReady(repoId, publicUrl)))
|
||||
.onComplete(result -> {
|
||||
if (result.failed()) {
|
||||
context.request().resume();
|
||||
context.response().setStatusCode(503).end();
|
||||
return;
|
||||
}
|
||||
proxy(context, result.result(), npmRequest.rest() + querySuffix(context));
|
||||
});
|
||||
}
|
||||
|
||||
private void proxy(RoutingContext context, String backendBaseUrl, String backendPath) {
|
||||
URI backend = URI.create(backendBaseUrl);
|
||||
RequestOptions options = new RequestOptions()
|
||||
.setHost(backend.getHost())
|
||||
.setPort(backend.getPort())
|
||||
.setURI(backendPath)
|
||||
.setMethod(context.request().method());
|
||||
proxyClient.request(options).onComplete(upstreamRequest -> {
|
||||
if (upstreamRequest.failed()) {
|
||||
context.request().resume();
|
||||
context.response().setStatusCode(503).end();
|
||||
return;
|
||||
}
|
||||
HttpClientRequest clientReq = upstreamRequest.result();
|
||||
copyRequestHeaders(context, clientReq);
|
||||
clientReq.response().onComplete(upstreamResponse -> {
|
||||
if (upstreamResponse.failed()) {
|
||||
if (!context.response().ended()) {
|
||||
context.response().setStatusCode(502).end();
|
||||
}
|
||||
return;
|
||||
}
|
||||
copyResponseHeaders(context, upstreamResponse.result());
|
||||
upstreamResponse.result().pipeTo(context.response());
|
||||
});
|
||||
clientReq.send(context.request()).onFailure(ignored -> {
|
||||
if (!context.response().ended()) {
|
||||
context.response().setStatusCode(502).end();
|
||||
}
|
||||
});
|
||||
context.request().resume();
|
||||
});
|
||||
}
|
||||
|
||||
private static void copyRequestHeaders(RoutingContext context, HttpClientRequest upstream) {
|
||||
context.request().headers().forEach(header -> {
|
||||
if (!HOP_BY_HOP_HEADERS.contains(header.getKey().toLowerCase())) {
|
||||
upstream.putHeader(header.getKey(), header.getValue());
|
||||
}
|
||||
});
|
||||
if (context.request().getHeader("Content-Length") == null
|
||||
&& ("chunked".equalsIgnoreCase(context.request().getHeader("Transfer-Encoding"))
|
||||
|| context.request().method() == HttpMethod.POST
|
||||
|| context.request().method() == HttpMethod.PUT
|
||||
|| context.request().method() == HttpMethod.PATCH)) {
|
||||
upstream.setChunked(true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Skips {@code Content-Length} and always streams chunked, rather than copying the upstream
|
||||
* length through: Quarkus's own response filters can commit this response to chunked framing
|
||||
* before this handler ever runs, and a Content-Length header set after that point makes Vert.x
|
||||
* throw on the first {@code pipeTo} write instead of silently doing the right thing.
|
||||
*/
|
||||
private static void copyResponseHeaders(RoutingContext context, HttpClientResponse upstream) {
|
||||
context.response().setStatusCode(upstream.statusCode());
|
||||
upstream.headers().forEach(header -> {
|
||||
if (!HOP_BY_HOP_HEADERS.contains(header.getKey().toLowerCase())
|
||||
&& !"content-length".equalsIgnoreCase(header.getKey())) {
|
||||
context.response().putHeader(header.getKey(), header.getValue());
|
||||
}
|
||||
});
|
||||
context.response().setChunked(true);
|
||||
}
|
||||
|
||||
private static String extractToken(RoutingContext context) {
|
||||
String authorization = context.request().getHeader("Authorization");
|
||||
if (authorization == null || !authorization.startsWith(BEARER_PREFIX)) {
|
||||
return null;
|
||||
}
|
||||
return authorization.substring(BEARER_PREFIX.length());
|
||||
}
|
||||
|
||||
private static String querySuffix(RoutingContext context) {
|
||||
String query = context.request().query();
|
||||
return query == null || query.isEmpty() ? "" : "?" + query;
|
||||
}
|
||||
|
||||
static Optional<NpmRequest> requestFor(String path) {
|
||||
if (path == null || !path.startsWith(PREFIX)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
String remainder = path.substring(PREFIX.length());
|
||||
String[] segments = remainder.split("/", 3);
|
||||
if (segments.length < 2 || segments[0].isEmpty() || segments[1].isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
String rest = segments.length == 3 ? "/" + segments[2] : "/";
|
||||
return Optional.of(new NpmRequest(segments[0], segments[1], rest));
|
||||
}
|
||||
|
||||
record NpmRequest(String domain, String repository, String rest) {}
|
||||
}
|
||||
+27
-6
@@ -71,7 +71,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");
|
||||
private static final List<String> CONTAINER_BACKED_FORMATS = List.of("maven", "npm");
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(CodeArtifactService.class);
|
||||
|
||||
@@ -313,16 +313,26 @@ public class CodeArtifactService implements Resettable {
|
||||
String key = repositoryKey(region, domain, repository);
|
||||
CodeArtifactRepository r = requireRepository(owner, key, repository);
|
||||
repositories.deleteForAccount(owner, key);
|
||||
// Generic over whatever formats this repository actually has sidecar ids for: adding the
|
||||
// next format's sidecar needs no change here, only a new RepositorySidecarManager
|
||||
// implementation that CodeArtifactSidecarRegistry discovers.
|
||||
//
|
||||
// Best-effort: the metadata delete above already committed, matching real CodeArtifact's
|
||||
// own DeleteRepository (synchronous and always succeeds once the repository exists; actual
|
||||
// content cleanup is an internal detail never exposed to the caller). A sidecar that is
|
||||
// unreachable when this runs must not turn an already-successful delete into a 500, and
|
||||
// must not leave the repository stuck: a retry would just 404, since the record above is
|
||||
// already gone.
|
||||
releaseSidecarStorage(r, domain, repository);
|
||||
return r;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic over whatever formats a repository actually has sidecar ids for: adding the next
|
||||
* format's sidecar needs no change here, only a new {@link RepositorySidecarManager}
|
||||
* implementation that {@link CodeArtifactSidecarRegistry} discovers. Shared by
|
||||
* {@link #deleteRepository} and {@link #beforeReset}, both of which forget the repository's
|
||||
* record right after calling this and so must not leave its sidecar storage (a Docker
|
||||
* container, for a per-repository sidecar such as Verdaccio) running with nothing left able to
|
||||
* ever release it again.
|
||||
*/
|
||||
private void releaseSidecarStorage(CodeArtifactRepository r, String domain, String repository) {
|
||||
r.getSidecarContainerIds().forEach((format, containerId) ->
|
||||
sidecarRegistry.forFormat(format).ifPresent(manager -> {
|
||||
try {
|
||||
@@ -332,7 +342,6 @@ public class CodeArtifactService implements Resettable {
|
||||
format, domain, repository, e.getMessage());
|
||||
}
|
||||
}));
|
||||
return r;
|
||||
}
|
||||
|
||||
public CodeArtifactRepository describeRepository(String region, String domain, String domainOwner,
|
||||
@@ -750,6 +759,18 @@ public class CodeArtifactService implements Resettable {
|
||||
ref.repository()).getTags();
|
||||
}
|
||||
|
||||
/**
|
||||
* Releases every repository's sidecar storage before {@link #clear} wipes the records that
|
||||
* name it. Without this, an emulator reset forgets which sidecar container backs a repository
|
||||
* without ever stopping it: harmless for Maven's one shared Reposilite instance, but a real
|
||||
* Docker container leak for a per-repository sidecar such as Verdaccio, one abandoned
|
||||
* container per repository that existed at reset time.
|
||||
*/
|
||||
@Override
|
||||
public void beforeReset() {
|
||||
repositories.scanAllAccounts().forEach(r -> releaseSidecarStorage(r, r.getDomainName(), r.getName()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
domains.clear();
|
||||
|
||||
+5
-38
@@ -7,15 +7,13 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.C
|
||||
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.SidecarHealthHelper;
|
||||
import io.quarkus.runtime.ShutdownEvent;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.event.Observes;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Base64;
|
||||
@@ -42,8 +40,7 @@ public class ReposiliteSidecarManager {
|
||||
private static final Logger LOG = Logger.getLogger(ReposiliteSidecarManager.class);
|
||||
private static final String CONTAINER_NAME = "floci-reposilite";
|
||||
private static final int REPOSILITE_PORT = 8080;
|
||||
private static final int HEALTH_POLL_MAX_MS = 30_000;
|
||||
private static final int HEALTH_POLL_INTERVAL_MS = 500;
|
||||
private static final String HEALTH_PATH = "/api/status/health";
|
||||
private static final int GENERATED_SECRET_BYTES = 24;
|
||||
|
||||
private final ContainerBuilder containerBuilder;
|
||||
@@ -93,7 +90,7 @@ public class ReposiliteSidecarManager {
|
||||
/** Base URL of a ready Reposilite instance, starting the managed container if needed. */
|
||||
public synchronized String ensureReady() {
|
||||
if (resolvedUrl != null) {
|
||||
if (containerId == null || probeHealth(resolvedUrl)) {
|
||||
if (containerId == null || SidecarHealthHelper.probeHealth(resolvedUrl, HEALTH_PATH)) {
|
||||
return resolvedUrl;
|
||||
}
|
||||
discardStaleManagedEndpoint();
|
||||
@@ -109,7 +106,7 @@ public class ReposiliteSidecarManager {
|
||||
}
|
||||
resolvedTokenName = token.substring(0, separator);
|
||||
resolvedTokenSecret = token.substring(separator + 1);
|
||||
probeHealth(url);
|
||||
SidecarHealthHelper.probeHealth(url, HEALTH_PATH);
|
||||
resolvedUrl = url;
|
||||
LOG.infov("Using pre-configured Reposilite sidecar URL: {0}", resolvedUrl);
|
||||
return resolvedUrl;
|
||||
@@ -159,41 +156,11 @@ public class ReposiliteSidecarManager {
|
||||
resolvedTokenName = MANAGED_TOKEN_NAME;
|
||||
resolvedTokenSecret = managedSecret;
|
||||
String url = "http://" + endpoint;
|
||||
waitForHealth(url);
|
||||
SidecarHealthHelper.waitForHealth(url, HEALTH_PATH);
|
||||
resolvedUrl = url;
|
||||
LOG.infov("Reposilite sidecar is ready at {0}", resolvedUrl);
|
||||
}
|
||||
|
||||
private boolean probeHealth(String baseUrl) {
|
||||
try {
|
||||
HttpURLConnection connection = (HttpURLConnection) URI.create(baseUrl + "/api/status/health").toURL()
|
||||
.openConnection();
|
||||
connection.setConnectTimeout(500);
|
||||
connection.setReadTimeout(500);
|
||||
return connection.getResponseCode() == 200;
|
||||
} catch (IOException e) {
|
||||
LOG.debugv(e, "Reposilite sidecar health probe failed for {0}", baseUrl);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void waitForHealth(String baseUrl) {
|
||||
long deadline = System.currentTimeMillis() + HEALTH_POLL_MAX_MS;
|
||||
while (System.currentTimeMillis() < deadline) {
|
||||
if (probeHealth(baseUrl)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Thread.sleep(HEALTH_POLL_INTERVAL_MS);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException("Interrupted while waiting for Reposilite sidecar", e);
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("Reposilite sidecar did not become healthy within " + HEALTH_POLL_MAX_MS
|
||||
+ " ms");
|
||||
}
|
||||
|
||||
void onStop(@Observes ShutdownEvent event) {
|
||||
if (containerId == null) {
|
||||
return;
|
||||
|
||||
+177
@@ -0,0 +1,177 @@
|
||||
package io.github.hectorvent.floci.services.codeartifact;
|
||||
|
||||
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.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 io.quarkus.runtime.ShutdownEvent;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.event.Observes;
|
||||
import jakarta.inject.Inject;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* Lazily starts and manages one Verdaccio container per CodeArtifact repository backing the
|
||||
* {@code npm} format. Unlike Reposilite (Maven), Verdaccio has no native concept of multiple
|
||||
* named repositories inside one instance, so npm repositories each get their own container
|
||||
* instead of sharing one the way {@link ReposiliteSidecarManager} does. The actual per-key
|
||||
* container lifecycle (map of running containers, per-key start lock, health poll,
|
||||
* restart-on-unhealthy, stop-all on shutdown) is generic and lives in
|
||||
* {@link PerKeyContainerPool}; this class only knows how to build and configure a Verdaccio
|
||||
* container specifically.
|
||||
*
|
||||
* <p>Each container is configured with open access (no auth, no upstream proxying) since the
|
||||
* real CodeArtifact authorization check happens once at Floci's proxy layer
|
||||
* ({@code CodeArtifactNpmDataPlane}), before a request ever reaches the container; the container
|
||||
* itself is never reachable directly by a client.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class VerdaccioSidecarManager implements RepositorySidecarManager {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(VerdaccioSidecarManager.class);
|
||||
private static final String FORMAT = "npm";
|
||||
private static final int VERDACCIO_PORT = 4873;
|
||||
private static final String HEALTH_PATH = "/-/ping";
|
||||
private static final String CONFIG_REMOTE_DIR = "/verdaccio/conf";
|
||||
private static final String CONFIG_FILE_NAME = "config.yaml";
|
||||
|
||||
/**
|
||||
* Open access for both read and publish (real CodeArtifact auth is already enforced before a
|
||||
* request reaches this container) and no uplinks, matching the Maven proxy's own deliberate
|
||||
* choice not to resolve upstream repositories or external connections.
|
||||
*/
|
||||
private static final String CONFIG_YAML = """
|
||||
storage: /verdaccio/storage/data
|
||||
plugins: /verdaccio/plugins
|
||||
auth:
|
||||
htpasswd:
|
||||
file: /verdaccio/storage/htpasswd
|
||||
uplinks: {}
|
||||
packages:
|
||||
'**':
|
||||
access: $all
|
||||
publish: $all
|
||||
unpublish: $all
|
||||
listen: 0.0.0.0:4873
|
||||
log:
|
||||
type: stdout
|
||||
format: pretty
|
||||
level: warn
|
||||
""";
|
||||
|
||||
private final ContainerBuilder containerBuilder;
|
||||
private final ContainerLifecycleManager lifecycleManager;
|
||||
private final EmulatorConfig config;
|
||||
private final PerKeyContainerPool pool;
|
||||
|
||||
@Inject
|
||||
public VerdaccioSidecarManager(ContainerBuilder containerBuilder, ContainerLifecycleManager lifecycleManager,
|
||||
EmulatorConfig config) {
|
||||
this.containerBuilder = containerBuilder;
|
||||
this.lifecycleManager = lifecycleManager;
|
||||
this.config = config;
|
||||
this.pool = new PerKeyContainerPool(lifecycleManager, HEALTH_PATH);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String format() {
|
||||
return FORMAT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Base URL of a ready Verdaccio instance for this npm repository, starting its container if
|
||||
* this is the first use. Each repository's container is independent, so concurrent first-use
|
||||
* of two different repositories never blocks on each other (unlike Reposilite's one shared
|
||||
* instance, which does need a single lock around every provisioning call).
|
||||
*
|
||||
* @param publicUrl the externally reachable proxy URL for this specific repository (e.g.
|
||||
* {@code http://localhost:4566/codeartifact/npm/<domain>/<repository>/}),
|
||||
* passed to the container as {@code VERDACCIO_PUBLIC_URL} so package metadata
|
||||
* it returns (notably {@code dist.tarball}) points back through Floci's proxy
|
||||
* instead of the container's own internal, unreachable-by-the-client address.
|
||||
*/
|
||||
@Override
|
||||
public String ensureReady(String npmRepositoryId, String publicUrl) {
|
||||
return pool.ensureReady(npmRepositoryId, () -> startContainer(npmRepositoryId, publicUrl));
|
||||
}
|
||||
|
||||
/** Stops and removes the container for one npm repository, if one was ever started. */
|
||||
@Override
|
||||
public void release(String npmRepositoryId) {
|
||||
pool.stopContainer(npmRepositoryId);
|
||||
}
|
||||
|
||||
private PerKeyContainerPool.StartedContainer startContainer(String npmRepositoryId, String publicUrl) {
|
||||
String image = config.services().codeartifact().npmImage();
|
||||
String containerName = ContainerStorageHelper.dockerName(config, "floci-verdaccio-" + npmRepositoryId);
|
||||
lifecycleManager.removeIfExists(containerName);
|
||||
|
||||
// Loopback-only: this container has no auth of its own (open $all access, matching the
|
||||
// Maven proxy's own deliberate choice not to resolve upstreams), so the Bearer check in
|
||||
// CodeArtifactNpmDataPlane 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.
|
||||
ContainerSpec spec = containerBuilder.newContainer(image)
|
||||
.withName(containerName)
|
||||
.withEnv("VERDACCIO_PUBLIC_URL", publicUrl)
|
||||
.withLoopbackPortBinding(VERDACCIO_PORT, 0)
|
||||
.withDockerNetwork(config.services().dockerNetwork())
|
||||
.withEmbeddedDns()
|
||||
.withLogRotation()
|
||||
.build();
|
||||
String containerId = lifecycleManager.create(spec);
|
||||
try {
|
||||
copyConfig(containerId);
|
||||
ContainerInfo info = lifecycleManager.startCreated(containerId, spec);
|
||||
EndpointInfo endpoint = info.getEndpoint(VERDACCIO_PORT);
|
||||
String url = "http://" + endpoint;
|
||||
LOG.infov("Verdaccio sidecar for npm repository {0} is ready at {1}", npmRepositoryId, url);
|
||||
return new PerKeyContainerPool.StartedContainer(containerId, url);
|
||||
} catch (RuntimeException e) {
|
||||
lifecycleManager.stopAndRemove(containerId, null);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies {@code config.yaml} into the created, not yet started, container. A copy rather than
|
||||
* a bind mount for the same reason {@code ContainerLifecycleManager} copies in the CA bundle:
|
||||
* when Floci itself runs in Docker, its own persistent path is not a host path the daemon can
|
||||
* mount into a sibling container.
|
||||
*/
|
||||
private void copyConfig(String containerId) {
|
||||
byte[] content = CONFIG_YAML.getBytes(StandardCharsets.UTF_8);
|
||||
try {
|
||||
ByteArrayOutputStream archive = new ByteArrayOutputStream(content.length + 512);
|
||||
try (TarArchiveOutputStream tar = new TarArchiveOutputStream(archive)) {
|
||||
TarArchiveEntry entry = new TarArchiveEntry(CONFIG_FILE_NAME);
|
||||
entry.setSize(content.length);
|
||||
entry.setMode(0644);
|
||||
tar.putArchiveEntry(entry);
|
||||
tar.write(content);
|
||||
tar.closeArchiveEntry();
|
||||
}
|
||||
lifecycleManager.getDockerClient().copyArchiveToContainerCmd(containerId)
|
||||
.withRemotePath(CONFIG_REMOTE_DIR)
|
||||
.withTarInputStream(new ByteArrayInputStream(archive.toByteArray()))
|
||||
.exec();
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Could not write the Verdaccio config into container " + containerId, e);
|
||||
}
|
||||
}
|
||||
|
||||
void onStop(@Observes ShutdownEvent ignored) {
|
||||
pool.stopAll();
|
||||
}
|
||||
}
|
||||
@@ -879,6 +879,7 @@ floci:
|
||||
enabled: true
|
||||
# maven-url: http://floci-reposilite:8080 # set to skip Reposilite sidecar container management
|
||||
maven-image: "dzikoysk/reposilite:3.6.3"
|
||||
npm-image: "verdaccio/verdaccio:6.10.4"
|
||||
marketplace:
|
||||
enabled: true
|
||||
dms:
|
||||
|
||||
+221
@@ -0,0 +1,221 @@
|
||||
package io.github.hectorvent.floci.core.common.docker;
|
||||
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import io.github.hectorvent.floci.core.common.docker.PerKeyContainerPool.StartedContainer;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
|
||||
/**
|
||||
* Covers the per-key container lifecycle (caching, restart-on-unhealthy, teardown, and the
|
||||
* concurrent-first-use guarantee) that every per-repository sidecar manager needs, independent of
|
||||
* what kind of container it actually starts.
|
||||
*/
|
||||
class PerKeyContainerPoolTest {
|
||||
|
||||
private final ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
|
||||
private final PerKeyContainerPool pool = new PerKeyContainerPool(lifecycleManager, "/ping");
|
||||
private final List<HttpServer> fakeContainers = new ArrayList<>();
|
||||
|
||||
@AfterEach
|
||||
void stopFakeContainers() {
|
||||
fakeContainers.forEach(server -> server.stop(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void ensureReadyStartsAContainerOnFirstUse() throws IOException {
|
||||
String url = startFakePing(200);
|
||||
AtomicInteger starterCalls = new AtomicInteger();
|
||||
|
||||
String result = pool.ensureReady("key-1", () -> {
|
||||
starterCalls.incrementAndGet();
|
||||
return new StartedContainer("container-1", url);
|
||||
});
|
||||
|
||||
assertEquals(url, result);
|
||||
assertEquals(1, starterCalls.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void ensureReadyReturnsTheCachedUrlWithoutCallingTheStarterWhenAlreadyHealthy() throws IOException {
|
||||
String url = startFakePing(200);
|
||||
pool.ensureReady("key-1", () -> new StartedContainer("container-1", url));
|
||||
|
||||
String result = pool.ensureReady("key-1", () -> {
|
||||
throw new AssertionError("starter must not be called for a healthy, already-started container");
|
||||
});
|
||||
|
||||
assertEquals(url, result);
|
||||
verifyNoInteractions(lifecycleManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
void ensureReadyRestartsAnUnhealthyContainer() throws IOException {
|
||||
String staleUrl = startFakePing(200);
|
||||
pool.ensureReady("key-1", () -> new StartedContainer("stale-container", staleUrl));
|
||||
// Only now does the first container stop being healthy: ensureReady's first call for a
|
||||
// key always waits for its just-started container to actually come up, so seeding with a
|
||||
// container that was never reachable at all would hang on that wait instead of exercising
|
||||
// the restart path this test is for.
|
||||
fakeContainers.forEach(server -> server.stop(0));
|
||||
|
||||
String freshUrl = startFakePing(200);
|
||||
String result = pool.ensureReady("key-1", () -> new StartedContainer("fresh-container", freshUrl));
|
||||
|
||||
assertEquals(freshUrl, result);
|
||||
verify(lifecycleManager).stopAndRemove("stale-container", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopContainerStopsAndForgetsATrackedContainer() throws IOException {
|
||||
String url = startFakePing(200);
|
||||
pool.ensureReady("key-1", () -> new StartedContainer("container-1", url));
|
||||
|
||||
pool.stopContainer("key-1");
|
||||
|
||||
verify(lifecycleManager).stopAndRemove("container-1", null);
|
||||
// A second ensureReady must start a new container: the old one is genuinely forgotten,
|
||||
// not just marked unhealthy.
|
||||
AtomicInteger starterCalls = new AtomicInteger();
|
||||
pool.ensureReady("key-1", () -> {
|
||||
starterCalls.incrementAndGet();
|
||||
return new StartedContainer("container-2", url);
|
||||
});
|
||||
assertEquals(1, starterCalls.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopContainerIsANoOpForAnUntrackedOrNullKey() {
|
||||
pool.stopContainer("never-started");
|
||||
pool.stopContainer(null);
|
||||
|
||||
verifyNoInteractions(lifecycleManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopContainerWaitsForAConcurrentFirstUseOfTheSameKeyRatherThanMissingIt() throws Exception {
|
||||
String url = startFakePing(200);
|
||||
CountDownLatch starterEntered = new CountDownLatch(1);
|
||||
CountDownLatch releaseStarter = new CountDownLatch(1);
|
||||
ExecutorService pool2 = Executors.newFixedThreadPool(2);
|
||||
try {
|
||||
Future<String> startFuture = pool2.submit(() -> pool.ensureReady("key-1", () -> {
|
||||
starterEntered.countDown();
|
||||
try {
|
||||
assertTrue(releaseStarter.await(5, TimeUnit.SECONDS));
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
return new StartedContainer("container-1", url);
|
||||
}));
|
||||
// Once this fires, ensureReady is inside its synchronized block, holding key-1's
|
||||
// lock: stopContainer below is guaranteed to block behind it, not race it, with no
|
||||
// sleep needed to line the two threads up.
|
||||
assertTrue(starterEntered.await(5, TimeUnit.SECONDS));
|
||||
|
||||
Future<?> stopFuture = pool2.submit(() -> pool.stopContainer("key-1"));
|
||||
releaseStarter.countDown();
|
||||
|
||||
assertEquals(url, startFuture.get(5, TimeUnit.SECONDS));
|
||||
stopFuture.get(5, TimeUnit.SECONDS);
|
||||
} finally {
|
||||
pool2.shutdown();
|
||||
assertTrue(pool2.awaitTermination(30, TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
// The container was tracked (by ensureReady, before stopContainer could run) and then
|
||||
// removed (by stopContainer, once it got the lock): the race does not orphan it.
|
||||
verify(lifecycleManager).stopAndRemove("container-1", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopAllStopsEveryTrackedContainerAndForgetsThem() throws IOException {
|
||||
String urlA = startFakePing(200);
|
||||
String urlB = startFakePing(200);
|
||||
pool.ensureReady("key-a", () -> new StartedContainer("container-a", urlA));
|
||||
pool.ensureReady("key-b", () -> new StartedContainer("container-b", urlB));
|
||||
|
||||
pool.stopAll();
|
||||
|
||||
verify(lifecycleManager).stopAndRemove("container-a", null);
|
||||
verify(lifecycleManager).stopAndRemove("container-b", null);
|
||||
verifyNoMoreInteractions(lifecycleManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopAllIsANoOpWhenNothingWasEverStarted() {
|
||||
pool.stopAll();
|
||||
|
||||
verifyNoInteractions(lifecycleManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
void concurrentFirstUseOfTheSameKeyOnlyStartsOneContainer() throws Exception {
|
||||
String url = startFakePing(200);
|
||||
AtomicInteger starterCalls = new AtomicInteger();
|
||||
int attempts = 8;
|
||||
ExecutorService pool2 = Executors.newFixedThreadPool(attempts);
|
||||
CountDownLatch ready = new CountDownLatch(attempts);
|
||||
CountDownLatch go = new CountDownLatch(1);
|
||||
List<String> results = new CopyOnWriteArrayList<>();
|
||||
try {
|
||||
for (int i = 0; i < attempts; i++) {
|
||||
pool2.submit(() -> {
|
||||
ready.countDown();
|
||||
try {
|
||||
go.await();
|
||||
results.add(pool.ensureReady("shared-key", () -> {
|
||||
starterCalls.incrementAndGet();
|
||||
return new StartedContainer("container-shared", url);
|
||||
}));
|
||||
} catch (InterruptedException ignored) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
});
|
||||
}
|
||||
ready.await(5, TimeUnit.SECONDS);
|
||||
go.countDown();
|
||||
} finally {
|
||||
pool2.shutdown();
|
||||
assertTrue(pool2.awaitTermination(30, TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
assertEquals(1, starterCalls.get());
|
||||
assertEquals(attempts, results.size());
|
||||
results.forEach(r -> assertEquals(url, r));
|
||||
}
|
||||
|
||||
private String startFakePing(int status) throws IOException {
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.createContext("/ping", exchange -> {
|
||||
byte[] bytes = "{}".getBytes(StandardCharsets.UTF_8);
|
||||
exchange.sendResponseHeaders(status, bytes.length);
|
||||
try (OutputStream out = exchange.getResponseBody()) {
|
||||
out.write(bytes);
|
||||
}
|
||||
});
|
||||
server.start();
|
||||
fakeContainers.add(server);
|
||||
return "http://127.0.0.1:" + server.getAddress().getPort();
|
||||
}
|
||||
}
|
||||
+15
@@ -238,6 +238,21 @@ class CodeArtifactIntegrationTest {
|
||||
.then().statusCode(400).body("__type", equalTo("ValidationException"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Proves the npm route is genuinely wired into the running instance ({@code @Observes
|
||||
* Router} registration, the {@code ServiceConfigAccess} enablement check, and the token
|
||||
* check), the one thing {@code CodeArtifactNpmDataPlaneTest} cannot: it builds the class by
|
||||
* hand rather than through CDI, so it proves nothing about whether the observer actually
|
||||
* registers. A missing token is rejected before the named domain or repository is looked up,
|
||||
* so this needs neither to exist, and needs no Docker.
|
||||
*/
|
||||
@Test
|
||||
void npmEndpointRejectsAMissingTokenWithoutADomainOrRepositoryExisting() {
|
||||
given().get("/codeartifact/npm/no-such-domain/no-such-repo/lodash")
|
||||
.then().statusCode(401)
|
||||
.header("WWW-Authenticate", equalTo("Bearer"));
|
||||
}
|
||||
|
||||
private static String sha256Hex(byte[] content) {
|
||||
try {
|
||||
return SigV4RequestValidator.sha256Hex(content);
|
||||
|
||||
+255
@@ -0,0 +1,255 @@
|
||||
package io.github.hectorvent.floci.services.codeartifact;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.ServiceConfigAccess;
|
||||
import io.github.hectorvent.floci.services.codeartifact.CodeArtifactService.AuthorizationTokenScope;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.http.HttpClient;
|
||||
import io.vertx.core.http.HttpClientRequest;
|
||||
import io.vertx.core.http.HttpClientResponse;
|
||||
import io.vertx.core.http.HttpMethod;
|
||||
import io.vertx.core.http.HttpServer;
|
||||
import io.vertx.core.http.RequestOptions;
|
||||
import io.vertx.ext.web.Router;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Unit tests for routing npm registry traffic through Floci's data plane to the per-repository
|
||||
* Verdaccio container. {@link VerdaccioSidecarManager} is mocked throughout, so no container ever
|
||||
* actually starts; the round-trip proxy tests point it at a fake upstream HTTP server instead. The
|
||||
* real container-creation path (config injection, {@code VERDACCIO_PUBLIC_URL}) is covered by the
|
||||
* Docker-gated npm client integration test.
|
||||
*/
|
||||
class CodeArtifactNpmDataPlaneTest {
|
||||
|
||||
private static final String DOMAIN = "dom";
|
||||
private static final String REPOSITORY = "repo";
|
||||
private static final String NPM_REPOSITORY_ID = "npm-repo-id-1";
|
||||
|
||||
private Vertx vertx;
|
||||
private HttpServer upstream;
|
||||
private HttpServer dataPlane;
|
||||
private HttpClient client;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
vertx = Vertx.vertx();
|
||||
client = vertx.createHttpClient();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() throws Exception {
|
||||
if (dataPlane != null) {
|
||||
dataPlane.close().toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
|
||||
}
|
||||
if (upstream != null) {
|
||||
upstream.close().toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
|
||||
}
|
||||
vertx.close().toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
void requestForParsesDomainRepositoryAndRest() {
|
||||
CodeArtifactNpmDataPlane.NpmRequest request =
|
||||
CodeArtifactNpmDataPlane.requestFor("/codeartifact/npm/dom/repo/lodash").orElseThrow();
|
||||
assertEquals("dom", request.domain());
|
||||
assertEquals("repo", request.repository());
|
||||
assertEquals("/lodash", request.rest());
|
||||
}
|
||||
|
||||
@Test
|
||||
void requestForDefaultsToRootForTheBareRepositoryPath() {
|
||||
CodeArtifactNpmDataPlane.NpmRequest request =
|
||||
CodeArtifactNpmDataPlane.requestFor("/codeartifact/npm/dom/repo").orElseThrow();
|
||||
assertEquals("/", request.rest());
|
||||
}
|
||||
|
||||
@Test
|
||||
void requestForRejectsAPathOutsideTheNpmPrefix() {
|
||||
assertTrue(CodeArtifactNpmDataPlane.requestFor("/codeartifact/maven/dom/repo/x").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingTokenIsRejectedWithoutStartingAContainer() throws Exception {
|
||||
CodeArtifactService service = mock(CodeArtifactService.class);
|
||||
VerdaccioSidecarManager verdaccioManager = mock(VerdaccioSidecarManager.class);
|
||||
startDataPlane(service, verdaccioManager);
|
||||
|
||||
HttpResponse response = get("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/lodash", null);
|
||||
|
||||
assertEquals(401, response.statusCode());
|
||||
assertEquals("Bearer", response.headers().get("www-authenticate"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void disabledCodeArtifactServiceIsNotServedByThisRoute() throws Exception {
|
||||
CodeArtifactService service = mock(CodeArtifactService.class);
|
||||
VerdaccioSidecarManager verdaccioManager = mock(VerdaccioSidecarManager.class);
|
||||
startDataPlane(service, verdaccioManager, false);
|
||||
|
||||
HttpResponse response = get("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/lodash", "good-token");
|
||||
|
||||
assertEquals(404, response.statusCode());
|
||||
verifyNoInteractions(service, verdaccioManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidTokenIsRejected() throws Exception {
|
||||
CodeArtifactService service = mock(CodeArtifactService.class);
|
||||
when(service.resolveAuthorizationToken(anyString(), anyString())).thenReturn(Optional.empty());
|
||||
VerdaccioSidecarManager verdaccioManager = mock(VerdaccioSidecarManager.class);
|
||||
startDataPlane(service, verdaccioManager);
|
||||
|
||||
HttpResponse response = get("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/lodash", "not-a-real-token");
|
||||
|
||||
assertEquals(401, response.statusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownRepositoryIsNotFound() throws Exception {
|
||||
CodeArtifactService service = mock(CodeArtifactService.class);
|
||||
when(service.resolveAuthorizationToken("good-token", DOMAIN))
|
||||
.thenReturn(Optional.of(new AuthorizationTokenScope("000000000000", "us-east-1")));
|
||||
when(service.ensureFormatContainerId("npm", "us-east-1", DOMAIN, "000000000000", REPOSITORY))
|
||||
.thenThrow(new AwsException("ResourceNotFoundException", "no such repository", 404));
|
||||
VerdaccioSidecarManager verdaccioManager = mock(VerdaccioSidecarManager.class);
|
||||
startDataPlane(service, verdaccioManager);
|
||||
|
||||
HttpResponse response = get("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/lodash", "good-token");
|
||||
|
||||
assertEquals(404, response.statusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aValidRequestIsProxiedToTheRepositorysContainerWithTheClientAuthorizationStripped() throws Exception {
|
||||
AtomicReference<String> upstreamPath = new AtomicReference<>();
|
||||
AtomicReference<String> upstreamAuthHeader = new AtomicReference<>();
|
||||
upstream = vertx.createHttpServer()
|
||||
.requestHandler(request -> {
|
||||
upstreamPath.set(request.path() + (request.query() != null ? "?" + request.query() : ""));
|
||||
upstreamAuthHeader.set(request.getHeader("Authorization"));
|
||||
request.response().putHeader("Content-Type", "application/json")
|
||||
.setStatusCode(200).end("{\"name\":\"lodash\"}");
|
||||
})
|
||||
.listen(0, "127.0.0.1")
|
||||
.toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
|
||||
|
||||
CodeArtifactService service = mock(CodeArtifactService.class);
|
||||
when(service.resolveAuthorizationToken("good-token", DOMAIN))
|
||||
.thenReturn(Optional.of(new AuthorizationTokenScope("000000000000", "us-east-1")));
|
||||
when(service.ensureFormatContainerId("npm", "us-east-1", DOMAIN, "000000000000", REPOSITORY))
|
||||
.thenReturn(NPM_REPOSITORY_ID);
|
||||
VerdaccioSidecarManager verdaccioManager = mock(VerdaccioSidecarManager.class);
|
||||
when(verdaccioManager.ensureReady(eq(NPM_REPOSITORY_ID), anyString()))
|
||||
.thenReturn("http://127.0.0.1:" + upstream.actualPort());
|
||||
startDataPlane(service, verdaccioManager);
|
||||
|
||||
HttpResponse response = get("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/lodash", "good-token");
|
||||
|
||||
assertEquals(200, response.statusCode());
|
||||
assertEquals("{\"name\":\"lodash\"}", response.body());
|
||||
assertEquals("/lodash", upstreamPath.get());
|
||||
assertNull(upstreamAuthHeader.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPublishPutStreamsTheBodyThrough() throws Exception {
|
||||
AtomicReference<String> upstreamBody = new AtomicReference<>();
|
||||
AtomicReference<HttpMethod> upstreamMethod = new AtomicReference<>();
|
||||
upstream = vertx.createHttpServer()
|
||||
.requestHandler(request -> {
|
||||
upstreamMethod.set(request.method());
|
||||
request.bodyHandler(body -> {
|
||||
upstreamBody.set(body.toString());
|
||||
request.response().setStatusCode(201).end();
|
||||
});
|
||||
})
|
||||
.listen(0, "127.0.0.1")
|
||||
.toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
|
||||
|
||||
CodeArtifactService service = mock(CodeArtifactService.class);
|
||||
when(service.resolveAuthorizationToken("good-token", DOMAIN))
|
||||
.thenReturn(Optional.of(new AuthorizationTokenScope("000000000000", "us-east-1")));
|
||||
when(service.ensureFormatContainerId("npm", "us-east-1", DOMAIN, "000000000000", REPOSITORY))
|
||||
.thenReturn(NPM_REPOSITORY_ID);
|
||||
VerdaccioSidecarManager verdaccioManager = mock(VerdaccioSidecarManager.class);
|
||||
when(verdaccioManager.ensureReady(eq(NPM_REPOSITORY_ID), anyString()))
|
||||
.thenReturn("http://127.0.0.1:" + upstream.actualPort());
|
||||
startDataPlane(service, verdaccioManager);
|
||||
|
||||
HttpResponse response = put("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/my-pkg", "good-token",
|
||||
"{\"name\":\"my-pkg\"}");
|
||||
|
||||
assertEquals(201, response.statusCode());
|
||||
assertEquals(HttpMethod.PUT, upstreamMethod.get());
|
||||
assertEquals("{\"name\":\"my-pkg\"}", upstreamBody.get());
|
||||
}
|
||||
|
||||
private void startDataPlane(CodeArtifactService service, VerdaccioSidecarManager verdaccioManager)
|
||||
throws Exception {
|
||||
startDataPlane(service, verdaccioManager, true);
|
||||
}
|
||||
|
||||
private void startDataPlane(CodeArtifactService service, VerdaccioSidecarManager verdaccioManager,
|
||||
boolean codeArtifactEnabled) throws Exception {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
|
||||
ServiceConfigAccess serviceConfigAccess = mock(ServiceConfigAccess.class);
|
||||
when(serviceConfigAccess.isEnabled("codeartifact")).thenReturn(codeArtifactEnabled);
|
||||
Router router = Router.router(vertx);
|
||||
new CodeArtifactNpmDataPlane(service, verdaccioManager, serviceConfigAccess, config, vertx).register(router);
|
||||
dataPlane = vertx.createHttpServer().requestHandler(router)
|
||||
.listen(0, "127.0.0.1")
|
||||
.toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
private HttpResponse get(String path, String bearerToken) throws Exception {
|
||||
return request(HttpMethod.GET, path, bearerToken, null);
|
||||
}
|
||||
|
||||
private HttpResponse put(String path, String bearerToken, String body) throws Exception {
|
||||
return request(HttpMethod.PUT, path, bearerToken, body);
|
||||
}
|
||||
|
||||
private HttpResponse request(HttpMethod method, String path, String bearerToken, String body) throws Exception {
|
||||
RequestOptions options = new RequestOptions()
|
||||
.setHost("127.0.0.1")
|
||||
.setPort(dataPlane.actualPort())
|
||||
.setMethod(method)
|
||||
.setURI(path);
|
||||
HttpClientRequest req = client.request(options).toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
|
||||
if (bearerToken != null) {
|
||||
req.putHeader("Authorization", "Bearer " + bearerToken);
|
||||
}
|
||||
HttpClientResponse resp = (body != null
|
||||
? req.send(body)
|
||||
: req.send())
|
||||
.toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
|
||||
String responseBody = resp.body().toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS)
|
||||
.toString(StandardCharsets.UTF_8);
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
resp.headers().forEach(h -> headers.put(h.getKey().toLowerCase(), h.getValue()));
|
||||
return new HttpResponse(resp.statusCode(), responseBody, headers);
|
||||
}
|
||||
|
||||
private record HttpResponse(int statusCode, String body, Map<String, String> headers) {}
|
||||
}
|
||||
+282
@@ -0,0 +1,282 @@
|
||||
package io.github.hectorvent.floci.services.codeartifact;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import org.hamcrest.Matcher;
|
||||
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.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.anyOf;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Runs a real npm-shaped publish/fetch round trip against a live, per-repository Verdaccio
|
||||
* container, started by {@link VerdaccioSidecarManager} exactly as in production. The publish
|
||||
* body is a real request the actual npm CLI sent during manual verification (captured with a tiny
|
||||
* Node.js capture server, see {@code npm-publish-fixture.json}), not a hand-rolled approximation,
|
||||
* so this test exercises the exact envelope shape (including a real gzip tarball attachment) a
|
||||
* real {@code npm publish} produces. The full round trip was additionally verified by hand with
|
||||
* the real npm CLI against a live Floci instance ({@code GetAuthorizationToken} through to
|
||||
* {@code npm install}), which is what first caught that {@code VERDACCIO_PUBLIC_URL} is required:
|
||||
* without it, {@code dist.tarball} in the metadata response points at the container's own
|
||||
* internal, client-unreachable address instead of back through this proxy.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(CodeArtifactNpmSidecarProfile.class)
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class CodeArtifactNpmDockerIntegrationTest {
|
||||
|
||||
private static final String AUTH = "AWS4-HMAC-SHA256 Credential=AKID/20260904/us-east-1/codeartifact/aws4_request";
|
||||
private static final String DOMAIN = "npm-sidecar-domain";
|
||||
private static final String REPO = "npm-sidecar-repo";
|
||||
private static final String PACKAGE_NAME = "floci-verdaccio-spike";
|
||||
|
||||
private static String bearerToken;
|
||||
private static byte[] publishFixture;
|
||||
private static byte[] expectedTarballBytes;
|
||||
|
||||
@BeforeAll
|
||||
static void setUp() throws IOException {
|
||||
CodeArtifactNpmSidecarProfile.requireDockerAndTheSidecarImage();
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
try (InputStream in = CodeArtifactNpmDockerIntegrationTest.class
|
||||
.getResourceAsStream("/codeartifact/npm-publish-fixture.json")) {
|
||||
publishFixture = in.readAllBytes();
|
||||
}
|
||||
JsonNode fixture = mapper.readTree(publishFixture);
|
||||
String base64Tarball = fixture.path("_attachments").path(PACKAGE_NAME + "-1.0.0.tgz").path("data").asText();
|
||||
expectedTarballBytes = Base64.getDecoder().decode(base64Tarball);
|
||||
}
|
||||
|
||||
@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 publishThenFetchRoundTripsTheRealNpmEnvelope() throws IOException {
|
||||
given().header("Authorization", "Bearer " + bearerToken).contentType("application/json")
|
||||
.body(publishFixture)
|
||||
.put("/codeartifact/npm/" + DOMAIN + "/" + REPO + "/" + PACKAGE_NAME)
|
||||
.then().statusCode(anyOf201Or200());
|
||||
|
||||
String metadataBody = given().header("Authorization", "Bearer " + bearerToken)
|
||||
.get("/codeartifact/npm/" + DOMAIN + "/" + REPO + "/" + PACKAGE_NAME)
|
||||
.then().statusCode(200)
|
||||
.extract().asString();
|
||||
String tarballUrl = new ObjectMapper().readTree(metadataBody)
|
||||
.path("versions").path("1.0.0").path("dist").path("tarball").asText();
|
||||
|
||||
// Pins VERDACCIO_PUBLIC_URL: without it this would be the container's own internal,
|
||||
// client-unreachable address instead of a URL back through this same proxy.
|
||||
assertTrue(tarballUrl.startsWith("http://localhost:4566/codeartifact/npm/" + DOMAIN + "/" + REPO + "/"),
|
||||
"tarball URL must point back through the proxy, was: " + tarballUrl);
|
||||
|
||||
byte[] fetchedTarball = given().header("Authorization", "Bearer " + bearerToken)
|
||||
.get(tarballUrl.substring("http://localhost:4566".length()))
|
||||
.then().statusCode(200)
|
||||
.extract().asByteArray();
|
||||
assertArrayEquals(expectedTarballBytes, fetchedTarball);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void missingPackageAndMissingRepositoryAreNotFound() {
|
||||
given().header("Authorization", "Bearer " + bearerToken)
|
||||
.get("/codeartifact/npm/" + DOMAIN + "/" + REPO + "/does-not-exist")
|
||||
.then().statusCode(404);
|
||||
|
||||
given().header("Authorization", "Bearer " + bearerToken)
|
||||
.get("/codeartifact/npm/" + DOMAIN + "/no-such-repo/does-not-exist")
|
||||
.then().statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void repositoriesAreIsolatedFromEachOther() {
|
||||
given().contentType("application/json").header("Authorization", AUTH).body("{}")
|
||||
.post("/v1/repository?domain=" + DOMAIN + "&repository=other-repo")
|
||||
.then().statusCode(200);
|
||||
|
||||
given().header("Authorization", "Bearer " + bearerToken)
|
||||
.get("/codeartifact/npm/" + DOMAIN + "/other-repo/" + PACKAGE_NAME)
|
||||
.then().statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void missingOrWrongDomainTokensAreUnauthorized() {
|
||||
given().get("/codeartifact/npm/" + DOMAIN + "/" + REPO + "/" + PACKAGE_NAME)
|
||||
.then().statusCode(401)
|
||||
.header("WWW-Authenticate", startsWith("Bearer"));
|
||||
|
||||
given().header("Authorization", "Bearer not-a-real-token")
|
||||
.get("/codeartifact/npm/" + DOMAIN + "/" + REPO + "/" + PACKAGE_NAME)
|
||||
.then().statusCode(401);
|
||||
|
||||
given().contentType("application/json").header("Authorization", AUTH).body("{}")
|
||||
.post("/v1/domain?domain=other-npm-token-domain")
|
||||
.then().statusCode(200);
|
||||
String otherDomainToken = given().header("Authorization", AUTH)
|
||||
.post("/v1/authorization-token?domain=other-npm-token-domain")
|
||||
.then().statusCode(200)
|
||||
.extract().jsonPath().getString("authorizationToken");
|
||||
|
||||
given().header("Authorization", "Bearer " + otherDomainToken)
|
||||
.get("/codeartifact/npm/" + DOMAIN + "/" + REPO + "/" + PACKAGE_NAME)
|
||||
.then().statusCode(401);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void concurrentFirstUseOfANewRepositoryOnlyStartsOneContainer() throws InterruptedException {
|
||||
given().contentType("application/json").header("Authorization", AUTH).body("{}")
|
||||
.post("/v1/repository?domain=" + DOMAIN + "&repository=concurrent-repo")
|
||||
.then().statusCode(200);
|
||||
|
||||
int attempts = 8;
|
||||
ExecutorService pool = Executors.newFixedThreadPool(attempts);
|
||||
CountDownLatch ready = new CountDownLatch(attempts);
|
||||
CountDownLatch go = new CountDownLatch(1);
|
||||
AtomicInteger notFoundResponses = new AtomicInteger();
|
||||
try {
|
||||
for (int i = 0; i < attempts; i++) {
|
||||
pool.submit(() -> {
|
||||
ready.countDown();
|
||||
try {
|
||||
go.await();
|
||||
given().header("Authorization", "Bearer " + bearerToken)
|
||||
.get("/codeartifact/npm/" + DOMAIN + "/concurrent-repo/does-not-exist")
|
||||
.then().statusCode(404);
|
||||
notFoundResponses.incrementAndGet();
|
||||
} catch (InterruptedException ignored) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
});
|
||||
}
|
||||
ready.await(5, TimeUnit.SECONDS);
|
||||
go.countDown();
|
||||
} finally {
|
||||
pool.shutdown();
|
||||
assertTrue(pool.awaitTermination(30, TimeUnit.SECONDS));
|
||||
}
|
||||
assertEquals(attempts, notFoundResponses.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void recreatingASameNamedRepositoryDoesNotInheritThePreviousOnesPackages() {
|
||||
given().contentType("application/json").header("Authorization", AUTH).body("{}")
|
||||
.post("/v1/repository?domain=" + DOMAIN + "&repository=reused-name")
|
||||
.then().statusCode(200);
|
||||
given().header("Authorization", "Bearer " + bearerToken).contentType("application/json")
|
||||
.body(publishFixture)
|
||||
.put("/codeartifact/npm/" + DOMAIN + "/reused-name/" + PACKAGE_NAME)
|
||||
.then().statusCode(anyOf201Or200());
|
||||
|
||||
given().header("Authorization", AUTH)
|
||||
.delete("/v1/repository?domain=" + DOMAIN + "&repository=reused-name")
|
||||
.then().statusCode(200);
|
||||
given().contentType("application/json").header("Authorization", AUTH).body("{}")
|
||||
.post("/v1/repository?domain=" + DOMAIN + "&repository=reused-name")
|
||||
.then().statusCode(200);
|
||||
|
||||
given().header("Authorization", "Bearer " + bearerToken)
|
||||
.get("/codeartifact/npm/" + DOMAIN + "/reused-name/" + PACKAGE_NAME)
|
||||
.then().statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void aDomainCreatedInANonDefaultRegionIsServedThroughTheTokensOwnRegion() {
|
||||
String nonDefaultRegionAuth =
|
||||
"AWS4-HMAC-SHA256 Credential=AKID/20260904/us-west-2/codeartifact/aws4_request";
|
||||
String domain = "npm-sidecar-non-default-region-domain";
|
||||
|
||||
given().contentType("application/json").header("Authorization", nonDefaultRegionAuth).body("{}")
|
||||
.post("/v1/domain?domain=" + domain)
|
||||
.then().statusCode(200);
|
||||
given().contentType("application/json").header("Authorization", nonDefaultRegionAuth).body("{}")
|
||||
.post("/v1/repository?domain=" + domain + "&repository=" + REPO)
|
||||
.then().statusCode(200);
|
||||
String token = given().header("Authorization", nonDefaultRegionAuth)
|
||||
.post("/v1/authorization-token?domain=" + domain)
|
||||
.then().statusCode(200)
|
||||
.extract().jsonPath().getString("authorizationToken");
|
||||
|
||||
given().header("Authorization", "Bearer " + token).contentType("application/json")
|
||||
.body(publishFixture)
|
||||
.put("/codeartifact/npm/" + domain + "/" + REPO + "/" + PACKAGE_NAME)
|
||||
.then().statusCode(anyOf201Or200());
|
||||
|
||||
given().header("Authorization", "Bearer " + token)
|
||||
.get("/codeartifact/npm/" + domain + "/" + REPO + "/" + PACKAGE_NAME)
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void aDomainCreatedUnderANonDefaultAccountIsServedThroughTheTokensOwnAccount() {
|
||||
String otherAccountAuth = "AWS4-HMAC-SHA256 Credential=111122223333/20260904/us-east-1/codeartifact/aws4_request";
|
||||
String domain = "npm-sidecar-cross-account-domain";
|
||||
|
||||
given().contentType("application/json").header("Authorization", otherAccountAuth).body("{}")
|
||||
.post("/v1/domain?domain=" + domain)
|
||||
.then().statusCode(200);
|
||||
given().contentType("application/json").header("Authorization", otherAccountAuth).body("{}")
|
||||
.post("/v1/repository?domain=" + domain + "&repository=" + REPO)
|
||||
.then().statusCode(200);
|
||||
String token = given().header("Authorization", otherAccountAuth)
|
||||
.post("/v1/authorization-token?domain=" + domain)
|
||||
.then().statusCode(200)
|
||||
.extract().jsonPath().getString("authorizationToken");
|
||||
|
||||
given().header("Authorization", "Bearer " + token).contentType("application/json")
|
||||
.body(publishFixture)
|
||||
.put("/codeartifact/npm/" + domain + "/" + REPO + "/" + PACKAGE_NAME)
|
||||
.then().statusCode(anyOf201Or200());
|
||||
|
||||
given().header("Authorization", "Bearer " + token)
|
||||
.get("/codeartifact/npm/" + domain + "/" + REPO + "/" + PACKAGE_NAME)
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
private static Matcher<Integer> anyOf201Or200() {
|
||||
return anyOf(is(200), is(201));
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package io.github.hectorvent.floci.services.codeartifact;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import org.eclipse.microprofile.config.ConfigProvider;
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Shared by every CodeArtifact integration test that needs a real Verdaccio sidecar container,
|
||||
* started by {@link VerdaccioSidecarManager} exactly as in production. Namespaces containers as
|
||||
* {@code floci-codeartifact-npm-test-*} so the manager's remove-before-start never touches a
|
||||
* developer's real running Floci. Needs Docker; skipped without it. Locally it is also skipped
|
||||
* when the pinned image is not present, so a developer without registry access is not stuck; in
|
||||
* CI a missing image is a failure, since a vanished tag must not pass silently.
|
||||
*/
|
||||
public class CodeArtifactNpmSidecarProfile implements QuarkusTestProfile {
|
||||
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Map.of("floci.docker.resource-namespace", "codeartifact-npm-test");
|
||||
}
|
||||
|
||||
public static void requireDockerAndTheSidecarImage() {
|
||||
Assumptions.assumeTrue(isDockerAvailable(), "Docker daemon must be available for CodeArtifact npm "
|
||||
+ "sidecar integration tests");
|
||||
String image = ConfigProvider.getConfig().getValue("floci.services.codeartifact.npm-image", String.class);
|
||||
Assumptions.assumeTrue(imageUsable(image), "Verdaccio sidecar image " + image + " is not present locally");
|
||||
}
|
||||
|
||||
private static boolean isDockerAvailable() {
|
||||
return run("docker", "version", "--format", "{{.Server.Version}}");
|
||||
}
|
||||
|
||||
private static boolean imageUsable(String image) {
|
||||
if ("true".equals(System.getenv("CI"))) {
|
||||
return true;
|
||||
}
|
||||
return run("docker", "image", "inspect", image);
|
||||
}
|
||||
|
||||
private static boolean run(String... command) {
|
||||
try {
|
||||
Process process = new ProcessBuilder(command).redirectErrorStream(true).start();
|
||||
return process.waitFor() == 0;
|
||||
} catch (Exception ignored) {
|
||||
// Docker (or the image) isn't available; the caller turns this into a skipped test,
|
||||
// not a failure, so there is nothing more useful to log here.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
+75
-7
@@ -49,6 +49,7 @@ class CodeArtifactServiceTest {
|
||||
private CodeArtifactService service;
|
||||
private RegionResolver regionResolver;
|
||||
private AccountAwareStorageBackend<CodeArtifactRepository> repoStore;
|
||||
private VerdaccioSidecarManager verdaccioManager;
|
||||
private ReposiliteSidecarClient reposiliteClient;
|
||||
|
||||
@BeforeEach
|
||||
@@ -67,10 +68,12 @@ class CodeArtifactServiceTest {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
|
||||
|
||||
verdaccioManager = mock(VerdaccioSidecarManager.class);
|
||||
when(verdaccioManager.format()).thenReturn("npm");
|
||||
reposiliteClient = mock(ReposiliteSidecarClient.class);
|
||||
when(reposiliteClient.format()).thenReturn("maven");
|
||||
service = new CodeArtifactService(domainStore, repoStore, packageVersionStore, regionResolver, config,
|
||||
true, null, new CodeArtifactSidecarRegistry(List.of(reposiliteClient)));
|
||||
true, null, new CodeArtifactSidecarRegistry(List.of(verdaccioManager, reposiliteClient)));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------- domains
|
||||
@@ -297,7 +300,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"), created.getSidecarContainerIds().keySet());
|
||||
assertEquals(Set.of("maven", "npm"), created.getSidecarContainerIds().keySet());
|
||||
created.getSidecarContainerIds().values()
|
||||
.forEach(id -> assertTrue(id != null && !id.isBlank()));
|
||||
}
|
||||
@@ -310,9 +313,12 @@ class CodeArtifactServiceTest {
|
||||
service.deleteRepository(REGION, "dom", null, "repo");
|
||||
CodeArtifactRepository recreated = service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
|
||||
|
||||
assertTrue(!first.getSidecarContainerIds().get("maven").equals(recreated.getSidecarContainerIds().get("maven")),
|
||||
"a recreated repository must never reuse the previous one's container id for a format, "
|
||||
+ "or it would inherit its leftover artifacts");
|
||||
for (String format : Set.of("maven", "npm")) {
|
||||
assertTrue(
|
||||
!first.getSidecarContainerIds().get(format).equals(recreated.getSidecarContainerIds().get(format)),
|
||||
"a recreated repository must never reuse the previous one's container id for a format, "
|
||||
+ "or it would inherit its leftover artifacts");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -331,6 +337,24 @@ class CodeArtifactServiceTest {
|
||||
service.describeRepository(REGION, "dom", null, "repo").getSidecarContainerIds().get("maven"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void ensureFormatContainerIdBackfillsALegacyRepositoryMissingTheNpmFormat() {
|
||||
service.createDomain(REGION, "dom", null, Map.of());
|
||||
CodeArtifactRepository created = service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
|
||||
// Simulates a repository persisted before the npm format's proxy existed: Jackson would
|
||||
// deserialize a missing map entry exactly like this.
|
||||
Map<String, String> withoutNpm = new HashMap<>(created.getSidecarContainerIds());
|
||||
withoutNpm.remove("npm");
|
||||
created.setSidecarContainerIds(withoutNpm);
|
||||
repoStore.putForAccount(ACCOUNT_ID, REGION + "::dom::repo", created);
|
||||
|
||||
String backfilled = service.ensureFormatContainerId("npm", REGION, "dom", null, "repo");
|
||||
|
||||
assertTrue(backfilled != null && !backfilled.isBlank());
|
||||
assertEquals(backfilled,
|
||||
service.describeRepository(REGION, "dom", null, "repo").getSidecarContainerIds().get("npm"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void ensureFormatContainerIdIsIdempotentForAnAlreadyAssignedFormat() {
|
||||
service.createDomain(REGION, "dom", null, Map.of());
|
||||
@@ -341,6 +365,16 @@ class CodeArtifactServiceTest {
|
||||
assertEquals(created.getSidecarContainerIds().get("maven"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void ensureFormatContainerIdIsIdempotentForAnAlreadyAssignedNpmFormat() {
|
||||
service.createDomain(REGION, "dom", null, Map.of());
|
||||
CodeArtifactRepository created = service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
|
||||
|
||||
String result = service.ensureFormatContainerId("npm", REGION, "dom", null, "repo");
|
||||
|
||||
assertEquals(created.getSidecarContainerIds().get("npm"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void ensureFormatContainerIdRequiresExistingRepository() {
|
||||
service.createDomain(REGION, "dom", null, Map.of());
|
||||
@@ -356,21 +390,55 @@ class CodeArtifactServiceTest {
|
||||
|
||||
service.deleteRepository(REGION, "dom", null, "repo");
|
||||
|
||||
verify(verdaccioManager).release(created.getSidecarContainerIds().get("npm"));
|
||||
verify(reposiliteClient).release(created.getSidecarContainerIds().get("maven"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteRepositorySucceedsEvenWhenSidecarReleaseFails() {
|
||||
void beforeResetReleasesEveryRepositorysSidecarStorageAcrossEveryAccount() {
|
||||
service.createDomain(REGION, "dom", null, Map.of());
|
||||
CodeArtifactRepository first = service.createRepository(REGION, "dom", null, "repo-1", null, null, Map.of());
|
||||
CodeArtifactRepository second = service.createRepository(REGION, "dom", null, "repo-2", null, null, Map.of());
|
||||
|
||||
service.beforeReset();
|
||||
|
||||
verify(verdaccioManager).release(first.getSidecarContainerIds().get("npm"));
|
||||
verify(reposiliteClient).release(first.getSidecarContainerIds().get("maven"));
|
||||
verify(verdaccioManager).release(second.getSidecarContainerIds().get("npm"));
|
||||
verify(reposiliteClient).release(second.getSidecarContainerIds().get("maven"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void beforeResetToleratesASidecarReleaseFailureAndStillReleasesTheRest() {
|
||||
service.createDomain(REGION, "dom", null, Map.of());
|
||||
CodeArtifactRepository first = service.createRepository(REGION, "dom", null, "repo-1", null, null, Map.of());
|
||||
CodeArtifactRepository second = service.createRepository(REGION, "dom", null, "repo-2", null, null, Map.of());
|
||||
doThrow(new IllegalStateException("Reposilite unreachable")).when(reposiliteClient)
|
||||
.release(first.getSidecarContainerIds().get("maven"));
|
||||
|
||||
// A reset must not abort partway through and leave later repositories' containers running
|
||||
// just because one repository's release failed.
|
||||
service.beforeReset();
|
||||
|
||||
verify(reposiliteClient).release(second.getSidecarContainerIds().get("maven"));
|
||||
verify(verdaccioManager).release(first.getSidecarContainerIds().get("npm"));
|
||||
verify(verdaccioManager).release(second.getSidecarContainerIds().get("npm"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteRepositorySucceedsEvenWhenOneFormatsSidecarReleaseFails() {
|
||||
service.createDomain(REGION, "dom", null, Map.of());
|
||||
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
|
||||
doThrow(new IllegalStateException("Reposilite unreachable")).when(reposiliteClient).release(anyString());
|
||||
|
||||
// The metadata delete already committed; a sidecar failure must not turn that into a
|
||||
// caller-visible error, and must not leave the caller unable to ever get a clean response
|
||||
// for this repository (a retry would just 404, since the record is already gone).
|
||||
// for this repository (a retry would just 404, since the record is already gone). npm's
|
||||
// release still runs for the same repository even though maven's threw first.
|
||||
CodeArtifactRepository deleted = service.deleteRepository(REGION, "dom", null, "repo");
|
||||
|
||||
assertEquals("repo", deleted.getName());
|
||||
verify(verdaccioManager).release(deleted.getSidecarContainerIds().get("npm"));
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> service.describeRepository(REGION, "dom", null, "repo"));
|
||||
assertEquals("ResourceNotFoundException", e.getErrorCode());
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
{"_id":"floci-verdaccio-spike","name":"floci-verdaccio-spike","description":"spike","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"floci-verdaccio-spike","version":"1.0.0","description":"spike","main":"index.js","_id":"floci-verdaccio-spike@1.0.0","readme":"ERROR: No README data found!","_nodeVersion":"22.23.2","_npmVersion":"10.9.8","dist":{"integrity":"sha512-RqexezC2reWJgFNtDzgLP9DVPluit9i3KCyAUdML6QvTTN2G5ZscrTrqQPA6HYHjeh3UTpUKs6aPBc7JwEMnTQ==","shasum":"33c611165bf0b85e94ad1945eb8e1fb479805044","tarball":"http://127.0.0.1:19999/floci-verdaccio-spike/-/floci-verdaccio-spike-1.0.0.tgz"}}},"access":null,"_attachments":{"floci-verdaccio-spike-1.0.0.tgz":{"content_type":"application/octet-stream","data":"H4sIAAAAAAAC/+3V0WrDIBQGYK/zFAevG3tMjYWOPYyYs2LbRNEkFMbefTRLxxi9bEPH/G4OHP8bL34Nxh7Nntaua+gsDok9ACJqpeDWflLVwDa1Rim1RgUMJW6rChiyBQypN5Eh3uGSiAjf849ofTOcSNA5+NgneAVVvRQs+y/C3P95ikPy3cL9l7X63X9ZK537v4T3AoB3piW+A/528taVI8XGWOt8mYI7El9dIiPF5Hx3SUmBAr+2DSUbXejnkx/51rhpdf1XePGRn5Usy7In8glDddyfAAwAAA==","length":220}}}
|
||||
Reference in new issue
Block a user