Skip to content

[Az.Network] Updated New-AzNetworkWatcherPacketCaptureV2 command and added a new sub command 'New-AzPacketCaptureSettingsConfig' for Network watcher Packet capture include ring buffer change. #27880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions src/Accounts/Accounts/Utilities/CommandMappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5158,6 +5158,7 @@
"New-AzP2sVpnGateway": {},
"New-AzPacketCaptureFilterConfig": {},
"New-AzPacketCaptureScopeConfig": {},
"New-AzPacketCaptureSettingsConfig": {},
"New-AzPrivateDnsZoneConfig": {},
"New-AzPrivateDnsZoneGroup": {},
"New-AzPrivateEndpoint": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ public void TestPacketCaptureVMSS()
TestRunner.RunTestScript("Test-PacketCaptureV2");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
[Trait(Category.Owner, NrpTeamAlias.netanalyticsdev)]
public void TestPacketCaptureVMSSWithRingBuffer()
{
TestRunner.RunTestScript("Test-PacketCaptureV2WithRingBuffer");
}

[Fact]
[Trait(Category.AcceptanceType, Category.LiveOnly)]
[Trait(Category.Owner, NrpTeamAlias.netanalyticsdev)]
Expand Down
97 changes: 97 additions & 0 deletions src/Network/Network.Test/ScenarioTests/NetworkWatcherAPITests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,103 @@ function Test-PacketCaptureV2 {
}
}

<#
.SYNOPSIS
Test PacketCapture API with ring buffer.
#>
function Test-PacketCaptureV2WithRingBuffer {
# Setup
$resourceGroupName = Get-NrpResourceGroupName
$virtualMachineScaleSetName = Get-NrpResourceName
$nwName = Get-NrpResourceName
$location = Get-PilotLocation
$resourceTypeParent = "Microsoft.Network/networkWatchers"
$nwLocation = Get-ProviderLocation $resourceTypeParent
$nwRgName = Get-NrpResourceGroupName
$templateFileVMSS = (Resolve-Path ".\TestData\DeploymentVMSS.json").Path
$pcName = Get-NrpResourceName
$pcName3 = $pcName + "2"

try {
. ".\AzureRM.Resources.ps1"

# Create Resource group
New-AzResourceGroup -Name $resourceGroupName -Location "$location"

# Deploy resources
Get-TestResourcesDeploymentVMSS -rgn "$resourceGroupName"

#Get public IP address
$address = Get-AzPublicIpAddress -ResourceGroupName $resourceGroupName

# Create Resource group for Network Watcher
New-AzResourceGroup -Name $nwRgName -Location "$location"

# Get Network Watcher
$nw = Get-CreateTestNetworkWatcher -location $location -nwName $nwName -nwRgName $nwRgName

Wait-Seconds 600

#Get Vmss and Instances
$vmss = Get-AzVmss -ResourceGroupName $resourceGroupName -VMScaleSetName $virtualMachineScaleSetName

#Install networkWatcherAgent on Vmss and Vmss Instances
Add-AzVmssExtension -VirtualMachineScaleSet $vmss -Name "AzureNetworkWatcherExtension" -Publisher "Microsoft.Azure.NetworkWatcher" -Type "NetworkWatcherAgentWindows" -TypeHandlerVersion "1.4" -AutoUpgradeMinorVersion $True
Update-AzVmss -ResourceGroupName "$resourceGroupName" -Name $virtualMachineScaleSetName -VirtualMachineScaleSet $vmss

# Updating all VMSS instances with NW agent
$instances = Get-AzVMSSVM -ResourceGroupName "$resourceGroupName" -VMScaleSetName $vmss.Name
foreach ($item in $instances) {
Update-AzVmssInstance -ResourceGroupName "$resourceGroupName" -VMScaleSetName $vmss.Name -InstanceId $item.InstanceID
}

# Create Capture settiings for packet capture, its only applicable if we are pass continuousCapture as true/false
$c1 = New-AzPacketCaptureSettingsConfig -FileCount 2 -FileSizeInBytes 102400 -SessionTimeLimitInSeconds 60

#$vmssid = $vmss.Id
#Write-Output ("VmssId: '$vmssid'")
#Write-Output ("pcname: '$pcName3' ")
#Write-Output ("C1: '$c1' ")

#Create packet capture
# with Continuous Capture, if you are using continuousCapture, change it to local Path instead FilePath
$job3 = New-AzNetworkWatcherPacketCaptureV2 -NetworkWatcher $nw -Name $pcName3 -TargetId $vmss.Id -TargetType "azurevmss" -ContinuousCapture $false -CaptureSettings $c1 -LocalPath C:\captures\Capture.cap -AsJob
$job3 | Wait-Job

Start-TestSleep -Seconds 2

#Get packet capture
$job3 = Get-AzNetworkWatcherPacketCapture -NetworkWatcher $nw -PacketCaptureName $pcName3 -AsJob
$job3 | Wait-Job
$pc3 = $job3 | Receive-Job

#Write-Output ("PC3: '$pc3'")

#Verification
Assert-AreEqual $pc3.Name $pcName3
Assert-AreEqual "Succeeded" $pc3.ProvisioningState
Assert-AreEqual 2 $c1.FileCount
Assert-AreEqual 102400 $c1.FileSizeInBytes
Assert-AreEqual 60 $c1.SessionTimeLimitInSeconds
Assert-Null $pc3.TotalBytesPerSession
Assert-Null $pc3.TimeLimitInSeconds
Assert-AreEqual $pc3.TargetType AzureVMSS

#Stop packet capture
$job3 = Stop-AzNetworkWatcherPacketCapture -NetworkWatcher $nw -PacketCaptureName $pcName3 -AsJob
$job3 | Wait-Job

#Remove packet capture
$job3 = Remove-AzNetworkWatcherPacketCapture -NetworkWatcher $nw -PacketCaptureName $pcName3 -AsJob
$job3 | Wait-Job
}
finally {
# Cleanup
# Clean-ResourceGroup $resourceGroupName
#Clean-ResourceGroup $nwRgName
}
}

