Manage .env from the admin panel

Add an Environment tab to /admin that lists every variable in .env.example
and lets keys, OAuth settings, email settings and generated secrets be
changed without SSH. Secrets are write-only; infrastructure variables are
shown read-only.

The backend never writes .env or touches Docker. It validates an edit and
queues it in env-apply/run/request.json; env-apply/apply.py, run on the host
by a systemd path unit (env-apply/install.sh), rewrites .env atomically,
keeps .env.bak, and recreates only the services that read the change.

backend/services/env_registry.json is the single list of variables, shared
by the backend and the host script; a test keeps it in step with
.env.example. It replaces the drifted checklist in handle_admin.go. The job
capture token card moves to the new tab.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-12 14:02:28 +01:00
co-authored by Claude Opus 5.5
parent 26f312e307
commit 2786bce022
24 changed files with 2293 additions and 306 deletions
+33 -1
View File
@@ -83,9 +83,41 @@ for the things GraphQL cannot do:
| `/notes/*path` | streams a file download, not JSON |
| `/ws` | WebSocket upgrade |
| `/rowing` (GET), `/spotify/listening`, `/spotify/recent` | predate the equivalent queries; kept working |
| `/admin/config`, `/admin/logs`, `/admin/capture-token` | process environment and files on disk, not the data model |
| `/admin/env`, `/admin/logs`, `/admin/capture-token` | process environment and files on disk, not the data model |
| `/jobs/capture`, `/jobs/capture/status`, `/jobs/capture/extract` | called by a userscript on other sites' origins, where the `SameSite=Lax` auth cookies are never sent; they take a bearer token instead |
### Environment tab
`handlers/handle_env.go` and `services/env_manager.go` let the admin panel
change `.env` on the Pi without SSH. The backend never reads or writes the
file and has no Docker access: it validates an edit and leaves it for the
host, which is the part trusted to act.
- **`services/env_registry.json`** lists every variable in `.env.example`
with its group, kind, whether it is editable, and the compose services that
read it. It is embedded in the binary and read from the checkout by the host
script, so one file decides what the panel offers and what the host accepts.
A test fails if it and `.env.example` disagree.
- `GET /admin/env` — the registry plus state from this process's own
environment. A secret's value is never sent, only "set" and, for values of
12+ characters, the last four.
- `PUT /admin/env` — `{"set": {NAME: value}, "generate": [NAME]}`; `""`
clears. The whole batch is validated first. Control characters are refused
because `.env` is line oriented: a newline would let an edit to an allowed
variable write a second, arbitrary one. Generated values (32 random bytes,
hex) are returned once.
- The change is written to `/backend/env-apply/request.json` (`./env-apply/run`
on the host), `0600` and chowned to that directory's owner so the
unprivileged host user can read it. `env-apply/apply.py` claims it, rewrites
`.env`, recreates the affected services and writes `status.json`.
- `GET /admin/env/status` — `idle`, `pending`, `applying`, `applied` or
`failed`. `watcher: false` means a request has sat unclaimed for 15 seconds,
i.e. nothing on the host is watching. `DELETE /admin/env/pending` withdraws
an unclaimed request.
The backend is always among the recreated services, because the values the
panel shows are the ones this process started with.
### Job capture
`handlers/handle_job_capture.go` is the server half of the browser userscript