-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1065 from JohnDuprey/dev
CIPP-SAM permissions and fixes
- Loading branch information
Showing
4 changed files
with
173 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...les/CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Core/Invoke-ExecServicePrincipals.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
function Invoke-ExecServicePrincipals { | ||
<# | ||
.FUNCTIONALITY | ||
Entrypoint | ||
.ROLE | ||
CIPP.Core.ReadWrite | ||
#> | ||
[CmdletBinding()] | ||
param($Request, $TriggerMetadata) | ||
|
||
$TenantFilter = $env:TenantId | ||
|
||
$Success = $true | ||
|
||
$Action = $Request.Query.Action ?? 'Default' | ||
try { | ||
switch ($Request.Query.Action) { | ||
'Create' { | ||
$Body = @{ | ||
'appId' = $Request.Query.AppId | ||
} | ConvertTo-Json -Compress | ||
$Results = New-GraphPostRequest -Uri 'https://graph.microsoft.com/beta/servicePrincipals' -tenantid $TenantFilter -type POST -body $Body | ||
} | ||
default { | ||
if ($Request.Query.AppId) { | ||
$Action = 'Get' | ||
$Results = New-GraphGetRequest -Uri "https://graph.microsoft.com/beta/servicePrincipals(appId='$($Request.Query.AppId)')" -tenantid $TenantFilter -NoAuthCheck $true | ||
} else { | ||
$Action = 'List' | ||
$Results = New-GraphGetRequest -Uri 'https://graph.microsoft.com/beta/servicePrincipals?$top=999&$orderby=displayName&$count=true' -ComplexFilter -tenantid $TenantFilter -NoAuthCheck $true | ||
} | ||
} | ||
} | ||
} catch { | ||
$Results = $_.Exception.Message | ||
$Success = $false | ||
} | ||
|
||
$Metadata = @{ | ||
'Action' = $Action | ||
'Success' = $Success | ||
} | ||
|
||
if ($Request.Query.AppId) { | ||
$Metadata.AppId = $Request.Query.AppId | ||
} | ||
|
||
$Body = @{ | ||
'Results' = $Results | ||
'Metadata' = $Metadata | ||
} | ||
|
||
$Json = $Body | ConvertTo-Json -Depth 10 -Compress | ||
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ | ||
StatusCode = [HttpStatusCode]::OK | ||
Body = $Json | ||
}) | ||
} |
112 changes: 112 additions & 0 deletions
112
...CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Settings/Invoke-ExecSAMAppPermissions.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
function Invoke-ExecSAMAppPermissions { | ||
<# | ||
.FUNCTIONALITY | ||
Entrypoint | ||
.ROLE | ||
CIPP.SuperAdmin.ReadWrite | ||
#> | ||
[CmdletBinding()] | ||
param($Request, $TriggerMetadata) | ||
|
||
switch ($Request.Query.Action) { | ||
'Update' { | ||
try { | ||
$Permissions = $Request.Body.Permissions | ||
$Entity = @{ | ||
'PartitionKey' = 'CIPP-SAM' | ||
'RowKey' = 'CIPP-SAM' | ||
'Permissions' = [string]($Permissions | ConvertTo-Json -Depth 10 -Compress) | ||
} | ||
$Table = Get-CIPPTable -TableName 'AppPermissions' | ||
$null = Add-CIPPAzDataTableEntity @Table -Entity $Entity -Force | ||
$Body = @{ | ||
'Results' = 'Permissions Updated' | ||
} | ||
} catch { | ||
$Body = @{ | ||
'Results' = $_.Exception.Message | ||
} | ||
} | ||
} | ||
default { | ||
$ModuleBase = Get-Module -Name CIPPCore | Select-Object -ExpandProperty ModuleBase | ||
$SamManifest = Get-Item "$ModuleBase\Public\SAMManifest.json" | ||
$AdditionalPermissions = Get-Item "$ModuleBase\Public\AdditionalPermissions.json" | ||
|
||
$LastWrite = @{ | ||
'SAMManifest' = $SamManifest.LastWriteTime | ||
'AdditionalPermissions' = $AdditionalPermissions.LastWriteTime | ||
} | ||
|
||
$ServicePrincipals = New-GraphGetRequest -Uri 'https://graph.microsoft.com/beta/servicePrincipals?$top=999&$select=appId,displayName,appRoles,publishedPermissionScopes' -tenantid $env:TenantID -NoAuthCheck $true | ||
$SAMManifest = Get-Content -Path $SamManifest.FullName | ConvertFrom-Json | ||
$AdditionalPermissions = Get-Content -Path $AdditionalPermissions.FullName | ConvertFrom-Json | ||
|
||
$RequiredResources = $SamManifest.requiredResourceAccess | ||
|
||
$AppIds = ($RequiredResources.resourceAppId + $AdditionalPermissions.resourceAppId) | Sort-Object -Unique | ||
|
||
$Permissions = @{} | ||
foreach ($AppId in $AppIds) { | ||
$ServicePrincipal = $ServicePrincipals | Where-Object -Property appId -EQ $AppId | ||
$AppPermissions = [System.Collections.Generic.List[object]]@() | ||
$ManifestPermissions = ($RequiredResources | Where-Object -Property resourceAppId -EQ $AppId).resourceAccess | ||
$UnpublishedPermissions = ($AdditionalPermissions | Where-Object -Property resourceAppId -EQ $AppId).resourceAccess | ||
|
||
foreach ($Permission in $ManifestPermissions) { | ||
$AppPermissions.Add($Permission) | ||
} | ||
if ($UnpublishedPermissions) { | ||
foreach ($Permission in $UnpublishedPermissions) { | ||
$AppPermissions.Add($Permission) | ||
} | ||
} | ||
|
||
$ApplicationPermissions = [system.collections.generic.list[object]]@() | ||
$DelegatedPermissions = [system.collections.generic.list[object]]@() | ||
foreach ($Permission in $AppPermissions) { | ||
if ($Permission.id -match '^[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}$') { | ||
if ($Permission.type -eq 'Role') { | ||
$PermissionName = ($ServicePrincipal.appRoles | Where-Object -Property id -EQ $Permission.id).value | ||
} else { | ||
$PermissionName = ($ServicePrincipal.publishedPermissionScopes | Where-Object -Property id -EQ $Permission.id).value | ||
} | ||
} else { | ||
$PermissionName = $Permission.id | ||
} | ||
|
||
if ($Permission.type -eq 'Role') { | ||
$ApplicationPermissions.Add([PSCustomObject]@{ | ||
id = $Permission.id | ||
value = $PermissionName | ||
|
||
}) | ||
} else { | ||
$DelegatedPermissions.Add([PSCustomObject]@{ | ||
id = $Permission.id | ||
value = $PermissionName | ||
}) | ||
} | ||
} | ||
|
||
$ServicePrincipal = $ServicePrincipals | Where-Object -Property appId -EQ $AppId | ||
$Permissions.$AppId = @{ | ||
applicationPermissions = @($ApplicationPermissions | Sort-Object -Property label) | ||
delegatedPermissions = @($DelegatedPermissions | Sort-Object -Property label) | ||
} | ||
} | ||
|
||
$Body = @{ | ||
'Permissions' = $Permissions | ||
'LastUpdate' = $LastWrite | ||
} | ||
} | ||
} | ||
|
||
|
||
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ | ||
StatusCode = [HttpStatusCode]::OK | ||
Body = ConvertTo-Json -Depth 10 -InputObject $Body | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters