Extract Azure email from cli credentials
See original GitHub issueIs your feature request related to a problem? Please describe.
I would like to extract the user email from the Azure CLI credentials to use in my application.
I know the authentication details would be stored in ~/.azure/azureProfile.json
. However, I haven’t found a native function to extract those automatically.
Describe the solution you’d like
from azure.common.credentials import get_principal_name_from_cli_credentials
user_email = get_principal_name_from_cli_credentials()
Describe alternatives you’ve considered
My first approach was using a solution similar to what suggested in #15260
from azure.common.credentials import get_azure_cli_credentials
from azure.graphrbac import GraphRbacManagementClient
TENANT_ID = 'xxxxxx-xxxxxxxx-xxxxxxx'
cred, _ = get_azure_cli_credentials( # pylint: disable=unbalanced-tuple-unpacking
resource='https://graph.windows.net'
)
graphrbac_client = GraphRbacManagementClient(credentials=cred, tenant_id=TENANT_ID)
user_obj = graphrbac_client.signed_in_user.get()
email = user_obj.user_principal_name
However azure-graphrbac
has now been deprecated. So I have tried to moved this approach to a non deprecated SDK:
from azure.common.credentials import get_cli_profile
try:
profile = get_cli_profile()
user = profile._storage.data['subscriptions'][0]['user']['name'] # pylint:disable=protected-access
except (FileNotFoundError, KeyError):
print("Couldn't find email")
user = "nope@nope"
This second code snippet is using azure.common.credentials
which I think it might conflict with azure.identity
? Also I’m accessing a protected variable that might change name or structure in the future.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Hi @gdippolito
The information you need is actually CLI based, and our SDK team has no special access to extract that for you. You could open an issue on the github CLI repo, and ask for a public API to access CLI values from Python. In other words, ask for a “Azure CLI SDK for Python”.
We used to have some convenience in
azure-common
indeed, but we are deprecating this code since CLI architecture changed and we can’t access reliably this information anymore. The CLI is now an application, and only official access is through executing commands. Code we used to have that was using the implementation details that CLI was in Python can no longer be executed. See this PR for instance.My suggestions would be:
Appreciate the feedback, and I apologize we can’t provide what you were hoping 😦
Gentle ping. 😄