Skip to content

Commit 73ad9da

Browse files
Merge pull request #89023 from msmbaldwin/akv-storage-keys
Akv storage keys
2 parents 3887708 + 1308af5 commit 73ad9da

File tree

4 files changed

+275
-169
lines changed

4 files changed

+275
-169
lines changed

articles/key-vault/key-vault-overview-storage-keys-powershell.md

Lines changed: 141 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,64 @@
11
---
22
title: Azure Key Vault managed storage account - PowerShell version
3-
description: The managed storage account feature provides a seemless integration, between Azure Key Vault and an Azure storage account.
3+
description: The managed storage account feature provides a seamless integration, between Azure Key Vault and an Azure storage account.
44
ms.topic: conceptual
55
ms.service: key-vault
66
author: msmbaldwin
77
ms.author: mbaldwin
88
manager: rkarlin
9-
ms.date: 03/01/2019
9+
ms.date: 09/10/2019
10+
1011
# Customer intent: As a developer I want storage credentials and SAS tokens to be managed securely by Azure Key Vault.
1112
---
12-
# Azure Key Vault managed storage account - PowerShell
13+
# 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:
1320

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.
2032

2133
[!INCLUDE [updated-for-az](../../includes/updated-for-az.md)]
2234

23-
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.
2438

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.
2640

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` |
2846

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
3348

34-
When you use the managed storage account key feature:
49+
To complete this guide, you must first do the following:
3550

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+
3955

40-
The following example shows you how to allow Key Vault to manage your storage account keys.
56+
## Manage storage account keys
4157

42-
## Connect to your Azure account
58+
### Connect to your Azure account
4359

4460
Authenticate your PowerShell session using the [Connect-AzAccount](/powershell/module/az.accounts/connect-azaccount?view=azps-2.5.0) cmdlet.
61+
4562
```azurepowershell-interactive
4663
Connect-AzAccount
4764
```
@@ -51,31 +68,33 @@ If you have multiple Azure subscriptions, you can list them using the [Get-AzSub
5168
Set-AzContext -SubscriptionId <subscriptionId>
5269
```
5370

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
6072

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).
6274

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.
6476

6577
```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
67-
$resourceGroupName = "rgContoso"
68-
$storageAccountName = "sacontoso"
78+
$resourceGroupName = <YourResourceGroupName>
79+
$storageAccountName = <YourStorageAccountName>
80+
$keyVaultName = <YourKeyVaultName>
81+
$keyVaultSpAppId = "cfa8b339-82a2-471a-a3c9-0fc0be7a4093"
6982
$storageAccountKey = "key1"
70-
$keyVaultName = "kvContoso"
71-
$keyVaultSpAppId = "cfa8b339-82a2-471a-a3c9-0fc0be7a4093" # See "IMPORTANT" block above for information on Key Vault Application IDs
7283
73-
# Get your User Id for later commands
84+
# Get your User Id
7485
$userId = (Get-AzContext).Account.Id
7586
7687
# Get a reference to your Azure storage account
7788
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -StorageAccountName $storageAccountName
89+
```
90+
91+
### Give Key Vault access to your storage account
92+
93+
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).
7894

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
7998
# 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."
8099
New-AzRoleAssignment -ApplicationId $keyVaultSpAppId -RoleDefinitionName 'Storage Account Key Operator Service Role' -Scope $storageAccount.Id
81100
```
@@ -96,12 +115,9 @@ CanDelegate : False
96115

97116
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.
98117

99-
## Give your user account permission to managed storage accounts
118+
### Give your user account permission to managed storage accounts
100119

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.
105121

106122
```azurepowershell-interactive
107123
# Give your user principal access to all storage account permissions, on your Key Vault instance
@@ -111,12 +127,13 @@ Set-AzKeyVaultAccessPolicy -VaultName $keyVaultName -UserPrincipalName $userId -
111127

112128
Note that permissions for storage accounts aren't available on the storage account "Access policies" page in the Azure portal.
113129

114-
## Add a managed storage account to your Key Vault instance
130+
### Add a managed storage account to your Key Vault instance
115131

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.
117133

118134
```azurepowershell-interactive
119135
# Add your storage account to your Key Vault's managed storage accounts
136+
120137
Add-AzKeyVaultManagedStorageAccount -VaultName $keyVaultName -AccountName $storageAccountName -AccountResourceId $storageAccount.Id -ActiveKeyName $storageAccountKey -DisableAutoRegenerateKey
121138
```
122139

@@ -138,10 +155,11 @@ Tags :
138155

139156
### Enable key regeneration
140157

141-
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'.
142159

143160
```azurepowershell-interactive
144161
$regenPeriod = [System.Timespan]::FromDays(3)
162+
145163
Add-AzKeyVaultManagedStorageAccount -VaultName $keyVaultName -AccountName $storageAccountName -AccountResourceId $storageAccount.Id -ActiveKeyName $storageAccountKey -RegenerationPeriod $regenPeriod
146164
```
147165

@@ -161,6 +179,85 @@ Updated : 11/19/2018 11:54:47 PM
161179
Tags :
162180
```
163181

182+
## Shared access signature tokens
183+
184+
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.
198+
199+
```azurepowershell-interactive
200+
$storageAccountName = <YourStorageAccountName>
201+
$keyVaultName = <YourKeyVaultName>
202+
203+
$storageContext = New-AzStorageContext -StorageAccountName $storageAccountName -Protocol Https -StorageAccountKey Key1
204+
```
205+
206+
### Create a shared access signature token
207+
208+
Create a shared access signature definition using the Azure PowerShell [New-AzStorageAccountSASToken](/powershell/module/az.storage/new-azstorageaccountsastoken?view=azps-2.6.0) cmdlets.
209+
210+
```azurepowershell-interactive
211+
$start = [System.DateTime]::Now.AddDays(-1)
212+
$end = [System.DateTime]::Now.AddMonths(1)
213+
214+
$sasToken = New-AzStorageAccountSasToken -Service blob,file,Table,Queue -ResourceType Service,Container,Object -Permission "racwdlup" -Protocol HttpsOnly -StartTime $start -ExpiryTime $end -Context $storageContext
215+
```
216+
The value of $sasToken will look similar to this.
217+
218+
```console
219+
?sv=2018-11-09&sig=5GWqHFkEOtM7W9alOgoXSCOJO%2B55qJr4J7tHQjCId9S%3D&spr=https&st=2019-09-18T18%3A25%3A00Z&se=2019-10-19T18%3A25%3A00Z&srt=sco&ss=bfqt&sp=racupwdl
220+
```
221+
222+
### Generate a shared access signature definition
223+
224+
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.
225+
226+
```azurepowershell-interactive
227+
Set-AzKeyVaultManagedStorageSasDefinition -AccountName $storageAccountName -VaultName $keyVaultName -Name <YourSASDefinitionName> -TemplateUri $sasToken -SasType 'account' -ValidityPeriod ([System.Timespan]::FromDays(30))
228+
```
229+
230+
### Verify the shared access signature definition
231+
232+
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.
235+
236+
```azurepowershell-interactive
237+
Get-AzKeyVaultSecret -vault-name <YourKeyVaultName>
238+
```
239+
240+
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.
251+
252+
```azurepowershell-interactive
253+
$secret = Get-AzKeyVaultSecret -VaultName <YourKeyVaultName> -Name <SecretName>
254+
255+
Write-Host $secret.SecretValueText
256+
```
257+
258+
The output of this command will show your SAS definition string.
259+
260+
164261
## Next steps
165262

166263
- [Managed storage account key samples](https://github.com/Azure-Samples?utf8=%E2%9C%93&q=key+vault+storage&type=&language=)

0 commit comments

Comments
 (0)