Skip to content

Fixed Set-AzWebApp and Set-AZWebAppSlot to rethrow exception when Service Principal/User doesn't have permission to list web app configuration. [#19942] #20159

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

Merged
merged 1 commit into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/Websites/Websites/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
-->
## Upcoming Release
* Added Tag parameter for `New-AzWebApp` and `New-AzWebAppSlot`
* Fixed `Set-AzWebApp` and `Set-AZWebAppSlot` to rethrow exception when Service Principal/User doesn't have permission to list web app configuration. [#19942]

## Version 2.11.5
* Fixed `Publish-AzWebApp` to use latest publish API when deploying war package [#19791]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public override void ExecuteCmdlet()
switch (ParameterSetName)
{
case ParameterSet1Name:
WebApp = new PSSite(WebsitesClient.GetWebApp(ResourceGroupName, Name, Slot));
WebApp = new PSSite(WebsitesClient.GetWebApp(ResourceGroupName, Name, Slot, false));
location = WebApp.Location;
tags = WebApp.Tags;
var parameters = new HashSet<string>(MyInvocation.BoundParameters.Keys, StringComparer.OrdinalIgnoreCase);
Expand Down
2 changes: 1 addition & 1 deletion src/Websites/Websites/Cmdlets/WebApps/SetAzureWebApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public override void ExecuteCmdlet()
switch (ParameterSetName)
{
case ParameterSet1Name:
WebApp = new PSSite(WebsitesClient.GetWebApp(ResourceGroupName, Name, null));
WebApp = new PSSite(WebsitesClient.GetWebApp(ResourceGroupName, Name, null, false));
location = WebApp.Location;
tags = WebApp.Tags;
var parameters = new HashSet<string>(MyInvocation.BoundParameters.Keys, StringComparer.OrdinalIgnoreCase);
Expand Down
30 changes: 25 additions & 5 deletions src/Websites/Websites/Utilities/WebsitesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public HttpStatusCode RemoveWebApp(string resourceGroupName, string webSiteName,
return HttpStatusCode.OK;
}

public PSSite GetWebApp(string resourceGroupName, string webSiteName, string slotName)
public PSSite GetWebApp(string resourceGroupName, string webSiteName, string slotName, Boolean ignoreError = true)
{
Site site = null;
string qualifiedSiteName;
Expand All @@ -255,7 +255,7 @@ public PSSite GetWebApp(string resourceGroupName, string webSiteName, string slo
WrappedWebsitesClient.WebApps().GetSlot(resourceGroupName, webSiteName, slotName) :
WrappedWebsitesClient.WebApps().Get(resourceGroupName, webSiteName);

GetWebAppConfiguration(resourceGroupName, webSiteName, slotName, site);
GetWebAppConfiguration(resourceGroupName, webSiteName, slotName, site, ignoreError);
PSSite psSite = new PSSite(site);
var AzureStorageAccounts = CmdletHelpers.ShouldUseDeploymentSlot(webSiteName, slotName, out qualifiedSiteName) ?
GetAzureStorageAccounts(resourceGroupName, webSiteName, slotName, true) :
Expand Down Expand Up @@ -601,7 +601,7 @@ public void UpdateWebAppConfiguration(string resourceGroupName, string location,
}
}

public void GetWebAppConfiguration(string resourceGroupName, string webSiteName, string slotName, Site site)
public void GetWebAppConfiguration(string resourceGroupName, string webSiteName, string slotName, Site site, Boolean ignoreError = true)
{
string qualifiedSiteName;
var useSlot = CmdletHelpers.ShouldUseDeploymentSlot(webSiteName, slotName, out qualifiedSiteName);
Expand Down Expand Up @@ -629,9 +629,13 @@ public void GetWebAppConfiguration(string resourceGroupName, string webSiteName,
Type = s.Value.Type
}).ToList();
}
catch
catch (Exception e)
{
//ignore if this call fails as it will for reader RBAC
if (!ignoreError)
{
ReThrowException(e);
}
//continue if ignoreError is true, this call fails as it will for reader RBAC
}
}

Expand Down Expand Up @@ -1081,5 +1085,21 @@ private void WriteError(string errorFormat, params object[] args)
ErrorLogger(string.Format(errorFormat, args));
}
}

private void ReThrowException(Exception e)
{
if (e.GetType() == typeof(DefaultErrorResponseException))
{
DefaultErrorResponseException originEx = (DefaultErrorResponseException)e;
if (originEx != null && originEx.Body != null && originEx.Body.Error != null)
{
string errorMessage = string.Format("{0}.{1}", originEx.Message, originEx.Body.Error.Message);
DefaultErrorResponseException ex = new DefaultErrorResponseException(errorMessage, originEx.InnerException);
ex.Body = originEx.Body;
throw ex;
}
}
throw e;
}
}
}