你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 Notation 工具和受信任签名对容器映像进行签名(预览版)

本文是一系列关于确保容器映像和其他开放容器计划(OCI)项目的完整性和真实性的一部分。 对于完整情况,请从概述开始,该 概述解释了签名为何重要并概述了各种方案。

本文重点介绍如何使用 Notary Project 工具、Notation 和受信任签名进行签名:

  • 此处将介绍的内容:如何使用 Notation 命令行接口(CLI)利用受信任的签名对工件进行签名。
  • 适合的位置:受信任的签名是 Azure Key Vault 的替代方法。 尽管 Key Vault 使组织能够完全控制证书生命周期管理,但受信任的签名提供了简化的签名体验,包括零接触证书生命周期管理和生存期较短的证书。
  • 为什么很重要:受信任的签名简化了开发人员体验,同时提供强大的标识保证。 它可帮助团队在不损害安全性的情况下降低运营复杂性。

先决条件

在使用表示法和受信任的签名对容器映像进行签名和验证之前,需要设置所需的 Azure 资源并安装必要的工具。 本部分将指导你准备 Azure 容器注册表、配置受信任的签名,以及将 Azure CLI 设置为开发环境。

注释

目前,受信任的签名仅适用于在美国和加拿大拥有三年或以上可验证历史记录的组织。

在 Azure 容器注册表中准备容器映像

  1. 创建或使用 容器注册表 来存储容器映像、OCI 项目和签名。
  2. 在您的容器注册表中上传或使用容器镜像。

设置受信任的签名

在 Azure 订阅中设置 受信任的签名帐户和证书配置文件

证书配置文件必须在证书主题中包含国家/地区(C)、州或省(STS)和组织(O)。 公证项目规范需要这些字段。

设置 Azure CLI

安装 Azure CLI,或使用 Azure Cloud Shell

安装 Notation 命令行界面和可信签名插件

本指南以示例的形式在 Linux AMD64 和 Windows 上运行命令。

  1. 安装 Notation CLI v1.3.2:

    curl -Lo notation.tar.gz https://github.com/notaryproject/notation/releases/download/v1.3.2/notation_1.3.2_linux_amd64.tar.gz
    # Validate the checksum
    EXPECTED_SHA256SUM="e1a0f060308086bf8020b2d31defb7c5348f133ca0dba6a1a7820ef3cbb6dfe5"
    echo "$EXPECTED_SHA256SUM  notation.tar.gz" | sha256sum -c -
    # Continue if sha256sum matches
    tar xvzf notation.tar.gz
    cp ./notation /usr/local/bin
    

    有关其他平台,请参阅 Notation 安装指南

  2. 安装受信任的签名插件:

    notation plugin install --url "https://github.com/Azure/trustedsigning-notation-plugin/releases/download/v1.0.0-beta.1/notation-azure-trustedsigning_1.0.0-beta.1_linux_amd64.tar.gz" --sha256sum 538b497be0f0b4c6ced99eceb2be16f1c4b8e3d7c451357a52aeeca6751ccb44
    

    请在发布页上查找最新插件的网址和校验和。

  3. 验证插件安装:

    notation plugin ls
    

    示例输出:

    NAME                   DESCRIPTION                                            VERSION   CAPABILITIES                ERROR
    azure-trustedsigning   Sign OCI artifacts using the Trusted Signing Service   0.3.0     [SIGNATURE_GENERATOR.RAW]   <nil>
    

配置环境变量

设置以下环境变量,以便在后续命令中使用。 将占位符替换为你的实际值。

可以在 Azure 门户中找到所需的值:

  • 对于受信任签名帐户信息,请转到帐户,然后选择“概述”
  • 有关证书配置文件信息,请转到帐户,然后选择“ 对象>证书配置文件”。
# Trusted Signing environment variables
TS_SUB_ID="<subscription-id>"
TS_ACCT_RG=<ts-account-resource-group>
TS_ACCT_NAME=<ts-account-name>
TS_ACCT_URL=<ts-account-url>
TS_CERT_PROFILE=<ts-cert-profile>
TS_CERT_SUBJECT=<ts-cert-subject>
TS_SIGNING_ROOT_CERT="https://www.microsoft.com/pkiops/certs/Microsoft%20Enterprise%20Identity%20Verification%20Root%20Certificate%20Authority%202020.crt"
TS_TSA_URL="http://timestamp.acs.microsoft.com/"
TS_TSA_ROOT_CERT="http://www.microsoft.com/pkiops/certs/microsoft%20identity%20verification%20root%20certificate%20authority%202020.crt"

# Azure Container Registry and image environment variables
ACR_SUB_ID="<acr-subscription-id>"
ACR_RG=<acr-resource-group>
ACR_NAME=<registry-name>
ACR_LOGIN_SERVER=$ACR_NAME.azurecr.io
REPOSITORY=<repository>
TAG=<tag>
IMAGE=$ACR_LOGIN_SERVER/${REPOSITORY}:$TAG

