内容信任的委派

Docker 内容信任 (DCT) 中的委派允许您控制谁可以签名,谁不能签名 镜像标记。委托将具有一对私有和公共委托密钥。一个代表团 可以包含多对密钥和贡献者,以便 a) 允许多个用户 成为委托的一部分,以及 b) 支持密钥轮换。

Docker Content Trust 中最重要的委派是 。 这被视为可信镜像标签的规范来源,并且没有 contributor 的密钥在此委托下,他们将无法对标签进行签名。targets/releases

幸运的是,在使用这些命令时,我们会自动 初始化存储库,管理存储库密钥,并通过 将协作者的密钥添加到委派中。$ docker trusttargets/releasesdocker trust signer add

配置 Docker 客户端

默认情况下,这些命令期望公证服务器 URL 为 与 image 标记中指定的注册表 URL 相同(遵循与 类似的逻辑)。使用 Docker Hub 或 DTR 时,公证人 服务器 URL 与注册表 URL 相同。但是,对于自托管 environments 或 3rd party registry 中,您需要指定替代项 公证服务器的 URL。这是通过以下方式完成的:$ docker trust$ docker push

$ export DOCKER_CONTENT_TRUST_SERVER=https://<URL>:<PORT>

如果不在自托管环境中导出此变量,您可能会看到 错误如下:

$ docker trust signer add --key cert.pem jeff registry.example.com/admin/demo
Adding signer "jeff" to registry.example.com/admin/demo...
<...>
Error: trust data missing for remote repository registry.example.com/admin/demo or remote repository not found: timestamp key trust data unavailable.  Has a notary repository been initialized?

$ docker trust inspect registry.example.com/admin/demo --pretty
WARN[0000] Error while downloading remote metadata, using cached timestamp - this might not be the latest version available remotely
<...>

如果您已为公证服务器启用身份验证,或者正在使用 DTR,则需要登录 在您可以将数据推送到 Notary 服务器之前。

$ docker login registry.example.com/user/repo
Username: admin
Password:

Login Succeeded

$ docker trust signer add --key cert.pem jeff registry.example.com/user/repo
Adding signer "jeff" to registry.example.com/user/repo...
Initializing signed repository for registry.example.com/user/repo...
Successfully initialized "registry.example.com/user/repo"
Successfully added signer: jeff to registry.example.com/user/repo

如果您不登录,您将看到:

$ docker trust signer add --key cert.pem jeff registry.example.com/user/repo
Adding signer "jeff" to registry.example.com/user/repo...
Initializing signed repository for registry.example.com/user/repo...
you are not authorized to perform this operation: server returned 401.

Failed to add signer to: registry.example.com/user/repo

配置 Notary 客户端

DCT 的一些更高级的功能需要 Notary CLI。要安装 并且 配置 Notary CLI:

  1. 下载客户端并确保它在您的路径上可用。

  2. 在 创建配置文件,内容如下:~/.notary/config.json

{
  "trust_dir" : "~/.docker/trust",
  "remote_server": {
    "url": "https://registry.example.com",
    "root_ca": "../.docker/ca.pem"
  }
}

新创建的配置文件包含有关本地 Docker 信任数据和公证服务器 URL 的位置的信息。

有关如何在 Docker Content Trust 使用案例,请参阅此处的 Notary CLI 文档

创建委托密钥

添加第一个贡献者的先决条件是一对委托密钥。 这些键可以使用 生成方式在本地生成,生成方式为 证书颁发机构。$ docker trust

使用 Docker Trust 生成密钥

Docker trust 具有用于委派密钥对的内置生成器。运行此命令将自动加载 将委托私有密钥委托到本地 Docker 信任存储中。$ docker trust generate <name>

$ docker trust key generate jeff

Generating key for jeff...
Enter passphrase for new jeff key with ID 9deed25: 
Repeat passphrase for new jeff key with ID 9deed25: 
Successfully generated and loaded private key. Corresponding public key available: /home/ubuntu/Documents/mytrustdir/jeff.pub

手动生成密钥

如果您需要手动生成私有密钥(RSA 或 ECDSA)和 x509 证书,您可以使用本地工具,如 OpenSSL 或 cfssl 以及本地或公司范围的证书颁发机构。

以下是如何生成 2048 位 RSA 部分密钥(所有 RSA 密钥 必须至少为 2048 位):

