背景
周末在给红帽官方的 Jenkins maven agent 添加 podman 的时候,在构建过程中需要使用到红帽的用户名和密码,以前没有遇到这种情况,从而花了点时间研究一下如何保护敏感信息,比如密码、私匙、token 等。
错误方式
1. 使用 COPY
命令拷贝包含敏感信息的文件
docker 镜像是分层的,所有层次的信息都会被保留,COPY
命名拷贝进去的文件会放在其中一层,即使你在后面的层删掉该文件,也可以从前面的拿出来。在同一个 RUN
命令通过 http 下载文件,使用完立刻删除掉,这种方法倒是可以的。
2. 使用 --build-arg
参数
构建参数信息也保留在镜像中,通过 docker history --no-trunc
可以清楚显示出来。
解决办法
使用 --secret
参数保护敏感信息
docker build 新加这个参数允许我们把 Dockerfile 使用到的敏感信息传进去,并且不会保留在最终的镜像中,也不需要在代码库中保留。
id
:secret ID,需要和 RUN --mount
中使用的 ID 一致,Docker 不会使用本地文件,文件名可能也包含了敏感信息。
dst
:给包含 secret file 重命名,在后面 RUN
命令中使用。
例子:
生成 secret file。
1
⋊> echo "1234567890" >> mysecret.txt
在 Dockerfile 中 mount secret file 并使用。
1
2
3
4
5
6
7FROM alpine
# 加载到默认路径
RUN --mount=type=secret,id=mysecret cat /run/secrets/mysecret
# 指定路径命名
RUN --mount=type=secret,id=mysecret,dst=/test1 cat /test1执行构建命令,通过
--secret
参数把 secret file 传进去。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44⋊> ~/test docker build --progress=plain --secret id=mysecret,src=mysecret.txt . 14:57:19
#1 [internal] load build definition from Dockerfile
#1 sha256:d20b390f28c3488b284798feeb583a50829f5e4ebb62ce2b6e9f3ac8113764b4
#1 transferring dockerfile: 223B done
#1 DONE 0.0s
#2 [internal] load .dockerignore
#2 sha256:1607d2d89e842160bdf4799dfd772c598593cbea6f035811da2d29f99468089b
#2 transferring context: 2B done
#2 DONE 0.0s
#3 [internal] load metadata for docker.io/library/alpine:latest
#3 sha256:d4fb25f5b5c00defc20ce26f2efc4e288de8834ed5aa59dff877b495ba88fda6
#3 DONE 3.2s
#4 [1/3] FROM docker.io/library/alpine@sha256:d9a7354e3845ea8466bb00b22224d9116b183e594527fb5b6c3d30bc01a20378
#4 sha256:e2353129f7e61082cab034ce5c46d184158ae870520b665d24db01b2e4cd31e6
#4 resolve docker.io/library/alpine@sha256:d9a7354e3845ea8466bb00b22224d9116b183e594527fb5b6c3d30bc01a20378 done
#4 sha256:d0710affa17fad5f466a70159cc458227bd25d4afb39514ef662ead3e6c99515 528B / 528B done
#4 sha256:7731472c3f2a25edbb9c085c78f42ec71259f2b83485aa60648276d408865839 1.47kB / 1.47kB done
#4 sha256:596ba82af5aaa3e2fd9d6f955b8b94f0744a2b60710e3c243ba3e4a467f051d1 0B / 2.81MB 0.1s
#4 sha256:d9a7354e3845ea8466bb00b22224d9116b183e594527fb5b6c3d30bc01a20378 1.64kB / 1.64kB done
#4 sha256:596ba82af5aaa3e2fd9d6f955b8b94f0744a2b60710e3c243ba3e4a467f051d1 1.05MB / 2.81MB 0.7s
#4 sha256:596ba82af5aaa3e2fd9d6f955b8b94f0744a2b60710e3c243ba3e4a467f051d1 2.10MB / 2.81MB 1.0s
#4 sha256:596ba82af5aaa3e2fd9d6f955b8b94f0744a2b60710e3c243ba3e4a467f051d1 2.81MB / 2.81MB 1.1s done
#4 extracting sha256:596ba82af5aaa3e2fd9d6f955b8b94f0744a2b60710e3c243ba3e4a467f051d1
#4 extracting sha256:596ba82af5aaa3e2fd9d6f955b8b94f0744a2b60710e3c243ba3e4a467f051d1 0.2s done
#4 DONE 1.4s
#5 [2/3] RUN --mount=type=secret,id=mysecret cat /run/secrets/mysecret
#5 sha256:98af4b597cbde2a1d51ae3b20040ee68fde257cb0a1bc317721a4aeac1facc70
#5 0.246 1234567890
#5 DONE 0.3s
#6 [3/3] RUN --mount=type=secret,id=mysecret,dst=/test1 cat /test1
#6 sha256:5d7f9e8e89a1aeefabe8ae05888bfc9647f8d05cd013c161716948ff0418f629
#6 0.249 1234567890
#6 DONE 0.3s
#7 exporting to image
#7 sha256:e8c613e07b0b7ff33893b694f7759a10d42e180f2b4dc349fb57dc6b71dcab00
#7 exporting layers 0.0s done
#7 writing image sha256:6a0b21aa77b4f41b9c02fc076d4b80aba7f5f66c79435998d1ac32c0a5182545 done
#7 DONE 0.0s
参考文档
Build images with BuildKit
Docker build secrets: the easy way, the wrong way, the sneaky way