-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMikeandMarcusAccessReviewTesting-phase2.ps1
More file actions
549 lines (502 loc) · 19.7 KB
/
MikeandMarcusAccessReviewTesting-phase2.ps1
File metadata and controls
549 lines (502 loc) · 19.7 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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
function BuildScopes
{
<#
.SYNOPSIS
Create Scopes block based on provided policy type
#>
param(
[parameter(Mandatory = $true)]
[object]$TargetObject,
[parameter(Mandatory = $true)]
[ValidateSet('Group', 'ServicePrincipal', 'Application')]
[string]$PolicyType
)
switch -regex ($PolicyType)
{
'Group'
{
return @{
'@odata.type' = '#microsoft.graph.principalResourceMembershipsScope'
principalScopes = @(
@{
'@odata.type' = '#microsoft.graph.accessReviewQueryScope'
'query' = "/v1.0/users?`$filter=userType eq 'Guest'"
'queryType' = 'MicrosoftGraph'
}
)
resourceScopes = @(
@{
'@odata.type' = '#microsoft.graph.accessReviewQueryScope'
'query' = "/v1.0/groups/$($TargetObject.Id)/members/microsoft.graph.user/?`$count=true&`$filter=(userType eq 'Guest')"
'queryType' = 'MicrosoftGraph'
},
@{
'@odata.type' = '#microsoft.graph.accessReviewQueryScope'
'query' = "/beta/teams/$($TargetObject.Id)/channels?`$filter=membershipType eq 'shared'"
'queryType' = 'MicrosoftGraph'
}
)
}
}
'ServicePrincipal|Application'
{
return @{
'@odata.type' = '#microsoft.graph.principalResourceMembershipsScope'
principalScopes = @(
@{
'@odata.type' = '#microsoft.graph.accessReviewQueryScope'
'query' = "/v1.0/users?`$filter=userType eq 'Guest'"
'queryType' = 'MicrosoftGraph'
},
@{
'@odata.type' = '#microsoft.graph.accessReviewQueryScope'
'query' = "./members/microsoft.graph.user/?`$count=true&`$filter=(userType eq 'Guest')"
'queryType' = 'MicrosoftGraph'
'queryRoot' = '/v1.0/groups'
}
)
resourceScopes = @(
@{
'@odata.type' = '#microsoft.graph.accessReviewQueryScope'
'query' = "/v1.0/servicePrincipals/$($TargetObject.Id)"
'queryType' = 'MicrosoftGraph'
}
)
}
}
}
}
function ApplyPolicy
{
<#
.SYNOPSIS
ApplyPolicy creates and applies a new governance policy
.DESCRIPTION
ApplyPolicy creates and applies a new governance policy on the specified object
.PARAMETER PolicyTarget
Object to apply policy to
.PARAMETER PolicyType
Type of policy to create based on object type
Group
ServicePrincipal
Application
.PARAMETER Start
DateTime of when policy is to go into effect
.PARAMETER FallbackReviewers
Array of fallback reviewers to assign
#>
param(
[parameter(Mandatory = $true)]
[object]$PolicyTarget,
[parameter(Mandatory = $true)]
[ValidateSet('Group', 'ServicePrincipal', 'Application')]
[string]$PolicyType,
[parameter(Mandatory = $true)]
[datetime]$Start,
[parameter(Mandatory = $true)]
[String[]]$FallbackReviewers
)
#build start date string
$startDate = $Start.ToString('yyyy-MM-dd')
#region CreatorInfo
if ($null -eq $CreatedBy)
{
$createdBy = @{
id = $PolicyTarget.Owners[0].Id
displayName = $PolicyTarget.Owners[0].AdditionalProperties.displayName
userPrincipalName = $PolicyTarget.Owners[0].AdditionalProperties.userPrincipalName
}
}
#endregion CreatorInfo
#region params
# Main param builder
#
$BannerText = 'Guest membership access review'
#
$ReviewTarget = $PolicyTarget.Id
# If the owners were found via application we need to get the objectId of the app to correctly set reviews
if ($PolicyType -eq 'Application')
{
$ReviewTarget = (Get-MgApplication -Filter "appid eq '$($PolicyTarget.AppId)'").Id
}
$params = @{
CreatedBy = $createdBy
DisplayName = "$BannerText - $($PolicyTarget.DisplayName)"
DescriptionForAdmins = "$BannerText- $($PolicyTarget.DisplayName)"
DescriptionForReviewers = 'Access by guests to Microsoft corporate resources, including via groups or app assignments, should reviewed regularly. For more details, please see: https://aka.ms/GuestReview'
Scope = BuildScopes -TargetObject $PolicyTarget -PolicyType $PolicyType
Reviewers = @(
@{
Query = "/$($PolicyType)s/$ReviewTarget/owners"
QueryType = 'MicrosoftGraph'
}
)
Settings = @{
MailNotificationsEnabled = $true
ReminderNotificationsEnabled = $true
JustificationRequiredOnApproval = $false
InstanceDurationInDays = 30
RecommendationsEnabled = $true
# Apply actions by default if reviewers failed to perform review
DefaultDecisionEnabled = $true
DefaultDecision = 'Deny'
AutoApplyDecisionsEnabled = $true
ApplyActions = @(
@{
'@odata.type' = '#microsoft.graph.removeAccessApplyAction'
}
)
# Set recommendation to look at users inactive over the last 90 days
recommendationInsightSettings = @(
@{
'@odata.type' = '#microsoft.graph.userLastSignInRecommendationInsightSetting'
recommendationLookBackDuration = 'P90D'
signInScope = 'tenant'
}
)
# Semi-annual review cycle
Recurrence = @{
Pattern = @{
type = 'absoluteMonthly'
Interval = 6
}
Range = @{
type = 'noEnd'
StartDate = $StartDate
}
}
}
}
#endregion params
#region fallbackreviewers
# Only add param if list is non-null
if ($FallbackReviewers.Count -gt 0)
{
$FallbackReviewList = @()
foreach ($r in $FallbackReviewers)
{
$FallbackReviewList += @{
Query = "/users/$r"
QueryType = 'MicrosoftGraph'
}
}
$params.Add('FallbackReviewers', $FallbackReviewList)
}
#endregion fallbackreviewers
try
{
$ret = New-MgIdentityGovernanceAccessReviewDefinition -BodyParameter $params -ErrorAction Stop
Write-Verbose "Successfully created AR for $($PolicyTarget.Id)/$($PolicyTarget.DisplayName)"
[PSCustomObject]@{
AccessReviewId = $ret.Id
PolicyType = $PolicyType
ImplementedDateTime = Get-Date -Format 'yyyy-MM-dd'
StartDateTime = $Start
TargetObject = $PolicyTarget.Id
}
}
catch
{
Write-Error "Failed to create AR for $($PolicyTarget.Id)/$($PolicyTarget.DisplayName): $_"
}
}
#region Reviewers
<#
ValidateBackupReviewers - perform lookup on provided entries to ensure valid accounts
Add entries to hashtable to reduce subsequent graph calls
Build list of reviewers for inclusion in New-MgIdentityGovernanceAccessReviewDefinition params
#>
function ValidateBackupReviewers
{
[OutputType([String[]])]
param(
[parameter(Mandatory = $true)]
[String[]]$reviewList
)
$validReviewers = @()
foreach ($user in $reviewList)
{
if ($_userTable.ContainsKey($user))
{
$validReviewers += $_userTable[$user]
}
else
{
try
{
$u = Get-MgUser -UserId $user -ExpandProperty manager -ErrorAction Stop
Write-Verbose "Added user $user to reviewers list"
$_userTable.Add($user, $u.Id)
AddOwnerManager -ownerId $u.Id -managerId $u.Manager.Id
$validReviewers += $u.Id
}
catch
{
Write-Verbose "Unable to resolve $user as valid reviewer"
}
}
}
$validReviewers
}
function AddOwnerManager
{
param(
[parameter(Mandatory = $true)]
[String]$ownerId,
[parameter(Mandatory = $true)]
[String]$managerId
)
if ($_managerList.ContainsKey($o))
{
if (-not [string]::IsNullOrEmpty($_managerList[$o]))
{
$mlist += $_managerList[$ownerId]
}
else
{
Write-Verbose "Adding $($mgr.Id) for owner $o"
$_managerList.Add($ownerId, $managerId)
}
}
}
<#
RetrieveOwnerManagers - perform lookup of managers for provided entries
Add entries to hashtable to reduce subsequent graph calls
Build list of reviewers for inclusion in New-MgIdentityGovernanceAccessReviewDefinition params
#>
function RetrieveOwnerManagers
{
[OutputType([String[]])]
param(
[parameter(Mandatory = $true)]
[String[]]$ownerList
)
$mlist = @()
foreach ($o in $ownerList)
{
if ($_managerList.ContainsKey($o) -and -not [string]::IsNullOrEmpty($_managerList[$o]))
{
$mlist += $_managerList[$o]
}
else
{
# Should never get here as manager should've been added during user retrieval and $expand
$mgr = Get-MgUserManager -UserId $o -ErrorAction SilentlyContinue
if ($null -ne $mgr)
{
Write-Verbose "Adding $($mgr.Id) for owner $o"
$_managerList.Add($o, $mgr.Id)
$mlist += $mgr.Id
}
else
{
$_managerList.Add($o, $null)
}
}
}
$mlist
}
#endregion Reviewers
#
# https://learn.microsoft.com/en-us/azure/templates/microsoft.authorization/accessreviewscheduledefinitions?pivots=deployment-language-bicep
# https://github.com/microsoftgraph/microsoft-graph-docs/blob/main/api-reference/beta/api/accessreviewset-post-definitions.md
# https://learn.microsoft.com/en-us/graph/api/accessreviewset-post-definitions?view=graph-rest-beta&tabs=http
function Set-AzureGovernancePolicy
{
<#
.SYNOPSIS
Set access review policy on an group
.DESCRIPTION
The Set-AzureGovernancePolicy cmdlet applies a default access review policy in the specified object(s)
.PARAMETER Id
Specifies the id (ObjectId) of an object in Azure Active Directory
.PARAMETER Start
Date to start the access review
.PARAMETER ProcessLegacyGroups
Process objects with less than required owner count
.PARAMETER BackupReviewers
Optional list of backup reviewers to add to access review. If not specified, function will attempt to create backup reviewers based on object owners' managers
.PARAMETER CreatedById
Optional object id of object to assign as creator of reviews. Default is FirstOrDefault owner of object
.PARAMETER LogFilePath
Optiona destination path for reporting purposes
.PARAMETER PolicyType
Type of policy for object object type
Group
ServicePrincipal
Application
.EXAMPLE
'7fce970a-291c-4674-a556-37c1e82df090', '9a0f0187-ad3d-4f69-8bb1-5287dac84acc' | Set-AzureGovernancePolicy -StartDate '5-1-2024' -PolicyType Group
.EXAMPLE
(Get-MgGroup -Filter "startswith(displayName,'ADO')" -CountVariable gcount -ConsistencyLevel Eventual).Id | Set-AzureGovernancePolicy -PolicyType Group -ProcessLegacyGroups:$true
.LINK
https://learn.microsoft.com/en-us/graph/api/accessreviewset-post-definitions?view=graph-rest-beta&tabs=http
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true,
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = 'ObjectId of the target object')]
[Alias('ObjectId')]
[ValidatePattern('^([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}$')]
[string]$Id,
[Parameter(Mandatory = $false,
ValueFromPipelineByPropertyName = $true)]
[datetime]$StartDate,
[Parameter(Mandatory = $false,
ValueFromPipelineByPropertyName = $true)]
[string[]]$BackupReviewers,
[Parameter(Mandatory = $false,
ValueFromPipelineByPropertyName = $true)]
[bool]$ProcessLegacyGroups = $false,
[parameter(Mandatory = $false)]
[String]$CreatedById,
[parameter(Mandatory = $false)]
[String[]]$LogFilePath,
[Parameter(Mandatory = $true)]
[ValidateSet('Group', 'ServicePrincipal', 'Application')]
[string]$PolicyType
)
Begin
{
# Test for authenticated session before any pipeline activity
$ctx = Get-MgContext
if ($null -eq $ctx)
{
throw 'You must be authenticated using Connect-MgGraph'
}
# Save home tenantId for app check
$_homeTenant = $ctx.TenantId
if ($null -eq $StartDate)
{
$StartDate = (Get-Date).AddDays(5)
}
$_managerList = @{}
$_userTable = @{}
if ($null -eq $CreatedById)
{
Write-Warning 'CreatedBy not set, using application owner as default'
}
else
{
try
{
$u = Get-MgUser -UserId $CreatedById -Property id, displayname, userprincipalname -ErrorAction Stop
$createdBy = @{
Id = $u.Id
displayName = $u.DisplayName
userPrincipalName = $u.UserPrincipalName
}
}
catch
{
Write-Warning "Unable to retrieve $CreatedById, using application owner as default"
}
}
}
Process
{
$validBackupReviewers = @()
switch ($PolicyType)
{
'Group'
{
# Validate group and owners
try
{
$target = Get-MgGroup -GroupId $id -ExpandProperty owners -Property id, displayName, owners
}
catch
{
Write-Error "Error retrieving information for group $Id - $_"
return $null
}
$target = $grp
$ownerList = $target.Owners
break
}
'ServicePrincipal'
{
# Validate Service Principal and owners
try
{
$sp = Get-MgServicePrincipal -ServicePrincipalId $id -ExpandProperty Owners -Property id, displayname, owners, appid, AppOwnerOrganizationId
}
catch
{
Write-Error "Error retrieving information for group $Id - $_"
continue
}
$target = $sp
$ownerlist = $sp.owners
# If SP is missing owners, check to see if it's registered in the home tenant and try ownership from there
if (($null -eq $ownerList -or $ownerlist.Count -eq 0) -and $target.AppOwnerOrganizationId -eq $_homeTenant)
{
Write-Warning "SP $id missing mandatory owners count, checking app registration for owners"
$app = Get-MgApplication -Filter "appid eq '$($sp.appid)'" -Property owners -ExpandProperty owners -ErrorAction SilentlyContinue
If ($null -eq $app -or $app.owners.count -eq 0)
{
if ($WriteWarningsToFile)
{
"Application $($app.DisplayName) missing mandatory owner count, skipping AR processing" | Out-File -FilePath $WarningOutputFilePath -Append -Encoding ascii #easier to read for me than the 2&>1 stuff
}
# Nothing here, time to leave
return
}
Else
{
$ownerlist = $app.Owners
$PolicyType = 'Application'
}
}
# Set ProcessLegacyGroups to bypass later group-based AR skip
$ProcessLegacyGroups = $true
}
}
#region buildownerlists
# If the object has less than the mandated 2 users and we're not processing legacy groups then notify and skip
if ($ownerListCount -lt 2 -and -not $ProcessLegacyGroups)
{
Write-Warning "$PolicyType $id missing mandatory owner count, skipping AR processing"
If ($WriteWarningsToFile)
{
"$PolicyType $id missing mandatory owner count, skipping AR processing" | Out-File -FilePath $WarningOutputFilePath -Append -Encoding ascii #easier to read for me than the 2&>1 stuff
}
continue
}
# In cases where there is no owner, require backup reviewer be provided or notify and continue pipeline
if ($ownerList.Count -eq 0 -and $BackupReviewers.Count -eq 0)
{
Write-Warning "$PolicyType $id has no reviewers available, skipping AR processing"
If ($LogFilePath)
{
"$PolicyType $id has no reviewers available, skipping AR processing" | Out-File -FilePath $LogFilePath -Append -Encoding ascii #easier to read for me than the 2&>1 stuff
}
continue
}
# Owners present so build list to include managers and any provided backup reviewers
if ($ownerList.Count -gt 0)
{
$validBackupReviewers = @(RetrieveOwnerManagers -ownerList $ownerList.Id | Sort-Object -Unique)
}
if ($BackupReviewers.Count -gt 0)
{
$validBackupReviewers += ValidateBackupReviewers -reviewList $BackupReviewers
}
if ($validBackupReviewers.Count -eq 0)
{
Write-Warning "No fallback reviews available for $PolicyType $($target.Id)"
If ($LogFilePath)
{
"No fallback reviews available for $PolicyType $($target.Id)" | Out-File -FilePath $LogFilePath -Append -Encoding ascii #easier to read for me than the 2&>1 stuff
}
}
#endregion buildownerlists
ApplyPolicy -PolicyTarget $target -PolicyType $PolicyType -Start $StartDate -FallbackReviewers $validBackupReviewers
}
}
$spid = '50541732-c369-4b60-b0cb-340a2cd9a833'
$spid = '13fbfc35-8dab-48ed-bdb4-21f632e8ce67'
Set-AzureGovernancePolicy -Id $spid -PolicyType ServicePrincipal # -Verbose