Files
mtusi2026lab7/scripts/test-openbao-lifecycle.sh

178 lines
5.6 KiB
Bash
Executable File

#!/bin/sh
set -eu
script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
. "$script_dir/openbao-common.sh"
require_command curl
require_command docker
require_command python3
require_command "$BAO_BIN"
require_file "$BAO_CACERT"
require_unsealed
load_operator_token
test_dir=$(mktemp -d "$openbao_runtime_dir/.lifecycle.XXXXXX")
chmod 0700 "$test_dir"
sealed=0
compose() {
docker compose --project-directory "$repo_dir" \
--env-file "${ENV_FILE:-$repo_dir/lab7.env.example}" "$@"
}
agent_lookup() {
compose exec -T openbao-agent-worker sh -c '
BAO_ADDR=https://openbao:8200 \
BAO_CACERT=/run/security/openbao/ca.crt \
BAO_TOKEN=$(cat /run/openbao-agent/token) \
bao token lookup >/dev/null 2>&1
'
}
recover() {
status=$?
trap - EXIT HUP INT TERM
if [ "$sealed" -eq 1 ]; then
"$script_dir/openbao-unseal.sh" >/dev/null || status=1
compose restart openbao-agent-worker >/dev/null || status=1
attempts=0
while [ "$attempts" -lt 30 ] && ! agent_lookup; do
attempts=$((attempts + 1))
sleep 1
done
agent_lookup || status=1
fi
rm -rf "$test_dir"
exit "$status"
}
trap recover EXIT HUP INT TERM
http_status() {
header_file=$1
method=$2
path=$3
payload=${4:-}
output=$test_dir/response.json
if [ -n "$payload" ]; then
curl --silent --show-error --cacert "$BAO_CACERT" \
--header @"$header_file" \
--header "Content-Type: application/json" \
--request "$method" \
--data-binary "$payload" \
--output "$output" \
--write-out '%{http_code}' \
"$BAO_ADDR/v1/$path"
else
curl --silent --show-error --cacert "$BAO_CACERT" \
--header @"$header_file" \
--request "$method" \
--output "$output" \
--write-out '%{http_code}' \
"$BAO_ADDR/v1/$path"
fi
}
write_token_header() {
response_file=$1
header_file=$2
python3 - "$response_file" "$header_file" <<'PY'
import json
import os
import sys
with open(sys.argv[1], encoding="utf-8") as source:
document = json.load(source)
token = document.get("auth", document)["client_token"]
with open(sys.argv[2], "w", encoding="utf-8") as destination:
destination.write(f"X-Vault-Token: {token}\n")
os.chmod(sys.argv[2], 0o600)
PY
}
issue_login_material() {
name=$1
identity_dir=$test_dir/$name-identity
payload_file=$test_dir/$name-login.json
"$script_dir/openbao-issue-approle.sh" worker "$identity_dir" >/dev/null
python3 - "$identity_dir/role-id" "$identity_dir/secret-id" "$payload_file" <<'PY'
import json
import os
import sys
with open(sys.argv[1], encoding="utf-8") as source:
role_id = source.read().strip()
with open(sys.argv[2], encoding="utf-8") as source:
secret_id = source.read().strip()
with open(sys.argv[3], "w", encoding="utf-8") as destination:
json.dump({"role_id": role_id, "secret_id": secret_id}, destination)
os.chmod(sys.argv[3], 0o600)
PY
printf '%s\n' "$payload_file"
}
login_payload=$(issue_login_material renewable)
login_response=$test_dir/renewable-login-response.json
login_status=$(curl --silent --show-error --cacert "$BAO_CACERT" \
--header "Content-Type: application/json" \
--request POST \
--data-binary @"$login_payload" \
--output "$login_response" \
--write-out '%{http_code}' \
"$BAO_ADDR/v1/auth/approle/login")
[ "$login_status" = 200 ] || fail "AppRole login returned HTTP $login_status"
chmod 0600 "$login_response"
renewable_header=$test_dir/renewable-token-header
write_token_header "$login_response" "$renewable_header"
[ "$(http_status "$renewable_header" GET secret/data/messenger/worker)" = 200 ] || fail "worker token cannot read own secret"
[ "$(http_status "$renewable_header" POST auth/token/renew-self '{}')" = 200 ] || fail "worker token renewal failed"
[ "$(http_status "$renewable_header" POST auth/token/revoke-self)" = 204 ] || fail "worker token self-revocation failed"
[ "$(http_status "$renewable_header" GET auth/token/lookup-self)" = 403 ] || fail "revoked token remained valid"
short_response=$test_dir/short-token-response.json
"$BAO_BIN" token create \
-policy=messenger-worker \
-no-default-policy \
-ttl=2s \
-explicit-max-ttl=2s \
-format=json >"$short_response"
chmod 0600 "$short_response"
short_header=$test_dir/short-token-header
write_token_header "$short_response" "$short_header"
[ "$(http_status "$short_header" GET secret/data/messenger/worker)" = 200 ] || fail "short-lived token cannot read own secret"
sleep 3
[ "$(http_status "$short_header" GET secret/data/messenger/worker)" = 403 ] || fail "expired token remained valid"
sealed_login_payload=$(issue_login_material sealed)
"$BAO_BIN" operator seal >/dev/null
sealed=1
if agent_lookup; then
fail "Agent token lookup succeeded while OpenBao was sealed"
fi
sealed_status=$(curl --silent --show-error --cacert "$BAO_CACERT" \
--header "Content-Type: application/json" \
--request POST \
--data-binary @"$sealed_login_payload" \
--output "$test_dir/sealed-login-response.json" \
--write-out '%{http_code}' \
"$BAO_ADDR/v1/auth/approle/login")
[ "$sealed_status" = 503 ] || fail "sealed AppRole login: expected HTTP 503, got $sealed_status"
"$script_dir/openbao-unseal.sh" >/dev/null
compose restart openbao-agent-worker >/dev/null
attempts=0
while [ "$attempts" -lt 30 ] && ! agent_lookup; do
attempts=$((attempts + 1))
sleep 1
done
agent_lookup || fail "Agent did not authenticate after unseal"
sealed=0
echo "OpenBao lifecycle passed: renew, revoke, expiry, seal denial and Agent re-authentication"