Skip to content

Mint Medic's gateway token from enable-compose.sh; start the gateway with either credential file missing - #4458

Merged
samarmstrong merged 1 commit into
mainfrom
factory/4429-compose-enable-compose-sh-mints-122afb5e
Nov 16, 2026
Merged

samarmstrong merged 1 commit into
mainfrom
factory/4429-compose-enable-compose-sh-mints-122afb5e

Conversation

@samarmstrong

Copy link
Copy Markdown
Collaborator

Closes #4429 (part of #4425).

On Compose, scripts/medic/enable-compose.sh now gives Medic's gateway an API token. Before, it made a password. The gateway also starts now when either credential file is missing.

Script (scripts/medic/enable-compose.sh)

  • A fresh enable makes no viewer_password.
  • Once the backend is up, the script runs python -m core.auth.service_account token <name>. The output goes straight into a 0600 temp file in the secrets dir.
    • The file must hold a vgl_api_… token. On Linux it is chowned to 10002 and then renamed onto medic_gateway_token, and medic-gateway is recreated.
    • A failed mint leaves the old file in place and no temp copy.
    • The script never calls ensure. It refuses (77) once the account has a token, and token creates a missing account itself.
  • A plain re-run with a non-empty token file mints nothing.
  • --rotate rotates the token (the old one works for 24 hours) along with api_key, and recreates the backend as before.
  • --secrets-only mints nothing and reports the token step as "still to do". With --rotate, it leaves a .mint marker so the next full run rotates the token.
  • A .mint marker is also written before every mint and cleared only once the new file is in place. token commits a rotation before the file is written, so a run cut short there gets re-minted next time instead of keeping a token that dies in 24 hours.
  • An upgraded site's viewer_password is left in place and is never rotated. The summary lists the token as minted, rotated, kept or still to do, and never prints its value.
  • make_secret now only makes api_key, so it is now make_api_key. ensure_account and read_secret are gone.

Compose

  • The new medic_gateway_token secret is mounted on medic-gateway, next to medic_viewer_password.
  • _dc_medic in scripts/lib.sh creates an empty 0600 placeholder for whichever file is missing; the enable script does the same. Without it, Compose refuses to start the container ("bind source path does not exist"). The gateway treats an empty file as absent.

Docs

  • docs/medic/enable.md and docs/upgrading.md cover the move by re-run, and the re-mint after a restore (delete medic_gateway_token and re-run).
  • CHANGELOG.md says the password path is removed no earlier than the release that ends the announced window.

Live tests (opt-in, nightly or manual dispatch only)

  • test_enable_live.py: the canary checks the token instead of the password, and the gateway read must show credential: token.
  • test_compose_live.py: its stub backend accepts only a password login, so the fixture writes a viewer_password to model a site enabled before tokens.

Verification

Ran. Every check below used the repo's Compose stack as shipped, under the factory slot (base compose file plus the Medic overlay, real backend and Postgres). The "15 GETs" are the gateway's whole outbound allowlist, requested through medic-gateway-out:8471 from inside the medic container. Each check also read /_gw/status.

  1. Fresh enable
    • It printed medic_gateway_token minted. viewer_password was a 0-byte placeholder; the token file was 52 bytes, owned by 10002, mode 0600.
    • All 15 GETs were relayed:
    • credential: token, state: ok.
  2. Plain re-run: printed kept. The token file's inode and mtime were unchanged, the database still had one credential, and no container was recreated.
  3. --rotate, probing every 2 s throughout
    • It printed rotated, and the old credential's expiry moved to +24 h.
    • No probe got a 401. The 502s came only while the backend was recreated for the new api_key, as before this change.
    • Afterwards the 15 GETs matched check 1, with credential: token.
  4. Upgrade from a password install
    • I enabled a new secrets dir with main's script, lib.sh and compose file (credential: password), then switched back to this branch's files.
    • Control: plain docker compose … up medic-gateway failed with bind source path does not exist: …/medic_gateway_token.
    • Through ensure_container (what start.sh calls), the placeholder was created, the gateway started, and it kept reading with credential: password.
  5. Re-run on that site
    • It printed minted, plus viewer_password left in place…. Then credential: token.
    • users.password_hash was NULL after the gateway's first token read, and a login with the old password returned 401.
  6. Starting with files missing (gateway removed and started through ensure_container each time)
    • Password file missing: credential: token.
    • Both missing: started, credential: none, no_credential; the auth'd routes returned 502 and the unauthenticated ones 200 or 404.
    • Token file missing: covered by check 4.

Further checks:

  • After the token file was deleted, a re-run brought reads back (rotated).
  • --secrets-only --rotate printed "still to do" and left .mint; the next plain run rotated the token and cleared it.
  • A refused mint (account deactivated, exit 77) left the token file unchanged (same inode, mtime and size). After the review fixes, a refused mint also left .mint and no temp file, and the next plain run re-minted.
  • --secrets-only on a fresh dir created both placeholders.
  • No run's output contained vgl_api_.
  • bash -n passes on both scripts, and py_compile on the two tests.

A subagent reviewed the diff. Fixed:

  • A rotation cut short before the rename would have been "kept" on re-run; the .mint marker before minting, plus an EXIT trap for the temp file, fix it.
  • --secrets-only left no placeholders for the printed docker compose command.
  • A stale ensure paragraph in enable.md.
  • The two live tests that this change breaks.

Did not run

  • ./start.sh itself. It would also start a host-run backend on 6987, which the containerised backend holds. I ran the function it uses to start containers (ensure_container → dc) instead.
  • The opt-in live pytest suites (tests/medic_compose/*_live.py). They are manual-dispatch/nightly and build their own stack; I updated them but didn't run them.
  • macOS / Docker Desktop, where nothing is chowned. No machine for it.
  • Helm. Out of scope.

Blocked by the machine

  • No .env. Compose needs JWT_SECRET_KEY, so I copied env.example to .env with a random secret and deleted it afterwards.
  • shellcheck isn't installed.

🤖 Generated with Claude Code

…with either credential file missing (#4429)

enable-compose.sh no longer makes viewer_password. Once the backend is up it
pipes `python -m core.auth.service_account token` straight into a 0600
medic_gateway_token (chowned to 10002 on Linux before the rename), then
recreates medic-gateway. A plain re-run with a token kept mints nothing;
--rotate rotates it with the API key; --secrets-only mints nothing and says so.
An upgraded site's viewer_password is left in place.

Compose mounts the new medic_gateway_token secret. lib.sh's dc and the script
create empty 0600 placeholders for a missing token or password file, so the
gateway starts with either missing and reads an empty one as absent.

Closes #4429

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@samarmstrong samarmstrong added the factory Opened by the software factory label Nov 16, 2026
@samarmstrong
samarmstrong merged commit e4d67f5 into main Nov 16, 2026
28 checks passed
@samarmstrong
samarmstrong deleted the factory/4429-compose-enable-compose-sh-mints-122afb5e branch November 16, 2026 06:00
samarmstrong added a commit that referenced this pull request Nov 16, 2026
Resolve docs/medic/enable.md: Compose (#4458) and Helm (this PR) both
read with a host-minted token now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

factory Opened by the software factory

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compose: enable-compose.sh mints Medic's token, and the gateway starts with either credential file missing

1 participant