forked from mtusi2026/mtusi2026lab7
test: verify security boundaries and recovery
This commit is contained in:
Executable
+140
@@ -0,0 +1,140 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
|
||||
script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
||||
. "$script_dir/openbao-common.sh"
|
||||
|
||||
require_command curl
|
||||
require_command python3
|
||||
require_command "$BAO_BIN"
|
||||
require_file "$BAO_CACERT"
|
||||
require_unsealed
|
||||
load_operator_token
|
||||
|
||||
test_dir="$openbao_runtime_dir/policy-test.$$"
|
||||
mkdir -p "$test_dir"
|
||||
chmod 0700 "$test_dir"
|
||||
|
||||
cleanup() {
|
||||
for service in api worker; do
|
||||
headers="$test_dir/$service/token-header"
|
||||
if [ -f "$headers" ]; then
|
||||
curl --silent --show-error --cacert "$BAO_CACERT" \
|
||||
--header @"$headers" \
|
||||
--request POST \
|
||||
"$BAO_ADDR/v1/auth/token/revoke-self" >/dev/null 2>&1 || true
|
||||
fi
|
||||
done
|
||||
rm -rf "$test_dir"
|
||||
}
|
||||
|
||||
trap cleanup EXIT HUP INT TERM
|
||||
|
||||
login() {
|
||||
service=$1
|
||||
identity_dir="$test_dir/$service/identity"
|
||||
session_dir="$test_dir/$service"
|
||||
response_file="$session_dir/login.json"
|
||||
payload_file="$session_dir/login-payload.json"
|
||||
|
||||
mkdir -p "$session_dir"
|
||||
chmod 0700 "$session_dir"
|
||||
"$script_dir/openbao-issue-approle.sh" "$service" "$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 role_source:
|
||||
role_id = role_source.read().strip()
|
||||
with open(sys.argv[2], encoding="utf-8") as secret_source:
|
||||
secret_id = secret_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
|
||||
|
||||
status=$(curl --silent --show-error --cacert "$BAO_CACERT" \
|
||||
--header "Content-Type: application/json" \
|
||||
--request POST \
|
||||
--data-binary @"$payload_file" \
|
||||
--output "$response_file" \
|
||||
--write-out '%{http_code}' \
|
||||
"$BAO_ADDR/v1/auth/approle/login")
|
||||
[ "$status" = "200" ] || fail "$service AppRole login returned HTTP $status"
|
||||
chmod 0600 "$response_file"
|
||||
|
||||
python3 - "$response_file" "$service" "$session_dir/token-header" <<'PY'
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
with open(sys.argv[1], encoding="utf-8") as source:
|
||||
auth = json.load(source)["auth"]
|
||||
|
||||
expected_policy = f"messenger-{sys.argv[2]}"
|
||||
if not auth["renewable"]:
|
||||
raise SystemExit("AppRole token is not renewable")
|
||||
if not 0 < auth["lease_duration"] <= 3600:
|
||||
raise SystemExit("AppRole token TTL is outside the configured limit")
|
||||
if auth["policies"] != [expected_policy]:
|
||||
raise SystemExit(f"unexpected token policies: {auth['policies']}")
|
||||
|
||||
with open(sys.argv[3], "w", encoding="utf-8") as destination:
|
||||
destination.write(f"X-Vault-Token: {auth['client_token']}\n")
|
||||
os.chmod(sys.argv[3], 0o600)
|
||||
PY
|
||||
|
||||
rm -f "$payload_file" "$response_file" "$identity_dir/secret-id"
|
||||
}
|
||||
|
||||
expect_status() {
|
||||
service=$1
|
||||
method=$2
|
||||
path=$3
|
||||
expected=$4
|
||||
payload=${5:-}
|
||||
headers="$test_dir/$service/token-header"
|
||||
output="$test_dir/$service/response.json"
|
||||
|
||||
if [ -n "$payload" ]; then
|
||||
status=$(curl --silent --show-error --cacert "$BAO_CACERT" \
|
||||
--header @"$headers" \
|
||||
--header "Content-Type: application/json" \
|
||||
--request "$method" \
|
||||
--data-binary "$payload" \
|
||||
--output "$output" \
|
||||
--write-out '%{http_code}' \
|
||||
"$BAO_ADDR/v1/$path")
|
||||
else
|
||||
status=$(curl --silent --show-error --cacert "$BAO_CACERT" \
|
||||
--header @"$headers" \
|
||||
--request "$method" \
|
||||
--output "$output" \
|
||||
--write-out '%{http_code}' \
|
||||
"$BAO_ADDR/v1/$path")
|
||||
fi
|
||||
|
||||
[ "$status" = "$expected" ] || fail "$service $method $path: expected HTTP $expected, got $status"
|
||||
echo "PASS $service $method $path -> $status"
|
||||
}
|
||||
|
||||
login api
|
||||
login worker
|
||||
|
||||
expect_status api GET secret/data/messenger/api 200
|
||||
expect_status api GET secret/data/messenger/worker 403
|
||||
expect_status api POST secret/data/messenger/api/probe 403 '{"data":{"probe":"denied"}}'
|
||||
expect_status api GET sys/policies/acl 403
|
||||
expect_status api POST auth/token/renew-self 200 '{}'
|
||||
|
||||
expect_status worker GET secret/data/messenger/worker 200
|
||||
expect_status worker GET secret/data/messenger/api 403
|
||||
expect_status worker POST secret/data/messenger/worker/probe 403 '{"data":{"probe":"denied"}}'
|
||||
expect_status worker GET sys/policies/acl 403
|
||||
expect_status worker POST auth/token/renew-self 200 '{}'
|
||||
|
||||
echo "OpenBao AppRole allow/deny and renewal checks passed"
|
||||
|
||||
Reference in New Issue
Block a user