Skip to content

Commit d9dd75c

Browse files
authored
Remove range for Priority, handle empty name and remove by subnet/ip (Azure#10997)
* Remove range for Priority, handle empty name and remove by subnet/ip * Update tests and default value * merge changes
1 parent 50aaa7a commit d9dd75c

12 files changed

+4994
-3763
lines changed

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,8 @@
1717
using System.Management.Automation;
1818
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
1919
using Microsoft.Azure.Management.WebSites.Models;
20-
using System;
2120
using Microsoft.Azure.Commands.WebApps.Utilities;
22-
using System.Linq;
23-
using Microsoft.Azure.Commands.Common.Authentication;
24-
using Microsoft.Azure.Management.Internal.Network.Version2017_10_01;
25-
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
26-
using Microsoft.Azure.Management.Monitor.Version2018_09_01.Models;
21+
using System;
2722

2823
namespace Microsoft.Azure.Commands.WebApps.Cmdlets.WebApps
2924
{
@@ -53,9 +48,9 @@ public class AddAzureWebAppAccessRestrictionRuleCmdlet : WebAppBaseClientCmdLet
5348
[ValidateNotNullOrEmpty]
5449
public string WebAppName { get; set; }
5550

56-
[Parameter(ParameterSetName = IpAddressParameterSet, Mandatory = true, HelpMessage = "Access Restriction rule name. E.g.: DeveloperWorkstation.")]
57-
[Parameter(ParameterSetName = SubnetNameParameterSet, Mandatory = true, HelpMessage = "Access Restriction rule name. E.g.: DeveloperWorkstation.")]
58-
[Parameter(ParameterSetName = SubnetIdParameterSet, Mandatory = true, HelpMessage = "Access Restriction rule name. E.g.: DeveloperWorkstation.")]
51+
[Parameter(ParameterSetName = IpAddressParameterSet, Mandatory = false, HelpMessage = "Access Restriction rule name. E.g.: DeveloperWorkstation.")]
52+
[Parameter(ParameterSetName = SubnetNameParameterSet, Mandatory = false, HelpMessage = "Access Restriction rule name. E.g.: DeveloperWorkstation.")]
53+
[Parameter(ParameterSetName = SubnetIdParameterSet, Mandatory = false, HelpMessage = "Access Restriction rule name. E.g.: DeveloperWorkstation.")]
5954
[ValidateNotNullOrEmpty]
6055
public string Name { get; set; }
6156

@@ -68,15 +63,14 @@ public class AddAzureWebAppAccessRestrictionRuleCmdlet : WebAppBaseClientCmdLet
6863
[Parameter(ParameterSetName = SubnetNameParameterSet, Mandatory = true, HelpMessage = "Access Restriction priority. E.g.: 500.")]
6964
[Parameter(ParameterSetName = SubnetIdParameterSet, Mandatory = true, HelpMessage = "Access Restriction priority. E.g.: 500.")]
7065
[ValidateNotNullOrEmpty]
71-
[ValidateRange(100, 65000)]
7266
public uint Priority { get; set; }
7367

74-
[Parameter(ParameterSetName = IpAddressParameterSet, Mandatory = true, HelpMessage = "Allow or Deny rule.")]
75-
[Parameter(ParameterSetName = SubnetNameParameterSet, Mandatory = true, HelpMessage = "Allow or Deny rule.")]
76-
[Parameter(ParameterSetName = SubnetIdParameterSet, Mandatory = true, HelpMessage = "Allow or Deny rule.")]
68+
[Parameter(ParameterSetName = IpAddressParameterSet, Mandatory = false, HelpMessage = "Allow or Deny rule.")]
69+
[Parameter(ParameterSetName = SubnetNameParameterSet, Mandatory = false, HelpMessage = "Allow or Deny rule.")]
70+
[Parameter(ParameterSetName = SubnetIdParameterSet, Mandatory = false, HelpMessage = "Allow or Deny rule.")]
7771
[ValidateNotNullOrEmpty]
7872
[ValidateSet("Allow", "Deny")]
79-
public string Action { get; set; }
73+
public string Action { get; set; } = "Allow";
8074

8175
[Parameter(ParameterSetName = IpAddressParameterSet, Mandatory = false, HelpMessage = "Deployment Slot name.")]
8276
[Parameter(ParameterSetName = SubnetNameParameterSet, Mandatory = false, HelpMessage = "Deployment Slot name.")]
@@ -121,29 +115,12 @@ public override void ExecuteCmdlet()
121115
SiteConfig siteConfig = webApp.SiteConfig;
122116
var accessRestrictionList = TargetScmSite ? siteConfig.ScmIpSecurityRestrictions : siteConfig.IpSecurityRestrictions;
123117
IpSecurityRestriction ipSecurityRestriction = null;
124-
bool accessRestrictionExists = false;
125118
int intPriority = checked((int)Priority);
126119
switch (ParameterSetName)
127120
{
128-
case IpAddressParameterSet:
129-
foreach (var accessRestriction in accessRestrictionList)
130-
{
131-
if (accessRestriction.IpAddress != null &&
132-
accessRestriction.IpAddress == IpAddress &&
133-
accessRestriction.Action.ToLowerInvariant() == Action.ToLowerInvariant())
134-
{
135-
accessRestrictionExists = true;
136-
accessRestriction.Name = Name;
137-
accessRestriction.Priority = intPriority;
138-
accessRestriction.Description = Description;
139-
break;
140-
}
141-
}
142-
if (!accessRestrictionExists)
143-
{
144-
ipSecurityRestriction = new IpSecurityRestriction(IpAddress, null, null, null, null, Action, null, intPriority, Name, Description);
145-
accessRestrictionList.Add(ipSecurityRestriction);
146-
}
121+
case IpAddressParameterSet:
122+
ipSecurityRestriction = new IpSecurityRestriction(IpAddress, null, null, null, null, Action, null, intPriority, Name, Description);
123+
accessRestrictionList.Add(ipSecurityRestriction);
147124
break;
148125

149126
case SubnetNameParameterSet:
@@ -158,29 +135,13 @@ public override void ExecuteCmdlet()
158135
{
159136
CmdletHelpers.VerifySubnetDelegation(subnetResourceId);
160137
}
161-
foreach (var accessRestriction in accessRestrictionList)
162-
{
163-
if (accessRestriction.VnetSubnetResourceId != null &&
164-
accessRestriction.VnetSubnetResourceId.ToLowerInvariant() == subnetResourceId.ToLowerInvariant() &&
165-
accessRestriction.Action.ToLowerInvariant() == Action.ToLowerInvariant())
166-
{
167-
accessRestrictionExists = true;
168-
accessRestriction.Name = Name;
169-
accessRestriction.Priority = intPriority;
170-
accessRestriction.Description = Description;
171-
break;
172-
}
173-
}
174-
if (!accessRestrictionExists)
175-
{
176-
ipSecurityRestriction = new IpSecurityRestriction(null, null, subnetResourceId, null, null, Action, null, intPriority, Name, Description);
177-
accessRestrictionList.Add(ipSecurityRestriction);
178-
}
138+
139+
ipSecurityRestriction = new IpSecurityRestriction(null, null, subnetResourceId, null, null, Action, null, intPriority, Name, Description);
140+
accessRestrictionList.Add(ipSecurityRestriction);
179141
break;
180142
}
181143

182-
string updateAction = accessRestrictionExists ? "Updating" : "Adding";
183-
if (ShouldProcess(WebAppName, $"{updateAction} Access Restriction Rule '{Name}' for Web App '{WebAppName}'"))
144+
if (ShouldProcess(WebAppName, $"Adding Access Restriction Rule for Web App '{WebAppName}'"))
184145
{
185146
// Update web app configuration
186147
WebsitesClient.UpdateWebAppConfiguration(ResourceGroupName, webApp.Location, WebAppName, SlotName, siteConfig);

src/Websites/Websites/Cmdlets/AccessRestriction/RemoveAzureWebAppAccessRestrictionRule.cs

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
using System.Management.Automation;
1818
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
1919
using Microsoft.Azure.Management.WebSites.Models;
20-
using Microsoft.Azure.Management.Internal.Network.Version2017_10_01.Models;
20+
using Microsoft.Azure.Commands.WebApps.Utilities;
2121

2222
namespace Microsoft.Azure.Commands.WebApps.Cmdlets.WebApps
2323
{
@@ -38,25 +38,46 @@ public class RemoveAzureWebAppAccessRestrictionRuleCmdlet : WebAppBaseClientCmdL
3838
[ValidateNotNullOrEmpty]
3939
public string WebAppName { get; set; }
4040

41-
[Parameter(Mandatory = true, HelpMessage = "Access Restriction rule name. E.g.: DeveloperWorkstation.")]
41+
[Parameter(Mandatory = false, HelpMessage = "Access Restriction rule name. E.g.: DeveloperWorkstation.")]
4242
[ValidateNotNullOrEmpty]
4343
public string Name { get; set; }
4444

45+
[Parameter(Mandatory = false, HelpMessage = "Allow or Deny rule.")]
46+
[ValidateNotNullOrEmpty]
47+
[ValidateSet("Allow", "Deny")]
48+
public string Action { get; set; } = "Allow";
49+
4550
[Parameter(Mandatory = false, HelpMessage = "Rule is aimed for Main site or Scm site.")]
4651
[ValidateNotNullOrEmpty]
4752
public SwitchParameter TargetScmSite { get; set; }
4853

4954
[Parameter(Mandatory = false, HelpMessage = "Deployment Slot name.")]
5055
public string SlotName { get; set; }
5156

57+
[Parameter(Mandatory = false, HelpMessage = "Ip Address v4 or v6 CIDR range. E.g.: 192.168.0.0/24")]
58+
[ValidateNotNullOrEmpty]
59+
public string IpAddress { get; set; }
60+
61+
[Parameter(Mandatory = false, HelpMessage = "Name of Subnet.")]
62+
[ValidateNotNullOrEmpty]
63+
public string SubnetName { get; set; }
64+
65+
[Parameter(Mandatory = false, HelpMessage = "Name of Virtual Network (must be in same resource group as Web App).")]
66+
[ValidateNotNullOrEmpty]
67+
public string VirtualNetworkName { get; set; }
68+
69+
[Parameter(Mandatory = false, HelpMessage = "ResourceId of Subnet.")]
70+
[ValidateNotNullOrEmpty]
71+
public string SubnetId { get; set; }
72+
5273
[Parameter(Mandatory = false, HelpMessage = "Return the access restriction config object.")]
5374
public SwitchParameter PassThru { get; set; }
5475

5576
public override void ExecuteCmdlet()
5677
{
5778
if (!string.IsNullOrWhiteSpace(ResourceGroupName) && !string.IsNullOrWhiteSpace(WebAppName))
5879
{
59-
if (ShouldProcess(WebAppName, $"Removing Access Restriction Rule '{Name}' from Web App '{WebAppName}'"))
80+
if (ShouldProcess(WebAppName, $"Removing Access Restriction Rule from Web App '{WebAppName}'"))
6081
{
6182
var webApp = new PSSite(WebsitesClient.GetWebApp(ResourceGroupName, WebAppName, SlotName));
6283
SiteConfig siteConfig = webApp.SiteConfig;
@@ -66,12 +87,44 @@ public override void ExecuteCmdlet()
6687

6788
foreach (var accessRestriction in accessRestrictionList)
6889
{
69-
if (accessRestriction.Name.ToLowerInvariant() == Name.ToLowerInvariant())
90+
if (!string.IsNullOrWhiteSpace(Name))
91+
{
92+
if (!string.IsNullOrWhiteSpace(accessRestriction.Name) && accessRestriction.Name.ToLowerInvariant() == Name.ToLowerInvariant() && accessRestriction.Action.ToLowerInvariant() == Action.ToLowerInvariant())
93+
{
94+
ipSecurityRestriction = accessRestriction;
95+
accessRestrictionExists = true;
96+
break;
97+
}
98+
}
99+
else if (!string.IsNullOrWhiteSpace(IpAddress))
70100
{
71-
ipSecurityRestriction = accessRestriction;
72-
accessRestrictionExists = true;
73-
break;
101+
if (!string.IsNullOrWhiteSpace(accessRestriction.IpAddress) && accessRestriction.IpAddress.ToLowerInvariant() == IpAddress.ToLowerInvariant() && accessRestriction.Action.ToLowerInvariant() == Action.ToLowerInvariant())
102+
{
103+
if (!string.IsNullOrWhiteSpace(Name))
104+
if (!string.IsNullOrWhiteSpace(accessRestriction.Name) && accessRestriction.Name.ToLowerInvariant() == Name.ToLowerInvariant() && accessRestriction.Action.ToLowerInvariant() == Action.ToLowerInvariant())
105+
continue;
106+
107+
ipSecurityRestriction = accessRestriction;
108+
accessRestrictionExists = true;
109+
break;
110+
}
74111
}
112+
else if (!string.IsNullOrWhiteSpace(SubnetId) || (!string.IsNullOrWhiteSpace(SubnetName) && !string.IsNullOrWhiteSpace(VirtualNetworkName)))
113+
{
114+
var subnet = !string.IsNullOrWhiteSpace(SubnetId) ? SubnetId : SubnetName;
115+
var subnetResourceId = CmdletHelpers.ValidateSubnet(subnet, VirtualNetworkName, ResourceGroupName, DefaultContext.Subscription.Id);
116+
if (!string.IsNullOrWhiteSpace(accessRestriction.VnetSubnetResourceId) && accessRestriction.VnetSubnetResourceId.ToLowerInvariant() == subnetResourceId.ToLowerInvariant() && accessRestriction.Action.ToLowerInvariant() == Action.ToLowerInvariant())
117+
{
118+
if (!string.IsNullOrWhiteSpace(Name))
119+
if (!string.IsNullOrWhiteSpace(accessRestriction.Name) && accessRestriction.Name.ToLowerInvariant() == Name.ToLowerInvariant() && accessRestriction.Action.ToLowerInvariant() == Action.ToLowerInvariant())
120+
continue;
121+
122+
ipSecurityRestriction = accessRestriction;
123+
accessRestrictionExists = true;
124+
break;
125+
}
126+
}
127+
75128
}
76129
if (accessRestrictionExists)
77130
{

src/Websites/Websites/help/Add-AzWebAppAccessRestrictionRule.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@ Adds an Access Restiction rule to an Azure Web App.
1313

1414
### IpAddressParameterSet (Default)
1515
```
16-
Add-AzWebAppAccessRestrictionRule [-ResourceGroupName] <String> [-WebAppName] <String> -Name <String>
17-
[-Description <String>] -Priority <UInt32> -Action <String> [-SlotName <String>] [-TargetScmSite]
16+
Add-AzWebAppAccessRestrictionRule [-ResourceGroupName] <String> [-WebAppName] <String> [-Name <String>]
17+
[-Description <String>] -Priority <UInt32> [-Action <String>] [-SlotName <String>] [-TargetScmSite]
1818
-IpAddress <String> [-PassThru] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm]
1919
[<CommonParameters>]
2020
```
2121

2222
### SubnetNameParameterSet
2323
```
24-
Add-AzWebAppAccessRestrictionRule [-ResourceGroupName] <String> [-WebAppName] <String> -Name <String>
25-
[-Description <String>] -Priority <UInt32> -Action <String> [-SlotName <String>] [-TargetScmSite]
24+
Add-AzWebAppAccessRestrictionRule [-ResourceGroupName] <String> [-WebAppName] <String> [-Name <String>]
25+
[-Description <String>] -Priority <UInt32> [-Action <String>] [-SlotName <String>] [-TargetScmSite]
2626
-SubnetName <String> -VirtualNetworkName <String> [-IgnoreMissingServiceEndpoint] [-PassThru]
2727
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
2828
```
2929

3030
### SubnetIdParameterSet
3131
```
32-
Add-AzWebAppAccessRestrictionRule [-ResourceGroupName] <String> [-WebAppName] <String> -Name <String>
33-
[-Description <String>] -Priority <UInt32> -Action <String> [-SlotName <String>] [-TargetScmSite]
32+
Add-AzWebAppAccessRestrictionRule [-ResourceGroupName] <String> [-WebAppName] <String> [-Name <String>]
33+
[-Description <String>] -Priority <UInt32> [-Action <String>] [-SlotName <String>] [-TargetScmSite]
3434
-SubnetId <String> [-IgnoreMissingServiceEndpoint] [-PassThru] [-DefaultProfile <IAzureContextContainer>]
3535
[-WhatIf] [-Confirm] [<CommonParameters>]
3636
```
@@ -67,9 +67,9 @@ Parameter Sets: (All)
6767
Aliases:
6868
Accepted values: Allow, Deny
6969

70-
Required: True
70+
Required: False
7171
Position: Named
72-
Default value: None
72+
Default value: Allow
7373
Accept pipeline input: False
7474
Accept wildcard characters: False
7575
```
@@ -142,7 +142,7 @@ Type: System.String
142142
Parameter Sets: (All)
143143
Aliases:
144144

145-
Required: True
145+
Required: False
146146
Position: Named
147147
Default value: None
148148
Accept pipeline input: False

0 commit comments

Comments
 (0)