Skip to content

Commit 5ff5884

Browse files
committed
Merge branch 'akv-rotate' of https://github.com/msmbaldwin/azure-docs-pr into akv-rotate
2 parents 584f18b + 1e1fd4e commit 5ff5884

File tree

1 file changed

+44
-28
lines changed

1 file changed

+44
-28
lines changed

articles/key-vault/tutorial-rotation.md

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
---
1+
---
22
title: Single User/Password Rotation Tutorial
3-
description: Use this how-to guide to help you set up key rotation and monitor key vault logs.
3+
description: Use this tutorial for automating rotation of single user/password
44
services: key-vault
55
author: msmbaldwin
66
manager: rkarlin
7-
tags: ''
7+
tags: 'rotation'
88

99
ms.service: key-vault
10-
ms.topic: conceptual
10+
ms.subservice: general
11+
ms.topic: tutorial
1112
ms.date: 01/26/2020
1213
ms.author: mbaldwin
1314

1415
---
15-
# Automate the rotation of a single user/password secret
16+
# Automate the rotation of a secret for resources with single user/password authentication
1617

1718
Although the best way to authenticate to Azure services is by using an [managed identity](managed-identity.md), there are some scenarios where this is not an option. In these cases, access keys or secrets are used. Access keys or secrets should be periodically rotated.
1819

@@ -25,7 +26,9 @@ This tutorial demonstrates how to automate the periodic rotation of secrets for
2526
1. The function App receives secret information, generates new random password, and create a new version for the secret with a new password in Key Vault.
2627
1. The function App updates SQL with new password.
2728

28-
Note: There could be a lag between step 3 and 4 and during that time secret in Key Vault would not be valid to authenticate to SQL. In case of failure in any of the steps Event Grid retries for 2 hours.
29+
> [!NOTE]
30+
> There could be a lag between step 3 and 4 and during that time secret in Key Vault would not be valid to authenticate to SQL.
31+
> In case of failure in any of the steps Event Grid retries for 2 hours.
2932
3033
## Setup
3134

@@ -35,7 +38,8 @@ Before we begin, we must create a Key Vault, create a SQL Server and database, a
3538

3639
This tutorial uses a pre-created Azure Resource Manager template to create components. You can find entire code here: [Basic Secret Rotation Template Sample](https://github.com/jlichwa/azure-keyvault-basicrotation-tutorial/tree/master/arm-templates).
3740

38-
1. Use the Azure Resource Manager template to create components by selecting this link: [Deploy](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2Fjlichwa%2Fazure-keyvault-basicrotation-tutorial%2Fmaster%2Farm-templates%2Finitial-setup%2Fazuredeploy.json)
41+
1. Click Azure template deployment link:
42+
<br><a href="https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2Fjlichwa%2Fazure-keyvault-basicrotation-tutorial%2Fmaster%2Farm-templates%2Finitial-setup%2Fazuredeploy.json" target="_blank"> <img src="https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/1-CONTRIBUTION-GUIDE/images/deploytoazure.png"/></a>
3943
1. For "Resource Group", select "Create New" and give it the name "simplerotation".
4044
1. Select "Purchase".
4145

@@ -59,14 +63,15 @@ simplerotation-sql/master simplerotation eastus Microsoft.S
5963

6064
## Create Function App
6165

62-
You must now create a Function App with a with system managed identity, as well as the additional required components:
66+
Create a Function App with a with system managed identity, as well as the additional required components:
6367

6468
Function app requires below components and configuration:
6569
- App Service Plan
6670
- Storage Account
6771
- Access policy to access secrets in Key Vault using Function App Managed Identity
6872

69-
1. Use the Azure Resource Manager template to create components by selecting this link: [Deploy](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2Fjlichwa%2Fazure-keyvault-basicrotation-tutorial%2Fmaster%2Farm-templates%2Ffunction-app%2Fazuredeploy.json)
73+
1. Click Azure template deployment link:
74+
<br><a href="https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2Fjlichwa%2Fazure-keyvault-basicrotation-tutorial%2Fmaster%2Farm-templates%2Ffunction-app%2Fazuredeploy.json" target="_blank"><img src="https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/1-CONTRIBUTION-GUIDE/images/deploytoazure.png"/></a>
7075
1. For "Resource Group", select "simplerotation".
7176
1. Select "Purchase".
7277

@@ -79,6 +84,8 @@ az resource list -o table
7984
```
8085

8186
The results will look something this:
87+
88+
```console
8289
Name ResourceGroup Location Type Status
8390
----------------------- -------------------- ---------- --------------------------------- --------
8491
simplerotation-kv simplerotation eastus Microsoft.KeyVault/vaults
@@ -87,12 +94,16 @@ simplerotation-sql/master simplerotation eastus Microsoft.S
8794
simplerotationstrg simplerotation eastus Microsoft.Storage/storageAccounts
8895
simplerotation-plan simplerotation eastus Microsoft.Web/serverFarms
8996
simplerotation-fn simplerotation eastus Microsoft.Web/sites
97+
```
9098

9199
For information how to create Function App and using Managed Identity to access Key Vault, see [Create a function app from the Azure portal](../azure-functions/functions-create-function-app-portal.md) and [Provide Key Vault authentication with a managed identity](managed-identity.md)
92100

93101
### Rotation function and deployment
102+
Function is using event as trigger and perform rotation of a secret updating Key Vault and SQL database.
94103

95-
Create a rotation function that retrieves the secret and executes rotation, using event grid as a trigger:
104+
#### Function Event Trigger Handler
105+
106+
Below Function reads event data and executes rotation logic
96107

97108
```csharp
98109
public static class SimpleRotationEventHandler
@@ -113,6 +124,7 @@ public static class SimpleRotationEventHandler
113124
}
114125
```
115126

127+
#### Secret Rotation Logic
116128
This rotation method reads database information from the secret, create a new version of the secret, and updates the database with a new secret.
117129

118130
```csharp
@@ -124,9 +136,9 @@ public class SecretRotator
124136

