Share via


在 ACR 工作中使用 Azure 受控識別進行跨登錄驗證

ACR 工作中,您可以啟用 Azure 資源的受控識別。 工作可以使用身分識別存取其他 Azure 資源,不需要提供或管理認證。

在本文中,您將了解如何在工作中啟用受控識別,從不同於用來執行工作的登錄中提取映像。

若要建立 Azure,本文需要您執行 Azure CLI 2.0.68 版或更新版本。 執行 az --version 以尋找版本。 如果您需要安裝或升級,請參閱安裝 Azure CLI

案例概觀

範例工作會從另一個 Azure 容器登錄提取基礎映像,以建置及推送應用程式映像。 若要提取基礎映像,您可以使用受控識別來設定工作,並為其指派適當的權限。

這個範例會示範使用使用者指派或系統指派受控識別的步驟。 您所選擇的身分識別取決於貴組織的需求。

在真實世界案例中,組織可能會維護一組可供所有開發小組用來建置其應用程式的基礎映像。 這些基礎映像會儲存在公司登錄中,每個開發小組僅具有「提取」權限。

必要條件

在本文中,您需要兩個 Azure 容器登錄:

  • 您可以使用第一個登錄來建立及執行 ACR 工作。 在本文中,此登錄名為 myregistry
  • 第二個登錄會裝載用來建置映像的基礎映像。 在本文中,第二個登錄名為 mybaseregistry

請在稍後的步驟中,以您自己的登錄名稱取代。

如果您還沒有所需的 Azure 容器登錄,請參閱快速入門:使用 Azure CLI 建立私人容器登錄。 您還不需要將映像推送至登錄。

準備基礎登錄

為了示範目的,請執行 [az acr import]、[az-acr-import],從 Docker Hub 將公用 Node.js 映像匯入您的基礎登錄,作為一次性作業。 實際上,組織的另一個小組或程序可能維護基礎登錄中的映像。

az acr import --name mybaseregistry \
  --source docker.io/library/node:15-alpine \
  --image baseimages/node:15-alpine 

定義 YAML 檔案中的工作步驟

多步驟工作這個範例的步驟是在 YAML 檔案中進行定義。 在您的本機工作目錄中建立名為 helloworldtask.yaml 的檔案,並貼上下列內容。 使用基礎登錄的伺服器名稱,更新組建步驟中的 REGISTRY_NAME 值。

version: v1.1.0
steps:
# Replace mybaseregistry with the name of your registry containing the base image
  - build: -t $Registry/hello-world:$ID  https://github.com/Azure-Samples/acr-build-helloworld-node.git#main -f Dockerfile-app --build-arg REGISTRY_NAME=mybaseregistry.azurecr.io
  - push: ["$Registry/hello-world:$ID"]

組建步驟會使用 Azure-Samples/acr-build-helloworld-node 存放庫中的 Dockerfile-app 檔案來建置映像。 --build-arg 會參考基礎登錄來提取基礎映像。 成功建置後,會將映像推送至用來執行工作的登錄。

選項 1:使用使用者指派的身分識別來建立工作

本節中的步驟可建立工作,並啟用使用者指派的身分識別。 如果您想要改為啟用系統指派的身分識別,請參閱選項 2:使用系統指派的身分識別來建立工作

建立使用者指派的身分識別

使用 az identity create 命令,在您的訂用帳戶中建立名為 myACRTasksId 的身分識別。 您可以使用先前用來建立容器登錄的相同資源群組,或使用不同的資源群組。

az identity create \
  --resource-group myResourceGroup \
  --name myACRTasksId

若要在下列步驟中設定使用者指派的身分識別,請使用 az identity show 命令,以將身分識別的資源識別碼、主體識別碼與用戶端識別碼儲存在變數中。

# Get resource ID of the user-assigned identity
resourceID=$(az identity show \
  --resource-group myResourceGroup \
  --name myACRTasksId \
  --query id --output tsv)

