|
| 1 | +function Select-CippAllowedTenantData { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Narrow a set of cached rows to the tenants the current caller is allowed to see. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + A family of cached List*/Exec*List endpoints reads a global cache table by PartitionKey |
| 8 | + and returns the rows directly. When a tenant-restricted custom role calls one of these |
| 9 | + with tenantFilter=AllTenants, the read must still be narrowed to the caller's allowed |
| 10 | + tenants - the same scope Get-Tenants already applies via $script:CippAllowedTenantsStorage. |
| 11 | + A direct cache-table read never touches Get-Tenants on a cache hit, so it would otherwise |
| 12 | + leak every managed tenant's data. Pipe those rows through this function at the point they |
| 13 | + become the response to close that gap. |
| 14 | +
|
| 15 | + This function MUST live in CIPPCore. The $script:CippAllowedTenantsStorage AsyncLocal slot |
| 16 | + is CIPPCore module-scoped; a copy defined in CIPPHTTP would read that module's own empty |
| 17 | + variable and silently filter nothing (see Get-CippRequestContext). |
| 18 | +
|
| 19 | + The stored scope is a list of customerIds (or $null = unrestricted). Cache rows identify |
| 20 | + their tenant by domain name (defaultDomainName, stored on a 'Tenant' property) and/or by |
| 21 | + customerId, so allowed customerIds are expanded to every identifier form an allowed tenant |
| 22 | + might present, mirroring the match logic in Invoke-ListLogs. |
| 23 | +
|
| 24 | + .PARAMETER InputObject |
| 25 | + The rows to filter. Accepts pipeline input. |
| 26 | +
|
| 27 | + .PARAMETER TenantProperty |
| 28 | + The property name(s) on each row that identify its tenant. Defaults to 'Tenant' and |
| 29 | + 'TenantId'. A row is kept when any of these properties matches an allowed tenant. For the |
| 30 | + Lighthouse aggregate use 'organizationId'. |
| 31 | +
|
| 32 | + .PARAMETER AllowPartner |
| 33 | + Also keep rows whose Tenant equals 'CIPP' (system/partner rows), mirroring Invoke-ListLogs. |
| 34 | +
|
| 35 | + .EXAMPLE |
| 36 | + $Rows = $Rows | Select-CippAllowedTenantData -TenantProperty 'Tenant' |
| 37 | +
|
| 38 | + .FUNCTIONALITY |
| 39 | + Internal |
| 40 | + #> |
| 41 | + [CmdletBinding()] |
| 42 | + param( |
| 43 | + [Parameter(ValueFromPipeline = $true)] |
| 44 | + [AllowNull()] |
| 45 | + $InputObject, |
| 46 | + |
| 47 | + [string[]]$TenantProperty = @('Tenant', 'TenantId'), |
| 48 | + |
| 49 | + [switch]$AllowPartner |
| 50 | + ) |
| 51 | + |
| 52 | + begin { |
| 53 | + # $null / empty stored scope means the caller is unrestricted - pass everything through |
| 54 | + # with zero overhead (no Get-Tenants call). |
| 55 | + $AllowedCustomerIds = if ($script:CippAllowedTenantsStorage) { $script:CippAllowedTenantsStorage.Value } else { $null } |
| 56 | + $Unrestricted = -not ($AllowedCustomerIds | Where-Object { $_ }) |
| 57 | + |
| 58 | + if (-not $Unrestricted) { |
| 59 | + # Build a case-insensitive set of every identifier a row might carry for an allowed |
| 60 | + # tenant. Get-Tenants is already narrowed to the caller's scope by the storage filter. |
| 61 | + $AllowedSet = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase) |
| 62 | + foreach ($Id in $AllowedCustomerIds) { |
| 63 | + if ($Id) { [void]$AllowedSet.Add([string]$Id) } |
| 64 | + } |
| 65 | + foreach ($Tenant in (Get-Tenants -IncludeErrors)) { |
| 66 | + foreach ($Value in @($Tenant.customerId, $Tenant.defaultDomainName, $Tenant.initialDomainName)) { |
| 67 | + if ($Value) { [void]$AllowedSet.Add([string]$Value) } |
| 68 | + } |
| 69 | + } |
| 70 | + if ($AllowPartner) { [void]$AllowedSet.Add('CIPP') } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + process { |
| 75 | + foreach ($Item in $InputObject) { |
| 76 | + if ($null -eq $Item) { continue } |
| 77 | + if ($Unrestricted) { |
| 78 | + $Item |
| 79 | + continue |
| 80 | + } |
| 81 | + foreach ($Prop in $TenantProperty) { |
| 82 | + $Value = $Item.$Prop |
| 83 | + if ($Value -and $AllowedSet.Contains([string]$Value)) { |
| 84 | + $Item |
| 85 | + break |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments