Oauth

From gilesknap/tpi-k3s-ansible · View interactive version

Oauth is an AI agent skill from the GitHub repository gilesknap/tpi-k3s-ansible. OAuth2-proxy authentication setup, configuration, and troubleshooting for the K3s cluster.

How to use this skill

Skill file

OAuth / Authentication

Architecture

Alertmanager, the Minecraft dashboard and scrap (PWA)

oauth2-proxy gateway → backend services

Configuration

MCP servers need a different auth model — do NOT try to reuse oauth2-proxy

Non-browser MCP clients (claude.ai's connector, Claude Code

--transport http) cannot authenticate through this cluster's

standard pattern. Two reasons:

  1. Cloudflare Access challenges with GitHub or one-time PIN —

solvable only in a browser.

  1. oauth2-proxy is cookie-based forward-auth — it's not an OAuth

2.1 authorization server, so MCP clients can't run a token flow

against it.

The established pattern for MCP servers in this cluster is:

(RFCs 8414 + 9728 + 7591 DCR + 7636 PKCE) and issues its own JWTs

with GitHub as the upstream IdP.

(non_identity) policy, as scrap's /mcp does in cloudflare/access.tf,

so non-browser clients aren't shown the Access login. See /cloudflare

skill.

WWW-Authenticate: Bearer resource_metadata="<host>/.well-known/oauth-protected-resource"

— without this header, MCP clients silently fail to discover the

auth server and just give up.

in-memory; clients re-register transparently on pod restart.

oauth.py (provider-agnostic), recoverable from git history with

git show de07b3c:open-brain-mcp/oauth.py. When standing up the next

MCP service, port the structure rather than reinventing.

Gotchas

nothing for Dex. Override the argo-cd client in dex.config with

trustedPeers instead. See additions/argocd/README.md.

/api/dex with allowedAudiences was added on purpose in PR #297 (see

the DEX duplicate client gotcha below). Do not remove it.

are read at startup. After rotating with just seal-argocd-dex <subcommand>,

restart affected pods.

template must include app.kubernetes.io/part-of: argocd label. Without

it, ArgoCD's $secret:key resolution in dex.config silently fails,

passing literal key names as OAuth client IDs (→ GitHub 404).

dex.config uses $argocd-dex-secret:key for each static client.

Missing keys silently resolve to empty, causing "Failed to get token

from provider" on login. The rebuild path (scripts/seal-from-json)

seals all clients (grafana, argocd-monitor) along with

the matching service-side secrets in one pass.

cookie-domain=.gkcluster.org, so its _oauth2_proxy cookie reaches all

subdomains. Any service with its own oauth2-proxy sidecar (e.g.

argocd-monitor) must use --cookie-name=<unique> to avoid validating

the shared proxy's cookie with its own secret (→ infinite login loop).

to email_domains = ["*"], which allows any GitHub user through and

silently overrides authenticatedEmailsFile. The fix is

config.configFile with email_domains = []. This bug was found and

fixed in PR #279 — do not remove the configFile override.

bytes for the AES cipher. base64.b64encode(token_bytes(32)) produces

44 chars and crashes oauth2-proxy. Use secrets.token_hex(16) (32 hex

chars = 32 bytes). This bug has regressed before — do not change the

generation in scripts/seal-argocd-dex. See /sealed-secrets skill.

argo-cd DEX client (without trustedPeers). Our dex.config also

declares one (with trustedPeers: [argocd-monitor]). DEX v2.45+ stores

the first and drops the duplicate, so trustedPeers never takes

effect. Fixed in PR #297 by adding oidc.config with

allowedAudiences: [argo-cd, argocd-monitor], which lets

argocd-monitor authenticate as itself. The duplicate argo-cd client

in dex.config is harmless but redundant — kept for clarity.

via envFrom or secretKeyRef cache values at startup. After

--tags cluster or just seal-argocd-dex, run just restart-dex and

kubectl rollout restart sts grafana-prometheus -n monitoring.

Without this, Dex reports "invalid client_secret" even though the

Secret objects match. See /sealed-secrets for the full namespace list.

backendRef is the internal Service (oauth2-proxy in oauth2-proxy).

The external domain resolves via Cloudflare to IPv6, which is

unreachable from the cluster; an unreachable auth service makes Envoy

answer 403 on every gated route (it fails closed).

404. OIDC clients that don't follow redirects

need the full discovery URL: .well-known/openid-configuration.

allow_sign_up under [auth.generic_oauth] is not sufficient alone.

Also set [auth].disable_signup_form: true to block manual signup.

Key Files

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.