教程:使用 Key Vault 密钥将数据加密到媒体服务帐户

Media Services logo v3


警告

Azure 媒体服务将于 2024 年 6 月 30 日停用。 有关详细信息,请参阅 AMS 停用指南

注意

托管标识仅适用于使用 v3 API 创建的媒体服务帐户。 如果你使用的是 v2 API,并且想要使用托管标识,请从 v2 迁移到 v3 关于从媒体服务 v2 迁移到 v3 的介绍

如果希望媒体服务使用 Key Vault 中的密钥来加密数据,必须向媒体服务帐户授予访问 Key Vault 的权限。 按照下列步骤为媒体服务帐户创建一个托管标识,然后使用媒体服务 CLI 向该标识授予访问 Key Vault 的权限。

Media Services account uses Key Vault with a Managed Identity

本教程使用 2020-05-01 媒体服务 API。

登录 Azure

要使用本文中的任何命令,首先必须登录到要使用的订阅。

登录 Azure。 使用此命令时,系统会提示你输入想要使用的订阅。

az login

设置订阅

使用此命令设置要使用的订阅。

使用 CLI 设置 Azure 订阅

在以下命令中,为媒体服务帐户提供想要使用的 Azure 订阅 ID。

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、Premium_LRS。

  • 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 用于加密媒体数据。 你将使用 your-keyvault-name 来创建密钥并供后续步骤使用。

使用以下命令创建 Key Vault 和密钥。 将 your-resource-group-nameyour-keyvault-nameyour-key-name 更改为你要使用的值。 该命令假定你已创建了资源组。

注意

--bypass AzureServices 允许媒体服务(和其他 Azure 服务)访问 Key Vault(在该访问通常会被 Key Vault 网络 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.cn/"
  },
  "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.cn/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
}

向媒体服务系统分配的托管标识授予访问 Key Vault 的权限

向媒体服务托管标识授予访问 Key Vault 的权限。 有两个命令:

获取(显示)媒体服务帐户的托管标识

下面的第一个命令显示媒体服务帐户的托管标识,它是命令返回的 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 策略

第二个命令向主体 ID 授予访问 Key Vault 的权限。 将 object-id 设置为你在上一步中获得的 principalId 的值。

若要使用此命令,必须将媒体服务 principalId 作为 object-id 包括在内。 请使用 az ams account show --name <your-media-services-account-name> --resource-group <your-resource-group> 获取此 ID(如果尚未这样做)。

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.cn/"
  },
  "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.cn/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.cn/"
  },
  "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>