Skip to content

Commit 4ad8c3d

Browse files
authored
Merge pull request #723 from tdcthosc/tdc-improve-mt-1029-1032-tests
Improve mt.1029-1032 tests
2 parents 241dbb7 + 19c686b commit 4ad8c3d

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

powershell/public/Test-MtPimAlertsExists.ps1

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,17 @@ function Test-MtPimAlertsExists {
4141

4242
# Get PIM Alerts and store as object to be used in the test
4343
Write-Verbose "Getting PIM Alerts"
44-
$Alert = Invoke-MtGraphRequest -ApiVersion "beta" -RelativeUri "privilegedAccess/aadroles/resources/$($tenantId)/alerts" | Where-Object { $_.id -eq $AlertId -and $_.isActive -eq "True" }
45-
46-
$AffectedRoleAssignments = $Alert.additionalData | ForEach-Object {
47-
$CurrentItem = $_.item
48-
$result = New-Object psobject
49-
foreach ($entry in $CurrentItem.GetEnumerator()) {
50-
$result | Add-Member -MemberType NoteProperty -Name $entry.key -Value $entry.value -Force
44+
$Alert = Invoke-MtGraphRequest -ApiVersion "beta" -RelativeUri "privilegedAccess/aadroles/resources/$($tenantId)/alerts" | Where-Object { $_.id -eq $AlertId }
45+
46+
if ($Alert.Where({$_.isActive -eq "True"}).additionalData) {
47+
$AffectedRoleAssignments = $Alert.Where({$_.isActive -eq "True"}).additionalData | ForEach-Object {
48+
$CurrentItem = $_.item
49+
$result = New-Object psobject
50+
foreach ($entry in $CurrentItem.GetEnumerator()) {
51+
$result | Add-Member -MemberType NoteProperty -Name $entry.key -Value $entry.value -Force
52+
}
53+
$result
5154
}
52-
$result
5355
}
5456

5557
# Filtering based on (EntraOps) Enterprise Access Model Tiering
@@ -66,43 +68,44 @@ function Test-MtPimAlertsExists {
6668
Write-Verbose "$($_.AssigneeUserPrincipalName) has been defined as Break Glass and removed from $($Alert.id)"
6769
}
6870
$AffectedRoleAssignments = $AffectedRoleAssignments | Where-Object { $_.AssigneeId -notin $($FilteredBreakGlass).Id }
69-
70-
# Set number of affected Items to value of filtered items (for example, original alert has two affected items, but all of them are break glass and excluded from the test)
71-
$Alert.numberOfAffectedItems = $AffectedRoleAssignments.Count
7271
}
7372

74-
# Create test result and details
75-
if ($Alert.Count -gt "0" -and $AffectedRoleAssignments.Count -gt 0) {
73+
# Set number of affected Items to value of filtered items (for example, original alert has two affected items, but all of them are break glass and excluded from the test)
74+
$Alert.numberOfAffectedItems = $AffectedRoleAssignments.Count
7675

77-
$testDescription = "
76+
# Create test result and details
77+
$convertHtmlLinkToMD = '<a.*?href=["'']([^"'']*)["''][^>]*>([^<]*)<\/a>' # Regular expression to detect HTML links
78+
$testDescription = "
7879
7980
**Security Impact**`n`n
80-
$($Alert.securityImpact)
81+
$($Alert.securityImpact -replace $convertHtmlLinkToMD, '[$2]($1)')
8182
8283
**Mitigation steps**`n`n
83-
$($Alert.mitigationSteps)
84+
$($Alert.mitigationSteps -replace $convertHtmlLinkToMD, '[$2]($1)')
8485
8586
**How to prevent**`n`n
86-
$($Alert.howToPrevent)
87+
$($Alert.howToPrevent -replace $convertHtmlLinkToMD, '[$2]($1)')
8788
"
8889

89-
$AffectedRoleAssignmentSummary = @()
90-
$AffectedRoleAssignmentSummary += foreach ($AffectedRoleAssignment in $AffectedRoleAssignments) {
91-
if ($null -ne $AffectedRoleAssignment.AssigneeDisplayName -or $null -ne $AffectedRoleAssignment.RoleDisplayName) {
92-
" - $($AffectedRoleAssignment.AssigneeDisplayName) with $($AffectedRoleAssignment.RoleDisplayName) by AssigneeId $($AffectedRoleAssignment.AssigneeId)`n"
93-
} else {
94-
" - $($AffectedRoleAssignment.AssigneeName) ($($AffectedRoleAssignment.AssigneeUserPrincipalName))`n"
95-
}
90+
$AffectedRoleAssignmentSummary = @()
91+
$AffectedRoleAssignmentSummary += foreach ($AffectedRoleAssignment in $AffectedRoleAssignments) {
92+
if ($null -ne $AffectedRoleAssignment.AssigneeDisplayName -or $null -ne $AffectedRoleAssignment.RoleDisplayName) {
93+
" - $($AffectedRoleAssignment.AssigneeDisplayName) with $($AffectedRoleAssignment.RoleDisplayName) by AssigneeId $($AffectedRoleAssignment.AssigneeId)`n"
94+
} else {
95+
" - $($AffectedRoleAssignment.AssigneeName) ($($AffectedRoleAssignment.AssigneeUserPrincipalName))`n"
9696
}
97+
}
9798

99+
if ($Alert.Count -gt "0" -and $AffectedRoleAssignments.Count -gt 0) {
98100
$testResult = "$($Alert.alertDescription)`n`n
99101
$($AffectedRoleAssignmentSummary)
100102
Get more details from the PIM alert [$($Alert.alertName)](https://portal.azure.com/#view/Microsoft_Azure_PIMCommon/AlertDetail/providerId/aadroles/alertId/$($AlertId)/resourceId/$($tenantId)) in the Azure Portal.
101103
"
102-
103-
Add-MtTestResultDetail -Description $testDescription -Result $testResult
104+
} else {
105+
$testResult = "All privileged role assignments are managed by PIM. Well done!"
104106
}
105107

108+
Add-MtTestResultDetail -Description $testDescription -Result $testResult
106109
return $Alert
107110
}
108111
}

0 commit comments

Comments
 (0)