Evan Gertis
Evan Gertis

Reputation: 2052

No environment configuration found. DefaultAzureCredential()

I am trying to use this python sample to authenticate a client with an Azure Service

# pip install azure-identity
from azure.identity import DefaultAzureCredential
# pip install azure-mgmt-compute
from azure.mgmt.compute import ComputeManagementClient
# pip install azure-mgmt-network
from azure.mgmt.network import NetworkManagementClient
# pip install azure-mgmt-resource
from azure.mgmt.resource import ResourceManagementClient

    SUBSCRIPTION_ID = creds_obj['SUBSCRIPTION_ID']

    # Create client
    # For other authentication approaches, please see: https://pypi.org/project/azure-identity/
    resource_client = ResourceManagementClient(
        credential=DefaultAzureCredential(),
        subscription_id=SUBSCRIPTION_ID
    )
    network_client = NetworkManagementClient(
        credential=DefaultAzureCredential(),
        subscription_id=SUBSCRIPTION_ID
    )
    compute_client = ComputeManagementClient(
        credential=DefaultAzureCredential(),
        subscription_id=SUBSCRIPTION_ID
    )

I keep getting No environment configuration found. The code sample is directly from the microsoft github: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/resources/azure-mgmt-resource/azure/mgmt/resource/resources/_resource_management_client.py. Ideally I would like to manage this configuration using environment variables or a config file. Is there any way to do this?

Upvotes: 0

Views: 7973

Answers (2)

p_smithuk
p_smithuk

Reputation: 11

I was having somewhat similar trouble following this Azure key vault tutorial which brought me here.

The solution I found was overriding the default values in the DefaultAzureCredential() constructor.

https://learn.microsoft.com/en-us/python/api/azure-identity/azure.identity.defaultazurecredential?view=azure-python

For reasons people far smarter than me will be able to explain I found that even though I had credentials from the azure cli it was not using those and instead looking for environment_credentials, which I did not have. So it threw an exception.

Once I set the exclude_environment_credential argument to True it then looked instead for the managed_identity_credentials, again which I did not have.

Eventually when I force excluded all credentials other than those from the cli it worked ok for me

I hope this helps someone. Those with more experience please feel free to edit as you see fit

Upvotes: 0

unknown
unknown

Reputation: 7483

When using Azure Identity client library for Python, DefaultAzureCredential attempts to authenticate via the following mechanisms in this order, stopping when one succeeds:

enter image description here

You could set Environment Variables to fix it.

enter image description here

from azure.identity import DefaultAzureCredential

credential=DefaultAzureCredential()

Or set the properties in config and use ClientSecretCredential to create credential.

from azure.identity import ClientSecretCredential

subscription_id = creds_obj["AZURE_SUBSCRIPTION_ID"]
tenant_id = creds_obj["AZURE_TENANT_ID"]
client_id = creds_obj["AZURE_CLIENT_ID"]
client_secret = creds_obj["AZURE_CLIENT_SECRET"]

credential = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)

Upvotes: 1

Related Questions