1
+ # ----------------------------------------------------------------------------------
2
+ #
3
+ # Copyright Microsoft Corporation
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+ # ----------------------------------------------------------------------------------
14
+
15
+ <#
16
+ . SYNOPSIS
17
+ Tests AzureFirewallPolicyCRUD.
18
+ #>
19
+ function Test-AzureFirewallPolicyCRUD {
20
+ # Setup
21
+ $rgname = Get-ResourceGroupName
22
+ $azureFirewallPolicyName = Get-ResourceName
23
+ $azureFirewallPolicyAsJobName = Get-ResourceName
24
+ $resourceTypeParent = " Microsoft.Network/FirewallPolicies"
25
+ $location = " westcentralus"
26
+
27
+ $ruleGroupName = Get-ResourceName
28
+
29
+ # AzureFirewallPolicyApplicationRuleCollection
30
+ $appRcName = " appRc"
31
+ $appRcPriority = 400
32
+ $appRcActionType = " Allow"
33
+
34
+ $pipelineRcPriority = 154
35
+
36
+ # AzureFirewallPolicyApplicationRuleCollection 2
37
+ $appRc2Name = " appRc2"
38
+ $appRc2Priority = 300
39
+ $appRc2ActionType = " Deny"
40
+
41
+ # AzureFirewallPolicyApplicationRule 1
42
+ $appRule1Name = " appRule"
43
+ $appRule1Desc = " desc1"
44
+ $appRule1Fqdn1 = " *google.com"
45
+ $appRule1Fqdn2 = " *microsoft.com"
46
+ $appRule1Protocol1 = " http:80"
47
+ $appRule1Port1 = 80
48
+ $appRule1ProtocolType1 = " http"
49
+ $appRule1Protocol2 = " https:443"
50
+ $appRule1Port2 = 443
51
+ $appRule1ProtocolType2 = " https"
52
+ $appRule1SourceAddress1 = " 192.168.0.0/16"
53
+
54
+ # AzureFirewallPolicyApplicationRule 2
55
+ $appRule2Name = " appRule2"
56
+ $appRule2Fqdn1 = " *bing.com"
57
+ $appRule2Protocol1 = " http:8080"
58
+ $appRule2Protocol2 = " https:443"
59
+ $appRule2Port1 = 8080
60
+ $appRule2ProtocolType1 = " http"
61
+ $appRule2SourceAddress1 = " 192.168.0.0/16"
62
+
63
+ # AzureFirewallPolicyNetworkRuleCollection
64
+ $networkRcName = " networkRc"
65
+ $networkRcPriority = 200
66
+ $networkRcActionType = " Deny"
67
+
68
+ # AzureFirewallPolicyNetworkRule 1
69
+ $networkRule1Name = " networkRule"
70
+ $networkRule1Desc = " desc1"
71
+ $networkRule1SourceAddress1 = " 10.0.0.0"
72
+ $networkRule1SourceAddress2 = " 111.1.0.0/24"
73
+ $networkRule1DestinationAddress1 = " *"
74
+ $networkRule1Protocol1 = " UDP"
75
+ $networkRule1Protocol2 = " TCP"
76
+ $networkRule1Protocol3 = " ICMP"
77
+ $networkRule1DestinationPort1 = " 90"
78
+
79
+ # AzureFirewallPolicyNatRuleCollection
80
+ $natRcName = " natRc"
81
+ $natRcPriority = 200
82
+ $natRcActionType = " Dnat"
83
+
84
+ # AzureFirewallPolicyNatRule 1
85
+ $natRule1Name = " natRule"
86
+ $natRule1Desc = " desc1"
87
+ $natRule1SourceAddress1 = " 10.0.0.0"
88
+ $natRule1SourceAddress2 = " 111.1.0.0/24"
89
+ $natRule1DestinationAddress1 = " 1.2.3.4"
90
+ $natRule1Protocol1 = " UDP"
91
+ $natRule1Protocol2 = " TCP"
92
+ $natRule1DestinationPort1 = " 90"
93
+ $natRule1TranslatedAddress = " 10.1.2.3"
94
+ $natRule1TranslatedPort = " 91"
95
+
96
+ try {
97
+ # Create the resource group
98
+ $resourceGroup = New-AzResourceGroup - Name $rgname - Location $location - Tags @ { testtag = " testval" }
99
+
100
+ # Create AzureFirewallPolicy (with no rules, ThreatIntel is in Alert mode by default)
101
+ $azureFirewallPolicy = New-AzFirewallPolicy - Name $azureFirewallPolicyName - ResourceGroupName $rgname - Location $location
102
+
103
+ # Get AzureFirewallPolicy
104
+ $getAzureFirewallPolicy = Get-AzFirewallPolicy - Name $azureFirewallPolicyName - ResourceGroupName $rgname
105
+
106
+ # verification
107
+ Assert-AreEqual $rgName $getAzureFirewallPolicy.ResourceGroupName
108
+ Assert-AreEqual $azureFirewallPolicyName $getAzureFirewallPolicy.Name
109
+ Assert-NotNull $getAzureFirewallPolicy.Location
110
+ Assert-AreEqual (Normalize- Location $location ) $getAzureFirewallPolicy.Location
111
+ Assert-AreEqual " Alert" $getAzureFirewallPolicy.ThreatIntelMode
112
+
113
+
114
+ # Create Application Rules
115
+ $appRule = New-AzFirewallPolicyApplicationRule - Name $appRule1Name - Description $appRule1Desc - Protocol $appRule1Protocol1 , $appRule1Protocol2 - TargetFqdn $appRule1Fqdn1 , $appRule1Fqdn2 - SourceAddress $appRule1SourceAddress1
116
+ $appRule2 = New-AzFirewallPolicyApplicationRule - Name $appRule2Name - Description $appRule1Desc - Protocol $appRule2Protocol1 , $appRule2Protocol2 - TargetFqdn $appRule2Fqdn1 - SourceAddress $appRule2SourceAddress1
117
+
118
+ # Create Network Rule Condition
119
+ $networkRule = New-AzFirewallPolicyNetworkRule - Name $networkRule1Name - Description $networkRule1Desc - Protocol $networkRule1Protocol1 , $networkRule1Protocol2 - SourceAddress $networkRule1SourceAddress1 , $networkRule1SourceAddress2 - DestinationAddress $networkRule1DestinationAddress1 - DestinationPort $networkRule1DestinationPort1
120
+
121
+ # Create Filter Rule with 2 rules
122
+ $appRc = New-AzFirewallPolicyFilterRuleCollection - Name $appRcName - Priority $appRcPriority - Rule $appRule , $appRule2 - ActionType $appRcActionType
123
+ # Create a second Filter Rule Collection with 1 rule
124
+ $appRc2 = New-AzFirewallPolicyFilterRuleCollection - Name $appRc2Name - Priority $appRc2Priority - Rule $networkRule - ActionType $appRc2ActionType
125
+
126
+ # Create a NAT rule
127
+ $natRc = New-AzFirewallPolicyNatRuleCollection - Name $networkRcName - Priority $natRcPriority - Rule $networkRule - TranslatedAddress $natRule1TranslatedAddress - TranslatedPort $natRule1TranslatedPort - ActionType $natRcActionType
128
+
129
+ New-AzFirewallPolicyRuleCollectionGroup - Name $ruleGroupName - Priority 100 - RuleCollection $appRc , $appRc2 , $natRc - FirewallPolicyObject $azureFirewallPolicy
130
+
131
+
132
+ # # Update ThreatIntel mode
133
+ $azureFirewallPolicy.ThreatIntelMode = " Deny"
134
+ # Set AzureFirewallPolicy
135
+ Set-AzFirewallPolicy - InputObject $azureFirewallPolicy
136
+ # Get AzureFirewallPolicy
137
+ $getAzureFirewallPolicy = Get-AzFirewallPolicy - Name $azureFirewallPolicyName - ResourceGroupName $rgName
138
+
139
+ # #verification
140
+ Assert-AreEqual $rgName $getAzureFirewallPolicy.ResourceGroupName
141
+ Assert-AreEqual $azureFirewallPolicyName $getAzureFirewallPolicy.Name
142
+ Assert-NotNull $getAzureFirewallPolicy.Location
143
+ Assert-AreEqual $location $getAzureFirewallPolicy.Location
144
+ Assert-AreEqual " Deny" $getAzureFirewallPolicy.ThreatIntelMode
145
+
146
+ # # Check rule groups count
147
+ Assert-AreEqual 1 @ ($getAzureFirewallPolicy.RuleCollectionGroups ).Count
148
+
149
+ $getRg = Get-AzFirewallPolicyRuleCollectionGroup - Name $ruleGroupName - AzureFirewallPolicy $getAzureFirewallPolicy
150
+
151
+ Assert-AreEqual 3 @ ($getRg.properties.ruleCollection ).Count
152
+
153
+ $filterRuleCollection1 = $getRg.Properties.GetRuleCollectionByName ($appRcName )
154
+ $natRuleCollection = $getRg.Properties.GetRuleCollectionByName ($networkRcName )
155
+
156
+ # Verify filter Rule1
157
+ Assert-AreEqual $appRcName $filterRuleCollection1.Name
158
+ Assert-AreEqual $appRcPriority $filterRuleCollection1.Priority
159
+ Assert-AreEqual $appRcActionType $filterRuleCollection1.Action.Type
160
+ Assert-AreEqual 2 $filterRuleCollection1.Rules.Count
161
+
162
+ $appRule = $filterRuleCollection1.GetRuleByName ($appRule1Name )
163
+ # Verify application rule 1
164
+ Assert-AreEqual $appRule1Name $appRule.Name
165
+
166
+ Assert-AreEqual 1 $appRule.SourceAddresses.Count
167
+ Assert-AreEqual $appRule1SourceAddress1 $appRule.SourceAddresses [0 ]
168
+
169
+ Assert-AreEqual 2 $appRule.Protocols.Count
170
+ Assert-AreEqual $appRule1ProtocolType1 $appRule.Protocols [0 ].ProtocolType
171
+ Assert-AreEqual $appRule1ProtocolType2 $appRule.Protocols [1 ].ProtocolType
172
+ Assert-AreEqual $appRule1Port1 $appRule.Protocols [0 ].Port
173
+ Assert-AreEqual $appRule1Port2 $appRule.Protocols [1 ].Port
174
+
175
+ Assert-AreEqual 2 $appRule.TargetFqdns.Count
176
+ Assert-AreEqual $appRule1Fqdn1 $appRule.TargetFqdns [0 ]
177
+ Assert-AreEqual $appRule1Fqdn2 $appRule.TargetFqdns [1 ]
178
+
179
+ # Verify NAT rule collection and NAT rule)
180
+ $natRule = $natRuleCollection.GetRuleByName ($networkRcName )
181
+
182
+ Assert-AreEqual $networkRcName $natRuleCollection.Name
183
+ Assert-AreEqual $natRcPriority $natRuleCollection.Priority
184
+
185
+ Assert-AreEqual $networkRule1Name $natRule.Name
186
+
187
+ Assert-AreEqual 2 $natRule.SourceAddresses.Count
188
+ Assert-AreEqual $natRule1SourceAddress1 $natRule.SourceAddresses [0 ]
189
+ Assert-AreEqual $natRule1SourceAddress2 $natRule.SourceAddresses [1 ]
190
+
191
+ Assert-AreEqual 1 $natRule.DestinationAddresses.Count
192
+
193
+ Assert-AreEqual 2 $natRule.Protocols.Count
194
+ Assert-AreEqual $networkRule1Protocol1 $natRule.Protocols [0 ]
195
+ Assert-AreEqual $networkRule1Protocol2 $natRule.Protocols [1 ]
196
+
197
+ Assert-AreEqual 1 $natRule.DestinationPorts.Count
198
+ Assert-AreEqual $natRule1DestinationPort1 $natRule.DestinationPorts [0 ]
199
+
200
+ Assert-AreEqual $natRule1TranslatedAddress $natRuleCollection.TranslatedAddress
201
+ Assert-AreEqual $natRule1TranslatedPort $natRuleCollection.TranslatedPort
202
+
203
+
204
+ $testPipelineRg = Get-AzFirewallPolicyRuleCollectionGroup - Name $ruleGroupName - AzureFirewallPolicyName $getAzureFirewallPolicy.Name - ResourceGroupName $rgname
205
+ $testPipelineRg | Set-AzFirewallPolicyRuleCollectionGroup - Priority $pipelineRcPriority
206
+ $testPipelineRg = Get-AzFirewallPolicyRuleCollectionGroup - Name $ruleGroupName - AzureFirewallPolicyName $getAzureFirewallPolicy.Name - ResourceGroupName $rgname
207
+ Assert-AreEqual $pipelineRcPriority $testPipelineRg.properties.Priority
208
+
209
+ $azureFirewallPolicyAsJob = New-AzFirewallPolicy - Name $azureFirewallPolicyAsJobName - ResourceGroupName $rgname - Location $location - AsJob
210
+ $result = $azureFirewallPolicyAsJob | Wait-Job
211
+ Assert-AreEqual " Completed" $result.State ;
212
+ }
213
+ finally {
214
+ # Cleanup
215
+ Clean - ResourceGroup $rgname
216
+ }
217
+ }
0 commit comments