125137
public static void RotateSecret(ILogger log, string secretName, string secretVersion, string keyVaultName)
126138
{
127-
//Retrieve Current Secret
139+
//Retrieve Current Secret
128140
var kvUri = "https://" + keyVaultName + ".vault.azure.net";
129-
var client = new SecretClient(new Uri(kvUri), new DefaultAzureCredential());
141+
var client = new SecretClient(new Uri(kvUri), new DefaultAzureCredential());
130142
KeyVaultSecret secret = client.GetSecret(secretName, secretVersion);
131143
log.LogInformation("Secret Info Retrieved");
132144

@@ -150,40 +162,42 @@ public class SecretRotator
150162
CreateNewSecretVersion(client, secret, randomPassword);
151163
log.LogInformation("New Secret Version Generated");
152164

153-
//Update db password
165+
//Update db password
154166
UpdateServicePassword(secret, randomPassword);
155167
log.LogInformation("Password Changed");
156168
log.LogInformation($"Secret Rotated Succesffuly");
157169
}
158170
}
159171
```
160-
161172
You can find entire code here:
162173
https://github.com/jlichwa/azure-keyvault-basicrotation-tutorial/tree/master/rotation-function
163174

164-
Download function app zip file:
175+
#### Function deployment
176+
177+
1. Download function app zip file:
165178
https://github.com/jlichwa/azure-keyvault-basicrotation-tutorial/raw/master/simplerotationsample-fn.zip
166179

167-
Upload file simplerotationsample-fn.zip to Cloud Shell.
180+
1. Upload file simplerotationsample-fn.zip to Cloud Shell.
168181

169-
Use below CLI command to deploy zip file to function app:
182+
1. Use below CLI command to deploy zip file to function app:
170183

171184
```azurecli
172185
az functionapp deployment source config-zip -g simplerotation -n simplerotation-fn --src /home/{firstname e.g jack}/simplerotationsample-fn.zip
173186
```
187+
![Purchase screen](./media/rotate4.png)
174188

175189
After deployment you should notice two functions under simplerotation-fn:
176190

177-
![Purchase screen](./media/rotate4.png)
191+
![Cloud Shell](./media/rotate5.png)
178192

179193
### Add event subscription for “SecretNearExpiry” event
180194

181195
Copy the function app eventgrid_extension key.
182196

183-
![Cloud Shell](./media/rotate5.png)
184-
185197
![Cloud Shell](./media/rotate6.png)
186198

199+
![Test and verify](./media/rotate7.png)
200+
187201
Use the copied eventgrid extension key and your subscription id in below command to create an event grid subscription for SecretNearExpiry events.
188202

189203
```azurecli
@@ -211,11 +225,11 @@ After few minutes, sqluser secret should automatically rotate.
211225

212226
To verify secret rotation verification, go to Key Vault > Secrets
213227

214-
![Test and verify](./media/rotate7.png)
228+
![Test and verify](./media/rotate8.png)
215229

216230
Open the "sqluser" secret and view the original and rotated version
217231

218-
![Test and verify](./media/rotate8.png)
232+
![Test and verify](./media/rotate9.png)
219233

220234
## Create Web App
221235

@@ -225,13 +239,15 @@ The web app requires below components and configuration:
225239
- Web App with System Managed Identity
226240
- Access policy to access secrets in Key Vault using Web App Managed Identity
227241

228-
1. Use the Azure Resource Manager template to create components by selecting this link: [Deploy](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2Fjlichwa%2Fazure-keyvault-basicrotation-tutorial%2Fmaster%2Farm-templates%2Fweb-app%2Fazuredeploy.json)
242+
1. Click Azure template deployment link:
243+
<br><a href="https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2Fjlichwa%2Fazure-keyvault-basicrotation-tutorial%2Fmaster%2Farm-templates%2Fweb-app%2Fazuredeploy.json" target="_blank"> <img src="https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/1-CONTRIBUTION-GUIDE/images/deploytoazure.png"/></a>
229244
1. Select ‘simplerotation’ resource group
230245
1. Click Purchase
231246

232247
### Deploy Web App
233248

234-
Source code for the web app is at https://github.com/jlichwa/azure-keyvault-basicrotation-tutorial/tree/master/test-webapp.To deploy the web app, do the following:
249+
Source code for the web app you can find at https://github.com/jlichwa/azure-keyvault-basicrotation-tutorial/tree/master/test-webapp
250+
For deployment of the web app, do the following:
235251

236252
1. Download the function app zip file from
237253
https://github.com/jlichwa/azure-keyvault-basicrotation-tutorial/raw/master/simplerotationsample-app.zip
@@ -252,8 +268,8 @@ The Generated Secret Value should be shown with Database Connected as true.
252268

253269
![Test and verify](./media/rotate11.png)
254270

255-
## Next Steps
271+
## Learn more:
256272

257-
- Learn more about [Azure Key Vault with key rotation and auditing](key-vault-key-rotation-log-monitoring.md)
258-
- Learn more about [Azure Functions](../azure-functions/functions-overview.md)
259-
- Learn more about [Azure SQL Database](../sql-database/sql-database-technical-overview.md)
273+
- Overview: [Monitoring Key Vault with Azure Event Grid (preview)](event-grid-overview.md)
274+
- How to: [Receive email when a key vault secret changes](event-grid-logicapps.md)
275+
- [Azure Event Grid event schema for Azure Key Vault (preview)](../event-grid/event-schema-key-vault.md)

0 commit comments

Comments
 (0)