Skip to content

feat(api): the installation registry's token endpoint and grants (part of #215) - #529

Merged
samuelmbabhazi merged 3 commits into
mainfrom
feat/215-registry-tokens
Nov 27, 2026
Merged

samuelmbabhazi merged 3 commits into
mainfrom
feat/215-registry-tokens

Conversation

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator

Part of #215 (M3.3), following ADR 0030. This is the control plane side of the installation's own registry: the token service that CNCF Distribution delegates authentication to. Nothing calls RegistryGrants.issue yet. The build and pull actions will do so in the next PRs.

What changed

  • Grants (registry_grants, tenant-scoped with RLS):
    • a grant is a credential for one application's repository, {organizationId}/{applicationId}: pull, or pull and push for a build, until the action's deadline;
    • the user name is {organizationId}.{grantId}, so that the anonymous endpoint can read the grant within its tenant;
    • the password is 32 random bytes, and only its SHA-256 is stored;
    • each grant is audited as registry.grant_issued (system actor). Grants expired for a day are removed when the organization is issued another one.
  • Token endpoint GET /api/v1/registry/token:
    • anonymous, takes basic auth, and is rate limited like any API request;
    • it answers Distribution's {token, access_token, expires_in, issued_at}. The token is an EdDSA JWT for the control plane's host, valid 5 minutes and never past the grant's deadline;
    • it grants only the grant's own repository, with push only for a build grant;
    • every refusal is the same 401 with WWW-Authenticate: Basic, and never says what was wrong.
  • Signing key:
    • a new installation key kind, registry (Ed25519), created and audited like the CA and confirmation keys;
    • its public half is written as a JWKS to REGISTRY_JWKS_FILE at start, for Distribution's auth.token.jwks.
  • Docs: SPEC (data model row and the registry section) and the route matrix.

Next PRs: the registry in the control plane's Compose project and its /v2/ routes (Compose Caddy, and the agent's route on one machine); then the build itself (agent BuildImage with rootless BuildKit, the pipeline's build step, grants on the pull actions).

How it was tested

  • Feasibility first: CNCF Distribution 3.1.2 accepts EdDSA tokens with a JWKS file, checks kid, aud and exp, and enforces the per-repository access claim. I checked this against a real registry:3.1.2 before writing the code.
  • Unit tests (registry-token.spec.ts) cover user names, basic headers, passwords and scopes, with malicious inputs:
    • path traversal in repository names, other repositories and organizations, the catalog, *, uppercase, newlines;
    • too many scopes, oversized values, SQL-shaped user names.
  • Integration with PostgreSQL (registry-tokens-http.integration.spec.ts):
    • the token verifies with the published JWKS;
    • a pull-only grant gets no push;
    • an unscoped request gets an empty access;
    • seven different refusals give one identical answer;
    • only the hash is stored, and the audit event carries no password.
  • Integration with a real Distribution (registry-distribution.integration.spec.ts, Testcontainers): a build grant pushes to its repository, another repository is refused, a pull grant cannot push, and a forged signature is refused. Testcontainers has no runtime in my sandbox, so I ran this test once against the host's Docker, with a local-only copy of the helper, and it passed. CI runs it as written.
  • Local results:
    • lint and typecheck are clean;
    • API unit tests: 949 passed;
    • integration: all pass except the usual environment failures: the PostgreSQL 18 error codes, and the specs that need a container runtime (VictoriaMetrics, S3, and this Distribution one).

Security considerations

  • An anonymous route that takes credentials. The answer never distinguishes an unknown grant, a wrong password, an expired grant or another service. The password is compared in constant time on its hash.
  • The authorization header is already redacted in request logs. The query only carries the service, the scopes and the user name, which is not secret.
  • A token is confined to one application's repository, and pushing needs a build grant. Distribution enforces the claim (tested).
  • The private key never leaves the key provider. It is stored encrypted under installation:keys, as the CA's is.
  • The runtime role gets SELECT, INSERT, DELETE on registry_grants, and no UPDATE.

About 900 changed lines without generated code, over the 500 target: tests make up about 400 of them.

…t of #215)

ADR 0030: Distribution's token authentication with the control plane as token service. A grant (pull, or pull and push for a build) is issued for an action until its deadline, its password stored hashed and the grant audited. GET /api/v1/registry/token answers a five-minute EdDSA token for the grant's repository only, signed with a new installation key whose public half is written as a JWKS for Distribution.

Signed-off-by: samuel mbabhazi <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 14 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 5579d3e and 988eba6.

Files ignored due to path filters: 3

  • apps/api/openapi.json is excluded by !apps/api/openapi.json
  • docs/generated/route-role-matrix.md is excluded by !**/generated/**, !docs/generated/**
  • packages/api-client/src/gen/openapi.ts is excluded by !**/gen/**, !packages/api-client/src/gen/**
Files selected for processing: 24

  • apps/api/prisma/migrations/20261127120000_registry_grants/migration.sql
  • apps/api/prisma/schema.prisma
  • apps/api/src/modules/fleet/application/ports.ts
  • apps/api/src/modules/fleet/fleet.integration.spec.ts
  • apps/api/src/modules/fleet/infrastructure/ca/installation-key-store.ts
  • apps/api/src/modules/fleet/infrastructure/ca/x509-certificate-authority.spec.ts
  • apps/api/src/modules/fleet/infrastructure/ca/x509-certificate-authority.ts
  • apps/api/src/modules/fleet/installation-keys.module.ts
  • apps/api/src/modules/registries/application/ports.ts
  • apps/api/src/modules/registries/application/registry-grants.ts
  • apps/api/src/modules/registries/application/registry-tokens.ts
  • apps/api/src/modules/registries/domain/registry-token.spec.ts
  • apps/api/src/modules/registries/domain/registry-token.ts
  • apps/api/src/modules/registries/infrastructure/prisma-registry-grant.store.ts
  • apps/api/src/modules/registries/infrastructure/registry-jwks.file.ts
  • apps/api/src/modules/registries/interface/registry-token.controller.ts
  • apps/api/src/modules/registries/registries.module.ts
  • apps/api/src/modules/registries/registry-distribution.integration.spec.ts
  • apps/api/src/modules/registries/registry-tokens-http.integration.spec.ts
  • apps/api/src/platform/config/config.schema.ts
  • apps/api/src/platform/prisma/tenant-database-role.ts
  • apps/api/src/testing/distribution.ts
  • deploy/postgres/runtime-role.sql
  • docs/SPEC.md
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

CodeQL read the grant's random 32-byte secret as a person's password. It is hashed like API tokens now, SHA-256 bound to its purpose (shipkeep:registry_grant:), and named a secret in the code and the column; clients still send it as the password of basic auth.

Signed-off-by: samuel mbabhazi <[email protected]>
@samuelmbabhazi
samuelmbabhazi merged commit e824b1b into main Nov 27, 2026
27 checks passed
@samuelmbabhazi
samuelmbabhazi deleted the feat/215-registry-tokens branch November 27, 2026 10:11
samuelmbabhazi added a commit that referenced this pull request Nov 27, 2026
…e project (part of #215) (#530)

Part of #215 (M3.3), following ADR 0030. This puts the installation's
registry in the control plane's Compose project and serves it under
`/v2/` on the control plane's name. **It builds on #529** (token
endpoint and grants) and contains its commit until #529 merges. The base
is `main` so that merging #529 does not close this PR.

## What changed

- **`registry` service** in `deploy/control-plane/compose.yml`: CNCF
Distribution 3.1.2, pinned by digest.
- It is configured by the new `registry.yml`, with token authentication.
Its realm (the control plane's `/api/v1/registry/token`) and its service
(`SHIPKEEP_DOMAIN`) come from the environment.
- Images live in the `registry-data` volume, which is not in the
instance backup.
- It reads the JWKS from the `registry-auth` volume, which the control
plane writes (`REGISTRY_JWKS_FILE`) and the registry mounts read-only.
  - It starts once the control plane is healthy, so the key exists.
- **Separate machine**: the Compose Caddy sends `/v2` and `/v2/*` to the
registry uncompressed, and everything else to the API as before.
- **Single machine**:
- `compose.single-machine.yml` publishes the registry on
`127.0.0.1:5000`, in the control plane's slice, capped at 256 MiB;
- the agent keeps a third fixed route before the control plane's:
`shipkeep-control-plane-registry`, with host plus path `/v2`, `/v2/*`,
to `control_plane.route.registry`;
- that setting defaults to `127.0.0.1:5000`, so existing agent
configurations need no change, and a non-loopback value is refused. No
action can name or remove the route (tested).
- **Image**: the control plane image creates
`/var/lib/shipkeep-registry`, owned by `node`, so that the new volume is
writable.
- **Installer and bundle**:
  - the bundle and the installer ship `registry.yml`;
  - the single-machine checks refuse a busy port 5000;
  - `install_test.sh` checks the merged Compose configuration.
- **Docs**: INSTALL (files, upgrade, port, route) and SPEC.

## How it was tested

- **Distribution 3.1.2**: the environment variables do override `realm`
and `service` from `registry.yml`. Tested against a real
`registry:3.1.2` with an EdDSA token: 401 with the right realm, then 202
for an allowed push.
- **Caddyfile**: `caddy adapt` with the pinned Caddy image confirms the
routing (`/v2` and `/v2/*` to the registry, the rest compressed to the
API).
- **Agent routes on a real Caddy** (pinned image, internal TLS):
- `/v2`, `/v2/` and `/v2/org/app/blobs/uploads/` reach the registry
upstream;
- `/`, `/api/v1/registry/token`, `/v2x` and `/web/v2/` reach the control
plane;
  - plain HTTP gets a 308 to HTTPS;
- a Host header with another port matches no route, the existing bound.
- **Merged Compose configuration** (`docker compose config` with both
files): realm, service, port `127.0.0.1:5000`, `depends_on` healthy, the
read-only volumes, the slice and the limit.
- **Go**: `go test ./agent/...` and `golangci-lint` are clean. New cases
cover the route order and paths, default and named registry upstreams,
non-loopback values refused, and the fixed routes out of reach of
`RemoveRoute`.

## Security considerations

- The registry accepts only tokens signed with the installation's key:
anonymous requests get 401 with the realm. Its port is published on
loopback only, and only on a single machine.
- The JWKS volume holds only the public key. The private key never
leaves the control plane's database, where it is encrypted.
- A route written before this change keeps working: the agent adds the
registry route ahead of it, and an action can never remove it.
- Known limit, per ADR 0030: on a single machine, the server's Docker
reaches the registry through the control plane's public name, via the
egress proxy. Behind a NAT that does not loop back, Git builds do not
work in this version. INSTALL will say so once builds ship.

---------

Signed-off-by: samuel mbabhazi <[email protected]>
samuelmbabhazi added a commit that referenced this pull request Nov 27, 2026
…wrong (#538)

The registry token test (#529) builds a wrong secret by replacing the
last character of a real one with `A`. When the secret already ended
with `A`, one time in 64, nothing changed and the case answered 200.
#536's integration run hit it.

The last character is now always replaced by a different one (`B` if it
was `A`). The test passed three times in a row locally (PostgreSQL).
Test only, no change to the code.

Signed-off-by: samuel mbabhazi <[email protected]>
samuelmbabhazi added a commit that referenced this pull request Nov 27, 2026
… then deploys the image (part of #215) (#537)

Part of #215 (M3.3), per ADR 0030. This connects the pieces already
merged: the registry and its grants (#529, #530), the agent's
`BuildImage` (#533) and the build lane (#532). A Git application with a
Dockerfile now builds when it deploys, and its replicas run the image
that was built.

## What changed

- **Configuration**: an application whose configuration is a
repository's may use `build.type: dockerfile`. Railpack and Nixpacks are
still refused with `unsupported_build_type` (new text, in the web UI and
in French). An application created from an image still runs an image.
- **Release**:
- `releaseDraft(config, { repository })` plans a build. Its image is
`<control plane host>/<organization>/<application>:<release number>`
(`builtImage`), set once the store knows the number;
- `repositoryPath` reads paths the way the agent does: `./api/` → `api`,
`.` → the root;
  - the commit was already kept (#527).
- **Planner** (`building` and `built` stages): the replicas are placed
first, then `build_image` runs on the first one's server, at the commit,
with the context, the Dockerfile and the args. Its digest becomes the
release's (`record_build`). A failed build gives its cause in words: its
output, its time limit, a server without a builder.
- **Runner**:
- the GitHub token is minted **outside any transaction**
(`RepositorySource.buildAccess`): read access to that one repository,
under its current name. If GitHub cannot be reached, the run waits and
tries again;
- the push grant (#529) is issued at enqueue time and lasts as long as
the action;
  - `release.image_built` is audited with the digest and the commit;
- every pull from the installation's registry carries a **pull-only
grant**.
- **Store**: an action's `buildImage.imageDigest` is read back, only if
it is a valid `sha256:` digest.
- **SPEC**: the build stage. Open question: the token is not revoked
when the build ends yet (it expires within the hour).

## How it was tested

- **Planner unit tests**:
- the full order, from `queued` through `building` and `built` to
`succeeded`;
- the exact build step (first replica's node, normalized paths, the
tag), with the default context and Dockerfile;
  - every replica pulls `<repository>@<built digest>`;
  - each failure cause and its text, with nothing deployed afterwards;
  - no digest, no commit, a release already built.
- **Agent action**: `BuildImage` carries the URL, the commit, the token
and the push grant, and round-trips through protobuf. A build without
its access is refused.
- **Integration** (`git-builds.integration.spec.ts`, PostgreSQL, fake
GitHub, real pipeline and token endpoint):
- the deploy creates `…/<app>:1`, and the build is the only action
queued;
  - the token is scoped to `repositoryIds: [777]`, `contents: read`;
- the push grant is accepted by the token endpoint for pull and push on
that repository;
  - after the agent's answer, the digest is recorded and audited;
- the pull goes to `<repo>@<digest>` with a grant that gets `pull` only;
- both grants are audited, and the token never appears in the audit log.
- **Suites**:
  - API: lint, typecheck, 411 unit tests;
- integration, all suites run: only the known environment failures
(PostgreSQL 18 error codes, specs that need a container runtime);
  - web: 398 tests.

## Security considerations

- **The token** is minted for one repository and read access, travels
only inside the encrypted action, and is never logged or audited. It is
not revoked yet (open question).
- **The grants**: pushing only for the build, only to the application's
repository, until the action's deadline. Servers only pull.
- **No GitHub call holds a database transaction.**

Signed-off-by: samuel mbabhazi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants