Kubernetes nginx入口+ oauth2外部身份验证超时

时间:2019-05-22 12:13:56

标签: kubernetes oauth-2.0 azure-active-directory azure-aks nginx-ingress

我试图使用Azure AD作为外部身份验证提供程序,使用oauth2_proxy保护服务的状态页。当前,如果我浏览到应用程序的公共URL(https://sub.domain.com/service/hangfire),则会收到504网关超时,它应指导我进行身份验证。

我主要遵循该指南作为参考:https://msazure.club/protect-kubernetes-webapps-with-azure-active-directory-aad-authentication/

如果我禁用指导身份验证的注释,则可以毫无问题地进入公共状态页面。如果我浏览到https://sub.domain.com/oauth2,则会得到提示,要求我对提供者进行身份验证。我不确定问题出在哪里,但无法找到与此在线,stackoverflow或其他类似的情况。

在这种情况下,所有内容(oauth部署,服务和入口规则)都位于“ dev”命名空间中,但实际的入口部署除外,后者位于其自己的命名空间中。我不认为这会有所作为,但是SSL终止是由群集外部的网关处理的。

oauth2部署:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: oauth2-proxy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: oauth2-proxy
  template:
    metadata:
      labels:
        app: oauth2-proxy
    spec:
      containers:
      - name: oauth2-proxy
        image: quay.io/pusher/oauth2_proxy:v3.2.0
        imagePullPolicy: IfNotPresent
        args:
        - --provider=azure
        - --email-domain=domain.com
        - --upstream=http://servicename
        - --http-address=0.0.0.0:4180
        - --azure-tenant=id
        - --client-id=id
        - --client-secret=number
        env:
         - name: OAUTH2_PROXY_COOKIE_SECRET
           value: secret
        ports:
         - containerPort: 4180
           protocol : TCP
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: oauth2-proxy
  name: oauth2-proxy
spec:
  ports:
  - name: http
    port: 4180
    protocol: TCP
    targetPort: 4180
  selector:
    app: oauth2-proxy 

进入规则:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: service-ingress1
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/auth-url: https://sub.domain.com/oauth2/auth"
    nginx.ingress.kubernetes.io/auth-signin: https://sub.domain.com/oauth2/start?rd=$https://sub.domain.com/service/hangfire"
spec:
  rules:
  - host: sub.domain.com       
    http:
      paths:
      - path: /service/hangfire
        backend:
          serviceName: service
          servicePort: 80                    
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: service-oauth2-proxy
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
  - host: sub.domain.com      
    http:
      paths:
      - path: /oauth2
        backend:
          serviceName: oauth2-proxy
          servicePort: 4180           

浏览到url时出现504错误,但是在入口窗格中没有看到任何错误。

3 个答案:

答案 0 :(得分:1)

这是我使用Azure AD的oAuth代理所做的事情:

  annotations:
    kubernetes.io/ingress.class: "nginx"
    ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/auth-url: "https://$host/oauth2/auth"
    nginx.ingress.kubernetes.io/auth-signin: "https://$host/oauth2/start?rd=$escaped_request_uri"

我一直在使用此oAuth代理:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: oauth2-proxy
  namespace: kube-system
spec:
  replicas: 1
  selector:
    matchLabels:
      app: oauth2-proxy
  template:
    metadata:
      labels:
        app: oauth2-proxy
    spec:
      containers:
      - env:
          - name: OAUTH2_PROXY_PROVIDER
            value: azure
          - name: OAUTH2_PROXY_AZURE_TENANT
            value: xxx
          - name: OAUTH2_PROXY_CLIENT_ID
            value: yyy
          - name: OAUTH2_PROXY_CLIENT_SECRET
            value: zzz
          - name: OAUTH2_PROXY_COOKIE_SECRET
            value: anyrandomstring
          - name: OAUTH2_PROXY_HTTP_ADDRESS
            value: "0.0.0.0:4180"
          - name: OAUTH2_PROXY_UPSTREAM
            value: "http://where_to_redirect_to:443"
        image: machinedata/oauth2_proxy:latest
        imagePullPolicy: IfNotPresent
        name: oauth2-proxy
        ports:
        - containerPort: 4180
          protocol: TCP

答案 1 :(得分:0)

我的设置类似于4c74356b41的

oauth2-proxy部署

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app: oauth2-proxy
  name: oauth2-proxy
  namespace: monitoring
spec:
  replicas: 1
  selector:
    matchLabels:
      app: oauth2-proxy
  template:
    metadata:
      labels:
        app: oauth2-proxy
    spec:
      containers:
      - args:
        - --azure-tenant=TENANT-GUID
        - --email-domain=company.com
        - --http-address=0.0.0.0:4180
        - --provider=azure
        - --upstream=file:///dev/null
        env:
        - name: OAUTH2_PROXY_CLIENT_ID
          valueFrom:
            secretKeyRef:
              key: client-id
              name: oauth2-proxy
        - name: OAUTH2_PROXY_CLIENT_SECRET
          valueFrom:
            secretKeyRef:
              key: client-secret
              name: oauth2-proxy
        - name: OAUTH2_PROXY_COOKIE_SECRET
          valueFrom:
            secretKeyRef:
              key: cookie-secret
              name: oauth2-proxy
        image: quay.io/pusher/oauth2_proxy:v3.1.0
        name: oauth2-proxy

oauth2-proxy服务

apiVersion: v1
kind: Service
metadata:
  labels:
    app: oauth2-proxy
  name: oauth2-proxy
  namespace: monitoring
spec:
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: http
  selector:
    app: oauth2-proxy
  type: ClusterIP

oauth2-proxy入口

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
  labels:
    app: oauth2-proxy
  name: oauth2-proxy
  namespace: monitoring
spec:
  rules:
  - host: myapp.hostname.net
    http:
      paths:
      - backend:
          serviceName: oauth2-proxy
          servicePort: 80
        path: /oauth2

oauth2-proxy配置

apiVersion: v1
kind: Secret
metadata:
  labels:
    app: oauth2-proxy
  name: oauth2-proxy
  namespace: monitoring
data:
# Values below are fake
  client-id: AAD_CLIENT_ID
  client-secret: AAD_CLIENT_SECRET
  cookie-secret: COOKIE_SECRET

使用AAD入口的应用程序

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/auth-signin: https://$host/oauth2/start?rd=$request_uri
    nginx.ingress.kubernetes.io/auth-url: https://$host/oauth2/auth
  labels:
    app: myapp
  name: myapp
  namespace: monitoring
spec:
  rules:
  - host: myapp.hostname.net
    http:
      paths:
      - backend:
          serviceName: myapp
          servicePort: 80
        path: /
  tls:
  - hosts:
    - myapp.hostname.net

需要完成的另一步骤是将重定向URI添加到AAD App注册中。在Azure门户中导航到您的AAD应用注册>身份验证>将https://myapp.hostname.net/oauth2/callback添加到重定向URI>保存

答案 2 :(得分:0)

我最终在这里找到解决方法:https://github.com/helm/charts/issues/5958

我必须使用内部服务地址作为auth-url,在其他任何地方都没有看到。

nginx.ingress.kubernetes.io/auth-url:“ http://oauth2-proxy.development.svc.cluster.local:4180/oauth2/auth