#!/usr/bin/env bash # init_oauth2_proxy_prole.sh # # Bootstrap the oauth2-proxy gate in front of Supabase Studio at db.prole.org # on the k3s homelab cluster. Companion to init_oauth2_proxy.sh (knoe.dev GKE) # but uses prole.org GCP project credentials and targets the k3s kubecontext. # # Gates access via Google Workspace OIDC (prole.org) so @prole.org identities # can sign in to Studio. Outside-domain users are rejected at this layer. # # Usage: # ./etc/init_oauth2_proxy_prole.sh # # Env vars (resolved from etc/secrets/* if not set in the shell): # OAUTH2_PROXY_CLIENT_ID ← from etc/secrets/oauth2-proxy-client-id-prole # OAUTH2_PROXY_CLIENT_SECRET ← from etc/secrets/oauth2-proxy-client-secret-prole # OAUTH2_PROXY_COOKIE_SECRET ← from etc/secrets/oauth2-proxy-cookie-secret-prole # # Optional: # K3S_KUBECONTEXT (default: $KUBECONTEXT then ambient) # NAMESPACE (default: supabase) # # Pre-reqs: # - OAuth 2.0 Web Application client created in the prole.org GCP project: # Authorized JS origins: https://db.prole.org # Authorized redirect URI: https://db.prole.org/oauth2/callback # Consent screen: Internal (prole.org Workspace) # Scopes: openid, email, profile # See deploy/gcp/gke/oauth2-proxy-google-oidc-secret-prole.example.yaml. # - Cookie secret generated with: openssl rand -base64 32 # - Three values saved (chmod 0600) to: # etc/secrets/oauth2-proxy-client-id-prole # etc/secrets/oauth2-proxy-client-secret-prole # etc/secrets/oauth2-proxy-cookie-secret-prole # # After this script runs, patch the db.prole.org Ingress/IngressRoute to route # through oauth2-proxy:80 instead of supabase-kong:8000 directly (see next # steps printed at the end of this script). # # When knoe-auth Round 1 ships an OIDC OP at https://api.prole.org/auth, change # --provider=google to --provider=oidc --oidc-issuer-url=https://api.prole.org/auth # in deploy/opentofu/k3s/manifests/knoe/oauth2-proxy-deployment-prole.yaml and # re-run this script. set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" MANIFEST_DIR="$REPO_ROOT/deploy/gcp/gke" K3S_MANIFEST_DIR="$REPO_ROOT/deploy/opentofu/k3s/manifests/knoe" NAMESPACE="${NAMESPACE:-supabase}" KCTX="${K3S_KUBECONTEXT:-${KUBECONTEXT:-}}" if [[ -n "$KCTX" ]]; then KCTX_FLAG=(--context="$KCTX") else KCTX_FLAG=() fi log() { printf "[%s] %s\n" "$(date +%H:%M:%S)" "$*"; } die() { log "ERROR: $*" >&2; exit 1; } resolve_secret() { local var="$1" file="$2" val="${!1:-}" if [[ -z "$val" && -f "$REPO_ROOT/etc/secrets/$file" ]]; then val="$(cat "$REPO_ROOT/etc/secrets/$file")" fi if [[ -z "$val" ]]; then die "missing $var (set the env var, or save the value into etc/secrets/$file)" fi printf '%s' "$val" } for tool in kubectl envsubst; do command -v "$tool" >/dev/null 2>&1 || die "required tool not found: $tool" done OAUTH2_PROXY_CLIENT_ID="$(resolve_secret OAUTH2_PROXY_CLIENT_ID oauth2-proxy-client-id-prole)" OAUTH2_PROXY_CLIENT_SECRET="$(resolve_secret OAUTH2_PROXY_CLIENT_SECRET oauth2-proxy-client-secret-prole)" OAUTH2_PROXY_COOKIE_SECRET="$(resolve_secret OAUTH2_PROXY_COOKIE_SECRET oauth2-proxy-cookie-secret-prole)" export OAUTH2_PROXY_CLIENT_ID OAUTH2_PROXY_CLIENT_SECRET OAUTH2_PROXY_COOKIE_SECRET SECRET_TMPL="$MANIFEST_DIR/oauth2-proxy-google-oidc-secret-prole.example.yaml" DEPLOY_MANIFEST="$K3S_MANIFEST_DIR/oauth2-proxy-deployment-prole.yaml" [[ -f "$SECRET_TMPL" ]] || die "missing manifest: $SECRET_TMPL" [[ -f "$DEPLOY_MANIFEST" ]] || die "missing manifest: $DEPLOY_MANIFEST" log "==> oauth2-proxy bootstrap (prole.org / k3s)" log " namespace : $NAMESPACE" log " kubectx : ${KCTX:-}" log "Applying oauth2-proxy-google-oidc Secret ..." envsubst '${OAUTH2_PROXY_CLIENT_ID} ${OAUTH2_PROXY_CLIENT_SECRET} ${OAUTH2_PROXY_COOKIE_SECRET}' \ < "$SECRET_TMPL" \ | kubectl "${KCTX_FLAG[@]}" -n "$NAMESPACE" apply -f - log "Applying oauth2-proxy ServiceAccount + Service + Deployment ..." kubectl "${KCTX_FLAG[@]}" -n "$NAMESPACE" apply -f "$DEPLOY_MANIFEST" log "Waiting for oauth2-proxy Deployment to become Ready (timeout 180s) ..." kubectl "${KCTX_FLAG[@]}" -n "$NAMESPACE" rollout status deployment/oauth2-proxy --timeout=180s log "==> oauth2-proxy bootstrap complete." echo "" echo " Next steps (NOT performed by this script):" echo " 1. Patch the db.prole.org Ingress/IngressRoute so traffic routes" echo " through oauth2-proxy:80 instead of supabase-kong:8000 directly." echo " Check current routing:" echo " kubectl -n supabase get ingress,ingressroute" echo " 2. Remove or disable any basic-auth plugin on the Studio route in" echo " the supabase-kong configmap; rollout-restart supabase-kong." echo " 3. Browser-test: https://db.prole.org/ → Google sign-in (prole.org" echo " account). Verify a non-prole.org account receives 403." echo ""