Skip to content

Commit 9512822

Browse files
committed
Add CFZTNA headers support to webhook alerts
When sending webhook alerts, the script now checks for CFZTNA extension configuration and, if enabled, adds CF-Access-Client-Id and CF-Access-Client-Secret headers to the API request. Also improves error handling by returning error messages when webhook sending fails.
1 parent 41895aa commit 9512822

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

Modules/CIPPCore/Public/Send-CIPPAlert.ps1

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ function Send-CIPPAlert {
100100

101101
if ($Type -eq 'webhook') {
102102
Write-Information 'Trying to send webhook'
103+
104+
$ExtensionTable = Get-CIPPTable -TableName Extensionsconfig
105+
$Configuration = ((Get-CIPPAzDataTableEntity @ExtensionTable).config | ConvertFrom-Json)
106+
107+
if ($Configuration.CFZTNA.WebhookEnabled -eq $true -and $Configuration.CFZTNA.Enabled -eq $true) {
108+
$CFAPIKey = Get-ExtensionAPIKey -Extension 'CFZTNA'
109+
$Headers = @{'CF-Access-Client-Id' = $Configuration.CFZTNA.ClientId; 'CF-Access-Client-Secret' = "$CFAPIKey" }
110+
Write-Information 'CF-Access-Client-Id and CF-Access-Client-Secret headers added to webhook API request'
111+
} else {
112+
$Headers = $null
113+
}
114+
103115
$JSONBody = Get-CIPPTextReplacement -TenantFilter $TenantFilter -Text $JSONContent -EscapeForJson
104116
try {
105117
if (![string]::IsNullOrWhiteSpace($Config.webhook) -or ![string]::IsNullOrWhiteSpace($AltWebhook)) {
@@ -124,7 +136,16 @@ function Send-CIPPAlert {
124136
Invoke-RestMethod -Uri $webhook -Method POST -ContentType 'Application/json' -Body $JSONBody
125137
}
126138
default {
127-
Invoke-RestMethod -Uri $webhook -Method POST -ContentType 'Application/json' -Body $JSONContent
139+
$RestMethod = @{
140+
Uri = $webhook
141+
Method = 'POST'
142+
ContentType = 'application/json'
143+
Body = $JSONContent
144+
}
145+
if ($Headers) {
146+
$RestMethod['Headers'] = $Headers
147+
}
148+
Invoke-RestMethod @RestMethod
128149
}
129150
}
130151
}
@@ -137,6 +158,7 @@ function Send-CIPPAlert {
137158
$ErrorMessage = Get-CippException -Exception $_
138159
Write-Information "Could not send alerts to webhook: $($ErrorMessage.NormalizedError)"
139160
Write-LogMessage -API 'Webhook Alerts' -message "Could not send alerts to webhook: $($ErrorMessage.NormalizedError)" -tenant $TenantFilter -sev error -LogData $ErrorMessage
161+
return "Could not send alerts to webhook: $($ErrorMessage.NormalizedError)"
140162
}
141163
}
142164

0 commit comments

Comments
 (0)