prole/etc/init_oauth2_proxy.sh
chrisfu 00f0ebec07 Merge claude/crazy-bose-fec256 into main
Bringing the long-running session-feature branch back into main in one
deliberate sweep. The branch carried the cluster work that's been live for
weeks (cross-cluster CNPG metrics, Grafana w/ Google OAuth, supabase
oauth2-proxy, cluster recovery, pg.0.knoe.dev + per-engineer onboarding,
GCS-backed CNPG backups via Workload Identity, the env-contamination
guard, the Junie brief queue, the cnpg-grafana CSRF + memory-request
fixes from today), while main accumulated Junie's parallel knoe-auth
Phase 2 OIDC work (full provider surface: discovery, authorize, token,
userinfo, JWKS, RS256 signing, code exchange, session services).

Key decision: the two branches did COMPETING rebrands off the same
starting point (5ba9b63, 2026-04-27):

  - claude branch (commit b355855, earlier): org.prole.authority.* →
                                              dev.knoe.auth.*
                                              (artifact renamed to
                                              knoe-auth.jar)
  - main (commit 9daa94b, recent): org.prole.authority.* →
                                    dev.knoe.authority.*
                                    (kept "authority" artifact name)

dev.knoe.auth wins: cluster runs from this name, the Maven artifact is
already knoe-auth.jar, and the broader rename is the documented
namespace direction (per ~/.claude/projects/-Users-chrisfu-dev-knoe-db/
memory/MEMORY.md). All of main's recent Phase 2 OIDC content was ported
from authority/src/.../dev/knoe/authority/ into
authority/src/.../dev/knoe/auth/ with package declarations rewritten.

== File-level resolution summary ==

Textual conflicts (4):

  authority/pom.xml
    - Took our artifactId="auth"
    - Took our branch's removal of spring-security-kerberos-client
      (verified: Junie's Phase 2 OIDC code does not import it; the dep
      was already-dead config)

  docs/pipeline-phases.md
    - Took our branch's "Phase 1 not started" status. Main had a
      misplaced " Complete" with a knoe-auth-Phase-1 commit ref
      in the autobuild Phase 1 section — different domain.

  docs/plans/knoe-auth-round-1.md
    - Took our branch's dev.knoe.auth file table (vs main's
      dev.knoe.authority listing). Pure rename mismatch.

  supabase/helm/knoe-supabase/templates/kong/config.yaml
    - Took our branch's onboard route + plain dashboard wiring.
      Main had an oauth2proxy.enabled toggle that put oauth2-proxy as
      a Kong upstream — but the deployed architecture (commit 25f1b2e)
      has oauth2-proxy in FRONT of Kong, not behind. Main's wrapper
      reflected an architecture that was never deployed.
    - Took our branch's removal of basic-auth from dashboard route
      (queue #15 brief still tracks the matching values.yaml /
      kong/deployment.yaml cleanup).

Java tree reconciliation (44 file-pairs):

  20 dual-path source files + 2 dual-path tests
    Body-identical between main's authority/ and our branch's auth/
    after stripping package decls — main's commit 9daa94b was a pure
    rebrand. Took our branch's auth/ version for all 22.

  8 main-only source files (Phase 2 OIDC), ported into auth/:
    web/JwksController.java
    web/OidcAuthorizeController.java
    web/OidcDiscoveryController.java
    web/OidcTokenController.java
    web/OidcUserInfoController.java
    session/OidcCodeService.java
    session/OidcTokenService.java
    session/SessionService.java

  12 main-only test files, ported into auth/:
    HealthControllerTest.java
    enroll/EnrollValueTypesTest.java
    enroll/EnrollmentControllerTest.java
    enroll/TotpServiceTest.java
    kerberos/KadminClientTest.java
    kerberos/KerberosSpnegoResultTest.java
    web/LoginControllerTest.java
    admin/AdminControllerTest.java
    user/PrincipalNormalizerTest.java
    regression/IdentityRegressionTest.java
    session/OidcCodeServiceTest.java
    session/SessionServiceTest.java

  Port mechanics: read main:authority/...<file> via git show, then sed
  rewrite `package dev.knoe.authority` → `package dev.knoe.auth` and
  `import dev.knoe.authority` → `import dev.knoe.auth`. Body content
  unchanged.

  authority/src/main/java/dev/knoe/authority/ — DELETED (duplicate)
  authority/src/test/java/dev/knoe/authority/  — DELETED (duplicate)

== Verification ==

- grep -rln '<<<<<<<' across .java/.md/.yaml/.yml/.sh/.xml/.tpl: clean
- find authority/src -path '*/dev/knoe/authority*': empty (subtree gone)
- grep 'package dev.knoe.authority' across repo: clean
- bash -n install.sh deploy.sh etc/preflight_kubecontext.sh: clean
- git ls-files -u | wc -l: 0 unmerged paths
- helm lint supabase/helm/knoe-supabase: pre-existing failure on
  studioIngress.enabled undefined in values.yaml (introduced by Junie
  on main; unrelated to this merge — flagging as follow-up).

== Followups (carried into TODO ranked queue or noted here) ==

  - helm lint failure: studioIngress block in values.yaml is missing
    enable flag; templates/studio/{ingress,oauth2proxy-deployment,
    oauth2proxy-service}.yaml all reference studioIngress.enabled with
    no default. Pre-existing on main; not introduced by this merge.
  - The five Junie briefs filed on this branch are now reachable from
    main at docs/plans/junie/{02,06,07,13,15}-*.md. Junie can pick them
    up in any order.
  - knoe-auth Phase 2 OIDC source (now at dev.knoe.auth.*) is not yet
    deployed to the cluster. Deployment is its own task.
  - The branch claude/crazy-bose-fec256 stays in place (worktree at
    .claude/worktrees/crazy-bose-fec256 may have ongoing context for
    Claude Code sessions). Safe to delete once next session starts
    cleanly from main.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 16:39:10 -07:00

128 lines
5.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# init_oauth2_proxy.sh
#
# Bootstrap the oauth2-proxy gate in front of Supabase Studio at
# db.0.knoe.dev. Gates access via Google Workspace OIDC (knoey.com) so any
# @knoey.com identity (chrisfu, ron) can sign in and share the Studio
# session. Outside-domain users are rejected at this layer.
#
# Usage:
# ./etc/init_oauth2_proxy.sh
#
# Env vars (resolved from etc/secrets/* if not set in the shell):
# OAUTH2_PROXY_CLIENT_ID ← from etc/secrets/oauth2-proxy-client-id
# OAUTH2_PROXY_CLIENT_SECRET ← from etc/secrets/oauth2-proxy-client-secret
# OAUTH2_PROXY_COOKIE_SECRET ← from etc/secrets/oauth2-proxy-cookie-secret
#
# Optional:
# APP_CLUSTER_KUBECONTEXT (default: $KUBECONTEXT then ambient)
# NAMESPACE (default: supabase)
#
# Pre-reqs:
# - OAuth 2.0 client created at GCP Console (see the secret template
# deploy/gcp/gke/oauth2-proxy-google-oidc-secret.example.yaml for the
# exact authorized redirect URI + consent screen settings).
# - cookie_secret generated with: openssl rand -base64 32
# - Three values saved into etc/secrets/oauth2-proxy-{client-id,client-secret,cookie-secret}
# (chmod 0600 each; etc/secrets/ is gitignored except for .keep).
#
# After this script runs and the oauth2-proxy Deployment is Ready, two
# manual steps complete the wiring (NOT done by this script — see the plan
# in docs/plans/ for the full sequence):
#
# 1. Patch the supabase-kong Ingress to route db.0.knoe.dev through
# oauth2-proxy:80 instead of supabase-kong:8000:
#
# kubectl --context=$APP_CLUSTER_KUBECONTEXT -n supabase patch ingress \
# supabase-kong --type=json -p '[
# {"op": "replace",
# "path": "/spec/rules/1/http/paths/0/backend/service/name",
# "value": "oauth2-proxy"},
# {"op": "replace",
# "path": "/spec/rules/1/http/paths/0/backend/service/port/number",
# "value": 80}
# ]'
# (verify the index by checking which rule has host=db.0.knoe.dev first;
# index may shift on future Helm reconciles)
#
# 2. Remove the basic-auth plugin from the dashboard route in the
# supabase-kong configmap (oauth2-proxy is the gate now; double-auth is
# friction). Then rollout-restart supabase-kong.
#
# When knoe-auth Round 1 ships an OIDC OP at https://api.knoe.dev/auth, change
# `--provider=google` to `--provider=oidc --oidc-issuer-url=https://api.knoe.dev/auth`
# in deploy/gcp/gke/oauth2-proxy-deployment.yaml and re-run this script.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
GKE_MANIFEST_DIR="$REPO_ROOT/deploy/gcp/gke"
NAMESPACE="${NAMESPACE:-supabase}"
KCTX="${APP_CLUSTER_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() {
# Resolve a value from env (preferred) or etc/secrets/<file>.
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)"
OAUTH2_PROXY_CLIENT_SECRET="$(resolve_secret OAUTH2_PROXY_CLIENT_SECRET oauth2-proxy-client-secret)"
OAUTH2_PROXY_COOKIE_SECRET="$(resolve_secret OAUTH2_PROXY_COOKIE_SECRET oauth2-proxy-cookie-secret)"
export OAUTH2_PROXY_CLIENT_ID OAUTH2_PROXY_CLIENT_SECRET OAUTH2_PROXY_COOKIE_SECRET
SECRET_TMPL="$GKE_MANIFEST_DIR/oauth2-proxy-google-oidc-secret.example.yaml"
DEPLOY_MANIFEST="$GKE_MANIFEST_DIR/oauth2-proxy-deployment.yaml"
[[ -f "$SECRET_TMPL" ]] || die "missing manifest: $SECRET_TMPL"
[[ -f "$DEPLOY_MANIFEST" ]] || die "missing manifest: $DEPLOY_MANIFEST"
log "==> oauth2-proxy bootstrap"
log " namespace : $NAMESPACE"
log " kubectx : ${KCTX:-<ambient>}"
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 + BackendConfig + 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 supabase-kong Ingress so db.0.knoe.dev routes to"
echo " oauth2-proxy:80 instead of supabase-kong:8000."
echo " 2. Remove the basic-auth plugin from the dashboard route in the"
echo " supabase-kong configmap, then rollout-restart supabase-kong."
echo " 3. In a browser, sign in to https://db.0.knoe.dev/ with a"
echo " @knoey.com Google account. Try a non-knoey account too — should"
echo " receive 403 from oauth2-proxy."
echo ""
echo " See the active plan in ~/.claude/plans/ for the exact patch commands."