6
6
7
7
import os .path
8
8
import time
9
+ import warnings
9
10
try :
10
11
from azure .core .credentials import AccessToken as _AccessToken
11
12
except ImportError :
15
16
def get_cli_profile ():
16
17
"""Return a CLI profile class.
17
18
19
+ *Disclaimer*: This method is not working for azure-cli-core>=2.21.0 (released in March 2021).
20
+
18
21
.. versionadded:: 1.1.6
19
22
23
+ .. deprecated:: 1.1.28
24
+
20
25
:return: A CLI Profile
21
26
:rtype: azure.cli.core._profile.Profile
22
27
:raises: ImportError if azure-cli-core package is not available
@@ -27,8 +32,12 @@ def get_cli_profile():
27
32
from azure .cli .core ._session import ACCOUNT
28
33
from azure .cli .core ._environment import get_config_dir
29
34
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
+ )
32
41
33
42
azure_folder = get_config_dir ()
34
43
ACCOUNT .load (os .path .join (azure_folder , 'azureProfile.json' ))
@@ -69,6 +78,7 @@ def get_token(self, *scopes, **kwargs): # pylint:disable=unused-argument
69
78
resource = scope
70
79
71
80
credentials = self ._get_cred (resource )
81
+ # _token_retriever() not accessible after azure-cli-core 2.21.0
72
82
_ , token , fulltoken = credentials ._token_retriever () # pylint:disable=protected-access
73
83
74
84
return _AccessToken (token , int (fulltoken ['expiresIn' ] + time .time ()))
@@ -81,19 +91,58 @@ def signed_session(self, session=None):
81
91
def get_azure_cli_credentials (resource = None , with_tenant = False ):
82
92
"""Return Credentials and default SubscriptionID of current loaded profile of the CLI.
83
93
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:
85
106
https://docs.microsoft.com/cli/azure/authenticate-azure-cli
86
107
87
108
Default subscription ID is either the only one you have, or you can define it:
88
109
https://docs.microsoft.com/cli/azure/manage-azure-subscriptions-azure-cli
89
110
90
111
.. versionadded:: 1.1.6
91
112
113
+ .. deprecated:: 1.1.28
114
+
115
+ .. seealso:: https://aka.ms/azsdk/python/identity/migration
116
+
92
117
:param str resource: The alternative resource for credentials if not ARM (GraphRBac, etc.)
93
118
:param bool with_tenant: If True, return a three-tuple with last as tenant ID
94
119
:return: tuple of Credentials and SubscriptionID (and tenant ID if with_tenant)
95
120
:rtype: tuple
96
121
"""
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
+
97
146
profile = get_cli_profile ()
98
147
cred , subscription_id , tenant_id = profile .get_login_credentials (resource = resource )
99
148
cred = _CliCredentials (profile , resource )
0 commit comments