如何为 kubernetes ingress basic-auth 设置辅助密钥

How to set secodary key for kubernetes ingress basic-auth

我想为我在 k8s 中的所有服务创建一个入口,并为入口提供基本身份验证。但是对于身份验证轮换,我想支持用户的二次身份验证,以便在他们重新生成主键时可以到达端点。

我目前可以按照 this guide 设置具有单一基本身份验证的入口。

调整 the guide,您可以将多个用户名和密码放入您用来生成基本身份验证密码的 auth 文件中。具体来说,如果您 运行 htpasswd 命令没有 -c 标志,例如htpasswd <filename> <username> 它将 添加 一个条目到文件而不是从头开始创建一个新文件:

$ htpasswd -c auth foo
New password: <bar>
Re-type new password: <bar>
Adding password for user foo

$ cat auth
foo:$apr1$isCec65Z$JNaQ0GJCpPeG8mR1gYsgM1

$ htpasswd auth user2
New password: <pass2>
Re-type new password: <pass2>
Adding password for user user2

$ cat auth
foo:$apr1$isCec65Z$JNaQ0GJCpPeG8mR1gYsgM1
user2:$apr1$.FsOzlqA$eFxym7flDnoDtymRLraA2/

如果您已经通过给定的命令首先创建了秘密:

$ kubectl create secret generic basic-auth --from-file=auth

然后您可以使用 更新密钥:

$ kubectl create secret generic basic-auth --from-file=auth\
  --dry-run -o yaml | kubectl apply -f -
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
secret/basic-auth configured

您可以确认设置密码有效:

$ kubectl get secret basic-auth -ojsonpath={.data.auth} | base64 -D
foo:$apr1$isCec65Z$JNaQ0GJCpPeG8mR1gYsgM1
user2:$apr1$.FsOzlqA$eFxym7flDnoDtymRLraA2/

最后,您可以使用用户名和密码测试基本身份验证是否有效:

$ curl http://<minikube_ip>/ -H 'Host: foo.bar.com' \
  -s -w"%{http_code}" -o /dev/null
401

$ curl http://<minikube_ip>/ -H 'Host: foo.bar.com' \
  -u 'wronguser:wrongpass' \
  -s -w"%{http_code}" -o /dev/null
401

$ curl http://<minikube_ip>/ -H 'Host: foo.bar.com' \
  -u 'foo:bar' \
  -s -w"%{http_code}" -o /dev/null
200

$ curl http://<minikube_ip>/ -H 'Host: foo.bar.com' \
  -u 'user2:pass2' \
  -s -w"%{http_code}" -o /dev/null
200