本文是確保容器映像檔和其他 Open Container Initiative (OCI) 構件的完整性和真實性系列的一部分。 如需完整畫面,請從 概述開始,其中解釋了為什麼簽名很重要並概述了各種場景。
您可以在兩種情況下使用本指南:
- 取用已簽署的映像:驗證經其他小組或組織使用 Notation 和信任簽署進行簽署的容器映像。
- 驗證您自己的映像:如果您自己發佈映像,請先使用 GitHub 工作流程 或 Notation 命令列介面 (CLI) 簽署它們。 然後按照本指南驗證簽名。
在本文中,您將學會如何:
- 設定 GitHub 工作流程。
- 確認容器映像已使用受信任簽署和 GitHub Actions for Notation 進行簽署。
先決條件
- 包含已簽署映像的 容器登錄 。
- 用於儲存工作流程檔案、信任原則和信任存放區的 GitHub 儲存庫。
從 Azure 驗證到 GitHub
根據 [ 使用 GitHub 動作連線到 Azure],您必須先使用 Azure 登入動作在工作流程中向 Azure 進行驗證,才能執行 Azure CLI 或 Azure PowerShell 命令。 Azure 登入動作支援多種驗證方法。
在本指南中,您會使用 OpenID Connect (OIDC) 登入、使用使用者分配的受控身分識別,並遵循《透過 OpenID Connect 使用 Azure 登入動作》中的步驟。
建立使用者指派的受控識別。 如果您有現有的受控識別,請略過此步驟。
az login az identity create -g <identity-resource-group> -n <identity-name>
取得受控身份的用戶端識別碼:
CLIENT_ID=$(az identity show -g <identity-resource-group> -n <identity-name> --query clientId -o tsv)
將角色指派給受控識別,以存取 Azure Container Registry。
對於未啟用屬性型存取控制 (ABAC) 的登錄,請指派
AcrPull角色:ACR_SCOPE=/subscriptions/<subscription-id>/resourceGroups/<acr-resource-group> az role assignment create --assignee $CLIENT_ID --scope $ACR_SCOPE --role "acrpush" --role "acrpull"對於已啟用 ABAC 的登錄,請指派
Container Registry Repository Reader角色:ACR_SCOPE=/subscriptions/<subscription-id>/resourceGroups/<acr-resource-group> az role assignment create --assignee $CLIENT_ID --scope $ACR_SCOPE --role "Container Registry Repository Reader" --role "Container Registry Repository Writer"
設定 GitHub 以信任您的身分識別。 請遵循 設定使用者指派的受控識別以信任外部身分識別提供者。
遵循為存放庫建立秘密 (英文) 來建立 GitHub 秘密。
將受控識別值對應至這些秘密:
GitHub 祕密 管理的身分識別值 AZURE_CLIENT_ID用戶端識別碼 AZURE_SUBSCRIPTION_ID訂用帳戶識別碼 AZURE_TENANT_ID目錄 (租用戶) 識別碼
準備信任存放區和信任原則
要驗證需將 公證專案信任庫和信任政策 簽入您的儲存庫。
建立信任存放區
信任存放區 (.github/truststore/) 包含驗證所需的憑證管理中心 (CA) 憑證及時間戳記管理中心 (TSA) 根憑證。
下載 信任簽署根憑證 並將其儲存在目錄 ca 中:
curl -o .github/truststore/x509/ca/mycerts/msft-identity-verification-root-cert-2020.crt \
"https://www.microsoft.com/pkiops/certs/Microsoft%20Enterprise%20Identity%20Verification%20Root%20Certificate%20Authority%202020.crt"
下載 信任簽署 TSA 根憑證 ,並將其儲存在目錄 tsa 中:
curl -o .github/truststore/x509/tsa/mytsacerts/msft-identity-verification-tsa-root-cert-2020.crt \
"http://www.microsoft.com/pkiops/certs/microsoft%20identity%20verification%20root%20certificate%20authority%202020.crt"
建立信任原則
信任原則 (.github/trustpolicy/trustpolicy.json) 會定義哪些身分和 CA 是受信任的。
以下是 的範例 trustpolicy.json。 將存放庫 URI、信任存放區名稱和信任簽署憑證設定檔主體取代為您的值。
{
"version": "1.0",
"trustPolicies": [
{
"name": "mypolicy",
"registryScopes": [ "myregistry.azurecr.io/myrepo1","myregistry.azurecr.io/myrepo2" ],
"signatureVerification": {
"level" : "strict"
},
"trustStores": [ "ca:mycerts", "tsa:mytsacerts" ],
"trustedIdentities": [
"x509.subject: C=US, ST=WA, L=Seattle, O=MyCompany.io, OU=Tools"
]
}
]
}
確認目錄結構
您的存放庫應該如下所示:
.github/
├── trustpolicy/
│ └── trustpolicy.json
└── truststore/
└── x509/
├── ca/
│ └── mycerts/
│ └── msft-identity-verification-root-cert-2020.crt
└── tsa/
└── mytsacerts/
└── msft-identity-verification-tsa-root-cert-2020.crt
建立 GitHub Actions 工作流程
驗證和信任設定準備就緒時,請建立工作流程:
如果存放庫不存在,請在存放庫中建立
.github/workflows目錄。建立新的工作流程檔案;例如,
verify-with-trusted-signing.yml.將下列工作流程範本複製到您的檔案中。
展開以檢視驗證工作流程範本。
name: notation-verify-with-trusted-signing on: push: env: ACR_LOGIN_SERVER: <registry-name>.azurecr.io # example: myRegistry.azurecr.io ACR_REPO_NAME: <repository-name> # example: myRepo IMAGE_TAG: <image-tag> # example: v1 #IMAGE_DIGEST: <image-digest> # example: sha256:xxx jobs: notation-verify: runs-on: ubuntu-latest permissions: id-token: write contents: read steps: - name: Checkout uses: actions/checkout@v3 # Log in to Azure with your service principal secret # - name: Azure login # uses: Azure/login@v1 # with: # creds: ${{ secrets.AZURE_CREDENTIALS }} # If you're using OIDC and federated credentials, make sure to replace the preceding step with the following: - name: Azure login uses: Azure/login@v2 with: client-id: ${{ secrets.AZURE_CLIENT_ID }} tenant-id: ${{ secrets.AZURE_TENANT_ID }} subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} # Log in to your container registry - name: ACR login run: | az acr login --name ${{ env.ACR_LOGIN_SERVER }} # Set up the Notation CLI - name: setup notation uses: notaryproject/notation-action/setup@v1.2.2 # Verify the OCI artifact, such as container images - name: verify OCI artifact uses: notaryproject/notation-action/verify@v1 with: target_artifact_reference: ${{ env.ACR_LOGIN_SERVER }}/${{ env.ACR_REPO_NAME }}:${{ env.IMAGE_TAG }} # Alternatively, use the image digest # target_artifact_reference: ${{ env.ACR_LOGIN_SERVER }}/${{ env.ACR_REPO_NAME }}@${{ env.IMAGE_DIGEST }} trust_policy: .github/trustpolicy/trustpolicy.json trust_store: .github/truststore使用您自己的登錄、存放庫和映像標籤/摘要來更新環境變數。 儲存並提交檔案。
觸發 GitHub Actions 工作流程
推送動作會觸發範例的工作流程。 若要啟動工作,請將工作流程檔案提交至您的儲存庫。
您可以檢視工作流程日誌,以確認工作已順利完成。 例如,檢查是否已匯入信任原則、是否已從信任存放區載入憑證,以及是否已驗證簽章。