Skip to content

Commit 5d35bf9

Browse files
committed
improve tenant access check
1 parent 281032a commit 5d35bf9

File tree

2 files changed

+53
-19
lines changed

2 files changed

+53
-19
lines changed

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Settings/Invoke-ExecAccessChecks.ps1

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using namespace System.Net
22

3-
Function Invoke-ExecAccessChecks {
3+
function Invoke-ExecAccessChecks {
44
<#
55
.FUNCTIONALITY
66
Entrypoint
@@ -50,16 +50,17 @@ Function Invoke-ExecAccessChecks {
5050
$Results = foreach ($Tenant in $Tenants) {
5151
$TenantCheck = $AccessChecks | Where-Object -Property RowKey -EQ $Tenant.customerId | Select-Object -Property Data
5252
$TenantResult = [PSCustomObject]@{
53-
TenantId = $Tenant.customerId
54-
TenantName = $Tenant.displayName
55-
DefaultDomainName = $Tenant.defaultDomainName
56-
GraphStatus = 'Not run yet'
57-
ExchangeStatus = 'Not run yet'
58-
GDAPRoles = ''
59-
MissingRoles = ''
60-
LastRun = ''
61-
GraphTest = ''
62-
ExchangeTest = ''
53+
TenantId = $Tenant.customerId
54+
TenantName = $Tenant.displayName
55+
DefaultDomainName = $Tenant.defaultDomainName
56+
GraphStatus = 'Not run yet'
57+
ExchangeStatus = 'Not run yet'
58+
GDAPRoles = ''
59+
MissingRoles = ''
60+
LastRun = ''
61+
GraphTest = ''
62+
ExchangeTest = ''
63+
OrgManagementRoles = @()
6364
}
6465
if ($TenantCheck) {
6566
$Data = @($TenantCheck.Data | ConvertFrom-Json -ErrorAction Stop)
@@ -70,6 +71,7 @@ Function Invoke-ExecAccessChecks {
7071
$TenantResult.LastRun = $Data.LastRun
7172
$TenantResult.GraphTest = $Data.GraphTest
7273
$TenantResult.ExchangeTest = $Data.ExchangeTest
74+
$TenantResult.OrgManagementRoles = $Data.OrgManagementRoles
7375
}
7476
$TenantResult
7577
}

Modules/CIPPCore/Public/Test-CIPPAccessTenant.ps1

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,15 @@ function Test-CIPPAccessTenant {
4848
$ExchangeStatus = $false
4949

5050
$Results = [PSCustomObject]@{
51-
TenantName = $Tenant.defaultDomainName
52-
GraphStatus = $false
53-
GraphTest = ''
54-
ExchangeStatus = $false
55-
ExchangeTest = ''
56-
GDAPRoles = ''
57-
MissingRoles = ''
58-
LastRun = (Get-Date).ToUniversalTime()
51+
TenantName = $Tenant.defaultDomainName
52+
GraphStatus = $false
53+
GraphTest = ''
54+
ExchangeStatus = $false
55+
ExchangeTest = ''
56+
GDAPRoles = ''
57+
MissingRoles = ''
58+
OrgManagementRoles = @()
59+
LastRun = (Get-Date).ToUniversalTime()
5960
}
6061

6162
$AddedText = ''
@@ -105,6 +106,37 @@ function Test-CIPPAccessTenant {
105106
$null = New-ExoRequest -tenantid $Tenant.customerId -cmdlet 'Get-OrganizationConfig' -ErrorAction Stop
106107
$ExchangeStatus = $true
107108
$ExchangeTest = 'Successfully connected to Exchange'
109+
110+
# Get the Exchange role definitions and assignments for the Organization Management role group
111+
$Requests = @(
112+
@{
113+
id = 'roleDefinitions'
114+
method = 'GET'
115+
url = 'roleManagement/exchange/roleDefinitions?$top=999'
116+
}
117+
@{
118+
id = 'roleAssignments'
119+
method = 'GET'
120+
url = "roleManagement/exchange/roleAssignments?`$filter=principalId eq '/RoleGroups/Organization Management'&`$top=999"
121+
}
122+
)
123+
124+
$ExchangeRoles = New-GraphBulkRequest -tenantid $Tenant.customerId -Requests $Requests
125+
126+
# Get results and expand assigments with role definitions
127+
$RoleDefinitions = ($ExchangeRoles | Where-Object -Property id -EQ 'roleDefinitions').body.value | Select-Object -Property id, displayName, description, isBuiltIn, isEnabled
128+
$RoleAssignments = ($ExchangeRoles | Where-Object -Property id -EQ 'roleAssignments').body.value
129+
$OrgManagementAssignments = $RoleAssignments | Where-Object -Property principalId -EQ '/RoleGroups/Organization Management' | Sort-Object -Property roleDefinitionId -Unique
130+
$OrgManagementRoles = $OrgManagementAssignments | ForEach-Object {
131+
$RoleDefinitions | Where-Object -Property id -EQ $_.roleDefinitionId
132+
} | Sort-Object -Property displayName
133+
134+
Write-Warning "Found $($OrgManagementRoles.Count) Organization Management role assignments in Exchange"
135+
$Results.OrgManagementRoles = $OrgManagementRoles
136+
137+
# TODO: Get list of known good roles and compare against the found roles
138+
139+
108140
} catch {
109141
$ErrorMessage = Get-CippException -Exception $_
110142
$ReportedError = ($_.ErrorDetails | ConvertFrom-Json -ErrorAction SilentlyContinue)

0 commit comments

Comments
 (0)