forked from KelvinTegelaar/CIPP-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c629a27
commit abada21
Showing
12 changed files
with
311 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"bindings": [ | ||
{ | ||
"authLevel": "anonymous", | ||
"type": "httpTrigger", | ||
"direction": "in", | ||
"name": "Request", | ||
"methods": [ | ||
"get", | ||
"post" | ||
] | ||
}, | ||
{ | ||
"type": "http", | ||
"direction": "out", | ||
"name": "Response" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using namespace System.Net | ||
|
||
# Input bindings are passed in via param block. | ||
param($Request, $TriggerMetadata) | ||
|
||
$APIName = $TriggerMetadata.FunctionName | ||
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message "Accessed this API" -Sev "Debug" | ||
$WebhookTable = Get-CIPPTable -TableName webhookTable | ||
$WebhookRow = Get-AzDataTableEntity @WebhookTable | ||
|
||
# Associate values to output bindings by calling 'Push-OutputBinding'. | ||
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ | ||
StatusCode = [HttpStatusCode]::OK | ||
Body = @($WebhookRow) | ||
}) | ||
|
146 changes: 75 additions & 71 deletions
146
...es/CIPPCore/Public/Invoke-CIPPWebhook.ps1 → ...e/Public/Invoke-CIPPWebhookProcessing.ps1
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
|
||
function Send-CIPPAlert { | ||
[CmdletBinding()] | ||
param ( | ||
$Type, | ||
$Title, | ||
$HTMLContent, | ||
$JSONContent, | ||
$TenantFilter, | ||
$APIName = "Send Alert", | ||
$ExecutingUser | ||
) | ||
|
||
$Table = Get-CIPPTable -TableName SchedulerConfig | ||
$Filter = "RowKey eq 'CippNotifications' and PartitionKey eq 'CippNotifications'" | ||
$Config = [pscustomobject](Get-AzDataTableEntity @Table -Filter $Filter) | ||
if ($Type -eq 'email') { | ||
try { | ||
if ($Config.email -like '*@*') { | ||
$Recipients = $Config.email.split(",").trim() | ForEach-Object { if ($_ -like '*@*') { [pscustomobject]@{EmailAddress = @{Address = $_ } } } } | ||
$PowerShellBody = [PSCustomObject]@{ | ||
message = @{ | ||
subject = $Title | ||
body = @{ | ||
contentType = "HTML" | ||
content = $HTMLcontent | ||
} | ||
toRecipients = @($Recipients) | ||
} | ||
saveToSentItems = "true" | ||
} | ||
|
||
$JSONBody = ConvertTo-Json -Compress -Depth 10 -InputObject $PowerShellBody | ||
New-GraphPostRequest -uri 'https://graph.microsoft.com/v1.0/me/sendMail' -tenantid $env:TenantID -NoAuthCheck $true -type POST -body ($JSONBody) | ||
} | ||
|
||
} | ||
catch { | ||
Write-Host "Could not send alerts to email: $($_.Exception.message)" | ||
Write-LogMessage -API 'Alerts' -message "Could not send alerts to : $($_.Exception.message)" -sev info | ||
} | ||
|
||
} | ||
if ($Type -eq 'webhook') { | ||
try { | ||
if ($Config.webhook -ne '') { | ||
switch -wildcard ($config.webhook) { | ||
|
||
'*webhook.office.com*' { | ||
$JSonBody = "{`"text`": `"You've setup your alert policies to be alerted whenever specific events happen. We've found some of these events in the log. <br><br>$JSONContent`"}" | ||
Invoke-RestMethod -Uri $config.webhook -Method POST -ContentType 'Application/json' -Body $JSONBody | ||
} | ||
|
||
'*discord.com*' { | ||
$JSonBody = "{`"content`": `"You've setup your alert policies to be alerted whenever specific events happen. We've found some of these events in the log. $JSONContent`"}" | ||
Invoke-RestMethod -Uri $config.webhook -Method POST -ContentType 'Application/json' -Body $JSONBody | ||
} | ||
default { | ||
Invoke-RestMethod -Uri $config.webhook -Method POST -ContentType 'Application/json' -Body $JSONContent | ||
} | ||
} | ||
|
||
} | ||
|
||
} | ||
catch { | ||
Write-Host "Could not send alerts to webhook: $($_.Exception.message)" | ||
Write-LogMessage -API 'Alerts' -message "Could not send alerts to : $($_.Exception.message)" -sev info | ||
} | ||
} | ||
if ($Type -eq 'psa') { | ||
if ($config.sendtoIntegration) { | ||
try { | ||
|
||
$Alert = @{ | ||
TenantId = $Tenant | ||
AlertText = "$HTMLContent" | ||
AlertTitle = "$($Title)" | ||
} | ||
New-CippExtAlert -Alert $Alert | ||
} | ||
catch { | ||
Write-Host "Could not send alerts to ticketing system: $($_.Exception.message)" | ||
Write-LogMessage -API 'Alerts' -message "Could not send alerts to : $($_.Exception.message)" -sev info | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"bindings": [ | ||
{ | ||
"authLevel": "anonymous", | ||
"type": "httpTrigger", | ||
"direction": "in", | ||
"name": "Request", | ||
"methods": ["get", "post"] | ||
}, | ||
{ | ||
"type": "http", | ||
"direction": "out", | ||
"name": "Response" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using namespace System.Net | ||
|
||
# Input bindings are passed in via param block. | ||
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) { | ||
if ($Request.query.ValidationToken -or $Request.body.validationCode) { | ||
$body = $request.query.ValidationToken | ||
} | ||
|
||
if ($Request.body.ContentUri) { | ||
if ($Request.body.ContentUri -notlike "https://manage.office.com/api/v1.0/*") { exit } | ||
$TenantFilter = (Get-Tenants | Where-Object -Property customerId -EQ $Request.body.TenantID).defaultDomainName | ||
$Data = New-GraphPostRequest -type GET -uri "$($request.body.contenturi)" -tenantid $TenantFilter -scope "https://manage.office.com/.default" | ||
} | ||
else { | ||
$TenantFilter = $Data.Tenant | ||
$Data = $Request.body | ||
} | ||
|
||
foreach ($Item in $Data) { | ||
if ($item.Operation -in $WebhookAlerts.Operation) { | ||
Invoke-CippWebhookProcessing -TenantFilter $TenantFilter -Data $Data -CIPPPURL $url | ||
} | ||
} | ||
|
||
$body = "OK" | ||
} | ||
else { | ||
$body = "This webhook is not authorized." | ||
} | ||
|
||
|
||
# Associate values to output bindings by calling 'Push-OutputBinding'. | ||
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ | ||
StatusCode = [HttpStatusCode]::OK | ||
Body = $body | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"bindings": [ | ||
{ | ||
"authLevel": "anonymous", | ||
"type": "httpTrigger", | ||
"direction": "in", | ||
"name": "Request", | ||
"methods": [ | ||
"get", | ||
"post" | ||
] | ||
}, | ||
{ | ||
"type": "http", | ||
"direction": "out", | ||
"name": "Response" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using namespace System.Net | ||
|
||
# Input bindings are passed in via param block. | ||
param($Request, $TriggerMetadata) | ||
|
||
$APIName = $TriggerMetadata.FunctionName | ||
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message "Accessed this API" -Sev "Debug" | ||
Import-Module CippCore | ||
try { | ||
$Results = Remove-CIPPGraphSubscription -TenantFilter $Request.query.TenantFilter -CIPPID $Request.query.CIPPID | ||
$body = [pscustomobject]@{"Results" = $Results } | ||
} | ||
catch { | ||
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message "Failed to remove webhook alert. $($_.Exception.Message)" -Sev "Error" | ||
$body = [pscustomobject]@{"Results" = "Failed to remove webhook alert: $($_.Exception.Message)" } | ||
} | ||
|
||
|
||
# Associate values to output bindings by calling 'Push-OutputBinding'. | ||
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ | ||
StatusCode = [HttpStatusCode]::OK | ||
Body = $body | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters