azure-cli: Corruption of `~/.azure` files

Describe the bug The files inside the .azure folder gets corrupted

To Reproduce IT’s a race condition, so it’s really diffictult to reproduce. I was using terraform, that uses the azure go sdk. at the same time I was running az aks list to see when the resource would come online. az cli failed with the following message.

az aks list
Failed to load token files. If you have a repro, please log an issue at https://github.com/Azure/azure-cli/issues. At the same time, you can clean up by running 'az account clear' and then 'az login'. (Inner Error: Failed to parse /Users/jopedros/.azure/accessTokens.json with exception:
    Extra data: line 1 column 16946 (char 16945))

Opening the file manually, I could see in the token array, there was an extra set of braces

}]

Manually correcting the file solved the issue.

Environment summary az --version azure-cli 2.0.63 *

osX, bash.

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 10
  • Comments: 21 (9 by maintainers)

Most upvoted comments

@nfx there is a subscribe button in the right column in the web UI, which you can use to avoid notifying existing subscribers 😉

I keep getting this error.

Failed to load token files. If you have a repro, please log an issue at https://github.com/Azure/azure-cli/issues. At the same time, you can clean up by running 'az account clear' and then 'az login'. (Inner Error: Failed to parse C:\Users\fey\.azure\accessTokens.json with exception:
    Extra data: line 1 column 15818 (char 15817))

We currently have a beta release for Windows which uses MSAL as the authentication library. It already contains the file locking mechanism. But we haven’t fully tested it yet. (Will test it in #14070.)

You may want to give it a try: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-windows?view=azure-cli-latest&tabs=azure-cli

I am seeing this now with the latest visual studio 2019, 16.6.2 and .net core 3.1. I noticed every day the first time I tried launch a project in visual studio, though if I clear the account, az account clear and then use a device code login I’ll be ok for the day. If I remove the characters at the position shown in the error to then end of the file, the tokens are still valid

Sorry for the inconvenience caused. Terraform azurerm provider also uses az account get-access-token internally (src):

err := jsonUnmarshalAzCmd(&token, "account", "get-access-token", "--resource", endpoint, "--subscription", subscriptionId, "-o=json")

According to https://stackoverflow.com/a/186464/2199657, there is no cross-platform way for file locking in Python built-in libraries. We will evaluate portalocker and see if we can incorporate it.

For now you may refer to https://docs.microsoft.com/en-us/cli/azure/use-cli-effectively#concurrent-builds for concurrent executions of Azure CLI.

I hit this issue many times as well. It’s very annoying.

I have encountered the same issue twice when running a .NET Core project (targeting netcoreapp3.1) from Visual Studio 2019 (16.4.2) on win10. No Azure orchestration was involved. Maybe this sheds some new light on the issue.

I am authentating with “az login” (azure-cli 2.0.77) and accessing a keyvault from the .NET Core project. At some seemingly arbitrary point my code stops working (in a manner that suggests expiration of a token), I try to “az login” again and it complains about extra data in (user dir)/.azure/accessTokens.json, same as in the original report. Removing the extra chars fixes the issue.

My extra data started at column 17991 and was 5 characters long, which suggests a pattern of the corrupt files being 8n+4 characters long.

add to S164.

I observe the same problem when using Terraform with fetching credentials from Azure CLI on macOS (https://www.terraform.io/docs/providers/azurerm/auth/azure_cli.html).

I do not think this is a problem within the Terraform Azure provider.

This is a known issue. Meantime, the workaround is to use Azure_CONFIG_DIR to isolate CLI into its own sandbox