【问题标题】:Nginx Ingress with OAuth2 authentication 404 page not found in Kubernetes在 Kubernetes 中找不到具有 OAuth2 身份验证 404 页面的 Nginx Ingress
【发布时间】:2021-02-22 20:00:43
【问题描述】:

按照link 上关于 Stack Overflow 的上一个问题,在成功验证后(在 Github.com),我在浏览器上得到 404 page not found

下面的 Ingress 配置(由 nginx-ingress 控制器使用):

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress
  namespace: nginx-ingress
  annotations:
     nginx.ingress.kubernetes.io/auth-url: "https://$host/oauth2/auth"
     nginx.ingress.kubernetes.io/auth-signin: "https://$host/oauth2/start?rd=$request_uri"
spec:
  ingressClassName: nginx
  rules:
  - host: site.example.com
    http:
      paths:
      - path: /v1                                               
        backend:
          serviceName: web-service
          servicePort: 8080
      - path: /
        backend:
          serviceName: oauth2-proxy
          servicePort: 4180
  tls:
  - hosts:
    - site.example.com
    secretName: example-tls

$ kubectl get ing -n nginx-ingress
  NAME      CLASS   HOSTS              ADDRESS   PORTS     
  ingress   nginx   site.example.com             80, 443   

我尝试通过 oauth2 访问的 node.js Web 应用程序已使用两条路径(/ 和 /v1)构建。 Web 应用程序在 Service web-service 之后。

OAuth2 Github 应用配置:

Homepage URL
https://site.example.com/

Authorization callback URL
https://site.example.com/oauth2/callback

OAuth2 部署和服务:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    k8s-app: oauth2-proxy
  name: oauth2-proxy
  namespace: nginx-ingress
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: oauth2-proxy
  template:
    metadata:
      labels:
        k8s-app: oauth2-proxy
    spec:
      containers:
      - args:
        - --provider=github
        - --email-domain=*
        - --upstream=file:///dev/null
        - --http-address=0.0.0.0:4180
        # Register a new application
        # https://github.com/settings/applications/new
        env:
        - name: OAUTH2_PROXY_CLIENT_ID
          value: 32066******52
        - name: OAUTH2_PROXY_CLIENT_SECRET
          value: ff2b0a***************9bd
        - name: OAUTH2_PROXY_COOKIE_SECRET
          value: deSF_t******03-HQ==
        image: quay.io/oauth2-proxy/oauth2-proxy:latest
        imagePullPolicy: Always
        name: oauth2-proxy
        ports:
        - containerPort: 4180
          protocol: TCP

apiVersion: v1
kind: Service
metadata:
  labels:
    k8s-app: oauth2-proxy
  name: oauth2-proxy
  namespace: nginx-ingress
spec:
  ports:
  - name: http
    port: 4180
    protocol: TCP
    targetPort: 4180
  selector:
    k8s-app: oauth2-proxy

来自 oauth2-proxy 容器的日志:

[2020/11/10 19:47:27] [logger.go:508] Error loading cookied session: cookie "_oauth2_proxy" not present, removing session
10.44.0.2:51854 - - [2020/11/10 19:47:27] site.example.com GET - "/" HTTP/1.1 "Mozilla/5.0
[2020/11/10 19:47:27] [logger.go:508] Error loading cookied session: cookie "_oauth2_proxy" not present, removing session
10.44.0.2:51858 - - [2020/11/10 19:47:27] site.example.com GET - "/favicon.ico" HTTP/1.1 "Mozilla/5.0 ....
10.44.0.2:51864 - - [2020/11/10 19:47:28] site.example.com GET - "/oauth2/start?rd=%2F" HTTP/1.1 "Mozilla/5.0 ....
10.44.0.2:52004 - marco.***81@gmail.com [2020/11/10 19:48:33] [AuthSuccess] Authenticated via OAuth2: Session{email:marco.***81@gmail.com user:mafi81 PreferredUsername: token:true created:2020-11-10 19:48:32.494549621 +0000 UTC m=+137.822819581}
10.44.0.2:52004 - - [2020/11/10 19:48:32] site.example.com GET - "/oauth2/callback?code=da9c3af9d8f35728d2d1&state=e3280edf2430c507cd74f3d4655500c1%3A%2F" HTTP/1.1 "Mozilla/5.0 ...
10.44.0.2:52012 - marco.****81@gmail.com [2020/11/10 19:48:33] site.example.com GET - "/" HTTP/1.1 "Mozilla/5.0 ....
10.44.0.2:52014 - marco.****81@gmail.com [2020/11/10 19:48:33] site.example.com GET - "/favicon.ico" HTTP/1.1 "Mozilla/5.0 .... Chrome/86.0.4240.193 Safari/537.36" 404 19 0.000

测试环境:

  • 带有 kubeadm v1.19.3 的 VirtualBox
  • NGINX 入口控制器版本=1.9.0。

我对 Ingress 资源下的路径配置仍然没有信心。 任何有关如何进行故障排除的建议都会很棒。

更新

按照马特的回答,给出测试身份验证的正确方法,这里是新环境:

NGINX Ingress controller
  Release:       v0.41.2
  Build:         d8a93551e6e5798fc4af3eb910cef62ecddc8938
  Repository:    https://github.com/kubernetes/ingress-nginx
  nginx version: nginx/1.19.4

OAuth2 Pod
  image: quay.io/oauth2-proxy/oauth2-proxy

入口清单:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress
  namespace: web
  annotations:
     nginx.ingress.kubernetes.io/auth-response-headers: Authorization
     nginx.ingress.kubernetes.io/auth-url: http://oauth2-proxy.web.svc.cluster.local:4180/oauth2/auth
     nginx.ingress.kubernetes.io/auth-signin: https://site.example.com/oauth2/start?rd=$request_uri
     nginx.ingress.kubernetes.io/configuration-snippet: |
       auth_request_set $name_upstream_1 $upstream_cookie__oauth2_proxy_1;

       access_by_lua_block {
         if ngx.var.name_upstream_1 ~= "" then
           ngx.header["Set-Cookie"] = "_oauth2_proxy_1=" ..  ngx.var.name_upstream_1 .. ngx.var.auth_cookie:match("(; .*)")
         end
       }

spec:
  ingressClassName: nginx-oauth
  rules:
  - host: site.example.com
    http:
      paths:
      - path: /
        backend:
          serviceName: web-service
          servicePort: 8080

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: oauth2-proxy
  namespace: web

spec:
  ingressClassName: nginx-oauth

  rules:
  - host: site.example.com
    http:
      paths:
      - backend:
          serviceName: oauth2-proxy
          servicePort: 4180
        path: /oauth2

  tls:
  - hosts:
    - site.example.com
    secretName: tls

请注意,我必须更改一个注释才能使其正常工作:

  • auth-url: http://oauth2-proxy.web.svc.cluster.local:4180/oauth2/auth(解决解析失败)

【问题讨论】:

  • 您从很多回复中省略了状态代码,但在回复日志中看到您的电子邮件地址,就好像所有的身份验证都成功发生了;如果您导航到 site.example.com/v1/whatever 我希望您无需再次进行身份验证就可以被允许(当然,假设 cookie 已成功提供)
  • 另外,虽然这不是您所要求的,但在您无法控制的项目上使用 :latest 的 docker 标签会要求开始出现非常、非常、非常奇怪的错误.该项目从 v5 到 v6 专门对其进行了几次大修,您的集群将在 pull 时静默更新

标签: nginx kubernetes oauth-2.0


【解决方案1】:

根据oauth-proxy documentation,您必须使用kubernetes/ingress-nginx

您可以在此处阅读有关differences between nginxinc/kubernetes-ingress and kubernetes/ingress-nginx Ingress Controllers 的更多信息。

在 oath2-proxy 文档(前面提到过)中,您可以找到以下内容:

当您在 Kubernetes 中使用 ingress-nginx 时,您必须为您的 Ingress 使用 kubernetes/ingress-nginx(包括 Lua 模块)和以下配置 sn-p。当通过 proxy_pass 处理位置时,使用 auth_request_set 设置的变量不能在普通 nginx 配置中设置,然后只能由 Lua 处理。注意 nginxinc/kubernetes-ingress 不包含 Lua 模块。

nginx.ingress.kubernetes.io/auth-response-headers: Authorization
nginx.ingress.kubernetes.io/auth-signin: https://$host/oauth2/start?rd=$escaped_request_uri
nginx.ingress.kubernetes.io/auth-url: https://$host/oauth2/auth
nginx.ingress.kubernetes.io/configuration-snippet: |
  auth_request_set $name_upstream_1 $upstream_cookie_name_1;

  access_by_lua_block {
    if ngx.var.name_upstream_1 ~= "" then
      ngx.header["Set-Cookie"] = "name_1=" .. ngx.var.name_upstream_1 .. ngx.var.auth_cookie:match("(; .*)")
    end
  }

因此,如果我们可以信任文档,您的身份验证将无法正常工作,因为您使用了错误的 nginx 控制器并且您缺少注释。

【讨论】:

  • 是的,它有效!经过一些调整,进入更新。谢谢马特
猜你喜欢
  • 2020-11-30
  • 1970-01-01
  • 2019-10-08
  • 1970-01-01
  • 2020-10-07
  • 1970-01-01
  • 2015-05-28
  • 2020-03-18
  • 1970-01-01
相关资源
最近更新 更多