Skip to content

Commit 6ba2ac6

Browse files
authored
Merge pull request Azure#11067 from iatodoro/azfw_forcetun_fix
Bug fixes for Azure Firewall
2 parents 9260f98 + 138c079 commit 6ba2ac6

File tree

5 files changed

+1593
-1613
lines changed

5 files changed

+1593
-1613
lines changed

src/Network/Network.Test/ScenarioTests/AzureFirewallTests.ps1

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ function Test-AzureFirewallCRUDwithManagementIpConfig {
998998
$rgname = Get-ResourceGroupName
999999
$azureFirewallName = Get-ResourceName
10001000
$resourceTypeParent = "Microsoft.Network/AzureFirewalls"
1001-
$location = Get-ProviderLocation $resourceTypeParent "centraluseuap"
1001+
$location = Get-ProviderLocation $resourceTypeParent "eastus2euap"
10021002

10031003
$vnetName = Get-ResourceName
10041004
$subnetName = "AzureFirewallSubnet"
@@ -1056,21 +1056,14 @@ function Test-AzureFirewallCRUDwithManagementIpConfig {
10561056
Assert-ThrowsContains { $getAzureFirewall.AddPublicIpAddress("ABCD") } "Cannot convert argument"
10571057
Assert-ThrowsContains { $getAzureFirewall.AddPublicIpAddress($publicip1) } "already attached to firewall"
10581058

1059-
# Test handling of incorrect values when setting management IP configuration
1060-
Assert-ThrowsContains { $getAzureFirewall.SetManagementIpConfiguration() } "Cannot find an overload"
1061-
Assert-ThrowsContains { $getAzureFirewall.SetManagementIpConfiguration($null) } "Cannot find an overload"
1062-
Assert-ThrowsContains { $getAzureFirewall.SetManagementIpConfiguration($null, $mgmtPublicIp) } "Virtual Network cannot be null"
1063-
Assert-ThrowsContains { $getAzureFirewall.SetManagementIpConfiguration("ABCD", "ABCDE") } "Cannot convert argument"
1064-
1065-
10661059
# Test handling of incorrect values when removing public IP Address
10671060
Assert-ThrowsContains { $getAzureFirewall.RemovePublicIpAddress() } "Cannot find an overload"
10681061
Assert-ThrowsContains { $getAzureFirewall.RemovePublicIpAddress($null) } "Public IP Address cannot be null"
10691062
Assert-ThrowsContains { $getAzureFirewall.RemovePublicIpAddress("ABCD") } "Cannot convert argument"
10701063
Assert-ThrowsContains { $getAzureFirewall.RemovePublicIpAddress($mgmtPublicIp) } "not attached to firewall"
10711064

1072-
# Change management public IP address
1073-
$getAzureFirewall.SetManagementIpConfiguration($vnet, $mgmtPublicIp2)
1065+
# Change Management PIP
1066+
$getAzureFirewall.ManagementIpConfiguration.PublicIpAddress = $mgmtPublicIp2
10741067

10751068
# Set AzureFirewall
10761069
Set-AzFirewall -AzureFirewall $getAzureFirewall

src/Network/Network.Test/SessionRecords/Commands.Network.Test.ScenarioTests.AzureFirewallTests/TestAzureFirewallCRUDwithManagementIpConfig.json

Lines changed: 1553 additions & 1560 deletions
Large diffs are not rendered by default.

src/Network/Network/Models/AzureFirewall/PSAzureFirewall.cs

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,11 @@ public void AddPublicIpAddress(PSPublicIpAddress publicIpAddress)
184184
throw new ArgumentNullException(nameof(publicIpAddress), "Public IP Address cannot be null!");
185185
}
186186

187+
PSAzureFirewallIpConfiguration conflictingIpConfig = null;
188+
187189
if (this.IpConfigurations.Count > 0)
188190
{
189-
var conflictingIpConfig = this.IpConfigurations.SingleOrDefault
191+
conflictingIpConfig = this.IpConfigurations.SingleOrDefault
190192
(ipConfig => string.Equals(ipConfig.PublicIpAddress?.Id, publicIpAddress.Id, System.StringComparison.CurrentCultureIgnoreCase));
191193

192194
if (conflictingIpConfig != null)
@@ -199,44 +201,26 @@ public void AddPublicIpAddress(PSPublicIpAddress publicIpAddress)
199201
throw new InvalidOperationException($"Please invoke {nameof(Allocate)} to attach the firewall to a Virtual Network");
200202
}
201203

204+
var i = 0;
205+
conflictingIpConfig = null;
206+
var newIpConfigName = "";
207+
208+
do
209+
{
210+
newIpConfigName = $"{AzureFirewallIpConfigurationName}{this.IpConfigurations.Count + i}";
211+
conflictingIpConfig = this.IpConfigurations.SingleOrDefault
212+
(ipConfig => string.Equals(ipConfig.Name, newIpConfigName, System.StringComparison.CurrentCultureIgnoreCase));
213+
i++;
214+
} while (conflictingIpConfig != null);
215+
202216
this.IpConfigurations.Add(
203217
new PSAzureFirewallIpConfiguration
204218
{
205-
Name = $"{AzureFirewallIpConfigurationName}{this.IpConfigurations.Count}",
219+
Name = newIpConfigName,
206220
PublicIpAddress = new PSResourceId { Id = publicIpAddress.Id }
207221
});
208222
}
209223

