Skip to content

Commit c6d92c9

Browse files
authored
Merge pull request #211971 from tamram/tamram22-0921
update PSH/CLI samples for CMK/RBAC access model
2 parents 26450df + cd1d559 commit c6d92c9

6 files changed

+216
-199
lines changed

articles/storage/common/customer-managed-keys-configure-existing-account.md

Lines changed: 65 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ author: tamram
77

88
ms.service: storage
99
ms.topic: how-to
10-
ms.date: 08/31/2022
10+
ms.date: 09/29/2022
1111
ms.author: tamram
1212
ms.reviewer: ozgun
1313
ms.subservice: common
@@ -39,112 +39,70 @@ The managed identity that authorizes access to the key vault may be either a use
3939

4040
### Use a user-assigned managed identity to authorize access
4141

42-
A user-assigned is a standalone Azure resource. You must create the user-assigned identity before you configure customer-managed keys. To learn how to create and manage a user-assigned managed identity, see [Manage user-assigned managed identities](../../active-directory/managed-identities-azure-resources/how-manage-user-assigned-managed-identities.md).
43-
44-
#### [Azure portal](#tab/azure-portal)
45-
46-
When you configure customer-managed keys with the Azure portal, you can select an existing user-assigned identity through the portal user interface. For details, see [Configure customer-managed keys for an existing account](#configure-customer-managed-keys-for-an-existing-account).
47-
48-
#### [PowerShell](#tab/azure-powershell)
49-
50-
To authorize access to the key vault with a user-assigned managed identity, you'll need the resource ID and principal ID of the user-assigned managed identity. Call [Get-AzUserAssignedIdentity](/powershell/module/az.managedserviceidentity/get-azuserassignedidentity) to get the user-assigned managed identity and assign it to a variable that you'll reference in subsequent steps:
51-
52-
```azurepowershell
53-
$userIdentity = Get-AzUserAssignedIdentity -Name <user-assigned-identity> -ResourceGroupName <resource-group>
54-
$principalId = $userIdentity.PrincipalId
55-
```
56-
57-
#### [Azure CLI](#tab/azure-cli)
58-
59-
To authorize access to the key vault with a user-assigned managed identity, you'll need the resource ID and principal ID of the user-assigned managed identity. Call [az identity show](/cli/azure/identity#az-identity-show) command to get the user-assigned managed identity, then save the resource ID and principal ID to variables. You'll need these values in subsequent steps:
60-
61-
```azurecli
62-
userIdentityId=$(az identity show --name sample-user-assigned-identity --resource-group storagesamples-rg --query id)
63-
principalId=$(az identity show --name sample-user-assigned-identity --resource-group storagesamples-rg --query principalId)
64-
```
65-
66-
---
42+
[!INCLUDE [storage-customer-managed-keys-key-vault-user-assigned-identity-include](../../../includes/storage-customer-managed-keys-key-vault-user-assigned-identity-include.md)]
6743

6844
### Use a system-assigned managed identity to authorize access
6945

7046
A system-assigned managed identity is associated with an instance of an Azure service, in this case an Azure Storage account. You must explicitly assign a system-assigned managed identity to a storage account before you can use the system-assigned managed identity to authorize access to the key vault that contains your customer-managed key.
7147

7248
Only existing storage accounts can use a system-assigned identity to authorize access to the key vault. New storage accounts must use a user-assigned identity, if customer-managed keys are configured on account creation.
7349

50+
The system-assigned managed identity must have permissions to access the key in the key vault. Assign the **Key Vault Crypto Service Encryption User** role to the system-assigned managed identity with key vault scope to grant these permissions.
51+
7452
#### [Azure portal](#tab/azure-portal)
7553

76-
When you configure customer-managed keys with the Azure portal with a system-assigned managed identity, the system-assigned managed identity is assigned to the storage account for you under the covers. For details, see [Configure customer-managed keys for an existing account](#configure-customer-managed-keys-for-an-existing-account).
54+
Before you can configure customer-managed keys with a system-assigned managed identity, you must assign the **Key Vault Crypto Service Encryption User** role to the system-assigned managed identity, scoped to the key vault. This role grants the system-assigned managed identity permissions to access the key in the key vault. For more information on assigning Azure RBAC roles with the Azure portal, see [Assign Azure roles using the Azure portal](../../role-based-access-control/role-assignments-portal.md).
55+
56+
When you configure customer-managed keys with the Azure portal with a system-assigned managed identity, the system-assigned managed identity is assigned to the storage account for you under the covers.
7757

7858
#### [PowerShell](#tab/azure-powershell)
7959

80-
To assign a system-assigned managed identity to your storage account, call [Set-AzStorageAccount](/powershell/module/az.storage/set-azstorageaccount):
60+
To assign a system-assigned managed identity to your storage account, first call [Set-AzStorageAccount](/powershell/module/az.storage/set-azstorageaccount):
8161

8262
```azurepowershell
83-
$storageAccount = Set-AzStorageAccount -ResourceGroupName <resource_group> `
84-
-Name <storage-account> `
63+
$accountName = "<storage-account>"
64+
65+
$storageAccount = Set-AzStorageAccount -ResourceGroupName $rgName `
66+
-Name $accountName `
8567
-AssignIdentity
8668
```
8769

88-
Next, get the principal ID for the system-assigned managed identity, and save it to a variable. You'll need this value in the next step to create the key vault access policy:
70+
Next, assign to the system-assigned managed identity the required RBAC role, scoped to the key vault. Remember to replace the placeholder values in brackets with your own values and to use the variables defined in the previous examples:
8971

9072
```azurepowershell
9173
$principalId = $storageAccount.Identity.PrincipalId
74+
75+
New-AzRoleAssignment -ObjectId $storageAccount.Identity.PrincipalId `
76+
-RoleDefinitionName "Key Vault Crypto Service Encryption User" `
77+
-Scope $keyVault.ResourceId
9278
```
9379

9480
#### [Azure CLI](#tab/azure-cli)
9581

96-
To authenticate access to the key vault with a system-assigned managed identity, assign the system-assigned managed identity to the storage account by calling [az storage account update](/cli/azure/storage/account#az-storage-account-update):
82+
To authenticate access to the key vault with a system-assigned managed identity, first assign the system-assigned managed identity to the storage account by calling [az storage account update](/cli/azure/storage/account#az-storage-account-update):
9783

9884
```azurecli
85+
accountName="<storage-account>"
86+
9987
az storage account update \
100-
--name <storage-account> \
101-
--resource-group <resource_group> \
88+
--name $accountName \
89+
--resource-group $rgName \
10290
--assign-identity
10391
```
10492

105-
Next, get the principal ID for the system-assigned managed identity, and save it to a variable. You'll need this value in the next step to create the key vault access policy:
93+
Next, assign to the system-assigned managed identity the required RBAC role, scoped to the key vault. Remember to replace the placeholder values in brackets with your own values and to use the variables defined in the previous examples:
10694

10795
```azurecli
108-
principalId = $(az storage account show --name <storage-account> --resource-group <resource_group> --query identity.principalId)
109-
```
110-
111-
---
112-
113-
## Configure the key vault access policy
114-
115-
The next step is to configure the key vault access policy. The key vault access policy grants permissions to the managed identity that will be used to authorize access to the key vault. To learn more about key vault access policies, see [Azure Key Vault Overview](../../key-vault/general/overview.md#securely-store-secrets-and-keys) and [Azure Key Vault security overview](../../key-vault/general/security-features.md#key-vault-authentication-options).
116-
117-
### [Azure portal](#tab/azure-portal)
118-
119-
To learn how to configure the key vault access policy with the Azure portal, see [Assign an Azure Key Vault access policy](../../key-vault/general/assign-access-policy.md).
120-
121-
### [PowerShell](#tab/azure-powershell)
122-
123-
To configure the key vault access policy with PowerShell, call [Set-AzKeyVaultAccessPolicy](/powershell/module/az.keyvault/set-azkeyvaultaccesspolicy), providing the variable for the principal ID that you previously retrieved for the managed identity.
124-
125-
```azurepowershell
126-
Set-AzKeyVaultAccessPolicy `
127-
-VaultName $keyVault.VaultName `
128-
-ObjectId $principalId `
129-
-PermissionsToKeys wrapkey,unwrapkey,get
130-
```
131-
132-
To learn more about assigning the key vault access policy with PowerShell, see [Assign an Azure Key Vault access policy](../../key-vault/general/assign-access-policy.md).
133-
134-
### [Azure CLI](#tab/azure-cli)
135-
136-
To configure the key vault access policy with PowerShell, call [az keyvault set-policy](/cli/azure/keyvault#az-keyvault-set-policy), providing the variable for the principal ID that you previously retrieved for the managed identity.
96+
principalId=$(az storage account show --name $accountName \
97+
--resource-group $rgName \
98+
--query identity.principalId \
99+
--output tsv)
137100
138-
```azurecli
139-
az keyvault set-policy \
140-
--name <key-vault> \
141-
--resource-group <resource_group>
142-
--object-id $principalId \
143-
--key-permissions get unwrapKey wrapKey
101+
az role assignment create --assignee-object-id $principalId \
102+
--role "Key Vault Crypto Service Encryption User" \
103+
--scope $kvResourceId
144104
```
145105

146-
To learn more about assigning the key vault access policy with Azure CLI, see [Assign an Azure Key Vault access policy](../../key-vault/general/assign-access-policy.md).
147-
148106
---
149107

150108
## Configure customer-managed keys for an existing account
@@ -200,8 +158,10 @@ To configure customer-managed keys for an existing account with automatic updati
200158
Next, call [Set-AzStorageAccount](/powershell/module/az.storage/set-azstorageaccount) to update the storage account's encryption settings, omitting the key version. Include the **-KeyvaultEncryption** option to enable customer-managed keys for the storage account.
201159

202160
```azurepowershell
203-
Set-AzStorageAccount -ResourceGroupName <resource-group> `
204-
-AccountName <storage-account> `
161+
$accountName = "<storage-account>"
162+
163+
Set-AzStorageAccount -ResourceGroupName $rgName `
164+
-AccountName $accountName `
205165
-KeyvaultEncryption `
206166
-KeyName $key.Name `
207167
-KeyVaultUri $keyVault.VaultUri
@@ -214,17 +174,20 @@ To configure customer-managed keys for an existing account with automatic updati
214174
Next, call [az storage account update](/cli/azure/storage/account#az-storage-account-update) to update the storage account's encryption settings, omitting the key version. Include the `--encryption-key-source` parameter and set it to `Microsoft.Keyvault` to enable customer-managed keys for the account.
215175

216176
```azurecli
217-
key_vault_uri=$(az keyvault show \
218-
--name <key-vault> \
219-
--resource-group <resource_group> \
177+
accountName="<storage-account>"
178+
179+
keyVaultUri=$(az keyvault show \
180+
--name $kvName \
181+
--resource-group $rgName \
220182
--query properties.vaultUri \
221183
--output tsv)
222-
az storage account update
223-
--name <storage-account> \
224-
--resource-group <resource_group> \
225-
--encryption-key-name <key> \
184+
185+
az storage account update \
186+
--name $accountName \
187+
--resource-group $rgName \
188+
--encryption-key-name $keyName \
226189
--encryption-key-source Microsoft.Keyvault \
227-
--encryption-key-vault $key_vault_uri
190+
--encryption-key-vault $keyVaultUri
228191
```
229192

230193
---
@@ -258,8 +221,10 @@ To configure customer-managed keys with manual updating of the key version, expl
258221
Remember to replace the placeholder values in brackets with your own values and to use the variables defined in the previous examples.
259222

260223
```azurepowershell
261-
Set-AzStorageAccount -ResourceGroupName <resource-group> `
262-
-AccountName <storage-account> `
224+
$accountName = "<storage-account>"
225+
226+
Set-AzStorageAccount -ResourceGroupName $rgName `
227+
-AccountName $accountName `
263228
-KeyvaultEncryption `
264229
-KeyName $key.Name `
265230
-KeyVersion $key.Version `
@@ -275,23 +240,27 @@ To configure customer-managed keys with manual updating of the key version, expl
275240
Remember to replace the placeholder values in brackets with your own values.
276241

277242
```azurecli
278-
key_vault_uri=$(az keyvault show \
279-
--name <key-vault> \
280-
--resource-group <resource_group> \
243+
accountName="<storage-account>"
244+
245+
keyVaultUri=$(az keyvault show \
246+
--name $kvName \
247+
--resource-group $rgName \
281248
--query properties.vaultUri \
282249
--output tsv)
283-
key_version=$(az keyvault key list-versions \
284-
--name <key> \
285-
--vault-name <key-vault> \
250+
251+
keyVersion=$(az keyvault key list-versions \
252+
--name $keyName \
253+
--vault-name $kvName \
286254
--query [-1].kid \
287255
--output tsv | cut -d '/' -f 6)
288-
az storage account update
289-
--name <storage-account> \
290-
--resource-group <resource_group> \
291-
--encryption-key-name <key> \
292-
--encryption-key-version $key_version \
256+
257+
az storage account update \
258+
--name $accountName \
259+
--resource-group $rgName \
260+
--encryption-key-name $keyName \
261+
--encryption-key-version $keyVersion \
293262
--encryption-key-source Microsoft.Keyvault \
294-
--encryption-key-vault $key_vault_uri
263+
--encryption-key-vault $keyVaultUri
295264
```
296265

297266
When you manually update the key version, you'll need to update the storage account's encryption settings to use the new version. First, query for the key vault URI by calling [az keyvault show](/cli/azure/keyvault#az-keyvault-show), and for the key version by calling [az keyvault key list-versions](/cli/azure/keyvault/key#az-keyvault-key-list-versions). Then call [az storage account update](/cli/azure/storage/account#az-storage-account-update) to update the storage account's encryption settings to use the new version of the key, as shown in the previous example.

0 commit comments

Comments
 (0)