Files
mtusi2026lab7/scripts/test_sealed_openbao.sh
T
2026-11-25 01:43:37 +03:00

66 lines
2.6 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
mkdir -p evidence
OUT_FILE="evidence/sealed-openbao.txt"
INIT_FILE="${1:-./.bao-operator/init.json}"
if [ ! -f "${INIT_FILE}" ]; then
echo "ERROR: init file not found at ${INIT_FILE}" >&2
exit 1
fi
ROOT_TOKEN="$(python3 -c 'import json, sys; d=json.load(open(sys.argv[1])); print(d.get("root_token") or d.get("RootToken") or "")' "${INIT_FILE}")"
UNSEAL_KEY="$(python3 -c 'import json, sys; d=json.load(open(sys.argv[1])); keys=d.get("unseal_keys_b64") or d.get("keys_base64") or []; print(keys[0] if keys else "")' "${INIT_FILE}")"
if [ -z "${ROOT_TOKEN}" ] || [ -z "${UNSEAL_KEY}" ]; then
echo "ERROR: could not extract root_token or unseal key from ${INIT_FILE}" >&2
exit 1
fi
cleanup() {
docker compose --env-file .env exec -T -e UNSEAL_KEY="${UNSEAL_KEY}" openbao sh -c '
export BAO_ADDR=https://127.0.0.1:8200 BAO_CACERT=/openbao/certs/ca.crt
bao operator unseal "$UNSEAL_KEY" >/dev/null 2>&1 || true
' >/dev/null 2>&1 || true
}
trap cleanup EXIT
{
echo "=== OpenBao Sealed Behavior Check ($(date -u '+%Y-%m-%dT%H:%M:%SZ')) ==="
echo ""
echo "--- 1. Seal OpenBao ---"
docker compose --env-file .env exec -T -e BAO_TOKEN="${ROOT_TOKEN}" openbao sh -c '
export BAO_ADDR=https://127.0.0.1:8200 BAO_CACERT=/openbao/certs/ca.crt
bao operator seal
bao status | grep -E "Initialized|Sealed" || true
'
echo ""
echo "--- 2. Attempt secret read while sealed (expected failure/sealed) ---"
docker compose --env-file .env exec -T -e BAO_TOKEN="${ROOT_TOKEN}" openbao sh -c '
export BAO_ADDR=https://127.0.0.1:8200 BAO_CACERT=/openbao/certs/ca.crt
bao kv get secret/messenger/api/db 2>&1 || true
'
echo ""
echo "--- 3. Ingress readiness check while OpenBao is sealed ---"
curl -sS -w "\nHTTP_CODE:%{http_code}\n" --ssl-no-revoke --cacert ./infra/certs/ca.crt https://localhost:8443/health/ready || true
echo ""
echo "--- 4. Unseal OpenBao ---"
docker compose --env-file .env exec -T -e UNSEAL_KEY="${UNSEAL_KEY}" openbao sh -c '
export BAO_ADDR=https://127.0.0.1:8200 BAO_CACERT=/openbao/certs/ca.crt
bao operator unseal "$UNSEAL_KEY" | grep -E "Initialized|Sealed|Progress" || true
'
echo ""
echo "--- 5. Verify secret read & ingress readiness after unseal ---"
docker compose --env-file .env exec -T -e BAO_TOKEN="${ROOT_TOKEN}" openbao sh -c '
export BAO_ADDR=https://127.0.0.1:8200 BAO_CACERT=/openbao/certs/ca.crt
bao kv get secret/messenger/api/db >/dev/null && echo "BAO_KV_GET_AFTER_UNSEAL=OK"
'
curl -sS -w "\nHTTP_CODE:%{http_code}\n" --ssl-no-revoke --cacert ./infra/certs/ca.crt https://localhost:8443/health/ready || true
} > "${OUT_FILE}"
cat "${OUT_FILE}"