Files
mtusi2026lab7/scripts/openbao-bootstrap.sh
T
dizi c03e067de8
CI / verify (pull_request) Failing after 1m23s
chore: finalize lab7 security verification
2026-11-06 21:44:48 +03:00

93 lines
2.6 KiB
Bash
Executable File

#!/bin/sh
set -eu
script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
. "$script_dir/openbao-common.sh"
if [ "$#" -ne 2 ]; then
echo "usage: $0 <api-secret-json> <worker-secret-json>" >&2
exit 2
fi
api_secret_file=$1
worker_secret_file=$2
require_command "$BAO_BIN"
require_command python3
require_runtime_path "$api_secret_file"
require_runtime_path "$worker_secret_file"
require_private_file "$api_secret_file"
require_private_file "$worker_secret_file"
require_unsealed
load_operator_token
python3 - "$api_secret_file" "$worker_secret_file" <<'PY'
import json
import sys
for path in sys.argv[1:]:
with open(path, encoding="utf-8") as source:
value = json.load(source)
if not isinstance(value, dict) or not value:
raise SystemExit(f"secret input must be a non-empty JSON object: {path}")
PY
mounts_file=$(mktemp "$openbao_runtime_dir/.mounts.XXXXXX")
auth_file=$(mktemp "$openbao_runtime_dir/.auth.XXXXXX")
chmod 0600 "$mounts_file" "$auth_file"
cleanup() {
rm -f "$mounts_file" "$auth_file"
}
trap cleanup EXIT HUP INT TERM
"$BAO_BIN" secrets list -format=json >"$mounts_file"
if ! python3 - "$mounts_file" <<'PY'
import json
import sys
with open(sys.argv[1], encoding="utf-8") as source:
raise SystemExit(0 if "secret/" in json.load(source) else 1)
PY
then
"$BAO_BIN" secrets enable -path=secret -version=2 kv >/dev/null
fi
"$BAO_BIN" auth list -format=json >"$auth_file"
if ! python3 - "$auth_file" <<'PY'
import json
import sys
with open(sys.argv[1], encoding="utf-8") as source:
raise SystemExit(0 if "approle/" in json.load(source) else 1)
PY
then
"$BAO_BIN" auth enable approle >/dev/null
fi
"$BAO_BIN" policy write messenger-api "$repo_dir/infra/openbao/policies/messenger-api.hcl" >/dev/null
"$BAO_BIN" policy write messenger-worker "$repo_dir/infra/openbao/policies/messenger-worker.hcl" >/dev/null
for service in api worker; do
"$BAO_BIN" delete "auth/approle/role/messenger-$service" >/dev/null 2>&1 || true
"$BAO_BIN" write "auth/approle/role/messenger-$service" \
bind_secret_id=true \
secret_id_ttl=1h \
secret_id_num_uses=0 \
token_policies="messenger-$service" \
token_no_default_policy=true \
token_ttl=15m \
token_max_ttl=1h \
token_num_uses=0 >/dev/null
done
"$BAO_BIN" kv put -mount=secret messenger/api @"$api_secret_file" >/dev/null
"$BAO_BIN" kv put -mount=secret messenger/worker @"$worker_secret_file" >/dev/null
"$script_dir/openbao-issue-approle.sh" api "$openbao_runtime_dir/approle/api"
"$script_dir/openbao-issue-approle.sh" worker "$openbao_runtime_dir/approle/worker"
echo "OpenBao policies, AppRoles and KV v2 secrets configured"