教學課程:使用金鑰保存庫金鑰將資料加密至媒體服務帳戶

媒體服務標誌 v3


警告

Azure 媒體服務將於 2024 年 6 月 30 日淘汰。 如需詳細資訊,請參閱 AMS淘汰指南

注意

受控識別僅適用於使用 v3 API 建立的媒體服務帳戶。 如果您使用 v2 API 且想要使用受控識別,請從 v2 移轉至 v3從 v2 媒體服務 v2 移轉至 v3 簡介

如果您想要媒體服務從金鑰保存庫使用金鑰加密資料,則必須將金鑰保存庫的存取權授與媒體服務帳戶。 請遵循以下步驟,為媒體服務帳戶建立受控識別,並使用媒體服務 CLI 將此身分識別存取權授與其金鑰保存庫。

媒體服務帳戶使用 Key Vault 搭配受控識別

本教學課程使用 2020-05-01 媒體服務 API。

登入 Azure

若要使用本文中的任何命令,請先登入要使用的訂閱。

登入 Azure。 當您使用此命令時,系統會提示您輸入要使用的訂閱。

az login

設定訂用帳戶

使用此命令設定要使用的訂閱。

使用 CLI 設定 Azure 訂用帳戶

在下列命令中,提供您要用於媒體服務帳戶的 Azure 訂用帳戶識別碼。

az account set --subscription <subscriptionName>

資源名稱

開始前,請決定您要建立的資源名稱。 這些名稱應該可以輕鬆識別為集合,尤其當您不打算在完成測試後使用這些名稱。 許多資源類型的命名規則都不同,因此最好保持全部小寫。 例如,「mediatest1rg」用於資源群組名稱,而「mediatest1stor」用於儲存體帳戶名稱。 針對本文中每個步驟使用相同的名稱。

您會在下列命令中看到這些參考的名稱。 您需要的資源名稱如下:

  • myRG
  • myStorageAccount
  • myAmsAccount
  • myKeyVault
  • myKey
  • location

注意

上方連字號僅用於分隔指引字組。 由於 Azure 服務中的命名資源不一致,命名資源時請勿使用連字號。 此外,您不會建立區域名稱。 區域名稱是由 Azure 決定。

列出 Azure 區域

如果您不確定要使用的實際區域名稱,請使用此命令取得清單:

使用此命令列出帳戶的可用區域。

az account list-locations --query "[].{DisplayName:displayName, Name:name}" -o table

順序

下方每個步驟會以特定順序完成,因為序列中的下一個步驟會使用來自 JSON 回應的一或多個值。

建立儲存體帳戶

您要建立的媒體服務帳戶必須有相關的儲存體帳戶。 請先建立媒體服務帳戶的儲存體帳戶。 您會將 your-storage-account-name 用於後續步驟。

使用 CLI 建立 Azure 記憶體帳戶

使用下列命令來建立 Azure 記憶體帳戶。

若要建立記憶體帳戶,您必須先在位置內建立資源群組。

若要列出可用的位置,請使用下列命令:

使用 CLI 列出可用的位置

若要列出可用的位置,請使用下列命令:

az account list-locations

使用 CLI 建立資源群組

若要建立資源群組,請使用下列命令:

az group create -n <resourceGroupName> --location chooseLocation

選擇 SKU

您也需要選擇記憶體帳戶的 SKU。 您可以列出記憶體帳戶。

從下列清單中選擇 SKU:Standard_LRS、Standard_GRS、Standard_RAGRS、Standard_ZRS、Premium_LRS、Premium_ZRS、Standard_GZRS Standard_RAGZRS。

  • 變更 myStorageAccount 為長度少於 24 個字元的唯一名稱。
  • 變更 chooseLocation 為您想要在其中工作的區域。
  • 變更 chooseSKU 為您慣用的 SKU。
az storage account create -n <myStorageAccount> -g <resourceGroup>  --location <chooseLocation> --sku <chooseSKU>

建立包含服務主體 (受控識別) 的媒體服務帳戶

接著建立包含服務主體 (或稱為受控識別) 的媒體服務帳戶。

重要

請務必記得在命令中使用 --mi 旗標。 否則後續步驟時,您將無法找到 principalId

以下 Azure CLI 命令會建立新的媒體服務帳戶。 以您要使用的名稱取代下列值:your-media-services-account-nameyour-storage-account-nameyour-resource-group-name。 此命令假設您已建立資源群組與儲存體帳戶。

命令會為媒體服務帳戶提供具有 --mi-system-assigned 旗標的系統指派受控識別。


az ams account create --name <your-media-services-account-name> --resource-group <your-resource-group-name> --mi-system-assigned --storage-account <your-storage-account-name>

範例 JSON 回應:

