|
| 1 | +--- |
| 2 | +title: Configure customer-managed keys for your Azure Cosmos DB account with Azure Managed HSM Key Vault |
| 3 | +description: Learn how to configure customer-managed keys for your Azure Cosmos DB account with Azure Managed HSM Key Vault |
| 4 | +author: dileepraotv-github |
| 5 | +ms.service: cosmos-db |
| 6 | +ms.topic: how-to |
| 7 | +ms.date: 12/25/2022 |
| 8 | +ms.author: turao |
| 9 | +ms.custom: devx-track-azurepowershell, devx-track-azurecli, ignite-2022 |
| 10 | +ms.devlang: azurecli |
| 11 | +--- |
| 12 | + |
| 13 | +# Configure customer-managed keys for your Azure Cosmos DB account with Azure Managed HSM Key Vault |
| 14 | + |
| 15 | +[!INCLUDE[NoSQL, MongoDB, Cassandra, Gremlin, Table](includes/appliesto-nosql-mongodb-cassandra-gremlin-table.md)] |
| 16 | + |
| 17 | +Please refer to link [Configure customer-managed keys with Azure Key Vault](./how-to-setup-customer-managed-keys.md) |
| 18 | + |
| 19 | +> [!NOTE] |
| 20 | +> Currently, customer-managed keys are available only for new Azure Cosmos DB accounts. You should configure them during account creation. |
| 21 | +
|
| 22 | +## <a id="register-resource-provider"></a> Register the Azure Cosmos DB resource provider for your Azure subscription |
| 23 | + |
| 24 | +1. Sign in to the [Azure portal](https://portal.azure.com/), go to your Azure subscription, and select **Resource providers** under the **Settings** tab: |
| 25 | + |
| 26 | + :::image type="content" source="./media/how-to-setup-cmk-mhsm/navigation-resource-providers.png" alt-text="Screenshot of the Resource providers option in the resource navigation menu."::: |
| 27 | + |
| 28 | +1. Search for the **Microsoft.DocumentDB** resource provider. Verify if the resource provider is already marked as registered. If not, choose the resource provider and select **Register**: |
| 29 | + |
| 30 | + :::image type="content" source="media/how-to-setup-cmk-mhsm/resource-provider-registration.png" lightbox="media/how-to-setup-cmk-mhsm/resource-provider-registration.png" alt-text="Screenshot of the Register option for the Microsoft.DocumentDB resource provider."::: |
| 31 | + |
| 32 | +## Configure your Azure Managed HSM Key Vault |
| 33 | + |
| 34 | +Using customer-managed keys with Azure Cosmos DB requires you to set two properties on the Azure Key Vault instance that you plan to use to host your encryption keys: **Soft Delete** and **Purge Protection**. |
| 35 | + |
| 36 | +Because soft delete is turned on by default, only purge protection must be enabled. When creating your managed HSM, use the following CLI command: |
| 37 | + |
| 38 | + |
| 39 | +```azurecli-interactive |
| 40 | +objectId = az ad signed-in-user show --query id -o tsv |
| 41 | +az keyvault create --hsm-name $hsmName --resource-group $rgName --location $location --enable-purge-protection true --administrators $objectId --retention-days 7 |
| 42 | +
|
| 43 | +``` |
| 44 | + |
| 45 | +If you're using an existing Azure Managed HSM Key Vault instance, you can verify that these properties are enabled by looking at the **Properties** section with the following command: |
| 46 | + |
| 47 | +```azurecli-interactive |
| 48 | +az keyvault show $hsmName $rgName |
| 49 | +
|
| 50 | +``` |
| 51 | + |
| 52 | +If purge protection isn't enabled, the following command can be used: |
| 53 | + |
| 54 | +```azurecli-interactive |
| 55 | +az keyvault update-hsm --enable-purge-protection true --hsm-name $hsmName --resource-group $rgName |
| 56 | +
|
| 57 | +``` |
| 58 | + |
| 59 | +For more information about the CLI commands available for managed HSM, refer to the following [Azure Key Vault](../key-vault/general/overview.md) |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +## Creating the encryption key and assigning the correspondent roles |
| 64 | + |
| 65 | +Once the Managed HSM [has been activated,](../key-vault/managed-hsm/quick-create-cli.md#activate-your-managed-hsm) |
| 66 | + the key that is going to be used for the CMK account needs to be created. For this, the role “Managed HSM Crypto User” is assigned to the administrator. To read more about how RBAC (role based access control) works with Managed HSM, refer to the following articles: [Managed HSM local RBAC built-in roles - Azure Key Vault | Microsoft Learn](../key-vault/managed-hsm/built-in-roles.md) and [Azure Managed HSM access control | Microsoft Learn](../key-vault/managed-hsm/access-control.md) |
| 67 | + |
| 68 | +```azurecli-interactive |
| 69 | +objectId = az ad signed-in-user show --query id -o tsv |
| 70 | +$keyName = "Name of your key" |
| 71 | +az keyvault role assignment create --hsm-name $hsmName --role "Managed HSM Crypto User" --assignee $objectId --scope /keys |
| 72 | +az keyvault key create --hsm-name $hsmName --name $keyName --ops wrapKey unwrapKey --kty RSA-HSM --size 3072 |
| 73 | +
|
| 74 | +``` |
| 75 | +Now that the key has been created, the correspondent role needs to be assigned to either the Cosmos DB Principal ID or the Azure Managed Identity for provisioning the account. The role “Managed HSM Crypto Service Encryption User” is used because it has the only three permissions needed to work with a CMK account, being: get, wrap and unwrap. These permissions are also scoped to only be useful on the keys stored on the Azure Managed HSM. |
| 76 | + |
| 77 | +Without Azure managed identity: |
| 78 | + |
| 79 | +```azurecli-interactive |
| 80 | +$cosmosPrincipal = az ad sp show --id a232010e-820c-4083-83bb-3ace5fc29d0b --query id -o tsv |
| 81 | +az keyvault role assignment create --hsm-name $hsmName --role "Managed HSM Crypto Service Encryption User" --assignee $cosmosPrincipal --scope /keys |
| 82 | +$keyURI = "https://{0}.managedhsm.azure.net/keys/{1}" -f $hsmName, $keyName |
| 83 | +az cosmosdb create -n $cosmosName -g $rgName --key-uri $keyURI |
| 84 | +
|
| 85 | +``` |
| 86 | +With Azure managed identity: |
| 87 | + |
| 88 | +```azurecli-interactive |
| 89 | +$identityResourceID = az identity show -g $rgName -n $identityName --query id -o tsv |
| 90 | +$identityPrincipal = az identity show -g $rgName -n $identityName --query principalId -o tsv |
| 91 | +$defaultIdentity = "UserAssignedIdentity={0}" -f $identityResourceID |
| 92 | +az keyvault role assignment create --hsm-name $hsmName --role "Managed HSM Crypto Service Encryption User" --assignee $cosmosPrincipal --scope /keys |
| 93 | +$keyURI = "https://{0}.managedhsm.azure.net/keys/{1}" -f $hsmName, $keyName |
| 94 | +az cosmosdb create -n $cosmosName -g $rgName --key-uri $keyURI --assign-identity $identityResourceID --default-identity $defaultIdentity |
| 95 | +
|
| 96 | +``` |
| 97 | +This will provision a Cosmos DB CMK account with a key stored on an Azure Managed HSM Key Vault. |
| 98 | + |
| 99 | +## Switching to system assigned managed identity. |
| 100 | + |
| 101 | +Cosmos DB supports the use of a system assigned managed identity for a CMK Cosmos DB account. For more information about system assigned managed identity CMK, refer to: [Configure customer-managed keys for your Azure Cosmos DB account](./how-to-setup-customer-managed-keys.md) |
| 102 | + |
| 103 | +Execute the following commands to switch from default identity to system assigned managed identity: |
| 104 | + |
| 105 | +```azurecli-interactive |
| 106 | +az cosmosdb identity assign -n $cosmosName -g $rgName |
| 107 | +$principalMSIId = az cosmosdb identity show -n $cosmosName -g $rgName --query principalId -o tsv |
| 108 | +az keyvault role assignment create --hsm-name $hsmName --role "Managed HSM Crypto Service Encryption User" --assignee $principalMSIId --scope /keys |
| 109 | +az cosmosdb update --resource-group $rgName --name $cosmosName --default-identity "SystemAssignedIdentity" |
| 110 | +
|
| 111 | +``` |
| 112 | +As an optional note, the original role assignment to Cosmos DB’s principal ID or Azure Managed Identity can be removed. |
| 113 | + |
| 114 | +```azurecli-interactive |
| 115 | +az keyvault role assignment delete --hsm-name $hsmName --role "Managed HSM Crypto Service Encryption User" --assignee $cosmosPrincipal --scope /keys |
| 116 | +
|
| 117 | +``` |
| 118 | + |
| 119 | +## Next steps |
| 120 | + |
| 121 | +- Learn more about [data encryption in Azure Cosmos DB](./database-encryption-at-rest.md). |
| 122 | +- Get an overview of [secure access to data in Azure Cosmos DB](secure-access-to-data.md). |
0 commit comments