# 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](#replace-a-lost-worker). Deleting one deletes that pod's data. Run the commands in Bash on your workstation. Replace `` 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: ```bash 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.` to Traefik's IP (`kubeVipAddress`), not the Kubernetes API address. 2. Check the pods, volumes and certificates: ```bash 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`**: ```bash 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: ```bash 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: ```bash 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: ```bash 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`](#rules), then log in with the root token when prompted: ```bash 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 `follower`s. 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: ```bash 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: ```bash 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: ```bash 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: ```bash kubectl -n openbao exec "$POD" -- bao secrets enable -path=secret -version=2 kv ``` ### 5. Test the admin login Open `https://bao.` 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](#save-a-snapshot). This also checks that the admin login can make backups. Before storing real secrets, test the [restore steps](#restore-into-a-new-openbao) 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`](#rules), then log in with the **root token**. Log in again even if you did so earlier, because the snapshot steps replaced the saved login: ```bash 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: ```bash 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**. ## 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`: ```bash kubectl -n openbao get pods -L openbao-active,openbao-sealed ``` 2. Unseal each one, replacing `` with its number: ```bash kubectl -n openbao exec -it openbao- -- 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](#save-a-snapshot). 1. Check that all three pods show `1/1`, and note which one is active: ```bash kubectl -n openbao get pods -L openbao-active ``` 2. Restart one **non-active** pod, then [unseal it](#unseal-after-a-restart): ```bash kubectl -n openbao delete pod openbao- ``` **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. Snapshots aren't automatic yet. 1. Set [`$POD`](#rules), then log in as `admin`: ```bash kubectl -n openbao exec -it "$POD" -- bao login -no-print -method=userpass username=admin ``` 2. Save a snapshot and copy it to your workstation: ```bash 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: ```bash 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](#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`](#rules), then log in with the **temporary root token**: ```bash 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: ```bash 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: ```bash kubectl config view --minify -o jsonpath='{.clusters[0].cluster.server}'; echo ``` 4. Copy the snapshot to the pod and restore it: ```bash kubectl -n openbao exec -i "$POD" -- sh -c 'cat > /home/openbao/restore.snap' < sha256sum kubectl -n openbao exec "$POD" -- sha256sum /home/openbao/restore.snap ``` **Checkpoint:** both checksums match. Then run: ```bash 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: ```bash kubectl -n openbao delete pod openbao-0 openbao-1 openbao-2 ``` 6. Once all three are running, [unseal them](#unseal-after-a-restart) with the **original key**. The temporary key and root token no longer work, so delete them. 7. Set [`$POD`](#rules) again, log in as `admin` and check the cluster: ```bash 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: ```bash 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](#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`](#rules), log in as `admin`, then remove the lost pod from the Raft cluster. Replace `` with its number: ```bash 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- 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: ```bash kubectl -n openbao delete pvc data-openbao- --wait=false kubectl -n openbao delete pod openbao- ``` 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](#unseal-after-a-restart) and run `list-peers` again. **Checkpoint:** all three pods are listed with `Voter true`, which can take a moment. 5. 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 ``` ## 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: ```hcl disable_unauthed_generate_root_endpoints = false ``` 2. [Restart all three pods](#restart-pods-after-a-change), one at a time. 3. Run this on your workstation. The `bao` command doesn't work for this without a login. Enter `https://bao.` and the unseal key when prompted. It cancels any unfinished attempt first: ```bash 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`](#rules) and log in with the new root token: ```bash kubectl -n openbao exec -it "$POD" -- bao login -no-print ``` Set a new password with step 4.4 of [First setup](#4-create-the-admin-login). Then [test the login](#5-test-the-admin-login) and [revoke the root token](#7-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: ```bash kubectl -n openbao get certificates ``` ## References - [OpenBao: Running on Kubernetes](https://openbao.org/docs/platform/k8s/helm/run/) - [OpenBao: Raft commands](https://openbao.org/docs/commands/operator/raft/) - [OpenBao: Raft API](https://openbao.org/docs/api/system/storage/raft/) - [OpenBao: Username and password login](https://openbao.org/docs/auth/userpass/) - [OpenBao: Generate a root token](https://openbao.org/docs/commands/operator/generate-root/) - [OpenBao: Listener settings](https://openbao.org/docs/configuration/listener/tcp/)