210-
public void SetManagementIpConfiguration(PSVirtualNetwork virtualNetwork, PSPublicIpAddress publicIpAddress)
211-
{
212-
if (publicIpAddress == null)
213-
{
214-
throw new ArgumentNullException(nameof(publicIpAddress), "Public IP Address cannot be null!");
215-
}
216-
217-
if (virtualNetwork == null)
218-
{
219-
throw new ArgumentNullException(nameof(virtualNetwork), "Virtual Network cannot be null!");
220-
}
221-
222-
PSSubnet subnet = null;
223-
try
224-
{
225-
subnet = virtualNetwork.Subnets.Single(mgmtSubnet => AzureFirewallMgmtSubnetName.Equals(mgmtSubnet.Name));
226-
}
227-
catch (InvalidOperationException)
228-
{
229-
throw new ArgumentException($"Virtual Network {virtualNetwork.Name} should contain a Subnet named {AzureFirewallMgmtSubnetName}");
230-
}
231-
232-
this.ManagementIpConfiguration = new PSAzureFirewallIpConfiguration
233-
{
234-
Name = AzureFirewallMgmtIpConfigurationName,
235-
PublicIpAddress = new PSResourceId { Id = publicIpAddress.Id },
236-
Subnet = new PSResourceId { Id = subnet.Id }
237-
};
238-
}
239-
240224
public void RemovePublicIpAddress(PSPublicIpAddress publicIpAddress)
241225
{
242226
if (publicIpAddress == null)
@@ -252,6 +236,11 @@ public void RemovePublicIpAddress(PSPublicIpAddress publicIpAddress)
252236
throw new ArgumentException($"Public IP Address {publicIpAddress.Id} is not attached to firewall {this.Name}");
253237
}
254238

239+
if (this.IpConfigurations.Count > 1 && ipConfigToRemove.Subnet != null)
240+
{
241+
throw new InvalidOperationException($"Cannot remove IpConfiguration {ipConfigToRemove.Name} because it references subnet {ipConfigToRemove.Subnet.Id}. Move the subnet reference to another IpConfiguration and try again.");
242+
}
243+
255244
if (this.IpConfigurations.Count == 1)
256245
{
257246
Console.ForegroundColor = ConsoleColor.Yellow;

src/Network/Network/help/New-AzIpGroup.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@ The **New-AzIpGroup** cmdlet creates an Azure IpGroup
2525

2626
### Example 1
2727
```powershell
28-
$ipGroup = '/subscriptions/8c992d64-fce9-426d-b278-85642dfeab03/resourceGroups/ipGroupRG/providers/Microsoft.Network/virtualNetworkGateways/erGateway'
2928
New-AzIpGroup -Name ipGroup -ResourceGroupName ipGroupRG -Location 'West US'
3029
```
3130

3231
### Example 2
3332
```powershell
34-
$ipGroup = '/subscriptions/8c992d64-fce9-426d-b278-85642dfeab03/resourceGroups/ipGroupRG/providers/Microsoft.Network/virtualNetworkGateways/erGateway'
3533
New-AzIpGroup -Name ipGroup -ResourceGroupName ipGroupRG -Location 'West US' -IpAddress 10.0.0.0/24,11.9.0.0/24
3634
```
3735

src/Network/Network/help/Set-AzFirewall.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,23 @@ $pip = Get-AzPublicIpAddress - ResourceGroupName rgName -Name publicIpName
7373
$firewall.Allocate($vnet, $pip)
7474
$firewall | Set-AzFirewall
7575
```
76-
7776
This example retrieves a Firewall, deallocates the firewall, and saves it. The Deallocate command removes the running
7877
service but preserves the firewall's configuration. For changes to be reflected in cloud, Set-AzFirewall must be called.
7978
If user wants to start the service again, the Allocate method should be called on the firewall.
8079
The new VNet and Public IP must be in the same resource group as the Firewall. Again, for changes to be reflected in cloud,
8180
Set-AzFirewall must be called.
8281

83-
### 5: Add a Public IP address to an Azure Firewall
82+
### 5: Allocate with a management public IP address for forced tunneling scenarios
83+
```
84+
$vnet = Get-AzVirtualNetwork -ResourceGroupName rgName -Name anotherVNetName
85+
$pip = Get-AzPublicIpAddress - ResourceGroupName rgName -Name publicIpName
86+
$mgmtPip = Get-AzPublicIpAddress - ResourceGroupName rgName -Name MgmtPublicIpName
87+
$firewall.Allocate($vnet, $pip, $mgmtPip)
88+
$firewall | Set-AzFirewall
89+
```
90+
This example allocates the firewall with a management public IP address and subnet for forced tunneling scenarios. The VNet must contain a subnet called "AzureFirewallManagementSubnet".
91+
92+
### 6: Add a Public IP address to an Azure Firewall
8493
```
8594
$pip = New-AzPublicIpAddress -Name "azFwPublicIp1" -ResourceGroupName "rg" -Sku "Standard" -Location "centralus" -AllocationMethod Static
8695
$azFw = Get-AzFirewall -Name "AzureFirewall" -ResourceGroupName "rg"
@@ -91,7 +100,7 @@ $azFw | Set-AzFirewall
91100

92101
In this example, the Public IP Address "azFwPublicIp1" as attached to the Firewall.
93102

94-
### 6: Remove a Public IP address from an Azure Firewall
103+
### 7: Remove a Public IP address from an Azure Firewall
95104
```
96105
$pip = Get-AzPublicIpAddress -Name "azFwPublicIp1" -ResourceGroupName "rg"
97106
$azFw = Get-AzFirewall -Name "AzureFirewall" -ResourceGroupName "rg"
@@ -102,18 +111,16 @@ $azFw | Set-AzFirewall
102111

103112
In this example, the Public IP Address "azFwPublicIp1" as detached from the Firewall.
104113

105-
### 7: Set management subnet and public IP address on an Azure Firewall
114+
### 8: Change the management public IP address on an Azure Firewall
106115
```
107-
$mgmtPip = Get-AzPublicIpAddress -Name "managementPublicIp1" -ResourceGroupName "rg"
108-
$vnet = Get-AzVirtualNetwork -ResourceGroupName "rg" -Name anotherVNetName
116+
$newMgmtPip = New-AzPublicIpAddress -Name "azFwMgmtPublicIp2" -ResourceGroupName "rg" -Sku "Standard" -Location "centralus" -AllocationMethod Static
109117
$azFw = Get-AzFirewall -Name "AzureFirewall" -ResourceGroupName "rg"
110-
$azFw.SetManagementIpConfiguration($vnet, $mgmtPip)
118+
$azFw.ManagementIpConfiguration.PublicIpAddress = $newMgmtPip
111119
112120
$azFw | Set-AzFirewall
113121
```
114122

115-
In this example, the subnet "AzureFirewallManagementSubnet" and the Public IP address ""managementPublicIp1" will be attached to the firewall.
116-
For forced tunneling scenarios, this subnet and IP address will be used by the firewall for management traffic.
123+
In this example, the management public IP address of the firewall will be changed to "AzFwMgmtPublicIp2"
117124

118125

119126
## PARAMETERS

0 commit comments

Comments
 (0)