Skip to content

Kubeflow oauth2-proxy: Dex Sign-in and Bearer Token (M2M) Authentication at the Gateway

Interactive Diagram

새 탭 ↗
ItemValue
Versionv7.15.2 (quay.io/oauth2-proxy/oauth2-proxy)
Official docsoauth2-proxy documentation
Upstream sourcecommon/oauth2-proxy

oauth2-proxy is originally an “OIDC/OAuth2 reverse proxy that sits in front of a web app”. In Kubeflow it is attached to Istio as an external authorizer (ExtAuthZ responder) and is used to decide whether a request entering the gateway comes from a signed-in user.

That means the gateway asks oauth2-proxy whether a request is authenticated. oauth2-proxy does not own the network path end to end, but it does own the decision about whether an unauthenticated browser request should be redirected to Dex or whether an authenticated request may proceed.

  • ext_authz responder: Istio’s CUSTOM AuthorizationPolicy delegates the allow/deny decision for a request to oauth2-proxy. The gateway sends the request to the app only after oauth2-proxy answers that the request may pass.
  • OIDC client and session manager: it redirects browser requests that are not signed in to the Dex login, and after login completes it issues and validates the session cookie. When it lets a request through, it forwards the Dex id_token upstream as the Authorization header and the user information as X-Auth-Request-Email and similar headers.

That split is important. Dex is the identity provider. Istio is the gateway enforcement point. oauth2-proxy sits between them as the component that turns browser login into an authorization decision the gateway can consume.

The Kubeflow gateway runs on top of a default deny (global-deny-all), and oauth2-proxy makes the actual decision on “is this request signed in”. Kubeflow is used in two ways, however: a person opens the dashboard in a browser, or a backend service calls Kubeflow APIs (KFP, model registry, and so on) programmatically. This deployment authenticates the two cases differently at a single place, the gateway. The case that matters most to us is the latter, API call integration.

  • Human users arrive in a browser and need an interactive login flow.
  • Backend services, CI jobs, and other automation need non-interactive API access.

For browser traffic, oauth2-proxy decides whether a request already has a valid session cookie. If not, it sends the user to Dex. If yes, it lets the request through and injects identity information for downstream applications.

Four policies are attached together to the gateway (ingress-gateway), and this combination splits requests into two paths.

  • istio-ingressgateway-oauth2-proxy (action: CUSTOM): sends only requests without an Authorization header to the oauth2-proxy ext_authz (interactive browser path).
  • m2m-token-issuer (RequestAuthentication): validates Authorization: Bearer JWTs issued by the Kubernetes ServiceAccount issuer (M2M path).
  • dex-token-issuer (RequestAuthentication): validates id_tokens issued by the Dex issuer (interactive path). This resource must exist for a session established by oauth2-proxy to be recognized as a principal at the gateway.
  • istio-ingressgateway-require-jwt (action: DENY): finally denies requests without a validated JWT principal. The paths needed to perform login itself are excluded from this DENY.

The selection condition of the CUSTOM policy is written literally in its when clause.

apiVersion: security.istio.io/v1
kind: AuthorizationPolicy
metadata:
name: istio-ingressgateway-oauth2-proxy
namespace: istio-system
spec:
selector:
matchLabels:
app: istio-ingressgateway
action: CUSTOM
provider:
name: oauth2-proxy
rules:
- when:
- key: request.headers[authorization]
notValues: ["*"]

notValues: ["*"] means “requests where the authorization header is absent”. A M2M request carrying a bearer token therefore never goes through ext_authz at all and moves straight to the next stage.

This is the part that is easy to misunderstand. In Istio, requestPrincipals exists only for a JWT that a RequestAuthentication has successfully validated. Headers injected by oauth2-proxy into the upstream request do not create a principal. So a browser user needs one of two things to pass require-jwt.

First, register a separate RequestAuthentication that trusts the Dex issuer, and have oauth2-proxy forward the session’s id_token upstream as the Authorization header (--pass-authorization-header=true).

apiVersion: security.istio.io/v1
kind: RequestAuthentication
metadata:
name: dex-token-issuer
namespace: istio-system
spec:
selector:
matchLabels:
app: istio-ingressgateway
jwtRules:
- issuer: "http://dex.auth.svc.cluster.local:5556/dex"
jwksUri: "http://dex.auth.svc.cluster.local:5556/dex/keys"
forwardOriginalToken: true

