kron in the stack: the money service, Vipps ePayment for Medlemskap

Under the pay profile: kron.env from the Medlemskap sales unit's keys,
PAY_URL for portal once the profile is on, betaling.tomtervel.no in DNS
and kasse's Caddy, and vipps-webhook.sh for the one-time webhook
registration whose secret must never reach a public log.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
blandClaude Fable 5.1 committed 2026-11-10 20:25:46 +01:00
1 parent 47342b0867
commit a0af480890
7 files changed
+171 -1

No files matched your search

+37 -1
View File
@@ -19,6 +19,7 @@ on:
- Caddyfile - Caddyfile
- portal/** - portal/**
- gdo/** - gdo/**
- rauthy-providers.sh
- nats/nats.conf - nats/nats.conf
- rauthy/config.toml - rauthy/config.toml
- deploy-tomtervel - deploy-tomtervel
@@ -34,6 +35,7 @@ jobs:
# What the stack is. # What the stack is.
PORTAL_HOST: ${{ vars.PORTAL_HOST }} PORTAL_HOST: ${{ vars.PORTAL_HOST }}
CHAT_HOST: ${{ vars.CHAT_HOST }} CHAT_HOST: ${{ vars.CHAT_HOST }}
PAY_HOST: ${{ vars.PAY_HOST }}
PORTAL_RELEASE: ${{ vars.PORTAL_RELEASE }} PORTAL_RELEASE: ${{ vars.PORTAL_RELEASE }}
GDO_RELEASE: ${{ vars.GDO_RELEASE }} GDO_RELEASE: ${{ vars.GDO_RELEASE }}
CNATS_TAG: ${{ vars.CNATS_TAG }} CNATS_TAG: ${{ vars.CNATS_TAG }}
@@ -76,6 +78,15 @@ jobs:
OIDC_ISSUER: ${{ vars.OIDC_ISSUER }} OIDC_ISSUER: ${{ vars.OIDC_ISSUER }}
RAUTHY_API_KEY: ${{ secrets.RAUTHY_API_KEY }} RAUTHY_API_KEY: ${{ secrets.RAUTHY_API_KEY }}
RAUTHY_LANGUAGE: ${{ vars.RAUTHY_LANGUAGE }} RAUTHY_LANGUAGE: ${{ vars.RAUTHY_LANGUAGE }}
# The money service (README.md, "Payments"): Vipps's keys for the
# Medlemskap sales unit, read by kron alone. All five are needed
# once COMPOSE_PROFILES has `pay`; VIPPS_WEBHOOK_SECRET may wait,
# kron then settles by asking Vipps every minute instead.
VIPPS_MSN: ${{ vars.VIPPS_MSN }}
VIPPS_CLIENT_ID: ${{ secrets.VIPPS_CLIENT_ID }}
VIPPS_CLIENT_SECRET: ${{ secrets.VIPPS_CLIENT_SECRET }}
VIPPS_SUBSCRIPTION_KEY: ${{ secrets.VIPPS_SUBSCRIPTION_KEY }}
VIPPS_WEBHOOK_SECRET: ${{ secrets.VIPPS_WEBHOOK_SECRET }}
steps: steps:
# A missing value stops here, by name, before anything on the host # A missing value stops here, by name, before anything on the host
# is touched. Only names are printed: the log is public. # is touched. Only names are printed: the log is public.
@@ -92,6 +103,14 @@ jobs:
[ -n "${!v}" ] || missing="$missing $v" [ -n "${!v}" ] || missing="$missing $v"
done done
[ -z "$missing" ] || { echo "not set as an Actions variable or secret:$missing"; exit 1; } [ -z "$missing" ] || { echo "not set as an Actions variable or secret:$missing"; exit 1; }
pay=
case ",$COMPOSE_PROFILES," in *,pay,*) pay=1;; esac
if [ -n "$pay" ]; then
for v in PAY_HOST VIPPS_MSN VIPPS_CLIENT_ID VIPPS_CLIENT_SECRET VIPPS_SUBSCRIPTION_KEY; do
[ -n "${!v}" ] || missing="$missing $v"
done
[ -z "$missing" ] || { echo "COMPOSE_PROFILES has pay, but not set:$missing"; exit 1; }
fi
umask 077 umask 077
mkdir -p "$STAGE" mkdir -p "$STAGE"
@@ -104,6 +123,7 @@ jobs:
CNATS_TAG=$CNATS_TAG CNATS_TAG=$CNATS_TAG
KRON_RELEASE=$KRON_RELEASE KRON_RELEASE=$KRON_RELEASE
COMPOSE_PROFILES=$COMPOSE_PROFILES COMPOSE_PROFILES=$COMPOSE_PROFILES
PAY_HOST=$PAY_HOST
RAUTHY_HOST=$RAUTHY_HOST RAUTHY_HOST=$RAUTHY_HOST
OIDC_ISSUER=$OIDC_ISSUER OIDC_ISSUER=$OIDC_ISSUER
EOF EOF
@@ -133,6 +153,13 @@ jobs:
RAUTHY_API_KEY=$RAUTHY_API_KEY RAUTHY_API_KEY=$RAUTHY_API_KEY
RAUTHY_LANGUAGE=$RAUTHY_LANGUAGE RAUTHY_LANGUAGE=$RAUTHY_LANGUAGE
EOF EOF
# With the money service in the stack, portal may ask for
# payments without `by_hand`; the address itself is never
# called (the two talk over NATS), the lint only reads that it
# is set. The content repo's lint sets it the same way.
if [ -n "$pay" ]; then
echo "PAY_URL=http://kron:3000" >> "$STAGE/portal.env"
fi
# NATS itself (nats.conf reads NATS_PASSWORD), gdo and the chat. # NATS itself (nats.conf reads NATS_PASSWORD), gdo and the chat.
cat > "$STAGE/nats.env" <<EOF cat > "$STAGE/nats.env" <<EOF
NATS_PASSWORD=$NATS_PASSWORD NATS_PASSWORD=$NATS_PASSWORD
@@ -142,6 +169,15 @@ jobs:
OAUTH2_CLIENT_ID=$CHAT_OAUTH2_CLIENT_ID OAUTH2_CLIENT_ID=$CHAT_OAUTH2_CLIENT_ID
OAUTH2_CLIENT_SECRET=$CHAT_OAUTH2_CLIENT_SECRET OAUTH2_CLIENT_SECRET=$CHAT_OAUTH2_CLIENT_SECRET
EOF EOF
# kron: Vipps's keys for the Medlemskap sales unit. Written even
# without the profile, so the file compose names is there.
cat > "$STAGE/kron.env" <<EOF
VIPPS_MSN=$VIPPS_MSN
VIPPS_CLIENT_ID=$VIPPS_CLIENT_ID
VIPPS_CLIENT_SECRET=$VIPPS_CLIENT_SECRET
VIPPS_SUBSCRIPTION_KEY=$VIPPS_SUBSCRIPTION_KEY
WEBHOOK_SECRET=$VIPPS_WEBHOOK_SECRET
EOF
# Rauthy's secrets. The first admin is the vel's first person. # Rauthy's secrets. The first admin is the vel's first person.
cat > "$STAGE/rauthy.env" <<EOF cat > "$STAGE/rauthy.env" <<EOF
ENC_KEYS=$RAUTHY_ENC_KEY ENC_KEYS=$RAUTHY_ENC_KEY
@@ -163,7 +199,7 @@ jobs:
# "$$" is compose's literal "$". Without this, RAUTHY_API_KEY reached # "$$" is compose's literal "$". Without this, RAUTHY_API_KEY reached
# portal as "portal" alone, Rauthy answered 401, and every invite # portal as "portal" alone, Rauthy answered 401, and every invite
# failed. .env is left alone: it is what compose.yml interpolates. # failed. .env is left alone: it is what compose.yml interpolates.
sed -i 's/\$/$$/g' "$STAGE/portal.env" "$STAGE/nats.env" "$STAGE/chat.env" "$STAGE/rauthy.env" sed -i 's/\$/$$/g' "$STAGE/portal.env" "$STAGE/nats.env" "$STAGE/chat.env" "$STAGE/rauthy.env" "$STAGE/kron.env"
- name: Pull and bring the stack up - name: Pull and bring the stack up
run: sudo /usr/local/bin/deploy-tomtervel run: sudo /usr/local/bin/deploy-tomtervel
+1
View File
@@ -5,3 +5,4 @@ gitea-oauth.secret
dns/cloudflare.env dns/cloudflare.env
nats.env nats.env
rauthy.env rauthy.env
kron.env
+42
View File
@@ -124,6 +124,10 @@ Run workflow**. Set them with `gt` (`gt variable set NAME --body ...`,
| `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_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` | | `RAUTHY_API_KEY`, `RAUTHY_LANGUAGE` | secret, variable | repo | portal makes accounts in Rauthy (`<name>$<secret>`, made in Rauthy's admin), in `nb` |
| `VIPPS_LOGIN_CLIENT_ID`, `VIPPS_LOGIN_CLIENT_SECRET` | variable, secret | repo | Vipps Login (the login sales unit's keys from portal.vippsmobilepay.no): the deploy keeps Vipps as a sign-in provider in Rauthy with them (`rauthy-providers.sh`) | | `VIPPS_LOGIN_CLIENT_ID`, `VIPPS_LOGIN_CLIENT_SECRET` | variable, secret | repo | Vipps Login (the login sales unit's keys from portal.vippsmobilepay.no): the deploy keeps Vipps as a sign-in provider in Rauthy with them (`rauthy-providers.sh`) |
| `PAY_HOST` | variable | repo | where the money service answers: `betaling.tomtervel.no` (DNS in `dns/`, a kasse Caddy block) |
| `VIPPS_MSN` | variable | repo | the Medlemskap sales unit's merchant serial number, `1192072` |
| `VIPPS_CLIENT_ID`, `VIPPS_CLIENT_SECRET`, `VIPPS_SUBSCRIPTION_KEY` | secret | repo | the Medlemskap sales unit's ePayment keys (For developers, API keys, production), read by kron alone |
| `VIPPS_WEBHOOK_SECRET` | secret | repo | what Vipps signs its webhooks with, from `vipps-webhook.sh register`; unset, kron refuses webhooks and asks Vipps about each open payment every minute instead |
A variable is printed wherever a workflow echoes it, and this repo's run 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. logs are public: a value that must not be read is a secret.
@@ -131,6 +135,44 @@ 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 What is not a value stays on the host, made there once: Garage's node
secret. secret.
## Payments
The money service is [kron](https://project.uhhm.no/uhhm/kron), in the
stack under the `pay` profile (`COMPOSE_PROFILES=chat,pay`). Portal
decides when money is owed: a household entering `open` or `fornyes` in
the content's aggregates owes the fee, and portal publishes one request
on this stack's NATS. kron answers at once with a link of its own,
`https://betaling.tomtervel.no/pay/<reference>`, which portal shows on
the person's own page and mails; opening it makes a Vipps payment for
the Medlemskap sales unit and sends the person into Vipps, and a fresh
one each time the last ran out (Vipps's own expire after ten minutes).
Once they have paid, kron captures the reserved amount and says `paid`
on the bus; portal moves the household to `betalt`, grants `medlemmer`,
and mails them. Vipps's keys live in `kron.env` and nowhere else; portal
never calls Vipps. `PAY_URL` in `portal.env` is what lets the content
leave `by_hand: true` out of a `payment:`; portal never calls that
address either.
Going live, in order:
1. Set `PAY_HOST`, `VIPPS_MSN` and the three key secrets (table above).
The keys are in `pass` as `tomtervel/vipps/medlemskap`.
2. Merge the DNS record (`dns/dnsconfig.js`, `betaling`) and run the DNS
workflow; add the `betaling.tomtervel.no` block from
`host-caddy/tomtervel.no` to kasse's Caddy.
3. Set `COMPOSE_PROFILES=chat,pay` and `KRON_RELEASE` (a bendik/kron
tag), then Run workflow. `https://betaling.tomtervel.no/healthz`
answers `ok`.
4. Register the webhook: `./vipps-webhook.sh register` on a machine with
`pass` (it puts the secret in pass), then `gt secret set
VIPPS_WEBHOOK_SECRET` as it says, and Run workflow once more. Until
then kron settles by polling, a minute behind.
5. Drop `by_hand: true` from the content's `payment:` states (the
content repo's lint reads `PAY_URL` the same way).
Nothing here touches the Vippsnummer (500396): people may keep paying
there, and the treasurer marks those by hand as before.
## Rauthy ## Rauthy
[Rauthy](https://github.com/sebadob/rauthy) at `RAUTHY_HOST` [Rauthy](https://github.com/sebadob/rauthy) at `RAUTHY_HOST`
+28
View File
@@ -144,6 +144,34 @@ services:
nats: nats:
condition: service_healthy condition: service_healthy
# The money service (uhhm/kron): portal asks for a payment on this
# NATS as a household owes its fee, kron gives it a link to pay by,
# talks to Vipps's ePayment API for the Medlemskap sales unit, and
# says `paid` back on the same bus. Vipps's keys are in kron.env and
# nowhere else; portal never calls Vipps. Host-published for kasse's
# host Caddy (betaling.tomtervel.no -> here): the pay links, the page
# people come back to from Vipps, and Vipps's webhooks. A profile, since
# it needs kron.env: COMPOSE_PROFILES gains `pay` once the keys are set.
kron:
profiles: ["pay"]
image: docker.io/bendik/kron:${KRON_RELEASE:-no-tag-set}
restart: unless-stopped
env_file:
- nats.env
- kron.env
environment:
# Must be the portal's own: a request names its site.
SITE_NAME: tomtervel
PROVIDER: vipps
VIPPS_ENV: prod
PUBLIC_URL: https://${PAY_HOST:-no-host-set}
LISTEN_ADDR: 0.0.0.0:3000
ports:
- "127.0.0.1:3080:3000"
depends_on:
nats:
condition: service_healthy
# The Gitea Actions runner is a host service on kasse (pacman gitea-runner), # The Gitea Actions runner is a host service on kasse (pacman gitea-runner),
# registered against prosjekt.klingenbergbygg.no. It reaches this NATS via the # registered against prosjekt.klingenbergbygg.no. It reaches this NATS via the
# published 127.0.0.1:4222 port above, so no in-compose runner — and no # published 127.0.0.1:4222 port above, so no in-compose runner — and no
+2
View File
@@ -31,6 +31,8 @@ D("tomtervel.no", REG_NONE, DnsProvider(DSP_CLOUDFLARE),
A("@", KASSE, CF_PROXY_OFF), A("@", KASSE, CF_PROXY_OFF),
CNAME("www", "tomtervel.no.", CF_PROXY_OFF), CNAME("www", "tomtervel.no.", CF_PROXY_OFF),
A("konto", KASSE, CF_PROXY_OFF), A("konto", KASSE, CF_PROXY_OFF),
// The money service (kron): pay links and Vipps's webhooks.
A("betaling", KASSE, CF_PROXY_OFF),
A("samtaler", KASSE, CF_PROXY_OFF), A("samtaler", KASSE, CF_PROXY_OFF),
// The old names, kept so links from before still land: Caddy sends // The old names, kept so links from before still land: Caddy sends
// id on to konto (the login) and chat on to samtaler. // id on to konto (the login) and chat on to samtaler.
+8
View File
@@ -33,6 +33,14 @@ konto.tomtervel.no {
reverse_proxy 127.0.0.1:8090 reverse_proxy 127.0.0.1:8090
} }
# The money service (kron, README.md "Payments"): the pay links portal
# gives out, the page people come back to from Vipps, and Vipps's
# webhooks. Kept out of search engines; nothing here is a page to find.
betaling.tomtervel.no {
header X-Robots-Tag "noindex, nofollow"
reverse_proxy 127.0.0.1:3080
}
# The content repo's previews (tomtervel/questions, .gitea/preview/): one # The content repo's previews (tomtervel/questions, .gitea/preview/): one
# portal per branch on <branch>.forhandsvisning.tomtervel.no, for five days # portal per branch on <branch>.forhandsvisning.tomtervel.no, for five days
# after its last push, and the list of them on the bare name. Every name # after its last push, and the list of them on the bare name. Every name
+53
View File
@@ -0,0 +1,53 @@
#!/usr/bin/env bash
# Vipps's webhooks for the money service (kron): registers kron's
# endpoint with the Webhooks API for the Medlemskap sales unit, or lists
# what is registered. Run by a person with the keys in `pass`, not by the
# deploy: the registration answers with the webhook's secret exactly once,
# and that secret has to become the repo secret VIPPS_WEBHOOK_SECRET (and
# kron's WEBHOOK_SECRET) without ever being printed in a public log.
#
# ./vipps-webhook.sh list # what Vipps has for the unit
# ./vipps-webhook.sh register # kron's /webhook, secret into pass
# ./vipps-webhook.sh delete <webhook id>
#
# VIPPS_ENV=test uses apitest.vipps.no and the test unit's keys
# (tomtervel/vipps/medlemskap-test in pass); the default is production
# (tomtervel/vipps/medlemskap). PAY_HOST is the host kron answers on
# (betaling.tomtervel.no).
set -euo pipefail
env=${VIPPS_ENV:-prod}
case "$env" in
test) api=https://apitest.vipps.no; entry=tomtervel/vipps/medlemskap-test ;;
prod) api=https://api.vipps.no; entry=tomtervel/vipps/medlemskap ;;
*) echo "VIPPS_ENV is $env; it is test or prod" >&2; exit 2 ;;
esac
host=${PAY_HOST:-betaling.tomtervel.no}
field() { pass show "$entry" | sed -n "s/^$1: //p" | head -n1; }
msn=$(field msn); cid=$(field client_id); csec=$(field client_secret); sub=$(field subscription_key_primary)
[ -n "$msn" ] && [ -n "$cid" ] && [ -n "$csec" ] && [ -n "$sub" ] || { echo "pass entry $entry lacks msn, client_id, client_secret or subscription_key_primary" >&2; exit 1; }
token=$(curl -sf -X POST "$api/accesstoken/get" -H "client_id: $cid" -H "client_secret: $csec" \
-H "Ocp-Apim-Subscription-Key: $sub" -H "Merchant-Serial-Number: $msn" -H 'Content-Length: 0' | jq -r .access_token)
[ -n "$token" ] && [ "$token" != null ] || { echo "no access token from $api" >&2; exit 1; }
auth=(-H "Authorization: Bearer $token" -H "Ocp-Apim-Subscription-Key: $sub" -H "Merchant-Serial-Number: $msn" -H 'Content-Type: application/json')
case "${1:-list}" in
list)
curl -sf "$api/webhooks/v1/webhooks" "${auth[@]}" | jq .
;;
register)
url="https://$host/webhook"
events='["epayments.payment.created.v1","epayments.payment.authorized.v1","epayments.payment.captured.v1","epayments.payment.aborted.v1","epayments.payment.expired.v1","epayments.payment.cancelled.v1","epayments.payment.terminated.v1","epayments.payment.refunded.v1"]'
answer=$(curl -sf -X POST "$api/webhooks/v1/webhooks" "${auth[@]}" -d "$(jq -n --arg url "$url" --argjson events "$events" '{url: $url, events: $events}')")
id=$(jq -r .id <<<"$answer"); secret=$(jq -r .secret <<<"$answer")
[ -n "$secret" ] && [ "$secret" != null ] || { echo "Vipps answered without a secret: $(jq -c 'del(.secret)' <<<"$answer")" >&2; exit 1; }
# The secret goes into pass and nowhere on screen.
printf 'id: %s\nurl: %s\nsecret: %s\n' "$id" "$url" "$secret" | pass insert -m -f "$entry-webhook" >/dev/null
echo "registered webhook $id for $url; its secret is in pass as $entry-webhook"
echo "now: pass show $entry-webhook | sed -n 's/^secret: //p' | gt secret set VIPPS_WEBHOOK_SECRET"
;;
delete)
[ -n "${2:-}" ] || { echo "which webhook? ./vipps-webhook.sh delete <id>" >&2; exit 2; }
curl -sf -X DELETE "$api/webhooks/v1/webhooks/$2" "${auth[@]}" && echo "deleted $2"
;;
*) echo "usage: $0 list | register | delete <id>" >&2; exit 2 ;;
esac