Skip to content

Commit 1f2b689

Browse files
authored
{AppService} Prevent duplicate rules from being created. (#13540)
* Prevent duplicate rules from being created. * Update ChangeLog
1 parent aa3753c commit 1f2b689

File tree

6 files changed

+2690
-7
lines changed

6 files changed

+2690
-7
lines changed

src/Websites/Websites.Test/ScenarioTests/AccessRestrictionTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,12 @@ public void TestAddWebAppAccessRestrictionSlot()
115115
{
116116
WebsitesController.NewInstance.RunPsTest(_logger, "Test-AddWebAppAccessRestrictionSlot");
117117
}
118+
119+
[Fact]
120+
[Trait(Category.AcceptanceType, Category.CheckIn)]
121+
public void TestAddWebAppAccessRestrictionDuplicate()
122+
{
123+
WebsitesController.NewInstance.RunPsTest(_logger, "Test-AddWebAppAccessRestrictionDuplicate");
124+
}
118125
}
119126
}

src/Websites/Websites.Test/ScenarioTests/AccessRestrictionTests.ps1

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,4 +593,54 @@ function Test-AddWebAppAccessRestrictionSlot
593593
# Cleanup
594594
Remove-AzResourceGroup -Name $rgname -Force
595595
}
596+
}
597+
598+
<#
599+
.SYNOPSIS
600+
Add Duplicate IpAddress Access Restriction
601+
#>
602+
function Test-AddWebAppAccessRestrictionDuplicate
603+
{
604+
# Setup
605+
$rgname = Get-ResourceGroupName
606+
$wname = Get-WebsiteName
607+
$location = Get-WebLocation
608+
$whpName = Get-WebHostPlanName
609+
$tier = "Shared"
610+
$ipAddress = "130.220.0.0/27"
611+
$duplicateErrorMessage = "Rule for '$ipAddress' already exist"
612+
613+
try
614+
{
615+
# Setup
616+
New-AzResourceGroup -Name $rgname -Location $location
617+
$serverFarm = New-AzAppServicePlan -ResourceGroupName $rgname -Name $whpName -Location $location -Tier $tier
618+
619+
# Create new web app
620+
$webApp = New-AzWebApp -ResourceGroupName $rgname -Name $wname -Location $location -AppServicePlan $whpName
621+
622+
# Assert Setup
623+
Assert-AreEqual $wname $webApp.Name
624+
Assert-AreEqual $serverFarm.Id $webApp.ServerFarmId
625+
626+
# Run Tests
627+
$actual = Add-AzWebAppAccessRestrictionRule -ResourceGroupName $rgname -WebAppName $wname -Name developers -Action Allow -IpAddress $ipAddress -Priority 200 -PassThru
628+
629+
try
630+
{
631+
Add-AzWebAppAccessRestrictionRule -ResourceGroupName $rgname -WebAppName $wname -Name developers -Action Allow -IpAddress $ipAddress -Priority 200
632+
}
633+
catch [System.Exception]
634+
{
635+
$actualErrorMessage = $_.Exception.Message
636+
}
637+
638+
# Assert
639+
Assert-AreEqual $actualErrorMessage $duplicateErrorMessage
640+
}
641+
finally
642+
{
643+
# Cleanup
644+
Remove-AzResourceGroup -Name $rgname -Force
645+
}
596646
}

src/Websites/Websites.Test/SessionRecords/Microsoft.Azure.Commands.Websites.Test.ScenarioTests.AccessRestrictionTests/TestAddWebAppAccessRestrictionDuplicate.json

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

