首页 > 解决方案 > 使用具有许多入口路径的一个 oauth2_proxy 实例?

问题描述

我在 Azure 上的 kubernetes 服务中运行一个应用程序,并使用 NGINX 入口控制器和一个带有 FQDN 的公共 IP 地址设置它。这一切都很好。
然后,我想通过使用 oauth2-proxy 进行第三方登录来增加安全性。我想将我的设置保留为每个命名空间一个入口控制器和一个 oauth2_proxy,多个应用程序一起运行。由于 Azure 不支持为此使用子域,因此我一直在使用路径路由到正确的应用程序。我见过这样的例子,关于如何将一个 oauth2_proxy 用于多个子域,但是是否可以让它与多个路径一起使用?

设置
这是当前的工作设置,只有一个应用程序,位于 root 上/。我想切换到特定于应用程序的路径以及在不同路径上运行多个应用程序的能力。例如。/my-app/another-app

oauth2-proxy-config.yaml

config:
  existingSecret: oauth2-proxy-creds

extraArgs:
  whitelist-domain: my-fqdn.uksouth.cloudapp.azure.com
  cookie-domain: my-fqdn.uksouth.cloudapp.azure.com
  email-domain: example.com
  provider: github

ingress:
  enabled: true
  path: /oauth2
  hosts:
    - my-fqdn.uksouth.cloudapp.azure.com
  annotations:
    kubernetes.io/ingress.class: nginx
    cert-manager.io/cluster-issuer: letsencrypt-prod # cert-manager cluster issuer set up for Let's Encrypt
  tls:
    - secretName: my-fqdn-tls # TLS generated by letsencrypt-prod
      hosts:
        - my-fqdn.uksouth.cloudapp.azure.com

这是使用以下 helm 命令安装的

helm upgrade oauth2-proxy --install stable/oauth2-proxy --namespace $NAMESPACE --reuse-values --values oauth2-proxy-config.yaml

应用入口.yaml

apiVersion: networking.k8s.io/v1beta1 # for versions before 1.14 use extensions/v1beta1
kind: Ingress
metadata:
  name: nginx-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    cert-manager.io/cluster-issuer: letsencrypt-prod
#    nginx.ingress.kubernetes.io/rewrite-target: /$2 # Not working with the /oauth2 path and not needed when using root path for the app
    nginx.ingress.kubernetes.io/auth-url: "https://my-fqdn.uksouth.cloudapp.azure.com/oauth2/auth"
    nginx.ingress.kubernetes.io/auth-signin: "https://my-fqdn.uksouth.cloudapp.azure.com/oauth2/start?rd=https%3A%2F%2F$host$request_uri"
spec:
  tls:
  - secretName: my-fqdn-tls
    hosts:
    - my-fqdn.uksouth.cloudapp.azure.com
  rules:
  - host: my-fqdn.uksouth.cloudapp.azure.com
    http:
      paths:
      - path: / # I would like to be able to use something like '/path1(/|$)(.*)' instead of root.
        backend:
          serviceName: my-app
          servicePort: 80

标签: azurenginxkubernetesoauth-2.0kubernetes-ingress

解决方案


当然,在单个 Ingress 资源定义中使用多个入口路径是可行的,请检查这个工作示例:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  labels:
    app: hello-worlds
  name: hello-wrolds
  annotations:
    cert-manager.io/issuer: selfsigned-issuer
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/auth-signin: "https://my-fqdn.uksouth.cloudapp.azure.com/oauth2/start?rd=$escaped_request_uri"
    nginx.ingress.kubernetes.io/auth-url: "https://my-fqdn.uksouth.cloudapp.azure.com/oauth2/auth"
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - host: my-fqdn.uksouth.cloudapp.azure.com
      http:
        paths:
          - path: /my-app/(.*)
            backend:
              serviceName: my-app
              servicePort: 5000
          - path: /another-app/(.*)
            backend:
              serviceName: another-app
              servicePort: 5000    
  tls:
    - hosts:
      - my-fqdn.uksouth.cloudapp.azure.com
      secretName: certmgr-selfsign-tls-requires-ouath

就我而言,对于两个后端,应用程序根文件夹都是“/hello”,因此请求的 URL 分别是:

https://my-fqdn.uksouth.cloudapp.azure.com/my-app/hello

https://my-fqdn.uksouth.cloudapp.azure.com/another-app/hello

推荐阅读