Skip to content

Commit 3e5789a

Browse files
authored
Merge pull request #205217 from timwarner-msft/timwarner-policyMG
Add support for management groups
2 parents 1c215ee + 683afc0 commit 3e5789a

File tree

3 files changed

+134
-31
lines changed

3 files changed

+134
-31
lines changed

articles/event-grid/event-schema-policy.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
22
title: Azure Policy as an Event Grid source
33
description: This article describes how to use Azure Policy as an Event Grid event source. It provides the schema and links to tutorial and how-to articles.
4-
ms.topic: conceptual
54
author: timwarner-msft
5+
ms.topic: conceptual
66
ms.author: timwarner
7-
ms.date: 07/12/2022
7+
ms.date: 07/19/2022
88
---
99

1010
# Azure Policy as an Event Grid source

articles/governance/policy/tutorials/route-state-change-events.md

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
title: "Tutorial: Route policy state change events to Event Grid with Azure CLI"
33
description: In this tutorial, you configure Event Grid to listen for policy state change events and call a webhook.
4-
ms.date: 06/29/2022
4+
author: timwarner-msft
5+
ms.date: 07/19/2022
56
ms.topic: tutorial
67
ms.custom: devx-track-azurecli
78
ms.author: timwarner
8-
author: timwarner-msft
99
---
1010
# Tutorial: Route policy state change events to Event Grid with Azure CLI
1111

@@ -27,19 +27,6 @@ send the events to a web app that collects and displays the messages.
2727
`az --version`. If you need to install or upgrade, see
2828
[Install Azure CLI](/cli/azure/install-azure-cli).
2929

30-
- Even if you've previously used Azure Policy or Event Grid, re-register their respective resource
31-
providers:
32-
33-
```azurecli-interactive
34-
# Log in first with az login if you're not using Cloud Shell
35-
36-
# Provider register: Register the Azure Policy provider
37-
az provider register --namespace Microsoft.PolicyInsights
38-
39-
# Provider register: Register the Azure Event Grid provider
40-
az provider register --namespace Microsoft.EventGrid
41-
```
42-
4330
[!INCLUDE [cloud-shell-try-it.md](../../../../includes/cloud-shell-try-it.md)]
4431

4532
## Create a resource group
@@ -63,14 +50,27 @@ az group create --name <resource_group_name> --location westus
6350
Now that we have a resource group, we create a
6451
[system topic](../../../event-grid/system-topics.md). A system topic in Event Grid represents one or
6552
more events published by Azure services such as Azure Policy and Azure Event Hubs. This system topic
66-
uses the `Microsoft.PolicyInsights.PolicyStates` topic type for Azure Policy state changes. Replace
67-
`<SubscriptionID>` in the **scope** parameter with the ID of your subscription and
68-
`<resource_group_name>` in **resource-group** parameter with the previously created resource group.
53+
uses the `Microsoft.PolicyInsights.PolicyStates` topic type for Azure Policy state changes.
54+
55+
First, you'll need to register the `PolicyInsights` and `EventGrid` resource providers (RPs) at the appropriate management scope. Whereas the Azure portal auto-registers any RPs you invoke for the first time, Azure CLI does not.
6956

7057
```azurecli-interactive
7158
# Log in first with az login if you're not using Cloud Shell
7259
73-
az eventgrid system-topic create --name PolicyStateChanges --location global --topic-type Microsoft.PolicyInsights.PolicyStates --source "/subscriptions/<subscriptionID>" --resource-group "<resource_group_name>"
60+
# Register the required RPs at the management group scope
61+
az provider register --namespace Microsoft.PolicyInsights -m <managementGroupId>
62+
az provider register --namespace Microsoft.EventGrid -m <managementGroupId>
63+
64+
# Alternatively, register the required RPs at the subscription scope (defaults to current subscription context)
65+
az provider register --namespace Microsoft.PolicyInsights
66+
az provider register --namespace Microsoft.EventGrid
67+
```
68+
69+
Next, replace `<subscriptionId>` in the **scope** parameter with the ID of your subscription and
70+
`<resource_group_name>` in **resource-group** parameter with the previously created resource group.
71+
72+
```azurecli-interactive
73+
az eventgrid system-topic create --name PolicyStateChanges --location global --topic-type Microsoft.PolicyInsights.PolicyStates --source "/subscriptions/<subscriptionId>" --resource-group "<resource_group_name>"
7474
```
7575

7676
If your Event Grid system topic will be applied to the management group scope, then the Azure CLI `--source` parameter syntax is a bit different. Here's an example:
@@ -144,7 +144,7 @@ hold the Event Grid topic:
144144
```azurecli-interactive
145145
# Log in first with az login if you're not using Cloud Shell
146146
147-
az policy assignment create --name 'requiredtags-events' --display-name 'Require tag on RG' --scope '<ResourceGroupScope>' --policy '<policy definition ID>' --params '{ \"tagName\": { \"value\": \"EventTest\" } }'
147+
az policy assignment create --name 'requiredtags-events' --display-name 'Require tag on RG' --scope '<resourceGroupScope>' --policy '<policy definition ID>' --params '{ \"tagName\": { \"value\": \"EventTest\" } }'
148148
```
149149

150150
The preceding command uses the following information:
@@ -155,7 +155,7 @@ The preceding command uses the following information:
155155
- **Scope** - A scope determines what resources or grouping of resources the policy assignment gets
156156
enforced on. It could range from a subscription to resource groups. Be sure to replace
157157
&lt;scope&gt; with the name of your resource group. The format for a resource group scope is
158-
`/subscriptions/<SubscriptionID>/resourceGroups/<ResourceGroup>`.
158+
`/subscriptions/<subscriptionId>/resourceGroups/<resourceGroup>`.
159159
- **Policy** - The policy definition ID, based on which you're using to create the assignment. In
160160
this case, it's the ID of policy definition _Require a tag on resource groups_. To get the policy
161161
definition ID, run this command:
@@ -175,19 +175,26 @@ event notification to appear in the web app. The resource group we created show
175175
## Trigger a change on the resource group
176176

177177
To make the resource group compliant, a tag with the name **EventTest** is required. Add the tag to
178-
the resource group with the following command replacing `<SubscriptionID>` with your subscription ID
179-
and `<ResourceGroup>` with the name of the resource group:
178+
the resource group with the following command replacing `<subscriptionId>` with your subscription ID
179+
and `<resourceGroup>` with the name of the resource group:
180180

181181
```azurecli-interactive
182182
# Log in first with az login if you're not using Cloud Shell
183183
184-
az tag create --resource-id '/subscriptions/<SubscriptionID>/resourceGroups/<ResourceGroup>' --tags EventTest=true
184+
az tag create --resource-id '/subscriptions/<SubscriptionID>/resourceGroups/<resourceGroup>' --tags EventTest=true
185185
```
186186

187187
After adding the required tag to the resource group, wait for a
188188
**Microsoft.PolicyInsights.PolicyStateChanged** event notification to appear in the web app. Expand
189189
the event and the `data.complianceState` value now shows _Compliant_.
190190

191+
## Troubleshooting
192+
193+
If you see an error similar to one of the following, please make sure that you've registered both resource providers at the scope to which you're subscribing (management group or subscription):
194+
195+
- `Deployment has failed with the following error: {"code":"Publisher Notification Error","message":"Failed to enable publisher notifications.","details":[{"code":"Publisher Provider Error","message":"GET request for <uri> failed with status code: Forbidden, code: AuthorizationFailed and message: The client '<identifier>' with object id '<identifier>' does not have authorization to perform action 'microsoft.policyinsights/eventGridFilters/read' over scope '<scope>/providers/microsoft.policyinsights/eventGridFilters/_default' or the scope is invalid. If access was recently granted, please refresh your credentials.."}]}`
196+
- `Deployment has failed with the following error: {'code':'Publisher Notification Error','message':'Failed to enable publisher notifications.','details':[{'code':'ApiVersionNotSupported','message':'Event Grid notifications are currently not supported by microsoft.policyinsights in global. Try re-registering Microsoft.EventGrid provider if this is your first event subscription in this region.'}]}`
197+
191198
## Clean up resources
192199

