签署容器镜像的动机是只信任专门的镜像提供者以减轻中间人 (MITM) 攻击或对容器注册表的攻击。签署图像的一种方法是使用 GNU Privacy Guard ( GPG ) 密钥。这种技术通常与任何符合 OCI 的容器注册表兼容,例如Quay.io。值得一提的是,OpenShift 集成容器注册表开箱即用地支持这种签名机制,这使得单独的签名存储变得不必要。
从技术角度来看,我们可以利用 Podman 对镜像进行签名,然后再将其推送到远程注册表。之后,所有运行 Podman 的系统都必须配置为从远程服务器检索签名,远程服务器可以是任何简单的 Web 服务器。这意味着在图像拉取操作期间,每个未签名的图像都将被拒绝。但这是如何工作的?
首先,我们必须创建一个 GPG 密钥对或选择一个已经在本地可用的密钥对。要生成新的 GPG 密钥,只需运行gpg --full-gen-key并按照交互式对话框操作。现在我们应该能够验证密钥在本地是否存在:
> gpg --list-keys sgrunert@suse.com pub rsa2048 2018-11-26 [SC] [expires: 2020-11-25] XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX uid [ultimate] Sascha Grunert <sgrunert@suse.com> sub rsa2048 2018-11-26 [E] [expires: 2020-11-25]
现在让我们假设我们运行一个容器注册表。例如,我们可以简单地在本地机器上启动一个:
sudo podman run -d -p 5000:5000 docker.io/registry
[root@localhost ~]# podman run -d -p 5000:5000 docker.io/registry
Trying to pull docker.io/library/registry:latest...
Getting image source signatures
Copying blob 5299e6f78605 skipped: already exists
Copying blob 213ec9aee27d skipped: already exists
Copying blob 44c4c74a95e4 done
Copying blob 4c2fb79b7ce6 done
Copying blob 74a97d2d84d9 done
Copying config 3a0f7b0a13 done
Writing manifest to image destination
Storing signatures
ddfb8e13c6abe86269371114ed12f79961d69c393e5a8c9e779b60af24696cf6
[root@localhost ~]#
注册表对镜像签名一无所知,它只是为容器镜像提供远程存储。这意味着如果我们想要对图像进行签名,我们必须注意如何分发签名。
alpine让我们为我们的签名实验选择一个标准图像:
[root@localhost ~]# podman pull docker://docker.io/alpine:latest
Trying to pull docker.io/library/alpine:latest...
Getting image source signatures
Copying blob 213ec9aee27d skipped: already exists
Copying config 9c6f072447 done
Writing manifest to image destination
Storing signatures
9c6f0724472873bb50a2ae67a9e7adcb57673a183cea8b06eb778dca859181b5
[root@localhost ~]#
[root@localhost ~]# podman images alpine
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/library/alpine latest 9c6f07244728 6 days ago 5.83 MB
[root@localhost ~]#
现在我们可以重新标记图像以将其指向我们的本地注册表:
[root@localhost ~]# podman tag alpine localhost:5000/alpine
[root@localhost ~]# podman images alpine
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/library/alpine latest