Files
homelab-infrastructure-as-code/docs/runbooks/openbao.md
T
kasun e416e6a555
Secret Scan / Betterleaks (pull_request) Successful in 7s
docs: add RustFS backup cleanup and policy checks to runbooks
Object lock only blocks deletion; it never removes old backups, and the
RustFS console has no lifecycle screen, so the rule is set with the AWS
CLI. Also note that a new policy must be attached to the user, which
caused the first backup job's AccessDenied.
2026-12-03 03:34:53 +01:00

20 KiB
Raw Blame History

OpenBao

Use this to set up OpenBao after its first deploy, unseal it after restarts, and back it up or recover it. 06-openbao runs three pods that copy their data to each other using Raft. Unsealing is manual, with one unseal key.

The main commands were tested on local OpenBao 2.7.3 servers without Kubernetes or TLS, not on the cluster. If a checkpoint doesn't match, stop.

Rules

  • Only run init once, during first setup or on a new, empty OpenBao before a restore. After a restart, you only need to unseal.
  • Keep the unseal key in Bitwarden, plus a copy outside Bitwarden (on paper or an offline USB stick). If all pods restart and the key is lost, the data can't be recovered, even from snapshots.
  • Never put keys, tokens or passwords in Git, chats, tickets or commands. Enter them only when prompted, in a terminal that isn't being recorded.
  • Don't delete the data-openbao-* PVCs, except when replacing a lost worker. Deleting one deletes that pod's data.

Run the commands in Bash on your workstation. Replace <domain> with your domain. In steps that use $POD, set it to the active pod first, and again whenever you open a new terminal or a pod restarts:

POD=$(kubectl -n openbao get pod -l openbao-active=true -o name); echo "$POD"

Checkpoint: it prints exactly one pod.

First setup

1. Check the deployment

  1. In pfSense's DNS Resolver, point bao.<domain> to Traefik's IP (kubeVipAddress), not the Kubernetes API address.

  2. Check the pods, volumes and certificates:

    kubectl -n openbao get pods,pvc,certificates
    

    Checkpoint: all three pods are Running but show 0/1, the PVCs are Bound and the certificates are Ready. The web UI doesn't work until OpenBao is unsealed.

2. Initialise

Run this once, only on openbao-0:

kubectl -n openbao exec -it openbao-0 -- bao operator init -key-shares=1 -key-threshold=1

It shows the unseal key and initial root token only once. Save both in Bitwarden and make the second copy of the unseal key. Then clear the terminal and its scrollback. You'll delete the root token in step 7.

3. Unseal

  1. Unseal openbao-0, and paste the key when prompted:

    kubectl -n openbao exec -it openbao-0 -- bao operator unseal
    kubectl -n openbao exec openbao-0 -- bao status
    

    Checkpoint: after a few seconds, bao status shows Sealed false and HA Mode active.

  2. Check that openbao-1 has found openbao-0, then unseal it with the same key:

    kubectl -n openbao exec openbao-1 -- bao status
    kubectl -n openbao exec -it openbao-1 -- bao operator unseal
    

    Checkpoint: bao status shows Initialized true before you unseal. Exit code 2 is normal because the pod is still sealed. If it shows Initialized false, don't run init. Check kubectl -n openbao logs openbao-1 instead.

  3. Do the same for openbao-2, then check all three:

    kubectl -n openbao get pods -L openbao-active,openbao-sealed
    

    Checkpoint: all pods show 1/1 and openbao-sealed=false, and only one has openbao-active=true.

4. Create the admin login

  1. Set $POD, then log in with the root token when prompted:

    kubectl -n openbao exec -it "$POD" -- bao login -no-print
    kubectl -n openbao exec "$POD" -- bao operator raft list-peers
    

    Checkpoint: there's one leader and two followers. All three show Voter true, which can take a moment.

  2. Create a policy with full access. Only use it for your admin login, never for apps:

    kubectl -n openbao exec -i "$POD" -- bao policy write admin - <<'EOF'
    path "*" {
      capabilities = ["create", "read", "update", "patch", "delete", "list", "scan", "sudo"]
    }
    EOF
    
  3. Turn on username and password logins:

    kubectl -n openbao exec "$POD" -- bao auth enable userpass
    
  4. Generate a strong password in Bitwarden and create the admin user. Run these lines one at a time, and paste the password after the first one:

    read -rsp 'Admin password: ' PW; echo
    printf %s "$PW" | kubectl -n openbao exec -i "$POD" -- bao write auth/userpass/users/admin password=- token_policies=admin token_ttl=1h token_max_ttl=8h
    unset PW
    
  5. Turn on a KV v2 secrets engine, which stores passwords and API keys:

    kubectl -n openbao exec "$POD" -- bao secrets enable -path=secret -version=2 kv
    

5. Test the admin login

Open https://bao.<domain> in a private window. Choose Username as the login method and log in as admin. Then:

  1. Create a fake secret in secret/, read it, then delete it.
  2. Create a test policy, then delete it.

Checkpoint: everything works, and the browser shows no certificate warning.

6. Save the first snapshot

Follow Save a snapshot. This also checks that the admin login can make backups. Before storing real secrets, test the restore steps on a separate test cluster.

7. Revoke the root token

Only do this once steps 5 and 6 have worked. Check that Bitwarden has the admin password and that you have a copy outside it.

  1. Set $POD, then log in with the root token. Log in again even if you did so earlier, because the snapshot steps replaced the saved login:

    kubectl -n openbao exec -it "$POD" -- bao login -no-print
    kubectl -n openbao exec "$POD" -- bao read -field=policies auth/token/lookup-self
    

    Checkpoint: it prints [root]. If it prints anything else, don't continue, because the next command revokes whichever token is logged in.

  2. Revoke it and remove the saved login:

    kubectl -n openbao exec "$POD" -- bao token revoke -self
    kubectl -n openbao exec "$POD" -- rm -f /home/openbao/.vault-token
    
  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, and a lifecycle rule deletes it after about 32 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 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, 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:

    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 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:

    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:

    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:

    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:

    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:

    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_<date>-<time>.snapshot file. Delete the job afterwards with kubectl -n openbao delete job openbao-snapshot-manual.

Check backups

kubectl -n openbao get cronjob openbao-snapshot
kubectl -n openbao get jobs -l app.kubernetes.io/component=snapshot-agent

Checkpoint: LAST SCHEDULE is less than an hour ago, and recent jobs show Complete. For a failed job, check its logs with kubectl -n openbao logs job/<job-name>.

Download a backup

Set up the admin function from step 8 of the RustFS runbook, then:

admin s3api list-objects-v2 --bucket openbao-backups --query 'Contents[].[Key,LastModified,Size]' --output text | sort -k2 | tail -5
admin s3api get-object --bucket openbao-backups --key <snapshot-file> <snapshot-file>

The file is saved in the current folder. Use it with Restore into a new OpenBao. The job doesn't check its snapshots, so a backup is only proven once a restore from it has been tested.

Unseal after a restart

What restarted What to do
One OpenBao pod or its node Unseal that pod. The other two keep OpenBao running.
All three pods Unseal all three. OpenBao works again once two are unsealed.
A node without an OpenBao pod Nothing
  1. Find the sealed pods, which show 0/1:

    kubectl -n openbao get pods -L openbao-active,openbao-sealed
    
  2. Unseal each one, replacing <n> with its number:

    kubectl -n openbao exec -it openbao-<n> -- bao operator unseal
    
  3. Run the first command again.

    Checkpoint: all pods show 1/1, and only one is active.

Restart pods after a change

