forked from CawaMS/EnableChangeNotifications
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangeNotifications.ps1
More file actions
424 lines (320 loc) · 15 KB
/
ChangeNotifications.ps1
File metadata and controls
424 lines (320 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<#
.SYNOPSIS
It's Azure Application Change Analysis Change Notifications Onboarding Script.
Script requires Az.Resources module, please follow the instructions to set it up https://docs.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-1.8.0
.DESCRIPTION
The script executes fololowing procedures in order:
1. Registers Microsoft.ChangeAnalysis resource provider if it's not registered.
2. Checks whether private preview feature flag is enlisted in the specified subscription and exits if not.
3. Checks if change notifications configuration profile already exists, and if not creates a new one. It extracts managed identity service principal from identity tag of the profile. This service principal will be accessing workspace to get shared keys.
4. Checks and creates custom role with 2 required actions to access shared keys from workspace scoped for WorkspaceSubscriptionId.
5. Checks and assigns custom role definition from #4 to managed identity service principal from #3 and scope is WorkspaceResourceId.
6. Updates change configuration profile with details of the workspace.
.PARAMETER SubscriptionId
The subscription guid to enable change notifications for. If nothing is specified, the default selected subscription selected during azure login will be selected from Get-AzContent
.PARAMETER WorkspaceId
The workspace Id GUID. Can be found in the Azure Monitor Workspace -> Properties blade.
.PARAMETER WorkspaceSubscriptionId
The workspace subscription Id GUID. Can be found in the Azure Monitor Workspace -> Properties blade.
.PARAMETER WorkspaceResourceId
The full ARM Azure Monitor Workspace Id that should look like : "/subscriptions/{SubscriptionID}/resourcegroups/{ResourceGroupName}/providers/microsoft.operationalinsights/workspaces/{AzureMonitorWorkspaceName}"
.PARAMETER ActivationState
Set to Disabled to disable notifications, or set to Enable to start sending to workspace.
.PARAMETER IncludeChangeDetails
Set to Exclude to exclude old/new change values from event details, or set to Include to include old/new change values in the event details. Warning this field may contain sensitive information.
.PARAMETER Location
The closest location to your resources. To get list of all locations run Get-AzLocation cmdlet - use location property from response.
.INPUTS
None. You cannot pipe objects to ChangeNotifications.ps1.
.OUTPUTS
None. ChangeNotifications.ps1 does not generate any output.
.EXAMPLE
PS> .\ChangeNotifications.ps1 -SubscriptionId 3<Subscription GUID> -ActivationState Enabled -IncludeChangeDetails Include -WorkspaceId <Workspace Id GUID> -WorkspaceResourceId <Workspace Resource Id> -Location <location>
#>
param (
[Parameter(Mandatory)]
[ValidateScript( {
try {
[System.Guid]::Parse($_) | Out-Null
$true
}
catch {
$false
}
})]
[string] $SubscriptionId = $null,
[Parameter(Mandatory)]
[ValidateScript( {
try {
[System.Guid]::Parse($_) | Out-Null
$true
}
catch {
$false
}
})]
[string] $WorkspaceId = $null,
[string] $WorkspaceSubscriptionId = $null,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string] $WorkspaceResourceId = $null,
[Parameter(Mandatory)]
[ValidateSet("Enabled", "Disabled")]
[string] $ActivationState = "Enabled",
[Parameter(Mandatory)]
[ValidateSet("Exclude", "Include")]
[string] $IncludeChangeDetails = "Exclude",
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[ValidateScript( { (Get-AzLocation | Where-Object Location -eq $_).Count -eq 1 })]
[string] $Location = "eastus"
)
# In case workspace subscription Id is not specified, defer to onboarded subsription id.
if ([string]::IsNullOrEmpty($WorkspaceSubscriptionId)){
$WorkspaceSubscriptionId = $SubscriptionId
if (! $WorkspaceResourceId.Contains($WorkspaceSubscriptionId)){
Write-Error "Ensure specified WorkspaceResourceId = $WorkspaceResourceId is defined in specified WorkspaceSubscriptionId = $WorkspaceSubscriptionId"
Exit
}
}
Write-Host "`nExecuting script to enable/disable notifications for subscription '$SubscriptionId'."
Write-Host "The Azure Monitor Workspace details:"
Write-Host "The specified Workspace Subscription Id = '$WorkspaceSubscriptionId'"
Write-Host "The specified Workspace Id = '$WorkspaceId'"
Write-Host "The specified Workspace ResourceId = '$WorkspaceResourceId'"
Write-Host "The change notifications feature will be : '$ActivationState'"
Write-Host "Include change details property is set to '$IncludeChangeDetails' change details in event payload.`n"
#####################################
# Global definitions:
#####################################
# Resource provider name
$ResourceProviderNamespace = "Microsoft.ChangeAnalysis"
# Private feature
$RequiredFeatureName = "NotificationsPrivatePreview"
# Custom role definition name
$CustomRoleDefinitionName = "Azure Application Change Analysis Workspace Access Role $WorkspaceSubscriptionId"
# Initial notifications configuration profile
$EmptyProfile = @{
identity = @{
type = "SystemAssigned"
}
location = $Location
}
# Updated notifications configuration profile.
$UpdatedProfile = @{
Properties = @{
Notifications = @{
AzureMonitorWorkspaceProperties = @{
WorkspaceId = $WorkspaceId
WorkspaceResourceId = $WorkspaceResourceId
IncludeChangeDetails = $IncludeChangeDetails
}
ActivationState = $ActivationState
}
}
identity = @{
type = "SystemAssigned"
}
location = $Location
}
#
# Method : check if private preview feature flag is enabled in the subscription and exits if not.
#
function CheckRequiredFeatureFlag() {
Write-Host "Checking whether requred feature flag '$RequiredFeatureName' is added to the subscription."
$feature = Get-AzProviderFeature -ProviderNamespace $ResourceProviderNamespace -FeatureName $RequiredFeatureName
# Check whether feature flag is available and requires installation
if ($? -and $feature.RegistrationState -eq "Registered") {
Write-Host "Notifications private preview feature flag is registered."
return $true;
}
else {
Write-Host "The notficiations private preview feature flag is not registered, please send email to changeanalysishelp@microsoft.com with a list of your subscription ids to onboard. The approval is manual at this point since it's a private preview."
return $false;
}
}
#
# Method: Registers Microsoft.ChangeAnalysis
#
function CheckAndRegisterProvider() {
Write-Host "Checking status of Resource Provider '$ResourceProviderNamespace'"
$provider = Get-AzResourceProvider -ProviderNamespace $ResourceProviderNamespace
if (!$?) {
Write-Host "Couldn't find Resource Provider namespace '$ResourceProviderNamespace' available for selected subscription"
return
}
elseif ($provider.RegistrationState -eq "Registered" -or $provider.RegistrationState -eq "Registering") {
Write-Host "Resource provider namespace '$ResourceProviderNamespace' is already registered, skipping..."
}
elseif ($provider.RegistrationState -ne "Registered") {
Register-AzResourceProvider -ProviderNamespace $ResourceProviderNamespace
if ($?) {
Write-Host "It would take few minutes for provider to register ..."
do {
Write-Host "Going to wait for 20 seconds until provider gets into Registered state..."
Start-Sleep 20
$provider = Get-AzResourceProvider -ProviderNamespace $ResourceProviderNamespace
}while ($provider.RegistrationState -ne "Registered" -or $provider.RegistrationState -ne "Registering")
Write-Host "Microsoft.ChangeAnalysis Provider is registered.`n"
}
}
}
function CreateCustomRoleDefinition() {
$role = [Microsoft.Azure.Commands.Resources.Models.Authorization.PSRoleDefinition]::new()
$role.Name = $CustomRoleDefinitionName
$role.Description = 'Microsoft.ChangeAnalysis Provider notifications configuration profile access role to Azure Monitor Workspace to query shared keys'
$role.IsCustom = $true
$perms = 'Microsoft.OperationalInsights/workspaces/sharedKeys/action', 'Microsoft.OperationalInsights/workspaces/sharedKeys/read', 'Microsoft.OperationalInsights/workspaces/read' , 'Microsoft.OperationalInsights/workspaces/listKeys/action'
$role.Actions = $perms
$subs = "/subscriptions/$WorkspaceSubscriptionId"
if ($WorkspaceSubscriptionId -ne $SubscriptionId) {
$subs = "/subscriptions/$WorkspaceSubscriptionId", "/subscriptions/$SubscriptionId"
}
$role.AssignableScopes = $subs
Write-Host "Composing a custom role definition object to access Azure Monitor Workspace. The role name: '$($role.Name)' and scope '$subs'"
return $role
}
#
# Custom role definition is recreated for each subscription, because assignable scope of custom roles is subscription level
# Role definition contains same list of allowed actions
#
function AddCustomRoleDefinition() {
Write-Host "Checking whether custom role definition with name '$CustomRoleDefinitionName' is already registered."
$customRoleDefinition = Get-AzRoleDefinition -Name $CustomRoleDefinitionName
if ($null -eq $customRoleDefinition) {
$role = CreateCustomRoleDefinition
$definition = New-AzRoleDefinition -Role $role
if ($? -and $null -ne $definition) {
Write-Host "New custom role definition is added with id $($definition.Id)"
return
}
Write-Host "Failed to setup custom role definition, exiting..."
}
else {
Write-Host "Custom role definition with name '$CustomRoleDefinitionName' is already registered, skipping ..."
}
}
#
# Method assigned custom role to a specified service principal.
#
function AddCustomRoleToServicePrincipal($servicePrincipal) {
Write-Host "Check whether required custom role is already assigned to managed identity service principal '$servicePrincipal'"
$assignment = Get-AzRoleAssignment -ObjectId $servicePrincipal -RoleDefinitionName $CustomRoleDefinitionName
if ($? -and $null -ne $assignment) {
Write-Host "Custom role definition '$CustomRoleDefinitionName' is already assigned, skipping ..."
return $true
}
$roleDefinition = Get-AzRoleDefinition -Name $CustomRoleDefinitionName
if (!$? -or $null -eq $roleDefinition) {
Write-Error "Failed to retrieve custom role definition with name = '$CustomRoleDefinitionName' from subscription $subscriptionId"
return $false
}
New-AzRoleAssignment -ObjectId $servicePrincipal -RoleDefinitionId $roleDefinition.Id -Scope $WorkspaceResourceId
return $true
}
#
# Method: Create Or Update configuration profile.
#
function CreateOrUpdateConfigurationProfile($httpMethod, $payload){
$context = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile.DefaultContext
$token = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account, $context.Environment, $context.Tenant.Id.ToString(), $null, [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never, $null, "https://management.azure.com").AccessToken
# Authorization header
$headers = @{
Accept = "application/json"
Authorization = "Bearer $token"
}
# End Point to execute
$ApiEndPoint = "https://management.azure.com/subscriptions/$subscriptionId/providers/Microsoft.ChangeAnalysis/profile/default?api-version=2020-04-01-preview"
try {
$result = $null
if ($null -eq $payload -and $httpMethod -eq "GET") {
$result = Invoke-RestMethod -Method GET -Uri $ApiEndPoint -ContentType "application/json" -Headers $headers
}
else {
$result = Invoke-RestMethod -Method $httpMethod -Uri $ApiEndPoint -Body (ConvertTo-Json $payload -Depth 5) -ContentType "application/json" -Headers $headers
}
return $result;
}
catch {
$statusCode = $_.Exception.Response.StatusCode.value__
if ($statusCode -eq "404" -and $httpMethod -eq "GET") {
return "NotFound"
}
Write-Error "`nFailed to execute '$httpMethod' with StatusCode: '$statusCode' on '$ApiEndPoint' with error StatusDescription: $_.Exception.Response.StatusDescription`n"
}
return "Error"
}
#
# Method: main
#
function Main() {
# check required modules
if (-not (Get-Module -ListAvailable -Name "Az.Resources")) {
Write-Host "Module Az.Resources is not installed, please follow the instructions to set it up https://docs.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-1.8.0"
return
}
# load modules
Import-Module Az -ErrorAction SilentlyContinue
if (! (Get-AzContext).Account) {
Write-Host "Connecting to Azure ..."
Connect-AzAccount
}
Write-Host "Selecting subscription ..."
if (!$SubscriptionId) {
$SubscriptionId = (Get-AzContext).Subscription.Id
}
else {
Get-AzSubscription -SubscriptionId $SubscriptionId | Set-AzContext
}
if (!$?) {
Write-Error "`nScript experienced an error to setup default subscription, aborting...`n"
Exit
}
Write-Host "Current subscription $subscriptionId"
Write-Host "Checking whether Microsoft.ChangeAnalysis is registered with subscription and adding it if not.`n"
# Check if provider is registered and if not try to register it and wait.
CheckAndRegisterProvider
Write-Host "Check required feature flag."
#Check if feature flag exists and if not exit.
if ( (CheckRequiredFeatureFlag) -eq $false) {
Exit
}
Write-Host "`nCreating empty configuration profile"
# Check or Create empty profile.
Write-Host "Check if subscription '$SubscriptionId' already has a registered notifications configuration profile."
$response = CreateOrUpdateConfigurationProfile "GET" $null
if ($response -eq "NotFound") {
Write-Host "Configuration profile doesnt exist, going to create new."
$response = CreateOrUpdateConfigurationProfile "PUT" $EmptyProfile
if ($response -eq "Error") {
Write-Error "Failed to create configuration profile, aborting."
Exit
}
}
elseif ($response -eq "Error") {
Write-Error "Failed to check if notifications configuration profile already registered with subscription."
Exit
}
else {
Write-Host "Notifications configuration profile already exists for current subscription.`n"
}
$servicePrincipalToAssign = $response.identity.principalId
Write-Host "`nAzure Application Change Analysis Notifications Profile Managed Identity '$servicePrincipalToAssign' `n"
Write-Host "Going to check and create (if needed) a custom role definition to access workspace '$WorkspaceResourceId' shared keys"
AddCustomRoleDefinition
Write-Host
if ( (AddCustomRoleToServicePrincipal $servicePrincipalToAssign) -eq $false) {
Write-Error "There was error to assign role to managed identity service principal."
Exit
}
Write-Host "`nUpdating configuration profile with final settings."
$response = CreateOrUpdateConfigurationProfile "PATCH" $UpdatedProfile
if ($response -eq "Error") {
Write-Error "Failed to update configuration profile, aborting..."
Exit
}
Write-Host "`nAt this point everything should be completed within several hours the change notifications should appear in selected workspace!`n"
Exit
}
# Entry point:
Main