|
| 1 | +--- |
| 2 | +title: Receive and respond to key vault notifications with Azure Event Grid |
| 3 | +description: Learn how to integrate Key Vault with Azure Event Grid. |
| 4 | +services: key-vault |
| 5 | +author: msmbaldwin |
| 6 | +manager: rkarlin |
| 7 | +tags: azure-resource-manager |
| 8 | + |
| 9 | +ms.service: key-vault |
| 10 | +ms.topic: tutorial |
| 11 | +ms.date: 10/25/2019 |
| 12 | +ms.author: mbaldwin |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +# How to: Receive and respond to key vault notifications with Azure Event Grid (preview) |
| 17 | + |
| 18 | +Key Vault integration with Azure Event Grid, currently in preview, enables users to be notified when the status of a secret stored in key vault has changed. For an overview of the feature, see [Monitoring Key Vault with Azure Event Grid](event-grid-overview.md). |
| 19 | + |
| 20 | +This guide will show you how to receive Key Vault notifications through Azure Event Grid, and how to respond to status changes with Azure Automation. |
| 21 | + |
| 22 | +## Prerequisites |
| 23 | + |
| 24 | +- An Azure Subscription. If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin. |
| 25 | +- A key vault in your Azure Subscription. You can quickly create a new key vault by following the steps in [Set and retrieve a secret from Azure Key Vault using Azure CLI](quick-create-cli.md) |
| 26 | + |
| 27 | +## Concepts |
| 28 | + |
| 29 | +Azure Event Grid is an eventing service for the cloud. In this guide, you will subscribe to events for key vault and route events to Azure Automation. When one of the secrets in the key vault is about to expire, Event Grid is notified of the status change and makes an HTTP POST to the endpoint. A web hook then triggers an Azure Automation execution of PowerShell script. |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +## Create an Azure Automation account |
| 34 | + |
| 35 | +Create an Azure Automation account through the [Azure portal](https://portal.azure.com). |
| 36 | + |
| 37 | +1. Go to portal.azure.com and log in to your subscription. |
| 38 | + |
| 39 | +1. In the search box, type in "Automation Accounts". |
| 40 | + |
| 41 | +1. Under the "Services" Section of the drop-down from the search bar, select "Automation Accounts". |
| 42 | + |
| 43 | +1. Click Add. |
| 44 | + |
| 45 | +  |
| 46 | + |
| 47 | +1. Fill the required information in the "Add Automation Account" Blade and select "Create". |
| 48 | + |
| 49 | +## Create a Runbook |
| 50 | + |
| 51 | +After your Azure Automation account is ready, create a runbook. |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +1. Select the automation account you just created. |
| 56 | + |
| 57 | +1. Select "Runbooks" under the Process Automation section. |
| 58 | + |
| 59 | +1. Click the "Create a runbook". |
| 60 | + |
| 61 | +1. Name your runbook and select "PowerShell" as the runbook type. |
| 62 | + |
| 63 | +1. Click on the runbook you created, and select the "Edit" Button. |
| 64 | + |
| 65 | +1. Enter the following code (for testing purposes) and click the "Publish" button. This will output the result of the POST request received. |
| 66 | + |
| 67 | +```azurepowershell |
| 68 | +param |
| 69 | +( |
| 70 | +[Parameter (Mandatory = $false)] |
| 71 | +[object] $WebhookData |
| 72 | +) |
| 73 | +
|
| 74 | +#If runbook was called from Webhook, WebhookData will not be null. |
| 75 | +if ($WebhookData) { |
| 76 | +
|
| 77 | +#rotate secret: |
| 78 | +#generate new secret version in key vault |
| 79 | +#update db/service with generated secret |
| 80 | +
|
| 81 | +#Write-Output "WebhookData <$WebhookData>" |
| 82 | +Write-Output $WebhookData.RequestBody |
| 83 | +} |
| 84 | +else |
| 85 | +{ |
| 86 | +# Error |
| 87 | +write-Error "No input data found." |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +## Create a webhook |
| 94 | + |
| 95 | +Now create a webhook, to trigger your newly created runbook. |
| 96 | + |
| 97 | +1. Select "Webhooks" from the resources section of the runbook you just published. |
| 98 | + |
| 99 | +1. Click "Add Webhook". |
| 100 | + |
| 101 | +  |
| 102 | + |
| 103 | +1. Select "Create new Webhook". |
| 104 | + |
| 105 | +1. Name the webhook, set an expiration date, and **copy the URL**. |
| 106 | + |
| 107 | + > [!IMPORTANT] |
| 108 | + > You cannot view the URL after you create it. Make sure you save a copy a secure location where you can access it for the remainder of this guide. |
| 109 | +
|
| 110 | +1. Click "Parameters and run settings", and select "OK". Do not enter any parameters. This will enable the "Create" button. |
| 111 | + |
| 112 | +1. Select "OK", and select "Create". |
| 113 | + |
| 114 | +  |
| 115 | + |
| 116 | +## Create an Event Grid subscription |
| 117 | + |
| 118 | +Create an Event Grid subscription through the [Azure portal](https://portal.azure.com). |
| 119 | + |
| 120 | +1. Open the Azure portal using the following link: https://ms.portal.azure.com/?Microsoft_Azure_KeyVault_ShowEvents=true&Microsoft_Azure_EventGrid_publisherPreview=true |
| 121 | + |
| 122 | +1. Go to your key vault and select the "Events" tab. If you cannot see the Events tab, make sure that you are using the [preview version of the portal](https://ms.portal.azure.com/?Microsoft_Azure_KeyVault_ShowEvents=true&Microsoft_Azure_EventGrid_publisherPreview=true). |
| 123 | + |
| 124 | +  |
| 125 | + |
| 126 | +1. Click the "+ Event Subscription" button. |
| 127 | + |
| 128 | +1. Create a descriptive name for the subscription. |
| 129 | + |
| 130 | +1. Choose "Event Grid Schema". |
| 131 | + |
| 132 | +1. "Topic Resource" should be the key vault you want to monitor for status changes. |
| 133 | + |
| 134 | +1. For "Filter to Event Types", leave all checked ("9 selected"). |
| 135 | + |
| 136 | +1. For "Endpoint Type", select "Webhook". |
| 137 | + |
| 138 | +1. Select "Select an endpoint". In the new context pane, paste the webhook URL from the [Create a webhook](#create-a-webhook) step into the "Subscriber Endpoint" field. |
| 139 | + |
| 140 | +1. Select "Confirm Selection" on the context pane. |
| 141 | + |
| 142 | +1. Select "Create". |
| 143 | + |
| 144 | +  |
| 145 | + |
| 146 | +## Test and verify |
| 147 | + |
| 148 | +Verify that your Event Grid subscription is property configured. This test assumes that you have subscribed to "Secret New Version Created" notification in the [Create an Event Grid subscription](#create-an-event-grid-subscription), and that you have the necessary privileges to create a new version of a secret in a key vault. |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | +1. Go to your key vault on the Azure portal |
| 155 | + |
| 156 | +1. Create a new secret. For testing purposes, set expiration to date to next day. |
| 157 | + |
| 158 | +1. Navigate to the events tab in your key vault. |
| 159 | + |
| 160 | +1. Select the event grid subscription you created. |
| 161 | + |
| 162 | +1. Under metrics, see if an event was captured. Two events are expected: SecretNewVersion and SecretNearExpiry. This validates that event grid successfully captured the status change of the secret in your key vault. |
| 163 | + |
| 164 | +  |
| 165 | + |
| 166 | +1. Go to your Azure Automation account. |
| 167 | + |
| 168 | +1. Select the "Runbooks" tab, and select the runbook you created. |
| 169 | + |
| 170 | +1. Select the "Webhooks" tab, and confirm that the "last triggered" timestamp is within 60 seconds of when you created the new secret. This confirms that Event Grid made a POST to the webhook with the event details of the status change in your key vault, and the webhook was triggered. |
| 171 | + |
| 172 | +  |
| 173 | + |
| 174 | +1. Return to your Runbook and select the "Overview" Tab. |
| 175 | + |
| 176 | +1. Look at the Recent Jobs list. You should see that a job was created and that the status is complete. This confirms that the webhook triggered the runbook to start executing its script. |
| 177 | + |
| 178 | +  |
| 179 | + |
| 180 | +1. Select the recent job and look at the POST request that was sent from event grid to the webhook. Examine the JSON and make sure that the parameters for your key vault and event type are correct. If the "event type" parameter in the JSON object matches the event which occurred in the key vault (in this example, Microsoft.KeyVault.SecretNearExpiry) the test was successful. |
| 181 | + |
| 182 | +## Troubleshooting |
| 183 | + |
| 184 | +### Unable to create event subscription |
| 185 | + |
| 186 | +Reregister Event Grid and Key Vault provider in your azure subscription resource providers. See [Azure resource providers and types](../azure-resource-manager/resource-manager-supported-services.md). |
| 187 | + |
| 188 | +## Next steps |
| 189 | + |
| 190 | +Congratulations! If you have followed all the steps above, you are now ready to programmatically respond to status changes of secrets stored in your key vault. |
| 191 | + |
| 192 | +If you have been using a polling-based system to look for status changes of secrets in your key vaults, migrate to using this notification feature. You can also replace the test script in your runbook with code to programmatically renew your secrets when they are about to expire. |
| 193 | + |
| 194 | +Learn more: |
| 195 | + |
| 196 | +- [Azure Key Vault overview](key-vault-overview.md) |
| 197 | +- [Azure Event Grid overview](../event-grid/overview.md) |
| 198 | +- [Monitoring Key Vault with Azure Event Grid (preview)](event-grid-overview.md) |
| 199 | +- [Azure Event Grid event schema for Azure Key Vault (preview)](../event-grid/event-schema-key-vault.md) |
| 200 | +- [Azure Automation overview](../automation/index.yml) |
0 commit comments