Changes to the OpenBao version or its config only take effect after you restart the pods. Before an upgrade, read the release notes and save a snapshot.

  1. Check that all three pods show 1/1, and note which one is active:

    kubectl -n openbao get pods -L openbao-active
    
  2. Restart one non-active pod, then unseal it:

    kubectl -n openbao delete pod openbao-<n>
    

    Checkpoint: all three pods show 1/1 before you restart the next one.

  3. Do the same for the other non-active pod, then restart the active pod last. Another pod takes over, which can cause a short interruption.

Save a snapshot

Do this before upgrades and after important changes, even with automatic backups.

  1. Set $POD, then log in as admin:

    kubectl -n openbao exec -it "$POD" -- bao login -no-print -method=userpass username=admin
    
  2. Save a snapshot and copy it to your workstation:

    SNAP=~/openbao-$(date +%F-%H%M).snap
    kubectl -n openbao exec "$POD" -- bao operator raft snapshot save /home/openbao/openbao.snap
    (umask 077; kubectl -n openbao exec "$POD" -- cat /home/openbao/openbao.snap > "$SNAP")
    kubectl -n openbao exec "$POD" -- sha256sum /home/openbao/openbao.snap
    sha256sum "$SNAP"
    

    Checkpoint: both checksums match.

  3. Remove the copy and saved login from the pod:

    kubectl -n openbao exec "$POD" -- bao token revoke -self
    kubectl -n openbao exec "$POD" -- rm -f /home/openbao/openbao.snap /home/openbao/.vault-token
    
  4. Move the snapshot to storage outside the cluster and restrict access to it. Don't store it with the unseal key, because someone with both can read your secrets. Note which unseal key it needs.

Restore into a new OpenBao

Use this on a separate test cluster, or if all three pods have lost their data. It replaces everything in the new OpenBao. Don't use it for a single lost pod or node. If the cluster being restored to has any working OpenBao data, stop and plan the recovery first.

You need the snapshot, the original unseal key and an admin password that was valid when the snapshot was taken. Use the same OpenBao version as the snapshot.

  1. Deploy a new, empty OpenBao. Follow First setup steps 1–3, but save the new unseal key and root token as temporary. Don't replace the original key in Bitwarden.

  2. Set $POD, then log in with the temporary root token:

    kubectl -n openbao exec -it "$POD" -- bao login -no-print
    
  3. Check your kubectl context, because -force overwrites the data and skips the unseal key check:

    kubectl config current-context
    

    Checkpoint: it's the cluster you want to restore to. Also check that its API address is correct, because different clusters can use the same context name:

    kubectl config view --minify -o jsonpath='{.clusters[0].cluster.server}'; echo
    
  4. Copy the snapshot to the pod and restore it:

    kubectl -n openbao exec -i "$POD" -- sh -c 'cat > /home/openbao/restore.snap' < <snapshot-file>
    sha256sum <snapshot-file>
    kubectl -n openbao exec "$POD" -- sha256sum /home/openbao/restore.snap
    

    Checkpoint: both checksums match. Then run:

    kubectl -n openbao exec "$POD" -- bao operator raft snapshot restore -force /home/openbao/restore.snap
    
  5. Wait 10 seconds, then restart all three pods. Otherwise, the non-active pods keep using the temporary key and get stuck:

    kubectl -n openbao delete pod openbao-0 openbao-1 openbao-2
    
  6. Once all three are running, unseal them with the original key. The temporary key and root token no longer work, so delete them.

  7. Set $POD again, log in as admin and check the cluster:

    kubectl -n openbao exec -it "$POD" -- bao login -no-print -method=userpass username=admin
    kubectl -n openbao exec "$POD" -- bao operator raft list-peers
    

    Checkpoint: there's one leader and two followers, and your secrets are there. All three should show Voter true, but this can take a moment.

  8. Remove the saved login:

    kubectl -n openbao exec "$POD" -- bao token revoke -self
    kubectl -n openbao exec "$POD" -- rm -f /home/openbao/.vault-token
    

Replace a lost worker