# Get principal ID of the task's user-assigned identity
principalID=$(az identity show \
  --resource-group myResourceGroup \
  --name myACRTasksId \
  --query principalId --output tsv)

# Get client ID of the user-assigned identity
clientID=$(az identity show \
  --resource-group myResourceGroup \
  --name myACRTasksId \
  --query clientId --output tsv)

建立工作

執行下列 az acr task create 命令來建立 helloworldtask 工作。 此工作會在沒有原始程式碼內容的情況下執行,而命令會參考工作目錄中的 helloworldtask.yaml 檔案。 --assign-identity 參數會傳遞使用者指派身分識別的資源識別碼。

az acr task create \
  --registry myregistry \
  --name helloworldtask \
  --context /dev/null \
  --file helloworldtask.yaml \
  --assign-identity $resourceID

在命令輸出中,identity 區段會顯示 UserAssigned 類型的身分識別已在工作中設定:

[...]
"identity": {
    "principalId": null,
    "tenantId": null,
    "type": "UserAssigned",
    "userAssignedIdentities": {
      "/subscriptions/xxxxxxxx-d12e-4760-9ab6-xxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myACRTasksId": {
        "clientId": "xxxxxxxx-f17e-4768-bb4e-xxxxxxxxxxxx",
        "principalId": "xxxxxxxx-1335-433d-bb6c-xxxxxxxxxxxx"
      }
[...]

將身分識別提取權限授與基礎登錄

在本節中,授與要從基礎登錄提取的受控識別權限,mybaseregistry

使用 az acr show 命令來取得基礎登錄的資源識別碼,並將其儲存在變數中:

baseregID=$(az acr show --name mybaseregistry --query id --output tsv)

請使用 az role assignment create 命令,將身分識別的 acrpull 角色指派給基礎登錄。 此角色只有從登錄提取映像的權限。

az role assignment create \
  --assignee $principalID \
  --scope $baseregID \
  --role acrpull

新增目標登錄認證至工作的程序。

選項 2:使用系統指派的身分識別來建立工作

本節中的步驟可建立工作,並啟用系統指派的身分識別。 如果您想要改為啟用使用者指派的身分識別,請參閱選項 1:使用使用者指派的身分識別來建立工作

建立工作

執行下列 az acr task create 命令來建立 helloworldtask 工作。 此工作會在沒有原始程式碼內容的情況下執行,而命令會參考工作目錄中的 helloworldtask.yaml 檔案。 不含值的 --assign-identity 參數可在工作上啟用系統指派的身分識別。

az acr task create \
  --registry myregistry \
  --name helloworldtask \
  --context /dev/null \
  --file helloworldtask.yaml \
  --assign-identity 

在命令輸出中,identity 區段會顯示 SystemAssigned 類型的身分識別已在工作中設定。 principalId 是工作身分識別的主體識別碼:

[...]
  "identity": {
    "principalId": "xxxxxxxx-2703-42f9-97d0-xxxxxxxxxxxx",
    "tenantId": "xxxxxxxx-86f1-41af-91ab-xxxxxxxxxxxx",
    "type": "SystemAssigned",
    "userAssignedIdentities": null
  },
  "location": "eastus",
[...]

使用 az acr task show 命令將 principalId 儲存在變數中,以便在稍後的命令中使用。 在下列命令中,取代您的工作名稱和登錄:

principalID=$(az acr task show \
  --name <task_name> --registry <registry_name> \
  --query identity.principalId --output tsv)

將身分識別提取權限授與基礎登錄

在本節中,授與要從基礎登錄提取的受控識別權限,mybaseregistry

使用 az acr show 命令來取得基礎登錄的資源識別碼,並將其儲存在變數中:

baseregID=$(az acr show --name mybaseregistry --query id --output tsv)

請使用 az role assignment create 命令,將身分識別的 acrpull 角色指派給基礎登錄。 此角色只有從登錄提取映像的權限。

az role assignment create \
  --assignee $principalID \
  --scope $baseregID \
  --role acrpull

將目標登錄認證新增至工作

現在,使用 az acr task credential add 命令,讓工作能夠使用身分識別的認證向基礎登錄進行驗證。 執行對應於您在工作中所啟用受控識別類型的命令。 如果您已啟用使用者指派的身分識別,請以身分識別的用戶端識別碼來傳遞 --use-identity。 如果您已啟用系統指派的身分識別,請傳遞 --use-identity [system]

# Add credentials for user-assigned identity to the task
az acr task credential add \
  --name helloworldtask \
  --registry myregistry \
  --login-server mybaseregistry.azurecr.io \
  --use-identity $clientID

# Add credentials for system-assigned identity to the task
az acr task credential add \
  --name helloworldtask \
  --registry myregistry \
  --login-server mybaseregistry.azurecr.io \
  --use-identity [system]

手動執行工作

若要確認您已啟用受控識別的工作成功執行,請使用 az acr task run 命令來手動觸發工作。

az acr task run \
  --name helloworldtask \
  --registry myregistry

如果工作成功執行,則輸出會類似於:

Queued a run with ID: cf10
Waiting for an agent...
2019/06/14 22:47:32 Using acb_vol_dbfbe232-fd76-4ca3-bd4a-687e84cb4ce2 as the home volume
2019/06/14 22:47:39 Creating Docker network: acb_default_network, driver: 'bridge'
2019/06/14 22:47:40 Successfully set up Docker network: acb_default_network
2019/06/14 22:47:40 Setting up Docker configuration...
2019/06/14 22:47:41 Successfully set up Docker configuration
2019/06/14 22:47:41 Logging in to registry: myregistry.azurecr.io
2019/06/14 22:47:42 Successfully logged into myregistry.azurecr.io
2019/06/14 22:47:42 Logging in to registry: mybaseregistry.azurecr.io
2019/06/14 22:47:43 Successfully logged into mybaseregistry.azurecr.io
2019/06/14 22:47:43 Executing step ID: acb_step_0. Timeout(sec): 600, Working directory: '', Network: 'acb_default_network'
2019/06/14 22:47:43 Scanning for dependencies...
2019/06/14 22:47:45 Successfully scanned dependencies
2019/06/14 22:47:45 Launching container with name: acb_step_0
Sending build context to Docker daemon   25.6kB
Step 1/6 : ARG REGISTRY_NAME
Step 2/6 : FROM ${REGISTRY_NAME}/baseimages/node:15-alpine
15-alpine: Pulling from baseimages/node
[...]
Successfully built 41b49a112663
Successfully tagged myregistry.azurecr.io/hello-world:cf10
2019/06/14 22:47:56 Successfully executed container: acb_step_0
2019/06/14 22:47:56 Executing step ID: acb_step_1. Timeout(sec): 600, Working directory: '', Network: 'acb_default_network'
2019/06/14 22:47:56 Pushing image: myregistry.azurecr.io/hello-world:cf10, attempt 1
The push refers to repository [myregistry.azurecr.io/hello-world]
[...]
2019/06/14 22:48:00 Step ID: acb_step_1 marked as successful (elapsed time in seconds: 2.517011)
2019/06/14 22:48:00 The following dependencies were found:
2019/06/14 22:48:00
- image:
    registry: myregistry.azurecr.io
    repository: hello-world
    tag: cf10
    digest: sha256:611cf6e3ae3cb99b23fadcd89fa144e18aa1b1c9171ad4a0da4b62b31b4e38d1
  runtime-dependency:
    registry: mybaseregistry.azurecr.io
    repository: baseimages/node
    tag: 15-alpine
    digest: sha256:e8e92cffd464fce3be9a3eefd1b65dc9cbe2484da31c11e813a4effc6105c00f
  git:
    git-head-revision: 0f988779c97fe0bfc7f2f74b88531617f4421643

Run ID: cf10 was successful after 32s

執行 az acr repository show-tags 命令,以確認建置映像並成功加以推送至 myregistry

az acr repository show-tags --name myregistry --repository hello-world --output tsv

範例輸出︰

cf10

下一步