Skip to content

Commit fa05c71

Browse files
authored
Merge pull request #211135 from NiviShenker/patch-2
bugs fix in migrate-to-policy script
2 parents 48a7f5e + 3ff3d0a commit fa05c71

File tree

1 file changed

+43
-40
lines changed

1 file changed

+43
-40
lines changed

articles/firewall-manager/migrate-to-policy.md

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,7 @@ $FirewallName = "azfw"
3131
$FirewallPolicyResourceGroup = "AzFWPolicyRG"
3232
$FirewallPolicyName = "fwpolicy"
3333
$FirewallPolicyLocation = "WestEurope"
34-
35-
$DefaultAppRuleCollectionGroupName = "ApplicationRuleCollectionGroup"
36-
$DefaultNetRuleCollectionGroupName = "NetworkRuleCollectionGroup"
37-
$DefaultNatRuleCollectionGroupName = "NatRuleCollectionGroup"
38-
$ApplicationRuleGroupPriority = 300
39-
$NetworkRuleGroupPriority = 200
40-
$NatRuleGroupPriority = 100
41-
34+
@@ -43,141 +44,186 @@ $InvalidCharsPattern = "[']"
4235
#Helper functions for translating ApplicationProtocol and ApplicationRule
4336
Function GetApplicationProtocolsString
4437
{
@@ -50,14 +43,12 @@ Function GetApplicationProtocolsString
5043
}
5144
return $output.Substring(0, $output.Length - 1)
5245
}
53-
5446
Function GetApplicationRuleCmd
5547
{
5648
Param([Object] $ApplicationRule)
57-
5849
$cmd = "New-AzFirewallPolicyApplicationRule"
59-
$cmd = $cmd + " -Name " + "'" + $($ApplicationRule.Name) + "'"
60-
50+
$parsedName = ParseRuleName($ApplicationRule.Name)
51+
$cmd = $cmd + " -Name " + "'" + $parsedName + "'"
6152
if ($ApplicationRule.SourceAddresses)
6253
{
6354
$ApplicationRule.SourceAddresses = $ApplicationRule.SourceAddresses -join ","
@@ -68,7 +59,6 @@ Function GetApplicationRuleCmd
6859
$ApplicationRule.SourceIpGroups = $ApplicationRule.SourceIpGroups -join ","
6960
$cmd = $cmd + " -SourceIpGroup " + $ApplicationRule.SourceIpGroups
7061
}
71-
7262
if ($ApplicationRule.Description)
7363
{
7464
$cmd = $cmd + " -Description " + "'" + $ApplicationRule.Description + "'"
@@ -77,32 +67,40 @@ Function GetApplicationRuleCmd
7767
{
7868
$protocols = GetApplicationProtocolsString($ApplicationRule.Protocols)
7969
$cmd = $cmd + " -Protocol " + $protocols
80-
8170
$AppRule = $($ApplicationRule.TargetFqdns) -join ","
8271
$cmd = $cmd + " -TargetFqdn " + $AppRule
83-
8472
}
8573
if ($ApplicationRule.FqdnTags)
8674
{
8775
$cmd = $cmd + " -FqdnTag " + "'" + $ApplicationRule.FqdnTags + "'"
8876
}
89-
9077
return $cmd
9178
}
92-
79+
Function ParseRuleName
80+
{
81+
Param([Object] $RuleName)
82+
if ($RuleName -match $InvalidCharsPattern) {
83+
$newRuleName = $RuleName -split $InvalidCharsPattern -join ""
84+
Write-Host "Rule $RuleName contains an invalid character. Invalid characters have been removed, rule new name is $newRuleName. " -ForegroundColor Cyan
85+
return $newRuleName
86+
}
87+
return $RuleName
88+
}
9389
If (!(Get-AzResourceGroup -Name $FirewallPolicyResourceGroup))
9490
{
9591
New-AzResourceGroup -Name $FirewallPolicyResourceGroup -Location $FirewallPolicyLocation
9692
}
97-
9893
$azfw = Get-AzFirewall -Name $FirewallName -ResourceGroupName $FirewallResourceGroup
99-
10094
Write-Host "creating empty firewall policy"
101-
$fwDnsSetting = New-AzFirewallPolicyDnsSetting -EnableProxy
102-
$fwp = New-AzFirewallPolicy -Name $FirewallPolicyName -ResourceGroupName $FirewallPolicyResourceGroup -Location $FirewallPolicyLocation -ThreatIntelMode $azfw.ThreatIntelMode -DnsSetting $fwDnsSetting -Force
95+
if ($azfw.DNSEnableProxy) {
96+
$fwDnsSetting = New-AzFirewallPolicyDnsSetting -EnableProxy
97+
$fwp = New-AzFirewallPolicy -Name $FirewallPolicyName -ResourceGroupName $FirewallPolicyResourceGroup -Location $FirewallPolicyLocation -ThreatIntelMode $azfw.ThreatIntelMode -DnsSetting $fwDnsSetting -Force
98+
}
99+
else {
100+
$fwp = New-AzFirewallPolicy -Name $FirewallPolicyName -ResourceGroupName $FirewallPolicyResourceGroup -Location $FirewallPolicyLocation -ThreatIntelMode $azfw.ThreatIntelMode
101+
}
103102
Write-Host $fwp.Name "created"
104103
Write-Host "creating " $azfw.ApplicationRuleCollections.Count " application rule collections"
105-
106104
#Translate ApplicationRuleCollection
107105
If ($azfw.ApplicationRuleCollections.Count -gt 0)
108106
{
@@ -128,7 +126,6 @@ If ($azfw.ApplicationRuleCollections.Count -gt 0)
128126
$appRuleGroup = New-AzFirewallPolicyRuleCollectionGroup -Name $DefaultAppRuleCollectionGroupName -Priority $ApplicationRuleGroupPriority -RuleCollection $firewallPolicyAppRuleCollections -FirewallPolicyObject $fwp
129127
Write-Host "Created ApplicationRuleCollectionGroup " $appRuleGroup.Name
130128
}
131-
132129
#Translate NetworkRuleCollection
133130
Write-Host "creating " $azfw.NetworkRuleCollections.Count " network rule collections"
134131
If ($azfw.NetworkRuleCollections.Count -gt 0)
@@ -142,34 +139,35 @@ If ($azfw.NetworkRuleCollections.Count -gt 0)
142139
$firewallPolicyNetRules = @()
143140
ForEach ($rule in $rc.Rules)
144141
{
142+
$parsedName = ParseRuleName($rule.Name)
145143
If ($rule.SourceAddresses)
146144
{
147145
If ($rule.DestinationAddresses)
148146
{
149-
$firewallPolicyNetRule = New-AzFirewallPolicyNetworkRule -Name $rule.Name -SourceAddress $rule.SourceAddresses -DestinationAddress $rule.DestinationAddresses -DestinationPort $rule.DestinationPorts -Protocol $rule.Protocols
147+
$firewallPolicyNetRule = New-AzFirewallPolicyNetworkRule -Name $parsedName -SourceAddress $rule.SourceAddresses -DestinationAddress $rule.DestinationAddresses -DestinationPort $rule.DestinationPorts -Protocol $rule.Protocols
150148
}
151149
elseif ($rule.DestinationIpGroups)
152150
{
153-
$firewallPolicyNetRule = New-AzFirewallPolicyNetworkRule -Name $rule.Name -SourceAddress $rule.SourceAddresses -DestinationIpGroup $rule.DestinationIpGroups -DestinationPort $rule.DestinationPorts -Protocol $rule.Protocols
151+
$firewallPolicyNetRule = New-AzFirewallPolicyNetworkRule -Name $parsedName -SourceAddress $rule.SourceAddresses -DestinationIpGroup $rule.DestinationIpGroups -DestinationPort $rule.DestinationPorts -Protocol $rule.Protocols
154152
}
155153
elseif ($rule.DestinationFqdns)
156154
{
157-
$firewallPolicyNetRule = New-AzFirewallPolicyNetworkRule -Name $rule.Name -SourceAddress $rule.SourceAddresses -DestinationFqdn $rule.DestinationFqdns -DestinationPort $rule.DestinationPorts -Protocol $rule.Protocols
155+
$firewallPolicyNetRule = New-AzFirewallPolicyNetworkRule -Name $parsedName -SourceAddress $rule.SourceAddresses -DestinationFqdn $rule.DestinationFqdns -DestinationPort $rule.DestinationPorts -Protocol $rule.Protocols
158156
}
159157
}
160158
elseif ($rule.SourceIpGroups)
161159
{
162160
If ($rule.DestinationAddresses)
163161
{
164-
$firewallPolicyNetRule = New-AzFirewallPolicyNetworkRule -Name $rule.Name -SourceIpGroup $rule.SourceIpGroups -DestinationAddress $rule.DestinationAddresses -DestinationPort $rule.DestinationPorts -Protocol $rule.Protocols
162+
$firewallPolicyNetRule = New-AzFirewallPolicyNetworkRule -Name $parsedName -SourceIpGroup $rule.SourceIpGroups -DestinationAddress $rule.DestinationAddresses -DestinationPort $rule.DestinationPorts -Protocol $rule.Protocols
165163
}
166164
elseif ($rule.DestinationIpGroups)
167165
{
168-
$firewallPolicyNetRule = New-AzFirewallPolicyNetworkRule -Name $rule.Name -SourceIpGroup $rule.SourceIpGroups -DestinationIpGroup $rule.DestinationIpGroups -DestinationPort $rule.DestinationPorts -Protocol $rule.Protocols
166+
$firewallPolicyNetRule = New-AzFirewallPolicyNetworkRule -Name $parsedName -SourceIpGroup $rule.SourceIpGroups -DestinationIpGroup $rule.DestinationIpGroups -DestinationPort $rule.DestinationPorts -Protocol $rule.Protocols
169167
}
170168
elseif ($rule.DestinationFqdns)
171169
{
172-
$firewallPolicyNetRule = New-AzFirewallPolicyNetworkRule -Name $rule.Name -SourceIpGroup $rule.SourceIpGroups -DestinationFqdn $rule.DestinationFqdns -DestinationPort $rule.DestinationPorts -Protocol $rule.Protocols
170+
$firewallPolicyNetRule = New-AzFirewallPolicyNetworkRule -Name $parsedName -SourceIpGroup $rule.SourceIpGroups -DestinationFqdn $rule.DestinationFqdns -DestinationPort $rule.DestinationPorts -Protocol $rule.Protocols
173171
}
174172
}
175173
Write-Host "Created network rule " $firewallPolicyNetRule.Name
@@ -183,15 +181,13 @@ If ($azfw.NetworkRuleCollections.Count -gt 0)
183181
$netRuleGroup = New-AzFirewallPolicyRuleCollectionGroup -Name $DefaultNetRuleCollectionGroupName -Priority $NetworkRuleGroupPriority -RuleCollection $firewallPolicyNetRuleCollections -FirewallPolicyObject $fwp
184182
Write-Host "Created NetworkRuleCollectionGroup " $netRuleGroup.Name
185183
}
186-
187184
#Translate NatRuleCollection
188185
# Hierarchy for NAT rule collection is different for AZFW and FirewallPolicy. In AZFW you can have a NatRuleCollection with multiple NatRules
189186
# where each NatRule will have its own set of source , dest, translated IPs and ports.
190187
# In FirewallPolicy a NatRuleCollection has a a set of rules which has one condition (source and dest IPs and Ports) and the translated IP and ports
191188
# as part of NatRuleCollection.
192189
# So when translating NAT rules we will have to create separate ruleCollection for each rule in AZFW and every ruleCollection will have only 1 rule.
193-
194-
Write-Host "creating " $azfw.NatRuleCollections.Count " network rule collections"
190+
Write-Host "creating " $azfw.NatRuleCollections.Count " NAT rule collections"
195191
If ($azfw.NatRuleCollections.Count -gt 0)
196192
{
197193
$firewallPolicyNatRuleCollections = @()
@@ -202,23 +198,30 @@ If ($azfw.NatRuleCollections.Count -gt 0)
202198
If ($rc.Rules.Count -gt 0)
203199
{
204200
Write-Host "creating " $rc.Rules.Count " nat rules for collection " $rc.Name
205-
ForEach ($rule in $rc.Rules)
206-
{
207-
$firewallPolicyNatRule = New-AzFirewallPolicyNatRule -Name $rule.Name -SourceAddress $rule.SourceAddresses -TranslatedAddress $rule.TranslatedAddress -TranslatedPort $rule.TranslatedPort -DestinationAddress $rule.DestinationAddresses -DestinationPort $rule.DestinationPorts -Protocol $rule.Protocols
208-
Write-Host "Created nat rule " $firewallPolicyNatRule.Name
201+
202+
ForEach ($rule in $rc.Rules)
203+
{
204+
$parsedName = ParseRuleName($rule.Name)
205+
If ($rule.SourceAddresses)
206+
@@ -188,18 +234,19 @@ If ($azfw.NatRuleCollections.Count -gt 0) {
207+
{
208+
$firewallPolicyNatRule = New-AzFirewallPolicyNatRule -Name $parsedName -SourceIpGroup $rule.SourceIpGroups -TranslatedAddress $rule.TranslatedAddress -TranslatedPort $rule.TranslatedPort -DestinationAddress $rule.DestinationAddresses -DestinationPort $rule.DestinationPorts -Protocol $rule.Protocols
209+
}
210+
Write-Host "Created NAT rule: " $firewallPolicyNatRule.Name
209211
$firewallPolicyNatRules += $firewallPolicyNatRule
210212
}
211-
$natRuleCollectionName = $rc.Name + $rule.Name
213+
214+
$natRuleCollectionName = $rc.Name
212215
$fwpNatRuleCollection = New-AzFirewallPolicyNatRuleCollection -Name $natRuleCollectionName -Priority $priority -ActionType $rc.Action.Type -Rule $firewallPolicyNatRules
213216
$priority += 1
214-
Write-Host "Created NatRuleCollection " $fwpNatRuleCollection.Name
217+
Write-Host "Created NAT RuleCollection " $fwpNatRuleCollection.Name
215218
$firewallPolicyNatRuleCollections += $fwpNatRuleCollection
216219
}
217220
}
218221
$natRuleGroup = New-AzFirewallPolicyRuleCollectionGroup -Name $DefaultNatRuleCollectionGroupName -Priority $NatRuleGroupPriority -RuleCollection $firewallPolicyNatRuleCollections -FirewallPolicyObject $fwp
219-
Write-Host "Created NatRuleCollectionGroup " $natRuleGroup.Name
222+
Write-Host "Created NAT RuleCollectionGroup " $natRuleGroup.Name
220223
}
221224
```
222225
## Next steps
223226

224-
Learn more about Azure Firewall Manager deployment: [Azure Firewall Manager deployment overview](deployment-overview.md).
227+
Learn more about Azure Firewall Manager deployment: [Azure Firewall Manager deployment overview](deployment-overview.md).

0 commit comments

Comments
 (0)