<#
.SYNOPSIS
Test Troubleshoot API.
Expand Down

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/Network/Network/Az.Network.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ CmdletsToExport = 'Add-AzApplicationGatewayAuthenticationCertificate',
'New-AzNetworkWatcherPacketCaptureV2',
'New-AzNetworkWatcherProtocolConfiguration',
'New-AzO365PolicyProperty', 'New-AzOffice365PolicyProperty',
'New-AzP2sVpnGateway', 'New-AzPacketCaptureFilterConfig',
'New-AzP2sVpnGateway', 'New-AzPacketCaptureFilterConfig',
'New-AzPacketCaptureSettingsConfig',
'New-AzPacketCaptureScopeConfig', 'New-AzPrivateDnsZoneConfig',
'New-AzPrivateDnsZoneGroup', 'New-AzPrivateEndpoint',
'New-AzPrivateEndpointIpConfiguration', 'New-AzPrivateLinkService',
Expand Down
10 changes: 8 additions & 2 deletions src/Network/Network/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@
--->

## Upcoming Release
* Added a new command which creates an object for CaptureSettings, and added properties 'FileCount', 'FileSizeInBytes', and 'SessionTimeLimitInSeconds', which helps to configure the capture settings for packet capture as well as support for it for the following cmdlets:
- `New-AzPacketCaptureSettingsConfig`

* Added properties 'ContinuousCapture', 'LocalPath', and 'CaptureSettings' reference in Packet capture V2 command, as well as support for it for the following cmdlets:
- `New-AzNetworkWatcherPacketCaptureV2`

* Onboarded Application Gateway WAF Exceptions cmdlet.
- `New-AzApplicationGatewayFirewallPolicyException`
- `New-AzApplicationGatewayFirewallPolicyException`

