Skip to content

Commit 3fc9675

Browse files
committed
tweaks for webhook table lookups
1 parent 70642a2 commit 3fc9675

1 file changed

Lines changed: 45 additions & 44 deletions

File tree

Modules/CIPPHTTP/Public/Entrypoints/HTTP Functions/Tenant/Administration/Alerts/Invoke-PublicWebhooks.ps1

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,11 @@ function Invoke-PublicWebhooks {
77
#>
88
param($Request, $TriggerMetadata)
99
$Headers = $Request.Headers
10-
11-
$WebhookTable = Get-CIPPTable -TableName webhookTable
12-
$WebhookIncoming = Get-CIPPTable -TableName WebhookIncoming
13-
$Webhooks = Get-CIPPAzDataTableEntity @WebhookTable
1410
Write-Host 'Received request'
1511
$url = ($Headers.'x-ms-original-url').split('/API') | Select-Object -First 1
1612
$CIPPURL = [string]$url
1713
Write-Host $url
18-
if ($Webhooks.Resource -eq 'M365AuditLogs') {
19-
Write-Host "Found M365AuditLogs - This is an old entry, we'll deny so Microsoft stops sending it."
20-
$body = 'This webhook is not authorized, its an old entry.'
21-
$StatusCode = [HttpStatusCode]::Forbidden
22-
}
14+
2315
if ($Request.Query.ValidationToken) {
2416
Write-Host 'Validation token received - query ValidationToken'
2517
$body = $Request.Query.ValidationToken
@@ -32,45 +24,54 @@ function Invoke-PublicWebhooks {
3224
Write-Host 'Validation token received - query validationCode'
3325
$body = $Request.Query.validationCode
3426
$StatusCode = [HttpStatusCode]::OK
35-
} elseif ($Request.Query.CIPPID -in $Webhooks.RowKey) {
36-
Write-Host 'Found matching CIPPID'
37-
$url = ($Headers.'x-ms-original-url').split('/API') | Select-Object -First 1
38-
$Webhookinfo = $Webhooks | Where-Object -Property RowKey -EQ $Request.Query.CIPPID
27+
} elseif ($Request.Query.CIPPID) {
28+
$WebhookTable = Get-CIPPTable -TableName webhookTable
29+
$Webhookinfo = Get-CIPPAzDataTableEntity @WebhookTable -Filter "RowKey eq '$($Request.Query.CIPPID)'" -First 1
30+
if (-not $Webhookinfo) {
31+
Write-Host "No matching CIPPID found: $($Request.Query.CIPPID)"
32+
$Body = 'This webhook is not authorized.'
33+
$StatusCode = [HttpStatusCode]::Forbidden
34+
} elseif ($Webhookinfo.Resource -eq 'M365AuditLogs') {
35+
Write-Host "Found M365AuditLogs - This is an old entry, we'll deny so Microsoft stops sending it."
36+
$Body = 'This webhook is not authorized, its an old entry.'
37+
$StatusCode = [HttpStatusCode]::Forbidden
38+
} else {
39+
Write-Host 'Found matching CIPPID'
40+
$WebhookIncoming = Get-CIPPTable -TableName WebhookIncoming
3941

40-
if ($Request.Query.Type -eq 'GraphSubscription') {
41-
# Graph Subscriptions
42-
[pscustomobject]$ReceivedItem = $Request.Body.value
43-
$Entity = [PSCustomObject]@{
44-
PartitionKey = 'Webhook'
45-
RowKey = [string](New-Guid).Guid
46-
Type = $Request.Query.Type
47-
Data = [string]($ReceivedItem | ConvertTo-Json -Depth 10)
48-
CIPPID = $Request.Query.CIPPID
49-
WebhookInfo = [string]($WebhookInfo | ConvertTo-Json -Depth 10)
50-
FunctionName = 'PublicWebhookProcess'
51-
}
52-
Add-CIPPAzDataTableEntity @WebhookIncoming -Entity $Entity
53-
## Push webhook data to queue
54-
#Invoke-CippGraphWebhookProcessing -Data $ReceivedItem -CIPPID $request.Query.CIPPID -WebhookInfo $Webhookinfo
42+
if ($Request.Query.Type -eq 'GraphSubscription') {
43+
# Graph Subscriptions
44+
[pscustomobject]$ReceivedItem = $Request.Body.value
45+
$Entity = [PSCustomObject]@{
46+
PartitionKey = 'Webhook'
47+
RowKey = [string](New-Guid).Guid
48+
Type = $Request.Query.Type
49+
Data = [string]($ReceivedItem | ConvertTo-Json -Depth 10)
50+
CIPPID = $Request.Query.CIPPID
51+
WebhookInfo = [string]($WebhookInfo | ConvertTo-Json -Depth 10)
52+
FunctionName = 'PublicWebhookProcess'
53+
}
54+
Add-CIPPAzDataTableEntity @WebhookIncoming -Entity $Entity
5555

56-
} elseif ($Request.Query.Type -eq 'PartnerCenter') {
57-
[pscustomobject]$ReceivedItem = $Request.Body
58-
$Entity = [PSCustomObject]@{
59-
PartitionKey = 'Webhook'
60-
RowKey = [string](New-Guid).Guid
61-
Type = $Request.Query.Type
62-
Data = [string]($ReceivedItem | ConvertTo-Json -Depth 10)
63-
CIPPID = $Request.Query.CIPPID
64-
WebhookInfo = [string]($WebhookInfo | ConvertTo-Json -Depth 10)
65-
FunctionName = 'PublicWebhookProcess'
56+
} elseif ($Request.Query.Type -eq 'PartnerCenter') {
57+
[pscustomobject]$ReceivedItem = $Request.Body
58+
$Entity = [PSCustomObject]@{
59+
PartitionKey = 'Webhook'
60+
RowKey = [string](New-Guid).Guid
61+
Type = $Request.Query.Type
62+
Data = [string]($ReceivedItem | ConvertTo-Json -Depth 10)
63+
CIPPID = $Request.Query.CIPPID
64+
WebhookInfo = [string]($WebhookInfo | ConvertTo-Json -Depth 10)
65+
FunctionName = 'PublicWebhookProcess'
66+
}
67+
Add-CIPPAzDataTableEntity @WebhookIncoming -Entity $Entity
68+
} else {
69+
$Body = 'This webhook is not authorized.'
70+
$StatusCode = [HttpStatusCode]::Forbidden
6671
}
67-
Add-CIPPAzDataTableEntity @WebhookIncoming -Entity $Entity
68-
} else {
69-
$Body = 'This webhook is not authorized.'
70-
$StatusCode = [HttpStatusCode]::Forbidden
72+
$Body = 'Webhook Received'
73+
$StatusCode = [HttpStatusCode]::OK
7174
}
72-
$Body = 'Webhook Received'
73-
$StatusCode = [HttpStatusCode]::OK
7475

7576
} else {
7677
$Body = 'This webhook is not authorized.'

0 commit comments

Comments
 (0)