forked from mtusi2026/mtusi2026lab7
grade3
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
OPERATOR_STATE_DIR="${OPERATOR_STATE_DIR:-./.bao-operator}"
|
||||
mkdir -p "$OPERATOR_STATE_DIR" ./infra/certs
|
||||
chmod 700 "$OPERATOR_STATE_DIR"
|
||||
for f in ./infra/certs/api-approle.env ./infra/certs/worker-approle.env; do
|
||||
if [ ! -f "$f" ]; then
|
||||
printf "BAO_ROLE_ID=\nBAO_SECRET_ID=\n" > "$f"
|
||||
chmod 600 "$f"
|
||||
fi
|
||||
done
|
||||
|
||||
bao_exec() {
|
||||
MSYS_NO_PATHCONV=1 docker compose exec -T \
|
||||
-e BAO_ADDR=https://127.0.0.1:8200 \
|
||||
-e BAO_CACERT=/openbao/certs/ca.crt \
|
||||
-e BAO_CLIENT_CERT=/openbao/certs/openbao.crt \
|
||||
-e BAO_CLIENT_KEY=/openbao/certs/openbao.key \
|
||||
${BAO_TOKEN:+-e BAO_TOKEN="$BAO_TOKEN"} \
|
||||
openbao bao "$@"
|
||||
}
|
||||
|
||||
if [ ! -s "$OPERATOR_STATE_DIR/init.json" ]; then
|
||||
INIT_OUT="$(bao_exec operator init -key-shares=1 -key-threshold=1 -format=json)"
|
||||
printf '%s\n' "$INIT_OUT" > "$OPERATOR_STATE_DIR/init.json"
|
||||
chmod 600 "$OPERATOR_STATE_DIR/init.json"
|
||||
fi
|
||||
|
||||
UNSEAL_KEY=$(python3 -c "import json; p='$OPERATOR_STATE_DIR/init.json'; d=json.load(open(p, encoding='utf-8')); print(d['unseal_keys_b64'][0])")
|
||||
ROOT_TOKEN=$(python3 -c "import json; p='$OPERATOR_STATE_DIR/init.json'; d=json.load(open(p, encoding='utf-8')); print(d['root_token'])")
|
||||
|
||||
if ! bao_exec status >/dev/null 2>&1; then
|
||||
bao_exec operator unseal "$UNSEAL_KEY" >/dev/null
|
||||
fi
|
||||
|
||||
export BAO_TOKEN="$ROOT_TOKEN"
|
||||
|
||||
bao_exec secrets enable -path=secret kv-v2 >/dev/null 2>&1 || true
|
||||
bao_exec auth enable approle >/dev/null 2>&1 || true
|
||||
|
||||
bao_exec policy write messenger-api /openbao/policies/policy-api.hcl >/dev/null
|
||||
bao_exec policy write messenger-worker /openbao/policies/policy-worker.hcl >/dev/null
|
||||
|
||||
bao_exec kv put secret/messenger/api/db \
|
||||
dsn="${DATABASE_URL:?set DATABASE_URL}" \
|
||||
hmac_key="${SECRET_HMAC_KEY:?set SECRET_HMAC_KEY}" >/dev/null
|
||||
|
||||
bao_exec kv put secret/messenger/api/s3 \
|
||||
access_key_id="${S3_ACCESS_KEY_ID:-appuser}" \
|
||||
secret_access_key="${S3_SECRET_ACCESS_KEY:-appsecretpassword}" >/dev/null
|
||||
|
||||
bao_exec kv put secret/messenger/worker/db \
|
||||
dsn="${DATABASE_URL:?set DATABASE_URL}" >/dev/null
|
||||
|
||||
bao_exec kv put secret/messenger/worker/s3 \
|
||||
access_key_id="${S3_ACCESS_KEY_ID:-appuser}" \
|
||||
secret_access_key="${S3_SECRET_ACCESS_KEY:-appsecretpassword}" >/dev/null
|
||||
|
||||
bao_exec write auth/approle/role/messenger-api \
|
||||
token_policies="messenger-api" \
|
||||
token_ttl="1h" \
|
||||
token_max_ttl="4h" >/dev/null
|
||||
|
||||
bao_exec write auth/approle/role/messenger-worker \
|
||||
token_policies="messenger-worker" \
|
||||
token_ttl="1h" \
|
||||
token_max_ttl="4h" >/dev/null
|
||||
|
||||
API_ROLE_ID=$(bao_exec read -field=role_id auth/approle/role/messenger-api/role-id)
|
||||
API_SECRET_ID=$(bao_exec write -f -field=secret_id auth/approle/role/messenger-api/secret-id)
|
||||
|
||||
WORKER_ROLE_ID=$(bao_exec read -field=role_id auth/approle/role/messenger-worker/role-id)
|
||||
WORKER_SECRET_ID=$(bao_exec write -f -field=secret_id auth/approle/role/messenger-worker/secret-id)
|
||||
|
||||
cat > ./infra/certs/api-approle.env <<EOF
|
||||
BAO_ROLE_ID=${API_ROLE_ID}
|
||||
BAO_SECRET_ID=${API_SECRET_ID}
|
||||
EOF
|
||||
|
||||
cat > ./infra/certs/worker-approle.env <<EOF
|
||||
BAO_ROLE_ID=${WORKER_ROLE_ID}
|
||||
BAO_SECRET_ID=${WORKER_SECRET_ID}
|
||||
EOF
|
||||
|
||||
chmod 600 ./infra/certs/api-approle.env ./infra/certs/worker-approle.env
|
||||
echo "OpenBao configured with scoped AppRole credentials (root token NOT written to app env)."
|
||||
Reference in New Issue
Block a user