azure-powershell icon indicating copy to clipboard operation
azure-powershell copied to clipboard

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

Open blueww opened this issue 4 years ago • 3 comments

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 avatar Feb 05 '21 09:02 blueww

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

blueww avatar Feb 05 '21 09:02 blueww

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)

JustinGrote avatar Aug 22 '24 22:08 JustinGrote

@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

blueww avatar Aug 23 '24 01:08 blueww