Skip to content

Commit badf15c

Browse files
sandeepjha000Copilot
andcommitted
pre-filter service principal type before enrichment to improve performance
Co-authored-by: Copilot <copilot@github.com>
1 parent 9028ea3 commit badf15c

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

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

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

21-
# Get all apps with permissions
22-
$allApps = Get-ApplicationsWithPermissions -Database $Database
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'
2323

24-
# Filter by privilege level and owner count using Where-Object (more efficient)
25-
# Only include Application type
24+
# Filter the retrieved applications by privilege level and owner count
2625
$filteredApps = $allApps | Where-Object {
27-
($PrivilegeLevel -contains $_.Risk) -and ($_.OwnerCount -lt 2) -and ($_.servicePrincipalType -eq 'Application')
26+
($PrivilegeLevel -contains $_.Risk) -and ($_.OwnerCount -lt 2)
2827
}
2928

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

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ function Get-ApplicationsWithPermissions {
1212
[CmdletBinding()]
1313
param(
1414
[Parameter(Mandatory = $true)]
15-
$Database
15+
$Database,
16+
17+
[Parameter()]
18+
[string[]]$ServicePrincipalType
1619
)
1720

1821
# Query ServicePrincipal objects with permissions
@@ -51,6 +54,12 @@ order by spsi.lastSignInActivity.lastSignInDateTime
5154
return @()
5255
}
5356

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
61+
}
62+
5463
# Enrich each app with permissions and risk classification (using Test-21770 pattern)
5564
$enrichedApps = @()
5665

0 commit comments

Comments
 (0)