#!/bin/sh set -eu project_root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd) state_file="$project_root/.operator/openbao-init.json" cd "$project_root" [ -s "$state_file" ] || { printf '%s\n' "OpenBao operator state is required" >&2; exit 1; } root_token=$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1], encoding="utf-8"))["root_token"])' "$state_file") unseal_key=$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1], encoding="utf-8"))["unseal_keys_b64"][0])' "$state_file") sealed_by_test=false bao_root() { docker compose exec -T -e BAO_TOKEN="$root_token" openbao bao "$@" } recover() { if [ "$sealed_by_test" = true ]; then docker compose exec -T openbao bao operator unseal "$unseal_key" >/dev/null 2>&1 || true docker compose up -d --no-deps --no-build --force-recreate api >/dev/null 2>&1 || true fi } trap recover EXIT HUP INT TERM bao_root write auth/approle/role/grade4-lifecycle \ token_policies=default,api token_ttl=10s token_max_ttl=24s \ secret_id_ttl=5s secret_id_num_uses=1 >/dev/null role_id=$(bao_root read -field=role_id auth/approle/role/grade4-lifecycle/role-id) secret_id=$(bao_root write -f -field=secret_id auth/approle/role/grade4-lifecycle/secret-id) token=$(docker compose exec -T openbao bao write -field=token auth/approle/login \ role_id="$role_id" secret_id="$secret_id") if docker compose exec -T openbao bao write -field=token auth/approle/login \ role_id="$role_id" secret_id="$secret_id" >/dev/null 2>&1; then printf '%s\n' "One-use SecretID was accepted twice" >&2 exit 1 fi printf '%s\n' "PASS: used SecretID is revoked" sleep 4 docker compose exec -T -e BAO_TOKEN="$token" openbao bao token renew -increment=10s >/dev/null sleep 7 docker compose exec -T -e BAO_TOKEN="$token" openbao bao kv get secret/api >/dev/null printf '%s\n' "PASS: renewable token works past its original TTL" sleep 14 if docker compose exec -T -e BAO_TOKEN="$token" openbao bao kv get secret/api >/dev/null 2>&1; then printf '%s\n' "Token exceeded its maximum TTL" >&2 exit 1 fi printf '%s\n' "PASS: token expires at bounded maximum TTL" bao_root delete auth/approle/role/grade4-lifecycle >/dev/null curl --silent --show-error --fail --cacert secrets/pki/ca.crt \ https://127.0.0.1:8443/health/live >/dev/null bao_root operator seal >/dev/null sealed_by_test=true curl --silent --show-error --fail --cacert secrets/pki/ca.crt \ https://127.0.0.1:8443/health/live >/dev/null printf '%s\n' "PASS: running API keeps only already-loaded credentials while OpenBao is sealed" role_id_api=$(cat secrets/openbao/api_role_id.txt) secret_id_api=$(cat secrets/openbao/api_secret_id.txt) if docker compose exec -T openbao bao write -field=token auth/approle/login \ role_id="$role_id_api" secret_id="$secret_id_api" >/dev/null 2>&1; then printf '%s\n' "AppRole login succeeded while OpenBao was sealed" >&2 exit 1 fi docker compose up -d --no-deps --no-build --force-recreate api >/dev/null sleep 4 if [ -n "$(docker compose ps --status running -q api)" ]; then printf '%s\n' "Fresh API process remained running without OpenBao bootstrap" >&2 exit 1 fi printf '%s\n' "PASS: fresh API fails closed while OpenBao is sealed" docker compose exec -T openbao bao operator unseal "$unseal_key" >/dev/null sealed_by_test=false docker compose up -d --no-deps --no-build --force-recreate api >/dev/null attempts=0 until curl --silent --show-error --fail --cacert secrets/pki/ca.crt \ https://127.0.0.1:8443/health/live >/dev/null 2>&1; do attempts=$((attempts + 1)) [ "$attempts" -lt 40 ] || { printf '%s\n' "API did not recover after unseal" >&2; exit 1; } sleep 1 done printf '%s\n' "PASS: operator unseal restores AppRole bootstrap without image rebuild"