Using multiple azure-cli credentials within automation
Have you ever needed an automated process to use alternative credentials for a subset of tasks?
Do your CI/CD processes run with least privilege and lack the permission to carry out certain higher privilege tasks like creating users, changing permissions or role assignments?
This post will demonstrate a technique that allows you setup multiple, concurrent authenticated sessions when using the azure-cli and switch freely between them.
Ordinarily when you run the az login
command, your profile stored in ~/.azure
will be updated with the obtained authentication token and the azure-cli will use this to authenticate subsequent commands.
However, it is possible to override the path used to store the profile data by setting the environment variable AZURE_CONFIG_DIR
. In this way your automated process can use the standard profile path for its default credentials but setup additional authenticated sessions for any other security contexts it requires to do its job.
You just need to ensure that this environment variable is set to point at the correct path before running your azure-cli command (and unset when you want to use the default credentials again).
The PowerShell script below should give you the general idea: