11# Guide for migrating to azure-identity from azure-common
22
3+ The newest Azure SDK libraries (the "client" and "management" libraries
4+ [ listed here] ( https://azure.github.io/azure-sdk/releases/latest/python.html ) )
5+ use credentials from ` azure-identity ` to authenticate requests. Older versions
6+ of these libraries typically used credentials from ` azure-common ` . Credential
7+ types from these two libraries have different APIs, causing clients to raise
8+ ` AttributeError ` when given a credential from the wrong library. For example, a
9+ client expecting an ` azure-identity ` credential will raise an error like
10+ ` 'ServicePrincipalCredentials' object has no attribute 'get_token' ` when given a
11+ credential from ` azure-common ` . A client expecting an ` azure-common ` credential
12+ will raise an error like
13+ ` 'ClientSecretCredential' object has no attribute 'signed_session' ` when given
14+ an ` azure-identity ` credential.
15+
16+ This document shows common authentication code using ` azure-common ` , and its
17+ equivalent using ` azure-identity ` .
18+
19+ ## Service principal authentication
20+
21+ ` azure-common ` uses ` ServicePrincipalCredentials ` to authenticate a service principal:
22+
23+ ``` py
24+ from azure.common.credentials import ServicePrincipalCredentials
25+
26+ credential = ServicePrincipalCredentials(client_id, client_secret, tenant = tenant_id)
27+ ```
28+
29+ ` azure-identity ` uses [ ` ClientSecretCredential ` ] [ client_secret_cred ] :
30+
31+ ``` py
32+ from azure.identity import ClientSecretCredential
33+
34+ credential = ClientSecretCredential(tenant_id, client_id, client_secret)
35+ ```
36+
37+ ## Authenticating through the Azure CLI
38+
39+ ` azure-common ` provides the
40+ [ ` get_client_from_cli_profile ` ] [ get_client_from_cli_profile ] function to
41+ integrate with the Azure CLI for authentication. This code works with older
42+ versions of ` azure-mgmt-resource ` such as 10.0.0:
43+
44+ ``` py
45+ from azure.common.client_factory import get_client_from_cli_profile
46+ from azure.mgmt.resource import SubscriptionClient
47+
48+ subscription_client = get_client_from_cli_profile(SubscriptionClient)
49+ ```
50+
51+ ` azure-identity ` integrates with the Azure CLI through its
52+ [ ` AzureCliCredential ` ] [ cli_cred ] . This code works with newer versions of
53+ ` azure-mgmt-resource ` , starting with 15.0.0:
54+
55+ ``` py
56+ from azure.identity import AzureCliCredential
57+ from azure.mgmt.resource import SubscriptionClient
58+
59+ credential = AzureCliCredential()
60+ subscription_client = SubscriptionClient(credential)
61+ ```
62+
363## JSON- and file-based authentication
464
565To encourage best security practices, ` azure-identity ` does not support JSON- and file-based authentication in the same
@@ -34,7 +94,7 @@ from azure.mgmt.keyvault import KeyVaultManagementClient
3494
3595with open (" credentials.json" ) as json_file:
3696 json_dict = json.load(json_file)
37-
97+
3898credential = ClientSecretCredential(
3999 tenant_id = json_dict[" tenantId" ],
40100 client_id = json_dict[" clientId" ],
@@ -55,7 +115,11 @@ control -- for example, by adding the credential file name to your project's `.g
55115The global documentation for authenticating Python apps on Azure is available [ here] [ authenticate_docs ] .
56116
57117[ authenticate_docs ] : https://docs.microsoft.com/azure/developer/python/azure-sdk-authenticate?tabs=cmd
118+ [ cli_cred ] : https://aka.ms/azsdk/python/identity/docs#azure.identity.AzureCliCredential
58119[ client_from_json ] : https://docs.microsoft.com/python/api/azure-common/azure.common.client_factory?view=azure-python#get-client-from-json-dict-client-class--config-dict----kwargs-
59120[ client_from_auth_file ] : https://docs.microsoft.com/python/api/azure-common/azure.common.client_factory?view=azure-python#get-client-from-auth-file-client-class--auth-path-none----kwargs-
60- [ client_secret_cred ] : https://docs.microsoft.com/python/api/azure-identity/azure.identity.clientsecretcredential?view=azure-python
121+ [ client_secret_cred ] : https://aka.ms/azsdk/python/identity/docs#azure.identity.ClientSecretCredential
122+ [ get_client_from_cli_profile ] : https://docs.microsoft.com/python/api/azure-common/azure.common.client_factory?view=azure-python#get-client-from-cli-profile-client-class----kwargs-
61123[ json ] : https://docs.python.org/3/library/json.html#json.load
124+
125+ ![ Impressions] ( https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-python%2Fsdk%2Fidentity%2Fazure-identity%2Fmigration_guide.png )
0 commit comments