193200
If you plan to continue working with this web app and Azure Policy event subscription, don't clean

includes/policy/policy-events.md

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
author: timwarner-msft
33
ms.service: azure-policy
44
ms.topic: include
5-
ms.date: 07/12/2022
5+
ms.date: 07/19/2022
66
ms.author: timwarner
77
ms.custom: generated
88
---
@@ -65,7 +65,7 @@ The data object has the following properties:
6565
## Example event
6666

6767
# [Event Grid event schema](#tab/event-grid-event-schema)
68-
The following example shows the schema of a policy state created event:
68+
The following example shows the schema of a policy state created event scoped at the subscription level:
6969

7070
```json
7171
[{
@@ -88,7 +88,7 @@ The following example shows the schema of a policy state created event:
8888
}]
8989
```
9090

91-
The schema for a policy state changed event is similar:
91+
The schema for a policy state changed event scoped at the subscription level is similar:
9292

9393
```json
9494
[{
@@ -110,9 +110,10 @@ The schema for a policy state changed event is similar:
110110
"metadataVersion": "1"
111111
}]
112112
```
113+
113114
# [Cloud event schema](#tab/cloud-event-schema)
114115

115-
The following example shows the schema of a policy state created event:
116+
The following example shows the schema of a policy state created event scoped at the subscription level:
116117

117118
```json
118119
[{
@@ -134,7 +135,7 @@ The following example shows the schema of a policy state created event:
134135
}]
135136
```
136137

137-
The schema for a policy state changed event is similar:
138+
The schema for a policy state changed event scoped at the subscription level is similar:
138139

139140
```json
140141
[{
@@ -157,3 +158,98 @@ The schema for a policy state changed event is similar:
157158
```
158159

