Skip to content

Commit 7e45dd7

Browse files
Merge pull request KelvinTegelaar#1444 from kris6673/feat-TransportRules-alltenants
Feat: Add support for AllTenants in transport rules page
2 parents a143bbb + 5640710 commit 7e45dd7

File tree

3 files changed

+113
-20
lines changed

3 files changed

+113
-20
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function Push-ListTransportRulesAllTenants {
2+
<#
3+
.FUNCTIONALITY
4+
Entrypoint
5+
#>
6+
param($Item)
7+
8+
$Tenant = Get-Tenants -TenantFilter $Item.customerId
9+
$DomainName = $Tenant.defaultDomainName
10+
$Table = Get-CIPPTable -TableName CacheTransportRules
11+
12+
try {
13+
$TransportRules = New-ExoRequest -tenantid $DomainName -cmdlet 'Get-TransportRule'
14+
$Results = foreach ($rule in $TransportRules) {
15+
$GUID = (New-Guid).Guid
16+
$Results = @{
17+
TransportRule = [string]($rule | ConvertTo-Json -Depth 10)
18+
RowKey = [string]$GUID
19+
PartitionKey = 'TransportRule'
20+
Tenant = [string]$DomainName
21+
}
22+
Add-CIPPAzDataTableEntity @Table -Entity $Results -Force | Out-Null
23+
}
24+
25+
} catch {
26+
$GUID = (New-Guid).Guid
27+
$ErrorText = ConvertTo-Json -InputObject @{
28+
Tenant = $DomainName
29+
Name = "Could not connect to Tenant: $($_.Exception.Message)"
30+
State = 'Error'
31+
Priority = 0
32+
Description = "Error retrieving transport rules: $($_.Exception.Message)"
33+
}
34+
$Results = @{
35+
TransportRule = [string]$ErrorText
36+
RowKey = [string]$GUID
37+
PartitionKey = 'TransportRule'
38+
Tenant = [string]$DomainName
39+
}
40+
Add-CIPPAzDataTableEntity @Table -Entity $Results -Force | Out-Null
41+
}
42+
}

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Email-Exchange/Transport/Invoke-ListTransportRules.ps1

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,76 @@ Function Invoke-ListTransportRules {
1313
$APIName = $Request.Params.CIPPEndpoint
1414
$Headers = $Request.Headers
1515
Write-LogMessage -headers $Headers -API $APIName -message 'Accessed this API' -Sev 'Debug'
16-
$TenantFilter = $request.Query.tenantFilter
16+
17+
# Interact with query parameters or the body of the request.
18+
$TenantFilter = $Request.Query.tenantFilter
1719

1820
try {
19-
$GraphRequest = New-ExoRequest -tenantid $TenantFilter -cmdlet 'Get-TransportRule'
21+
$Results = if ($TenantFilter -ne 'AllTenants') {
22+
# Single tenant functionality
23+
New-ExoRequest -tenantid $TenantFilter -cmdlet 'Get-TransportRule'
24+
} else {
25+
# AllTenants functionality
26+
$Table = Get-CIPPTable -TableName CacheTransportRules
27+
$PartitionKey = 'TransportRule'
28+
$Filter = "PartitionKey eq '$PartitionKey'"
29+
$Rows = Get-CIPPAzDataTableEntity @Table -filter $Filter | Where-Object -Property Timestamp -GT (Get-Date).AddMinutes(-60)
30+
$QueueReference = '{0}-{1}' -f $TenantFilter, $PartitionKey
31+
$RunningQueue = Invoke-ListCippQueue | Where-Object { $_.Reference -eq $QueueReference -and $_.Status -notmatch 'Completed' -and $_.Status -notmatch 'Failed' }
32+
33+
# If a queue is running, we will not start a new one
34+
if ($RunningQueue) {
35+
$Metadata = [PSCustomObject]@{
36+
QueueMessage = 'Still loading transport rules for all tenants. Please check back in a few more minutes'
37+
}
38+
} elseif (!$Rows -and !$RunningQueue) {
39+
# If no rows are found and no queue is running, we will start a new one
40+
$TenantList = Get-Tenants -IncludeErrors
41+
$Queue = New-CippQueueEntry -Name 'Transport Rules - All Tenants' -Link '/email/transport/list-rules?tenantFilter=AllTenants' -Reference $QueueReference -TotalTasks ($TenantList | Measure-Object).Count
42+
$Metadata = [PSCustomObject]@{
43+
QueueMessage = 'Loading transport rules for all tenants. Please check back in a few minutes'
44+
}
45+
$InputObject = [PSCustomObject]@{
46+
OrchestratorName = 'TransportRuleOrchestrator'
47+
QueueFunction = @{
48+
FunctionName = 'GetTenants'
49+
QueueId = $Queue.RowKey
50+
TenantParams = @{
51+
IncludeErrors = $true
52+
}
53+
DurableName = 'ListTransportRulesAllTenants'
54+
}
55+
SkipLog = $true
56+
}
57+
Start-NewOrchestration -FunctionName 'CIPPOrchestrator' -InputObject ($InputObject | ConvertTo-Json -Depth 5 -Compress) | Out-Null
58+
} else {
59+
# Return cached data
60+
$Rules = $Rows
61+
foreach ($rule in $Rules) {
62+
$RuleObj = $rule.TransportRule | ConvertFrom-Json
63+
$RuleObj | Add-Member -MemberType NoteProperty -Name Tenant -Value $rule.Tenant -Force
64+
$RuleObj
65+
}
66+
}
67+
}
2068
$StatusCode = [HttpStatusCode]::OK
2169
} catch {
2270
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
2371
$StatusCode = [HttpStatusCode]::Forbidden
24-
$GraphRequest = $ErrorMessage
72+
$Body = $ErrorMessage
73+
}
74+
75+
# If the body is not set by an error, we will set it here
76+
if (!$Body) {
77+
$Body = [PSCustomObject]@{
78+
Results = @($Results)
79+
Metadata = $Metadata
80+
}
2581
}
2682

2783
# Associate values to output bindings by calling 'Push-OutputBinding'.
2884
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
2985
StatusCode = $StatusCode
30-
Body = @($GraphRequest)
86+
Body = $Body
3187
})
32-
3388
}

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Security/Invoke-ExecIncidentsList.ps1

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ Function Invoke-ExecIncidentsList {
1919

2020
try {
2121
$GraphRequest = if ($TenantFilter -ne 'AllTenants') {
22-
$incidents = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/security/incidents' -tenantid $TenantFilter -AsApp $true
22+
# Single tenant functionality
23+
$Incidents = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/security/incidents' -tenantid $TenantFilter -AsApp $true
2324

24-
foreach ($incident in $incidents) {
25+
foreach ($incident in $Incidents) {
2526
[PSCustomObject]@{
2627
Tenant = $TenantFilter
2728
Id = $incident.id
@@ -40,6 +41,7 @@ Function Invoke-ExecIncidentsList {
4041
}
4142
}
4243
} else {
44+
# AllTenants functionality
4345
$Table = Get-CIPPTable -TableName cachealertsandincidents
4446
$PartitionKey = 'Incident'
4547
$Filter = "PartitionKey eq '$PartitionKey'"
@@ -51,9 +53,6 @@ Function Invoke-ExecIncidentsList {
5153
$Metadata = [PSCustomObject]@{
5254
QueueMessage = 'Still loading data for all tenants. Please check back in a few more minutes'
5355
}
54-
[PSCustomObject]@{
55-
Waiting = $true
56-
}
5756
} elseif (!$Rows -and !$RunningQueue) {
5857
# If no rows are found and no queue is running, we will start a new one
5958
$TenantList = Get-Tenants -IncludeErrors
@@ -73,13 +72,10 @@ Function Invoke-ExecIncidentsList {
7372
}
7473
SkipLog = $true
7574
}
76-
Start-NewOrchestration -FunctionName 'CIPPOrchestrator' -InputObject ($InputObject | ConvertTo-Json -Depth 5 -Compress)
77-
[PSCustomObject]@{
78-
Waiting = $true
79-
}
75+
Start-NewOrchestration -FunctionName 'CIPPOrchestrator' -InputObject ($InputObject | ConvertTo-Json -Depth 5 -Compress) | Out-Null
8076
} else {
81-
$incidents = $Rows
82-
foreach ($incident in $incidents) {
77+
$Incidents = $Rows
78+
foreach ($incident in $Incidents) {
8379
$IncidentObj = $incident.Incident | ConvertFrom-Json
8480
[PSCustomObject]@{
8581
Tenant = $incident.Tenant
@@ -101,19 +97,19 @@ Function Invoke-ExecIncidentsList {
10197
}
10298
}
10399
} catch {
100+
$Body = Get-NormalizedError -Message $_.Exception.Message
104101
$StatusCode = [HttpStatusCode]::Forbidden
105-
$body = $_.Exception.message
106102
}
107-
if (!$body) {
103+
if (!$Body) {
108104
$StatusCode = [HttpStatusCode]::OK
109-
$body = [PSCustomObject]@{
105+
$Body = [PSCustomObject]@{
110106
Results = @($GraphRequest | Where-Object -Property id -NE $null | Sort-Object id -Descending)
111107
Metadata = $Metadata
112108
}
113109
}
114110
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
115111
StatusCode = $StatusCode
116-
Body = $body
112+
Body = $Body
117113
})
118114

119115
}

0 commit comments

Comments
 (0)