Skip to content

Commit 0ce495c

Browse files
committed
Revisions
1 parent 4ed57be commit 0ce495c

File tree

1 file changed

+69
-65
lines changed

1 file changed

+69
-65
lines changed

articles/key-vault/tutorial-rotation.md

Lines changed: 69 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Function app requires below components and configuration:
6666
- Storage Account
6767
- Access policy to access secrets in Key Vault using Function App Managed Identity
6868

69-
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)
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)
7070
1. For "Resource Group", select "simplerotation".
7171
1. Select "Purchase".
7272

@@ -92,12 +92,12 @@ For information how to create Function App and using Managed Identity to access
9292

9393
### Rotation function and deployment
9494

95-
Rotation function is using event grid as a trigger, retrieves secret information and executes rotation:
95+
Create a rotation function that retrieves the secret and executes rotation, using event grid as a trigger:
9696

97-
```
97+
```csharp
9898
public static class SimpleRotationEventHandler
9999
{
100-
[FunctionName("SimpleRotation")]
100+
[FunctionName("SimpleRotation")]
101101
public static void Run([EventGridTrigger]EventGridEvent eventGridEvent, ILogger log)
102102
{
103103
log.LogInformation("C# Event trigger function processed a request.");
@@ -113,48 +113,48 @@ public static class SimpleRotationEventHandler
113113
}
114114
```
115115

116-
Rotation method reads database information from secret, create new version of secret and updates database with new secret.
116+
This rotation method reads database information from the secret, create a new version of the secret, and updates the database with a new secret.
117117

118-
```
119-
public class SeretRotator
118+
```csharp
119+
public class SecretRotator
120120
{
121121
private const string UserIdTagName = "UserID";
122122
private const string DataSourceTagName = "DataSource";
123123
private const int SecretExpirationDays = 31;
124124

125-
public static void RotateSecret(ILogger log, string secretName, string secretVersion, string keyVaultName)
126-
{
127-
//Retrieve Current Secret
128-
var kvUri = "https://" + keyVaultName + ".vault.azure.net";
129-
var client = new SecretClient(new Uri(kvUri), new DefaultAzureCredential());
130-
KeyVaultSecret secret = client.GetSecret(secretName, secretVersion);
131-
log.LogInformation("Secret Info Retrieved");
132-
133-
//Retrieve Secret Info
134-
var userId = secret.Properties.Tags.ContainsKey(UserIdTagName) ?
135-
secret.Properties.Tags[UserIdTagName] : "";
136-
var datasource = secret.Properties.Tags.ContainsKey(DataSourceTagName) ?
137-
secret.Properties.Tags[DataSourceTagName] : "";
138-
log.LogInformation($"Data Source Name: {datasource}");
139-
log.LogInformation($"User Id Name: {userId}");
140-
141-
//create new password
142-
var randomPassword = CreateRandomPassword();
143-
log.LogInformation("New Password Generated");
144-
145-
//Check db connection using existing secret
146-
CheckServiceConnection(secret);
147-
log.LogInformation("Service Connection Validated");
148-
149-
//Create new secret with generated password
150-
CreateNewSecretVersion(client, secret, randomPassword);
151-
log.LogInformation("New Secret Version Generated");
152-
153-
//Update db password
154-
UpdateServicePassword(secret, randomPassword);
155-
log.LogInformation("Password Changed");
156-
log.LogInformation($"Secret Rotated Succesffuly");
157-
}
125+
public static void RotateSecret(ILogger log, string secretName, string secretVersion, string keyVaultName)
126+
{
127+
//Retrieve Current Secret
128+
var kvUri = "https://" + keyVaultName + ".vault.azure.net";
129+
var client = new SecretClient(new Uri(kvUri), new DefaultAzureCredential());
130+
KeyVaultSecret secret = client.GetSecret(secretName, secretVersion);
131+
log.LogInformation("Secret Info Retrieved");
132+
133+
//Retrieve Secret Info
134+
var userId = secret.Properties.Tags.ContainsKey(UserIdTagName) ?
135+
secret.Properties.Tags[UserIdTagName] : "";
136+
var datasource = secret.Properties.Tags.ContainsKey(DataSourceTagName) ?
137+
secret.Properties.Tags[DataSourceTagName] : "";
138+
log.LogInformation($"Data Source Name: {datasource}");
139+
log.LogInformation($"User Id Name: {userId}");
140+
141+
//create new password
142+
var randomPassword = CreateRandomPassword();
143+
log.LogInformation("New Password Generated");
144+
145+
//Check db connection using existing secret
146+
CheckServiceConnection(secret);
147+
log.LogInformation("Service Connection Validated");
148+
149+
//Create new secret with generated password
150+
CreateNewSecretVersion(client, secret, randomPassword);
151+
log.LogInformation("New Secret Version Generated");
152+
153+
//Update db password
154+
UpdateServicePassword(secret, randomPassword);
155+
log.LogInformation("Password Changed");
156+
log.LogInformation($"Secret Rotated Succesffuly");
157+
}
158158
}
159159
```
160160

@@ -164,7 +164,7 @@ https://github.com/jlichwa/azure-keyvault-basicrotation-tutorial/tree/master/rot
164164
Download function app zip file:
165165
https://github.com/jlichwa/azure-keyvault-basicrotation-tutorial/raw/master/simplerotationsample-fn.zip
166166

167-
Upload file simplerotationsample-fn.zip to Cloud Shell
167+
Upload file simplerotationsample-fn.zip to Cloud Shell.
168168

169169
Use below CLI command to deploy zip file to function app:
170170

@@ -178,78 +178,82 @@ After deployment you should notice two functions under simplerotation-fn:
178178

179179
### Add event subscription for “SecretNearExpiry” event
180180

181-
Copy function app eventgrid_extension key.
181+
Copy the function app eventgrid_extension key.
182182

183183
![Cloud Shell](./media/rotate5.png)
184184

185185
![Cloud Shell](./media/rotate6.png)
186186

187-
Replace copied key and your subscription id in below command to create event grid subscription for SecretNearExpiry events.
187+
Use the copied eventgrid extension key and your subscription id in below command to create an event grid subscription for SecretNearExpiry events.
188188

189189
```azurecli
190-
az eventgrid event-subscription create --name simplerotation-eventsubscription --source-resource-id "/subscriptions/{subscriptionId}/resourceGroups/simplerotation/providers/Microsoft.KeyVault/vaults/simplerotation-kv" --endpoint "https://simplerotation-fn.azurewebsites.net/runtime/webhooks/EventGrid?functionName=SimpleRotation&code={key}" --endpoint-type WebHook --included-event-types "Microsoft.KeyVault.SecretNearExpiry"
190+
az eventgrid event-subscription create --name simplerotation-eventsubscription --source-resource-id "/subscriptions/<subscription-id>/resourceGroups/simplerotation/providers/Microsoft.KeyVault/vaults/simplerotation-kv" --endpoint "https://simplerotation-fn.azurewebsites.net/runtime/webhooks/EventGrid?functionName=SimpleRotation&code=<extension-key>" --endpoint-type WebHook --included-event-types "Microsoft.KeyVault.SecretNearExpiry"
191191
```
192192

193193
### Add secret to Key Vault
194-
Set access policy to give permission to manage secrets for user
194+
Set your access policy to give "manage secrets" permission to users.
195195

196196
```azurecli
197-
az keyvault set-policy --upn "{email e.g. [email protected]}" --name simplerotation-kv --secret-permissions set delete get list
197+
az keyvault set-policy --upn <email-address-of-user> --name simplerotation-kv --secret-permissions set delete get list
198198
```
199199

200-
Create new secret with tags containing sql database datasource and user id with expiration date for tomorrow.
200+
Now create a new secret with tags containing sql database datasource and user id, with the expiration date set for tomorrow.
201201

202202
```azurecli
203203
$tomorrowDate = (get-date).AddDays(+1).ToString("yyy-MM-ddThh:mm:ssZ")
204204
az keyvault secret set --name sqluser --vault-name simplerotation-kv --value "Simple123" --tags "UserID=azureuser" "DataSource=simplerotation-sql.database.windows.net" --expires $tomorrowDate
205205
```
206206

207-
Creating secret with short expiration date would immediately publish SecretNearExpiry event which would trigger function to rotate the secret.
207+
Creating a secret with a short expiration date will immediately publish a SecretNearExpiry event, which will in turn trigger the function to rotate the secret.
208208

209209
### Test and verify
210-
After few minutes sqluser secret should automatically rotate.
210+
After few minutes, sqluser secret should automatically rotate.
211211

212-
To verify secret rotation verification, go to Key Vault>Secrets
212+
To verify secret rotation verification, go to Key Vault > Secrets
213213

214214
![Test and verify](./media/rotate7.png)
215215

216-
Open sqluser secret to see initial and rotated version
216+
Open the "sqluser" secret and view the original and rotated version
217217

218218
![Test and verify](./media/rotate8.png)
219219

220-
To verify SQL credentials, use a web application. The web application will get secret from key vault, extract sql database information and credentials from secret and test connection to sql.
221-
222220
## Create Web App
223221

224-
Web app requires below components and configuration:
222+
To verify SQL credentials, create a web application. This web application will get the secret from key vault, extract sql database information and credentials from the secret, and test the connection to sql.
223+
224+
The web app requires below components and configuration:
225225
- Web App with System Managed Identity
226226
- Access policy to access secrets in Key Vault using Web App Managed Identity
227227

228-
Azure Resource Manager template to create components:
229-
[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)
230-
- Select ‘simplerotation’ resource group
231-
- Click Purchase
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)
229+
1. Select ‘simplerotation’ resource group
230+
1. Click Purchase
232231

