Skip to content

Commit 7c8c540

Browse files
authored
Merge pull request #87105 from msmbaldwin/akveventgrid
Key Vault Event Grid
2 parents 9c6a9ab + dff44f2 commit 7c8c540

19 files changed

+352
-1
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
title: Azure Event Grid Azure Key Vault event schema
3+
description: Describes the properties and schema provided for Azure Key Vault events with Azure Event Grid
4+
services: event-grid
5+
author: msmbaldwin
6+
ms.service: event-grid
7+
ms.topic: reference
8+
ms.date: 10/25/2019
9+
ms.author: mbaldwin
10+
---
11+
12+
# Azure Event Grid event schema for Azure Key Vault (preview)
13+
14+
This article provides the properties and schema for [Azure Key Vault](../key-vault/index.yml) events, currently in preview. For an introduction to event schemas, see [Azure Event Grid event schema](event-schema.md).
15+
16+
## Available event types
17+
18+
An Azure Key Vault account emits the following event types:
19+
20+
| Event full Name | Event display name | description |
21+
| ---------- | ----------- |---|
22+
| Microsoft.KeyVault.CertificateNewVersionCreated | Certificate New Version Created | Triggered when new certificate or new certificate version is created |
23+
| Microsoft.KeyVault.CertificateNearExpiry | Certificate Near Expiry | Triggered when current version of certificate is about to expire (default is 30 days before expiration date) |
24+
| Microsoft.KeyVault.CertificateExpired | Certificate Expired | Triggered when certificate is expired |
25+
| Microsoft.KeyVault.KeyNewVersionCreated | Key New Version Created | Triggered when new key or new key version is created |
26+
| Microsoft.KeyVault.KeyNearExpiry | Key Near Expiry | Triggered when current version of key is about to expire (default is 30 days before expiration date) |
27+
| Microsoft.KeyVault.KeyExpired | Key Expired | Triggered when key is expired |
28+
| Microsoft.KeyVault.SecretNewVersionCreated | Secret New Version Created | Triggered when new secret or new secret version is created |
29+
| Microsoft.KeyVault.SecretNearExpiry | Secret Near Expiry | Triggered when current version of secret is about to expire (default is 30 days before expiration date) |
30+
| Microsoft.KeyVault.SecretExpired | Secret Expired | Triggered when secret is expired |
31+
32+
## Event examples
33+
34+
The following example show schema for **Microsoft.KeyVault.SecretNewVersionCreated**.
35+
36+
```JSON
37+
[
38+
{
39+
"id":"00eccf70-95a7-4e7c-8299-2eb17ee9ad64",
40+
"topic":"/subscriptions/{subscription-id}/resourceGroups/sample-rg/providers/Microsoft.KeyVault/vaults/sample-kv",
41+
"subject":"newsecret",
42+
"eventType":"Microsoft.KeyVault.SecretNewVersionCreated",
43+
"eventTime":"2019-07-25T01:08:33.1036736Z",
44+
"data":{
45+
"Id":"https://sample-kv.vault.azure.net/secrets/newsecret/ee059b2bb5bc48398a53b168c6cdcb10",
46+
"vaultName":"sample-kv",
47+
"objectType":"Secret",
48+
"objectName ":"newsecret",
49+
"version":" ee059b2bb5bc48398a53b168c6cdcb10",
50+
"nbf":"1559081980",
51+
"exp":"1559082102"
52+
},
53+
"dataVersion":"1",
54+
"metadataVersion":"1"
55+
}
56+
]
57+
```
58+
59+
## Event properties
60+
61+
An event has the following top-level data:
62+
63+
| Property | Type | Description |
64+
| ---------- | ----------- |---|
65+
| id | string | The ID of the object that triggered this event. |
66+
| vaultName | string | Key vault name of the object that triggered this event. |
67+
| objectType | string | The type of the object that triggered this event |
68+
| objectName | string | The name of the object that triggered this event |
69+
| version | string | The version of the object that triggered this event |
70+
| nbf | number | Not before date in seconds since 1970-01-01T00:00:00Z of the object that triggered this event |
71+
| exp | number | The expiration date in seconds since 1970-01-01T00:00:00Z of the object that triggered this event |
72+
73+
74+
## Next steps
75+
76+
* For an introduction to Azure Event Grid, see [What is Event Grid?](overview.md)
77+
* For more information about creating an Azure Event Grid subscription, see [Event Grid subscription schema](subscription-creation-schema.md).
78+
* To learn more about Key Vault / Event Grid integration, see [Monitoring Key Vault with Azure Event Grid (preview)](../key-vault/event-grid-overview.md)
79+
* To see a tutorial on Key Vault / Event Grid integration, see [How to: Route Key Vault Events to Automation Runbook (preview)](../key-vault/event-grid-tutorial.md).
80+
81+
- [Azure Key Vault overview](../key-vault/key-vault-overview.md)
82+
- [Azure Event Grid overview](overview.md)
83+
- [Monitoring Key Vault with Azure Event Grid (preview)](../key-vault/event-grid-overview.md)
84+
- [How to: Route Key Vault Events to Automation Runbook (preview)](../key-vault/event-grid-tutorial.md).
85+
- [Azure Automation overview](../automation/index.yml)

