-
Notifications
You must be signed in to change notification settings - Fork 30
/
Deny-Route-NextHopVirtualAppliance.Tests.ps1
148 lines (130 loc) · 7.72 KB
/
Deny-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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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 'Deny-Route-NextHopVirtualAppliance'" -Tag "deny-route-nexthopvirtualappliance" {
# Create or update route is actually the same PUT request, hence testing create covers update as well.
# PATCH requests are currently not supported in Network Resource Provider.
# See also: https://docs.microsoft.com/en-us/rest/api/virtualnetwork/routes/createorupdate
Context "When route is created or updated" -Tag "deny-route-nexthopvirtualappliance-route-create-update" {
It "Should deny incompliant route 0.0.0.0/0 with next hop type 'None'" -Tag "deny-route-nexthopvirtualappliance-route-create-update-10" {
AzTest -ResourceGroup {
param($ResourceGroup)
$routeTable = New-AzRouteTable `
-Name "route-table" `
-ResourceGroupName $ResourceGroup.ResourceGroupName `
-Location $ResourceGroup.Location
# Should be disallowed by policy, so exception should be thrown.
{
# Directly calling REST API with PUT routes, since New-AzRouteConfig/Set-AzRouteTable will issue PUT routeTables.
$routeTable | Invoke-RoutePut `
-Name "default" `
-AddressPrefix "0.0.0.0/0" `
-NextHopType "None" # Incompliant.
} | Should -Throw "*RequestDisallowedByPolicy*Deny-Route-NextHopVirtualAppliance*"
}
}
It "Should deny incompliant route 0.0.0.0/0 with next hop IP address '10.10.10.10'" -Tag "deny-route-nexthopvirtualappliance-route-create-update-20" {
AzTest -ResourceGroup {
param($ResourceGroup)
$routeTable = New-AzRouteTable `
-Name "route-table" `
-ResourceGroupName $ResourceGroup.ResourceGroupName `
-Location $ResourceGroup.Location
# Should be disallowed by policy, so exception should be thrown.
{
# Directly calling REST API with PUT routes, since New-AzRouteConfig/Set-AzRouteTable will issue PUT routeTables.
$routeTable | Invoke-RoutePut `
-Name "default" `
-AddressPrefix "0.0.0.0/0" `
-NextHopType "VirtualAppliance" `
-NextHopIpAddress "10.10.10.10" # Incompliant.
} | Should -Throw "*RequestDisallowedByPolicy*Deny-Route-NextHopVirtualAppliance*"
}
}
It "Should allow compliant route route 0.0.0.0/0" -Tag "deny-route-nexthopvirtualappliance-route-create-update-30" {
AzTest -ResourceGroup {
param($ResourceGroup)
$routeTable = New-AzRouteTable `
-Name "route-table" `
-ResourceGroupName $ResourceGroup.ResourceGroupName `
-Location $ResourceGroup.Location
# Should be allowed by policy, so no exception should be thrown.
{
# Directly calling REST API with PUT routes, since New-AzRouteConfig/Set-AzRouteTable will issue PUT routeTables.
$routeTable | Invoke-RoutePut `
-Name "default" `
-AddressPrefix "0.0.0.0/0" `
-NextHopType "VirtualAppliance" `
-NextHopIpAddress (Get-VirtualApplianceIpAddress -Location $routeTable.Location) # Compliant.
} | Should -Not -Throw
}
}
}
# Create or update route tables is actually the same PUT request, hence testing create covers update as well.
# PATCH requests are currently not supported in Network Resource Provider.
# See also: https://docs.microsoft.com/en-us/rest/api/virtualnetwork/routetables/createorupdate
Context "When route table is created or updated" -Tag "deny-route-nexthopvirtualappliance-routetable-create-update" {
It "Should deny route table containing incompliant route 0.0.0.0/0 with next hop type 'None'" -Tag "deny-route-nexthopvirtualappliance-routetable-create-update-10" {
AzTest -ResourceGroup {
param($ResourceGroup)
# Create route table.
$route = New-AzRouteConfig `
-Name "virtual-appliance" `
-AddressPrefix "0.0.0.0/0" `
-NextHopType "None" # Incompliant.
# Should be disallowed by policy, so exception should be thrown.
{
New-AzRouteTable `
-Name "route-table" `
-ResourceGroupName $ResourceGroup.ResourceGroupName `
-Location $ResourceGroup.Location `
-Route $route `
-ErrorAction Stop # Otherwise no exception would be thrown, since $ErrorActionPreference defaults to 'Continue' in PowerShell.
} | Should -Throw "*RequestDisallowedByPolicy*Deny-Route-NextHopVirtualAppliance*"
}
}
It "Should deny route table containing incompliant route 0.0.0.0/0 with next hop IP address '10.10.10.10'" -Tag "deny-route-nexthopvirtualappliance-routetable-create-update-20" {
AzTest -ResourceGroup {
param($ResourceGroup)
# Create route table.
$route = New-AzRouteConfig `
-Name "virtual-appliance" `
-AddressPrefix "0.0.0.0/0" `
-NextHopType "VirtualAppliance" `
-NextHopIpAddress "10.10.10.10" # Incompliant.
# Should be disallowed by policy, so exception should be thrown.
{
New-AzRouteTable `
-Name "route-table" `
-ResourceGroupName $ResourceGroup.ResourceGroupName `
-Location $ResourceGroup.Location `
-Route $route `
-ErrorAction Stop # Otherwise no exception would be thrown, since $ErrorActionPreference defaults to 'Continue' in PowerShell.
} | Should -Throw "*RequestDisallowedByPolicy*Deny-Route-NextHopVirtualAppliance*"
}
}
It "Should allow route table containing compliant route 0.0.0.0/0" -Tag "deny-route-nexthopvirtualappliance-route-routetable-update-30" {
AzTest -ResourceGroup {
param($ResourceGroup)
# Create route table
$route = New-AzRouteConfig `
-Name "virtual-appliance" `
-AddressPrefix "0.0.0.0/0" `
-NextHopType "VirtualAppliance" `
-NextHopIpAddress (Get-VirtualApplianceIpAddress -Location $ResourceGroup.Location) # Compliant.
# Should be allowed by policy, so no exception should be thrown.
{
New-AzRouteTable `
-Name "route-table" `
-ResourceGroupName $ResourceGroup.ResourceGroupName `
-Location $ResourceGroup.Location `
-Route $route `
-ErrorAction Stop # Otherwise no exception would be thrown, since $ErrorActionPreference defaults to 'Continue' in PowerShell.
} | Should -Not -Throw
}
}
}
}