Lab 7
Skeleton contracts / contracts (pull_request) Canceled after 0s

This commit is contained in:
2026-11-27 20:47:58 +03:00
parent 20afc2c790
commit d995f98446
97 changed files with 8638 additions and 56 deletions
+95
View File
@@ -0,0 +1,95 @@
#!/bin/sh
set -eu
# Operator-run OpenBao bootstrap for the LOCAL lab stand. NOT committed secrets.
#
# * initialises and unseals OpenBao (1/1 share for the lab; production uses
# Shamir shares held by separate operators);
# * enables KV v2 + AppRole, writes the two least-privilege policies;
# * creates the API and worker AppRoles with short-lived SecretIDs;
# * writes each service's scoped secret from ./secrets (generated by
# scripts/gen-secrets.sh).
#
# The root token, unseal key and SecretIDs are written under ./secrets/openbao
# (gitignored, mode 600) and are NEVER printed. Re-running is safe.
cd "$(dirname "$0")/.."
OUT=secrets/openbao
mkdir -p "$OUT"
chmod 700 "$OUT" 2>/dev/null || true
compose() { docker compose --env-file .env "$@"; }
bao_exec() { compose exec -T -e BAO_TOKEN="$ROOT_TOKEN" openbao bao "$@"; }
need() {
command -v "$1" >/dev/null 2>&1 || { echo "required tool missing: $1" >&2; exit 2; }
}
need docker
need python
# 1. Wait until the OpenBao container answers (sealed is fine).
echo "waiting for OpenBao ..."
# "reachable" includes sealed: bao status exits 2 when sealed, 1 when the API
# cannot be reached at all.
reachable() { compose exec -T openbao sh -c 'bao status >/dev/null 2>&1; c=$?; [ "$c" -eq 0 ] || [ "$c" -eq 2 ]'; }
i=0
until reachable || [ $i -ge 60 ]; do
i=$((i+1)); sleep 2
done
if [ $i -ge 60 ]; then echo "OpenBao did not become reachable" >&2; exit 1; fi
# 2. Initialise once if the operator has not done it yet.
if [ ! -s "$OUT/init.json" ]; then
echo "initialising OpenBao (root token + unseal share -> $OUT/init.json, not printed)"
compose exec -T openbao bao operator init -key-shares=1 -key-threshold=1 -format=json > "$OUT/init.json"
chmod 600 "$OUT/init.json"
fi
UNSEAL_KEY=$(python -c "import json;print(json.load(open('$OUT/init.json'))['unseal_keys_b64'][0])")
ROOT_TOKEN=$(python -c "import json;print(json.load(open('$OUT/init.json'))['root_token'])")
# 3. Unseal (idempotent: already-unsealed errors are ignored).
compose exec -T openbao bao operator unseal "$UNSEAL_KEY" >/dev/null 2>&1 || true
# 4. KV v2 mount.
if ! bao_exec secrets list -format=json 2>/dev/null | python -c "import json,sys;sys.exit(0 if 'secret/' in json.load(sys.stdin) else 1)"; then
bao_exec secrets enable -path=secret kv-v2 >/dev/null
echo "enabled kv-v2 at secret/"
fi
# 5. Policies and AppRoles.
bao_exec policy write messenger-api - < infra/openbao/policies/api.hcl >/dev/null
bao_exec policy write messenger-worker - < infra/openbao/policies/worker.hcl >/dev/null
if ! bao_exec auth list -format=json 2>/dev/null | python -c "import json,sys;sys.exit(0 if 'approle/' in json.load(sys.stdin) else 1)"; then
bao_exec auth enable approle >/dev/null
echo "enabled approle"
fi
for role in messenger-api messenger-worker; do
bao_exec write "auth/approle/role/${role}" \
token_policies="${role}" token_ttl=15m token_max_ttl=1h secret_id_ttl=24h >/dev/null
done
emit_identity() {
role="$1"; prefix="$2"
bao_exec read -field=role_id "auth/approle/role/${role}/role-id" > "$OUT/${prefix}_role_id"
bao_exec write -f -field=secret_id "auth/approle/role/${role}/secret-id" > "$OUT/${prefix}_secret_id"
chmod 600 "$OUT/${prefix}_role_id" "$OUT/${prefix}_secret_id"
}
emit_identity messenger-api api
emit_identity messenger-worker worker
# Backwards-compatible names used by the api service's secret mounts.
cp -f "$OUT/api_role_id" "$OUT/role_id"
cp -f "$OUT/api_secret_id" "$OUT/secret_id"
chmod 600 "$OUT/role_id" "$OUT/secret_id"
# 6. Scoped secrets. API and worker each read only their own path.
read_secret() { cat "secrets/$1"; }
bao_exec kv put secret/messenger/api \
s3_access_key_id="$(read_secret s3_access_key_id)" \
s3_secret_access_key="$(read_secret s3_secret_access_key)" \
broker_url="$(read_secret broker_url)" >/dev/null
bao_exec kv put secret/messenger/worker \
s3_access_key_id="$(read_secret s3_access_key_id)" \
s3_secret_access_key="$(read_secret s3_secret_access_key)" \
broker_url="$(read_secret broker_url)" >/dev/null
echo "OpenBao bootstrap complete: policies messenger-api/messenger-worker, AppRoles, KV secret/messenger/{api,worker}."
echo "Identity files: $OUT/{api,worker}_{role_id,secret_id} (mode 600, gitignored)."