Only use this if the worker can't be recovered and the other two OpenBao pods are working. If it's only offline, bring it back and unseal its pod. local-path data can't move to another node, and each OpenBao pod needs its own node.

  1. Save a snapshot. Make sure the old worker can't come back online, then add its replacement to the cluster and remove the old node from Kubernetes. Wait until the replacement is Ready.

  2. Set $POD, log in as admin, then remove the lost pod from the Raft cluster. Replace <n> with its number:

    kubectl -n openbao exec -it "$POD" -- bao login -no-print -method=userpass username=admin
    kubectl -n openbao exec "$POD" -- bao operator raft remove-peer openbao-<n>
    kubectl -n openbao exec "$POD" -- bao operator raft list-peers
    

    Checkpoint: only the two working pods are listed.

  3. Delete only the lost pod and its PVC. Check the number first:

    kubectl -n openbao delete pvc data-openbao-<n> --wait=false
    kubectl -n openbao delete pod openbao-<n>
    

    If the pod gets stuck in Terminating, stop and check that the old node was removed. Don't force-delete the pod while its node could still be running.

  4. Wait for the new pod to show Running and Initialized true. Don't run init. Unseal it and run list-peers again.

    Checkpoint: all three pods are listed with Voter true, which can take a moment.

  5. Remove the saved login:

    kubectl -n openbao exec "$POD" -- bao token revoke -self
    kubectl -n openbao exec "$POD" -- rm -f /home/openbao/.vault-token
    

If you lose the admin login

By default, the unseal key alone can't create a new root token. You need to temporarily turn on an older endpoint that doesn't need a login. While it's on, anyone who has the unseal key and can reach OpenBao can get full access.

  1. In 06-openbao/index.ts, add this to the listener "tcp" block. Preview the change, then deploy it:

    disable_unauthed_generate_root_endpoints = false
    
  2. Restart all three pods, one at a time.

  3. Run this on your workstation. The bao command doesn't work for this without a login. Enter https://bao.<domain> and the unseal key when prompted. It cancels any unfinished attempt first:

    cat > /tmp/openbao-root.py <<'EOF'
    import base64, getpass, json, urllib.request
    
    url = input("OpenBao URL: ").rstrip("/")
    
    def call(method, path, data=None):
        body = None if data is None else json.dumps(data).encode()
        req = urllib.request.Request(f"{url}/v1/sys/generate-root/{path}", body, method=method)
        with urllib.request.urlopen(req) as res:
            return json.loads(res.read() or b"{}")
    
    call("DELETE", "attempt")
    start = call("PUT", "attempt", {})
    key = getpass.getpass("Unseal key: ")
    done = call("PUT", "update", {"key": key, "nonce": start["nonce"]})
    encoded = done["encoded_root_token"]
    raw = base64.b64decode(encoded + "=" * (-len(encoded) % 4))
    print("Root token:", bytes(a ^ b for a, b in zip(raw, start["otp"].encode())).decode())
    EOF
    python3 /tmp/openbao-root.py
    rm /tmp/openbao-root.py
    

    Save the token in Bitwarden for now, then clear the terminal and its scrollback. If the script shows 405 Method Not Allowed, the setting isn't active on the pod that answered.

  4. Set $POD and log in with the new root token:

    kubectl -n openbao exec -it "$POD" -- bao login -no-print
    

    Set a new password with step 4.4 of First setup. Then test the login and revoke the root token.

  5. Remove the setting, deploy again and restart all three pods, one at a time. Run the script again and enter any text as the key.

    Checkpoint: it fails with 405 Method Not Allowed.

Certificates

cert-manager renews both the browser certificate (openbao-ingress-tls) and OpenBao's internal one (openbao-server-tls). OpenBao loads its new internal certificate without a restart, so you don't need to unseal it.

Don't delete the openbao-ca Secret. It holds the key for our internal CA. Renewing or replacing the CA doesn't immediately renew OpenBao's internal certificate. We haven't tested this, so plan and test it first. Check the certificates with:

kubectl -n openbao get certificates

References