prole/mock_val/init_grafana_oauth.sh
chrisfu 3f96f66a78 feat(mock_val): rewrite init scripts; add new service init scripts
Rewrites (updated for knoe namespace, GKE support, and current service configs):
  init_gitlab.sh, init_kong.sh, init_cnpg_backup.sh, init_monitoring.sh,
  init_garage_store.sh, init_gitea.sh, init_kdc.sh, init_openbao.sh,
  init_argocd.sh, init_certmgr.sh, init_common_services.sh, init_db_manager.sh,
  init_forgejo.sh, init_k3s_registry.sh, init_kerberos.sh, init_nginx_ingress.sh,
  init_port_forwards.sh, init_registry.sh, init_service_layer.sh

Deleted: init_cloudnative_pg.sh (superseded by init_cnpg_gke.sh)

New scripts:
  init_cnpg_gke.sh      — CNPG setup for GKE with Workload Identity
  init_knoe_auth.sh     — knoe-auth OIDC service init
  init_knoe_users.sh    — user provisioning
  init_redis.sh         — Redis init
  init_oauth2_proxy.sh / init_oauth2_proxy_prole.sh — OAuth2 proxy setup
  init_grafana_oauth.sh / init_grafana_oauth_prole.sh — Grafana OAuth wiring
  init_1password.sh     — 1Password Connect init
  init_min.sh           — minimal bootstrap

Co-authored-by: Junie <junie@jetbrains.com>
2026-05-23 21:31:27 -07:00

100 lines
4.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# init_grafana_oauth.sh
#
# Bootstrap the Google-OAuth secret that kps-grafana mounts as env vars
# (GF_AUTH_GOOGLE_CLIENT_ID / GF_AUTH_GOOGLE_CLIENT_SECRET) for its native
# auth.google sign-in flow. Companion to:
# - deploy/gcp/gke/grafana-google-oidc-secret.example.yaml (envsubst template)
# - monitoring/kps-values-gke.yaml (Helm overrides for grafana subchart)
#
# Auth model:
# - Anyone in the @knoey.com Workspace can sign in (auth.google.allowed_domains).
# - chrisfu@knoey.com + ron@knoey.com get Admin (role_attribute_path JMESPath).
# - Everyone else @knoey.com gets Editor.
#
# Usage:
# ./etc/init_grafana_oauth.sh
#
# Env vars (resolved from etc/secrets/* if not set in the shell):
# GRAFANA_GOOGLE_CLIENT_ID ← from etc/secrets/grafana-google-oidc-client-id
# GRAFANA_GOOGLE_CLIENT_SECRET ← from etc/secrets/grafana-google-oidc-client-secret
#
# Optional:
# APP_CLUSTER_KUBECONTEXT (default: $KUBECONTEXT then ambient)
# NAMESPACE (default: monitoring)
#
# Pre-reqs:
# - OAuth 2.0 client created at GCP Console (see the secret template
# deploy/gcp/gke/grafana-google-oidc-secret.example.yaml for the
# exact authorized redirect URI + consent screen settings).
# - Two values saved into etc/secrets/grafana-google-oidc-client-{id,secret}
# (chmod 0600 each; etc/secrets/ is gitignored except for .keep).
#
# After this script runs and the Secret is in place, the next `helm upgrade`
# (or kubectl-apply of the chart's rendered manifest) of kps-grafana picks
# up the Secret via `envFromSecret: grafana-google-oidc`. Verify with:
# kubectl --context=$APP_CLUSTER_KUBECONTEXT -n monitoring exec -it kps-grafana-0 -c grafana -- \
# env | grep GF_AUTH_GOOGLE_
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:-monitoring}"
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() {
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
GRAFANA_GOOGLE_CLIENT_ID="$(resolve_secret GRAFANA_GOOGLE_CLIENT_ID grafana-google-oidc-client-id)"
GRAFANA_GOOGLE_CLIENT_SECRET="$(resolve_secret GRAFANA_GOOGLE_CLIENT_SECRET grafana-google-oidc-client-secret)"
export GRAFANA_GOOGLE_CLIENT_ID GRAFANA_GOOGLE_CLIENT_SECRET
SECRET_TMPL="$GKE_MANIFEST_DIR/grafana-google-oidc-secret.example.yaml"
[[ -f "$SECRET_TMPL" ]] || die "missing manifest: $SECRET_TMPL"
log "==> grafana google-oauth bootstrap"
log " namespace : $NAMESPACE"
log " kubectx : ${KCTX:-<ambient>}"
log "Applying grafana-google-oidc Secret ..."
envsubst '${GRAFANA_GOOGLE_CLIENT_ID} ${GRAFANA_GOOGLE_CLIENT_SECRET}' \
< "$SECRET_TMPL" \
| kubectl "${KCTX_FLAG[@]}" -n "$NAMESPACE" apply -f -
log "==> grafana google-oauth secret applied."
echo ""
echo " Next steps:"
echo " 1. Apply the Helm values override at monitoring/kps-values-gke.yaml:"
echo " helm upgrade --reuse-values kps prometheus-community/kube-prometheus-stack \\"
echo " --namespace $NAMESPACE \\"
echo " -f $REPO_ROOT/monitoring/kps-values-gke.yaml"
echo " 2. Patch knoe-svc-kong-config to add the /grafana route (see"
echo " deploy/opentofu/k3s/manifests/knoe/kong-configmap.yaml for the canonical source)."
echo " 3. Restart Kong: kubectl rollout restart deployment/knoe-svc-kong -n knoe-system"
echo " 4. Browser-test: https://svc.knoe.dev/grafana → Google sign-in → Grafana"
echo ""