src/Websites/Websites.sln

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ScenarioTest.ResourceManage
2020
EndProject
2121
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestFx", "..\..\tools\TestFx\TestFx.csproj", "{BC80A1D0-FFA4-43D9-AA74-799F5CB54B58}"
2222
EndProject
23-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Authenticators", "..\Accounts\Authenticators\Authenticators.csproj", "{31473C1B-1B34-4545-BF2D-50BEE84E7283}"
24-
EndProject
2523
Global
2624
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2725
Debug|Any CPU = Debug|Any CPU
@@ -40,6 +38,10 @@ Global
4038
{142D7B0B-388A-4CEB-A228-7F6D423C5C2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
4139
{142D7B0B-388A-4CEB-A228-7F6D423C5C2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
4240
{142D7B0B-388A-4CEB-A228-7F6D423C5C2E}.Release|Any CPU.Build.0 = Release|Any CPU
41+
{6BD6B80A-06AF-4B5B-9230-69CCFC6C8D64}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
42+
{6BD6B80A-06AF-4B5B-9230-69CCFC6C8D64}.Debug|Any CPU.Build.0 = Debug|Any CPU
43+
{6BD6B80A-06AF-4B5B-9230-69CCFC6C8D64}.Release|Any CPU.ActiveCfg = Release|Any CPU
44+
{6BD6B80A-06AF-4B5B-9230-69CCFC6C8D64}.Release|Any CPU.Build.0 = Release|Any CPU
4345
{FF81DC73-B8EC-4082-8841-4FBF2B16E7CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
4446
{FF81DC73-B8EC-4082-8841-4FBF2B16E7CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
4547
{FF81DC73-B8EC-4082-8841-4FBF2B16E7CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -56,10 +58,6 @@ Global
5658
{BC80A1D0-FFA4-43D9-AA74-799F5CB54B58}.Debug|Any CPU.Build.0 = Debug|Any CPU
5759
{BC80A1D0-FFA4-43D9-AA74-799F5CB54B58}.Release|Any CPU.ActiveCfg = Release|Any CPU
5860
{BC80A1D0-FFA4-43D9-AA74-799F5CB54B58}.Release|Any CPU.Build.0 = Release|Any CPU
59-
{31473C1B-1B34-4545-BF2D-50BEE84E7283}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
60-
{31473C1B-1B34-4545-BF2D-50BEE84E7283}.Debug|Any CPU.Build.0 = Debug|Any CPU
61-
{31473C1B-1B34-4545-BF2D-50BEE84E7283}.Release|Any CPU.ActiveCfg = Release|Any CPU
62-
{31473C1B-1B34-4545-BF2D-50BEE84E7283}.Release|Any CPU.Build.0 = Release|Any CPU
6361
EndGlobalSection
6462
GlobalSection(SolutionProperties) = preSolution
6563
HideSolutionNode = FALSE

src/Websites/Websites/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Prevent duplicate access restriction rules
2122

2223
## Version 2.1.0
2324
* Added support for new access restriction features: ServiceTag, multi-ip and http-headers

src/Websites/Websites/Cmdlets/AccessRestriction/AddAzureWebAppAccessRestrictionRule.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,14 @@ public override void ExecuteCmdlet()
164164
int intPriority = checked((int)Priority);
165165
switch (ParameterSetName)
166166
{
167-
case IpAddressParameterSet:
167+
case IpAddressParameterSet:
168+
CheckDuplicateIPRestriction(IpAddress, accessRestrictionList);
168169
ipSecurityRestriction = new IpSecurityRestriction(IpAddress, null, null, null, null, Action, null, intPriority, Name, Description, httpHeader);
169170
accessRestrictionList.Add(ipSecurityRestriction);
170171
break;
171172

172173
case ServiceTagParameterSet:
174+
CheckDuplicateIPRestriction(ServiceTag, accessRestrictionList);
173175
ipSecurityRestriction = new IpSecurityRestriction(ServiceTag, null, null, null, null, Action, "ServiceTag", intPriority, Name, Description, httpHeader);
174176
accessRestrictionList.Add(ipSecurityRestriction);
175177
break;
@@ -182,6 +184,7 @@ public override void ExecuteCmdlet()
182184
//If unble to fetch SubNet rg from above step, use the input RG to get validation error from api call.
183185
subNetResourceGroupName = !String.IsNullOrEmpty(subNetResourceGroupName) ? subNetResourceGroupName : ResourceGroupName;
184186
var subnetResourceId = CmdletHelpers.ValidateSubnet(Subnet, VirtualNetworkName, subNetResourceGroupName, DefaultContext.Subscription.Id);
187+
CheckDuplicateServiceEndpointRestriction(subnetResourceId, accessRestrictionList);
185188
if (!IgnoreMissingServiceEndpoint)
186189
{
187190
CmdletHelpers.VerifySubnetDelegation(subnetResourceId);
@@ -232,5 +235,23 @@ private IDictionary<string, IList<string>> ConvertHeaderHashtable(Hashtable head
232235
}
233236
return headerResult;
234237
}
238+
239+
private void CheckDuplicateIPRestriction(string ipRange, IList<IpSecurityRestriction> accessRestrictionList)
240+
{
241+
foreach (var accessRestriction in accessRestrictionList)
242+
{
243+
if (accessRestriction.IpAddress == ipRange)
244+
throw new Exception($"Rule for '{ipRange}' already exist");
245+
}
246+
}
247+
248+
private void CheckDuplicateServiceEndpointRestriction(string subnetResourceId, IList<IpSecurityRestriction> accessRestrictionList)
249+
{
250+
foreach (var accessRestriction in accessRestrictionList)
251+
{
252+
if (accessRestriction.VnetSubnetResourceId == subnetResourceId)
253+
throw new Exception($"Rule for '{subnetResourceId}' already exist");
254+
}
255+
}
235256
}
236257
}

0 commit comments

Comments
 (0)