Skip to content

Commit 2a25730

Browse files
nikhilpadhye1saisujithreddymwyunchi-ms
authored
Adding support for AzureFirewallPacketCapture in powershell (#24003)
* Adding support for AzureFirewallPacketCapture in powershell * adding test structure * fixing error in get call breaking builds * adding test and commands to files * session recordings * fixing signatures * added change to changelog * minor change in changelog * resolving merge conflict in changelog * merge conflict resolution * removing changes since i dont have write permissions * Help files * reveting az.netword.md changes * updated az.network.md * updating the help files * Update New-AzFirewallPacketCaptureParameter.md * Update Invoke-AzFirewallPacketCapture.md * Update New-AzFirewallPacketCaptureParameter.md * Update New-AzFirewallPacketCaptureRule.md * Update Invoke-AzFirewallPacketCapture.md * Update New-AzFirewallPacketCaptureParameter.md * Update Invoke-AzFirewallPacketCapture.md --------- Co-authored-by: Sai Sujith Reddy Mankala <[email protected]> Co-authored-by: Yunchi Wang <[email protected]>
1 parent a67dcfc commit 2a25730

18 files changed

+2403
-6
lines changed

src/Network/Network.Test/ScenarioTests/AzureFirewallTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,13 @@ public void TestGetAzureFirewallLearnedIpPrefixes()
201201
{
202202
TestRunner.RunTestScript("Test-GetAzureFirewallLearnedIpPrefixes");
203203
}
204+
205+
[Fact]
206+
[Trait(Category.AcceptanceType, Category.CheckIn)]
207+
[Trait(Category.Owner, NrpTeamAlias.azurefirewall)]
208+
public void TestInvokeAzureFirewallPacketCapture()
209+
{
210+
TestRunner.RunTestScript("Test-InvokeAzureFirewallPacketCapture");
211+
}
204212
}
205213
}

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,4 +2119,48 @@ function Test-GetAzureFirewallLearnedIpPrefixes {
21192119
# Cleanup
21202120
Clean-ResourceGroup $rgname
21212121
}
2122+
}
2123+
2124+
<#
2125+
.SYNOPSIS
2126+
Tests Invoke-AzureFirewallPacketCapture
2127+
#>
2128+
function Test-InvokeAzureFirewallPacketCapture {
2129+
$rgname = Get-ResourceGroupName
2130+
$azureFirewallName = Get-ResourceName
2131+
$resourceTypeParent = "Microsoft.Network/AzureFirewalls"
2132+
$location = Get-ProviderLocation $resourceTypeParent "eastus"
2133+
2134+
$vnetName = Get-ResourceName
2135+
$subnetName = "AzureFirewallSubnet"
2136+
$publicIpName = Get-ResourceName
2137+
2138+
try {
2139+
2140+
# Create the resource group
2141+
$resourceGroup = New-AzResourceGroup -Name $rgname -Location $location
2142+
2143+
# Create public ip
2144+
$publicip = New-AzPublicIpAddress -ResourceGroupName $rgname -name $publicIpName -location $location -AllocationMethod Static -Sku Standard
2145+
2146+
# Create AzureFirewall
2147+
$azureFirewall = New-AzFirewall -Name $azureFirewallName -ResourceGroupName $rgname -Location $location
2148+
2149+
# Verify
2150+
$azFirewall = Get-AzFirewall -Name $azureFirewallName -ResourceGroupName $rgname
2151+
2152+
# Create a filter rules
2153+
$filter1 = New-AzFirewallPacketCaptureRule -Source "10.0.0.2","192.123.12.1" -Destination "172.32.1.2" -DestinationPort "80","443"
2154+
$filter2 = New-AzFirewallPacketCaptureRule -Source "10.0.0.5" -Destination "172.20.10.2" -DestinationPort "80","443"
2155+
2156+
# Create the firewall packet capture parameters
2157+
$Params = New-AzFirewallPacketCaptureParameter -DurationInSeconds 300 -NumberOfPackets 5000 -SASUrl "ValidSasUrl" -Filename "AzFwPacketCapture" -Flag "Syn","Ack" -Protocol "Any" -Filter $Filter1, $Filter2
2158+
2159+
# Invoke a firewall packet capture
2160+
Invoke-AzFirewallPacketCapture -AzureFirewall $azureFirewall -Parameter $Params
2161+
}
2162+
finally {
2163+
# Cleanup
2164+
Clean-ResourceGroup $rgname
2165+
}
21222166
}

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

Lines changed: 1430 additions & 0 deletions
Large diffs are not rendered by default.

src/Network/Network/Az.Network.psd1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,10 @@ CmdletsToExport = 'Add-AzApplicationGatewayAuthenticationCertificate',
646646
'Remove-AzNetworkManagerManagementGroupConnection',
647647
'Set-AzNetworkManagerManagementGroupConnection',
648648
'Get-AzFirewallLearnedIpPrefix', 'New-AzFirewallPolicySnat',
649-
'New-AzGatewayCustomBgpIpConfigurationObject'
649+
'New-AzGatewayCustomBgpIpConfigurationObject',
650+
'New-AzFirewallPacketCaptureRule',
651+
'New-AzFirewallPacketCaptureParameter',
652+
'Invoke-AzFirewallPacketCapture'
650653

651654
# Variables to export from this module
652655
# VariablesToExport = @()

src/Network/Network/AzureFirewall/AzureFirewallBaseCmdlet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
using Microsoft.Azure.Commands.Network.Models;
1818
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
1919
using Microsoft.Azure.Management.Network;
20-
using Microsoft.Azure.Management.Network.Models;
20+
using MNM = Microsoft.Azure.Management.Network.Models;
2121
using Newtonsoft.Json;
2222

2323
namespace Microsoft.Azure.Commands.Network
@@ -94,7 +94,7 @@ public PSAzureFirewall GetAzureFirewall(string resourceGroupName, string name)
9494
return psAzureFirewall;
9595
}
9696

