Skip to content

Commit 15d1a77

Browse files
authored
Merge pull request #1191 from microsoft/bugfix/issue-1176
Bugfix/issue 1176: Update application ownership check to exclude servicePrincipalType: 'ManagedIdentities'
2 parents ddd6605 + 8aa41da commit 15d1a77

2 files changed

Lines changed: 19 additions & 10 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 all apps with permissions
22-
$allApps = Get-ApplicationsWithPermissions -Database $Database
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 by privilege level and owner count using Where-Object (more efficient)
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: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,29 @@ function Get-ApplicationsWithPermissions {
1212
[CmdletBinding()]
1313
param(
1414
[Parameter(Mandatory = $true)]
15-
$Database
15+
$Database,
16+
17+
[Parameter()]
18+
[ValidateSet('Application', 'ManagedIdentity', 'Legacy', 'ServiceIdentity')]
19+
[string[]]$ExcludeServicePrincipalType
1620
)
1721

22+
# Build optional SQL exclusion clause for service principal types
23+
$excludeClause = ''
24+
if ($ExcludeServicePrincipalType) {
25+
$quoted = "'" + ($ExcludeServicePrincipalType -join "', '") + "'"
26+
$excludeClause = "`n and sp.servicePrincipalType not in ($quoted)"
27+
}
28+
1829
# Query ServicePrincipal objects with permissions
1930
# Used by tests 21770, 24518, and 21867
2031
$sql = @"
2132
select sp.id, sp.appId, sp.displayName, sp.appOwnerOrganizationId, sp.publisherName,
2233
spsi.lastSignInActivity.lastSignInDateTime,
23-
sp.owners, sp.signInAudience
34+
sp.owners, sp.signInAudience, sp.servicePrincipalType
2435
from main.ServicePrincipal sp
2536
left join main.ServicePrincipalSignIn spsi on spsi.appId = sp.appId
26-
where sp.id in
37+
where (sp.id in
2738
(
2839
select sp.id
2940
from main.ServicePrincipal sp
@@ -39,7 +50,7 @@ where sp.id in
3950
from main.ServicePrincipal) spAppRole
4051
on sp.appRoleId = spAppRole.id
4152
where permissionName is not null
42-
)
53+
))$excludeClause
4354
order by spsi.lastSignInActivity.lastSignInDateTime
4455
"@
4556

0 commit comments

Comments
 (0)