【问题标题】:Basic authentication via nginx ingress controller通过 nginx 入口控制器进行基本身份验证
【发布时间】:2020-05-06 12:10:07
【问题描述】:

我在 AWS 上使用 nginx 入口控制器 (https://kubernetes.github.io/ingress-nginx/deploy/)。后端服务(来自 ECK 的 kibana:https://www.elastic.co/guide/en/cloud-on-k8s/current/k8s-operator-config.html)使用 HTTP 基本身份验证机制。

有没有办法调整 nginx,使其将 Authorization: Basic 标头附加到转发到我的服务的每个请求,以便用户不必输入密码?

这个解决方案对我不起作用:

nginx.ingress.kubernetes.io/configuration-snippet: |
      more_set_headers "Authorization: Basic encoded_credentals";

因为我仍然被提示输入密码。

【问题讨论】:

    标签: nginx kubernetes nginx-ingress


    【解决方案1】:

    这是一个使用包含使用 htpasswd 生成的文件的秘密的入口规则。生成的文件命名为 auth 很重要(实际上 - 密钥具有密钥 data.auth),否则入口控制器返回 503。

    apiVersion: networking.k8s.io/v1beta1
    kind: Ingress
    metadata:
      name: ingress-with-auth
      annotations:
        # type of authentication
        nginx.ingress.kubernetes.io/auth-type: basic
        # name of the secret that contains the user/password definitions
        nginx.ingress.kubernetes.io/auth-secret: basic-auth
        # message to display with an appropriate context why the authentication is required
        nginx.ingress.kubernetes.io/auth-realm: 'Authentication Required - foo'
    spec:
      rules:
      - host: foo.bar.com
        http:
          paths:
          - path: /
            backend:
              serviceName: http-svc
              servicePort: 80
    

    秘密创作

    $ htpasswd -c auth foo
    New password: <bar>
    New password:
    Re-type new password:
    Adding password for user foo
    $ kubectl create secret generic basic-auth --from-file=auth
    secret "basic-auth" created
    $ kubectl get secret basic-auth -o yaml
    apiVersion: v1
    data:
      auth: Zm9vOiRhcHIxJE9GRzNYeWJwJGNrTDBGSERBa29YWUlsSDkuY3lzVDAK
    kind: Secret
    metadata:
      name: basic-auth
      namespace: default
    type: Opaque
    

    使用 curl 访问它,你应该得到 200 Ok。

    $ curl -v http://10.2.29.4/ -H 'Host: foo.bar.com' -u 'foo:bar'
    

    检查这个例子here

    【讨论】:

    • 这增加了一个身份验证层,这与我想要完成的完全相反。
    • 好吧,也许我理解错了...如果用户没有从您希望密码的来源输入密码?
    【解决方案2】:

    解决方案:

    nginx.ingress.kubernetes.io/configuration-snippet: |
        more_set_input_headers "Authorization: Basic <based64 user:pass>";
    

    【讨论】:

      猜你喜欢
      • 2017-12-06
      • 2018-01-11
      • 2017-09-18
      • 2021-03-19
      • 2016-06-17
      • 2021-05-20
      • 2021-03-25
      • 2018-01-02
      相关资源
      最近更新 更多