Skip to content

Commit fa50f5d

Browse files
committed
improve password cred removal
1 parent 1871edc commit fa50f5d

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tenant/Administration/Application Approval/Invoke-ExecApplication.ps1

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function Invoke-ExecApplication {
7474

7575
try {
7676
if ($Action -eq 'RemoveKey' -or $Action -eq 'RemovePassword') {
77-
# Handle credential removal by patching the object
77+
# Handle credential removal
7878
$KeyIds = $Request.Body.KeyIds.value ?? $Request.Body.KeyIds
7979
if (-not $KeyIds -or $KeyIds.Count -eq 0) {
8080
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
@@ -84,29 +84,49 @@ function Invoke-ExecApplication {
8484
return
8585
}
8686

87-
# Get the current application/service principal
88-
$CurrentObject = New-GraphGetRequest -Uri $Uri -tenantid $TenantFilter -AsApp $true
89-
9087
if ($Action -eq 'RemoveKey') {
91-
# Filter out the key credentials to remove
88+
# For key credentials, use a single PATCH request
89+
$CurrentObject = New-GraphGetRequest -Uri $Uri -tenantid $TenantFilter -AsApp $true
9290
$UpdatedKeyCredentials = $CurrentObject.keyCredentials | Where-Object { $_.keyId -notin $KeyIds }
9391
$PatchBody = @{
9492
keyCredentials = @($UpdatedKeyCredentials)
9593
}
94+
95+
$Response = New-GraphPOSTRequest -Uri $Uri -Type 'PATCH' -Body ($PatchBody | ConvertTo-Json -Depth 10) -tenantid $TenantFilter -AsApp $true
96+
97+
$Results = @{
98+
resultText = "Successfully removed $($KeyIds.Count) key credential(s) from $Type"
99+
state = 'success'
100+
details = @($Response)
101+
}
96102
} else {
97-
# Filter out the password credentials to remove
98-
$UpdatedPasswordCredentials = $CurrentObject.passwordCredentials | Where-Object { $_.keyId -notin $KeyIds }
99-
$PatchBody = @{
100-
passwordCredentials = @($UpdatedPasswordCredentials)
103+
# For password credentials, use bulk removePassword requests
104+
$BulkRequests = foreach ($KeyId in $KeyIds) {
105+
$RemoveBody = @{
106+
keyId = $KeyId
107+
}
108+
109+
@{
110+
id = $KeyId
111+
method = 'POST'
112+
url = "$($Type)$($IdPath)/removePassword"
113+
body = $RemoveBody
114+
headers = @{
115+
'Content-Type' = 'application/json'
116+
}
117+
}
101118
}
102-
}
103119

104-
# Update the object with the filtered credentials
105-
$null = New-GraphPOSTRequest -Uri $Uri -Type 'PATCH' -Body ($PatchBody | ConvertTo-Json -Depth 10) -tenantid $TenantFilter -AsApp $true
120+
$BulkResults = New-GraphBulkRequest -Requests @($BulkRequests) -tenantid $TenantFilter -AsApp $true
106121

107-
$Results = @{
108-
resultText = "Successfully removed $($KeyIds.Count) credential(s) from $Type"
109-
state = 'success'
122+
$SuccessCount = ($BulkResults | Where-Object { $_.status -eq 204 }).Count
123+
$FailureCount = ($BulkResults | Where-Object { $_.status -ne 204 }).Count
124+
125+
$Results = @{
126+
resultText = "Bulk RemovePassword completed. Success: $SuccessCount, Failures: $FailureCount"
127+
state = if ($FailureCount -eq 0) { 'success' } else { 'error' }
128+
details = @($BulkResults)
129+
}
110130
}
111131
} else {
112132
# Handle regular actions
@@ -132,4 +152,4 @@ function Invoke-ExecApplication {
132152
Body = @{ Results = @($Results) }
133153
})
134154
}
135-
}
155+
}

0 commit comments

Comments
 (0)