66
77import os .path
88import time
9+ import warnings
910try :
1011 from azure .core .credentials import AccessToken as _AccessToken
1112except ImportError :
1516def get_cli_profile ():
1617 """Return a CLI profile class.
1718
19+ *Disclaimer*: This method is not working for azure-cli-core>=2.21.0 (released in March 2021).
20+
1821 .. versionadded:: 1.1.6
1922
23+ .. deprecated:: 1.1.28
24+
2025 :return: A CLI Profile
2126 :rtype: azure.cli.core._profile.Profile
2227 :raises: ImportError if azure-cli-core package is not available
@@ -27,8 +32,12 @@ def get_cli_profile():
2732 from azure .cli .core ._session import ACCOUNT
2833 from azure .cli .core ._environment import get_config_dir
2934 except ImportError :
30- raise ImportError ("You need to install 'azure-cli-core' to load CLI credentials" )
31-
35+ raise ImportError (
36+ "The public API of azure-cli-core has been deprecated starting 2.21.0, " +
37+ "and this method can no longer return a profile. " +
38+ "If you need to load CLI profile using this method, you need to install 'azure-cli-core<2.21.0'. " +
39+ "You may corrupt data if you use current CLI and old azure-cli-core."
40+ )
3241
3342 azure_folder = get_config_dir ()
3443 ACCOUNT .load (os .path .join (azure_folder , 'azureProfile.json' ))
@@ -69,6 +78,7 @@ def get_token(self, *scopes, **kwargs): # pylint:disable=unused-argument
6978 resource = scope
7079
7180 credentials = self ._get_cred (resource )
81+ # _token_retriever() not accessible after azure-cli-core 2.21.0
7282 _ , token , fulltoken = credentials ._token_retriever () # pylint:disable=protected-access
7383
7484 return _AccessToken (token , int (fulltoken ['expiresIn' ] + time .time ()))
@@ -81,19 +91,58 @@ def signed_session(self, session=None):
8191def get_azure_cli_credentials (resource = None , with_tenant = False ):
8292 """Return Credentials and default SubscriptionID of current loaded profile of the CLI.
8393
84- Credentials will be the "az login" command:
94+ *Disclaimer*: This method is not working for azure-cli-core>=2.21.0 (released in March 2021).
95+ It is now recommended to authenticate using https://pypi.org/project/azure-identity/ and AzureCliCredential.
96+ See example code below:
97+
98+ .. code:: python
99+
100+ from azure.identity import AzureCliCredential
101+ from azure.mgmt.compute import ComputeManagementClient
102+ client = ComputeManagementClient(AzureCliCredential(), subscription_id)
103+
104+
105+ For compatible azure-cli-core version (< 2.20.0), credentials will be the "az login" command:
85106 https://docs.microsoft.com/cli/azure/authenticate-azure-cli
86107
87108 Default subscription ID is either the only one you have, or you can define it:
88109 https://docs.microsoft.com/cli/azure/manage-azure-subscriptions-azure-cli
89110
90111 .. versionadded:: 1.1.6
91112
113+ .. deprecated:: 1.1.28
114+
115+ .. seealso:: https://aka.ms/azsdk/python/identity/migration
116+
92117 :param str resource: The alternative resource for credentials if not ARM (GraphRBac, etc.)
93118 :param bool with_tenant: If True, return a three-tuple with last as tenant ID
94119 :return: tuple of Credentials and SubscriptionID (and tenant ID if with_tenant)
95120 :rtype: tuple
96121 """
122+ warnings .warn (
123+ "get_client_from_cli_profile is deprecated, please use azure-identity and AzureCliCredential instead. " +
124+ "https://aka.ms/azsdk/python/identity/migration." ,
125+ DeprecationWarning
126+ )
127+
128+ azure_cli_core_check_failed = False
129+ try :
130+ import azure .cli .core
131+ minor_version = int (azure .cli .core .__version__ .split ("." )[1 ])
132+ if minor_version >= 21 :
133+ azure_cli_core_check_failed = True
134+ except Exception :
135+ azure_cli_core_check_failed = True
136+
137+ if azure_cli_core_check_failed :
138+ raise NotImplementedError (
139+ "The public API of azure-cli-core has been deprecated starting 2.21.0, " +
140+ "and this method can no longer return a valid credential. " +
141+ "If you need to still use this method, you need to install 'azure-cli-core<2.21.0'. " +
142+ "You may corrupt data if you use current CLI and old azure-cli-core. " +
143+ "See also: https://aka.ms/azsdk/python/identity/migration"
144+ )
145+
97146 profile = get_cli_profile ()
98147 cred , subscription_id , tenant_id = profile .get_login_credentials (resource = resource )
99148 cred = _CliCredentials (profile , resource )
0 commit comments