Skip to content

Commit

Permalink
webhook updates
Browse files Browse the repository at this point in the history
  • Loading branch information
KelvinTegelaar committed Aug 7, 2023
1 parent 3d16bce commit 16982a0
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 43 deletions.
21 changes: 14 additions & 7 deletions AddAlert/run.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,21 @@ $Results = foreach ($Tenant in $tenants) {
}

$Table = get-cipptable -TableName 'SchedulerConfig'
<#$TableRow = @{
table = ()
property = $CompleteObject
UpdateExisting = $true
}#>

#Add-AzTableRow @TableRow | Out-Null
Add-AzDataTableEntity @Table -Entity $CompleteObject -Force

$URL = ($request.headers.'x-ms-original-url').split('/api') | Select-Object -First 1
foreach ($eventype in $Request.body.EventTypes.value) {
$params = @{
TenantFilter = $tenant
auditLogAPI = $true
operations = ($Request.body.Operations.value -join ',')
allowedLocations = ($Request.body.AllowedLocations.value -join ',')
BaseURL = $url
EventType = $Eventtype
ExecutingUser = $Request.headers.'x-ms-client-principal'
}
New-CIPPGraphSubscription @params
}
"Successfully added Alert for $($Tenant) to queue."
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -tenant $tenant -message "Successfully added Alert for $($Tenant) to queue." -Sev 'Info'
}
Expand Down
3 changes: 2 additions & 1 deletion Modules/CIPPCore/Public/Invoke-CIPPWebhookProcessing.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ function Invoke-CippWebhookProcessing {
$TenantFilter,
$Data,
$Resource,
$AllowedLocations,
$CIPPPURL,
$APIName = "Process webhook",
$ExecutingUser
)
$HTML = Get-Content "TemplateEmail.HTML" -Raw | Out-String
$LocationTable = Get-CIPPTable -TableName knownlocationdb

$AllowedLocations = $AllowedLocations -split ','
$GeoTable = Get-CIPPTable -TableName geoipdb
if ($data.clientip) {
($Data.clientIp).split(':')[0].Split(".") | ForEach-Object { $IPAsint = 0; $IPAddressByte = 0 } {
Expand Down
8 changes: 7 additions & 1 deletion Modules/CIPPCore/Public/New-CIPPGraphSubscription.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ function New-CIPPGraphSubscription {
$TenantFilter,
[bool]$auditLogAPI = $false,
$TypeofSubscription,
$AllowedLocations,
$BaseURL,
$operations,
$Resource,
$EventType,
$APIName = "Create Webhook",
Expand Down Expand Up @@ -33,6 +35,8 @@ function New-CIPPGraphSubscription {
RowKey = [string]$CIPPID
EventType = [string]$EventType
Resource = "M365AuditLogs"
Operations = [string]$operations
AllowedLocations = [string]$AllowedLocations
Expiration = "None"
WebhookNotificationUrl = [string]$Auditlog.webhook.address
}
Expand All @@ -49,6 +53,8 @@ function New-CIPPGraphSubscription {
EventType = [string]$EventType
Resource = [string]$Resource
Expiration = [string]$expiredate
Operations = [string]$operations
AllowedLocations = [string]$AllowedLocations
WebhookNotificationUrl = [string]$GraphRequest.notificationUrl
}
$null = Add-AzDataTableEntity @WebhookTable -Entity $WebhookRow
Expand All @@ -60,7 +66,7 @@ function New-CIPPGraphSubscription {
}
catch {
Write-LogMessage -user $ExecutingUser -API $APIName -message "Failed to create Webhook Subscription: $($_.Exception.Message)" -Sev "Error" -tenant $TenantFilter
Return "Failed to create Webhook Subscription: $($_.Exception.Message)"
Return "Failed to create Webhook Subscription for $($TenantFilter): $($_.Exception.Message)"
}

}
Expand Down
28 changes: 0 additions & 28 deletions Modules/CIPPCore/Public/Remove-CIPPGraphSubcription.ps1

This file was deleted.

4 changes: 2 additions & 2 deletions Modules/CIPPCore/Public/Remove-CIPPGraphSubscription.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Remove-CIPPGraphSubscription{
function Remove-CIPPGraphSubscription {
[CmdletBinding()]
param (
$TenantFilter,
Expand All @@ -18,7 +18,7 @@ function Remove-CIPPGraphSubscription{
$GraphRequest = New-GraphPostRequest -uri "https://graph.microsoft.com/beta/subscriptions/$($oldId.ID)" -tenantid $TenantFilter -type DELETE -body {} -Verbose
$null = Remove-AzDataTableEntity @WebhookTable -Entity $WebhookRow
}
return "Removed webhook subscription to $($GraphRequest.value.notificationUrl)"
return "Removed webhook subscription to $($WebhookRow.resource) for $($TenantFilter)"

}
catch {
Expand Down
7 changes: 3 additions & 4 deletions PublicWebhooks/run.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ param($Request, $TriggerMetadata)
$WebhookTable = Get-CIPPTable -TableName webhookTable
$Webhooks = Get-AzDataTableEntity @WebhookTable

$WebhookAlertTable = Get-CIPPTable -TableName webhookAlertTable
$WebhookAlerts = Get-AzDataTableEntity $WebhookAlertTable
$url = ($request.headers.'x-ms-original-url').split('/api') | Select-Object -First 1

if ($Request.CIPPID -in $Webhooks.CIPPID) {
$Webhookinfo = $Webhooks | Where-Object -Property CIPPID -EQ $Request.CIPPID
if ($Request.query.ValidationToken -or $Request.body.validationCode) {
$body = $request.query.ValidationToken
}
Expand All @@ -25,8 +24,8 @@ if ($Request.CIPPID -in $Webhooks.CIPPID) {
}

foreach ($Item in $Data) {
if ($item.Operation -in $WebhookAlerts.Operation) {
Invoke-CippWebhookProcessing -TenantFilter $TenantFilter -Data $Data -CIPPPURL $url
if ($item.Operation -in ($Webhooks.Operation -split ',')) {
Invoke-CippWebhookProcessing -TenantFilter $TenantFilter -Data $Data -CIPPPURL $url -allowedlocations $Webhookinfo.AllowedLocations
}
}

Expand Down

0 comments on commit 16982a0

Please sign in to comment.