登录到 Azure

使用 Azure CLI 使用用户标识登录:

az login
USER_ID=$(az ad signed-in-user show --query id -o tsv)

注释

本指南演示如何使用用户帐户登录。 有关其他标识选项(包括托管标识)请参阅 使用 Azure CLI 向 Azure 进行身份验证

为 Azure 容器注册表和受信任的签名分配权限

向标识授予访问容器注册表所需的角色:

  • 对于启用了基于属性的访问控制(ABAC)的注册表,请分配:
    • Container Registry Repository Reader
    • Container Registry Repository Writer
  • 对于非 ABAC 注册表,应分配:
    • AcrPull
    • AcrPush
az role assignment create --role "Container Registry Repository Reader" --assignee $USER_ID --scope "/subscriptions/$ACR_SUB_ID/resourceGroups/$ACR_RG/providers/Microsoft.ContainerRegistry/registries/$ACR_NAME"
az role assignment create --role "Container Registry Repository Writer" --assignee $USER_ID --scope "/subscriptions/$ACR_SUB_ID/resourceGroups/$ACR_RG/providers/Microsoft.ContainerRegistry/registries/$ACR_NAME"

将角色 Trusted Signing Certificate Profile Signer 分配给标识,以便可以使用受信任签名进行签名:

az role assignment create --assignee $USER_ID --role "Trusted Signing Certificate Profile Signer" --scope "/subscriptions/$TS_SUB_ID/resourceGroups/$TS_ACCT_RG/providers/Microsoft.CodeSigning/codeSigningAccounts/$TS_ACCT_NAME/certificateProfiles/$TS_CERT_PROFILE"

对容器映像进行签名

# Authenticate to Azure Container Registry
az acr login --name $ACR_NAME

# Download the timestamping root certificate
curl -o msft-tsa-root-certificate-authority-2020.crt $TS_TSA_ROOT_CERT

# Sign the image
notation sign --signature-format cose --timestamp-url $TS_TSA_URL --timestamp-root-cert "msft-tsa-root-certificate-authority-2020.crt" --id $TS_CERT_PROFILE --plugin azure-trustedsigning --plugin-config accountName=$TS_ACCT_NAME --plugin-config baseUrl=$TS_ACCT_URL --plugin-config certProfile=$TS_CERT_PROFILE $IMAGE

关键标志说明:

  • --signature-format cose:对签名使用 CBOR 对象签名和加密 (COSE) 格式。
  • --timestamp-url:使用 Trusted Signing 支持的时间戳服务器。
  • --plugin-config:将配置传递给受信任的签名插件。

列出签名的图像和签名:

notation ls $IMAGE

示例输出:

myregistry.azurecr.io/myrepo@sha256:5d0bf1e8f5a0c74a4c22d8c0f962a7cfa06a4f9d8423b196e482df8af23b5d55
└── application/vnd.cncf.notary.signature
    └── sha256:d3a4c9fbc17e27b19a0b28e7b6a33f2c0f541dbdf8d2e5e8d0d79a835e8a76f2a

验证容器映像

  1. 下载并添加根证书:

    curl -o msft-root-certificate-authority-2020.crt $TS_SIGNING_ROOT_CERT
    SIGNING_TRUST_STORE="myRootCerts"
    notation cert add --type ca --store $SIGNING_TRUST_STORE msft-root-certificate-authority-2020.crt
    
    curl -o msft-tsa-root-certificate-authority-2020.crt $TS_TSA_ROOT_CERT
    TSA_TRUST_STORE="myTsaRootCerts"
    notation cert add -t tsa -s $TSA_TRUST_STORE msft-tsa-root-certificate-authority-2020.crt
    notation cert ls
    

  1. 创建信任策略 JSON 文件:

    cat <<EOF > trustpolicy.json
    {
        "version": "1.0",
        "trustPolicies": [
            {
                "name": "myPolicy",
                "registryScopes": [ "$ACR_LOGIN_SERVER/$REPOSITORY" ],
                "signatureVerification": {
                    "level" : "strict"
                },
                "trustStores": [ "ca:$SIGNING_TRUST_STORE", "tsa:$TSA_TRUST_STORE" ],
                "trustedIdentities": [
                    "x509.subject: $TS_CERT_SUBJECT"
                ]
            }
        ]
    }
    EOF
    

    导入并检查策略:

    notation policy import trustpolicy.json
    notation policy show
    

  1. 验证映像:

    notation verify $IMAGE
    

    示例输出:

    Successfully verified signature for myregistry.azurecr.io/myrepo@sha256:5d0bf1e8f5a0c74a4c22d8c0f962a7cfa06a4f9d8423b196e482df8af23b5d55
    

    如果验证失败,请确保正确配置了信任策略和证书。