共用方式為


使用 Notation 和信任簽署來簽署容器映像(預覽版)

本文是確保容器映像檔和其他 Open Container Initiative (OCI) 構件的完整性和真實性系列的一部分。 如需完整畫面,請從 概述開始,其中解釋了為什麼簽名很重要並概述了各種場景。

本文著重於使用公證專案工具、標記法和 信任簽署進行簽署:

  • 您將在這裡學到什麼: 如何使用 Notation 命令列介面 (CLI) ,透過使用授信簽署來簽署構件。
  • 適合的位置:信任簽署是 Azure 金鑰保存庫的替代方案。 雖然 Key Vault 可讓組織完全控制憑證生命週期管理,但信任簽署透過零接觸憑證生命週期管理和短期憑證提供簡化的簽署體驗。
  • 爲何重要:可信簽名簡化了開發人員體驗,同時提供了強大的身份保證。 它幫助團隊在不影響安全性的情況下降低營運複雜性。

先決條件

您必須先設定必要的 Azure 資源,並安裝必要的工具,才能使用表示法和信任簽署來簽署和驗證容器映像。 本節會逐步引導您準備 Azure 容器登錄、設定信任簽署,以及將 Azure CLI 設定為您的開發環境。

備註

目前,信任簽署僅適用於位於美國和加拿大且具有三年或更長時間可驗證歷史記錄的組織。

在 Azure Container Registry 中準備容器映像

  1. 建立或使用 容器登錄 來儲存容器映像檔、OCI 成品和簽章。
  2. 在您的容器註冊表中推送或使用容器映像檔。

設定信任簽署

在 Azure 訂用帳戶中設定 受信任的簽署帳戶和憑證設定檔

您的憑證設定檔必須在憑證主體中包含國家/地區 (C)、州或省 (STS) 和組織 (O)。 公證專案規格需要這些欄位。

設定 Azure CLI

安裝 Azure CLI,或使用 Azure Cloud Shell

安裝 Notation CLI 和信任簽署外掛程式

本指南以 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
    

    發行頁面上尋找最新的外掛程式 URL 和總和檢查碼。

  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 Container Registry 和信任簽署的權限

為您的身分識別授與必要角色以存取 Azure Container Registry:

  • 對於啟用屬性型存取控制 (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:使用信任簽署支援的時間戳記伺服器。
  • --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
    

    如果驗證失敗,請確定您的信任原則和憑證已正確設定。