Skip to content

Commit 9bc7905

Browse files
authored
Support Rate Limit Rule For Application Gateway WAF Custom Rule (#21557)
* try * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 2 * 2 * 2 * 1 * comment * 2 * 2 * 2 * 2 * name * 2 * test * 2 * 1 * 1 * 1 * 2 * 1 * 1 * 1 * 1
1 parent fb17222 commit 9bc7905

22 files changed

+1858
-207
lines changed

src/Network/Network.Test/Network.Test.csproj

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<PackageReference Include="Microsoft.Azure.Management.OperationalInsights" Version="0.25.0-preview" />
2424
<PackageReference Include="Microsoft.Azure.Management.ManagedServiceIdentity" Version="0.10.0-preview" />
2525
<PackageReference Include="Microsoft.Azure.Management.Storage" Version="25.0.0" />
26+
<PackageReference Include="Microsoft.Azure.Management.Network" Version="26.0.0" />
2627
</ItemGroup>
2728

2829
<ItemGroup>
@@ -49,9 +50,6 @@
4950
<ItemGroup>
5051
<Folder Include="SessionRecords\Commands.Network.Test.ScenarioTests.NetworkManagerTests\" />
5152
</ItemGroup>
52-
53-
<ItemGroup>
54-
<ProjectReference Include="..\Network.Management.Sdk\Network.Management.Sdk.csproj" />
55-
</ItemGroup>
5653

5754
</Project>
55+

src/Network/Network.Test/ScenarioTests/ApplicationGatewayTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,13 @@ public void TestApplicationGatewayFirewallPolicyWithCustomRules()
237237
TestRunner.RunTestScript("Test-ApplicationGatewayFirewallPolicyWithCustomRules");
238238
}
239239

240+
[Fact]
241+
[Trait(Category.AcceptanceType, Category.CheckIn)]
242+
[Trait(Category.Owner, NrpTeamAlias.nvadev_subset1)]
243+
public void TestApplicationGatewayFirewallPolicyWithRateLimitRule()
244+
{
245+
TestRunner.RunTestScript("Test-ApplicationGatewayFirewallPolicyWithRateLimitRule");
246+
}
240247

241248
[Fact]
242249
[Trait(Category.AcceptanceType, Category.CheckIn)]

src/Network/Network.Test/ScenarioTests/ApplicationGatewayTests.ps1

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4245,6 +4245,62 @@ function Test-ApplicationGatewayFirewallPolicyWithCustomRules
42454245
}
42464246
}
42474247

