Skip to content

Commit ef80b26

Browse files
rachelxj-msXuejun Li
andauthored
Fixed Set-AzWebApp and Set-AZWebAppSlot to rethrow exception when Service Principal/User doesn't have permission to list web app configuration. [#19942] (#20159)
Co-authored-by: Xuejun Li <[email protected]>
1 parent 5d5d238 commit ef80b26

File tree

6 files changed

+6275
-15724
lines changed

6 files changed

+6275
-15724
lines changed

src/Websites/Websites.Test/SessionRecords/Microsoft.Azure.Commands.Websites.Test.ScenarioTests.WebAppTests/TestGetWebApp.json

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

src/Websites/Websites.Test/SessionRecords/Microsoft.Azure.Commands.Websites.Test.ScenarioTests.WebAppTests/TestSetWebApp.json

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

src/Websites/Websites/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
-->
2020
## Upcoming Release
2121
* Added Tag parameter for `New-AzWebApp` and `New-AzWebAppSlot`
22+
* Fixed `Set-AzWebApp` and `Set-AZWebAppSlot` to rethrow exception when Service Principal/User doesn't have permission to list web app configuration. [#19942]
2223

2324
## Version 2.11.5
2425
* Fixed `Publish-AzWebApp` to use latest publish API when deploying war package [#19791]

src/Websites/Websites/Cmdlets/DeploymentSlots/SetAzureWebAppSlot.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public override void ExecuteCmdlet()
147147
switch (ParameterSetName)
148148
{
149149
case ParameterSet1Name:
150-
WebApp = new PSSite(WebsitesClient.GetWebApp(ResourceGroupName, Name, Slot));
150+
WebApp = new PSSite(WebsitesClient.GetWebApp(ResourceGroupName, Name, Slot, false));
151151
location = WebApp.Location;
152152
tags = WebApp.Tags;
153153
var parameters = new HashSet<string>(MyInvocation.BoundParameters.Keys, StringComparer.OrdinalIgnoreCase);

src/Websites/Websites/Cmdlets/WebApps/SetAzureWebApp.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public override void ExecuteCmdlet()
151151
switch (ParameterSetName)
152152
{
153153
case ParameterSet1Name:
154-
WebApp = new PSSite(WebsitesClient.GetWebApp(ResourceGroupName, Name, null));
154+
WebApp = new PSSite(WebsitesClient.GetWebApp(ResourceGroupName, Name, null, false));
155155
location = WebApp.Location;
156156
tags = WebApp.Tags;
157157
var parameters = new HashSet<string>(MyInvocation.BoundParameters.Keys, StringComparer.OrdinalIgnoreCase);

src/Websites/Websites/Utilities/WebsitesClient.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public HttpStatusCode RemoveWebApp(string resourceGroupName, string webSiteName,
246246
return HttpStatusCode.OK;
247247
}
248248

249-
public PSSite GetWebApp(string resourceGroupName, string webSiteName, string slotName)
249+
public PSSite GetWebApp(string resourceGroupName, string webSiteName, string slotName, Boolean ignoreError = true)
250250
{
251251
Site site = null;
252252
string qualifiedSiteName;
@@ -255,7 +255,7 @@ public PSSite GetWebApp(string resourceGroupName, string webSiteName, string slo
255255
WrappedWebsitesClient.WebApps().GetSlot(resourceGroupName, webSiteName, slotName) :
256256
WrappedWebsitesClient.WebApps().Get(resourceGroupName, webSiteName);
257257

258-
GetWebAppConfiguration(resourceGroupName, webSiteName, slotName, site);
258+
GetWebAppConfiguration(resourceGroupName, webSiteName, slotName, site, ignoreError);
259259
PSSite psSite = new PSSite(site);
260260
var AzureStorageAccounts = CmdletHelpers.ShouldUseDeploymentSlot(webSiteName, slotName, out qualifiedSiteName) ?
261261
GetAzureStorageAccounts(resourceGroupName, webSiteName, slotName, true) :
@@ -601,7 +601,7 @@ public void UpdateWebAppConfiguration(string resourceGroupName, string location,
601601
}
602602
}
603603

604-
public void GetWebAppConfiguration(string resourceGroupName, string webSiteName, string slotName, Site site)
604+
public void GetWebAppConfiguration(string resourceGroupName, string webSiteName, string slotName, Site site, Boolean ignoreError = true)
605605
{
606606
string qualifiedSiteName;
607607
var useSlot = CmdletHelpers.ShouldUseDeploymentSlot(webSiteName, slotName, out qualifiedSiteName);
@@ -629,9 +629,13 @@ public void GetWebAppConfiguration(string resourceGroupName, string webSiteName,
629629
Type = s.Value.Type
630630
}).ToList();
631631
}
632-
catch
632+
catch (Exception e)
633633
{
634-
//ignore if this call fails as it will for reader RBAC
634+
if (!ignoreError)
635+
{
636+
ReThrowException(e);
637+
}
638+
//continue if ignoreError is true, this call fails as it will for reader RBAC
635639
}
636640
}
637641

@@ -1081,5 +1085,21 @@ private void WriteError(string errorFormat, params object[] args)
10811085
ErrorLogger(string.Format(errorFormat, args));
10821086
}
10831087
}
1088+
1089+
private void ReThrowException(Exception e)
1090+
{
1091+
if (e.GetType() == typeof(DefaultErrorResponseException))
1092+
{
1093+
DefaultErrorResponseException originEx = (DefaultErrorResponseException)e;
1094+
if (originEx != null && originEx.Body != null && originEx.Body.Error != null)
1095+
{
1096+
string errorMessage = string.Format("{0}.{1}", originEx.Message, originEx.Body.Error.Message);
1097+
DefaultErrorResponseException ex = new DefaultErrorResponseException(errorMessage, originEx.InnerException);
1098+
ex.Body = originEx.Body;
1099+
throw ex;
1100+
}
1101+
}
1102+
throw e;
1103+
}
10841104
}
10851105
}

0 commit comments

Comments
 (0)