Skip to content

Commit 32ec17e

Browse files
sandeepjha000Copilot
andcommitted
fix: exclude Managed Identities from application ownership checks
Co-authored-by: Copilot <copilot@github.com>
1 parent badf15c commit 32ec17e

2 files changed

Lines changed: 10 additions & 11 deletions

File tree

src/powershell/private/tests-shared/Get-ApplicationsWithInsufficientOwners.ps1

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ function Get-ApplicationsWithInsufficientOwners {
1818
[string[]]$PrivilegeLevel
1919
)
2020

21-
# Get apps with permissions, pre-filtered to Application type only to avoid enriching non-ownable types (e.g. Managed Identities)
22-
$allApps = Get-ApplicationsWithPermissions -Database $Database -ServicePrincipalType 'Application'
21+
# Get all apps with permissions, excluding Managed Identities before enrichment as owners cannot be assigned to them
22+
$allApps = Get-ApplicationsWithPermissions -Database $Database -ExcludeServicePrincipalType 'ManagedIdentity'
2323

24-
# Filter the retrieved applications by privilege level and owner count
25-
$filteredApps = $allApps | Where-Object {
26-
($PrivilegeLevel -contains $_.Risk) -and ($_.OwnerCount -lt 2)
27-
}
24+
# Filter by privilege level and owner count
25+
$filteredApps = $allApps | Where-Object {($PrivilegeLevel -contains $_.Risk) -and ($_.OwnerCount -lt 2)}
2826

2927
Write-PSFMessage "Filtered to $($filteredApps.Count) applications with < 2 owners matching privilege levels: $($PrivilegeLevel -join ', ')" -Level Verbose
3028

src/powershell/private/tests-shared/Get-ApplicationsWithPermissions.ps1

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ function Get-ApplicationsWithPermissions {
1515
$Database,
1616

1717
[Parameter()]
18-
[string[]]$ServicePrincipalType
18+
[ValidateSet('Application', 'ManagedIdentity', 'Legacy', 'ServiceIdentity')]
19+
[string[]]$ExcludeServicePrincipalType
1920
)
2021

2122
# Query ServicePrincipal objects with permissions
@@ -54,10 +55,10 @@ order by spsi.lastSignInActivity.lastSignInDateTime
5455
return @()
5556
}
5657

57-
# Optionally filter by servicePrincipalType before enrichment to avoid unnecessary per-item DB calls
58-
if ($ServicePrincipalType) {
59-
$results = $results | Where-Object { $ServicePrincipalType -contains $_.servicePrincipalType }
60-
Write-PSFMessage "Pre-filtered to $($results.Count) service principals of type(s): $($ServicePrincipalType -join ', ')" -Level Verbose
58+
# Exclude specified service principal types before enrichment to avoid unnecessary per-item DB calls
59+
if ($ExcludeServicePrincipalType) {
60+
$results = $results | Where-Object { $ExcludeServicePrincipalType -notcontains $_.servicePrincipalType }
61+
Write-PSFMessage "Excluded $($ExcludeServicePrincipalType -join ', ') type(s), $($results.Count) service principals remaining" -Level Verbose
6162
}
6263

6364
# Enrich each app with permissions and risk classification (using Test-21770 pattern)

0 commit comments

Comments
 (0)