Files
infrastructure/README.md
T

239 lines
12 KiB
Markdown

# Tomter Vel — infrastructure
Everything the vel's own site needs on one host, as containers: NATS
with JetStream for the records, Rauthy for who is who, portal, the site
itself, and the chat. The content (pages, forms, desks) lives in
[tomtervel/questions](https://prosjekt.klingenbergbygg.no/tomtervel/questions)
and is fetched from there; this repo owns the host.
```
Internet ──► Caddy (Let's Encrypt)
├── PORTAL_HOST ──► portal:3000 ──► NATS (records) + Rauthy (login)
├── RAUTHY_HOST ──► rauthy:8080
└── CHAT_HOST ────► cnats:3000
```
Nothing about the vel's members or records depends on anyone else's
host: the login, the bus and the site are the vel's own.
## First start
On a fresh Linux host with podman + podman-compose (run rootful, so Caddy
can bind 80/443 and Rauthy sees a stable source IP), with a Gitea runner
on it, and the variables and secrets below set (`NATS_PASSWORD` is any
long random string: `openssl rand -hex 24`; Rauthy's are under
[Rauthy](#rauthy)):
```sh
git clone https://prosjekt.klingenbergbygg.no/tomtervel/infrastructure /srv/tomtervel/infrastructure
sudo install -m 0755 /srv/tomtervel/infrastructure/deploy-tomtervel /usr/local/bin/deploy-tomtervel # and the sudoers line in it
# Actions -> deploy -> Run workflow # the env files, and the stack up
```
Then make the clients and portal's API key in Rauthy ([Rauthy](#rauthy)).
## Running on kasse (behind the host Caddy)
On kasse the host Caddy already owns 80/443, so the bundled Caddy stays
off (`profiles: [edge]`). The services publish loopback-only ports for
it, and `host-caddy/tomtervel.no` is its file for them
(`/etc/caddy/conf.d/tomtervel.no`):
- portal → `127.0.0.1:3050`
- rauthy → `127.0.0.1:8090`
- cnats → `127.0.0.1:3070`
- nats → `127.0.0.1:4223` (kasse's shared platform NATS owns 4222)
## Accounts and groups
Rauthy holds the accounts: sign-in, passkeys, the account pages and the
account mail, in Norwegian. Portal and the chat sign in there
(`OIDC_ISSUER`), and portal makes the accounts there itself
(`RAUTHY_API_KEY`):
- **The first person in.** The `SEED_ADMIN_EMAIL` secret: at start,
portal gives them the most privileged group (the board) and makes
their account; Rauthy mails them how to set a password or a passkey.
The same address is Rauthy's first admin.
- **Everyone else** is invited from a desk, or given a place by a case
(`grants:`). Portal makes the account if there is none, and Rauthy
mails the way in.
- **The people who ask the questions.** Everyone a page names as
`responsible` gets an account at start and a place in
`RESPONSIBLE_GROUP` (`redaktor`).
The groups are portal's (`PEOPLE_BACKEND=memberships`): who is in
`styret` is on the board's desk, "Medlemmer og invitasjoner", where a
person is taken out or invited again. Portal decides what a group may
do, on the site and in the chat's rooms alike.
## Content changes
A push to tomtervel/questions is linted on push and tells the portal
to reload over NATS. That reload reaches the host it runs on: today
Klingenberg Bygg's. On this host the runner is a **host service**
(`pacman -S gitea-runner`, registered against prosjekt.klingenbergbygg.no) —
not an in-compose container — so it reaches NATS on the published
`127.0.0.1:4222`. Give the content repo's `lint-and-reload.yml` a reload job
whose `runs-on` matches that runner's host label, running
`nats --server nats://portal:$NATS_PASSWORD@127.0.0.1:4222 pub portal.content.reload ""`.
Until then, `podman compose restart portal` picks up new content.
## Upgrading portal
Set the org variable `PORTAL_RELEASE` (a tag of uhhm/portal) and run the
deploy workflow. The content repo's lint reads the same variable, so the
two cannot drift apart.
## Variables and secrets
Every value the stack runs with is an Actions variable or secret in
Gitea, never a file in the repo (it is public) and never edited on the
host. The deploy workflow writes them into the env files on kasse
(`.env`, `portal.env`, `nats.env`, `chat.env`) on every run, so a host
name or a version is changed in Gitea, and then **Actions -> deploy ->
Run workflow**. Set them with `gt` (`gt variable set NAME --body ...`,
`gt secret set NAME`, `-o tomtervel` for the org's).
| name | kind | where | what |
|---|---|---|---|
| `PORTAL_HOST`, `RAUTHY_HOST`, `CHAT_HOST` | variable | repo | the site, the login, the chat; each needs DNS (`dns/`) and a kasse Caddy block |
| `PORTAL_RELEASE` | variable | **org** | a uhhm/portal tag; the content repo's lint reads the same one |
| `GDO_RELEASE`, `CNATS_TAG`, `KRON_RELEASE` | variable | repo | the mailer, the chat, the money service |
| `COMPOSE_PROFILES` | variable | repo | `chat` |
| `CONTENT_REPO`, `CONTENT_BRANCH` | variable | repo | the content this site serves |
| `SITE_NAME` | variable | repo | `tomtervel`; never changed (a new name is a new, empty site) |
| `PEOPLE_BACKEND`, `RESPONSIBLE_GROUP` | variable | repo | `memberships`, `redaktor` |
| `OAUTH2_CLIENT_ID`, `CHAT_OAUTH2_CLIENT_ID` | variable | repo | the Rauthy clients of the site and the chat: `portal`, `chat` |
| `NATS_ADOPT_LEGACY` | variable | repo | `sole` |
| `GARAGE_S3_ENDPOINT`, `GARAGE_UPLOADS_BUCKET` | variable | repo | uploads |
| `NATS_PASSWORD` | secret | **org** | the stack's NATS; the content repo's reload uses it too |
| `OAUTH2_CLIENT_SECRET` | secret | repo | the site's Rauthy client |
| `CHAT_OAUTH2_CLIENT_SECRET` | secret | repo | the chat's Rauthy client |
| `GARAGE_ACCESS_KEY`, `GARAGE_SECRET_KEY` | secret | repo | uploads |
| `SEED_ADMIN_EMAIL`, `SEED_ADMIN_NAME` | secret | repo | the first person in; an address, so not a variable |
| `CLOUDFLARE_API_TOKEN` | secret | repo | DNS edit for tomtervel.no only (`dns/`) |
| `MAPBOX_TOKEN` | secret | repo | optional: the map tiles portal proxies (the landing hero, location fields); unset, maps are off |
| `MAPBOX_STYLE` | variable | repo | optional: `<owner>/<style>`; unset, `mapbox/streets-v12` |
| `DNSCONTROL_VERSION` | variable | repo | the DNS workflow's dnscontrol |
| `RAUTHY_ENC_KEY`, `RAUTHY_SECRET_RAFT`, `RAUTHY_SECRET_API` | secret | repo | Rauthy's own: made once, never changed (the key encrypts its database) |
| `OIDC_ISSUER` | variable | repo | where the site and the chat sign in: `https://konto.tomtervel.no/auth/v1/` |
| `RAUTHY_BOOTSTRAP_API_KEY`, `RAUTHY_BOOTSTRAP_API_KEY_SECRET` | secret | repo | optional, Rauthy's first start only: a short-lived key (base64 of `{"name","exp","access"}`, secret 64+ alphanumerics) that makes the OIDC clients through the API; read into an empty database, ignored after |
| `RAUTHY_API_KEY`, `RAUTHY_LANGUAGE` | secret, variable | repo | portal makes accounts in Rauthy (`<name>$<secret>`, made in Rauthy's admin), in `nb` |
A variable is printed wherever a workflow echoes it, and this repo's run
logs are public: a value that must not be read is a secret.
What is not a value stays on the host, made there once: Garage's node
secret.
## Rauthy
[Rauthy](https://github.com/sebadob/rauthy) at `RAUTHY_HOST`
(`konto.tomtervel.no`): sign-in, passkeys, account pages and account
mail, in Norwegian (`nb`) and English. A passkey belongs to the name it
was made on, so `RAUTHY_HOST` stays: a new name means new passkeys for
everyone.
On a new host:
1. The secrets, made once and never changed (the key encrypts its
database):
```sh
echo "$(openssl rand -hex 4)/$(openssl rand -base64 32)" | gt secret set RAUTHY_ENC_KEY -R tomtervel/infrastructure
openssl rand -hex 24 | gt secret set RAUTHY_SECRET_RAFT -R tomtervel/infrastructure
openssl rand -hex 24 | gt secret set RAUTHY_SECRET_API -R tomtervel/infrastructure
```
Optionally a short-lived key for making the clients through the API
(`RAUTHY_BOOTSTRAP_API_KEY`, see the table), read on the first start
only.
2. Deploy. The first admin is `SEED_ADMIN_EMAIL`; its password is in the
log once: `podman compose logs rauthy | grep -A2 -i password`.
Signed in, it sets up a passkey.
3. In Rauthy's admin (`https://konto.tomtervel.no/auth/v1/admin`), or
with the bootstrap key: the client `portal` (`authorization_code`,
`S256`, scopes `openid profile email`, redirect
`https://tomtervel.no/auth/callback`) and `chat` (also
`refresh_token`, redirect `https://samtaler.tomtervel.no/auth/callback`),
their secrets into `OAUTH2_CLIENT_SECRET` and
`CHAT_OAUTH2_CLIENT_SECRET`, and an API key `portal` with read,
create and update on users and groups into `RAUTHY_API_KEY`.
Rauthy's mail leaves through kasse's postfix like gdo's, and Rauthy
stops if it cannot reach it.
## Preview
`forhandsvisning.tomtervel.no` shows the content branch last pushed to
tomtervel/questions, before it is merged. The content repo's
`lint-and-reload` workflow runs it after the lint passes: a container
`tomtervel-preview` from this site's own image
(`localhost/tomtervel-portal:<PORTAL_RELEASE>`), beside a throwaway NATS
(`tomtervel-preview-nats`), on 127.0.0.1:3060. It has no gdo, so nothing
mails anyone, and no sign-in, so desks stay shut and the public pages
show. A form sent on it lands in the throwaway NATS and nowhere else.
The commit gets a "forhåndsvisning" status with the link, which a PR
shows among its checks. Main is never previewed: it is the site.
## Backups
- Rauthy's database is the `rauthy_data` volume: the accounts, passkeys
and clients. Snapshot it with NATS's.
- NATS JetStream data is the `nats_data` volume: every record ever
submitted and every state change, the memberships and the chat's
history. Snapshot the volume.
- Caddy's certificates regenerate; nothing to keep.
## What is not here
Mail (a person's reset link is a token you hand them), monitoring, and
the vel's current website at tomtervel.no, which stays where it is
until the vel points the apex at PORTAL_HOST.
## DNS
tomtervel.no's records are `dns/dnsconfig.js`, applied to Cloudflare
with DNSControl: the site, the login and the chat pointing here, Zoho's
mail, and SPF, DKIM and DMARC for the mail the site sends. How to run
it, the token it needs, and the order of a move are in
[dns/README.md](dns/README.md).
## Mail
Portal decides what to send - a `mail:` on a state in the content, or an
invite - and publishes it on this stack's NATS. `gdo` is what hands it to
a mail server, and it runs here, in the vel's own stack, so the vel's mail
leaves on the vel's own terms.
It has no mail server of its own. It relays through the host's, which on
kasse is Klingenberg Bygg's postfix on port 25, reached from the container
as `host.containers.internal`. That is the one thing in this stack the vel
borrows, and the one thing that changes if the vel ever moves to a host of
its own: point `SMTP_HOST` at whatever that host runs.
`site.yaml` in the content repo needs a `mail.from`, or portal has nothing
to send from and says so in its log.
Replies are not read yet: `JMAP_URL`, `JMAP_TOKEN` and `MAIL_REPLY_DOMAIN`
turn a reply into a note on the record it answers, and none of them are set
here.
## Mail leaving the host
gdo hands the vel's mail to kasse's own postfix over the container
network, so postfix has to be willing to relay from it: `mynetworks`
on kasse includes `172.16.0.0/12` and `10.88.0.0/16` (set 2026-09-30,
the original is in `/etc/postfix/main.cf.before-containers`). Without
it every address outside the host's own domains is refused with
"Relay access denied", and gdo retries a minute apart until it is.
## Uploads
Pictures and documents a desk attaches, and what `portal import` brings
in, go to Garage in this project. `go-live.sh garage` renders
`garage/garage.toml` with this node's own rpc secret (gitignored), makes
the bucket `tomtervel-attachments` and the key `tomtervel-portal-uploads`,
and puts `GARAGE_S3_ENDPOINT`, `GARAGE_ACCESS_KEY`, `GARAGE_SECRET_KEY` and
`GARAGE_UPLOADS_BUCKET` in `portal.env`. Without them an upload field
fails closed. Nothing of Garage is published on the host.