You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Manage storage account keys with Key Vault and Azure PowerShell
14
+
15
+
An Azure storage account uses credentials comprising an account name and a key. The key is autogenerated and serves as a password, rather than an as a cryptographic key. Key Vault manages storage account keys by storing them as [Key Vault secrets](/azure/key-vault/about-keys-secrets-and-certificates#key-vault-secrets).
16
+
17
+
You can use the Key Vault managed storage account key feature to list (sync) keys with an Azure storage account, and regenerate (rotate) the keys periodically. You can manage keys for both storage accounts and Classic storage accounts.
18
+
19
+
When you use the managed storage account key feature, consider the following points:
13
20
14
-
> [!NOTE]
15
-
> [Azure storage integration with Azure Active Directory (Azure AD) is now in preview](../storage/common/storage-auth-aad.md). We recommend using Azure AD for authentication and authorization, which provides OAuth2 token-based access to Azure storage, just like Azure Key Vault. This allows you to:
16
-
> - Authenticate your client application using an application or user identity, instead of storage account credentials.
17
-
> - Use an [Azure AD managed identity](/azure/active-directory/managed-identities-azure-resources/) when running on Azure. Managed identities remove the need for client authentication all together, and storing credentials in or with your application.
18
-
> - Use Role Based Access Control (RBAC) for managing authorization, which is also supported by Key Vault.
19
-
> - AAD access to Storage Account does not work for accessing tables as of yet.
21
+
- Key values are never returned in response to a caller.
22
+
- Only Key Vault should manage your storage account keys. Don't manage the keys yourself and avoid interfering with Key Vault processes.
23
+
- Only a single Key Vault object should manage storage account keys. Don't allow key management from multiple objects.
24
+
- You can request Key Vault to manage your storage account with a user principal, but not with a service principal.
25
+
- Regenerate keys by using Key Vault only. Don't manually regenerate your storage account keys.
26
+
27
+
We recommend using Azure Storage integration with Azure Active Directory (Azure AD), Microsoft's cloud-based identity and access management service. Azure AD integration is available for [Azure blobs and queues](../storage/common/storage-auth-aad.md), and provides OAuth2 token-based access to Azure Storage (just like Azure Key Vault).
28
+
29
+
Azure AD allows you to authenticate your client application by using an application or user identity, instead of storage account credentials. You can use an [Azure AD managed identity](/azure/active-directory/managed-identities-azure-resources/) when you run on Azure. Managed identities remove the need for client authentication and storing credentials in or with your application.
30
+
31
+
Azure AD uses role-based access control (RBAC) to manage authorization, which is also supported by Key Vault.
An [Azure storage account](/azure/storage/storage-create-storage-account) uses a credential that consists of an account name and a key. The key is autogenerated, and serves more as a "password" as opposed to a cryptographic key. Key Vault can manage these storage account keys, by storing them as [Key Vault secrets](/azure/key-vault/about-keys-secrets-and-certificates#key-vault-secrets).
35
+
## Service principal application ID
36
+
37
+
An Azure AD tenant provides each registered application with a [service principal](/azure/active-directory/develop/developer-glossary#service-principal-object). The service principal serves as the application ID, which is used during authorization setup for access to other Azure resources via RBAC.
24
38
25
-
## Overview
39
+
Key Vault is a Microsoft application that's pre-registered in all Azure AD tenants. Key Vault is registered under the same Application ID in each Azure cloud.
26
40
27
-
The Key Vault managed storage account feature performs several management functions on your behalf:
41
+
| Tenants | Cloud | Application ID |
42
+
| --- | --- | --- |
43
+
| Azure AD | Azure Government |`7e7c393b-45d0-48b1-a35e-2905ddf8183c`|
44
+
| Azure AD | Azure public |`cfa8b339-82a2-471a-a3c9-0fc0be7a4093`|
45
+
| Other | Any |`cfa8b339-82a2-471a-a3c9-0fc0be7a4093`|
28
46
29
-
- Lists (syncs) keys with an Azure storage account.
30
-
- Regenerates (rotates) the keys periodically.
31
-
- Manages keys for both storage accounts and Classic storage accounts.
32
-
- Key values are never returned in response to caller.
47
+
## Prerequisites
33
48
34
-
When you use the managed storage account key feature:
49
+
To complete this guide, you must first do the following:
35
50
36
-
-**Only allow Key Vault to manage your storage account keys.** Don't attempt to manage them yourself, as you'll interfere with Key Vault's processes.
37
-
-**Don't allow storage account keys to be managed by more than one Key Vault object**.
38
-
-**Don't manually regenerate your storage account keys**. We recommend that you regenerate them via Key Vault.
51
+
-[Install the Azure PowerShell module](/powershell/azure/install-az-ps?view=azps-2.6.0).
52
+
-[Create a key vault](quick-create-powershell.md)
53
+
-[Create an Azure storage account](../storage/common/storage-quickstart-create-account.md?tabs=azure-powershell). The storage account name must use only lowercase letters and numbers. The length of the name must be between 3 and 24 characters.
54
+
39
55
40
-
The following example shows you how to allow Key Vault to manage your storage account keys.
56
+
## Manage storage account keys
41
57
42
-
## Connect to your Azure account
58
+
###Connect to your Azure account
43
59
44
60
Authenticate your PowerShell session using the [Connect-AzAccount](/powershell/module/az.accounts/connect-azaccount?view=azps-2.5.0) cmdlet.
61
+
45
62
```azurepowershell-interactive
46
63
Connect-AzAccount
47
64
```
@@ -51,31 +68,33 @@ If you have multiple Azure subscriptions, you can list them using the [Get-AzSub
51
68
Set-AzContext -SubscriptionId <subscriptionId>
52
69
```
53
70
54
-
## Authorize Key Vault to access to your storage account
55
-
56
-
> [!IMPORTANT]
57
-
> An Azure AD tenant provides each registered application with a **[service principal](/azure/active-directory/develop/developer-glossary#service-principal-object)**, which serves as the application's identity. The service principal's Application ID is used when giving it authorization to access other Azure resources, through role-based access control (RBAC). Because Key Vault is a Microsoft application, it's pre-registered in all Azure AD tenants under the same Application ID, within each Azure cloud:
58
-
> - Azure AD tenants in Azure government cloud use Application ID `7e7c393b-45d0-48b1-a35e-2905ddf8183c`.
59
-
> - Azure AD tenants in Azure public cloud and all others use Application ID `cfa8b339-82a2-471a-a3c9-0fc0be7a4093`.
71
+
### Set variables
60
72
61
-
Before Key Vault can access and manage your storage account keys, you must authorize its access your storage account. The Key Vault application requires permissions to *list*and *regenerate* keys for your storage account. These permissions are enabled through the built-in RBAC role [Storage Account Key Operator Service Role](/azure/role-based-access-control/built-in-roles#storage-account-key-operator-service-role).
73
+
First, set the variables to be used by the PowerShell cmdlets in the following steps. Be sure to update the <YourResourceGroupName>, <YourStorageAccountName>, and <YourKeyVaultName> placeholders, and set $keyVaultSpAppId to `cfa8b339-82a2-471a-a3c9-0fc0be7a4093` (as specified in [Service principal application ID](#service-principal-application-id), above).
62
74
63
-
Assign this role to the Key Vault service principal, limiting scope to your storage account, using the following steps. Be sure to update the `$resourceGroupName`, `$storageAccountName`, `$storageAccountKey`, and `$keyVaultName` variables before you run the script:
75
+
We will also use the Azure PowerShell [Get-AzContext](/powershell/module/az.accounts/get-azcontext?view=azps-2.6.0) and [Get-AzStorageAccount](/powershell/module/az.storage/get-azstorageaccount?view=azps-2.6.0) cmdlets to get your user ID and the context of your Azure storage account.
64
76
65
77
```azurepowershell-interactive
66
-
# TODO: Update with the resource group where your storage account resides, your storage account name, the name of your active storage account key, and your Key Vault instance name
Before Key Vault can access and manage your storage account keys, you must authorize its access your storage account. The Key Vault application requires permissions to *list* and *regenerate* keys for your storage account. These permissions are enabled through the built-in RBAC role [Storage Account Key Operator Service Role](/azure/role-based-access-control/built-in-roles#storage-account-key-operator-service-role).
78
94
95
+
Assign this role to the Key Vault service principal, limiting scope to your storage account, using the Azure PowerShell [New-AzRoleAssignment](/powershell/module/az.resources/new-azroleassignment?view=azps-2.6.0) cmdlet.
96
+
97
+
```azurepowershell-interactive
79
98
# Assign RBAC role "Storage Account Key Operator Service Role" to Key Vault, limiting the access scope to your storage account. For a classic storage account, use "Classic Storage Account Key Operator Service Role."
If Key Vault has already been added to the role on your storage account, you'll receive a *"The role assignment already exists."* error. You can also verify the role assignment, using the storage account "Access control (IAM)" page in the Azure portal.
98
117
99
-
## Give your user account permission to managed storage accounts
118
+
###Give your user account permission to managed storage accounts
100
119
101
-
>[!TIP]
102
-
> Just as Azure AD provides a **service principal** for an application's identity, a **user principal** is provided for a user's identity. The user principal can then be given authorization to access Key Vault, through Key Vault access policy permissions.
103
-
104
-
Using the same PowerShell session, update the Key Vault access policy for managed storage accounts. This step applies storage account permissions to your user account, ensuring that you can access the managed storage account features:
120
+
Use the Azure PowerShell [Set-AzKeyVaultAccessPolicy](/powershell/module/az.keyvault/set-azkeyvaultaccesspolicy?view=azps-2.6.0) cmdlet to update the Key Vault access policy and grant storage account permissions to your user account.
105
121
106
122
```azurepowershell-interactive
107
123
# Give your user principal access to all storage account permissions, on your Key Vault instance
Note that permissions for storage accounts aren't available on the storage account "Access policies" page in the Azure portal.
113
129
114
-
## Add a managed storage account to your Key Vault instance
130
+
###Add a managed storage account to your Key Vault instance
115
131
116
-
Using the same PowerShell session, create a managed storage account in your Key Vault instance. The `-DisableAutoRegenerateKey` switch specifies NOT to regenerate the storage account keys.
132
+
Use the Azure PowerShell [Add-AzKeyVaultManagedStorageAccount](/powershell/module/az.keyvault/add-azkeyvaultmanagedstorageaccount?view=azps-2.6.0) cmdlet to create a managed storage account in your Key Vault instance. The `-DisableAutoRegenerateKey` switch specifies NOT to regenerate the storage account keys.
117
133
118
134
```azurepowershell-interactive
119
135
# Add your storage account to your Key Vault's managed storage accounts
If you want Key Vault to regenerate your storage account keys periodically, you can set a regeneration period. In the following example, we set a regeneration period of three days. After three days, Key Vault will regenerate 'key2' and swap the active key from 'key2' to 'key1'.
158
+
If you want Key Vault to regenerate your storage account keys periodically, you can use the Azure PowerShell [Add-AzKeyVaultManagedStorageAccount](/powershell/module/az.keyvault/add-azkeyvaultmanagedstorageaccount?view=azps-2.6.0) cmdlet to set a regeneration period. In this example, we set a regeneration period of three days. After three days, Key Vault will regenerate 'key2' and swap the active key from 'key2' to 'key1'.
You can also ask Key Vault to generate shared access signature tokens. A shared access signature provides delegated access to resources in your storage account. You can grant clients access to resources in your storage account without sharing your account keys. A shared access signature provides you with a secure way to share your storage resources without compromising your account keys.
185
+
186
+
The commands in this section complete the following actions:
187
+
188
+
- Set an account shared access signature definition.
189
+
- Create an account shared access signature token for Blob, File, Table, and Queue services. The token is created for resource types Service, Container, and Object. The token is created with all permissions, over https, and with the specified start and end dates.
190
+
- Set a Key Vault managed storage shared access signature definition in the vault. The definition has the template URI of the shared access signature token that was created. The definition has the shared access signature type `account` and is valid for N days.
191
+
- Verify that the shared access signature was saved in your key vault as a secret.
192
+
-
193
+
### Set variables
194
+
195
+
First, set the variables to be used by the PowerShell cmdlets in the following steps. Be sure to update the <YourStorageAccountName> and <YourKeyVaultName> placeholders.
196
+
197
+
We will also use the Azure PowerShell [New-AzStorageContext](/powershell/module/az.storage/new-azstoragecontext?view=azps-2.6.0) cmdlets to get the context of your Azure storage account.
Create a shared access signature definition using the Azure PowerShell [New-AzStorageAccountSASToken](/powershell/module/az.storage/new-azstorageaccountsastoken?view=azps-2.6.0) cmdlets.
Use the the Azure PowerShell [Set-AzKeyVaultManagedStorageSasDefinition](/powershell/module/az.keyvault/set-azkeyvaultmanagedstoragesasdefinition?view=azps-2.6.0) cmdlet to create a shared access signature definition. You can provide the name of your choice to the `-Name` parameter.
You can verify that the shared access signature definition has been stored in your key vault using the Azure PowerShell [Get-AzKeyVaultSecret](/powershell/module/az.keyvault/get-azkeyvaultsecret?view=azps-2.6.0) cmdlet.
233
+
234
+
First, find the shared access signature definition in your key vault.
The secret corresponding to your SAS definition will have these properties:
241
+
242
+
```console
243
+
Vault Name : <YourKeyVaultName>
244
+
Name : <SecretName>
245
+
...
246
+
Content Type : application/vnd.ms-sastoken-storage
247
+
Tags :
248
+
```
249
+
250
+
You can now use the [Get-AzKeyVaultSecret](/cli/azure/keyvault/secret?view=azure-cli-latest#az-keyvault-secret-show) cmdlet and the secret `Name` property to view the content of that secret.
0 commit comments