bettersight-statement-extra.../pocketbase/configure-secrets.sh

42 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# The ONLY place R2/Google secrets are handled - never put these in a
# migration file (migrations are committed to git). Run once after the
# pocketbase container is up and a superuser exists:
#
# docker compose exec pocketbase ./pocketbase superuser upsert "$POCKETBASE_SUPERUSER_EMAIL" "$POCKETBASE_SUPERUSER_PASSWORD"
# set -a; source .env; set +a; ./pocketbase/configure-secrets.sh
#
# Safe to re-run (every call is a PATCH against the current state).
set -euo pipefail
POCKETBASE_URL="${POCKETBASE_URL:-http://localhost:8090}"
for var in POCKETBASE_SUPERUSER_EMAIL POCKETBASE_SUPERUSER_PASSWORD \
R2_ACCOUNT_ID R2_ACCESS_KEY R2_SECRET_KEY R2_BUCKET \
GOOGLE_OAUTH_CLIENT_ID GOOGLE_OAUTH_CLIENT_SECRET; do
if [ -z "${!var:-}" ]; then
echo "Missing required env var: $var (see .env.example)" >&2
exit 1
fi
done
echo "Authenticating as superuser..."
TOKEN=$(curl -sf -X POST "$POCKETBASE_URL/api/collections/_superusers/auth-with-password" \
-H "Content-Type: application/json" \
-d "{\"identity\": \"$POCKETBASE_SUPERUSER_EMAIL\", \"password\": \"$POCKETBASE_SUPERUSER_PASSWORD\"}" \
| python3 -c "import json,sys; print(json.load(sys.stdin)['token'])")
echo "Enabling R2 (S3-compatible) file storage..."
curl -sf -X PATCH "$POCKETBASE_URL/api/settings" \
-H "Authorization: $TOKEN" -H "Content-Type: application/json" \
-d "{\"s3\": {\"enabled\": true, \"bucket\": \"$R2_BUCKET\", \"region\": \"auto\", \"endpoint\": \"https://$R2_ACCOUNT_ID.r2.cloudflarestorage.com\", \"accessKey\": \"$R2_ACCESS_KEY\", \"secret\": \"$R2_SECRET_KEY\", \"forcePathStyle\": true}}" \
> /dev/null
echo "Enabling Google sign-in on the users collection..."
curl -sf -X PATCH "$POCKETBASE_URL/api/collections/users" \
-H "Authorization: $TOKEN" -H "Content-Type: application/json" \
-d "{\"oauth2\": {\"enabled\": true, \"providers\": [{\"name\": \"google\", \"clientId\": \"$GOOGLE_OAUTH_CLIENT_ID\", \"clientSecret\": \"$GOOGLE_OAUTH_CLIENT_SECRET\"}]}}" \
> /dev/null
echo "Done - R2 storage and Google sign-in are configured."