#!/bin/sh set -eu project_root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd) operator_dir="$project_root/.operator" state_file="$operator_dir/openbao-init.json" cd "$project_root" command -v python3 >/dev/null 2>&1 || { printf '%s\n' "Python 3 is required for OpenBao operator bootstrap" >&2 exit 1 } json_field() { python3 -c 'import json, sys value = json.load(sys.stdin) for part in sys.argv[1].split("."): value = value[int(part)] if isinstance(value, list) else value[part] print(str(value).lower() if isinstance(value, bool) else value)' "$1" } umask 077 mkdir -p "$operator_dir" "$project_root/secrets/openbao" chmod 700 "$operator_dir" docker compose up -d openbao attempts=0 until status_json=$(docker compose exec -T openbao bao status -format=json 2>/dev/null) || [ -n "${status_json:-}" ]; do attempts=$((attempts + 1)) if [ "$attempts" -ge 60 ]; then printf '%s\n' "Timed out waiting for OpenBao" >&2 exit 1 fi sleep 2 done initialized=$(printf '%s' "$status_json" | json_field initialized) if [ "$initialized" != true ]; then init_json=$(docker compose exec -T openbao bao operator init -format=json -key-shares=1 -key-threshold=1) printf '%s\n' "$init_json" > "$state_file" chmod 600 "$state_file" fi if [ ! -s "$state_file" ]; then printf '%s\n' "OpenBao is initialized but .operator/openbao-init.json is unavailable; an operator must provide unseal material" >&2 exit 1 fi unseal_key=$(json_field unseal_keys_b64.0 < "$state_file") root_token=$(json_field root_token < "$state_file") sealed=$(docker compose exec -T openbao bao status -format=json 2>/dev/null | json_field sealed) if [ "$sealed" = true ]; then docker compose exec -T openbao bao operator unseal "$unseal_key" >/dev/null fi bao_root() { docker compose exec -T -e BAO_TOKEN="$root_token" openbao bao "$@" } if ! bao_root secrets list -format=json | grep -q '"secret/"'; then bao_root secrets enable -path=secret kv-v2 >/dev/null fi if ! bao_root auth list -format=json | grep -q '"approle/"'; then bao_root auth enable approle >/dev/null fi for role in api worker dispatcher; do bao_root policy write "$role" "/policies/$role-policy.hcl" >/dev/null bao_root kv put "secret/$role" "@/bootstrap/$role.json" >/dev/null bao_root write "auth/approle/role/$role" \ "token_policies=$role" token_ttl=15m token_max_ttl=30m \ secret_id_ttl=168h secret_id_num_uses=0 >/dev/null bao_root read -field=role_id "auth/approle/role/$role/role-id" \ > "$project_root/secrets/openbao/${role}_role_id.txt" bao_root write -f -field=secret_id "auth/approle/role/$role/secret-id" \ > "$project_root/secrets/openbao/${role}_secret_id.txt" chmod 600 "$project_root/secrets/openbao/${role}_role_id.txt" \ "$project_root/secrets/openbao/${role}_secret_id.txt" done printf '%s\n' "OpenBao is unsealed and scoped AppRoles are configured; root/unseal material remains under .operator only."