콘텐츠로 이동

Kubeflow oauth2-proxy: Dex 로그인과 Bearer token(M2M) 인증을 gateway 한 곳에서 처리하기

Interactive Diagram

새 탭 ↗
항목값
versionv7.15.2 (quay.io/oauth2-proxy/oauth2-proxy)
공식 문서oauth2-proxy 공식 문서
upstream 소스common/oauth2-proxy

oauth2-proxy 는 원래 “웹앱 앞에 두는 OIDC/OAuth2 reverse proxy”다. Kubeflow 에서는 이것을 Istio 의 external authorizer(ExtAuthZ responder)로 붙여서, gateway 로 들어오는 요청의 로그인 여부를 판단하는 역할로 쓴다.

  • ext_authz responder: Istio 의 CUSTOM AuthorizationPolicy 가 요청 허용/거부 판단을 oauth2-proxy 에게 위임한다. oauth2-proxy 가 “이 요청 통과 가능”이라고 답해야 gateway 가 app 으로 보낸다.
  • OIDC client + session 관리자: 로그인 안 된 브라우저 요청을 Dex 로그인으로 redirect 하고, 로그인 완료 후 session cookie 를 발급/검증한다. 통과시킬 때는 Dex id_token 을 Authorization header 로, 사용자 정보를 X-Auth-Request-Email 등으로 upstream 에 전달한다.

Kubeflow gateway 는 기본 거부(global-deny-all) 위에서 동작하고, “로그인 되었는가”의 실제 판단을 oauth2-proxy 가 내린다. 그런데 Kubeflow 를 쓰는 방식은 두 가지다. 사람이 브라우저로 대시보드를 여는 경우와, backend service 가 Kubeflow API(KFP, model registry 등)를 프로그램적으로 호출하는 경우다. 이 배포는 두 경우를 gateway 한 곳에서 서로 다른 방식으로 인증한다. 우리에게 특히 중요한 것은 후자, 즉 API 호출 연계다.

gateway(ingress-gateway)에는 네 가지 정책이 함께 걸려 있고, 이 조합이 요청을 두 경로로 가른다.

  • istio-ingressgateway-oauth2-proxy (action: CUSTOM): Authorization header 가 없는 요청만 oauth2-proxy ext_authz 로 보낸다(대화형 browser 경로).
  • m2m-token-issuer (RequestAuthentication): Kubernetes ServiceAccount issuer 로 발급된 Authorization: Bearer JWT 를 검증한다(M2M 경로).
  • dex-token-issuer (RequestAuthentication): Dex issuer 로 발급된 id_token 을 검증한다(대화형 경로). oauth2-proxy 가 세운 세션이 gateway 에서 principal 로 인정되려면 이 resource 가 반드시 있어야 한다.
  • istio-ingressgateway-require-jwt (action: DENY): 검증된 JWT principal 이 없는 요청을 최종 거부한다. 단, 로그인 자체를 위한 경로는 이 DENY 에서 제외된다.

CUSTOM 정책의 선택 조건은 when 절에 그대로 적혀 있다.

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: ["*"] 는 “authorization header 가 존재하지 않는 요청”을 뜻한다. 즉 Bearer token 을 든 M2M 요청은 ext_authz 자체를 타지 않고 곧장 다음 단계로 넘어간다.

브라우저 사용자는 어떻게 require-jwt 를 통과하는가

섹션 제목: “브라우저 사용자는 어떻게 require-jwt 를 통과하는가”

여기서 오해하기 쉬운 지점이 있다. Istio 에서 requestPrincipals 는 RequestAuthentication 이 검증에 성공한 JWT 에 대해서만 생긴다. oauth2-proxy 가 upstream 요청에 header 를 주입한다고 해서 principal 이 생기지는 않는다. 따라서 브라우저 사용자가 require-jwt 를 통과하려면 둘 중 하나가 필요하다.

첫째, Dex issuer 를 신뢰하는 RequestAuthentication 을 별도로 등록하고, oauth2-proxy 가 세션의 id_token 을 Authorization header 로 upstream 에 전달(--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

둘째, 로그인 자체에 필요한 경로는 애초에 principal 을 가질 수 없으므로 DENY 규칙에서 제외한다. /dex/* 와 /oauth2/* 가 그 대상이다.

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/*"]

이 두 장치가 없으면 로그인 화면 자체가 DENY 에 걸려 redirect loop 가 난다.

Istio 의 인가 평가 순서는 CUSTOM -> DENY -> ALLOW 다. 즉 ext_authz(CUSTOM)가 먼저 돌고, 그 다음 DENY 정책이 적용되며, 마지막으로 ALLOW 정책이 평가된다. 어느 단계에서든 거부되면 즉시 종료된다. 브라우저 첫 요청은 CUSTOM 단계에서 oauth2-proxy 가 302 를 돌려주므로 DENY 까지 가지 않고, 로그인 후 재요청은 Authorization header 를 달고 오므로 CUSTOM 을 건너뛰고 dex-token-issuer 가 세운 principal 로 DENY 를 통과한다.

즉 Authorization header 유무로 경로가 갈린다. header 가 없으면 oauth2-proxy(-> Dex 로그인), 있으면 RequestAuthentication(-> token 검증). 두 경로 모두 마지막엔 “검증된 principal 이 있어야 통과”라는 require-jwt 를 지난다.

backend 가 Kubeflow API 를 호출할 때는 브라우저 redirect 를 못 쓰므로, Kubernetes ServiceAccount token 을 Authorization: Bearer 로 제시한다. 이 배포는 영구 API 용도를 위해 만료 없는 legacy Secret token(platform-api, JWT iss = kubernetes/serviceaccount)을 기본 credential 로 쓰며, bound(projected) token 도 함께 신뢰한다. gateway 는 다음을 구성해 이 token 을 검증한다.

  • m2m-token-issuer RequestAuthentication 이 token 의 issuer 와 서명을 검증한다. 신뢰 issuer 는 세 가지다: https://kubernetes.default.svc.cluster.local, https://kubernetes.default.svc(bound token 용), 그리고 kubernetes/serviceaccount(legacy 영구 token 용). 서명 검증용 공개키(JWKS)는 세 issuer 모두 동일한 cluster-jwks-proxy 를 쓴다(legacy 와 bound 가 같은 SA 서명 키로 서명됨).
  • 검증에 성공하면 token 의 claim 을 header 로 매핑한다: sub -> kubeflow-userid, groups -> kubeflow-groups. 이 header 가 Kubeflow 의 multi-user RBAC 에서 “누가 호출했는가”를 결정한다.
  • Authorization header 가 있으므로 oauth2-proxy(CUSTOM)는 이 요청을 건너뛴다. RequestAuthentication 이 principal 을 세웠으니 require-jwt 도 통과한다.

이 구조 덕분에 CI/automation 이나 다른 backend 가 클러스터 밖에서도 gateway 를 통해 SA token 만으로 Kubeflow API 를 호출할 수 있다. legacy 영구 token 은 만료가 없어 backend 상주 용도에 편하지만, cluster 의 모든 legacy SA token 을 gateway 가 신원으로 인정하게 되므로 실제 접근은 downstream 의 RBAC 으로 통제된다.

overlays/dev/oauth2-proxy/overlays/m2m-dex-and-kind 가 common/oauth2-proxy/overlays/m2m-dex-and-kind 를 참조한다. 이 overlay 는 Dex 기반 browser 로그인과 Kubernetes ServiceAccount token 기반 M2M 인증을 함께 설치한다. 구성은 base(oauth2-proxy Deployment) + 세 component 다.

  • cluster-jwks-proxy: 클러스터 OIDC JWKS(/openid/v1/jwks, /.well-known/openid-configuration)를 인증 없이 노출하는 proxy(docker.io/bitnami/kubectl 기반). Kubernetes API server 의 JWKS endpoint 는 보통 인증을 요구하는데, Istio RequestAuthentication 은 인증 없이 JWKS 를 가져와야 하므로 이 proxy 가 중간에서 열어 준다.
  • istio-external-auth: 대화형 경로 wiring. oauth2-proxy 를 ExtAuthZ provider 로 등록하는 meshConfig 패치와 CUSTOM/require-jwt AuthorizationPolicy 를 설치한다.
  • istio-m2m: M2M 경로 wiring. m2m-token-issuer RequestAuthentication 을 설치한다.

overlay 는 이 RequestAuthentication 의 issuer 와 jwksUri 를 이 클러스터 값으로 patch 한다. issuer 는 https://kubernetes.default.svc(및 .cluster.local)와 legacy 용 kubernetes/serviceaccount 세 가지를 등록하고, jwksUri 는 http://cluster-jwks-proxy.istio-system.svc.cluster.local/openid/v1/jwks 로 지정한다.

namespaceresource이름역할
oauth2-proxyDeployment + Serviceoauth2-proxyext_authz responder + OIDC client
istio-systemRequestAuthenticationm2m-token-issuerBearer JWT(SA token) 검증 + claim -> header 매핑
istio-systemRequestAuthenticationdex-token-issuerDex issuer id_token 검증(대화형 경로의 principal 근거)
istio-systemAuthorizationPolicyistio-ingressgateway-oauth2-proxy (CUSTOM)authorization header 없는 요청만 oauth2-proxy 로 위임
istio-systemAuthorizationPolicyistio-ingressgateway-require-jwt (DENY)검증된 principal 없으면 최종 거부(/dex/*, /oauth2/* 제외)
istio-systemDeployment + Servicecluster-jwks-proxy클러스터 JWKS 를 인증 없이 노출

PlantUML 원본:

PlantUML Diagram

backend 는 audience 가 맞는 Kubernetes ServiceAccount projected token 을 얻어 Authorization: Bearer 로 gateway 에 보낸다. gateway 는 RequestAuthentication 으로 token 을 검증하고 신원 header 를 세운 뒤 대상 API 로 routing 한다. 대상 API 는 그 header 로 RBAC 을 수행한다.

PlantUML 원본:

PlantUML Diagram

Authorization header 가 없는 브라우저 요청은 CUSTOM AuthorizationPolicy 를 통해 oauth2-proxy 로 간다. session cookie 가 없으면 Dex 로그인으로 redirect 되고(이 redirect 경로인 /dex/*, /oauth2/* 는 require-jwt DENY 에서 제외된다), 로그인 후 oauth2-proxy 가 Dex id_token 을 Authorization header 로 붙여 보내면 dex-token-issuer RequestAuthentication 이 그것을 검증해 principal 을 세우고 require-jwt 를 통과한다. 이 경로의 상세 순서는 Istio 문서의 동작 흐름을 참고한다.

serving 요청(kserve.10.x.x.x.nip.io/serving/<ns>/<isvc>/...)은 앞의 gateway 한 곳에서 끝나지 않고 hop 이 하나 더 있다. KServe(serverless)는 InferenceService VirtualService 가 authority 를 예측기 내부 host 로 재작성해 cluster-local-gateway 를 경유하기 때문이다. 그래서 M2M serving 은 세 관문을 모두 통과해야 한다.

  • 1-hop JWT (istio-ingressgateway): m2m-token-issuer 가 token 을 검증(legacy issuer 포함).
  • 2-hop JWT (cluster-local-gateway): cluster-local-gateway-jwt RequestAuthentication 이 재검증한다. 이 것은 app: istio-ingressgateway 가 아닌 app: cluster-local-gateway 를 selector 로 가지므로, m2m-token-issuer 와 별개로 동일한 legacy issuer(kubernetes/serviceaccount)를 별도 등록해야 한다. 빠지면 이 hop 이 Jwt issuer is not configured(401)로 거부한다(realm 이 예측기 내부 host).
  • 예측기 authz: 예측기 sidecar 에는 profile 이 만든 ns-owner-access-istio(ALLOW)가 적용되는데, 이것은 profile 소유자(kubeflow-userid == 소유자 email)만 허용한다. M2M 호출의 kubeflow-userid 는 SA subject 라 그 규칙에 안 걸려 403(RBAC: access denied)이 난다. Istio ALLOW 정책은 OR 로 평가되므로, 소유자 정책을 건드리지 않고 예측기(component: predictor)를 선택해 platform-api SA 를 허용하는 추가 ALLOW 정책(platform-api-serving-access)을 해당 네임스페이스에 둔다.

즉 대시보드 host(1-hop)와 달리 serving host 는 2-hop JWT + 예측기 authz 까지 맞춰야 통한다. 상세 KServe/knative 구조는 해당 component 문서(예정)에서 다룬다.

  • 선행: istio(ExtAuthZ/RequestAuthentication 을 태울 gateway 와 meshConfig), dex(OIDC IdP). cluster-jwks-proxy 는 이 overlay 가 함께 배포한다.
  • 후행 (oauth2-proxy 인증에 의존): gateway 를 통과하는 모든 app 과 API. Dashboard, Notebooks, Pipelines UI 뿐 아니라 backend 가 호출하는 KFP/model registry API 도 이 게이트를 지난다.

Authorization header 유무로 경로가 갈린다. header 없음 -> oauth2-proxy(cookie/Dex) 경로, header 있음 -> RequestAuthentication(Bearer JWT) 경로. 두 경로 모두 최종적으로 검증된 principal 을 가진 요청만 require-jwt 를 통과해 app/API 로 전달되며, 신원은 kubeflow-userid header 로 downstream 에 넘어간다.

  • 클러스터 내부에서 API service 를 직접(ClusterIP) 호출하면 gateway 를 우회하므로 이 인증을 타지 않고, mesh mTLS + global-deny-all AuthorizationPolicy + 대상 API 자체의 신원 검사가 적용된다.
  • M2M 인증 실패의 흔한 원인은 token 의 issuer/audience 가 RequestAuthentication 이 신뢰하는 값과 불일치하는 것이다.
  • overlay: overlays/dev/oauth2-proxy/overlays/m2m-dex-and-kind
  • 관련 문서: