#!/usr/bin/env bash # Operator-only OpenBao unseal command. # Reads 3 Shamir shares from $LAB7_OPERATOR_DIR/openbao/ (outside Git, never # mounted into containers) and unseals OpenBao over HTTPS with CA verification. # Never prints unseal keys. Idempotent if OpenBao is already unsealed. set -euo pipefail cd "$(dirname "$0")/.." OPERATOR_DIR="${LAB7_OPERATOR_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/mtusi-lab7-operator}" SHARES_DIR="" while [ $# -gt 0 ]; do case "$1" in --from-operator-dir) shift SHARES_DIR="${1:?missing path for --from-operator-dir}" ;; -h|--help) echo "Usage: $0 [--from-operator-dir ]" exit 0 ;; *) echo "Unknown argument: $1" >&2 exit 2 ;; esac shift done if [ -z "$SHARES_DIR" ]; then SHARES_DIR="${OPERATOR_DIR}/openbao" fi if [ -f .env ]; then # shellcheck disable=SC1091 set -a; . ./.env; set +a fi BAO_ADDR="${BAO_ADDR_HOST:-https://localhost:${OPENBAO_PORT:-8200}}" CA_CERT="${CA_FILE:-$(pwd)/secrets/pki/ca.crt}" if [ ! -s "$CA_CERT" ]; then echo "error: CA certificate not found at ${CA_CERT}" >&2 exit 1 fi # Wait briefly for listener readiness after container restart seal_json="" for _ in $(seq 1 30); do if seal_json="$(curl -fsS --cacert "$CA_CERT" "${BAO_ADDR}/v1/sys/seal-status" 2>/dev/null)"; then break fi sleep 1 done if [ -z "$seal_json" ]; then echo "error: OpenBao not reachable at ${BAO_ADDR}" >&2 exit 1 fi initialized="$(printf '%s' "$seal_json" | jq -r '.initialized')" sealed="$(printf '%s' "$seal_json" | jq -r '.sealed')" if [ "$initialized" != "true" ]; then echo "error: OpenBao is not initialized yet; run scripts/openbao-bootstrap.sh first" >&2 exit 1 fi if [ "$sealed" = "false" ]; then echo "openbao already unsealed (${BAO_ADDR})" exit 0 fi for idx in 1 2 3; do share_file="${SHARES_DIR}/unseal-share-${idx}" if [ ! -s "$share_file" ]; then echo "error: missing unseal share file ${share_file}" >&2 exit 1 fi # Pipe share strictly via stdin so key material never appears in process argv. resp="$(jq -Rs '{key: (gsub("\r|\n"; ""))}' < "$share_file" \ | curl -fsS --cacert "$CA_CERT" -X PUT \ -H 'Content-Type: application/json' \ --data-binary @- \ "${BAO_ADDR}/v1/sys/unseal")" sealed="$(printf '%s' "$resp" | jq -r '.sealed')" if [ "$sealed" = "false" ]; then break fi done if [ "$sealed" != "false" ]; then echo "error: OpenBao remained sealed after submitting 3 shares" >&2 exit 1 fi echo "openbao unsealed ok (${BAO_ADDR})"