articles/event-grid/event-sources.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ Subscribe to IoT Hub events to respond to device created, deleted, connected, di
7979
| [Event schema](event-schema-iot-hub.md) | Shows fields in IoT Hub events. |
8080
| [Order device connected and device disconnected events](../iot-hub/iot-hub-how-to-order-connection-state-events.md) | Shows how to order device connection state events. |
8181

82+
## Key Vault (preview)
83+
84+
Key Vault integration with Event Grid is currently in preview.
85+
86+
Subscribe to Key Vault events to be notified when a secret is about to expire, a secret expires, or a secret has a new version available.
87+
88+
|Title |Description |
89+
|---------|---------|
90+
| [Monitoring Key Vault events with Azure Event Grid](../key-vault/event-grid-overview.md) | Overview of integrating Key Vault with Event Grid. |
91+
| [Tutorial: Create and monitor Key Vault events with Event Grid](../key-vault/event-grid-tutorial.md) | Learn how to set up Event Grid notifications for Key Vault. |
92+
| [Event schema](event-schema-key-vault.md) | Shows fields in Key Vault events. |
93+
8294
## Media Services
8395

8496
Subscribe to Media Services events to respond to job state events.
@@ -143,7 +155,7 @@ Subscribe to Azure App Configuration events to respond to key-value modification
143155
|Title | Description |
144156
|---------|---------|
145157
| [React to Azure App Configuration events by using Event Grid](../azure-app-configuration/concept-app-configuration-event.md?toc=%2fazure%2fevent-grid%2ftoc.json) | Overview of integrating Azure App Configuration with Event Grid. |
146-
| [QuickStart: route Azure App Configuration events to a custom web endpoint with Azure CLI](../azure-app-configuration/howto-app-configuration-event.md?toc=%2fazure%2fevent-grid%2ftoc.json) | Shows how to use Azure CLI to send Azure App Configuration events to a WebHook. |
158+
| [Quickstart: route Azure App Configuration events to a custom web endpoint with Azure CLI](../azure-app-configuration/howto-app-configuration-event.md?toc=%2fazure%2fevent-grid%2ftoc.json) | Shows how to use Azure CLI to send Azure App Configuration events to a WebHook. |
147159
| [Event schema](event-schema-app-configuration.md) | Shows fields in Azure App Configuration events. |
148160

149161
## Azure SignalR

