Skip to content

Commit c824dfb

Browse files
authored
[Core] Drop Track 1 SDK authentication (#29631)
1 parent 01b4428 commit c824dfb

File tree

4 files changed

+18
-77
lines changed

4 files changed

+18
-77
lines changed

src/azure-cli-core/azure/cli/core/_profile.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -385,16 +385,8 @@ def logout_all(self):
385385
identity.logout_all_users()
386386
identity.logout_all_service_principal()
387387

388-
def get_login_credentials(self, resource=None, subscription_id=None, aux_subscriptions=None, aux_tenants=None):
389-
"""Get a CredentialAdaptor instance to be used with both Track 1 and Track 2 SDKs.
390-
391-
:param resource: The resource ID to acquire an access token. Only provide it for Track 1 SDKs.
392-
:param subscription_id:
393-
:param aux_subscriptions:
394-
:param aux_tenants:
395-
"""
396-
resource = resource or self.cli_ctx.cloud.endpoints.active_directory_resource_id
397-
388+
def get_login_credentials(self, subscription_id=None, aux_subscriptions=None, aux_tenants=None):
389+
"""Get a credential compatible with Track 2 SDK."""
398390
if aux_tenants and aux_subscriptions:
399391
raise CLIError("Please specify only one of aux_subscriptions and aux_tenants, not both")
400392

@@ -407,17 +399,21 @@ def get_login_credentials(self, resource=None, subscription_id=None, aux_subscri
407399
from .auth.msal_credentials import CloudShellCredential
408400
from azure.cli.core.auth.credential_adaptor import CredentialAdaptor
409401
# The credential must be wrapped by CredentialAdaptor so that it can work with Track 1 SDKs.
410-
cred = CredentialAdaptor(CloudShellCredential(), resource=resource)
402+
cred = CredentialAdaptor(CloudShellCredential())
411403

412404
elif managed_identity_type:
413405
# managed identity
414406
if _on_azure_arc():
415407
from .auth.msal_credentials import ManagedIdentityCredential
416408
from azure.cli.core.auth.credential_adaptor import CredentialAdaptor
417409
# The credential must be wrapped by CredentialAdaptor so that it can work with Track 1 SDKs.
418-
cred = CredentialAdaptor(ManagedIdentityCredential(), resource=resource)
410+
cred = CredentialAdaptor(ManagedIdentityCredential())
419411
else:
420-
cred = MsiAccountTypes.msi_auth_factory(managed_identity_type, managed_identity_id, resource)
412+
# The resource is merely used by msrestazure to get the first access token.
413+
# It is not actually used in an API invocation.
414+
cred = MsiAccountTypes.msi_auth_factory(
415+
managed_identity_type, managed_identity_id,
416+
self.cli_ctx.cloud.endpoints.active_directory_resource_id)
421417

422418
else:
423419
# user and service principal
@@ -436,9 +432,7 @@ def get_login_credentials(self, resource=None, subscription_id=None, aux_subscri
436432
for external_tenant in external_tenants:
437433
external_credentials.append(self._create_credential(account, tenant_id=external_tenant))
438434
from azure.cli.core.auth.credential_adaptor import CredentialAdaptor
439-
cred = CredentialAdaptor(credential,
440-
auxiliary_credentials=external_credentials,
441-
resource=resource)
435+
cred = CredentialAdaptor(credential, auxiliary_credentials=external_credentials)
442436

443437
return (cred,
444438
str(account[_SUBSCRIPTION_ID]),

src/azure-cli-core/azure/cli/core/auth/credential_adaptor.py

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,69 +3,39 @@
33
# Licensed under the MIT License. See License.txt in the project root for license information.
44
# --------------------------------------------------------------------------------------------
55

6-
import requests
76
from knack.log import get_logger
8-
from knack.util import CLIError
9-
10-
from .util import resource_to_scopes
117

128
logger = get_logger(__name__)
139

1410

1511
class CredentialAdaptor:
16-
def __init__(self, credential, resource=None, auxiliary_credentials=None):
17-
"""
18-
Adaptor to both
19-
- Track 1: msrest.authentication.Authentication, which exposes signed_session
20-
- Track 2: azure.core.credentials.TokenCredential, which exposes get_token
12+
def __init__(self, credential, auxiliary_credentials=None):
13+
"""Cross-tenant credential adaptor. It takes a main credential and auxiliary credentials.
14+
15+
It implements Track 2 SDK's azure.core.credentials.TokenCredential by exposing get_token.
2116
2217
:param credential: Main credential from .msal_authentication
23-
:param resource: AAD resource for Track 1 only
2418
:param auxiliary_credentials: Credentials from .msal_authentication for cross tenant authentication.
2519
Details about cross tenant authentication:
2620
https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/authenticate-multi-tenant
2721
"""
2822

2923
self._credential = credential
3024
self._auxiliary_credentials = auxiliary_credentials
31-
self._resource = resource
32-
33-
def _get_token(self, scopes=None, **kwargs):
34-
external_tenant_tokens = []
35-
# If scopes is not provided, use CLI-managed resource
36-
scopes = scopes or resource_to_scopes(self._resource)
37-
try:
38-
token = self._credential.get_token(*scopes, **kwargs)
39-
if self._auxiliary_credentials:
40-
external_tenant_tokens = [cred.get_token(*scopes) for cred in self._auxiliary_credentials]
41-
return token, external_tenant_tokens
42-
except requests.exceptions.SSLError as err:
43-
from azure.cli.core.util import SSLERROR_TEMPLATE
44-
raise CLIError(SSLERROR_TEMPLATE.format(str(err)))
45-
46-
def signed_session(self, session=None):
47-
logger.debug("CredentialAdaptor.signed_session")
48-
session = session or requests.Session()
49-
token, external_tenant_tokens = self._get_token()
50-
header = "{} {}".format('Bearer', token.token)
51-
session.headers['Authorization'] = header
52-
if external_tenant_tokens:
53-
aux_tokens = ';'.join(['{} {}'.format('Bearer', tokens2.token) for tokens2 in external_tenant_tokens])
54-
session.headers['x-ms-authorization-auxiliary'] = aux_tokens
55-
return session
5625

5726
def get_token(self, *scopes, **kwargs):
27+
"""Get an access token from the main credential."""
5828
logger.debug("CredentialAdaptor.get_token: scopes=%r, kwargs=%r", scopes, kwargs)
5929

6030
# Discard unsupported kwargs: tenant_id, enable_cae
6131
filtered_kwargs = {}
6232
if 'data' in kwargs:
6333
filtered_kwargs['data'] = kwargs['data']
6434

65-
token, _ = self._get_token(scopes, **filtered_kwargs)
66-
return token
35+
return self._credential.get_token(*scopes, **filtered_kwargs)
6736

6837
def get_auxiliary_tokens(self, *scopes, **kwargs):
38+
"""Get access tokens from auxiliary credentials."""
6939
# To test cross-tenant authentication, see https://github.com/Azure/azure-cli/issues/16691
7040
if self._auxiliary_credentials:
7141
return [cred.get_token(*scopes, **kwargs) for cred in self._auxiliary_credentials]

src/azure-cli-core/azure/cli/core/auth/tests/test_credential_adaptor.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/azure-cli-core/azure/cli/core/commands/client_factory.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ def _get_mgmt_service_client(cli_ctx,
213213
subscription_id=None,
214214
api_version=None,
215215
base_url_bound=True,
216-
resource=None,
217216
sdk_profile=None,
218217
aux_subscriptions=None,
219218
aux_tenants=None,
@@ -222,10 +221,6 @@ def _get_mgmt_service_client(cli_ctx,
222221
from azure.cli.core._profile import Profile
223222
logger.debug('Getting management service client client_type=%s', client_type.__name__)
224223

225-
# Track 1 SDK doesn't maintain the `resource`. The `resource` of the token is the one passed to
226-
# get_login_credentials.
227-
resource = resource or cli_ctx.cloud.endpoints.active_directory_resource_id
228-
229224
if credential:
230225
# Use a custom credential
231226
if not subscription_id:
@@ -234,7 +229,7 @@ def _get_mgmt_service_client(cli_ctx,
234229
# Get a credential for the current `az login` context
235230
profile = Profile(cli_ctx=cli_ctx)
236231
credential, subscription_id, _ = profile.get_login_credentials(
237-
subscription_id=subscription_id, resource=resource,
232+
subscription_id=subscription_id,
238233
aux_subscriptions=aux_subscriptions, aux_tenants=aux_tenants)
239234

240235
client_kwargs = {}

0 commit comments

Comments
 (0)