diff --git a/06-openbao/index.ts b/06-openbao/index.ts index d479edb..171ca9f 100644 --- a/06-openbao/index.ts +++ b/06-openbao/index.ts @@ -8,9 +8,18 @@ const appsRef = new pulumi.StackReference( `${pulumi.getOrganization()}/k8s-apps/dev`, ); +const config = new pulumi.Config(); +const snapshotS3AccessKeyId = config.requireSecret("snapshotS3AccessKeyId"); +const snapshotS3SecretAccessKey = config.requireSecret( + "snapshotS3SecretAccessKey", +); + const kubeconfig = bootstrapRef.requireOutput("kubeconfig"); const domain = appsRef.requireOutput("domain"); const openbaoHost = pulumi.interpolate`bao.${domain}`; +// RustFS on TrueNAS, see docs/runbooks/truenas-rustfs.md. +const snapshotS3Host = pulumi.interpolate`rustfs.${domain}:30292`; +const snapshotBucket = "openbao-backups"; const k8sProvider = new k8s.Provider("k3s", { kubeconfig, @@ -116,6 +125,23 @@ const serversTransport = new k8s.apiextensions.CustomResource( opts({ dependsOn: [serverCert] }), ); +// Upload-only RustFS key for the snapshot CronJob. Object lock on the bucket +// keeps uploaded snapshots from being deleted or overwritten. +const snapshotS3Credentials = new k8s.core.v1.Secret( + "openbao-snapshot-s3", + { + metadata: { + name: "openbao-snapshot-s3", + namespace: openbaoNs.metadata.name, + }, + stringData: { + AWS_ACCESS_KEY_ID: snapshotS3AccessKeyId, + AWS_SECRET_ACCESS_KEY: snapshotS3SecretAccessKey, + }, + }, + opts(), +); + new k8s.helm.v3.Release( "openbao", { @@ -202,6 +228,20 @@ new k8s.helm.v3.Release( { port: 8201, protocol: "TCP" }, ], }, + { + from: [ + { + podSelector: { + matchLabels: { + "app.kubernetes.io/name": "openbao", + "app.kubernetes.io/instance": "openbao", + "app.kubernetes.io/component": "snapshot-agent", + }, + }, + }, + ], + ports: [{ port: 8200, protocol: "TCP" }], + }, ], }, ha: { @@ -254,7 +294,50 @@ audit "file" "stdout" { size: "2Gi", }, }, + // Hourly Raft snapshots to RustFS. The job logs in to OpenBao with + // Kubernetes auth, set up by hand in docs/runbooks/openbao.md. + snapshotAgent: { + enabled: true, + schedule: "0 * * * *", + s3CredentialsSecret: "openbao-snapshot-s3", + config: { + s3Host: snapshotS3Host, + // s3cmd's --host-bucket. Using the host itself keeps requests + // path-style, so DNS doesn't need a record per bucket. + s3Bucket: snapshotS3Host, + s3Uri: `s3://${snapshotBucket}`, + // Empty turns off the agent's own pruning, which would need delete + // access. RustFS lifecycle rules handle retention instead. + s3ExpireDays: "", + // RustFS rejects s3cmd's default signing region, "US". + s3cmdExtraFlag: "-v --region=us-east-1", + baoAuthPath: "kubernetes", + baoRole: "openbao-snapshot", + }, + extraEnvironmentVars: { + BAO_CACERT: "/openbao/tls/ca.crt", + }, + extraVolumes: [ + { + name: "openbao-ca", + secret: { + secretName: "openbao-server-tls", + items: [{ key: "ca.crt", path: "ca.crt" }], + }, + }, + ], + extraVolumeMounts: [ + { name: "openbao-ca", mountPath: "/openbao/tls", readOnly: true }, + ], + resources: { + requests: { cpu: "50m", memory: "64Mi" }, + limits: { memory: "256Mi" }, + }, + }, }, }, - opts({ dependsOn: [serversTransport], protect: true }), + opts({ + dependsOn: [serversTransport, snapshotS3Credentials], + protect: true, + }), ); diff --git a/CLAUDE.md b/CLAUDE.md index 2866535..fa9d216 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -36,7 +36,7 @@ Branch names, commit messages and PR titles follow @CONVENTIONS.md. The commit-m Only commit when I explicitly say "commit" for that change. A yes to a question that also mentioned committing is not enough; when unsure, ask. -The remote is a self-hosted Gitea, so `gh` doesn't work. For PRs and their CI status use `.claude/scripts/gitea.sh` (acts as `claude-bot`), e.g. `.claude/scripts/gitea.sh GET '/repos/kasun/homelab-infrastructure-as-code/pulls?state=open'`. Use it for PRs only, and ask before opening or merging one. +The remote is a self-hosted Gitea, so `gh` doesn't work. For PRs and their CI status use `.claude/scripts/gitea.sh` (acts as `claude-bot`), e.g. `.claude/scripts/gitea.sh GET '/repos/kasun/homelab-infrastructure-as-code/pulls?state=open'`. ## Live infrastructure diff --git a/docs/runbooks/openbao.md b/docs/runbooks/openbao.md index 56bf9fb..776aae8 100644 --- a/docs/runbooks/openbao.md +++ b/docs/runbooks/openbao.md @@ -146,6 +146,90 @@ Only do this once steps 5 and 6 have worked. Check that Bitwarden has the admin 3. Check that you can still log in to the UI as `admin`. Then delete the root token from Bitwarden, but **keep the unseal key**. +## Automatic backups + +A CronJob from the OpenBao Helm chart saves a Raft snapshot every hour and uploads it to the `openbao-backups` bucket in RustFS on TrueNAS. Object lock keeps each backup for 30 days. The job logs in with Kubernetes auth and can only read snapshots. Its RustFS key can only upload. + +> Missed or failed backups don't trigger alerts yet. Check the job now and then, and still [save a snapshot](#save-a-snapshot) before upgrades. + +### Set up backups + +Do these in order. Otherwise, the hourly job fails until its login exists. + +1. Set up RustFS, then create the bucket and upload-only user. Follow [the RustFS runbook](truenas-rustfs.md), including step 8. +2. Add the backup user's key to the stack's config. Run these from the repo root, and paste each value when prompted: + + ```bash + pulumi config set --secret --cwd 06-openbao snapshotS3AccessKeyId + pulumi config set --secret --cwd 06-openbao snapshotS3SecretAccessKey + ``` + + Commit and push the change in the private config repo, because CI uses the pushed version. + +3. Set [`$POD`](#rules) and log in as `admin`. Run the login **by itself**, not pasted with the next steps, or the password prompt may not work. Then check it: + + ```bash + kubectl -n openbao exec -it "$POD" -- bao login -no-print -method=userpass username=admin + kubectl -n openbao exec "$POD" -- bao read -field=policies auth/token/lookup-self + ``` + + **Checkpoint:** it prints `[admin default]`. If it doesn't, stop. The next steps fail with `permission denied` without a valid login. + +4. Turn on Kubernetes logins. OpenBao uses its own service account and the cluster's CA to check logins: + + ```bash + kubectl -n openbao exec "$POD" -- bao auth enable kubernetes + kubectl -n openbao exec "$POD" -- bao write auth/kubernetes/config kubernetes_host=https://kubernetes.default.svc:443 + ``` + +5. Create the backup policy and role. The role only accepts the `openbao-snapshot` service account in the `openbao` namespace: + + ```bash + kubectl -n openbao exec -i "$POD" -- bao policy write openbao-snapshot - <<'EOF' + path "sys/storage/raft/snapshot" { + capabilities = ["read"] + } + EOF + kubectl -n openbao exec "$POD" -- bao write auth/kubernetes/role/openbao-snapshot bound_service_account_names=openbao-snapshot bound_service_account_namespaces=openbao token_policies=openbao-snapshot token_ttl=10m token_max_ttl=10m + ``` + +6. Remove the saved login: + + ```bash + kubectl -n openbao exec "$POD" -- bao token revoke -self + kubectl -n openbao exec "$POD" -- rm -f /home/openbao/.vault-token + ``` + +7. Deploy `06-openbao`. Then start a backup without waiting for the hourly run: + + ```bash + kubectl -n openbao create job --from=cronjob/openbao-snapshot openbao-snapshot-manual + kubectl -n openbao wait --for=condition=complete job/openbao-snapshot-manual --timeout=5m + kubectl -n openbao logs job/openbao-snapshot-manual + ``` + + **Checkpoint:** the job completes, and the log ends with an `upload:` line for a `bao_-