159160
---
161+
162+
# [Event Grid event schema](#tab/event-grid-event-schema)
163+
The following example shows the schema of a policy state created event scoped at the management group level:
164+
165+
```json
166+
[{
167+
"id": "5829794FCB5075FCF585476619577B5A5A30E52C84842CBD4E2AD73996714C4C",
168+
"topic": "/tenants/<tenantId>/providers/Microsoft.Management/managementGroups/<managementGroupId>",
169+
"subject": "/subscriptions/<SubscriptionID>/resourceGroups/<ResourceGroup>/providers/<ProviderNamespace>/<ResourceType>/<ResourceName>",
170+
"data": {
171+
"timestamp": "2021-03-27T18:37:42.4496956Z",
172+
"policyAssignmentId": "<policy-assignment-scope>/providers/microsoft.authorization/policyassignments/<policy-assignment-name>",
173+
"policyDefinitionId": "<policy-definition-scope>/providers/microsoft.authorization/policydefinitions/<policy-definition-name>",
174+
"policyDefinitionReferenceId": "",
175+
"complianceState": "NonCompliant",
176+
"subscriptionId": "<subscription-id>",
177+
"complianceReasonCode": ""
178+
},
179+
"eventType": "Microsoft.PolicyInsights.PolicyStateCreated",
180+
"eventTime": "2021-03-27T18:37:42.5241536Z",
181+
"dataVersion": "1",
182+
"metadataVersion": "1"
183+
}]
184+
```
185+
186+
The schema for a policy state changed event scoped at the management group level is similar:
187+
188+
```json
189+
[{
190+
"id": "5829794FCB5075FCF585476619577B5A5A30E52C84842CBD4E2AD73996714C4C",
191+
"topic": "/tenants/<tenantId>/providers/Microsoft.Management/managementGroups/<managementGroupId>",
192+
"subject": "/subscriptions/<SubscriptionID>/resourceGroups/<ResourceGroup>/providers/<ProviderNamespace>/<ResourceType>/<ResourceName>",
193+
"data": {
194+
"timestamp": "2021-03-27T18:37:42.4496956Z",
195+
"policyAssignmentId": "<policy-assignment-scope>/providers/microsoft.authorization/policyassignments/<policy-assignment-name>",
196+
"policyDefinitionId": "<policy-definition-scope>/providers/microsoft.authorization/policydefinitions/<policy-definition-name>",
197+
"policyDefinitionReferenceId": "",
198+
"complianceState": "NonCompliant",
199+
"subscriptionId": "<subscription-id>",
200+
"complianceReasonCode": ""
201+
},
202+
"eventType": "Microsoft.PolicyInsights.PolicyStateChanged",
203+
"eventTime": "2021-03-27T18:37:42.5241536Z",
204+
"dataVersion": "1",
205+
"metadataVersion": "1"
206+
}]
207+
```
208+
209+
# [Cloud event schema](#tab/cloud-event-schema)
210+
211+
The following example shows the schema of a policy state created event scoped at the management group level:
212+
213+
```json
214+
[{
215+
"id": "5829794FCB5075FCF585476619577B5A5A30E52C84842CBD4E2AD73996714C4C",
216+
"source": "/tenants/<tenantId>/providers/Microsoft.Management/managementGroups/<managementGroupId>",
217+
"subject": "/subscriptions/<SubscriptionID>/resourceGroups/<ResourceGroup>/providers/<ProviderNamespace>/<ResourceType>/<ResourceName>",
218+
"data": {
219+
"timestamp": "2021-03-27T18:37:42.4496956Z",
220+
"policyAssignmentId": "<policy-assignment-scope>/providers/microsoft.authorization/policyassignments/<policy-assignment-name>",
221+
"policyDefinitionId": "<policy-definition-scope>/providers/microsoft.authorization/policydefinitions/<policy-definition-name>",
222+
"policyDefinitionReferenceId": "",
223+
"complianceState": "NonCompliant",
224+
"subscriptionId": "<subscription-id>",
225+
"complianceReasonCode": ""
226+
},
227+
"type": "Microsoft.PolicyInsights.PolicyStateCreated",
228+
"time": "2021-03-27T18:37:42.5241536Z",
229+
"specversion": "1.0"
230+
}]
231+
```
232+
233+
The schema for a policy state changed event scoped at the management group level is similar:
234+
235+
```json
236+
[{
237+
"id": "5829794FCB5075FCF585476619577B5A5A30E52C84842CBD4E2AD73996714C4C",
238+
"source": "/tenants/<tenantId>/providers/Microsoft.Management/managementGroups/<managementGroupId>",
239+
"subject": "/subscriptions/<SubscriptionID>/resourceGroups/<ResourceGroup>/providers/<ProviderNamespace>/<ResourceType>/<ResourceName>",
240+
"data": {
241+
"timestamp": "2021-03-27T18:37:42.4496956Z",
242+
"policyAssignmentId": "<policy-assignment-scope>/providers/microsoft.authorization/policyassignments/<policy-assignment-name>",
243+
"policyDefinitionId": "<policy-definition-scope>/providers/microsoft.authorization/policydefinitions/<policy-definition-name>",
244+
"policyDefinitionReferenceId": "",
245+
"complianceState": "NonCompliant",
246+
"subscriptionId": "<subscription-id>",
247+
"complianceReasonCode": ""
248+
},
249+
"type": "Microsoft.PolicyInsights.PolicyStateChanged",
250+
"time": "2021-03-27T18:37:42.5241536Z",
251+
"specversion": "1.0"
252+
}]
253+
```
254+
255+
---

0 commit comments

Comments
 (0)