共用方式為


教學課程:使用 Key Vault 金鑰將數據加密至媒體服務帳戶

媒體服務標誌 v3


警告

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

注意

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

如果您想要媒體服務使用 Key Vault 中的金鑰加密數據,則必須將媒體服務帳戶授與 金鑰保存庫 存取權。 請遵循下列步驟來建立媒體服務帳戶的受控識別,並使用媒體服務 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
  • 位置

注意

上述連字元僅用於分隔指引字組。 由於 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"
}

建立 Key Vault

建立 Key Vault。 Key Vault 用來加密媒體數據。 您將使用 your-keyvault-name 來建立金鑰,以及後續步驟。

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

注意

--bypass AzureServices 可讓媒體服務(和其他 Azure 服務)在金鑰保存庫網路 ACL 通常會封鎖該存取權時存取 Key Vault。如果未設定 --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
}

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

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

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

下列第一個命令顯示媒體服務帳戶的受控識別,這是命令所傳回 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"
}

設定 Key Vault 原則

第二個命令會授與密鑰保存庫的主體標識碼存取權。 將 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 Vault 中的金鑰

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

若要使用此命令,您必須已經建立 Key Vault 和密鑰。

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 應設定為客戶 Key Vault 中密鑰的路徑。

清除資源

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

使用 CLI 刪除資源群組

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

取得說明和支援

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