$ openssl genrsa -out delegation.key 2048

Generating RSA private key, 2048 bit long modulus
....................................................+++
............+++
e is 65537 (0x10001)

它们应该保持私有,因为它用于对标签进行签名。delegation.key

然后,他们需要生成一个包含公钥的 x509 证书,即 你需要从他们那里得到什么。以下是生成 CSR(证书 签名请求):

$ openssl req -new -sha256 -key delegation.key -out delegation.csr

然后,他们可以将其发送到您信任的任何 CA 以签署证书,或者他们 可以自签名证书(在此示例中,创建一个 有效期为 1 年):

$ openssl x509 -req -sha256 -days 365 -in delegation.csr -signkey delegation.key -out delegation.crt

然后他们需要给你,无论是自签名还是签名 由 CA 提供。delegation.crt

最后,您需要将私钥添加到本地 Docker 信任存储中。

$ docker trust key load delegation.key --name jeff

Loading key from "delegation.key"...
Enter passphrase for new jeff key with ID 8ae710e: 
Repeat passphrase for new jeff key with ID 8ae710e: 
Successfully imported key from delegation.key

查看本地委派密钥

为了列出已导入到本地 Docker 信任存储中的密钥,我们 可以使用 Notary CLI。

$ notary key list

ROLE       GUN                          KEY ID                                                              LOCATION
----       ---                          ------                                                              --------
root                                    f6c6a4b00fefd8751f86194c7d87a3bede444540eb3378c4a11ce10852ab1f96    /home/ubuntu/.docker/trust/private
jeff                                    9deed251daa1aa6f9d5f9b752847647cf8d705da0763aa5467650d0987ed5306    /home/ubuntu/.docker/trust/private

在 Notary Server 中管理委托

使用 将第一个委托添加到 Notary Server 时, 我们会自动启动存储库的信任数据。这包括创建 notary target 和 snapshots 键,并将快照键轮换为 由 Notary 服务器管理。有关这些密钥的更多信息,请参见此处$ docker trust

启动存储库时,您将需要本地 公证规范根密钥。如果您之前没有启动过仓库,并且 因此没有 Notary 根密钥,将为您创建一个。$ docker trust

重要

请务必保护和备份您的 Notary Canonical Root Key

启动存储库

要将第一个密钥上传到委托,同时启动 repository 中,您可以使用命令。这将添加 贡献者的公钥分配给委托,并创建一个 第二个代表团。$ docker trust signer addtargets/releasestargets/<name>

对于 DCT,第二个委托的名称(在以下示例中)可帮助您跟踪密钥的所有者。在 更多 Notary 附加委托的高级用例用于层次结构。jeff

$ docker trust signer add --key cert.pem jeff registry.example.com/admin/demo

Adding signer "jeff" to registry.example.com/admin/demo...
Initializing signed repository for registry.example.com/admin/demo...
Enter passphrase for root key with ID f6c6a4b: 
Enter passphrase for new repository key with ID b0014f8: 
Repeat passphrase for new repository key with ID b0014f8: 
Successfully initialized "registry.example.com/admin/demo"
Successfully added signer: jeff to registry.example.com/admin/demo

您可以查看每个存储库的哪些密钥已推送到 Notary 服务器 使用命令。$ docker trust inspect

$ docker trust inspect --pretty registry.example.com/admin/demo

No signatures for registry.example.com/admin/demo


List of signers and their keys for registry.example.com/admin/demo

SIGNER              KEYS
jeff                1091060d7bfd

Administrative keys for registry.example.com/admin/demo

  Repository Key:	b0014f8e4863df2d028095b74efcb05d872c3591de0af06652944e310d96598d
  Root Key:	64d147e59e44870311dd2d80b9f7840039115ef3dfa5008127d769a5f657a5d7

您还可以使用 Notary CLI 列出委托和密钥。在这里你可以 清楚地看到密钥已附加到 和 。targets/releasestargets/jeff

$ notary delegation list registry.example.com/admin/demo

ROLE                PATHS             KEY IDS                                                             THRESHOLD
----                -----             -------                                                             ---------
targets/jeff        "" <all paths>    1091060d7bfd938dfa5be703fa057974f9322a4faef6f580334f3d6df44c02d1    1
                                          
targets/releases    "" <all paths>    1091060d7bfd938dfa5be703fa057974f9322a4faef6f580334f3d6df44c02d1    1 

添加其他签名者

Docker Trust 允许您为每个存储库配置多个委托, 允许您管理委托的生命周期。添加额外的 具有 collaborators 密钥的委托将再次添加到 角色。$ docker trusttargets/release

请注意,您将需要存储库密钥的密码;这本来是 在您首次启动存储库时配置。

$ docker trust signer add --key ben.pub ben registry.example.com/admin/demo

Adding signer "ben" to registry.example.com/admin/demo...
Enter passphrase for repository key with ID b0014f8: 
Successfully added signer: ben to registry.example.com/admin/demo

检查以证明现在有 2 个委托 (Signer)。

$ docker trust inspect --pretty registry.example.com/admin/demo

No signatures for registry.example.com/admin/demo

List of signers and their keys for registry.example.com/admin/demo

SIGNER              KEYS
ben                 afa404703b25
jeff                1091060d7bfd

Administrative keys for registry.example.com/admin/demo

  Repository Key:	b0014f8e4863df2d028095b74efcb05d872c3591de0af06652944e310d96598d
  Root Key:	64d147e59e44870311dd2d80b9f7840039115ef3dfa5008127d769a5f657a5d7

向现有委托添加密钥

要支持密钥轮换和过期/停用密钥等操作,您可以发布 每个委托多个贡献者密钥。这里唯一的先决条件是使 在本例中,请确保您使用相同的 delegation name .Docker 信任 将自动处理将此新密钥添加到 .jefftargets/releases

注意

您将需要存储库密钥的密码;这本来是 在您首次启动存储库时配置。

$ docker trust signer add --key cert2.pem jeff registry.example.com/admin/demo

Adding signer "jeff" to registry.example.com/admin/demo...
Enter passphrase for repository key with ID b0014f8: 
Successfully added signer: jeff to registry.example.com/admin/demo

检查以证明委托 (Signer) 现在包含多个密钥 ID。

$ docker trust inspect --pretty registry.example.com/admin/demo

No signatures for registry.example.com/admin/demo


List of signers and their keys for registry.example.com/admin/demo

SIGNER              KEYS
jeff                1091060d7bfd, 5570b88df073

Administrative keys for registry.example.com/admin/demo

  Repository Key:	b0014f8e4863df2d028095b74efcb05d872c3591de0af06652944e310d96598d
  Root Key:	64d147e59e44870311dd2d80b9f7840039115ef3dfa5008127d769a5f657a5d7

删除委托

如果需要删除委托,包括 附加到角色后,您可以使用命令。targets/releases$ docker trust signer remove

注意

由已删除的委托签名的标记将需要重新签名 由活跃的代表团

$ docker trust signer remove ben registry.example.com/admin/demo
Removing signer "ben" from registry.example.com/admin/demo...
Enter passphrase for repository key with ID b0014f8: 
Successfully removed ben from registry.example.com/admin/demo

故障 排除

  1. 如果您看到 中没有可用键的错误,则 将需要使用 before 添加其他委托 重新签名镜像。targets/releasesdocker trust signer add

    WARN[0000] role targets/releases has fewer keys than its threshold of 1; it will not be usable until keys are added to it
  2. 如果您已添加其他委托并看到错误 消息中没有有效的签名,您将需要 以使用 Notary CLI 对委派文件进行签名。targest/releasestargets/releases

    WARN[0000] Error getting targets/releases: valid signatures did not meet threshold for targets/releases 

    使用命令$ notary witness

    $ notary witness registry.example.com/admin/demo targets/releases --publish
    

    有关该命令的更多信息,请参见 此处$ notary witness

从委托中删除贡献者的密钥

作为委派轮换密钥的一部分,您可能希望删除单个 key 但保留委托。这可以通过 Notary CLI 完成。

