Skip to content

Commit 13950c4

Browse files
lmazuelxiangyan99
andauthored
Deprecate CLI factory in azure-common (Azure#21337)
* Deprecate CLI factory in azure-common * Correct syntax * ChangeLog * NotImplementedError * Deprecate all CLI related methods * Rewording * Date change * Typo thanks @annatisch * Typo * Fix tests * Update CHANGELOG.md Co-authored-by: Xiang Yan <[email protected]>
1 parent f363c22 commit 13950c4

File tree

5 files changed

+101
-19
lines changed

5 files changed

+101
-19
lines changed

sdk/core/azure-common/CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Release History
22

3-
## 1.1.28 (Unreleased)
3+
## 1.1.28 (2022-02-03)
4+
5+
- Raise a NotImplementedError if trying to use CLI credentials were CLI version is higher than 2.21.0 #20657 #21313
6+
- Deprecate all methods that needs access to CLI code using azure-cli-core, since this package is no longer importable as public API
47

58
## 1.1.27 (2021-03-23)
69

sdk/core/azure-common/azure/common/client_factory.py

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
import re
1111
import sys
12+
import warnings
1213
try:
1314
from inspect import getfullargspec as get_arg_spec
1415
except ImportError:
@@ -50,7 +51,18 @@ def _client_resource(client_class, cloud):
5051
def get_client_from_cli_profile(client_class, **kwargs):
5152
"""Return a SDK client initialized with current CLI credentials, CLI default subscription and CLI default cloud.
5253
53-
This method will fill automatically the following client parameters:
54+
*Disclaimer*: This is NOT the recommended approach to authenticate with CLI login, this method is deprecated.
55+
use https://pypi.org/project/azure-identity/ and AzureCliCredential instead. See example code below:
56+
57+
.. code:: python
58+
59+
from azure.identity import AzureCliCredential
60+
from azure.mgmt.compute import ComputeManagementClient
61+
client = ComputeManagementClient(AzureCliCredential(), subscription_id)
62+
63+
This method is not working for azure-cli-core>=2.21.0 (released in March 2021).
64+
65+
For compatible azure-cli-core (< 2.20.0), This method will automatically fill the following client parameters:
5466
- credentials/credential
5567
- subscription_id
5668
- base_url
@@ -67,10 +79,20 @@ def get_client_from_cli_profile(client_class, **kwargs):
6779
6880
.. versionadded:: 1.1.6
6981
82+
.. deprecated:: 1.1.28
83+
84+
.. seealso:: https://aka.ms/azsdk/python/identity/migration
85+
7086
:param client_class: A SDK client class
7187
:return: An instantiated client
7288
:raises: ImportError if azure-cli-core package is not available
7389
"""
90+
warnings.warn(
91+
"get_client_from_cli_profile is deprecated, please use azure-identity and AzureCliCredential instead. "+
92+
"See also https://aka.ms/azsdk/python/identity/migration.",
93+
DeprecationWarning
94+
)
95+
7496
cloud = get_cli_active_cloud()
7597
parameters = {}
7698
no_credential_sentinel = object()
@@ -120,11 +142,7 @@ def _is_autorest_v3(client_class):
120142
def get_client_from_json_dict(client_class, config_dict, **kwargs):
121143
"""Return a SDK client initialized with a JSON auth dict.
122144
123-
The easiest way to obtain this content is to call the following CLI commands:
124-
125-
.. code:: bash
126-
127-
az ad sp create-for-rbac --sdk-auth
145+
*Disclaimer*: This is NOT the recommended approach, see https://aka.ms/azsdk/python/identity/migration for guidance.
128146
129147
This method will fill automatically the following client parameters:
130148
- credentials
@@ -156,14 +174,18 @@ def get_client_from_json_dict(client_class, config_dict, **kwargs):
156174
157175
.. versionadded:: 1.1.7
158176
177+
.. deprecated:: 1.1.28
178+
179+
.. seealso:: https://aka.ms/azsdk/python/identity/migration
180+
159181
:param client_class: A SDK client class
160182
:param dict config_dict: A config dict.
161183
:return: An instantiated client
162184
"""
163185
if _is_autorest_v3(client_class):
164186
raise ValueError(
165187
"Auth file or JSON dict are deprecated auth approach and are not supported anymore. "
166-
"Please read https://aka.ms/azsdk/python/azidmigration for details"
188+
"See also https://aka.ms/azsdk/python/identity/migration."
167189
)
168190

169191
import adal
@@ -215,11 +237,7 @@ def get_client_from_json_dict(client_class, config_dict, **kwargs):
215237
def get_client_from_auth_file(client_class, auth_path=None, **kwargs):
216238
"""Return a SDK client initialized with auth file.
217239
218-
The easiest way to obtain this file is to call the following CLI commands:
219-
220-
.. code:: bash
221-
222-
az ad sp create-for-rbac --sdk-auth
240+
*Disclaimer*: This is NOT the recommended approach, see https://aka.ms/azsdk/python/identity/migration for guidance.
223241
224242
You can specific the file path directly, or fill the environment variable AZURE_AUTH_LOCATION.
225243
File must be UTF-8.
@@ -258,6 +276,10 @@ def get_client_from_auth_file(client_class, auth_path=None, **kwargs):
258276
259277
.. versionadded:: 1.1.7
260278
279+
.. deprecated:: 1.1.28
280+
281+
.. seealso:: https://aka.ms/azsdk/python/identity/migration
282+
261283
:param client_class: A SDK client class
262284
:param str auth_path: Path to the file.
263285
:return: An instantiated client

sdk/core/azure-common/azure/common/cloud.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
def get_cli_active_cloud():
88
"""Return a CLI active cloud.
99
10+
*Disclaimer*: This method is not working for azure-cli-core>=2.21.0 (released in March 2021).
11+
1012
.. versionadded:: 1.1.6
1113
14+
.. deprecated:: 1.1.28
15+
1216
:return: A CLI Cloud
1317
:rtype: azure.cli.core.cloud.Cloud
1418
:raises: ImportError if azure-cli-core package is not available
@@ -17,6 +21,10 @@ def get_cli_active_cloud():
1721
try:
1822
from azure.cli.core.cloud import get_active_cloud
1923
except ImportError:
20-
raise ImportError("You need to install 'azure-cli-core' to load CLI active Cloud")
24+
raise ImportError(
25+
"The public API of azure-cli-core has been deprecated starting 2.21.0, " +
26+
"and this method no longer can return a cloud instance. " +
27+
"If you want to use this method, you need to install 'azure-cli-core<2.21.0'. " +
28+
"You may corrupt data if you use current CLI and old azure-cli-core."
29+
)
2130
return get_active_cloud()
22-

sdk/core/azure-common/azure/common/credentials.py

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import os.path
88
import time
9+
import warnings
910
try:
1011
from azure.core.credentials import AccessToken as _AccessToken
1112
except ImportError:
@@ -15,8 +16,12 @@
1516
def 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):
8191
def 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)

sdk/core/azure-common/tests/test_client_factory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def __init__(self, credential):
290290

291291
with pytest.raises(ValueError) as excinfo:
292292
get_client_from_auth_file(KeyVaultClientTrack2, temp_auth_file.name)
293-
assert "https://aka.ms/azsdk/python/azidmigration" in str(excinfo.value)
293+
assert "https://aka.ms/azsdk/python/identity/migration" in str(excinfo.value)
294294

295295
os.unlink(temp_auth_file.name)
296296

0 commit comments

Comments
 (0)