Second, the paths needed for login itself cannot carry a principal in the first place, so they are excluded from the DENY rule. /dex/* and /oauth2/* are those paths.

apiVersion: security.istio.io/v1
kind: AuthorizationPolicy
metadata:
name: istio-ingressgateway-require-jwt
namespace: istio-system
spec:
selector:
matchLabels:
app: istio-ingressgateway
action: DENY
rules:
- from:
- source:
notRequestPrincipals: ["*"]
to:
- operation:
notPaths: ["/dex/*", "/oauth2/*"]

Without these two pieces, the login screen itself is caught by the DENY and the browser ends up in a redirect loop.

Istio evaluates authorization in the order CUSTOM, DENY, ALLOW. The ext_authz (CUSTOM) runs first, DENY policies are applied next, and ALLOW policies are evaluated last. A rejection at any stage terminates evaluation immediately. The first browser request is answered with a 302 by oauth2-proxy during the CUSTOM stage, so it never reaches DENY. The request after login carries an Authorization header, skips CUSTOM, and passes DENY using the principal established by dex-token-issuer.

So the path splits on whether an Authorization header is present. Without the header, the request goes to oauth2-proxy (then the Dex login). With it, the request goes to RequestAuthentication (token validation). Both paths finally pass through require-jwt, which only lets a request through when it has a validated principal.

When a backend calls a Kubeflow API it cannot use a browser redirect, so it presents a Kubernetes ServiceAccount token as Authorization: Bearer. For permanent API use, this deployment uses a non-expiring legacy Secret token (platform-api, JWT iss = kubernetes/serviceaccount) as the default credential, and it also trusts bound (projected) tokens. The gateway is configured as follows to validate this token.

  • The m2m-token-issuer RequestAuthentication validates the token’s issuer and signature. There are three trusted issuers: https://kubernetes.default.svc.cluster.local, https://kubernetes.default.svc (for bound tokens), and kubernetes/serviceaccount (for legacy permanent tokens). All three issuers use the same cluster-jwks-proxy for the public keys (JWKS) used in signature validation (legacy and bound tokens are signed with the same SA signing key).
  • On successful validation, token claims are mapped to headers: sub to kubeflow-userid, groups to kubeflow-groups. These headers determine “who made the call” in Kubeflow’s multi-user RBAC.
  • Because an Authorization header is present, oauth2-proxy (CUSTOM) skips this request. RequestAuthentication has established a principal, so the request also passes require-jwt.

Thanks to this structure, CI/automation and other backends can call Kubeflow APIs through the gateway with only an SA token, even from outside the cluster. The legacy permanent token does not expire, which is convenient for resident backends, but the gateway then accepts every legacy SA token in the cluster as an identity, so actual access is controlled by downstream RBAC.

The repository treats backend API access as a real use case, not as an afterthought. For that reason, the machine-to-machine path is described alongside browser login rather than hidden in a separate appendix.

overlays/dev/oauth2-proxy/overlays/m2m-dex-and-kind references common/oauth2-proxy/overlays/m2m-dex-and-kind. This overlay installs Dex-based browser login and Kubernetes ServiceAccount token-based M2M authentication together. It consists of a base (the oauth2-proxy Deployment) plus three components.

  • cluster-jwks-proxy: a proxy (based on docker.io/bitnami/kubectl) that exposes the cluster OIDC JWKS (/openid/v1/jwks, /.well-known/openid-configuration) without authentication. The Kubernetes API server’s JWKS endpoint usually requires authentication, but Istio RequestAuthentication has to fetch JWKS without authentication, so this proxy opens it up in between.
  • istio-external-auth: interactive path wiring. It installs the meshConfig patch that registers oauth2-proxy as an ExtAuthZ provider and the CUSTOM/require-jwt AuthorizationPolicies.
  • istio-m2m: M2M path wiring. It installs the m2m-token-issuer RequestAuthentication.

The overlay patches the issuer and jwksUri of this RequestAuthentication to this cluster’s values. It registers three issuers, https://kubernetes.default.svc (and .cluster.local) and kubernetes/serviceaccount for legacy tokens, and sets jwksUri to http://cluster-jwks-proxy.istio-system.svc.cluster.local/openid/v1/jwks.

oauth2-proxy itself runs in the oauth2-proxy namespace as the external authorizer and OIDC client.

NamespaceResourceNamePurpose
oauth2-proxyDeployment + Serviceoauth2-proxyext_authz responder + OIDC client
istio-systemRequestAuthenticationm2m-token-issuerValidates Bearer JWTs (SA tokens) and maps claims to headers
istio-systemRequestAuthenticationdex-token-issuerValidates Dex issuer id_tokens (basis for the interactive path’s principal)
istio-systemAuthorizationPolicyistio-ingressgateway-oauth2-proxy (CUSTOM)Delegates only requests without an authorization header to oauth2-proxy
istio-systemAuthorizationPolicyistio-ingressgateway-require-jwt (DENY)Finally rejects requests without a validated principal (except /dex/*, /oauth2/*)
istio-systemDeployment + Servicecluster-jwks-proxyExposes the cluster JWKS without authentication

PlantUML source:

PlantUML Diagram

The backend obtains a Kubernetes ServiceAccount projected token with the correct audience and sends it to the gateway as Authorization: Bearer. The gateway validates the token with RequestAuthentication, sets the identity headers, and routes the request to the target API. The target API performs RBAC using those headers.

PlantUML source:

PlantUML Diagram

A browser request without an Authorization header goes to oauth2-proxy through the CUSTOM AuthorizationPolicy. If there is no session cookie, the user is redirected to the Dex login (these redirect paths, /dex/* and /oauth2/*, are excluded from the require-jwt DENY). After login, oauth2-proxy attaches the Dex id_token as the Authorization header, the dex-token-issuer RequestAuthentication validates it and establishes a principal, and the request passes require-jwt. For the detailed sequence of this path, see the runtime flow in the Istio article.

For interactive use, the flow is:

  1. a browser hits the Kubeflow gateway without an Authorization header;
  2. the CUSTOM authorization policy sends the auth decision to oauth2-proxy;
  3. oauth2-proxy checks for a valid session cookie;
  4. if no session exists, the user is redirected to Dex;
  5. after successful login, oauth2-proxy establishes the session and allows the request;
  6. on subsequent requests oauth2-proxy forwards the Dex id_token as an Authorization header, dex-token-issuer validates it, and the resulting principal clears the require-jwt DENY;
  7. the gateway forwards the request to the target application.

This is why dashboard access, notebook UIs, and other interactive apps can share one gateway and one sign-in flow.

A serving request (kserve.10.x.x.x.nip.io/serving/<ns>/<isvc>/...) does not end at the gateway described above; there is one more hop. In KServe (serverless), the InferenceService VirtualService rewrites the authority to the predictor’s internal host and the request goes through cluster-local-gateway. M2M serving therefore has to pass all three checks.

  • 1-hop JWT (istio-ingressgateway): m2m-token-issuer validates the token (including the legacy issuer).
  • 2-hop JWT (cluster-local-gateway): the cluster-local-gateway-jwt RequestAuthentication validates it again. Its selector is app: cluster-local-gateway, not app: istio-ingressgateway, so the same legacy issuer (kubernetes/serviceaccount) has to be registered separately from m2m-token-issuer. If it is missing, this hop rejects the request with Jwt issuer is not configured (401) (the realm is the predictor’s internal host).
  • Predictor authz: the predictor sidecar has the ns-owner-access-istio (ALLOW) policy created by the profile, which allows only the profile owner (kubeflow-userid == owner email). The kubeflow-userid of an M2M call is the SA subject, so it does not match that rule and the call fails with 403 (RBAC: access denied). Istio ALLOW policies are evaluated with OR, so without touching the owner policy, an additional ALLOW policy (platform-api-serving-access) that selects the predictor (component: predictor) and allows the platform-api SA is placed in that namespace.

So unlike the dashboard host (1-hop), the serving host only works once the 2-hop JWT and predictor authz are also in place. The detailed KServe/knative structure will be covered in the corresponding component article (planned).

This repository adds that serving authorization policy later in the user-profile overlay, so that the platform-api ServiceAccount can reach predictor Pods even though its identity is not the same as the profile owner’s email identity.

  • Prerequisites: istio (the gateway and meshConfig that carry ExtAuthZ/RequestAuthentication) and dex (the OIDC IdP). cluster-jwks-proxy is deployed together by this overlay.
  • Dependents (rely on oauth2-proxy authentication): every app and API that passes through the gateway. Not only Dashboard, Notebooks, and the Pipelines UI, but also the KFP/model registry APIs that backends call pass through this gate.

The path splits on whether an Authorization header is present. No header means the oauth2-proxy (cookie/Dex) path; a header means the RequestAuthentication (Bearer JWT) path. On both paths, only requests that finally carry a validated principal pass require-jwt and reach the app/API, and the identity is passed downstream in the kubeflow-userid header.

oauth2-proxy only handles one branch of the gateway auth tree, but it is the branch that turns interactive Kubeflow login into a working user experience.

  • Calling an API service directly inside the cluster (ClusterIP) bypasses the gateway, so this authentication does not apply; instead mesh mTLS, the global-deny-all AuthorizationPolicy, and the target API’s own identity check apply.
  • A common cause of M2M authentication failure is a token issuer/audience that does not match the values the RequestAuthentication trusts.
  • overlay: overlays/dev/oauth2-proxy/overlays/m2m-dex-and-kind
  • Related articles:

oauth2-proxy is the gateway’s authentication brain for browser traffic, not merely an accessory login page. It also lives alongside the machine-token path, which is essential for real platform automation. That combination is why it matters so much in this Kubeflow setup.