#!/usr/bin/env bash # Generates random passwords for every app and writes them as SealedSecrets # (apps//sealed-secret.yaml), which are safe to commit. # # Needs: kubectl + kubeseal, KUBECONFIG pointing at the cluster, and the # sealed-secrets controller running (cluster/sealed-secrets.yaml synced). # # Existing files are never overwritten: Postgres only reads its passwords on # the very first start, so regenerating them would lock the apps out. # To rotate one app's secret, delete its file, rerun, and change the password # in Postgres by hand. # # Optional, for the nightly Postgres backup (apps/postgres/backup-cronjob.yaml): # RESTIC_REPOSITORY=rest:http://k3s-postgres:@10.0.0.15:8000/k3s-postgres \ # RESTIC_PASSWORD=... KUMA_PUSH_URL=https://status.un-oeil.fr/api/push/... \ # scripts/seal-secrets.sh set -euo pipefail cd "$(dirname "$0")/.." rand() { openssl rand -hex "${1:-24}"; } # The default kubeconfig on the laptop points at ViaRézo's cluster. export KUBECONFIG=${KUBECONFIG:-$HOME/.kube/k3s-homelab.yaml} server=$(kubectl config view --minify -o jsonpath='{.clusters[0].cluster.server}') [ "$server" = "https://10.0.0.131:6443" ] || { echo "kubectl points at $server, not the homelab k3s. Set KUBECONFIG=~/.kube/k3s-homelab.yaml."; exit 1; } kubeseal --fetch-cert >/dev/null || { echo "Sealed Secrets controller not reachable yet (kubectl -n kube-system get deploy sealed-secrets-controller)."; exit 1; } # seal KEY=VALUE... seal() { local app=$1 name=$2 out=apps/$1/$3; shift 3 if [ -e "$out" ]; then echo "skip $out (exists)"; return; fi local args=() for kv in "$@"; do args+=(--from-literal="$kv"); done # Write to a temp file first, so a failed kubeseal leaves nothing behind. kubectl create secret generic "$name" -n "$app" "${args[@]}" --dry-run=client -o yaml \ | kubeseal --format yaml > "$out.tmp" mv "$out.tmp" "$out" echo "wrote $out" } # Passwords shared between Postgres and the apps. WIKIJS=$(rand); PAPERLESS=$(rand); LINKWARDEN=$(rand); FIREFLY=$(rand); HEDGEDOC=$(rand) # If Postgres already has its passwords, a DB app missing its file would get a # new password Postgres doesn't know. if [ -e apps/postgres/sealed-secret.yaml ]; then for a in wikijs paperless linkwarden firefly hedgedoc; do if [ ! -e apps/$a/sealed-secret.yaml ]; then echo "apps/$a/sealed-secret.yaml is missing but Postgres already has its password." echo "Set it by hand: ALTER ROLE $a PASSWORD '...'; then seal the same value. Aborting." exit 1 fi done fi seal postgres postgres-secrets sealed-secret.yaml \ POSTGRES_PASSWORD="$(rand)" \ WIKIJS_DB_PASSWORD="$WIKIJS" PAPERLESS_DB_PASSWORD="$PAPERLESS" \ LINKWARDEN_DB_PASSWORD="$LINKWARDEN" FIREFLY_DB_PASSWORD="$FIREFLY" \ HEDGEDOC_DB_PASSWORD="$HEDGEDOC" seal wikijs wikijs-secrets sealed-secret.yaml DB_PASS="$WIKIJS" seal paperless paperless-secrets sealed-secret.yaml \ PAPERLESS_DBPASS="$PAPERLESS" PAPERLESS_SECRET_KEY="$(rand 32)" \ PAPERLESS_ADMIN_PASSWORD="$(rand 12)" seal linkwarden linkwarden-secrets sealed-secret.yaml \ DATABASE_URL="postgresql://linkwarden:$LINKWARDEN@postgres.postgres.svc:5432/linkwarden" \ NEXTAUTH_SECRET="$(rand 32)" # Firefly wants exactly 32 characters for both. seal firefly firefly-secrets sealed-secret.yaml \ APP_KEY="$(rand 16)" DB_PASSWORD="$FIREFLY" STATIC_CRON_TOKEN="$(rand 16)" seal hedgedoc hedgedoc-secrets sealed-secret.yaml \ CMD_DB_URL="postgres://hedgedoc:$HEDGEDOC@postgres.postgres.svc:5432/hedgedoc" \ CMD_SESSION_SECRET="$(rand 32)" seal vikunja vikunja-secrets sealed-secret.yaml VIKUNJA_SERVICE_JWTSECRET="$(rand 32)" seal searxng searxng-secrets sealed-secret.yaml SEARXNG_SECRET="$(rand 32)" seal n8n n8n-secrets sealed-secret.yaml N8N_ENCRYPTION_KEY="$(rand 32)" if [ -n "${RESTIC_REPOSITORY:-}" ]; then seal postgres postgres-backup-secrets sealed-backup-secret.yaml \ RESTIC_REPOSITORY="$RESTIC_REPOSITORY" RESTIC_PASSWORD="${RESTIC_PASSWORD:?}" \ KUMA_PUSH_URL="${KUMA_PUSH_URL:?}" grep -q sealed-backup-secret.yaml apps/postgres/kustomization.yaml \ || sed -i 's/^ - sealed-secret.yaml$/&\n - sealed-backup-secret.yaml/' apps/postgres/kustomization.yaml echo "Now set 'suspend: false' in apps/postgres/backup-cronjob.yaml." fi echo echo "Done. Commit apps/*/sealed-secret.yaml and push." echo "Paperless admin password, once synced:" echo " kubectl -n paperless get secret paperless-secrets -o jsonpath='{.data.PAPERLESS_ADMIN_PASSWORD}' | base64 -d; echo"