From 302a163fbd0878aa15b61f91096889a2a2440e83 Mon Sep 17 00:00:00 2001 From: Bendik Aagaard Lynghaug Date: Sat, 7 Nov 2026 15:36:14 +0100 Subject: [PATCH 1/6] tomtervel.no's DNS as DNSControl, and the vel's own DKIM key on kasse dns/dnsconfig.js is the whole zone for Cloudflare: the site, id and chat pointing at kasse unproxied, Zoho's MX, SPF for Zoho and kasse, DMARC reporting, and kasse's DKIM key once it is made. kasse-dkim-tomtervel.sh makes that key in kasse's rspamd beside the other domains'. dns/README.md has the token, the first-push check against the live zone, and the order of the move. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01L5fbDmSQSb6u27EiEuwzoM --- README.md | 8 ++++++ dns/README.md | 56 ++++++++++++++++++++++++++++++++++++++ dns/creds.json | 7 +++++ dns/dnsconfig.js | 60 +++++++++++++++++++++++++++++++++++++++++ kasse-dkim-tomtervel.sh | 41 ++++++++++++++++++++++++++++ 5 files changed, 172 insertions(+) create mode 100644 dns/README.md create mode 100644 dns/creds.json create mode 100644 dns/dnsconfig.js create mode 100755 kasse-dkim-tomtervel.sh diff --git a/README.md b/README.md index ce555dd..a96777b 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,14 @@ 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 diff --git a/dns/README.md b/dns/README.md new file mode 100644 index 0000000..5c929f4 --- /dev/null +++ b/dns/README.md @@ -0,0 +1,56 @@ +# tomtervel.no's DNS + +The zone is `dnsconfig.js`, applied to Cloudflare with +[DNSControl](https://dnscontrol.org). No state file: every run compares +the file with the live zone, so it can be run by anyone with the token, +from any machine. + +```sh +paru -S dnscontrol +cd dns +export CLOUDFLARE_API_TOKEN=... # see below; never commit it +dnscontrol check # the file parses +dnscontrol preview # what would change, nothing changed +dnscontrol push # change it +``` + +`creds.json` names the token by its variable only, so it is in the repo. + +## The token + +tomtervel.no is in Bendik's father's Cloudflare account. He makes a token +for it (My Profile -> API Tokens -> Create Token -> "Edit zone DNS"), +with **Zone / DNS / Edit** for **tomtervel.no only**, and hands it over +outside of git. A token for one zone's DNS can do nothing else in the +account. Revoke it there when it is no longer needed. + +## Before the first push + +The file was written from what the zone publishes, which is not all of +it: a record Cloudflare holds that nobody looks up from outside would +not be in it, and `push` deletes what is not in the file. So once: + +```sh +dnscontrol get-zones --format=js cloudflare - tomtervel.no > /tmp/tomtervel-live.js +``` + +and compare it with `dnsconfig.js`. Anything there that is still used +goes into the file. Then `preview`, and read every line of it: the +first push is meant to change the apex and `www` (from proxied to +kasse), add `id`, `chat` and `_dmarc`, and change the SPF record. +Nothing about Zoho's MX should appear. + +## Order, when the vel moves here + +1. On kasse, `kasse-dkim-tomtervel.sh` makes the vel's signing key and + prints its public half. Put it in `KASSE_DKIM`. +2. `dnscontrol push`. The apex leaves the old website at this moment. +3. `.env` here: `PORTAL_HOST=tomtervel.no`, `ID_HOST=id.tomtervel.no`, + `CHAT_HOST=chat.tomtervel.no`, and the Caddy blocks in + `host-caddy/` on kasse; Kanidm's domain is renamed before anyone + but the first person has a passkey. +4. Only then `mail.from: post@tomtervel.no` in the content repo's + `site.yaml`; before step 2 that mail fails every check. + +The hosts in `dnsconfig.js` and in `.env` are the same names and have to +stay so; DNSControl does not read `.env`. diff --git a/dns/creds.json b/dns/creds.json new file mode 100644 index 0000000..d64c6f7 --- /dev/null +++ b/dns/creds.json @@ -0,0 +1,7 @@ +{ + "none": { "TYPE": "NONE" }, + "cloudflare": { + "TYPE": "CLOUDFLAREAPI", + "apitoken": "$CLOUDFLARE_API_TOKEN" + } +} diff --git a/dns/dnsconfig.js b/dns/dnsconfig.js new file mode 100644 index 0000000..9f6b1fa --- /dev/null +++ b/dns/dnsconfig.js @@ -0,0 +1,60 @@ +// tomtervel.no, all of it, as DNSControl applies it to Cloudflare. +// +// cd dns +// CLOUDFLARE_API_TOKEN=... dnscontrol preview # what would change +// CLOUDFLARE_API_TOKEN=... dnscontrol push # change it +// +// This file is the whole zone: a record that is in Cloudflare and not +// here is deleted on push. `preview` lists every deletion first, so read +// it before every push. See dns/README.md. + +var REG_NONE = NewRegistrar("none"); +var DSP_CLOUDFLARE = NewDnsProvider("cloudflare"); + +// kasse: Klingenberg Bygg's host, where the vel's stack runs (portal, +// Kanidm, chat, gdo) behind kasse's own Caddy, and whose postfix sends +// the vel's mail. Move the vel to a host of its own: change this. +var KASSE = "195.159.177.252"; + +// kasse's rspamd signs mail From @tomtervel.no with this key (selector +// `kasse`). It is the public half, printed by kasse-dkim-tomtervel.sh; +// the private half stays on kasse. Empty: no record yet, and the site +// must keep sending from klingenbergbygg.no (site.yaml `mail.from`). +var KASSE_DKIM = ""; + +D("tomtervel.no", REG_NONE, DnsProvider(DSP_CLOUDFLARE), + DefaultTTL(1), // Cloudflare's "auto" + + // The site, the login and the chat. Not proxied: kasse's Caddy gets + // the certificates itself, and the login's cookies and the chat's + // websockets reach it as they are. + A("@", KASSE, CF_PROXY_OFF), + CNAME("www", "tomtervel.no.", CF_PROXY_OFF), + A("id", KASSE, CF_PROXY_OFF), + A("chat", KASSE, CF_PROXY_OFF), + + // The board's inbox, post@tomtervel.no, is at Zoho. + MX("@", 10, "mx.zoho.eu."), + MX("@", 20, "mx2.zoho.eu."), + MX("@", 50, "mx3.zoho.eu."), + TXT("@", "zoho-verification=zb14986736.zmverify.zoho.eu"), + + // Who may send as @tomtervel.no: Zoho, for the board's own mail, and + // kasse, for the site's. One SPF record only; two break both. + TXT("@", "v=spf1 include:zoho.eu ip4:" + KASSE + " ~all"), + + // Reports only, to start. Tighten to p=quarantine once the reports + // show both Zoho and kasse passing. + TXT("_dmarc", "v=DMARC1; p=none; rua=mailto:post@tomtervel.no"), + + // Zoho's DKIM key, as it stands today: on the zone's apex, where no + // receiver looks for it, so mail from Zoho is unsigned in effect. Zoho + // Mail's admin (Domains -> Email configuration -> DKIM) names its + // selector; move it to TXT("._domainkey", ...) and drop it here. + TXT("@", "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCA2wb0ETspWV0uV8xjxBb4s0e+Ybe7Uh1CJgboXvZmBdZyUEfduuUXwBpJD+XuIXoaE16zGzeheVxwzMy4tvLa/FQ9XDZVQOSgUUoSyta1MHi5pPjgpmhqngEQczCrBzBuK/ew+biIdwNhyEHB738mxHTDTyY013Pob1cU9EFB+wIDAQAB"), + + // Left from before; whoever set it up may still use Search Console. + TXT("@", "google-site-verification=ElKwNmdiHDNHHrK07rI9zWAIO979bkxbPOp1rWcG8ZY"), + + KASSE_DKIM ? TXT("kasse._domainkey", KASSE_DKIM) : [] +); diff --git a/kasse-dkim-tomtervel.sh b/kasse-dkim-tomtervel.sh new file mode 100755 index 0000000..1b82e68 --- /dev/null +++ b/kasse-dkim-tomtervel.sh @@ -0,0 +1,41 @@ +#!/bin/bash +# Run on kasse (see dns/README.md): rspamd signs mail From @tomtervel.no with its own DKIM key +# (selector "kasse"), beside klingenbergbygg.no and dodre.no. Additive: +# the other domains' signing is untouched. Safe to rerun. +set -euo pipefail +K=/var/lib/rspamd/dkim/tomtervel.no.kasse.key +C=/etc/rspamd/local.d/dkim_signing.conf + +sudo cp -n "$C" "$C.before-tomtervel" + +if ! sudo test -f "$K"; then + sudo rspamadm dkim_keygen -d tomtervel.no -s kasse -b 2048 -k "$K" | sudo tee /root/tomtervel-dkim-dns.txt >/dev/null +fi +sudo chown rspamd:rspamd "$K" +sudo chmod 600 "$K" + +if ! sudo grep -q '"tomtervel.no"' "$C"; then + sudo python3 - "$C" <<'PY' +import sys +p = sys.argv[1] +s = open(p).read() +block = ''' "tomtervel.no" { + path = "/var/lib/rspamd/dkim/tomtervel.no.kasse.key"; + selector = "kasse"; + } +''' +i = s.rstrip().rfind('}') # the closing brace of domain { ... } +s = s[:i] + block + s[i:] +open(p, 'w').write(s) +PY +fi + +sudo rspamadm configtest +sudo systemctl reload rspamd +systemctl is-active rspamd +echo +echo "== dkim_signing.conf" +sudo grep -vE '^\s*(#|$)' "$C" +echo +echo "== Publish this as TXT kasse._domainkey.tomtervel.no (the p= value, joined into one string):" +sudo cat /root/tomtervel-dkim-dns.txt From 213ef712c7ab2d4465ad5d25f8196fa9337e70a3 Mon Sep 17 00:00:00 2001 From: Bendik Aagaard Lynghaug Date: Sat, 7 Nov 2026 16:00:30 +0100 Subject: [PATCH 2/6] DNS: the token in dns/cloudflare.env, gitignored Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01L5fbDmSQSb6u27EiEuwzoM --- .gitignore | 1 + dns/README.md | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 27f4f02..ce5cfb5 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ nats/nats.conf certs/ .kanidm-recovered gitea-oauth.secret +dns/cloudflare.env diff --git a/dns/README.md b/dns/README.md index 5c929f4..ffef800 100644 --- a/dns/README.md +++ b/dns/README.md @@ -8,13 +8,17 @@ from any machine. ```sh paru -S dnscontrol cd dns -export CLOUDFLARE_API_TOKEN=... # see below; never commit it +# The token in dns/cloudflare.env (gitignored, chmod 600), one line: +# CLOUDFLARE_API_TOKEN=... +set -a; . ./cloudflare.env; set +a # bash; in fish: export (cat cloudflare.env) dnscontrol check # the file parses dnscontrol preview # what would change, nothing changed dnscontrol push # change it ``` `creds.json` names the token by its variable only, so it is in the repo. +The account ID is not needed (it is only for creating zones), and there +is no state to keep, so no S3 keys either. ## The token From a76e6321ee4cc7098f43d55c72532409825179ef Mon Sep 17 00:00:00 2001 From: Bendik Aagaard Lynghaug Date: Sat, 7 Nov 2026 16:29:55 +0100 Subject: [PATCH 3/6] What the stack is, in git: .env committed, the secrets on the host .env is what `podman compose` reads as is: the hosts, the versions and where the content comes from, so moving to tomtervel.no or bumping portal is a commit that the deploy workflow brings up. Nothing in it is secret. The secrets stay on kasse in secrets.env (the old .env's NATS password, OAuth2 secret and first person), and what the scripts render from it. gdo and the chat get NATS_URL from nats.env instead of interpolating the password, and portal takes its URLs and content settings from .env over portal.env, so a new host name reaches it without re-running bootstrap.sh. The deploy script on kasse is unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01L5fbDmSQSb6u27EiEuwzoM --- .env | 45 +++++++++++++++++++++++++ .env.example | 65 ------------------------------------- .gitea/workflows/deploy.yml | 10 +++--- .gitignore | 3 +- README.md | 29 ++++++++++++++--- bootstrap.sh | 13 +++++--- compose.yml | 19 +++++++++-- kanidm-setup.sh | 3 +- secrets.env.example | 21 ++++++++++++ 9 files changed, 125 insertions(+), 83 deletions(-) create mode 100644 .env delete mode 100644 .env.example create mode 100644 secrets.env.example diff --git a/.env b/.env new file mode 100644 index 0000000..3dd1995 --- /dev/null +++ b/.env @@ -0,0 +1,45 @@ +# The vel's stack as it is meant to run: hosts, versions, where the +# content comes from. Committed, and read by `podman compose` as is, so a +# change here is a reviewed commit and a push to main deploys it. +# +# Nothing secret goes here: the repo is public. The secrets are in +# secrets.env, portal.env, nats.env and chat.env on the host, made there +# by bootstrap.sh and kanidm-setup.sh (see secrets.env.example). + +# Where the site, the login and the chat are served. Each needs an A +# record pointing here (dns/dnsconfig.js) and a block in kasse's Caddy. +PORTAL_HOST=vel.klingenbergbygg.no +ID_HOST=id.vel.klingenbergbygg.no +CHAT_HOST=chat.vel.klingenbergbygg.no + +# Versions. A bump is the upgrade. +# portal: a tag of https://project.uhhm.no/uhhm/portal; keep the content +# repo's lint (PORTAL_RELEASE in its lint-and-reload.yml) on the same one. +PORTAL_RELEASE=v0.13.0 +# gdo: a tag of https://project.uhhm.no/uhhm/gdo. It hands the vel's mail +# to kasse's postfix. +GDO_RELEASE=v0.2.0 +# cnats, the chat: a bendik/cnats tag on Docker Hub, above 0.4.0. +CNATS_TAG=0.4.2 +# kron, the money service: a bendik/kron tag on Docker Hub. +KRON_RELEASE=0.1.0 +COMPOSE_PROFILES=chat + +# The content this instance serves, and reloads live on every push. +CONTENT_REPO=https://prosjekt.klingenbergbygg.no/tomtervel/questions +CONTENT_BRANCH=main +# The namespace of everything the site keeps on NATS. Must not change: a +# new name is a new, empty site. +SITE_NAME=tomtervel + +# Who is who. `memberships`: portal keeps the groups itself, and Kanidm +# only makes the accounts. Everyone a page names as responsible joins +# RESPONSIBLE_GROUP. +PEOPLE_BACKEND=memberships +RESPONSIBLE_GROUP=redaktor +OAUTH2_CLIENT_ID=tomtervel-portal + +# A project Gitea that RESPONSIBLE_GROUP may sign in to with the same +# account (kanidm-setup.sh makes its client). Empty: no Gitea. +GITEA_URL=https://prosjekt.klingenbergbygg.no +GITEA_AUTH_NAME=tomtervel diff --git a/.env.example b/.env.example deleted file mode 100644 index f307066..0000000 --- a/.env.example +++ /dev/null @@ -1,65 +0,0 @@ -# Copy to .env and fill in. Everything rendered from this (nats.conf, -# kanidm/server.toml, portal.env) is gitignored. - -# Where the site and the identity provider are served. Both need an A -# record pointing at this host before the first start (Caddy gets the -# certificates over HTTP-01). tomtervel.no itself stays where it is -# until the vel decides to point the apex here. -PORTAL_HOST=vel.klingenbergbygg.no -ID_HOST=id.vel.klingenbergbygg.no - -# The portal version to run: a tag of https://project.uhhm.no/uhhm/portal -PORTAL_RELEASE=v0.10.2 - -# The content this instance serves, and reloads live on every push. -CONTENT_REPO=https://prosjekt.klingenbergbygg.no/tomtervel/questions -CONTENT_BRANCH=main -SITE_NAME=tomtervel - -# Generated once by bootstrap.sh if left empty. -NATS_PASSWORD= - -# The first person in: portal invites this address into the site's most -# privileged group (the board, on the vel - the group whose desk may -# invite into the most groups) and mails them the one-time link, once. -# Everyone else they invite themselves, from their desk. -SEED_ADMIN_EMAIL= -SEED_ADMIN_NAME= - -# The people who ask the questions: everyone a page names as -# responsible gets an account (portal makes it at start and mails them) -# and joins this group. Kanidm names are ASCII: redaktor, "Redaktør". -RESPONSIBLE_GROUP=redaktor - -# Where the groups live. `memberships`: portal keeps them itself, a person -# is their phone number first, and Kanidm only makes the account (until a -# sign-in everyone already has, like Vipps, takes over). Unset: Kanidm -# holds the groups too. -PEOPLE_BACKEND=memberships - -# A project Gitea they may sign in to with the same account: kanidm-setup -# makes its OAuth2 client (only RESPONSIBLE_GROUP may use it) and, on the -# host that runs that Gitea, adds the sign-in source. Empty: no Gitea. -GITEA_URL=https://prosjekt.klingenbergbygg.no -GITEA_AUTH_NAME=tomtervel - -# The vel's conversations, on their own address. go-live.sh sets the two -# below once chat.env (the sign-in client's secret, gitignored) is there; -# without them `up` leaves the chat out. 0.4.0 and older say the wrong -# name on their sign-in page: use a tag above it. -#CNATS_TAG=0.4.1 -#COMPOSE_PROFILES=chat -CHAT_HOST=chat.vel.klingenbergbygg.no - -# Filled in by bootstrap.sh after it creates the Kanidm client. -OAUTH2_CLIENT_ID=tomtervel-portal -OAUTH2_CLIENT_SECRET= - -# Only for the optional runner profile: a registration token from -# prosjekt.klingenbergbygg.no -> tomtervel org -> Settings -> Actions -> Runners. -RUNNER_REGISTRATION_TOKEN= - -# The gdo version to run: a tag of https://project.uhhm.no/uhhm/gdo, -# installed from the [uhhm] Arch registry. gdo hands the vel's mail to -# the host's mail server; on kasse that is Klingenberg Bygg's postfix. -GDO_RELEASE=v0.2.0 diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 5f79f6b..fefd2dd 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -1,18 +1,20 @@ # Deploy this stack onto the host it runs on. A push that changes the # compose file, a Dockerfile or a rendered config pulls and brings the -# stack up; a portal or gdo upgrade is a commit that bumps the version in -# .env.example and, on the host, in .env. +# stack up; a portal or gdo upgrade, or a new host name, is a commit that +# changes .env. # # Runs on kasse's host runner. It is not root: it may run one script, # /usr/local/bin/deploy-tomtervel, which pulls this repo under # /srv/tomtervel and runs `podman compose up -d --build` and nothing -# else. The .env on the host - the NATS password, the OAuth2 secret, the -# Kanidm token - is never in this repo and is not touched by a deploy. +# else. The secrets on the host - secrets.env, portal.env, nats.env, +# chat.env: the NATS password, the OAuth2 secret, the Kanidm token - are +# never in this repo and are not touched by a deploy. name: deploy on: push: branches: [main] paths: + - .env - compose.yml - Caddyfile - portal/** diff --git a/.gitignore b/.gitignore index ce5cfb5..35ed6d9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -.env portal.env chat.env kanidm/server.toml @@ -7,3 +6,5 @@ certs/ .kanidm-recovered gitea-oauth.secret dns/cloudflare.env +secrets.env +nats.env diff --git a/README.md b/README.md index a96777b..f747a6f 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ i.e. as root, so Caddy can bind 80/443 and Kanidm sees a stable source IP): ```sh git clone https://prosjekt.klingenbergbygg.no/tomtervel/infrastructure /srv/tomtervel/infrastructure cd /srv/tomtervel/infrastructure -cp .env.example .env # set PORTAL_HOST and ID_HOST; DNS must point here +cp secrets.env.example secrets.env # the hosts are in .env, committed; DNS must point here sudo sh bootstrap.sh # renders configs, makes the internal cert, starts, recovers Kanidm admin sh kanidm-setup.sh # logs in, creates the portal client and desk groups, restarts portal ``` @@ -80,7 +80,7 @@ and is safe to rerun: - **Onboarding.** The `portal-onboarding` service account manages every desk group, so an invite from a desk and a `grants:` can add people to them. Its token goes into `portal.env`. -- **The first person in.** `SEED_ADMIN_EMAIL` in `.env`: at start, +- **The first person in.** `SEED_ADMIN_EMAIL` in `secrets.env`: at start, portal invites them into the most privileged group (the board) and mails the link, once. Everyone else they invite from their desk. - **The people who ask the questions.** Everyone a page names as @@ -124,9 +124,28 @@ Until then, `podman compose restart portal` picks up new content. ## Upgrading portal -Bump `PORTAL_RELEASE` in `.env` (a tag of uhhm/portal) and -`podman compose up -d --build portal`. Keep `IRIS_RELEASE` in the -content repo's workflow matched to it. +Bump `PORTAL_RELEASE` in `.env` (a tag of uhhm/portal), commit and push +to main: the deploy workflow brings it up. Keep `PORTAL_RELEASE` in the +content repo's `lint-and-reload.yml` matched to it. + +## What is in git, and what is on the host + +`.env` is committed: the hosts, the versions, where the content comes +from - everything the stack *is*. `podman compose` reads it as it is, +so a change to it is a commit, and a push to main deploys it. The repo is +public, so nothing secret goes there. + +The secrets stay on the host, made there and never in CI: `secrets.env` +(the NATS password, the OAuth2 secret, the first person's address; see +`secrets.env.example`), and what the scripts render from it - `portal.env`, +`nats.env`, `chat.env`, `nats/nats.conf`, `kanidm/server.toml`. +`bootstrap.sh` and `kanidm-setup.sh` read `.env` and then `secrets.env`. + +`kanidm/server.toml` takes `ID_HOST` when `bootstrap.sh` renders it, not on +a deploy: Kanidm's domain is renamed on purpose, once (see dns/README.md). + +The one secret in Gitea is the repo secret `CLOUDFLARE_API_TOKEN`, for +the DNS workflow. ## Backups diff --git a/bootstrap.sh b/bootstrap.sh index fc121e2..474e477 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -4,23 +4,28 @@ # and DNS for PORTAL_HOST and ID_HOST already pointing here. Run rootful # (as root) so Caddy can bind 80/443 and Kanidm sees a stable source IP. # -# cp .env.example .env # fill in the hosts +# cp secrets.env.example secrets.env # the hosts are in .env, committed # sudo sh bootstrap.sh set -eu cd "$(dirname "$0")" -[ -f .env ] || { echo "copy .env.example to .env and fill it in first"; exit 1; } +[ -f secrets.env ] || { echo "copy secrets.env.example to secrets.env first"; exit 1; } +chmod 600 secrets.env . ./.env +. ./secrets.env # A NATS password, once. if [ -z "${NATS_PASSWORD:-}" ]; then NATS_PASSWORD=$(openssl rand -base64 36 | tr -d '/+=' | cut -c1-40) - sed -i "s|^NATS_PASSWORD=.*|NATS_PASSWORD=$NATS_PASSWORD|" .env - echo "NATS_PASSWORD generated into .env" + sed -i "s|^NATS_PASSWORD=.*|NATS_PASSWORD=$NATS_PASSWORD|" secrets.env + echo "NATS_PASSWORD generated into secrets.env" fi # Rendered configs (gitignored). sed "s|\${ID_HOST}|$ID_HOST|g" kanidm/server.toml.tpl > kanidm/server.toml sed "s|\${NATS_PASSWORD}|$NATS_PASSWORD|g" nats/nats.conf.tpl > nats/nats.conf +# gdo and the chat reach NATS with the same password. +printf 'NATS_URL=nats://portal:%s@nats:4222\n' "$NATS_PASSWORD" > nats.env +chmod 600 nats.env # The onboarding token is written by kanidm-setup.sh, once; a rerun of # this script must not lose it, or every invite fails closed again. kept_token=$(grep '^KANIDM_API_TOKEN=' portal.env 2>/dev/null | head -1 || true) diff --git a/compose.yml b/compose.yml index a5ec1c3..d28b220 100644 --- a/compose.yml +++ b/compose.yml @@ -85,8 +85,20 @@ services: args: PORTAL_RELEASE: ${PORTAL_RELEASE} restart: unless-stopped + # portal.env holds the secrets (NATS, the OAuth2 secret, the + # onboarding token, Garage's keys); what the site is comes from .env + # here, and wins over the same name in portal.env. env_file: portal.env environment: + PUBLIC_URL: https://${PORTAL_HOST} + KANIDM_URL: https://${ID_HOST} + CHAT_URL: https://${CHAT_HOST} + OAUTH2_CLIENT_ID: ${OAUTH2_CLIENT_ID} + COOKIE_SECURE: "true" + CONTENT_REPO: ${CONTENT_REPO} + CONTENT_BRANCH: ${CONTENT_BRANCH} + PEOPLE_BACKEND: ${PEOPLE_BACKEND} + RESPONSIBLE_GROUP: ${RESPONSIBLE_GROUP} LEPTOS_SITE_ADDR: 0.0.0.0:3000 LEPTOS_SITE_ROOT: site LEPTOS_HASH_FILES: "true" @@ -121,8 +133,9 @@ services: args: GDO_RELEASE: ${GDO_RELEASE} restart: unless-stopped + # NATS_URL, with the password: made by bootstrap.sh, never committed. + env_file: nats.env environment: - NATS_URL: nats://portal:${NATS_PASSWORD}@nats:4222 # The host, from inside the container. Podman resolves this to the # gateway; the host's postfix listens on 0.0.0.0:25. SMTP_HOST: host.containers.internal @@ -151,14 +164,14 @@ services: image: docker.io/bendik/cnats:${CNATS_TAG:-no-tag-set} restart: unless-stopped env_file: + - nats.env - path: chat.env required: false environment: # Must be the portal's own: a new name is a new, empty chat. SITE_NAME: tomtervel - NATS_URL: nats://portal:${NATS_PASSWORD}@nats:4222 KANIDM_URL: https://${ID_HOST} - PUBLIC_URL: https://${CHAT_HOST:-chat.vel.klingenbergbygg.no} + PUBLIC_URL: https://${CHAT_HOST} COOKIE_SECURE: "true" ports: - "127.0.0.1:3070:3000" diff --git a/kanidm-setup.sh b/kanidm-setup.sh index 878d7c6..0087cb7 100755 --- a/kanidm-setup.sh +++ b/kanidm-setup.sh @@ -12,6 +12,7 @@ set -eu cd "$(dirname "$0")" . ./.env +[ -f secrets.env ] && . ./secrets.env K="kanidm -D idm_admin -H https://$ID_HOST" C=$OAUTH2_CLIENT_ID SA=portal-onboarding @@ -167,7 +168,7 @@ fi # Kanidm, at creation; a rerun keeps the one portal.env already has. setenv() { if grep -q "^$1=" portal.env; then sed -i "s|^$1=.*|$1=$2|" portal.env; else printf '%s=%s\n' "$1" "$2" >> portal.env; fi; } secret=$($K system oauth2 show-basic-secret $C 2>/dev/null | tail -1) -sed -i "s|^OAUTH2_CLIENT_SECRET=.*|OAUTH2_CLIENT_SECRET=$secret|" .env +sed -i "s|^OAUTH2_CLIENT_SECRET=.*|OAUTH2_CLIENT_SECRET=$secret|" secrets.env setenv OAUTH2_CLIENT_SECRET "$secret" if ! grep -q '^KANIDM_API_TOKEN=.' portal.env 2>/dev/null; then token=$($K service-account api-token generate $SA "portal" --readwrite 2>/dev/null | tail -1) diff --git a/secrets.env.example b/secrets.env.example new file mode 100644 index 0000000..88e1740 --- /dev/null +++ b/secrets.env.example @@ -0,0 +1,21 @@ +# Copy to secrets.env on the host (chmod 600). Never committed: what the +# stack runs as is in .env, which is. bootstrap.sh and kanidm-setup.sh +# read both, .env first. + +# Generated once by bootstrap.sh if left empty. bootstrap.sh also writes +# it into nats/nats.conf and nats.env (NATS_URL for gdo and the chat). +NATS_PASSWORD= + +# Filled in by kanidm-setup.sh after it creates the Kanidm client. +OAUTH2_CLIENT_SECRET= + +# The first person in: portal invites this address into the site's most +# privileged group (the board) and mails them the one-time link, once. +# Everyone else they invite themselves, from their desk. An address, so +# not in the public repo. +SEED_ADMIN_EMAIL= +SEED_ADMIN_NAME= + +# Only for the optional runner profile: a registration token from +# prosjekt.klingenbergbygg.no -> tomtervel org -> Settings -> Actions -> Runners. +RUNNER_REGISTRATION_TOKEN= From e183510466488ee27cf559a853f77ba93e3b9d0d Mon Sep 17 00:00:00 2001 From: Bendik Aagaard Lynghaug Date: Sat, 7 Nov 2026 16:29:56 +0100 Subject: [PATCH 4/6] DNS from CI: preview on every push, applied by hand on main dns.yml runs DNSControl on kasse's host runner with the repo secret CLOUDFLARE_API_TOKEN: check and preview on any push touching dns/, push only from Run workflow on main, so a merge is never the cutover. Push events only; the repo is public. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01L5fbDmSQSb6u27EiEuwzoM --- .gitea/workflows/dns.yml | 61 ++++++++++++++++++++++++++++++++++++++++ dns/README.md | 15 ++++++++-- 2 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 .gitea/workflows/dns.yml diff --git a/.gitea/workflows/dns.yml b/.gitea/workflows/dns.yml new file mode 100644 index 0000000..d124ce8 --- /dev/null +++ b/.gitea/workflows/dns.yml @@ -0,0 +1,61 @@ +# tomtervel.no's DNS: dns/dnsconfig.js, applied to Cloudflare by +# DNSControl on kasse's host runner. A push that touches dns/, on any +# branch, shows what would change (`preview`) and changes nothing. The +# change itself is a person's: Actions -> dns -> Run workflow, on main. +# So merging is not the cutover; pressing the button is. +# +# Push only, never pull_request: the repo is public, and a stranger's pull +# request must not run on kasse with the token. The token is the repo +# secret CLOUDFLARE_API_TOKEN, DNS edit for tomtervel.no only +# (dns/README.md). +name: dns +on: + push: + paths: + - dns/** + - .gitea/workflows/dns.yml + workflow_dispatch: + +jobs: + dns: + runs-on: fish + env: + DNSCONTROL_VERSION: 5.7.2 + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} + steps: + - name: checkout + run: | + rm -rf checkout + git clone --depth 1 --branch "${{ github.ref_name }}" "${{ github.server_url }}/${{ github.repository }}.git" checkout + + # The release binary, checked against the release's own checksums, + # kept beside the portal lint so each version is fetched once. + - name: dnscontrol + run: | + bin="/var/local/cargo-target/dnscontrol-$DNSCONTROL_VERSION" + if [ ! -x "$bin" ]; then + tmp=$(mktemp -d) + base="https://github.com/StackExchange/dnscontrol/releases/download/v$DNSCONTROL_VERSION" + tar="dnscontrol_${DNSCONTROL_VERSION}_linux_amd64.tar.gz" + curl -sfL "$base/$tar" -o "$tmp/$tar" + curl -sfL "$base/checksums.txt" -o "$tmp/checksums.txt" + (cd "$tmp" && grep " $tar\$" checksums.txt | sha256sum -c -) + tar -xzf "$tmp/$tar" -C "$tmp" dnscontrol + install -m 0755 "$tmp/dnscontrol" "$bin" + rm -rf "$tmp" + fi + "$bin" version + echo "DNSCONTROL=$bin" >> "$GITHUB_ENV" + + - name: check + working-directory: checkout/dns + run: $DNSCONTROL check + + - name: preview + working-directory: checkout/dns + run: $DNSCONTROL preview + + - name: push + if: github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/main' + working-directory: checkout/dns + run: $DNSCONTROL push diff --git a/dns/README.md b/dns/README.md index ffef800..24cf4cd 100644 --- a/dns/README.md +++ b/dns/README.md @@ -5,6 +5,16 @@ The zone is `dnsconfig.js`, applied to Cloudflare with the file with the live zone, so it can be run by anyone with the token, from any machine. +## From CI + +`.gitea/workflows/dns.yml` runs on kasse's host runner. Every push that +touches `dns/`, on any branch, runs `check` and `preview`: the run's log +is the diff, and nothing changes. Applying it is a person pressing +**Actions -> dns -> Run workflow** on `main`, so merging is never the +cutover by accident. The token is the repo secret `CLOUDFLARE_API_TOKEN`. + +## By hand + ```sh paru -S dnscontrol cd dns @@ -48,8 +58,9 @@ Nothing about Zoho's MX should appear. 1. On kasse, `kasse-dkim-tomtervel.sh` makes the vel's signing key and prints its public half. Put it in `KASSE_DKIM`. -2. `dnscontrol push`. The apex leaves the old website at this moment. -3. `.env` here: `PORTAL_HOST=tomtervel.no`, `ID_HOST=id.tomtervel.no`, +2. Actions -> dns -> Run workflow on main (or `dnscontrol push`). The apex + leaves the old website at this moment. +3. A commit to `.env`: `PORTAL_HOST=tomtervel.no`, `ID_HOST=id.tomtervel.no`, `CHAT_HOST=chat.tomtervel.no`, and the Caddy blocks in `host-caddy/` on kasse; Kanidm's domain is renamed before anyone but the first person has a passkey. From efdae35d15f3efa3a06656a160c804a534f29a5c Mon Sep 17 00:00:00 2001 From: Bendik Aagaard Lynghaug Date: Sat, 7 Nov 2026 16:30:40 +0100 Subject: [PATCH 5/6] DNS workflow: dnscontrol in the runner user's cache Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01L5fbDmSQSb6u27EiEuwzoM --- .gitea/workflows/dns.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitea/workflows/dns.yml b/.gitea/workflows/dns.yml index d124ce8..68f2137 100644 --- a/.gitea/workflows/dns.yml +++ b/.gitea/workflows/dns.yml @@ -29,10 +29,11 @@ jobs: git clone --depth 1 --branch "${{ github.ref_name }}" "${{ github.server_url }}/${{ github.repository }}.git" checkout # The release binary, checked against the release's own checksums, - # kept beside the portal lint so each version is fetched once. + # kept in the runner user's cache so each version is fetched once. - name: dnscontrol run: | - bin="/var/local/cargo-target/dnscontrol-$DNSCONTROL_VERSION" + mkdir -p "$HOME/.cache/dnscontrol" + bin="$HOME/.cache/dnscontrol/dnscontrol-$DNSCONTROL_VERSION" if [ ! -x "$bin" ]; then tmp=$(mktemp -d) base="https://github.com/StackExchange/dnscontrol/releases/download/v$DNSCONTROL_VERSION" From 3bcd2bc68cc46de32f8b511688cdbad980b45b21 Mon Sep 17 00:00:00 2001 From: Bendik Aagaard Lynghaug Date: Sat, 7 Nov 2026 16:44:51 +0100 Subject: [PATCH 6/6] Every value in Gitea's Actions variables and secrets The stack's settings are repo variables, its secrets repo secrets, and NATS_PASSWORD an org secret the content repo can share. The deploy workflow checks they are all set, writes .env, portal.env, nats.env and chat.env into the runner's own directory, and the root script (now in the repo as deploy-tomtervel) pulls main, puts them in place as root's and brings the stack up. So nothing is edited on the host or committed: a host name or a version is a variable changed and a Run workflow. nats.conf reads NATS_PASSWORD from its environment, so it is committed rather than rendered. bootstrap.sh only makes Kanidm's config and certificate and recovers its admins; kanidm-setup.sh reads portal.env, and leaves the two secrets it makes in kanidm-setup.secrets for gt. The DNS workflow uses actions/checkout and actions/cache (the runner's cache server) for dnscontrol, at the DNSCONTROL_VERSION variable. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01L5fbDmSQSb6u27EiEuwzoM --- .env | 45 ------------ .gitea/workflows/deploy.yml | 116 ++++++++++++++++++++++++++---- .gitea/workflows/dns.yml | 59 +++++++-------- .gitignore | 4 +- README.md | 65 +++++++++++------ bootstrap.sh | 71 +++++++----------- compose.yml | 16 ++--- deploy-tomtervel | 50 +++++++++++++ dns/README.md | 8 +-- kanidm-setup.sh | 29 ++++---- nats/{nats.conf.tpl => nats.conf} | 5 +- secrets.env.example | 21 ------ 12 files changed, 282 insertions(+), 207 deletions(-) delete mode 100644 .env create mode 100644 deploy-tomtervel rename nats/{nats.conf.tpl => nats.conf} (64%) delete mode 100644 secrets.env.example diff --git a/.env b/.env deleted file mode 100644 index 3dd1995..0000000 --- a/.env +++ /dev/null @@ -1,45 +0,0 @@ -# The vel's stack as it is meant to run: hosts, versions, where the -# content comes from. Committed, and read by `podman compose` as is, so a -# change here is a reviewed commit and a push to main deploys it. -# -# Nothing secret goes here: the repo is public. The secrets are in -# secrets.env, portal.env, nats.env and chat.env on the host, made there -# by bootstrap.sh and kanidm-setup.sh (see secrets.env.example). - -# Where the site, the login and the chat are served. Each needs an A -# record pointing here (dns/dnsconfig.js) and a block in kasse's Caddy. -PORTAL_HOST=vel.klingenbergbygg.no -ID_HOST=id.vel.klingenbergbygg.no -CHAT_HOST=chat.vel.klingenbergbygg.no - -# Versions. A bump is the upgrade. -# portal: a tag of https://project.uhhm.no/uhhm/portal; keep the content -# repo's lint (PORTAL_RELEASE in its lint-and-reload.yml) on the same one. -PORTAL_RELEASE=v0.13.0 -# gdo: a tag of https://project.uhhm.no/uhhm/gdo. It hands the vel's mail -# to kasse's postfix. -GDO_RELEASE=v0.2.0 -# cnats, the chat: a bendik/cnats tag on Docker Hub, above 0.4.0. -CNATS_TAG=0.4.2 -# kron, the money service: a bendik/kron tag on Docker Hub. -KRON_RELEASE=0.1.0 -COMPOSE_PROFILES=chat - -# The content this instance serves, and reloads live on every push. -CONTENT_REPO=https://prosjekt.klingenbergbygg.no/tomtervel/questions -CONTENT_BRANCH=main -# The namespace of everything the site keeps on NATS. Must not change: a -# new name is a new, empty site. -SITE_NAME=tomtervel - -# Who is who. `memberships`: portal keeps the groups itself, and Kanidm -# only makes the accounts. Everyone a page names as responsible joins -# RESPONSIBLE_GROUP. -PEOPLE_BACKEND=memberships -RESPONSIBLE_GROUP=redaktor -OAUTH2_CLIENT_ID=tomtervel-portal - -# A project Gitea that RESPONSIBLE_GROUP may sign in to with the same -# account (kanidm-setup.sh makes its client). Empty: no Gitea. -GITEA_URL=https://prosjekt.klingenbergbygg.no -GITEA_AUTH_NAME=tomtervel diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index fefd2dd..1afd117 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -1,32 +1,124 @@ -# Deploy this stack onto the host it runs on. A push that changes the -# compose file, a Dockerfile or a rendered config pulls and brings the -# stack up; a portal or gdo upgrade, or a new host name, is a commit that -# changes .env. +# Deploy this stack onto kasse. Every value the stack runs with is an +# Actions variable (vars.*: hosts, versions, where the content comes from) +# or secret (secrets.*: passwords, tokens, the first person's address) of +# this repo, or of the tomtervel org for what other repos share +# (NATS_PASSWORD, which the content repo's reload uses too). The table is +# in README.md. A new host name or a portal bump is a variable changed and +# then Run workflow; a change to the files here deploys on push. # -# Runs on kasse's host runner. It is not root: it may run one script, -# /usr/local/bin/deploy-tomtervel, which pulls this repo under -# /srv/tomtervel and runs `podman compose up -d --build` and nothing -# else. The secrets on the host - secrets.env, portal.env, nats.env, -# chat.env: the NATS password, the OAuth2 secret, the Kanidm token - are -# never in this repo and are not touched by a deploy. +# Runs on kasse's host runner, which is not root. It writes the env files +# into its own directory, and may run one script as root, +# /usr/local/bin/deploy-tomtervel (deploy-tomtervel here), which pulls +# main, puts those files in place and runs `podman compose up -d --build`. name: deploy on: push: branches: [main] paths: - - .env - compose.yml - Caddyfile - portal/** - gdo/** + - nats/nats.conf - kanidm/server.toml.tpl - - nats/nats.conf.tpl + - deploy-tomtervel - .gitea/workflows/deploy.yml workflow_dispatch: jobs: deploy: + if: github.ref == 'refs/heads/main' runs-on: fish + env: + STAGE: /var/lib/gitea-runner/tomtervel-env + # What the stack is. + PORTAL_HOST: ${{ vars.PORTAL_HOST }} + ID_HOST: ${{ vars.ID_HOST }} + CHAT_HOST: ${{ vars.CHAT_HOST }} + PORTAL_RELEASE: ${{ vars.PORTAL_RELEASE }} + GDO_RELEASE: ${{ vars.GDO_RELEASE }} + CNATS_TAG: ${{ vars.CNATS_TAG }} + KRON_RELEASE: ${{ vars.KRON_RELEASE }} + COMPOSE_PROFILES: ${{ vars.COMPOSE_PROFILES }} + CONTENT_REPO: ${{ vars.CONTENT_REPO }} + CONTENT_BRANCH: ${{ vars.CONTENT_BRANCH }} + SITE_NAME: ${{ vars.SITE_NAME }} + PEOPLE_BACKEND: ${{ vars.PEOPLE_BACKEND }} + RESPONSIBLE_GROUP: ${{ vars.RESPONSIBLE_GROUP }} + OAUTH2_CLIENT_ID: ${{ vars.OAUTH2_CLIENT_ID }} + CHAT_OAUTH2_CLIENT_ID: ${{ vars.CHAT_OAUTH2_CLIENT_ID }} + NATS_ADOPT_LEGACY: ${{ vars.NATS_ADOPT_LEGACY }} + GARAGE_S3_ENDPOINT: ${{ vars.GARAGE_S3_ENDPOINT }} + GARAGE_UPLOADS_BUCKET: ${{ vars.GARAGE_UPLOADS_BUCKET }} + # What it runs with. + NATS_PASSWORD: ${{ secrets.NATS_PASSWORD }} + OAUTH2_CLIENT_SECRET: ${{ secrets.OAUTH2_CLIENT_SECRET }} + CHAT_OAUTH2_CLIENT_SECRET: ${{ secrets.CHAT_OAUTH2_CLIENT_SECRET }} + KANIDM_API_TOKEN: ${{ secrets.KANIDM_API_TOKEN }} + GARAGE_ACCESS_KEY: ${{ secrets.GARAGE_ACCESS_KEY }} + GARAGE_SECRET_KEY: ${{ secrets.GARAGE_SECRET_KEY }} + SEED_ADMIN_EMAIL: ${{ secrets.SEED_ADMIN_EMAIL }} + SEED_ADMIN_NAME: ${{ secrets.SEED_ADMIN_NAME }} steps: + # A missing value stops here, by name, before anything on the host + # is touched. Only names are printed: the log is public. + - name: Write the env files + run: | + missing= + for v in PORTAL_HOST ID_HOST CHAT_HOST PORTAL_RELEASE GDO_RELEASE CNATS_TAG \ + KRON_RELEASE CONTENT_REPO CONTENT_BRANCH SITE_NAME PEOPLE_BACKEND \ + RESPONSIBLE_GROUP OAUTH2_CLIENT_ID CHAT_OAUTH2_CLIENT_ID \ + GARAGE_S3_ENDPOINT GARAGE_UPLOADS_BUCKET \ + NATS_PASSWORD OAUTH2_CLIENT_SECRET CHAT_OAUTH2_CLIENT_SECRET \ + KANIDM_API_TOKEN GARAGE_ACCESS_KEY GARAGE_SECRET_KEY SEED_ADMIN_EMAIL; do + [ -n "${!v}" ] || missing="$missing $v" + done + [ -z "$missing" ] || { echo "not set as an Actions variable or secret:$missing"; exit 1; } + + umask 077 + mkdir -p "$STAGE" + # Interpolated by compose: hosts, versions, profiles. No secret. + cat > "$STAGE/.env" < "$STAGE/portal.env" < "$STAGE/nats.env" < "$STAGE/chat.env" <> "$GITHUB_ENV" + [ -n "$DNSCONTROL_VERSION" ] || { echo "the DNSCONTROL_VERSION variable is not set"; exit 1; } + mkdir -p ~/.cache/dnscontrol + cd "$(mktemp -d)" + base="https://github.com/StackExchange/dnscontrol/releases/download/v$DNSCONTROL_VERSION" + tar="dnscontrol_${DNSCONTROL_VERSION}_linux_amd64.tar.gz" + curl -sfLO "$base/$tar" + curl -sfLO "$base/checksums.txt" + grep " $tar\$" checksums.txt | sha256sum -c - + tar -xzf "$tar" dnscontrol + install -m 0755 dnscontrol ~/.cache/dnscontrol/dnscontrol - name: check - working-directory: checkout/dns - run: $DNSCONTROL check + working-directory: dns + run: ~/.cache/dnscontrol/dnscontrol check - name: preview - working-directory: checkout/dns - run: $DNSCONTROL preview + working-directory: dns + run: ~/.cache/dnscontrol/dnscontrol preview - name: push if: github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/main' - working-directory: checkout/dns - run: $DNSCONTROL push + working-directory: dns + run: ~/.cache/dnscontrol/dnscontrol push diff --git a/.gitignore b/.gitignore index 35ed6d9..acf63f5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,10 @@ +.env portal.env chat.env kanidm/server.toml -nats/nats.conf certs/ .kanidm-recovered gitea-oauth.secret dns/cloudflare.env -secrets.env nats.env +kanidm-setup.secrets diff --git a/README.md b/README.md index f747a6f..5b20b41 100644 --- a/README.md +++ b/README.md @@ -20,14 +20,18 @@ so nothing about the vel's members or records depends on anyone else. ## First start On a fresh Linux host with podman + podman-compose and openssl (run rootful, -i.e. as root, so Caddy can bind 80/443 and Kanidm sees a stable source IP): +i.e. as root, so Caddy can bind 80/443 and Kanidm 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`): ```sh git clone https://prosjekt.klingenbergbygg.no/tomtervel/infrastructure /srv/tomtervel/infrastructure cd /srv/tomtervel/infrastructure -cp secrets.env.example secrets.env # the hosts are in .env, committed; DNS must point here -sudo sh bootstrap.sh # renders configs, makes the internal cert, starts, recovers Kanidm admin -sh kanidm-setup.sh # logs in, creates the portal client and desk groups, restarts portal +sudo install -m 0755 deploy-tomtervel /usr/local/bin/deploy-tomtervel # and the sudoers line in it +sudo ID_HOST=id.tomtervel.no sh bootstrap.sh # Kanidm's config and certificate +# Actions -> deploy -> Run workflow # the env files, and the stack up +sudo sh bootstrap.sh # recovers the Kanidm admins, once +sh kanidm-setup.sh # the portal client and desk groups; set the two secrets it names ``` `bootstrap.sh` prints the `admin` and `idm_admin` passwords once; @@ -80,7 +84,7 @@ and is safe to rerun: - **Onboarding.** The `portal-onboarding` service account manages every desk group, so an invite from a desk and a `grants:` can add people to them. Its token goes into `portal.env`. -- **The first person in.** `SEED_ADMIN_EMAIL` in `secrets.env`: at start, +- **The first person in.** The `SEED_ADMIN_EMAIL` secret: at start, portal invites them into the most privileged group (the board) and mails the link, once. Everyone else they invite from their desk. - **The people who ask the questions.** Everyone a page names as @@ -124,28 +128,47 @@ Until then, `podman compose restart portal` picks up new content. ## Upgrading portal -Bump `PORTAL_RELEASE` in `.env` (a tag of uhhm/portal), commit and push -to main: the deploy workflow brings it up. Keep `PORTAL_RELEASE` in the +Set the `PORTAL_RELEASE` variable (a tag of uhhm/portal) and run the +deploy workflow. Keep `PORTAL_RELEASE` in the content repo's `lint-and-reload.yml` matched to it. -## What is in git, and what is on the host +## Variables and secrets -`.env` is committed: the hosts, the versions, where the content comes -from - everything the stack *is*. `podman compose` reads it as it is, -so a change to it is a commit, and a push to main deploys it. The repo is -public, so nothing secret goes there. +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). -The secrets stay on the host, made there and never in CI: `secrets.env` -(the NATS password, the OAuth2 secret, the first person's address; see -`secrets.env.example`), and what the scripts render from it - `portal.env`, -`nats.env`, `chat.env`, `nats/nats.conf`, `kanidm/server.toml`. -`bootstrap.sh` and `kanidm-setup.sh` read `.env` and then `secrets.env`. +| name | kind | where | what | +|---|---|---|---| +| `PORTAL_HOST`, `ID_HOST`, `CHAT_HOST` | variable | repo | the site, the login, the chat; each needs DNS (`dns/`) and a kasse Caddy block | +| `PORTAL_RELEASE` | variable | repo | a uhhm/portal tag; keep the content repo's lint on the same | +| `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 Kanidm clients of the site and the 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`, `KANIDM_API_TOKEN` | secret | repo | made by `kanidm-setup.sh`, which says how to set them | +| `CHAT_OAUTH2_CLIENT_SECRET` | secret | repo | the chat's Kanidm 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/`) | +| `DNSCONTROL_VERSION` | variable | repo | the DNS workflow's dnscontrol | -`kanidm/server.toml` takes `ID_HOST` when `bootstrap.sh` renders it, not on -a deploy: Kanidm's domain is renamed on purpose, once (see dns/README.md). +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. -The one secret in Gitea is the repo secret `CLOUDFLARE_API_TOKEN`, for -the DNS workflow. +What is not a value stays on the host, made there once: Kanidm's config +and internal certificate (`bootstrap.sh`), and Garage's node secret. +`kanidm/server.toml` takes `ID_HOST` once: Kanidm's domain is renamed on +purpose, not by a variable changed (see dns/README.md). ## Backups diff --git a/bootstrap.sh b/bootstrap.sh index 474e477..f6974f2 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -1,50 +1,26 @@ #!/bin/sh -# First start on a fresh host. Idempotent: rerunning renders configs -# again and skips what exists. Needs podman + podman-compose and openssl, -# and DNS for PORTAL_HOST and ID_HOST already pointing here. Run rootful -# (as root) so Caddy can bind 80/443 and Kanidm sees a stable source IP. +# What a fresh host needs before the first deploy, and what only exists +# after it. Everything the stack runs with is an Actions variable or +# secret (README.md), written onto the host by the deploy workflow; this +# makes the two things that are not values: Kanidm's config and its +# internal certificate. Idempotent: rerunning skips what exists. # -# cp secrets.env.example secrets.env # the hosts are in .env, committed -# sudo sh bootstrap.sh +# sudo ID_HOST=id.tomtervel.no sh bootstrap.sh # before the first deploy +# (Actions -> deploy -> Run workflow) +# sudo sh bootstrap.sh # after: Kanidm admin recovery +# +# Run rootful (as root) so Kanidm sees a stable source IP. set -eu cd "$(dirname "$0")" -[ -f secrets.env ] || { echo "copy secrets.env.example to secrets.env first"; exit 1; } -chmod 600 secrets.env -. ./.env -. ./secrets.env +[ -f .env ] && . ./.env +: "${ID_HOST:?ID_HOST=id. sh bootstrap.sh, or run the deploy workflow first}" -# A NATS password, once. -if [ -z "${NATS_PASSWORD:-}" ]; then - NATS_PASSWORD=$(openssl rand -base64 36 | tr -d '/+=' | cut -c1-40) - sed -i "s|^NATS_PASSWORD=.*|NATS_PASSWORD=$NATS_PASSWORD|" secrets.env - echo "NATS_PASSWORD generated into secrets.env" -fi - -# Rendered configs (gitignored). -sed "s|\${ID_HOST}|$ID_HOST|g" kanidm/server.toml.tpl > kanidm/server.toml -sed "s|\${NATS_PASSWORD}|$NATS_PASSWORD|g" nats/nats.conf.tpl > nats/nats.conf -# gdo and the chat reach NATS with the same password. -printf 'NATS_URL=nats://portal:%s@nats:4222\n' "$NATS_PASSWORD" > nats.env -chmod 600 nats.env -# The onboarding token is written by kanidm-setup.sh, once; a rerun of -# this script must not lose it, or every invite fails closed again. -kept_token=$(grep '^KANIDM_API_TOKEN=' portal.env 2>/dev/null | head -1 || true) -cat > portal.env <> portal.env -chmod 600 portal.env +# Kanidm's domain is fixed here, once: renaming it later is Kanidm's own +# `domain rename`, not a variable changed. +[ -f kanidm/server.toml ] || { + sed "s|\${ID_HOST}|$ID_HOST|g" kanidm/server.toml.tpl > kanidm/server.toml + echo "kanidm/server.toml made for $ID_HOST" +} # Kanidm's internal certificate: Caddy holds the public one. mkdir -p certs @@ -56,12 +32,13 @@ if [ ! -f certs/kanidm-key.pem ]; then echo "internal Kanidm certificate made" fi -podman compose up -d --build -echo "containers up; Caddy is fetching certificates for $PORTAL_HOST and $ID_HOST" - # The Kanidm admin accounts exist only after the first start; their -# passwords are set by recovery. Do this once; the output is the -# password, shown once. +# passwords are set by recovery. Once; the output is the password, shown +# once. +if ! podman compose ps --format '{{.Names}}' 2>/dev/null | grep -q kanidm; then + echo "Kanidm is not running yet: run the deploy workflow, then this again." + exit 0 +fi if [ ! -f .kanidm-recovered ]; then echo echo "=== Kanidm admin recovery (write these passwords down) ===" diff --git a/compose.yml b/compose.yml index d28b220..b98855b 100644 --- a/compose.yml +++ b/compose.yml @@ -42,6 +42,8 @@ services: image: nats:2.14.6-alpine restart: unless-stopped command: ["-c", "/etc/nats/nats.conf"] + # NATS_PASSWORD, which nats.conf reads from the environment. + env_file: nats.env volumes: - ./nats/nats.conf:/etc/nats/nats.conf:ro - nats_data:/data @@ -85,20 +87,10 @@ services: args: PORTAL_RELEASE: ${PORTAL_RELEASE} restart: unless-stopped - # portal.env holds the secrets (NATS, the OAuth2 secret, the - # onboarding token, Garage's keys); what the site is comes from .env - # here, and wins over the same name in portal.env. + # Written by the deploy workflow from the Actions variables and + # secrets (README.md has the table). env_file: portal.env environment: - PUBLIC_URL: https://${PORTAL_HOST} - KANIDM_URL: https://${ID_HOST} - CHAT_URL: https://${CHAT_HOST} - OAUTH2_CLIENT_ID: ${OAUTH2_CLIENT_ID} - COOKIE_SECURE: "true" - CONTENT_REPO: ${CONTENT_REPO} - CONTENT_BRANCH: ${CONTENT_BRANCH} - PEOPLE_BACKEND: ${PEOPLE_BACKEND} - RESPONSIBLE_GROUP: ${RESPONSIBLE_GROUP} LEPTOS_SITE_ADDR: 0.0.0.0:3000 LEPTOS_SITE_ROOT: site LEPTOS_HASH_FILES: "true" diff --git a/deploy-tomtervel b/deploy-tomtervel new file mode 100644 index 0000000..b1b3487 --- /dev/null +++ b/deploy-tomtervel @@ -0,0 +1,50 @@ +#!/usr/bin/env bash +# Tomter Vel's stack, brought up from its own repo. This is the whole of +# what CI may do as root for the vel: pull main, put the env files the +# deploy workflow wrote in place, and bring the compose project up. It +# takes no arguments, so there is nothing for a workflow to point +# somewhere else. +# +# The env files come from the repo's and org's Actions variables and +# secrets: the workflow writes them, as the runner, into $stage (the +# runner's own, 0700), and this script moves them into the checkout as +# root's, 0600, and leaves nothing behind in $stage. +# +# Install: sudo install -m 0755 -o root -g root \ +# deploy-tomtervel /usr/local/bin/deploy-tomtervel +# Grant: gitea-runner ALL=(root) NOPASSWD: /usr/local/bin/deploy-tomtervel +set -euo pipefail + +root=/srv/tomtervel/infrastructure +stage=/var/lib/gitea-runner/tomtervel-env +files=(.env portal.env nats.env chat.env) + +[ -d "$root/.git" ] || { echo "no checkout at $root - bootstrap it first" >&2; exit 1; } +for f in "${files[@]}"; do + [ -s "$stage/$f" ] || { echo "no $stage/$f - the deploy workflow writes it" >&2; exit 1; } +done +cd "$root" + +git fetch --quiet origin main +git reset --hard --quiet origin/main + +for f in "${files[@]}"; do + install -m 0600 -o root -g root "$stage/$f" "$root/$f" + rm -f "$stage/$f" +done + +# --build so a bumped PORTAL_RELEASE or GDO_RELEASE is actually fetched. +podman compose up -d --build + +# The vel's portal, reached the way the host Caddy reaches it. No +# forwarded header on purpose: a check that sends the one Caddy sends +# cannot tell you the site is broken for everything that is not Caddy. +code=000 +for n in $(seq 1 30); do + code=$(curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:3050/ || true) + [ "$code" = 200 ] && break + sleep 2 +done +podman compose ps --format '{{.Names}} {{.State}}' || true +echo "portal http $code after $((n * 2))s" +[ "$code" = 200 ] diff --git a/dns/README.md b/dns/README.md index 24cf4cd..a87fedf 100644 --- a/dns/README.md +++ b/dns/README.md @@ -60,12 +60,12 @@ Nothing about Zoho's MX should appear. prints its public half. Put it in `KASSE_DKIM`. 2. Actions -> dns -> Run workflow on main (or `dnscontrol push`). The apex leaves the old website at this moment. -3. A commit to `.env`: `PORTAL_HOST=tomtervel.no`, `ID_HOST=id.tomtervel.no`, - `CHAT_HOST=chat.tomtervel.no`, and the Caddy blocks in +3. The Actions variables `PORTAL_HOST=tomtervel.no`, `ID_HOST=id.tomtervel.no`, + `CHAT_HOST=chat.tomtervel.no`, the deploy workflow run, and the Caddy blocks in `host-caddy/` on kasse; Kanidm's domain is renamed before anyone but the first person has a passkey. 4. Only then `mail.from: post@tomtervel.no` in the content repo's `site.yaml`; before step 2 that mail fails every check. -The hosts in `dnsconfig.js` and in `.env` are the same names and have to -stay so; DNSControl does not read `.env`. +The hosts in `dnsconfig.js` and the `*_HOST` variables are the same names +and have to stay so; DNSControl does not read the variables. diff --git a/kanidm-setup.sh b/kanidm-setup.sh index 0087cb7..38c76e8 100755 --- a/kanidm-setup.sh +++ b/kanidm-setup.sh @@ -12,7 +12,11 @@ set -eu cd "$(dirname "$0")" . ./.env -[ -f secrets.env ] && . ./secrets.env +. ./portal.env +# The project Gitea that RESPONSIBLE_GROUP signs in to. Not an Actions +# variable: Gitea refuses names starting GITEA_. Empty: no Gitea. +GITEA_URL=${GITEA_URL-https://prosjekt.klingenbergbygg.no} +GITEA_AUTH_NAME=${GITEA_AUTH_NAME:-tomtervel} K="kanidm -D idm_admin -H https://$ID_HOST" C=$OAUTH2_CLIENT_ID SA=portal-onboarding @@ -164,33 +168,34 @@ if [ -n "${GITEA_URL:-}" ] && [ -n "${RESPONSIBLE_GROUP:-}" ]; then fi fi -# Secrets into portal.env, never printed. The token is shown once, by -# Kanidm, at creation; a rerun keeps the one portal.env already has. +# The two secrets Kanidm makes, never printed: into portal.env so portal +# has them now, and into kanidm-setup.secrets for the repo's Actions +# secrets, which the next deploy writes portal.env from. The token is +# shown once, by Kanidm, at creation; a rerun keeps the one there is. setenv() { if grep -q "^$1=" portal.env; then sed -i "s|^$1=.*|$1=$2|" portal.env; else printf '%s=%s\n' "$1" "$2" >> portal.env; fi; } +(umask 077; : > kanidm-setup.secrets) secret=$($K system oauth2 show-basic-secret $C 2>/dev/null | tail -1) -sed -i "s|^OAUTH2_CLIENT_SECRET=.*|OAUTH2_CLIENT_SECRET=$secret|" secrets.env setenv OAUTH2_CLIENT_SECRET "$secret" +printf 'OAUTH2_CLIENT_SECRET=%s\n' "$secret" >> kanidm-setup.secrets if ! grep -q '^KANIDM_API_TOKEN=.' portal.env 2>/dev/null; then token=$($K service-account api-token generate $SA "portal" --readwrite 2>/dev/null | tail -1) if [ -n "$token" ]; then setenv KANIDM_API_TOKEN "$token" + printf 'KANIDM_API_TOKEN=%s\n' "$token" >> kanidm-setup.secrets echo "onboarding token written to portal.env" else echo "warning: could not generate the onboarding token - invites will fail closed until it is set" >&2 echo " retry by hand: $K service-account api-token generate $SA portal --readwrite" >&2 fi fi -# The first person in, if .env names one: portal invites them into the -# most privileged group on its next start, once, and mails the link. -if [ -n "${SEED_ADMIN_EMAIL:-}" ]; then setenv SEED_ADMIN_EMAIL "$SEED_ADMIN_EMAIL"; fi -if [ -n "${SEED_ADMIN_NAME:-}" ]; then setenv SEED_ADMIN_NAME "$SEED_ADMIN_NAME"; fi -if [ -n "${RESPONSIBLE_GROUP:-}" ]; then setenv RESPONSIBLE_GROUP "$RESPONSIBLE_GROUP"; fi -# Where the groups live: `memberships` keeps them in portal, and this -# Kanidm only makes the accounts (portal docs, Memberships). -if [ -n "${PEOPLE_BACKEND:-}" ]; then setenv PEOPLE_BACKEND "$PEOPLE_BACKEND"; fi podman compose up -d portal echo "portal restarted: signs in through $C, onboards through $SA, desks: $(echo $groups)" +echo +echo "Into the repo's Actions secrets, or the next deploy undoes them (from a" +echo "machine with gt; kanidm-setup.secrets is root's, here):" +echo " ssh kasse sudo cat $(pwd)/kanidm-setup.secrets | gt secret set -R tomtervel/infrastructure --env-file -" +echo "then remove it: sudo rm $(pwd)/kanidm-setup.secrets" if [ -n "${SEED_ADMIN_EMAIL:-}" ]; then echo "the first person, $SEED_ADMIN_EMAIL, gets their invite from portal now (once)"; fi echo echo "Everyone after that is invited from a desk. By hand, if ever needed:" diff --git a/nats/nats.conf.tpl b/nats/nats.conf similarity index 64% rename from nats/nats.conf.tpl rename to nats/nats.conf index 00a0563..213235b 100644 --- a/nats/nats.conf.tpl +++ b/nats/nats.conf @@ -1,5 +1,6 @@ # NATS with JetStream: portal's records, events and projections live -# here. bootstrap.sh renders this into nats.conf from .env. +# here. The password is NATS_PASSWORD from the container's environment +# (nats.env, written by the deploy workflow from the org secret). port: 4222 http_port: 8222 @@ -10,7 +11,7 @@ max_connections: 1000 # (nats://user:pass@host) behave the same in every client library. authorization { users = [ - { user: "portal", password: "${NATS_PASSWORD}" } + { user: "portal", password: $NATS_PASSWORD } ] } diff --git a/secrets.env.example b/secrets.env.example deleted file mode 100644 index 88e1740..0000000 --- a/secrets.env.example +++ /dev/null @@ -1,21 +0,0 @@ -# Copy to secrets.env on the host (chmod 600). Never committed: what the -# stack runs as is in .env, which is. bootstrap.sh and kanidm-setup.sh -# read both, .env first. - -# Generated once by bootstrap.sh if left empty. bootstrap.sh also writes -# it into nats/nats.conf and nats.env (NATS_URL for gdo and the chat). -NATS_PASSWORD= - -# Filled in by kanidm-setup.sh after it creates the Kanidm client. -OAUTH2_CLIENT_SECRET= - -# The first person in: portal invites this address into the site's most -# privileged group (the board) and mails them the one-time link, once. -# Everyone else they invite themselves, from their desk. An address, so -# not in the public repo. -SEED_ADMIN_EMAIL= -SEED_ADMIN_NAME= - -# Only for the optional runner profile: a registration token from -# prosjekt.klingenbergbygg.no -> tomtervel org -> Settings -> Actions -> Runners. -RUNNER_REGISTRATION_TOKEN=