基于容器来添加账户形式生成htpasswd

1. 使用 Apache 官方镜像(自带 htpasswd)

1
docker run --rm httpd:latest htpasswd -nb admin admin@321

输出:

1
admin:$apr1$xxxxxx$xxxxxxxxxxxx

其中:

  • -n输出到 stdout,不写文件
  • -b:密码从参数读取

生成文件:

1
2
3
4
docker run --rm \
-v $(pwd):/data \
httpd:latest \
htpasswd -bc /data/htpasswd admin admin@321

结果:

当前目录生成:

1
./htpasswd

内容:

1
admin:$apr1$xxxxx$xxxxx

2. Alpine 安装 apache2-utils

临时生成:

1
docker run --rm -it alpine sh

容器内:

1
2
3
apk add apache2-utils
htpasswd -bc /tmp/htpasswd admin admin@321
cat /tmp/htpasswd

3. Kubernetes Secret 场景

例如创建 Basic Auth:

1
htpasswd -nb admin admin@321 > auth

生成:

1
admin:$apr1$xxxx

创建 secret:

1
2
kubectl create secret generic basic-auth \
--from-file=auth

Ingress:

1
2
3
nginx.ingress.kubernetes.io/auth-type: basic
nginx.ingress.kubernetes.io/auth-secret: basic-auth
nginx.ingress.kubernetes.io/auth-realm: "Authentication Required"

生产环境更推荐:

1
docker run --rm httpd:2.4-alpine htpasswd -nbB admin admin@321

使用 bcrypt:

1
admin:$2y$05$xxxxxxxxxxxxxxxxxxxx

安全性比默认 $apr1$ MD5 更高。