Azure 創建登入 Azure Container Registry (ACR) 使用者

在 Azure 上面我們想要創建多個使用者使用 ACR 可以在 Azure AD 建立 service principal 物件,然後指定 IAM 授權透過 Azure RBAC 如此 service principal 就可以對 ACR 進行 push 或是 pull

Azure 授權角色

在 Azure 的授權角色有以下幾種:

  • AcrPull: pull
  • AcrPush: push 與 pull
  • Owner: push 與 pull 並且可以 assign roles

創建 Service Principal 並且提供 ACR 權限的方法

創建方法可以直接使用 Bash 腳本

  1. 在使用前先登入 Azure
$ az login
The default web browser has been opened at https://login.microsoftonline.com/common/oauth2/authorize. Please continue the login in the web browser. If no web browser is available or if the web browser fails to open, use device code flow with `az login --use-device-code`.
You have logged in. Now let us find all the subscriptions to which you have access...
[
  {
    "cloudName": "AzureCloud",
    "homeTenantId": "00000000-0000-0000-0000-000000000000",
    "id": "00000000-0000-0000-0000-000000000000",
    "isDefault": true,
    "managedByTenants": [],
    "name": "Azure in Open",
    "state": "Enabled",
    "tenantId": "00000000-0000-0000-0000-000000000000",
    "user": {
      "name": "[email protected]",
      "type": "user"
    }
  }
]
  1. 確定 ACR 名稱與 service principal 名稱
    假設我有一個 Registry 名稱叫:ContainerRegistry 與指定 service principal 叫:acr-service-principal
  2. 撰寫創建腳本
    那就可以直接使用以下腳本進行創建 service principal 並且提供 push 與 pull 的權限, role 的地方可以修改權限可以使用 acrpull、acrpush 或是 owner,這邊以 acrpush 做完範例
#!/bin/bash
ACR_NAME=ContainerRegistry
SERVICE_PRINCIPAL_NAME=acr-service-principal

ACR_REGISTRY_ID=$(az acr show --name $ACR_NAME --query id --output tsv)

SP_PASSWD=$(az ad sp create-for-rbac --name $SERVICE_PRINCIPAL_NAME --scopes $ACR_REGISTRY_ID --role acrpush --query password --output tsv)
SP_APP_ID=$(az ad sp list --display-name $SERVICE_PRINCIPAL_NAME --query [].appId --output tsv)

echo "Service principal ID: $SP_APP_ID"
echo "Service principal password: $SP_PASSWD"

儲存之後使用 bash create-acr-principal.sh 執行

$ bash azure-add-principal.sh
Service principal ID: 00000000-0000-0000-0000-000000000000
Service principal password: <SP_PASSWD>
  1. 測試 Docker 登入
    測試完後可以使用 docker login 登入試試看
$ docker login containerregistry.azurecr.io --username 00000000-0000-0000-0000-000000000000 --password $SP_PASSWD
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Login Succeeded

以上就是使用獨立帳號進行權限切分的方法

已有 Service Principal 新增 ACR 權限的方法

如果已經擁有現有的 Service Principal 可以直接加入權限就好,減少一堆帳號的問題

  1. 撰寫創建腳本
    使用以下腳本新增權限,假設 Service Principal ID 為 00000000-0000-0000-0000-000000000000
#!/bin/bash

ACR_NAME=ContainerRegistry
SERVICE_PRINCIPAL_ID=00000000-0000-0000-0000-000000000000

ACR_REGISTRY_ID=$(az acr show --name $ACR_NAME --query id --output tsv)

az role assignment create --assignee $SERVICE_PRINCIPAL_ID --scope $ACR_REGISTRY_ID --role acrpush

儲存之後使用 bash add-acr-role.sh 執行

$ bash azure-add-principal.sh
{
  "canDelegate": null,
  "condition": null,
  "conditionVersion": null,
  "description": null,
  "id": "",
  "name": "00000000-0000-0000-0000-000000000000",
  "principalId": "00000000-0000-0000-0000-000000000000",
  "principalType": "ServicePrincipal",
  "resourceGroup": "ResourceGroup",
  "roleDefinitionId": "",
  "type": "Microsoft.Authorization/roleAssignments"
}

如此原本的 Service Principal 就擁有了 containerregistry.azurecr.io 的 push 權限了

參考資料

  • https://docs.microsoft.com/zh-tw/azure/container-registry/container-registry-auth-service-principal