Get-AzContext is unable to detect password changes to account and is unreliable
Description
Get-Azcontext cannot be reliably used as a way to determine if the user is connected to azure.
I believe the problem may involve accounts with auto rotating passwords.
Example: Monday: Connect-azaccount, then do work as normal. Monday at midnight - account password is reset in AAD. Tuesday: Run Get-AzContext to see if account is logged in - This command returns information, as if there was an active connection, Then run any command such as get-azsubscription - the command fails with:
WARNING: Unable to acquire token for tenant 'xxxxx' with error
'SharedTokenCacheCredential authentication unavailable. Token acquisition failed for user [email protected]. Ensure
that you have authenticated with a developer tool that supports Azure single sign on.'
What I believe may be happening is that signing in with connect-azaccount likely sets a token into a file on the local PC - that file is then referenced by get-azcontext without get-azcontext actually checking to see if the token is valid. When the AAD account associated with the token has it's password changed, the cached token is technically not valid anymore, but get-azcontext does not check the validity and reports back an active context.
Solution: Get-azcontext should confirm the token is valid before reporting.
Issue script & Debug output
I can't recreate the debug script at this time since I've already re-logged into azure for the day. Please let me know if the other information isn't sufficient.
Environment data
Name Value
---- -----
PSVersion 5.1.19041.1320
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.19041.1320
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Module versions
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 2.7.2 Az.Accounts {Add-AzEnvironment, Clear-AzContext, Clear-AzDefault, Con...
Binary 2.0.2.134 AzureADPreview {Add-AzureADAdministrativeUnitMember, Add-AzureADApplicat...
Error output
No response
Thanks for reporting. It is be design. In the most cases, credential won't be changed with high frequency but check all context is expensive. We rely on customer to make the call. If credential might be changed, calling Connect-AzAccount or Get-AzResourceGroup can detect kind of error.
Thanks Dingmeng-xue
Here are some thoughts:
Why is it a problem if checking contexts is expensive? Is the reason for the existence of this commandlet to let the user know if they have a connection? (my thought is that a connection check is typically only run once per script execution - so the time it takes is minimal compared to a script that say enumerates through 60 subscriptions and 20,000 resources.)
As for the alternatives - I've noticed that all the get-az* commands will fail if the token is no good, but was looking for a more elegant way of validating that it worked. That said, my first thought was to investigate why get-azcontext does not work as designed, so I've not explored other options - would something like clearing errors, running get-azsubscription and then checking error state work here? (Connect-azaccount is what we're trying to avoid running so I don't want to use that, but I appreciate the suggestion!)
@jackfruhecolab the Azure PowerShell context is an object used to hold subscription information and authentication information like account name and the intent of the Get-AzContext cmdlet is to obtain the metadata of the context used but not to verify its validity. Making a validation of the context would induce unnecessary calls in a vast majority of cases and in configurations using a lot of subscriptions, the performance impact would not be negligible.
That being said, I'd like to investigate how we can address this situation and why you would like to avoid using Connect-AzAccount?
Hi, we're sending this friendly reminder because we haven't heard back from you in a while. We need more information about this issue to help address it. Please be sure to give us your input within the next 7 days. If we don't hear back from you within 14 days of this comment the issue will be automatically closed. Thank you!
Thanks @dcaro
I'm looking for a way to determine if connect-azaccount needs to be run.
Basically we want a single script with logic that will allow it to run either automated or on a users PC.
Ideally we need this logic in the script:
# Are we running in a runbook in Azure Automation?
if ($runningasRunbookInAzureAutomation -eq $true)
{
if($runningOnAHybridWorker -eq $true)
{
#use managed identy to connect to azure
} else {
#use the runas account to connect to azure
}
} else {
#else - means we are not running in an automation account - we must be running on someones PC
if($userisconnectedtoAzure -eq $true)
{
#do nothing, user is already logged into Azure
} else {
#otherwise, the user needs to log in to azure
Connect-azaccount #prompt user for credentials
}
}
These are the scenarios we want to account for in our scripts, thanks!