Skip to content

Commit 671a548

Browse files
Merge pull request #1201 from BNWEIN/interface-rewrite
Added Connection Filter Functions
2 parents 499d0c7 + 64a463b commit 671a548

File tree

5 files changed

+200
-0
lines changed

5 files changed

+200
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using namespace System.Net
2+
3+
Function Invoke-AddConnectionFilter {
4+
<#
5+
.FUNCTIONALITY
6+
Entrypoint
7+
.ROLE
8+
Exchange.ConnectionFilter.ReadWrite
9+
#>
10+
[CmdletBinding()]
11+
param($Request, $TriggerMetadata)
12+
13+
14+
$APIName = $TriggerMetadata.FunctionName
15+
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message 'Accessed this API' -Sev 'Debug'
16+
17+
$RequestParams = $Request.Body.PowerShellCommand |
18+
ConvertFrom-Json |
19+
Select-Object -Property *, @{Name='identity'; Expression={$_.name}} -ExcludeProperty GUID, comments, name
20+
21+
$Tenants = ($Request.body.selectedTenants).value
22+
$Result = foreach ($Tenantfilter in $tenants) {
23+
try {
24+
$GraphRequest = New-ExoRequest -tenantid $Tenantfilter -cmdlet 'Set-HostedConnectionFilterPolicy' -cmdParams $RequestParams
25+
"Successfully created Connectionfilter for $tenantfilter."
26+
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -tenant $tenantfilter -message "Updated Connection filter rule for $($tenantfilter)" -sev Info
27+
} catch {
28+
"Could not create create Connection Filter rule for $($tenantfilter): $($_.Exception.message)"
29+
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -tenant $tenantfilter -message "Could not create create connection filter rule for $($tenantfilter): $($_.Exception.message)" -sev Error
30+
}
31+
}
32+
33+
# Associate values to output bindings by calling 'Push-OutputBinding'.
34+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
35+
StatusCode = [HttpStatusCode]::OK
36+
Body = @{Results = @($Result) }
37+
})
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using namespace System.Net
2+
3+
Function Invoke-AddConnectionFilterTemplate {
4+
<#
5+
.FUNCTIONALITY
6+
Entrypoint
7+
.ROLE
8+
Exchange.ConnectionFilter.ReadWrite
9+
#>
10+
[CmdletBinding()]
11+
param($Request, $TriggerMetadata)
12+
13+
$APIName = $TriggerMetadata.FunctionName
14+
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message 'Accessed this API' -Sev 'Debug'
15+
Write-Host ($request | ConvertTo-Json -Compress)
16+
17+
try {
18+
$GUID = (New-Guid).GUID
19+
$JSON = if ($request.body.PowerShellCommand) {
20+
Write-Host 'PowerShellCommand'
21+
$request.body.PowerShellCommand | ConvertFrom-Json
22+
}
23+
else {
24+
$GUID = (New-Guid).GUID
25+
([pscustomobject]$Request.body | Select-Object Name, EnableSafeList, IPAllowList , IPBlockList ) | ForEach-Object {
26+
$NonEmptyProperties = $_.psobject.Properties | Where-Object { $null -ne $_.Value } | Select-Object -ExpandProperty Name
27+
$_ | Select-Object -Property $NonEmptyProperties
28+
}
29+
}
30+
$JSON = ($JSON | Select-Object @{n = 'name'; e = { $_.name } }, @{n = 'comments'; e = { $_.comments } }, * | ConvertTo-Json -Depth 10)
31+
$Table = Get-CippTable -tablename 'templates'
32+
$Table.Force = $true
33+
Add-CIPPAzDataTableEntity @Table -Entity @{
34+
JSON = "$json"
35+
RowKey = "$GUID"
36+
PartitionKey = 'ConnectionfilterTemplate'
37+
}
38+
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message "Created Connection Filter Template $($Request.body.name) with GUID $GUID" -Sev 'Debug'
39+
$body = [pscustomobject]@{'Results' = 'Successfully added template' }
40+
41+
}
42+
catch {
43+
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message "Failed to create Connection Filter Template: $($_.Exception.Message)" -Sev 'Error'
44+
$body = [pscustomobject]@{'Results' = "ConnectionFilter Template Deployment failed: $($_.Exception.Message)" }
45+
}
46+
47+
48+
# Associate values to output bindings by calling 'Push-OutputBinding'.
49+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
50+
StatusCode = [HttpStatusCode]::OK
51+
Body = $body
52+
})
53+
54+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using namespace System.Net
2+
3+
Function Invoke-ListConnectionFilter {
4+
<#
5+
.FUNCTIONALITY
6+
Entrypoint
7+
.ROLE
8+
Exchange.ConnectionFilter.Read
9+
#>
10+
[CmdletBinding()]
11+
param($Request, $TriggerMetadata)
12+
13+
$APIName = $TriggerMetadata.FunctionName
14+
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message 'Accessed this API' -Sev 'Debug'
15+
$Tenantfilter = $request.Query.tenantfilter
16+
17+
try {
18+
$Policies = New-ExoRequest -tenantid $Tenantfilter -cmdlet 'Get-HostedConnectionFilterPolicy' | Select-Object * -ExcludeProperty *odata*, *data.type*
19+
$StatusCode = [HttpStatusCode]::OK
20+
} catch {
21+
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
22+
$StatusCode = [HttpStatusCode]::Forbidden
23+
$Policies = $ErrorMessage
24+
}
25+
26+
# Associate values to output bindings by calling 'Push-OutputBinding'.
27+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
28+
StatusCode = $StatusCode
29+
Body = @($Policies)
30+
})
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using namespace System.Net
2+
3+
Function Invoke-ListConnectionFilterTemplates {
4+
<#
5+
.FUNCTIONALITY
6+
Entrypoint
7+
.ROLE
8+
Exchange.ConnectionFilter.Read
9+
#>
10+
[CmdletBinding()]
11+
param($Request, $TriggerMetadata)
12+
13+
$APIName = $TriggerMetadata.FunctionName
14+
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message 'Accessed this API' -Sev 'Debug'
15+
$Table = Get-CippTable -tablename 'templates'
16+
17+
#List new policies
18+
$Table = Get-CippTable -tablename 'templates'
19+
$Filter = "PartitionKey eq 'ConnectionfilterTemplate'"
20+
$Templates = (Get-CIPPAzDataTableEntity @Table -Filter $Filter) | ForEach-Object {
21+
$GUID = $_.RowKey
22+
$data = $_.JSON | ConvertFrom-Json
23+
$data | Add-Member -NotePropertyName 'GUID' -NotePropertyValue $GUID
24+
$data
25+
}
26+
27+
if ($Request.query.ID) { $Templates = $Templates | Where-Object -Property RowKey -EQ $Request.query.id }
28+
29+
30+
# Associate values to output bindings by calling 'Push-OutputBinding'.
31+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
32+
StatusCode = [HttpStatusCode]::OK
33+
Body = @($Templates)
34+
})
35+
36+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using namespace System.Net
2+
3+
Function Invoke-RemoveConnectionfilterTemplate {
4+
<#
5+
.FUNCTIONALITY
6+
Entrypoint
7+
.ROLE
8+
Exchange.ConnectionFilter.ReadWrite
9+
#>
10+
[CmdletBinding()]
11+
param($Request, $TriggerMetadata)
12+
13+
$APIName = $TriggerMetadata.FunctionName
14+
$User = $request.headers.'x-ms-client-principal'
15+
Write-LogMessage -user $User -API $APINAME -message 'Accessed this API' -Sev 'Debug'
16+
17+
$ID = $request.body.id
18+
try {
19+
$Table = Get-CippTable -tablename 'templates'
20+
$Filter = "PartitionKey eq 'ConnectionfilterTemplate' and RowKey eq '$id'"
21+
$ClearRow = Get-CIPPAzDataTableEntity @Table -Filter $Filter -Property PartitionKey, RowKey
22+
Remove-AzDataTableEntity -Force @Table -Entity $clearRow
23+
Write-LogMessage -user $User -API $APINAME -message "Removed Connection Filter Template with ID $ID." -Sev 'Info'
24+
$body = [pscustomobject]@{'Results' = 'Successfully removed Connection Filter Template' }
25+
} catch {
26+
$ErrorMessage = Get-CippException -Exception $_
27+
Write-LogMessage -user $User -API $APINAME -message "Failed to remove Connection Filter template $ID. $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
28+
$body = [pscustomobject]@{'Results' = "Failed to remove template: $($ErrorMessage.NormalizedError)" }
29+
}
30+
31+
32+
# Associate values to output bindings by calling 'Push-OutputBinding'.
33+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
34+
StatusCode = [HttpStatusCode]::OK
35+
Body = $body
36+
})
37+
38+
39+
}

0 commit comments

Comments
 (0)