Add OAuth to a workload
This guide shows how to put HTTP endpoints of your pod behind OIDC login (Keycloak, Auth0, Google, etc.) using the oauth2-proxy sidecar pattern.
Multicloud does not ship a shared auth proxy. Each workload owns its own OIDC client, its own secrets, and its own proxy container. That keeps the platform simple and lets every app pick its own realm, client-id, and auth policy independently.
How it works
Browser
│ HTTPS
▼
Traefik (K3S worker, TLS termination, host/path routing)
│
│ Two Ingress objects share one host:
│ protected → Service port "auth" (4180, oauth2-proxy)
│ public → Service port "http" (80, app)
│
▼ ▼
Pod (nginx + oauth2-proxy sidecar)
┌──────────────────────────┐
│ oauth2-proxy :4180 │
│ • validates session │
│ • redirects to IdP │
│ • /oauth2/* endpoints │
│ • upstream=127.0.0.1:80│
└──────────┬───────────────┘
│ localhost
▼
┌──────────────────────────┐
│ nginx :80 │
└──────────────────────────┘
- Protected traffic goes Traefik → Service(auth:4180) → oauth2-proxy
→ localhost → app. The proxy owns
/oauth2/*on the app's hostname (callback, sign_in, sign_out) and reverse-proxies everything else. - Public traffic (e.g.
/healthz,/metrics, internal service calls) points at Service(http:80) and bypasses the sidecar entirely — zero auth overhead. - Different workloads can use completely different issuers, realms, and clients — nothing is shared between them.
Step 1 — Create an OIDC client in Keycloak
The Multicloud realm is at https://auth.multicloud.io/realms/multicloud
(issuer URL). Keycloak 26+ admin console flow below. For scripting, skip to
the kcadm CLI section.
Admin console flow
1. Create the client
Log into https://keycloak.multicloud.io/admin/ → select realm
multicloud → Clients → Create client.
The admin console lives on
keycloak.multicloud.io, behind mTLS — you need a client certificate signed by the platform CA.auth.multicloud.iois the public OIDC hostname, and/adminreturns 403 there as of 2026-07-31 (a platform rule). It used to serve the same Admin API with no client certificate at all, so an older bookmark pointing there will now fail — that is the rule working, not an outage.
- General settings
- Client type: OpenID Connect
- Client ID:
nginx-ingress-test(pick something identifying your workload; this value goes into the Secret'sclient-idkey and into theaudclaim in tokens) - Name:
nginx ingress test(display only)
2. Capability config (next page)
- Client authentication: On (makes it a confidential client)
- Authorization: Off (we don't need fine-grained Keycloak policies; oauth2-proxy does its own enforcement)
- Authentication flow:
- ✅ Standard flow (authorization code — used for browser login)
- ❌ Direct access grants (password grant — disable, oauth2-proxy never uses it)
- ❌ Implicit flow, Service accounts roles, OAuth 2.0 Device Authorization Grant, OIDC CIBA Grant
3. Login settings (next page)
- Root URL:
https://nginx-test.demo.multicloud.io - Home URL:
https://nginx-test.demo.multicloud.io - Valid redirect URIs:
https://nginx-test.demo.multicloud.io/oauth2/callback(must match--redirect-urlexactly — no wildcard, no trailing slash) - Valid post logout redirect URIs:
https://nginx-test.demo.multicloud.io/* - Web origins:
https://nginx-test.demo.multicloud.io(CORS) - Click Save.
4. Grab the client secret
Once saved, go to the client's Credentials tab → copy
Client Secret. This is the value for the Secret's client-secret
key.
5. Add the audience mapper (critical — oauth2-proxy will loop-redirect without this)
Keycloak's default access token has aud populated from the resource_access
claim, which by default does not include the client's own name. oauth2-proxy
rejects tokens whose aud doesn't contain the configured --client-id. Fix
it with a dedicated mapper.
Client → Client scopes tab → click the dedicated scope
nginx-ingress-test-dedicated → Add mapper → By configuration →
Audience:
- Name:
audience - Included Client Audience:
nginx-ingress-test(same as client id) - Add to ID token: On
- Add to access token: On
- Add to lightweight access token: Off
- Add to token introspection: On
Click Save.
6. (Optional) Add a groups mapper
If you plan to use --allowed-group=/team/x later, add a group membership
mapper on the same dedicated scope:
Client scopes → nginx-ingress-test-dedicated → Add mapper →
By configuration → Group Membership:
- Name:
groups - Token Claim Name:
groups - Full group path: On (emits
/team/xinstead of justx) - Add to ID token: On
- Add to access token: On
- Add to userinfo: On
Click Save.
Keycloak CLI alternative
Read the realm and issuer values from your Keycloak admin console.
For creating the client itself, use kcadm.sh shipped with Keycloak:
# 1. Authenticate (one-time per session; credentials cached in ~/.keycloak/)
kcadm.sh config credentials \
--server https://auth.multicloud.io \
--realm master \
--user admin \
--password "$KEYCLOAK_ADMIN_PASSWORD"
# 2. Create the client (returns the generated UUID on stdout)
CID=$(kcadm.sh create clients -r multicloud -f - <<EOF | awk -F"'" '/Created/ { print $2 }'
{
"clientId": "nginx-ingress-test",
"protocol": "openid-connect",
"publicClient": false,
"standardFlowEnabled": true,
"directAccessGrantsEnabled": false,
"serviceAccountsEnabled": false,
"implicitFlowEnabled": false,
"rootUrl": "https://nginx-test.demo.multicloud.io",
"redirectUris": ["https://nginx-test.demo.multicloud.io/oauth2/callback"],
"webOrigins": ["https://nginx-test.demo.multicloud.io"],
"attributes": {
"post.logout.redirect.uris": "https://nginx-test.demo.multicloud.io/*"
}
}
EOF
)
# 3. Retrieve the generated client secret
CLIENT_SECRET=$(kcadm.sh get "clients/$CID/client-secret" -r multicloud | jq -r .value)
echo "client-secret: $CLIENT_SECRET"
# 4. Find the dedicated client scope and add the audience mapper
SCOPE_ID=$(kcadm.sh get clients/$CID/default-client-scopes -r multicloud \
| jq -r '.[] | select(.name == "nginx-ingress-test-dedicated") | .id')
kcadm.sh create "client-scopes/$SCOPE_ID/protocol-mappers/models" \
-r multicloud -f - <<EOF
{
"name": "audience",
"protocol": "openid-connect",
"protocolMapper": "oidc-audience-mapper",
"config": {
"included.client.audience": "nginx-ingress-test",
"id.token.claim": "true",
"access.token.claim": "true",
"introspection.token.claim":"true"
}
}
EOF
# 5. (Optional) groups mapper
kcadm.sh create "client-scopes/$SCOPE_ID/protocol-mappers/models" \
-r multicloud -f - <<EOF
{
"name": "groups",
"protocol": "openid-connect",
"protocolMapper": "oidc-group-membership-mapper",
"config": {
"claim.name": "groups",
"full.path": "true",
"id.token.claim": "true",
"access.token.claim": "true",
"userinfo.token.claim": "true"
}
}
EOF
echo "Client ready. Issuer URL: https://auth.multicloud.io/realms/multicloud"
echo "Client ID: nginx-ingress-test"
echo "Client Secret: $CLIENT_SECRET"
Verify the client before deploying
Fetch the OIDC discovery document — confirms the issuer URL is reachable and returns the expected endpoints:
curl -s https://auth.multicloud.io/realms/multicloud/.well-known/openid-configuration \
| jq '{issuer, authorization_endpoint, token_endpoint, jwks_uri}'
Exchange credentials for a token (requires Direct Access Grants, which we
disabled above — but you can temporarily enable it for a single test, then
turn it off again) and decode to confirm the aud claim contains your
client id:
TOKEN=$(curl -s -X POST \
https://auth.multicloud.io/realms/multicloud/protocol/openid-connect/token \
-d grant_type=password \
-d client_id=nginx-ingress-test \
-d client_secret="$CLIENT_SECRET" \
-d username=yaron \
-d password="$USER_PASSWORD" \
-d scope=openid \
| jq -r .access_token)
# Decode payload (JWT = header.payload.signature)
echo "$TOKEN" | cut -d. -f2 | base64 -d 2>/dev/null | jq '{aud, preferred_username, groups}'
# Expected: "aud" contains "nginx-ingress-test"
If aud is missing the client id, revisit the audience mapper (Step 5).
Non-Keycloak IdPs
For Auth0, Okta, Google, Azure AD, GitHub, etc., the same three values are needed: issuer URL, client id, client secret. In the sidecar args, set:
- --provider=oidc
- --oidc-issuer-url=https://your-issuer/
instead of --provider=keycloak-oidc. oauth2-proxy's built-in providers for
google, github, azure, etc. are also options — see
the oauth2-proxy provider configuration documentation.
Step 2 — Create the workload Secret
apiVersion: v1
kind: Secret
metadata:
name: nginx-oauth2
namespace: multicloud-<cluster>
type: Opaque
stringData:
client-id: nginx-ingress-test
client-secret: <from-keycloak>
# 32 random bytes, url-safe base64:
# openssl rand -base64 32 | tr '+/' '-_' | tr -d '='
cookie-secret: <generated>
The cookie secret signs/encrypts the oauth2-proxy session cookie — generate a fresh one per workload; don't share it across apps.
Step 3 — Add the sidecar to your Deployment
containers:
- name: nginx # your app container
image: nginx:1.27-alpine
ports:
- containerPort: 80
name: http
- name: oauth2-proxy
image: quay.io/oauth2-proxy/oauth2-proxy:v7.15.2
args:
- --provider=keycloak-oidc
- --oidc-issuer-url=https://auth.multicloud.io/realms/multicloud
- --redirect-url=https://nginx-test.demo.multicloud.io/oauth2/callback
- --cookie-domain=nginx-test.demo.multicloud.io
- --whitelist-domain=nginx-test.demo.multicloud.io
- --upstream=http://127.0.0.1:80
- --http-address=0.0.0.0:4180
- --email-domain=*
- --reverse-proxy=true
- --code-challenge-method=S256 # PKCE
- --skip-provider-button=true # go straight to IdP
- --set-xauthrequest=true # adds X-Auth-Request-* to upstream
- --pass-authorization-header=true # forward the raw JWT to your app
- --cookie-secure=true
- --cookie-samesite=lax
env:
- { name: OAUTH2_PROXY_CLIENT_ID, valueFrom: { secretKeyRef: { name: nginx-oauth2, key: client-id }}}
- { name: OAUTH2_PROXY_CLIENT_SECRET, valueFrom: { secretKeyRef: { name: nginx-oauth2, key: client-secret }}}
- { name: OAUTH2_PROXY_COOKIE_SECRET, valueFrom: { secretKeyRef: { name: nginx-oauth2, key: cookie-secret }}}
ports:
- containerPort: 4180
name: auth
readinessProbe: { httpGet: { path: /ping, port: 4180 } }
livenessProbe: { httpGet: { path: /ping, port: 4180 } }
resources:
requests: { cpu: 50m, memory: 64Mi }
limits: { memory: 128Mi }
Key flags to adjust per workload:
| Flag | Set to |
|---|---|
--provider |
keycloak-oidc, oidc, google, github, … |
--oidc-issuer-url |
Your realm / issuer URL |
--redirect-url |
https://<your-app-host>/oauth2/callback |
--cookie-domain |
Your app host (or a parent domain for SSO across apps) |
--whitelist-domain |
Same as cookie-domain |
--upstream |
http://127.0.0.1:<your-app-port> |
--email-domain |
* or restrict (e.g. multicloud.io) |
--allowed-group |
Optional: require Keycloak group membership |
--allowed-role |
Optional: require realm/client role |
Step 4 — Expose two Service ports
apiVersion: v1
kind: Service
metadata:
name: nginx-ingress-test
namespace: multicloud-<cluster>
spec:
selector: { app: nginx-ingress-test }
ports:
- { name: auth, port: 4180, targetPort: 4180 } # through the sidecar
- { name: http, port: 80, targetPort: 80 } # direct to the app
Only auth needs to exist for minimal setups; http is only needed
if you have public paths that should bypass auth.
Step 5 — Split the Ingress
Protected (goes through the sidecar):
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress-test-protected
namespace: multicloud-<cluster>
spec:
ingressClassName: traefik
tls:
- { hosts: [nginx-test.demo.multicloud.io], secretName: multicloud-tls }
rules:
- host: nginx-test.demo.multicloud.io
http:
paths:
- path: /
pathType: Prefix
backend: { service: { name: nginx-ingress-test, port: { name: auth } } }
Public (skips the sidecar — same host, different port):
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress-test-public
namespace: multicloud-<cluster>
spec:
ingressClassName: traefik
tls:
- { hosts: [nginx-test.demo.multicloud.io], secretName: multicloud-tls }
rules:
- host: nginx-test.demo.multicloud.io
http:
paths:
- path: /healthz
pathType: Exact
backend: { service: { name: nginx-ingress-test, port: { name: http } } }
Traefik merges same-host Ingresses and picks the more-specific path per
request: /healthz goes to the public Ingress (no auth); everything
else falls through to the protected one.
If you have no public paths, skip the second Ingress and the
httpService port.
Accessing user identity in your app
When a request reaches your app through the sidecar, oauth2-proxy
injects these headers (because of --set-xauthrequest=true):
| Header | Value |
|---|---|
X-Auth-Request-User |
Preferred username |
X-Auth-Request-Email |
|
X-Auth-Request-Preferred-Username |
Same as User |
X-Auth-Request-Groups |
Comma-separated groups (if mapper configured) |
Authorization: Bearer <jwt> |
Raw Keycloak access token (if --pass-authorization-header=true) |
Your app should trust these headers only on the localhost interface — because the sidecar sets them, and the app binds only to 127.0.0.1 from the sidecar. Never trust them on a public-facing Service port.
Per-path auth patterns
Same auth, mixed public/private
Use two Ingresses as above — protected and public each targets a different Service port.
Different auth policies per path
Run two oauth2-proxy sidecars in the same pod (different container
names, different ports, different Secrets) and add a third Ingress that
targets the second proxy's port. Example: /admin behind a stricter
client that requires --allowed-role=admin, / behind the permissive
default.
Skip specific paths inside the proxy
If splitting Ingresses is overkill, --skip-auth-routes='^/healthz$'
lets the sidecar itself pass those requests through without a login.
The platform doesn't care either way — this is a flag on your sidecar.
Troubleshooting
- Infinite redirect loop after login —
audclaim missing the client id. Add the audience mapper on the Keycloak client. - 302 to
/oauth2/sign_inbut browser shows blank page — check that--redirect-urlexactly matches the Keycloak "Valid redirect URIs" setting. bad request: 400 — state parameter mismatch— cookie domain mismatch.--cookie-domainmust cover the host the user is browsing.- Public Ingress still prompts for login — path priority. Make the
public path more specific (
pathType: Exact) and verify withkubectl describe ingress -n multicloud-<cluster>. - "Permission denied" on /oauth2/callback after TLS works —
--cookie-secure=truerequires HTTPS; use--cookie-secure=falseonly when testing over HTTP.
Why a sidecar (not a platform proxy)
Earlier designs put a single oauth2-proxy per namespace, with a Traefik
Middleware CR that Ingresses annotated into. That approach was
dropped because:
- Different workloads need different realms / clients. A single proxy is hard-wired to one issuer, one client id, one cookie secret. Sharing it forces every workload onto the same auth policy.
- Auth isn't a platform concern. The platform's job is to provision compute, networking, DNS, and TLS. Which IdP protects which path is an application decision, and it should live in the application's manifest.
- The sidecar model is self-contained. A workload's OAuth config travels with the workload. No coordination with cluster bootstrap, no shared Secret, no shared blast radius.
- Scales by reality, not by policy. One app with no auth needs zero sidecars. One app with two different auth policies runs two sidecars in its own pod. The platform doesn't care.
The trade-off is ~50m CPU / 64Mi RAM per pod that opts in, which is cheap compared to the coordination cost of a shared proxy.