Azure / azure-powershell

Microsoft Azure PowerShell
Other
4.21k stars 3.81k forks source link

[Az.Storage] Set-AzCurrentStorageAccount can't set context with Oauth credential #14144

Open blueww opened 3 years ago

blueww commented 3 years ago

Description

Set-AzCurrentStorageAccount can't set context with Oauth credential

Steps to reproduce


PS C:\Users\weiwei> $oauthCtx = New-AzStorageContext -StorageAccountName $accountName

PS C:\Users\weiwei> Set-AzCurrentStorageAccount -Context $oauthCtx
Set-AzCurrentStorageAccount : Object reference not set to an instance of an object.
At line:1 char:1
+ Set-AzCurrentStorageAccount -Context $oauthCtx
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Set-AzCurrentStorageAccount], NullReferenceException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Management.Storage.SetAzureRmCurrentStorageAccount

Environment data

Not related

Module versions

Az.Storage 3.2.1

Debug output

PS C:\> Set-AzCurrentStorageAccount -Context $oauthCtx -debug
DEBUG: 5:48:31 PM - SetAzureRmCurrentStorageAccount begin processing with ParameterSet 'UsingStorageContext'.
DEBUG: 5:48:32 PM - using account id '[hidden]'...
Set-AzCurrentStorageAccount : Object reference not set to an instance of an object.
At line:2 char:1
+ Set-AzCurrentStorageAccount -Context $oauthCtx -debug
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Set-AzCurrentStorageAccount], NullReferenceException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Management.Storage.SetAzureRmCurrentStorageAccount

DEBUG: 5:48:33 PM - SetAzureRmCurrentStorageAccount end processing.

Error output

Set-AzCurrentStorageAccount : Object reference not set to an instance of an object.
At line:1 char:1
+ Set-AzCurrentStorageAccount -Context $oauthCtx
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Set-AzCurrentStorageAccount], NullReferenceException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Management.Storage.SetAzureRmCurrentStorageAccount
blueww commented 3 years ago

This can't be fixed now since: SetCurrentStorageAccount(this IAzureContext context, IStorageContext storageContext) will lost the oauth credential when set the default context. This is caused by the function will convert the account to connectionstring, then set current context with connectionstring. But Oauth does NOT support connection string.

@dingmeng-xue , @erich-wang Would you please help to fix the function SetCurrentStorageAccount(this IAzureContext context, IStorageContext storageContext)? Then I can fix the cmdlet in storage module.

https://github.com/Azure/azure-powershell-common/blob/a02bcdbecd649bd807ed2634b53fddc7d02d571f/src/Storage/AzureContextExtensions.cs#L43

JustinGrote commented 3 weeks ago

I think I just hit this while trying to set a context using a managed identity in Azure Functions as the default context.

System.NullReferenceException:
   at Microsoft.Azure.Commands.Management.Storage.SetAzureRmCurrentStorageAccount.ExecuteCmdlet (Microsoft.Azure.PowerShell.Cmdlets.Storage.Management, Version=6.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
   at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions+<>c__3`1.<ExecuteSynchronouslyOrAsJob>b__3_0 (Microsoft.Azure.PowerShell.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
   at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.ExecuteSynchronouslyOrAsJob (Microsoft.Azure.PowerShell.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
   at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.ExecuteSynchronouslyOrAsJob (Microsoft.Azure.PowerShell.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
   at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord (Microsoft.Azure.PowerShell.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
blueww commented 3 weeks ago

@isra-fel Would you please help to look at this issue? The root cause is in the below common function. Any idea to fix it? https://github.com/Azure/azure-powershell-common/blob/a02bcdbecd649bd807ed2634b53fddc7d02d571f/src/Storage/AzureContextExtensions.cs#L43