请记住,您必须从两个角色中删除密钥 以及特定于该签名者的角色。targets/releasestargets/<name>

  1. 我们需要从 Notary 服务器获取密钥 ID

    $ notary delegation list registry.example.com/admin/demo
    
    ROLE                PATHS             KEY IDS                                                             THRESHOLD
    ----                -----             -------                                                             ---------
    targets/jeff        "" <all paths>    8fb597cbaf196f0781628b2f52bff6b3912e4e8075720378fda60d17232bbcf9    1
                                          1091060d7bfd938dfa5be703fa057974f9322a4faef6f580334f3d6df44c02d1    
    targets/releases    "" <all paths>    8fb597cbaf196f0781628b2f52bff6b3912e4e8075720378fda60d17232bbcf9    1
                                          1091060d7bfd938dfa5be703fa057974f9322a4faef6f580334f3d6df44c02d1    
    
  2. 从委托中删除targets/releases

    $ notary delegation remove registry.example.com/admin/demo targets/releases 1091060d7bfd938dfa5be703fa057974f9322a4faef6f580334f3d6df44c02d1 --publish
    
    Auto-publishing changes to registry.example.com/admin/demo
    Enter username: admin
    Enter password: 
    Enter passphrase for targets key with ID b0014f8: 
    Successfully published changes for repository registry.example.com/admin/demo
    
  3. 从委托中删除targets/<name>

    $ notary delegation remove registry.example.com/admin/demo targets/jeff 1091060d7bfd938dfa5be703fa057974f9322a4faef6f580334f3d6df44c02d1 --publish
    
    Removal of delegation role targets/jeff with keys [5570b88df0736c468493247a07e235e35cf3641270c944d0e9e8899922fc6f99], to repository "registry.example.com/admin/demo" staged for next publish.
    
    Auto-publishing changes to registry.example.com/admin/demo
    Enter username: admin    
    Enter password: 
    Enter passphrase for targets key with ID b0014f8: 
    Successfully published changes for repository registry.example.com/admin/demo
    
  4. 检查剩余的委托列表

    $ notary delegation list registry.example.com/admin/demo
    
    ROLE                PATHS             KEY IDS                                                             THRESHOLD
    ----                -----             -------                                                             ---------
    targets/jeff        "" <all paths>    8fb597cbaf196f0781628b2f52bff6b3912e4e8075720378fda60d17232bbcf9    1    
    targets/releases    "" <all paths>    8fb597cbaf196f0781628b2f52bff6b3912e4e8075720378fda60d17232bbcf9    1    
    

删除本地委托私钥

作为轮换委托密钥的一部分,您可能需要删除本地委托 key 中。这是通过 Notary CLI 完成的,使用 命令。$ notary key remove

  1. 我们需要从本地 Docker Trust 存储中获取密钥 ID

    $ notary key list
    
    ROLE       GUN                          KEY ID                                                              LOCATION
    ----       ---                          ------                                                              --------
    root                                    f6c6a4b00fefd8751f86194c7d87a3bede444540eb3378c4a11ce10852ab1f96    /home/ubuntu/.docker/trust/private
    admin                                   8fb597cbaf196f0781628b2f52bff6b3912e4e8075720378fda60d17232bbcf9    /home/ubuntu/.docker/trust/private
    jeff                                    1091060d7bfd938dfa5be703fa057974f9322a4faef6f580334f3d6df44c02d1    /home/ubuntu/.docker/trust/private
    targets    ...example.com/admin/demo    c819f2eda8fba2810ec6a7f95f051c90276c87fddfc3039058856fad061c009d    /home/ubuntu/.docker/trust/private
    
  2. 从本地 Docker Trust 存储中删除密钥

    $ notary key remove 1091060d7bfd938dfa5be703fa057974f9322a4faef6f580334f3d6df44c02d1
    
    Are you sure you want to remove 1091060d7bfd938dfa5be703fa057974f9322a4faef6f580334f3d6df44c02d1 (role jeff) from /home/ubuntu/.docker/trust/private?  (yes/no)  y
    
    Deleted 1091060d7bfd938dfa5be703fa057974f9322a4faef6f580334f3d6df44c02d1 (role jeff) from /home/ubuntu/.docker/trust/private.
    

从仓库中删除所有信任数据

您可以从存储库中删除所有信任数据,包括 repository、target、 snapshot 和所有委托密钥。

这通常是容器注册表在特定存储库之前需要的 可以删除。

$ notary delete registry.example.com/admin/demo --remote

Deleting trust data for repository registry.example.com/admin/demo
Enter username: admin
Enter password: 
Successfully deleted local and remote trust data for repository registry.example.com/admin/demo

$ docker trust inspect --pretty registry.example.com/admin/demo

No signatures or cannot access registry.example.com/admin/demo