## Version 7.17.0
* Added properties 'PublicIpAddressesV6', 'PublicIpPrefixesV6', and 'SourceVirtualNetwork' to NatGateway, as well as support for it for the following cmdlets:
Expand Down Expand Up @@ -1443,4 +1449,4 @@
- New-AzApplicationGatewaySslCertificate
- Set-AzApplicationGatewaySslCertificate
- New-AzApplicationGateway cmdlet updated with optional parameter -UserAssignedIdentityId, -UserAssignedIdentity
* Add MaxCapacity property in ApplicationGatewayAutoscaleConfiguration
* Add MaxCapacity property in ApplicationGatewayAutoscaleConfiguration
46 changes: 46 additions & 0 deletions src/Network/Network/Models/PSPacketCaptureSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.Commands.Network.Models
{
using Microsoft.WindowsAzure.Commands.Common.Attributes;
using Newtonsoft.Json;

public class PSPacketCaptureSettings
{
/// <summary>
/// Gets or sets number of file count. Default value of count is 10 and maximum
/// number is 10000.
/// </summary>
[JsonProperty(Order = 1)]
[Ps1Xml(Target = ViewControl.Table)]
public int? FileCount { get; set; }

/// <summary>
/// Gets or sets number of bytes captured per packet. Default value in bytes
/// 104857600 (100MB) and maximum in bytes 4294967295 (4GB).
/// </summary>
[JsonProperty(Order = 2)]
[Ps1Xml(Target = ViewControl.Table)]
public long? FileSizeInBytes { get; set; }

/// <summary>
/// Gets or sets maximum duration of the capture session in seconds is 604800s
/// (7 days) for a file. Default value in second 86400s (1 day).
/// </summary>
[JsonProperty(Order = 3)]
[Ps1Xml(Target = ViewControl.Table)]
public int? SessionTimeLimitInSeconds { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,17 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using AutoMapper;
using Microsoft.Azure.Commands.Network.Models;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
using Microsoft.Azure.Management.Network;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;
using MNM = Microsoft.Azure.Management.Network.Models;

namespace Microsoft.Azure.Commands.Network
{
[Cmdlet("New", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "NetworkWatcherPacketCaptureV2", SupportsShouldProcess = true, DefaultParameterSetName = "SetByResource"),OutputType(typeof(PSPacketCaptureResult))]
[Cmdlet("New", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "NetworkWatcherPacketCaptureV2", SupportsShouldProcess = true, DefaultParameterSetName = "SetByResource"), OutputType(typeof(PSPacketCaptureResult))]
public class NewAzureNetworkWatcherPacketCaptureCommandV2 : PacketCaptureBaseCmdlet
{
[Parameter(
Expand Down Expand Up @@ -94,7 +91,7 @@ public class NewAzureNetworkWatcherPacketCaptureCommandV2 : PacketCaptureBaseCmd
[Parameter(
Mandatory = false,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Local file path.")]
HelpMessage = "Local File path.")]
[ValidateNotNullOrEmpty]
public string LocalFilePath { get; set; }

Expand Down Expand Up @@ -140,6 +137,25 @@ public class NewAzureNetworkWatcherPacketCaptureCommandV2 : PacketCaptureBaseCmd
[ValidateNotNull]
public PSPacketCaptureFilter[] Filter { get; set; }

[Parameter(
Mandatory = false,
ValueFromPipelineByPropertyName = true,
HelpMessage = "This continuous capture is a nullable boolean, which can hold 'null', 'true' or 'false' value. If we do not pass this parameter, it would be consider as 'null', default value is 'null'.")]
public bool? ContinuousCapture { get; set; }

[Parameter(
Mandatory = false,
ValueFromPipelineByPropertyName = true,
HelpMessage = "This path is valid if 'ContinuousCapture' is provided and required if no storage ID is provided, otherwise optional. Must include the name of the capture file (*.cap).")]
[ValidateNotNullOrEmpty]
public string LocalPath { get; set; }

[Parameter(
Mandatory = false,
HelpMessage = "Filters for packet capture session.")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update the description from swagger repo. Also mentions, only required when continous capture field is being set.

[ValidateNotNull]
public PSPacketCaptureSettings CaptureSettings { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")]
public SwitchParameter AsJob { get; set; }

Expand Down Expand Up @@ -172,6 +188,68 @@ public override void Execute()
name = this.NetworkWatcherName;
}

#region Capture Settings Validations
if (this.ContinuousCapture == null)
{
if (string.IsNullOrEmpty(this.LocalFilePath) && string.IsNullOrEmpty(this.StorageAccountId))
{
throw new ArgumentException("PacketCaptureIsMissingStorageIdAndLocalFilePath: StorageLocation must have either storage id or local file path specified.");
}
}
else
{
if (this.TotalBytesPerSession != null)
{
throw new ArgumentException("InvalidRequestPropertiesInPacketCaptureRequest: TotalBytesPerSession is not supported in packet capture request.");
}

if (this.TimeLimitInSecond != null)
{
throw new ArgumentException("InvalidRequestPropertiesInPacketCaptureRequest: TimeLimitInSecond is not supported in packet capture request.");
}

if (this.LocalFilePath != null)
{
throw new ArgumentException("PacketCaptureIsMissingStorageIdAndLocalPath: StorageLocation must have either storage id or local path specified.");
}

if (string.IsNullOrEmpty(this.LocalPath) && string.IsNullOrEmpty(this.StorageAccountId))
{
throw new ArgumentException("PacketCaptureIsMissingStorageIdAndLocalFilePath: StorageLocation must have either storage id or local file path specified.");
}

if (this.CaptureSettings == null)
{
this.CaptureSettings = new PSPacketCaptureSettings
{
FileCount = 10,
FileSizeInBytes = 104857600,
SessionTimeLimitInSeconds = 86400
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to pass these default values ?
What if we don't pass captureSettings as well, this should be set at the server side, right ?

}
else
{
this.CaptureSettings.FileCount = this.CaptureSettings.FileCount ?? 10;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to set default value here, it should be taken care by server only, lets test this behavior once.
applicable to other fields as well.

this.CaptureSettings.FileSizeInBytes = this.CaptureSettings.FileSizeInBytes ?? 104857600;
this.CaptureSettings.SessionTimeLimitInSeconds = this.CaptureSettings.SessionTimeLimitInSeconds ?? 86400;

if (this.CaptureSettings.FileCount < 1 || this.CaptureSettings.FileCount > 10000)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - lets do the validation before assignment,

{
throw new ArgumentException("FileCount must be between 1 and 10,000. Default is 10.");
}
if (this.CaptureSettings.FileSizeInBytes < 102400 || this.CaptureSettings.FileSizeInBytes > 4294967295)
{
throw new ArgumentException("FileSizeInBytes must be between 102400 byte and 4,294,967,295 bytes (4 GB). Default is 104,857,600 bytes (100 MB).");
}
if (this.CaptureSettings.SessionTimeLimitInSeconds < 1 || this.CaptureSettings.SessionTimeLimitInSeconds > 604800)
{
throw new ArgumentException("SessionTimeLimitInSeconds must be between 1 second and 604,800 seconds (7 days). Default is 86,400 seconds.");
}
}
}

#endregion

var present = this.IsPacketCapturePresent(resourceGroupName, name, this.Name);

if (!present)
Expand All @@ -185,13 +263,17 @@ public override void Execute()
WriteObject(packetCapture);
});
}
else
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets add this scenario in existing test-case or we can write a new one.

{
throw new ArgumentException($"PacketCaptureExistingAlready: Existing Packet capture can not be updated.");
}
}

private PSPacketCaptureResult CreatePacketCapture(string resourceGroupName, string networkWatcherName)
{
MNM.PacketCapture packetCaptureProperties = new MNM.PacketCapture();

if(this.BytesToCapturePerPacket != null)
if (this.BytesToCapturePerPacket != null)
{
packetCaptureProperties.BytesToCapturePerPacket = this.BytesToCapturePerPacket;
}
Expand All @@ -209,10 +291,25 @@ private PSPacketCaptureResult CreatePacketCapture(string resourceGroupName, stri
packetCaptureProperties.Target = this.TargetId;

packetCaptureProperties.StorageLocation = new MNM.PacketCaptureStorageLocation();
packetCaptureProperties.StorageLocation.FilePath = this.LocalFilePath;
packetCaptureProperties.ContinuousCapture = this.ContinuousCapture;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continousCapture only needs to be set when it's provided ?
do we need null check here ?


if (this.ContinuousCapture != null)
{
packetCaptureProperties.StorageLocation.LocalPath = this.LocalPath;
packetCaptureProperties.CaptureSettings = new MNM.PacketCaptureSettings()
{
FileCount = this.CaptureSettings.FileCount,
FileSizeInBytes = this.CaptureSettings.FileSizeInBytes,
SessionTimeLimitInSeconds = this.CaptureSettings.SessionTimeLimitInSeconds
};
}
else
{
packetCaptureProperties.StorageLocation.FilePath = this.LocalFilePath;
}

packetCaptureProperties.StorageLocation.StorageId = this.StorageAccountId;
packetCaptureProperties.StorageLocation.StoragePath = this.StoragePath;

packetCaptureProperties.TargetType = MNM.PacketCaptureTargetType.AzureVM;

if (!string.IsNullOrEmpty(this.TargetType))
Expand Down
Loading