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.
Merge branch 'dev' of https://github.com/johnduprey/CIPP-API into dev
- Loading branch information
Showing
3 changed files
with
128 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
function New-CIPPGraphSubscription { | ||
[CmdletBinding()] | ||
param ( | ||
$TenantFilter, | ||
[bool]$auditLogAPI = $false, | ||
$TypeofSubscription, | ||
$Resource, | ||
$EventType, | ||
$APIName = "Create Webhook", | ||
$ExecutingUser | ||
) | ||
$CIPPID = (New-Guid).GUID | ||
$expiredate = (Get-Date).AddDays(1).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ") | ||
$params = @{ | ||
changeType = $TypeofSubscription | ||
notificationUrl = "https://webhook.site/9650bc4a-0120-41de-8ffd-6616e71244e1?EventType=$EventType&CIPPID=$CIPPID" | ||
resource = $Resource | ||
expirationDateTime = $expiredate | ||
} | ConvertTo-Json | ||
$WebhookTable = Get-CIPPTable -TableName webhookTable | ||
|
||
try { | ||
if ($auditLogAPI) { | ||
$AuditLogParams = @{ | ||
webhook = @{ | ||
"address" = "https://webhook.site/9650bc4a-0120-41de-8ffd-6616e71244e1/" | ||
} | ||
} | ConvertTo-Json | ||
$AuditLog = New-GraphPOSTRequest -uri "https://manage.office.com/api/v1.0/$($TenantFilter)/activity/feed/subscriptions/start?contentType=$EventType&PublisherIdentifier=$($TenantFilter)" -tenantid $TenantFilter -type POST -body $AuditLogparams -verbose | ||
$WebhookRow = @{ | ||
PartitionKey = [string]$TenantFilter | ||
RowKey = [string]$CIPPID | ||
EventType = [string]$EventType | ||
Resource = "M365AuditLogs" | ||
Expiration = "None" | ||
WebhookNotificationUrl = [string]$Auditlog.webhook.address | ||
} | ||
$null = Add-AzDataTableEntity @WebhookTable -Entity $WebhookRow | ||
} | ||
else { | ||
$GraphRequest = New-GraphPostRequest -uri "https://graph.microsoft.com/beta/subscriptions" -tenantid $TenantFilter -type POST -body $params -verbose | ||
#If creation is succesfull, we store the GUID in the storage table webhookTable to make sure we can check against this later on. | ||
#We store the GUID as rowkey, the event type, the resource, and the expiration date as properties, we also add the Tenant name so we can easily find this later on. | ||
#We don't store the return, because Ms decided that a renewal or re-authenticate does not change the url, but does change the id... | ||
$WebhookRow = @{ | ||
PartitionKey = [string]$TenantFilter | ||
RowKey = [string]$CIPPID | ||
EventType = [string]$EventType | ||
Resource = [string]$Resource | ||
Expiration = [string]$expiredate | ||
WebhookNotificationUrl = [string]$GraphRequest.notificationUrl | ||
} | ||
$null = Add-AzDataTableEntity @WebhookTable -Entity $WebhookRow | ||
#todo: add remove webhook function, add check webhook function, add list webhooks function | ||
#add refresh webhook function based on table. | ||
} | ||
Write-LogMessage -user $ExecutingUser -API $APIName -message "Created Webhook subscription for $($TenantFilter)" -Sev "Info" -tenant $TenantFilter | ||
return "Created Webhook subscription for $($TenantFilter)" | ||
} | ||
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)" | ||
} | ||
|
||
} | ||
|
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,28 @@ | ||
function Remove-CIPPGraphSubscription { | ||
[CmdletBinding()] | ||
param ( | ||
$TenantFilter, | ||
$CIPPID, | ||
$APIName = "Remove Graph Webhook", | ||
$ExecutingUser | ||
) | ||
try { | ||
$WebhookTable = Get-CIPPTable -TableName webhookTable | ||
$WebhookRow = Get-AzDataTableEntity @WebhookTable | Where-Object { $_.RowKey -eq $CIPPID } | ||
if ($WebhookRow.Resource -eq "M365AuditLogs") { | ||
$AuditLog = New-GraphPOSTRequest -uri "https://manage.office.com/api/v1.0/$($TenantFilter)/activity/feed/subscriptions/stop?contentType=$($WebhookRow.EventType)" -tenantid $TenantFilter -type POST -body "{}" -verbose | ||
|
||
} | ||
else { | ||
$OldID = (New-GraphGetRequest -uri "https://graph.microsoft.com/beta/subscriptions" -tenantid $TenantFilter) | Where-Object { $_.notificationUrl -eq $WebhookRow.WebhookNotificationUrl } | ||
$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 "Remove webhook subscription to $($GraphRequest.value.notificationUrl)" | ||
|
||
} | ||
catch { | ||
Write-LogMessage -user $ExecutingUser -API $APIName -message "Failed to renew Webhook Subscription: $($_.Exception.Message)" -Sev "Error" -tenant $TenantFilter | ||
return "Failed to remove Webhook Subscription $($GraphRequest.value.notificationUrl): $($_.Exception.Message)" | ||
} | ||
} |
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,34 @@ | ||
function Set-CIPPGraphSubscription { | ||
[CmdletBinding()] | ||
param ( | ||
$TenantFilter, | ||
$RenewSubscriptions, | ||
$Resource, | ||
$EventType, | ||
$APIName = "Set Graph Webhook", | ||
$ExecutingUser | ||
) | ||
|
||
if ($RenewSubscriptions) { | ||
$RenewalDate = (Get-Date).AddDays(1).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ") | ||
$body = @{ | ||
"expirationDateTime" = "$RenewalDate" | ||
} | ConvertTo-Json | ||
$ExistingSub = (New-GraphGetRequest -uri "https://graph.microsoft.com/beta/subscriptions" -tenantid $TenantFilter) | ForEach-Object { | ||
try { | ||
$GraphRequest = New-GraphPostRequest -uri "https://graph.microsoft.com/beta/subscriptions/$($_.id)" -tenantid $TenantFilter -type PATCH -body $body -Verbose | ||
$WebhookTable = Get-CIPPTable -TableName webhookTable | ||
#get the row from the table, grab it by the webhook notification url, and update the expiration date. | ||
$WebhookRow = Get-AzDataTableEntity @WebhookTable | Where-Object { $_.WebhookNotificationUrl -eq $GraphRequest.notificationUrl } | ||
$WebhookRow.Expiration = $RenewalDate | ||
$null = Add-AzDataTableEntity @WebhookTable -Entity $WebhookRow -Force | ||
return "Renewed $($GraphRequest.notificationUrl)" | ||
|
||
} | ||
catch { | ||
Write-LogMessage -user $ExecutingUser -API $APIName -message "Failed to renew Webhook Subscription: $($_.Exception.Message)" -Sev "Error" -tenant $TenantFilter | ||
return "Failed to renew Webhook Subscription $($WebhookRow.RowKey): $($_.Exception.Message)" | ||
} | ||
} | ||
} | ||
} |