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

Łuk*_*asz 4 nginx kubernetes nginx-ingress

我在 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";
Run Code Online (Sandbox Code Playgroud)

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

Arg*_*dhu 7

这是一个使用包含 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
Run Code Online (Sandbox Code Playgroud)

秘密创作

$ 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
Run Code Online (Sandbox Code Playgroud)

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

$ curl -v http://10.2.29.4/ -H 'Host: foo.bar.com' -u 'foo:bar'
Run Code Online (Sandbox Code Playgroud)

在此处检查此示例

  • 这添加了一个身份验证层,这与我想要实现的目标完全相反。 (3认同)

Łuk*_*asz 5

解决方案:

nginx.ingress.kubernetes.io/configuration-snippet: |
    more_set_input_headers "Authorization: Basic <based64 user:pass>";
Run Code Online (Sandbox Code Playgroud)