97-
public PSAzureFirewall ToPsAzureFirewall(AzureFirewall firewall)
97+
public PSAzureFirewall ToPsAzureFirewall(MNM.AzureFirewall firewall)
9898
{
9999
var azureFirewall = NetworkResourceManagerProfile.Mapper.Map<PSAzureFirewall>(firewall);
100100

src/Network/Network/AzureFirewall/GetAzureFirewallCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
using Microsoft.Azure.Commands.Network.Models;
1919
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
2020
using Microsoft.Azure.Management.Network;
21-
using Microsoft.Azure.Management.Network.Models;
21+
using MNM = Microsoft.Azure.Management.Network.Models;
2222
using Microsoft.Rest.Azure;
2323

2424
namespace Microsoft.Azure.Commands.Network
@@ -55,12 +55,12 @@ public override void ExecuteCmdlet()
5555
}
5656
else
5757
{
58-
IPage<AzureFirewall> azureFirewallPage = ShouldListBySubscription(ResourceGroupName, Name)
58+
IPage<MNM.AzureFirewall> azureFirewallPage = ShouldListBySubscription(ResourceGroupName, Name)
5959
? this.AzureFirewallClient.ListAll()
6060
: this.AzureFirewallClient.List(this.ResourceGroupName);
6161

6262
// Get all resources by polling on next page link
63-
var azureFirewallResponseList = ListNextLink<AzureFirewall>.GetAllResourcesByPollingNextLink(azureFirewallPage, this.AzureFirewallClient.ListNext);
63+
var azureFirewallResponseList = ListNextLink<MNM.AzureFirewall>.GetAllResourcesByPollingNextLink(azureFirewallPage, this.AzureFirewallClient.ListNext);
6464

6565
var psAzureFirewalls = azureFirewallResponseList.Select(firewall =>
6666
{
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
using System;
16+
using System.Linq;
17+
using System.Management.Automation;
18+
using Microsoft.Azure.Commands.Network.Models;
19+
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
20+
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
21+
using Microsoft.Azure.Management.Network;
22+
using Microsoft.Azure.Management.Network.Models;
23+
using Microsoft.Rest.Azure;
24+
using MNM = Microsoft.Azure.Management.Network.Models;
25+
26+
namespace Microsoft.Azure.Commands.Network
27+
{
28+
[Cmdlet("Invoke", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "FirewallPacketCapture", SupportsShouldProcess = true), OutputType(typeof(PSAzureFirewallPacketCaptureParameters))]
29+
public class InvokeAzureFirewallPacketCaptureCommand : AzureFirewallBaseCmdlet
30+
{
31+
[Parameter(
32+
Mandatory = true,
33+
ValueFromPipeline = true,
34+
HelpMessage = "The AzureFirewall")]
35+
public PSAzureFirewall AzureFirewall { get; set; }
36+
37+
[Parameter(
38+
Mandatory = true,
39+
ValueFromPipeline = true,
40+
HelpMessage = "The packet capture parameters")]
41+
public PSAzureFirewallPacketCaptureParameters Parameter { get; set; }
42+
43+
44+
[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")]
45+
public SwitchParameter AsJob { get; set; }
46+
47+
public override void Execute()
48+
{
49+
base.Execute();
50+
51+
if (!this.IsAzureFirewallPresent(this.AzureFirewall.ResourceGroupName, this.AzureFirewall.Name))
52+
{
53+
throw new ArgumentException(Microsoft.Azure.Commands.Network.Properties.Resources.ResourceNotFound);
54+
}
55+
56+
// Map to the sdk object
57+
var secureGwParamsModel = NetworkResourceManagerProfile.Mapper.Map<MNM.FirewallPacketCaptureParameters>(this.Parameter);
58+
59+
60+
// Execute the PUT AzureFirewall call
61+
var headers = this.AzureFirewallClient.PacketCaptureAsync(this.AzureFirewall.ResourceGroupName, this.AzureFirewall.Name, secureGwParamsModel);
62+
63+
WriteObject(headers);
64+
}
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using Microsoft.Azure.Commands.Network.Models;
2+
using System;
3+
using System.Collections;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Management.Automation;
7+
using System.Text;
8+
using MNM = Microsoft.Azure.Management.Network.Models;
9+
10+
11+
namespace Microsoft.Azure.Commands.Network.AzureFirewall.PacketCapture
12+
{
13+
[Cmdlet(VerbsCommon.New, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "FirewallPacketCaptureParameter", SupportsShouldProcess = true), OutputType(typeof(PSAzureFirewallPacketCaptureParameters))]
14+
public class NewAzureFirewallPacketCaptureParametersCommand : NetworkBaseCmdlet
15+
{
16+
[Parameter(
17+
Mandatory = true,
18+
HelpMessage = "The intended durations of packet capture in seconds")]
19+
[ValidateRange(30,1800)]
20+
public uint DurationInSeconds { get; set; }
21+
22+
[Parameter(
23+
Mandatory = true,
24+
HelpMessage = "The intended number of packets to capture")]
25+
[ValidateRange(100,90000)]
26+
public uint NumberOfPacketsToCapture { get; set; }
27+
28+
[Parameter(
29+
Mandatory = true,
30+
HelpMessage = "Upload capture storage container SASURL with write and delete permissions")]
31+
[ValidateNotNullOrEmpty]
32+
public virtual string SasUrl { get; set; }
33+
34+
[Parameter(
35+
Mandatory = true,
36+
HelpMessage = "Name of packet capture file")]
37+
[ValidateNotNullOrEmpty]
38+
public virtual string FileName { get; set; }
39+
40+
[Parameter(
41+
Mandatory = false,
42+
HelpMessage = "The Protocols to capture")]
43+
[ValidateSet(
44+
MNM.AzureFirewallNetworkRuleProtocol.Any,
45+
MNM.AzureFirewallNetworkRuleProtocol.TCP,
46+
MNM.AzureFirewallNetworkRuleProtocol.UDP,
47+
MNM.AzureFirewallNetworkRuleProtocol.Icmp,
48+
IgnoreCase = false)]
49+
public string Protocol { get; set; }
50+
51+
[Parameter(
52+
Mandatory = false,
53+
HelpMessage = "The list of tcp-flags to capture")]
54+
public string[] Flag { get; set; }
55+
56+
[Parameter(
57+
Mandatory = true,
58+
HelpMessage = "The list of filters to capture")]
59+
[ValidateNotNullOrEmpty]
60+
public PSAzureFirewallPacketCaptureRule[] Filter { get; set; }
61+
62+
public override void Execute()
63+
{
64+
base.Execute();
65+
66+
List<PSAzureFirewallPacketCaptureFlags> PSFlags = new List<PSAzureFirewallPacketCaptureFlags>();
67+
68+
if(Flag != null)
69+
{
70+
foreach (var flag in Flag)
71+
{
72+
PSFlags.Add(PSAzureFirewallPacketCaptureFlags.MapUserInputToPacketCaptureFlag(flag));
73+
}
74+
}
75+
76+
var packetCaptureParameters = new PSAzureFirewallPacketCaptureParameters
77+
{
78+
DurationInSeconds = this.DurationInSeconds,
79+
NumberOfPacketsToCapture = this.NumberOfPacketsToCapture,
80+
SasUrl = this.SasUrl,
81+
FileName = this.FileName,
82+
Protocol = this.Protocol,
83+
Flags = PSFlags,
84+
Filters = this.Filter?.ToList(),
85+
86+
};
87+
88+
WriteObject(packetCaptureParameters);
89+
}
90+
}
91+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
using System;
16+
using System.Collections.Generic;
17+
using System.Linq;
18+
using System.Management.Automation;
19+
using Microsoft.Azure.Commands.Network.Models;
20+
using System.Text.RegularExpressions;
21+
using MNM = Microsoft.Azure.Management.Network.Models;
22+
23+
namespace Microsoft.Azure.Commands.Network.AzureFirewall.PacketCaptureRule
24+
{
25+
[Cmdlet(VerbsCommon.New, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "FirewallPacketCaptureRule", SupportsShouldProcess = true), OutputType(typeof(PSAzureFirewallPacketCaptureRule))]
26+
public class NewAzureFirewallPacketCaptureRuleCommand : AzureFirewallBaseCmdlet
27+
{
28+
[Parameter(
29+
Mandatory = true,
30+
HelpMessage = "The source addresses of the rule")]
31+
[ValidateNotNullOrEmpty]
32+
public string[] Source { get; set; }
33+
34+
[Parameter(
35+
Mandatory = true,
36+
HelpMessage = "The destination addresses of the rule")]
37+
[ValidateNotNullOrEmpty]
38+
public string[] Destination { get; set; }
39+
40+
[Parameter(
41+
Mandatory = false,
42+
HelpMessage = "The destination ports of the rule")]
43+
public string[] DestinationPort { get; set; }
44+
45+
public override void Execute()
46+
{
47+
base.Execute();
48+
49+
// Sources and destinations must be specified
50+
if ((Source == null) || (Destination == null))
51+
{
52+
throw new ArgumentException("Both Sources and Destinations must be specified.");
53+
}
54+
55+
var filterRule = new PSAzureFirewallPacketCaptureRule
56+
{
57+
Sources = Source.ToList(),
58+
Destinations = Destination.ToList(),
59+
DestinationPorts = DestinationPort?.ToList()
60+
};
61+
WriteObject(filterRule);
62+
}
63+
}
64+
}

src/Network/Network/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* Added support of `InternetIngressIp` Property in New-AzNetworkVirtualAppliance
2828
* Added the new cmdlet for supporting `InternetIngressIp` Property with Network Virtual Appliances -`New-AzVirtualApplianceInternetIngressIpsProperty`
2929
* Added a new AuxiliaryMode value `AuxiliaryMode.Floating`
30+
* Added support for AzureFirewallPacketCapture
3031

3132
## Version 7.1.0
3233
* Added DefaultOutboundAccess parameter on subnet creation

0 commit comments

Comments
 (0)