{
  "encryption": {
    "keyVaultProperties": null,
    "type": "SystemKey"
  },
  "id": "/subscriptions/00000000-0000-0000-0000-00000000/resourceGroups/your-resource-group/providers/Microsoft.Media/mediaservices/your-media-services-account-name",
  "identity": {
    "principalId": "00000000-0000-0000-0000-00000000",
    "tenantId": "00000000-0000-0000-0000-00000000",
    "type": "SystemAssigned"
  },
  "location": "your-region",
  "mediaServiceId": "00000000-0000-0000-0000-00000000",
  "name": "your-media-services-account-name",
  "resourceGroup": "your-resource-group",
  "storageAccounts": [
    {
      "id": "/subscriptions/00000000-0000-0000-0000-00000000/resourceGroups/mediatest1rg/providers/Microsoft.Storage/storageAccounts/your-storage-account-name",
      "resourceGroup": "your-resource-group",
      "type": "Primary"
    }
  ],
  "storageAuthentication": "System",
  "systemData": {
    "createdAt": "2021-05-14T21:25:12.3492071Z",
    "createdBy": "you@example.com",
    "createdByType": "User",
    "lastModifiedAt": "2021-05-14T21:25:12.3492071Z",
    "lastModifiedBy": "you@example.com",
    "lastModifiedByType": "User"
  },
  "tags": null,
  "type": "Microsoft.Media/mediaservices"
}

建立金鑰保存庫

建立金鑰保存庫。 金鑰保存庫是用來加密媒體資料。 您將使用 your-keyvault-name 來建立金鑰,並用於後續步驟。

使用下列命令來建立金鑰保存庫和金鑰。 將 your-resource-group-nameyour-keyvault-nameyour-key-name 變更為您想要使用的值。 此命令假設您已建立資源群組。

注意

--bypass AzureServices 允許媒體服務 (和其他 Azure 服務) 存取金鑰保存庫,該存取通常是由金鑰保存庫網路 ACL 封鎖。--enable-purge-protection如果未設定,您將無法使用金鑰。

建立 Key Vault


az keyvault create --resource-group <your-resource-group-name> --bypass AzureServices --enable-purge-protection --name <your-keyvault-name>

範例 JSON 回應:

{
  "id": "/subscriptions/the-subscription-id/resourceGroups/your-resource-group-name/providers/Microsoft.KeyVault/vaults/your-keyvault-name",
  "location": "your-region",
  "name": "your-keyvault-name",
  "properties": {
    "accessPolicies": [
      {
        "applicationId": null,
        "objectId": "00000000-0000-0000-0000-000000000000",
        "permissions": {
          "certificates": [
            "get",
            "list",
            "delete",
            "create",
            "import",
            "update",
            "managecontacts",
            "getissuers",
            "listissuers",
            "setissuers",
            "deleteissuers",
            "manageissuers",
            "recover"
          ],
          "keys": [
            "get",
            "create",
            "delete",
            "list",
            "update",
            "import",
            "backup",
            "restore",
            "recover"
          ],
          "secrets": [
            "get",
            "list",
            "set",
            "delete",
            "backup",
            "restore",
            "recover"
          ],
          "storage": [
            "get",
            "list",
            "delete",
            "set",
            "update",
            "regeneratekey",
            "setsas",
            "listsas",
            "getsas",
            "deletesas"
          ]
        },
        "tenantId": "the-tenant-id"
      }
    ],
    "createMode": null,
    "enablePurgeProtection": true,
    "enableRbacAuthorization": null,
    "enableSoftDelete": true,
    "enabledForDeployment": false,
    "enabledForDiskEncryption": null,
    "enabledForTemplateDeployment": null,
    "networkAcls": null,
    "privateEndpointConnections": null,
    "provisioningState": "Succeeded",
    "sku": {
      "name": "standard"
    },
    "softDeleteRetentionInDays": 90,
    "tenantId": "the-tenant-id",
    "vaultUri": "https://your-keyvault-name.vault.azure.net/"
  },
  "resourceGroup": "your-resource-group-name",
  "tags": {},
  "type": "Microsoft.KeyVault/vaults"
}

建立金鑰

az keyvault key create --kty RSA --name your-key-name --vault-name your-keyvault-name

範例 JSON 回應:


{
  "attributes": {
    "created": "2021-05-12T22:41:29+00:00",
    "enabled": true,
    "expires": null,
    "notBefore": null,
    "recoveryLevel": "Recoverable",
    "updated": "2021-05-12T22:41:29+00:00"
  },
  "key": {
    "crv": null,
    "d": null,
    "dp": null,
    "dq": null,
    "e": "AQAB",
    "k": null,
    "keyOps": [
      "encrypt",
      "decrypt",
      "sign",
      "verify",
      "wrapKey",
      "unwrapKey"
    ],
    "kid": "https://your-keyvault-name.vault.azure.net/keys/your-key-name/your-subsription-id",
    "kty": "RSA",
    "n": "THISISTHEKEY51V9thvU7KsBUo/q1mEOcuxqt0qUcnx0IRO9YCL32fPjD/nnS8hKS5qkgUKfe2NRAtzVQ+elQAha65l7OsHu+TXmH/n/RPCgstpqSdCfiUR1JTmFYFRWdxCPwoKJMYaqlCEhn2Dkon3StTN0Id0sjRSA/YOLjgWU7YnVbntg5/048HgcTKn3PCWCuJc+P8hI/8Os5EAIpun62PffYwPX0/NIA1PY8wIB+sYEY0zxVGwWrCu7VgCo9xeqbMQEq5OenYmYpc+cjLozU/ohGhfWTpQU8d7fFypTHQraENDOFKEY",
    "p": null,
    "q": null,
    "qi": null,
    "t": null,
    "x": null,
    "y": null
  },
  "managed": null,
  "tags": null
}

將媒體服務系統指派的受控識別存取權授與金鑰保存庫

將媒體服務受控識別存取權授與金鑰保存庫。 有兩個命令:

取得 (顯示) 媒體服務帳戶的受控識別

下方第一個命令顯示媒體服務帳戶的受控識別,即命令傳回 JSON 中列出的 principalId

此命令會顯示媒體服務帳戶的所有屬性。

az ams account show --name <your-media-services-account-name> --resource-group <your-resource-group>

注意

如果您已指派存取角色給媒體服務帳戶,這一行會傳回 "storageAuthentication": "ManagedIdentity"

範例 JSON 回應:

{
  "encryption": {
    "keyVaultProperties": null,
    "type": "SystemKey"
  },
  "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/your-resource-group-name/providers/Microsoft.Media/mediaservices/your-media-services-account",
  "identity": {
    "principalId": "00000000-0000-0000-0000-000000000000",
    "tenantId": "00000000-0000-0000-0000-000000000000",
    "type": "SystemAssigned"  //Type will show "Managed Identity" if you have assigned a role to the Media Services account.
  },
  "location": "your-region",
  "mediaServiceId": "00000000-0000-0000-0000-000000000000",
  "name": "your-media-services-account",
  "resourceGroup": "your-resource-group-name",
  "storageAccounts": [
    {
      "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/your-resource-group-name/providers/Microsoft.Storage/storageAccounts/your-storage-account-name",
      "resourceGroup": "your-resource-group-name",
      "type": "Primary"
    }
  ],
  "storageAuthentication": "System", //If you have assigned access roles to the account, this line will return storageAuthentication": "ManagedIdentity"
  "systemData": {
    "createdAt": "2021-05-14T21:25:12.3492071Z",
    "createdBy": "you@example.com",
    "createdByType": "User",
    "lastModifiedAt": "2021-05-14T21:25:12.3492071Z",
    "lastModifiedBy": "you@example.com",
    "lastModifiedByType": "User"
  },
  "tags": null,
  "type": "Microsoft.Media/mediaservices"
}

設定金鑰保存庫原則

第二個命令會將主體識別碼存取權授與金鑰保存庫。 將 object-id 設定為您從上一個步驟取得的值 principalId

若要使用此命令,您必須將媒體服務 principalId 包含為 object-id。 如果您尚未這麼做,請使用 az ams account show --name <your-media-services-account-name> --resource-group <your-resource-group> 來取得此識別碼。

az keyvault set-policy --name <your-keyvault-name> --object-id <principalId> --key-permissions decrypt encrypt get list unwrapKey wrapKey

範例 JSON 回應:

{
  "id": "/subscriptions/00000000-0000-0000-000000000000/resourceGroups/your-resource-group-name/providers/Microsoft.KeyVault/vaults/your-keyvault-name",
  "location": "your-region",
  "name": "your-keyvault-name",
  "properties": {
    "accessPolicies": [
      {
        "applicationId": null,
        "objectId": "00000000-0000-0000-000000000000",
        "permissions": {
          "certificates": [
            "get",
            "list",
            "delete",
            "create",
            "import",
            "update",
            "managecontacts",
            "getissuers",
            "listissuers",
            "setissuers",
            "deleteissuers",
            "manageissuers",
            "recover"
          ],
          "keys": [
            "get",
            "create",
            "delete",
            "list",
            "update",
            "import",
            "backup",
            "restore",
            "recover"
          ],
          "secrets": [
            "get",
            "list",
            "set",
            "delete",
            "backup",
            "restore",
            "recover"
          ],
          "storage": [
            "get",
            "list",
            "delete",
            "set",
            "update",
            "regeneratekey",
            "setsas",
            "listsas",
            "getsas",
            "deletesas"
          ]
        },
        "tenantId": "00000000-0000-0000-000000000000"
      },
      {
        "applicationId": null,
        "objectId": "00000000-0000-0000-000000000000",
        "permissions": {
          "certificates": null,
          "keys": [
            "encrypt",
            "get",
            "list",
            "wrapKey",
            "decrypt",
            "unwrapKey"
          ],
          "secrets": null,
          "storage": null
        },
        "tenantId": "00000000-0000-0000-000000000000"
      }
    ],
    "createMode": null,
    "enablePurgeProtection": true,
    "enableRbacAuthorization": null,
    "enableSoftDelete": true,
    "enabledForDeployment": false,
    "enabledForDiskEncryption": null,
    "enabledForTemplateDeployment": null,
    "networkAcls": null,
    "privateEndpointConnections": null,
    "provisioningState": "Succeeded",
    "sku": {
      "name": "standard"
    },
    "softDeleteRetentionInDays": 90,
    "tenantId": "00000000-0000-0000-000000000000",
    "vaultUri": "https://your-keyvault-name.vault.azure.net/"
  },
  "resourceGroup": "your-resource-group-name",
  "tags": {},
  "type": "Microsoft.KeyVault/vaults"
}

將媒體服務設定為使用來自金鑰保存庫 的金鑰

將媒體服務設定為使用您已建立的金鑰。 key-identifier 屬性的值來自於建立金鑰時的輸出。 此命令可能會因為傳播存取控制變更所需的時間而失敗。 如果發生這種情況,請在幾分鐘後重試。

若要使用此命令,您必須已建立金鑰保存庫和金鑰。

az ams account encryption set --account-name <your-media-services-account-name> --resource-group <your-resource-group> --key-type CustomerKey --key-identifier https://<your-keyvault-name>.vault.azure.net/keys/<your-key-name>

範例 JSON 回應:

{
  "id": "/subscriptions/00000000-0000-0000-000000000000/resourceGroups/your-resource-group-name/providers/Microsoft.KeyVault/vaults/your-keyvault-name",
  "location": "your-region",
  "name": "your-keyvault-name",
  "properties": {
    "accessPolicies": [
      {
        "applicationId": null,
        "objectId": "00000000-0000-0000-000000000000",
        "permissions": {
          "certificates": [
            "get",
            "list",
            "delete",
            "create",
            "import",
            "update",
            "managecontacts",
            "getissuers",
            "listissuers",
            "setissuers",
            "deleteissuers",
            "manageissuers",
            "recover"
          ],
          "keys": [
            "get",
            "create",
            "delete",
            "list",
            "update",
            "import",
            "backup",
            "restore",
            "recover"
          ],
          "secrets": [
            "get",
            "list",
            "set",
            "delete",
            "backup",
            "restore",
            "recover"
          ],
          "storage": [
            "get",
            "list",
            "delete",
            "set",
            "update",
            "regeneratekey",
            "setsas",
            "listsas",
            "getsas",
            "deletesas"
          ]
        },
        "tenantId": "the-tenant-id"
      },
      {
        "applicationId": null,
        "objectId": "the-media-services-account-id",
        "permissions": {
          "certificates": null,
          "keys": [
            "encrypt",
            "get",
            "list",
            "wrapKey",
            "decrypt",
            "unwrapKey"
          ],
          "secrets": null,
          "storage": null
        },
        "tenantId": "the-tenant-id"
      }
    ],
    "createMode": null,
    "enablePurgeProtection": true,
    "enableRbacAuthorization": null,
    "enableSoftDelete": true,
    "enabledForDeployment": false,
    "enabledForDiskEncryption": null,
    "enabledForTemplateDeployment": null,
    "networkAcls": null,
    "privateEndpointConnections": null,
    "provisioningState": "Succeeded",
    "sku": {
      "name": "standard"
    },
    "softDeleteRetentionInDays": 90,
    "tenantId": "the-tenant-id",
    "vaultUri": "https://your-keyvault-name.vault.azure.net/"
  },
  "resourceGroup": "your-resource-group-name",
  "tags": {},
  "type": "Microsoft.KeyVault/vaults"
}

驗證

若要使用客戶自控金鑰確認帳戶已加密,請檢視帳戶加密屬性:

使用 CLI 顯示帳戶加密

如需此命令的詳細資訊,請參閱媒體服務 CLI 參考

type 屬性應該會顯示 CustomerKey,且 currentKeyIdentifier 應該設定為客戶金鑰保存庫中金鑰的路徑。

清除資源

如果您不打算使用所建立的資源,請刪除資源群組。

使用 CLI 刪除資源群組

az group delete --name <your-resource-group-name>

取得說明及支援

您可以連絡媒體服務並詢問問題,或依照下列其中一種方法追蹤我們的更新: