通过nginx ingress控制器实现基本身份验证

9
我正在AWS上使用nginx ingress控制器(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";

由于我仍然被提示输入密码。

2个回答

12

这里是一个使用包含由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'

请查看此处的示例。


3
这会添加一个认证层,这与我正在努力实现的目标完全相反。 - Łukasz
1
好吧,也许我误解了...如果用户没有输入密码,那你希望密码从哪里来? - Arghya Sadhu

7

解决方案:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接