-
Notifications
You must be signed in to change notification settings - Fork 30
/
Audit-Route-NextHopVirtualAppliance.Tests.ps1
65 lines (54 loc) · 3.2 KB
/
Audit-Route-NextHopVirtualAppliance.Tests.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
Import-Module -Name Az.Network
Import-Module -Name Az.Resources
Import-Module "$($PSScriptRoot)/../utils/Policy.Utils.psm1" -Force
Import-Module "$($PSScriptRoot)/../utils/Rest.Utils.psm1" -Force
Import-Module "$($PSScriptRoot)/../utils/RouteTable.Utils.psm1" -Force
Import-Module "$($PSScriptRoot)/../utils/Test.Utils.psm1" -Force
Describe "Testing policy 'Audit-Route-NextHopVirtualAppliance'" -Tag "audit-route-nexthopvirtualappliance" {
Context "When auditing route tables" {
It "Should mark route table as compliant with route 0.0.0.0/0 pointing to virtual appliance." -Tag "audit-route-nexthopvirtualappliance-compliant" {
AzTest -ResourceGroup {
param($ResourceGroup)
# Create compliant route table.
$route = New-AzRouteConfig `
-Name "default" `
-AddressPrefix "0.0.0.0/0" `
-NextHopType "VirtualAppliance" `
-NextHopIpAddress (Get-VirtualApplianceIpAddress -Location $ResourceGroup.Location)
$routeTable = New-AzRouteTable `
-Name "route-table" `
-ResourceGroupName $ResourceGroup.ResourceGroupName `
-Location $ResourceGroup.Location `
-Route $Route
# Trigger compliance scan for resource group and wait for completion.
$ResourceGroup | Complete-PolicyComplianceScan
# Verify that route table is compliant.
$routeTable
| Get-PolicyComplianceState -PolicyDefinitionName "Audit-Route-NextHopVirtualAppliance"
| Should -BeTrue
}
}
It "Should mark route table as incompliant without route 0.0.0.0/0 pointing to virtual appliance." -Tag "audit-route-nexthopvirtualappliance-incompliant" {
AzTest -ResourceGroup {
param($ResourceGroup)
# Create incompliant route table by deleting route 0.0.0.0/0 pointing to the virtual appliance.
$routeTable = New-AzRouteTable `
-Name "route-table" `
-ResourceGroupName $ResourceGroup.ResourceGroupName `
-Location $ResourceGroup.Location `
-Route $Route
# Get route 0.0.0.0/0 pointing to the virtual appliance, which was added by policy.
$route = Get-RouteNextHopVirtualAppliance -RouteTable $RouteTable
# Remove-AzRouteConfig/Set-AzRouteTable will issue a PUT request for routeTables and hence policy might kick in.
# In order to delete the route without policy interfering, directly call the REST API by issuing a DELETE request for route.
$routeTable | Invoke-RouteDelete -Route $route
# Trigger compliance scan for resource group and wait for completion.
$ResourceGroup | Complete-PolicyComplianceScan
# Verify that route table is incompliant.
$routeTable
| Get-PolicyComplianceState -PolicyDefinitionName "Audit-Route-NextHopVirtualAppliance"
| Should -BeFalse
}
}
}
}