233232
### Deploy Web App
234233

235-
Source code:
236-
https://github.com/jlichwa/azure-keyvault-basicrotation-tutorial/tree/master/test-webapp
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:
237235

238-
1. Download function app zip file:
236+
1. Download the function app zip file from
239237
https://github.com/jlichwa/azure-keyvault-basicrotation-tutorial/raw/master/simplerotationsample-app.zip
240-
1. Upload file simplerotationsample-app.zip to Cloud Shell
241-
1. Use below CLI command to deploy zip file to function app:
238+
1. Upload the file "simplerotationsample-app.zip" to Cloud Shell.
239+
1. Use this Azure CLI command to deploy the zip file to the function app:
242240

243241
```azurecli
244242
az webapp deployment source config-zip -g simplerotation -n simplerotation-app --src /home/{firstname e.g jack}/simplerotationsample-app.zip
245243
```
246244

247245
#### Open web Application
248246

249-
Go to deployed application and click URL
247+
Go to the deployed application and click "URL":
250248

251249
![Test and verify](./media/rotate10.png)
252250

253-
Generated Secret Value should be shown with Database Connected as true.
251+
The Generated Secret Value should be shown with Database Connected as true.
254252

255253
![Test and verify](./media/rotate11.png)
254+
255+
# Next Steps
256+
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)

0 commit comments

Comments
 (0)