articles/event-grid/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@
162162
href: event-schema-event-hubs.md
163163
- name: IoT Hub
164164
href: event-schema-iot-hub.md
165+
- name: Key Vault
166+
href: event-schema-key-vault.md
165167
- name: Media Services
166168
href: ../media-services/latest/media-services-event-schemas.md?toc=%2fazure%2fevent-grid%2ftoc.json
167169
- name: Resource groups
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
title: Monitoring Key Vault with Azure Event Grid
3+
description: Use Azure Event Grid to subscribe to Key Vault events
4+
services: media-services
5+
author: msmbaldwin
6+
manager: rkarlin
7+
8+
ms.service: key-vault
9+
ms.topic: article
10+
ms.date: 10/25/2019
11+
ms.author: mbaldwin
12+
---
13+
14+
# Monitoring Key Vault with Azure Event Grid (preview)
15+
16+
Key Vault integration with Event Grid is currently in preview. It allows users to be notified when the status of a secret stored in key vault has changed. A status change is defined as a secret that is about to expire (within 30 days of expiration), a secret that has expired, or a secret that has a new version available. Notifications for all three secret types (key, certificate, and secret) are supported.
17+
18+
Applications can react to these events using modern serverless architectures, without the need for complicated code or expensive and inefficient polling services. Events are pushed through [Azure Event Grid](https://azure.microsoft.com/services/event-grid/) to event handlers such as [Azure Functions](https://azure.microsoft.com/services/functions/), [Azure Logic Apps](https://azure.microsoft.com/services/logic-apps/), or even to your own Webhook, and you only pay for what you use. For information about pricing, see [Event Grid pricing](https://azure.microsoft.com/pricing/details/event-grid/).
19+
20+
## Key Vault events and schemas
21+
22+
Event grid uses [event subscriptions](../event-grid/concepts.md#event-subscriptions) to route event messages to subscribers. Key Vault events contain all the information you need to respond to changes in your data. You can identify a Key Vault event because the eventType property starts with "Microsoft.KeyVault".
23+
24+
For more information, see the [Key Vault event schema](../event-grid/event-schema-key-vault.md).
25+
26+
> [!NOTE]
27+
> Events are triggered only for secret versions (all three types) created after subscription is set.
28+
>
29+
> For existing secrets, you must generate new versions.
30+
31+
## Practices for consuming events
32+
33+
Applications that handle Key Vault events should follow a few recommended practices:
34+
35+
* Multiple subscriptions can be configured to route events to the same event handler. It is important not to assume events are from a particular source, but to check the topic of the message to ensure that it comes from the key vault you are expecting.
36+
* Similarly, check that the eventType is one you are prepared to process, and do not assume that all events you receive will be the types you expect.
37+
* Ignore fields you don't understand. This practice will help keep you resilient to new features that might be added in the future.
38+
* Use the "subject" prefix and suffix matches to limit events to a particular event.
39+
40+
## Next steps
41+
42+
- [Azure Key Vault overview](key-vault-overview.md)
43+
- [Azure Event Grid overview](../event-grid/overview.md)
44+
- [How to: Route Key Vault Events to Automation Runbook (preview)](event-grid-tutorial.md).
45+
- [Azure Event Grid event schema for Azure Key Vault (preview)](../event-grid/event-schema-key-vault.md)
46+
- [Azure Automation overview](../automation/index.yml)
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
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+
![image](media/image1.png)
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+
![](media/image2.png)
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+
![](media/image3.png)
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+
![](media/image4.png)
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+
![](media/image5.png)
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+
![](media/image6.png)
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+
![](media/image7.png)
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+
![](media/image8.png)
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+
![](media/image9.png)
151+
152+
![](media/image10.png)
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+
![](media/image11.png)
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+
![](media/image12.png)
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+
![](media/image13.png)
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)

articles/key-vault/media/image1.png

71.2 KB
Loading

articles/key-vault/media/image10.png

215 KB
Loading

articles/key-vault/media/image11.png

95 KB
Loading

articles/key-vault/media/image12.png

32.7 KB
Loading

articles/key-vault/media/image13.png

36.8 KB
Loading

0 commit comments

Comments
 (0)