简体   繁体   English

通过 nginx 入口控制器进行基本身份验证

[英]Basic authentication via nginx ingress controller

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

Is there a way to tune nginx so that it appends Authorization: Basic header to every request forwarded to my service so that users won't have to type the password?有没有办法调整 nginx 以便它向转发到我的服务的每个请求附加 Authorization: Basic 标头,这样用户就不必输入密码?

This solution did not work for me:此解决方案对我不起作用:

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

as I am still being prompted for a password.因为我仍然被提示输入密码。

Here is an ingress rule using a secret that contains a file generated with htpasswd.这是一个使用包含 htpasswd 生成的文件的秘密的入口规则。 It's important the file generated is named auth (actually - that the secret has a key data.auth), otherwise the ingress-controller returns a 503.生成的文件命名为 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

Secret creation秘密创作

$ 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

Access it using curl and you should get 200 Ok.使用 curl 访问它,你应该得到 200 Ok。

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

Check this example here 在此处检查此示例

Solution:解决方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM