From 65c54f177976b0bb81ec3db11d93ab85fd41e818 Mon Sep 17 00:00:00 2001 From: Yoshify Date: Mon, 9 Oct 2023 07:47:31 +1000 Subject: [PATCH 1/8] Add tenantid param to Write-LogMessage in GraphHelper.psm1 --- GraphHelper.psm1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/GraphHelper.psm1 b/GraphHelper.psm1 index 7cf967bd4c6d..cb8792440473 100644 --- a/GraphHelper.psm1 +++ b/GraphHelper.psm1 @@ -116,7 +116,7 @@ function Get-GraphToken($tenantid, $scope, $AsApp, $AppID, $refreshToken, $Retur } } -function Write-LogMessage ($message, $tenant = 'None', $API = 'None', $user, $sev) { +function Write-LogMessage ($message, $tenant = 'None', $API = 'None', $tenantId = 'None', $user, $sev) { try { $username = ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($user)) | ConvertFrom-Json).userDetails } @@ -135,6 +135,7 @@ function Write-LogMessage ($message, $tenant = 'None', $API = 'None', $user, $se $PartitionKey = (Get-Date -UFormat '%Y%m%d').ToString() $TableRow = @{ 'Tenant' = [string]$tenant + 'TenantID' = [string]$tenantId 'API' = [string]$API 'Message' = [string]$message 'Username' = [string]$username From 9418377ba93f393e8ddc9406e11f110c5e082785 Mon Sep 17 00:00:00 2001 From: Yoshify Date: Mon, 9 Oct 2023 07:48:51 +1000 Subject: [PATCH 2/8] Pass $Tenant.tenantid to $ShippedAlerts in Scheduler_Alert/run.ps1 --- Scheduler_Alert/run.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Scheduler_Alert/run.ps1 b/Scheduler_Alert/run.ps1 index 524cc8c8d993..7f0b4bb37bac 100644 --- a/Scheduler_Alert/run.ps1 +++ b/Scheduler_Alert/run.ps1 @@ -378,7 +378,7 @@ try { $ShippedAlerts | ForEach-Object { if ($_ -notin $currentlog.Message) { - Write-LogMessage -message $_ -API 'Alerts' -tenant $tenant.tenant -sev Alert + Write-LogMessage -message $_ -API 'Alerts' -tenant $tenant.tenant -sev Alert -tenantid $Tenant.tenantid } } [PSCustomObject]@{ From ba62d6bb5817c8804b754766273705b32a5d679d Mon Sep 17 00:00:00 2001 From: Yoshify Date: Mon, 9 Oct 2023 09:57:10 +1000 Subject: [PATCH 3/8] Add includeTenantId to NotificationConfig --- ExecNotificationConfig/run.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/ExecNotificationConfig/run.ps1 b/ExecNotificationConfig/run.ps1 index 959818c8b535..e4cbc4066c97 100644 --- a/ExecNotificationConfig/run.ps1 +++ b/ExecNotificationConfig/run.ps1 @@ -22,6 +22,7 @@ $results = try { 'webhook' = "$($Request.Body.Webhook)" 'onePerTenant' = [boolean]$Request.Body.onePerTenant 'sendtoIntegration' = [boolean]$Request.Body.sendtoIntegration + 'includeTenantId' = [boolean]$Request.Body.includeTenantId 'PartitionKey' = 'CippNotifications' 'RowKey' = 'CippNotifications' } From c333c831b8567d7be9d92e17f414f45cc250157b Mon Sep 17 00:00:00 2001 From: Yoshify Date: Mon, 9 Oct 2023 09:57:51 +1000 Subject: [PATCH 4/8] Use Config in alerts to optionally send tenant ID if includeTenantId is true --- Scheduler_Alert/run.ps1 | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Scheduler_Alert/run.ps1 b/Scheduler_Alert/run.ps1 index 7f0b4bb37bac..b28645c4a386 100644 --- a/Scheduler_Alert/run.ps1 +++ b/Scheduler_Alert/run.ps1 @@ -11,6 +11,9 @@ try { } $Alerts = Get-AzDataTableEntity @Table -Filter $Filter + $ConfigFilter = "RowKey eq 'CippNotifications' and PartitionKey eq 'CippNotifications'" + $Config = [pscustomobject](Get-AzDataTableEntity @Table -Filter $ConfigFilter) + $DeltaTable = Get-CIPPTable -Table DeltaCompare $LastRunTable = Get-CIPPTable -Table AlertLastRun @@ -378,7 +381,12 @@ try { $ShippedAlerts | ForEach-Object { if ($_ -notin $currentlog.Message) { - Write-LogMessage -message $_ -API 'Alerts' -tenant $tenant.tenant -sev Alert -tenantid $Tenant.tenantid + if ($Config.includeTenantId) { + Write-LogMessage -message $_ -API 'Alerts' -tenant $tenant.tenant -sev Alert -tenantid $Tenant.tenantid + } + else { + Write-LogMessage -message $_ -API 'Alerts' -tenant $tenant.tenant -sev Alert + } } } [PSCustomObject]@{ From 25c9fefa2767473d457a38859c90b5ac45af7448 Mon Sep 17 00:00:00 2001 From: Yoshify Date: Mon, 9 Oct 2023 10:39:27 +1000 Subject: [PATCH 5/8] Make $tenantId param null by default, only include in alert if it's not null --- GraphHelper.psm1 | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/GraphHelper.psm1 b/GraphHelper.psm1 index cb8792440473..8c45cf115d7e 100644 --- a/GraphHelper.psm1 +++ b/GraphHelper.psm1 @@ -116,7 +116,7 @@ function Get-GraphToken($tenantid, $scope, $AsApp, $AppID, $refreshToken, $Retur } } -function Write-LogMessage ($message, $tenant = 'None', $API = 'None', $tenantId = 'None', $user, $sev) { +function Write-LogMessage ($message, $tenant = 'None', $API = 'None', $tenantId = $null, $user, $sev) { try { $username = ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($user)) | ConvertFrom-Json).userDetails } @@ -144,6 +144,12 @@ function Write-LogMessage ($message, $tenant = 'None', $API = 'None', $tenantId 'PartitionKey' = $PartitionKey 'RowKey' = ([guid]::NewGuid()).ToString() } + + if($tenantId) + { + $TableRow.Add('TenantID', [string]$tenantId) + } + $Table.Entity = $TableRow Add-AzDataTableEntity @Table | Out-Null } From 0386d0567e69043cb50f4002a95ea83082453cb9 Mon Sep 17 00:00:00 2001 From: Yoshify Date: Mon, 9 Oct 2023 12:31:04 +1000 Subject: [PATCH 6/8] Update ListNotificationConfig to exclude includeTenantId --- ListNotificationConfig/run.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ListNotificationConfig/run.ps1 b/ListNotificationConfig/run.ps1 index 82e382843126..bf04ba1c990b 100644 --- a/ListNotificationConfig/run.ps1 +++ b/ListNotificationConfig/run.ps1 @@ -15,7 +15,7 @@ if ($Config) { $Config = @{} } #$config | Add-Member -NotePropertyValue @() -NotePropertyName 'logsToInclude' -Force -$config.logsToInclude = @(([pscustomobject]$config | Select-Object * -ExcludeProperty schedule, type, tenantid, onepertenant, sendtoIntegration, partitionkey, rowkey, tenant, ETag, email, logsToInclude, Severity, Alert, Info, Error, timestamp, webhook).psobject.properties.name) +$config.logsToInclude = @(([pscustomobject]$config | Select-Object * -ExcludeProperty schedule, type, tenantid, onepertenant, sendtoIntegration, partitionkey, rowkey, tenant, ETag, email, logsToInclude, Severity, Alert, Info, Error, timestamp, webhook includeTenantId).psobject.properties.name) if (!$config.logsToInclude) { $config.logsToInclude = @('None') } From 5371f91f7def9e99e5bb1d8fa359010a13a48ea6 Mon Sep 17 00:00:00 2001 From: Yoshify Date: Mon, 9 Oct 2023 13:28:10 +1000 Subject: [PATCH 7/8] Ensure TenantID isn't included in alert by default (regression from prior merge) --- GraphHelper.psm1 | 1 - 1 file changed, 1 deletion(-) diff --git a/GraphHelper.psm1 b/GraphHelper.psm1 index 8c45cf115d7e..0fdc7a966f2c 100644 --- a/GraphHelper.psm1 +++ b/GraphHelper.psm1 @@ -135,7 +135,6 @@ function Write-LogMessage ($message, $tenant = 'None', $API = 'None', $tenantId $PartitionKey = (Get-Date -UFormat '%Y%m%d').ToString() $TableRow = @{ 'Tenant' = [string]$tenant - 'TenantID' = [string]$tenantId 'API' = [string]$API 'Message' = [string]$message 'Username' = [string]$username From b025e414fd01b381a6bac7b35346d1faf5d6cbb4 Mon Sep 17 00:00:00 2001 From: Yoshify Date: Mon, 9 Oct 2023 13:29:59 +1000 Subject: [PATCH 8/8] Formatting, added missing comma. --- ListNotificationConfig/run.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ListNotificationConfig/run.ps1 b/ListNotificationConfig/run.ps1 index bf04ba1c990b..a33c2f38c3ba 100644 --- a/ListNotificationConfig/run.ps1 +++ b/ListNotificationConfig/run.ps1 @@ -15,7 +15,7 @@ if ($Config) { $Config = @{} } #$config | Add-Member -NotePropertyValue @() -NotePropertyName 'logsToInclude' -Force -$config.logsToInclude = @(([pscustomobject]$config | Select-Object * -ExcludeProperty schedule, type, tenantid, onepertenant, sendtoIntegration, partitionkey, rowkey, tenant, ETag, email, logsToInclude, Severity, Alert, Info, Error, timestamp, webhook includeTenantId).psobject.properties.name) +$config.logsToInclude = @(([pscustomobject]$config | Select-Object * -ExcludeProperty schedule, type, tenantid, onepertenant, sendtoIntegration, partitionkey, rowkey, tenant, ETag, email, logsToInclude, Severity, Alert, Info, Error, timestamp, webhook, includeTenantId).psobject.properties.name) if (!$config.logsToInclude) { $config.logsToInclude = @('None') }