homek8s/docs/registry-pull-auth.md

140 lines
6.8 KiB
Markdown
Raw Normal View History

# Registry pull authentication (Forgejo container registry)
Status: **enforced** since 2026-11-27 (audit finding F11). Every workload that pulls
`forgejo.siriusdevops.com/sirius/*` carries `imagePullSecrets: [forgejo-pull]`, and the
registry refuses to mint tokens for anonymous clients.
## What was wrong
Forgejo 16 (Gitea 1.22 lineage) grants anonymous callers the **ghost user** and then treats
that ghost as `perm.AccessModeRead` on *every* package — including packages of **private**
repos. Source: `services/context/package.go::determineAccessMode` (case 3: owner visibility
`public` → `Read` for `doer == nil`), reached from
`routers/api/packages/container/*::Authenticate`, which mints a ghost token *unless*
`setting.Service.RequireSignInView` is set.
Measured before the fix (2026-11-27T05:11Z): an unauthenticated `/v2/token` returned a JWT
with `{"UserID": -1, "Scope": ""}` and that token returned `200` for
`/v2/sirius/osint-dashboard/manifests/…`. `sirius/siriusdevops`, `sirius/websites` and
`sirius/sirius-growth` are **private** repos and their images (`siriusdevops-site`, `phs-site`,
`sirius-growth-*`, `sgs-leadgen`) were publicly pullable too — so "make the repo private" is
*not* a mitigation on this version.
## The switch
`apps/base/forgejo/deployment.yaml`:
```yaml
- name: FORGEJO__service__REQUIRE_SIGNIN_VIEW
value: "true"
```
Effects:
* `/v2/token` → `401 {"errors":[{"code":"UNAUTHORIZED",...}]}` for an anonymous client, and
`/v2/<repo>/manifests/<ref>` → `401` with a `WWW-Authenticate` challenge.
* Authenticated callers (basic auth: username + access token) still get the two-stage flow:
`/v2/token` (returns a scoped JWT) → `Bearer <jwt>` on `/v2/...`.
* The **web UI and the REST API are login-only for anonymous visitors**. Public repos
(`homek8s`, `onionwire`, `osint-dashboard`, …) are no longer browsable or clonable without a
Forgejo account. This is deliberate: the instance is a dev/infra host, not a public code
forge; the public *sites* are served from the cluster, not from Forgejo.
* CI pushes are unaffected — workflows authenticate with `REGISTRY_TOKEN` (`write:package`).
## The credential
A dedicated non-admin user, `registry-reader` (visibility `limited`, `read:package` scope).
secrets: bring 29 hand-made cluster Secrets under Git via SOPS/age (audit F12) Nothing in the cluster was reconciled from the repo: 67 hand-made Secrets and no managed Secret at all, so a restore of this Git tree produced a cluster that would not start. This commits the first-party ones as SOPS/age ciphertext and enables Flux's sops decryptor. * provider: SOPS + age (chosen over SealedSecrets: Flux already ships the sops decryptor, so no extra controller/CRD, and the age key is escrowed off-cluster). * 29 Secrets across forgejo, siriusdevops, piedmont, sirius-growth, osint, metabase and customer1, as apps/base/<ns>/secret-<name>.sops.yaml with encrypted_regex ^(data|stringData)$ — names/namespaces stay reviewable. * each file was generated from the LIVE object and decrypts to a byte-identical data map and type, so the first apply is a no-op (not a rotation). * the CNPG bootstrap secrets (forgejo-pg-app, pg-app, sirius-growth-portal-db) are ours, not operator output — bootstrap.initdb.secret points at them, so a rebuild has no application password without them. * forgejo-pull is included despite docs/registry-pull-auth.md calling it out-of-band: with imagePullSecrets on every workload it is the one Secret whose absence stops the whole cluster from pulling images. * .gitignore keeps **/*secret*.yaml ignored and re-includes only .sops.yaml, so a plaintext Secret still cannot be committed; age.agekey stays ignored. * excluded on purpose (controller/helm-owned): CNPG *-server/*-replication/*-ca, cnpg-system CA+webhook, cert-manager webhook CA, cilium/hubble certs, helm release records, prometheus-operator rendered prompts. Full table in docs/secrets-sops.md. Key locations (never in git): Secret flux-system/sops-age (age.agekey) and /root/sops/homek8s-age.key on the Linode, reached only over the mesh.
2026-11-27 00:17:47 -05:00
Its token lives in the Kubernetes Secrets, and since `docs/secrets-sops.md` also as SOPS
ciphertext in git (`apps/base/<ns>/secret-forgejo-pull.sops.yaml`).
Secret `forgejo-pull` (`kubernetes.io/dockerconfigjson`) must exist in every namespace whose
pods pull from the registry. Currently: `osint`, `piedmont`, `sirius-growth`, `siriusdevops`.
secrets: bring 29 hand-made cluster Secrets under Git via SOPS/age (audit F12) Nothing in the cluster was reconciled from the repo: 67 hand-made Secrets and no managed Secret at all, so a restore of this Git tree produced a cluster that would not start. This commits the first-party ones as SOPS/age ciphertext and enables Flux's sops decryptor. * provider: SOPS + age (chosen over SealedSecrets: Flux already ships the sops decryptor, so no extra controller/CRD, and the age key is escrowed off-cluster). * 29 Secrets across forgejo, siriusdevops, piedmont, sirius-growth, osint, metabase and customer1, as apps/base/<ns>/secret-<name>.sops.yaml with encrypted_regex ^(data|stringData)$ — names/namespaces stay reviewable. * each file was generated from the LIVE object and decrypts to a byte-identical data map and type, so the first apply is a no-op (not a rotation). * the CNPG bootstrap secrets (forgejo-pg-app, pg-app, sirius-growth-portal-db) are ours, not operator output — bootstrap.initdb.secret points at them, so a rebuild has no application password without them. * forgejo-pull is included despite docs/registry-pull-auth.md calling it out-of-band: with imagePullSecrets on every workload it is the one Secret whose absence stops the whole cluster from pulling images. * .gitignore keeps **/*secret*.yaml ignored and re-includes only .sops.yaml, so a plaintext Secret still cannot be committed; age.agekey stays ignored. * excluded on purpose (controller/helm-owned): CNPG *-server/*-replication/*-ca, cnpg-system CA+webhook, cert-manager webhook CA, cilium/hubble certs, helm release records, prometheus-operator rendered prompts. Full table in docs/secrets-sops.md. Key locations (never in git): Secret flux-system/sops-age (age.agekey) and /root/sops/homek8s-age.key on the Linode, reached only over the mesh.
2026-11-27 00:17:47 -05:00
These Secrets **are** reconciled by Flux from the SOPS files above (this supersedes the earlier
"out of band" wording): a namespace recreated by a Flux prune gets its copy back on the same
reconcile, instead of losing it before the pods reschedule. The manual recipe below is now only
for **rotating** the token.
secrets: bring 29 hand-made cluster Secrets under Git via SOPS/age (audit F12) Nothing in the cluster was reconciled from the repo: 67 hand-made Secrets and no managed Secret at all, so a restore of this Git tree produced a cluster that would not start. This commits the first-party ones as SOPS/age ciphertext and enables Flux's sops decryptor. * provider: SOPS + age (chosen over SealedSecrets: Flux already ships the sops decryptor, so no extra controller/CRD, and the age key is escrowed off-cluster). * 29 Secrets across forgejo, siriusdevops, piedmont, sirius-growth, osint, metabase and customer1, as apps/base/<ns>/secret-<name>.sops.yaml with encrypted_regex ^(data|stringData)$ — names/namespaces stay reviewable. * each file was generated from the LIVE object and decrypts to a byte-identical data map and type, so the first apply is a no-op (not a rotation). * the CNPG bootstrap secrets (forgejo-pg-app, pg-app, sirius-growth-portal-db) are ours, not operator output — bootstrap.initdb.secret points at them, so a rebuild has no application password without them. * forgejo-pull is included despite docs/registry-pull-auth.md calling it out-of-band: with imagePullSecrets on every workload it is the one Secret whose absence stops the whole cluster from pulling images. * .gitignore keeps **/*secret*.yaml ignored and re-includes only .sops.yaml, so a plaintext Secret still cannot be committed; age.agekey stays ignored. * excluded on purpose (controller/helm-owned): CNPG *-server/*-replication/*-ca, cnpg-system CA+webhook, cert-manager webhook CA, cilium/hubble certs, helm release records, prometheus-operator rendered prompts. Full table in docs/secrets-sops.md. Key locations (never in git): Secret flux-system/sops-age (age.agekey) and /root/sops/homek8s-age.key on the Linode, reached only over the mesh.
2026-11-27 00:17:47 -05:00
### Rotate it
The SOPS file is the source of truth — a `kubectl apply` of a new Secret is reverted by the next
`apps` reconcile, so rotate in git:
```bash
# 1. token (mint a fresh one; never echo it)
kubectl -n forgejo exec deploy/forgejo -- su-exec git forgejo admin user generate-access-token \
--username registry-reader --raw --scopes read:package --token-name pull-k8s-$(date +%Y%m%d)
# (if the user itself is gone: POST /api/v1/admin/users with visibility "limited",
# then the command above; the password is only needed for interactive login and is
# never used by containerd.)
# 2. rewrite the encrypted value in every namespace's file (no plaintext ever hits disk)
umask 077
read -r -s -p 'token: ' RT; echo
python3 - "$RT" > /tmp/dc.json <<'PY'
import base64, json, sys
t = sys.argv[1].strip()
print(json.dumps({"auths": {"forgejo.siriusdevops.com": {
"username": "registry-reader", "password": t,
"auth": base64.b64encode(f"registry-reader:{t}".encode()).decode()}}}))
PY
for ns in osint piedmont sirius-growth siriusdevops; do
f=apps/base/$ns/secret-forgejo-pull.sops.yaml
sops decrypt --output-type json "$f" \
| jq --rawfile dc /tmp/dc.json '.data[".dockerconfigjson"] = ($dc | rtrimstr("\n") | @base64)' \
| sops encrypt --filename-override "$f" --input-type json --output-type yaml /dev/stdin > /tmp/new.yaml \
&& mv /tmp/new.yaml "$f"
done
shred -u /tmp/dc.json
git commit -am 'registry(auth): rotate the pull token' && git push && # then PR + merge + reconcile
```
The consumer picks the new value up on its **next pull** — the Secret is read per pull, so no pod
restart is needed. Delete the old token in Forgejo afterwards (`profile → Applications`).
The `registry-reader` token is minted without `--expires-at`, i.e. non-expiring; rotating it is
therefore an explicit action, not something the clock forces.
### Add a namespace
Three edits, all in git:
1. copy an existing `secret-forgejo-pull.sops.yaml` into `apps/base/<ns>/` and point it at the
namespace (`sops` re-encrypts on save; `metadata.namespace` is not encrypted);
2. list it plus `secret-<other>.sops.yaml` in that app's `kustomization.yaml` (the "Secrets,
SOPS/age-encrypted in git" block);
3. `imagePullSecrets: [{name: forgejo-pull}]` in the pod spec (or on the namespace's
ServiceAccount) — do not add the secret to workloads that do not pull from this registry.
## Why not a node-level credential
Containerd is deliberately left with no registry credential
(`config_path = ""` in `/etc/containerd/config.toml`, no `/etc/containerd/certs.d/*/hosts.toml`
on any node). A node-level token would be one credential shared by every workload on every
node; `imagePullSecrets` scopes the read grant to the namespace that needs it and keeps the
audit trail per-pod.
## Verification
```bash
# anonymous must be refused
curl -s -o /dev/null -w '%{http_code}\n' \
'https://forgejo.siriusdevops.com/v2/token?service=container_registry&scope=repository:sirius/osint-dashboard:pull' # 401
curl -s -o /dev/null -w '%{http_code}\n' \
https://forgejo.siriusdevops.com/v2/sirius/osint-dashboard/manifests/amd64-54897b49bc57 # 401
# authenticated two-stage flow must work
T=$(curl -s -u "registry-reader:$RT" \
'https://forgejo.siriusdevops.com/v2/token?service=container_registry&scope=repository:sirius/osint-dashboard:pull' \
| python3 -c 'import json,sys;print(json.load(sys.stdin)["token"])')
curl -s -o /dev/null -w '%{http_code}\n' -H "Authorization: Bearer $T" \
-H 'Accept: application/vnd.oci.image.index.v1+json' \
https://forgejo.siriusdevops.com/v2/sirius/osint-dashboard/manifests/amd64-54897b49bc57 # 200
# no global credential on the nodes
for h in siriussec rpi; do ssh "$h" 'grep -E "^\s*config_path" /etc/containerd/config.toml; find /etc/containerd -name hosts.toml'; done
```
A pod that pulls without the secret now fails with
`failed to authorize: ... 401 Unauthorized` → `ErrImagePull`, which is the intended negative
control.