4248+
function Test-ApplicationGatewayFirewallPolicyWithRateLimitRule
4249+
{
4250+
# Setup
4251+
$location = Get-ProviderLocation "Microsoft.Network/applicationGateways" "West US 2"
4252+
$rgname = Get-ResourceGroupName
4253+
$wafPolicyName = "wafPolicy1"
4254+
4255+
try {
4256+
4257+
$resourceGroup = New-AzResourceGroup -Name $rgname -Location $location -Tags @{ testtag = "APPGw tag"}
4258+
4259+
# WAF Policy with rate limiting rule custom Rule
4260+
$variable = New-AzApplicationGatewayFirewallMatchVariable -VariableName RequestHeaders -Selector Malicious-Header
4261+
$condition = New-AzApplicationGatewayFirewallCondition -MatchVariable $variable -Operator Any -NegationCondition $False
4262+
$groupbyVar = New-AzApplicationGatewayFirewallCustomRuleGroupByVariable -VariableName ClientAddr
4263+
$groupbyUserSes = New-AzApplicationGatewayFirewallCustomRuleGroupByUserSession -GroupByVariable $groupbyVar
4264+
$customRule = New-AzApplicationGatewayFirewallCustomRule -Name example -Priority 2 -RateLimitDuration OneMin -RateLimitThreshold 10 -RuleType RateLimitRule -MatchCondition $condition -GroupByUserSession $groupbyUserSes -Action Block
4265+
4266+
$policySettings = New-AzApplicationGatewayFirewallPolicySetting -Mode Prevention -State Enabled -MaxFileUploadInMb 70 -MaxRequestBodySizeInKb 70
4267+
$managedRuleSet = New-AzApplicationGatewayFirewallPolicyManagedRuleSet -RuleSetType "OWASP" -RuleSetVersion "3.2"
4268+
$managedRule = New-AzApplicationGatewayFirewallPolicyManagedRule -ManagedRuleSet $managedRuleSet
4269+
New-AzApplicationGatewayFirewallPolicy -Name $wafPolicyName -ResourceGroupName $rgname -Location $location -ManagedRule $managedRule -PolicySetting $policySettings -CustomRule $customRule
4270+
4271+
$policy = Get-AzApplicationGatewayFirewallPolicy -Name $wafPolicyName -ResourceGroupName $rgname
4272+
4273+
# Check WAF policy
4274+
Assert-AreEqual $policy.CustomRules[0].Name $customRule.Name
4275+
Assert-AreEqual $policy.CustomRules[0].RuleType $customRule.RuleType
4276+
Assert-AreEqual $policy.CustomRules[0].Action $customRule.Action
4277+
Assert-AreEqual $policy.CustomRules[0].Priority $customRule.Priority
4278+
Assert-AreEqual $policy.CustomRules[0].RateLimitDuration $customRule.RateLimitDuration
4279+
Assert-AreEqual $policy.CustomRules[0].RateLimitThreshold $customRule.RateLimitThreshold
4280+
Assert-AreEqual $policy.CustomRules[0].State "Enabled"
4281+
Assert-AreEqual $policy.CustomRules[0].MatchConditions[0].OperatorProperty $customRule.MatchConditions[0].OperatorProperty
4282+
Assert-AreEqual $policy.CustomRules[0].MatchConditions[0].NegationConditon $customRule.MatchConditions[0].NegationConditon
4283+
Assert-AreEqual $policy.CustomRules[0].MatchConditions[0].MatchVariables[0].VariableName $customRule.MatchConditions[0].MatchVariables[0].VariableName
4284+
Assert-AreEqual $policy.CustomRules[0].MatchConditions[0].MatchVariables[0].Selector $customRule.MatchConditions[0].MatchVariables[0].Selector
4285+
Assert-AreEqual $policy.CustomRules[0].GroupByUserSession[0].GroupByVariables[0].VariableName $customRule.GroupByUserSession[0].GroupByVariables[0].VariableName
4286+
Assert-AreEqual $policy.PolicySettings.FileUploadLimitInMb $policySettings.FileUploadLimitInMb
4287+
Assert-AreEqual $policy.PolicySettings.MaxRequestBodySizeInKb $policySettings.MaxRequestBodySizeInKb
4288+
Assert-AreEqual $policy.PolicySettings.RequestBodyCheck $policySettings.RequestBodyCheck
4289+
Assert-AreEqual $policy.PolicySettings.Mode $policySettings.Mode
4290+
Assert-AreEqual $policy.PolicySettings.State $policySettings.State
4291+
4292+
$policy.CustomRules[0].State = "Disabled"
4293+
Set-AzApplicationGatewayFirewallPolicy -InputObject $policy
4294+
$policy1 = Get-AzApplicationGatewayFirewallPolicy -Name $wafPolicyName -ResourceGroupName $rgname
4295+
Assert-AreEqual $policy1.CustomRules[0].State "Disabled"
4296+
}
4297+
finally
4298+
{
4299+
# Cleanup
4300+
Clean-ResourceGroup $rgname
4301+
}
4302+
}
4303+
42484304
function Test-ApplicationGatewayFirewallPolicyWithUppercaseTransform
42494305
{
42504306
# Setup

src/Network/Network.Test/SessionRecords/Commands.Network.Test.ScenarioTests.ApplicationGatewayTests/TestApplicationGatewayFirewallPolicyWithCustomRules.json

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

src/Network/Network.Test/SessionRecords/Commands.Network.Test.ScenarioTests.ApplicationGatewayTests/TestApplicationGatewayFirewallPolicyWithRateLimitRule.json

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

src/Network/Network/Az.Network.psd1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ CmdletsToExport = 'Add-AzApplicationGatewayAuthenticationCertificate',
111111
'New-AzApplicationGatewayFirewallPolicyExclusionManagedRuleSet',
112112
'New-AzApplicationGatewayFirewallCondition',
113113
'New-AzApplicationGatewayFirewallCustomRule',
114+
'New-AzApplicationGatewayFirewallCustomRuleGroupByUserSession',
115+
'New-AzApplicationGatewayFirewallCustomRuleGroupByVariable',
114116
'New-AzApplicationGatewayFirewallMatchVariable',
115117
'New-AzApplicationGatewayFirewallPolicy',
116118
'Get-AzApplicationGatewayFirewallPolicy',

src/Network/Network/ChangeLog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
-`Remove-AzRouteMap`
3131
* Update cmdlets to add inbound/outbound route maps in routingConfiguration
3232
-`New-AzRoutingConfiguration`
33+
* Added new cmdlets to support Rate Limiting Rule for Application Gateway WAF
34+
- 'New-AzApplicationGatewayFirewallCustomRuleGroupByUserSession',
35+
- 'New-AzApplicationGatewayFirewallCustomRuleGroupByVariable',
36+
- Also updated cmdlet to add the property of RateLimitDuration, RateLimitThreshold and GroupByUserSession
37+
- `New-AzureApplicationGatewayFirewallCustomRule`
3338

3439
## Version 5.6.0
3540
* Updated `New-AzLoadBalancer` and `Set-AzLoadBalancer` to validate surface level parameters for global tier load balancers
@@ -45,6 +50,7 @@
4550
* Fixed bugs related to auto learn IP prefixes and Snat
4651
* Updated multi-auth to be supported when both OpenVPN and IkeV2 protocols are used for VNG and VWAN VPN
4752

53+
4854
## Version 5.5.0
4955
* Updated cmdlets to add new property of `Snat` in Azure Firewall Policy.
5056
- `New-AzFirewallPolicySnat`

src/Network/Network/Common/NetworkResourceManagerProfile.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,8 @@ private static void Initialize()
10181018
cfg.CreateMap<CNM.PSApplicationGatewayWebApplicationFirewallConfiguration, MNM.ApplicationGatewayWebApplicationFirewallConfiguration>();
10191019
cfg.CreateMap<CNM.PSApplicationGatewayFirewallCondition, MNM.MatchCondition>();
10201020
cfg.CreateMap<CNM.PSApplicationGatewayFirewallCustomRule, MNM.WebApplicationFirewallCustomRule>();
1021+
cfg.CreateMap<CNM.PSApplicationGatewayFirewallCustomRuleGroupByUserSession, MNM.GroupByUserSession>();
1022+
cfg.CreateMap<CNM.PSApplicationGatewayFirewallCustomRuleGroupByVariable, MNM.GroupByVariable>();
10211023
cfg.CreateMap<CNM.PSApplicationGatewayFirewallMatchVariable, MNM.MatchVariable>();
10221024
cfg.CreateMap<CNM.PSApplicationGatewayWebApplicationFirewallPolicy, MNM.WebApplicationFirewallPolicy>();
10231025
cfg.CreateMap<CNM.PSApplicationGatewayFirewallPolicySettings, MNM.PolicySettings>();
@@ -1094,6 +1096,8 @@ private static void Initialize()
10941096
cfg.CreateMap<MNM.ApplicationGatewayWebApplicationFirewallConfiguration, CNM.PSApplicationGatewayWebApplicationFirewallConfiguration>();
10951097
cfg.CreateMap<MNM.MatchCondition, CNM.PSApplicationGatewayFirewallCondition>();
10961098
cfg.CreateMap<MNM.WebApplicationFirewallCustomRule, CNM.PSApplicationGatewayFirewallCustomRule>();
1099+
cfg.CreateMap<MNM.GroupByUserSession, CNM.PSApplicationGatewayFirewallCustomRuleGroupByUserSession>();
1100+
cfg.CreateMap<MNM.GroupByVariable, CNM.PSApplicationGatewayFirewallCustomRuleGroupByVariable>();
10971101
cfg.CreateMap<MNM.MatchVariable, CNM.PSApplicationGatewayFirewallMatchVariable>();
10981102
cfg.CreateMap<MNM.WebApplicationFirewallPolicy, CNM.PSApplicationGatewayWebApplicationFirewallPolicy>();
10991103
cfg.CreateMap<MNM.PolicySettings, CNM.PSApplicationGatewayFirewallPolicySettings>();

src/Network/Network/FirewallPolicy/FirewallCustomRule/AzureApplicationGatewayFirewallCustomRuleBase.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,23 @@ public class AzureApplicationGatewayFirewallCustomRuleBase : NetworkBaseCmdlet
3434
[ValidateNotNullOrEmpty]
3535
public int Priority { get; set; }
3636

37+
[Parameter(
38+
Mandatory = false,
39+
HelpMessage = "Duration over which Rate Limit policy will be applied. Applies only when ruleType is RateLimitRule.")]
40+
[ValidateSet("OneMin", "FiveMins", IgnoreCase = true)]
41+
[ValidateNotNullOrEmpty]
42+
public string RateLimitDuration { get; set; }
43+
44+
[Parameter(
45+
Mandatory = false,
46+
HelpMessage = "Rate Limit threshold to apply in case ruleType is RateLimitRule. Must be greater than or equal to 1")]
47+
[ValidateNotNullOrEmpty]
48+
public int RateLimitThreshold { get; set; }
49+
3750
[Parameter(
3851
Mandatory = true,
3952
HelpMessage = "Describes type of rule.")]
40-
[ValidateSet("MatchRule", IgnoreCase = true)]
53+
[ValidateSet("MatchRule", "RateLimitRule", IgnoreCase = true)]
4154
[ValidateNotNullOrEmpty]
4255
public string RuleType { get; set; }
4356

@@ -47,6 +60,13 @@ public class AzureApplicationGatewayFirewallCustomRuleBase : NetworkBaseCmdlet
4760
[ValidateNotNullOrEmpty]
4861
public PSApplicationGatewayFirewallCondition[] MatchCondition { get; set; }
4962

63+
[Parameter(
64+
Mandatory = false,
65+
HelpMessage = "Define user session identifier group by clauses.")]
66+
[ValidateCount(1, 1)]
67+
[ValidateNotNullOrEmpty]
68+
public PSApplicationGatewayFirewallCustomRuleGroupByUserSession[] GroupByUserSession { get; set; }
69+
5070
[Parameter(
5171
Mandatory = true,
5272
HelpMessage = "Type of Actions.")]
@@ -77,7 +97,10 @@ protected PSApplicationGatewayFirewallCustomRule NewObject()
7797
Name = this.Name,
7898
Priority = this.Priority,
7999
RuleType = this.RuleType,
100+
RateLimitDuration = this.RateLimitDuration,
101+
RateLimitThreshold = this.RateLimitThreshold,
80102
MatchConditions = this.MatchCondition?.ToList(),
103+
GroupByUserSession = this.GroupByUserSession?.ToList(),
81104
Action = this.Action,
82105
State = this.State
83106
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 Microsoft.Azure.Commands.Network.Models;
16+
using System.Linq;
17+
using System.Management.Automation;
18+
19+
namespace Microsoft.Azure.Commands.Network
20+
{
21+
[Cmdlet("New", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "ApplicationGatewayFirewallCustomRuleGroupByVariable"), OutputType(typeof(PSApplicationGatewayFirewallCustomRuleGroupByVariable))]
22+
public class NewAzureApplicationGatewayFirewallCustomRuleGroupByVariableCommand : NetworkBaseCmdlet
23+
{
24+
[Parameter(
25+
Mandatory = true,
26+
HelpMessage = "User Session clause variable.")]
27+
[ValidateNotNullOrEmpty]
28+
[ValidateSet("ClientAddr", "Geo", "None", IgnoreCase = true)]
29+
public string VariableName { get; set; }
30+
31+
public override void ExecuteCmdlet()
32+
{
33+
base.ExecuteCmdlet();
34+
WriteObject(NewObject());
35+
}
36+
37+
protected PSApplicationGatewayFirewallCustomRuleGroupByVariable NewObject()
38+
{
39+
return new PSApplicationGatewayFirewallCustomRuleGroupByVariable()
40+
{
41+
VariableName = this.VariableName
42+
};
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)