Skip to content

Commit bcb4157

Browse files
authored
Merge pull request KelvinTegelaar#1674 from kris6673/TenantAllowBlockList
Feat: Logging improvements, add AllTenants support for listing tenant allow/blocklist
2 parents b476958 + 4038748 commit bcb4157

File tree

3 files changed

+131
-21
lines changed

3 files changed

+131
-21
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
function Push-ListTenantAllowBlockListAllTenants {
2+
<#
3+
.FUNCTIONALITY
4+
Entrypoint
5+
#>
6+
[CmdletBinding()]
7+
param($Item)
8+
9+
$Tenant = Get-Tenants -TenantFilter $Item.customerId
10+
$domainName = $Tenant.defaultDomainName
11+
$Table = Get-CIPPTable -TableName 'cacheTenantAllowBlockList'
12+
$ListTypes = 'Sender', 'Url', 'FileHash', 'IP'
13+
14+
try {
15+
foreach ($ListType in $ListTypes) {
16+
$Entries = New-ExoRequest -tenantid $domainName -cmdlet 'Get-TenantAllowBlockListItems' -cmdParams @{ ListType = $ListType }
17+
foreach ($Entry in $Entries) {
18+
$CleanEntry = $Entry | Select-Object -ExcludeProperty *'@data.type'*, *'(DateTime])'*
19+
$CleanEntry | Add-Member -MemberType NoteProperty -Name Tenant -Value $domainName -Force
20+
$CleanEntry | Add-Member -MemberType NoteProperty -Name ListType -Value $ListType -Force
21+
$Entity = @{
22+
Entry = [string]($CleanEntry | ConvertTo-Json -Depth 10 -Compress)
23+
RowKey = [string](New-Guid).Guid
24+
PartitionKey = 'TenantAllowBlockList'
25+
Tenant = [string]$domainName
26+
}
27+
Add-CIPPAzDataTableEntity @Table -Entity $Entity -Force | Out-Null
28+
}
29+
}
30+
} catch {
31+
$ErrorEntry = [pscustomobject]@{
32+
Tenant = $domainName
33+
ListType = 'Error'
34+
Identity = 'Error'
35+
DisplayName = "Could not retrieve tenant allow/block list: $($_.Exception.Message)"
36+
Timestamp = (Get-Date).ToString('s')
37+
}
38+
$Entity = @{
39+
Entry = [string]($ErrorEntry | ConvertTo-Json -Depth 10 -Compress)
40+
RowKey = [string](New-Guid).Guid
41+
PartitionKey = 'TenantAllowBlockList'
42+
Tenant = [string]$domainName
43+
}
44+
Add-CIPPAzDataTableEntity @Table -Entity $Entity -Force | Out-Null
45+
}
46+
}

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Email-Exchange/Spamfilter/Invoke-AddTenantAllowBlockList.ps1

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Function Invoke-AddTenantAllowBlockList {
1+
function Invoke-AddTenantAllowBlockList {
22
<#
33
.FUNCTIONALITY
44
Entrypoint
@@ -9,14 +9,24 @@ Function Invoke-AddTenantAllowBlockList {
99
param($Request, $TriggerMetadata)
1010

1111
$APIName = $Request.Params.CIPPEndpoint
12+
$Headers = $Request.Headers
13+
1214
$BlockListObject = $Request.Body
13-
if ($Request.Body.tenantId -eq 'AllTenants') { $Tenants = (Get-Tenants).defaultDomainName } else { $Tenants = @($Request.body.tenantId) }
15+
$TenantID = $Request.Body.tenantID.value ?? $Request.Body.tenantID
16+
17+
if ($TenantID -eq 'AllTenants') {
18+
$Tenants = (Get-Tenants).defaultDomainName
19+
} elseif ($TenantID -is [array]) {
20+
$Tenants = $TenantID
21+
} else {
22+
$Tenants = @($TenantID)
23+
}
1424
$Results = [System.Collections.Generic.List[string]]::new()
1525
$Entries = @()
1626
if ($BlockListObject.entries -is [array]) {
1727
$Entries = $BlockListObject.entries
1828
} else {
19-
$Entries = @($BlockListObject.entries -split "[,;]" | Where-Object { $_ -ne "" } | ForEach-Object { $_.Trim() })
29+
$Entries = @($BlockListObject.entries -split '[,;]' | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } | ForEach-Object { $_.Trim() })
2030
}
2131
foreach ($Tenant in $Tenants) {
2232
try {
@@ -38,19 +48,20 @@ Function Invoke-AddTenantAllowBlockList {
3848
}
3949

4050
New-ExoRequest @ExoRequest
41-
42-
$results.add("Successfully added $($BlockListObject.Entries) as type $($BlockListObject.ListType) to the $($BlockListObject.listMethod) list for $tenant")
43-
Write-LogMessage -headers $Request.Headers -API $APIName -tenant $Tenant -message $result -Sev 'Info'
51+
$Result = "Successfully added $($BlockListObject.Entries) as type $($BlockListObject.ListType) to the $($BlockListObject.listMethod) list for $tenant"
52+
$Results.Add($Result)
53+
Write-LogMessage -headers $Headers -API $APIName -tenant $Tenant -message $Result -Sev 'Info'
4454
} catch {
45-
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
46-
$results.add("Failed to create blocklist. Error: $ErrorMessage")
47-
Write-LogMessage -headers $Request.Headers -API $APIName -tenant $Tenant -message $result -Sev 'Error'
55+
$ErrorMessage = Get-CippException -Exception $_
56+
$Result = "Failed to create blocklist. Error: $($ErrorMessage.NormalizedError)"
57+
$Results.Add($Result)
58+
Write-LogMessage -headers $Headers -API $APIName -tenant $Tenant -message $Result -Sev 'Error' -LogData $ErrorMessage
4859
}
4960
}
5061
return ([HttpResponseContext]@{
5162
StatusCode = [HttpStatusCode]::OK
5263
Body = @{
53-
'Results' = $results
64+
'Results' = $Results
5465
'Request' = $ExoRequest
5566
}
5667
})
Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Function Invoke-ListTenantAllowBlockList {
1+
function Invoke-ListTenantAllowBlockList {
22
<#
33
.FUNCTIONALITY
44
Entrypoint
@@ -11,21 +11,74 @@ Function Invoke-ListTenantAllowBlockList {
1111
$TenantFilter = $Request.Query.tenantFilter
1212
$ListTypes = 'Sender', 'Url', 'FileHash', 'IP'
1313
try {
14-
$Results = $ListTypes | ForEach-Object -Parallel {
15-
Import-Module CIPPCore
16-
$TempResults = New-ExoRequest -tenantid $using:TenantFilter -cmdlet 'Get-TenantAllowBlockListItems' -cmdParams @{ListType = $_ }
17-
$TempResults | Add-Member -MemberType NoteProperty -Name ListType -Value $_
18-
$TempResults | Select-Object -ExcludeProperty *'@data.type'*, *'(DateTime])'*
19-
} -ThrottleLimit 5
20-
14+
if ($TenantFilter -ne 'AllTenants') {
15+
$Results = $ListTypes | ForEach-Object -Parallel {
16+
Import-Module CIPPCore
17+
$TempResults = New-ExoRequest -tenantid $using:TenantFilter -cmdlet 'Get-TenantAllowBlockListItems' -cmdParams @{ ListType = $_ }
18+
$TempResults | Add-Member -MemberType NoteProperty -Name ListType -Value $_ -Force
19+
$TempResults | Add-Member -MemberType NoteProperty -Name Tenant -Value $using:TenantFilter -Force
20+
$TempResults | Select-Object -ExcludeProperty *'@data.type'*, *'(DateTime])'*
21+
} -ThrottleLimit 5
22+
$Metadata = [PSCustomObject]@{}
23+
} else {
24+
$Table = Get-CIPPTable -TableName 'cacheTenantAllowBlockList'
25+
$PartitionKey = 'TenantAllowBlockList'
26+
$Filter = "PartitionKey eq '$PartitionKey'"
27+
$Rows = Get-CIPPAzDataTableEntity @Table -filter $Filter | Where-Object -Property Timestamp -GT (Get-Date).AddMinutes(-60)
28+
$QueueReference = '{0}-{1}' -f $TenantFilter, $PartitionKey
29+
$RunningQueue = Invoke-ListCippQueue -Reference $QueueReference | Where-Object { $_.Status -notmatch 'Completed' -and $_.Status -notmatch 'Failed' }
30+
if ($RunningQueue) {
31+
$Metadata = [PSCustomObject]@{
32+
QueueMessage = 'Still loading data for all tenants. Please check back in a few more minutes'
33+
QueueId = $RunningQueue.RowKey
34+
}
35+
$Results = @()
36+
} elseif (!$Rows -and !$RunningQueue) {
37+
$TenantList = Get-Tenants -IncludeErrors
38+
$Queue = New-CippQueueEntry -Name 'Tenant Allow/Block List - All Tenants' -Link '/tenant/administration/allow-block-list?customerId=AllTenants' -Reference $QueueReference -TotalTasks ($TenantList | Measure-Object).Count
39+
$Metadata = [PSCustomObject]@{
40+
QueueMessage = 'Loading data for all tenants. Please check back in a few minutes'
41+
QueueId = $Queue.RowKey
42+
}
43+
$InputObject = [PSCustomObject]@{
44+
OrchestratorName = 'TenantAllowBlockListOrchestrator'
45+
QueueFunction = @{
46+
FunctionName = 'GetTenants'
47+
QueueId = $Queue.RowKey
48+
TenantParams = @{
49+
IncludeErrors = $true
50+
}
51+
DurableName = 'ListTenantAllowBlockListAllTenants'
52+
}
53+
SkipLog = $true
54+
}
55+
Start-NewOrchestration -FunctionName 'CIPPOrchestrator' -InputObject ($InputObject | ConvertTo-Json -Depth 5 -Compress) | Out-Null
56+
$Results = @()
57+
} else {
58+
$Metadata = [PSCustomObject]@{
59+
QueueId = $RunningQueue.RowKey ?? $null
60+
}
61+
$Results = foreach ($Row in $Rows) {
62+
$Row.Entry | ConvertFrom-Json
63+
}
64+
}
65+
}
2166
$StatusCode = [HttpStatusCode]::OK
2267
} catch {
2368
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
2469
$StatusCode = [HttpStatusCode]::Forbidden
2570
$Results = $ErrorMessage
2671
}
27-
return [HttpResponseContext]@{
28-
StatusCode = $StatusCode
29-
Body = @($Results)
72+
73+
if (!$Body) {
74+
$Body = [PSCustomObject]@{
75+
Results = @($Results)
76+
Metadata = $Metadata
3077
}
78+
}
79+
80+
return [HttpResponseContext]@{
81+
StatusCode = $StatusCode
82+
Body = $Body
83+
}
3184
}

0 commit comments

Comments
 (0)