Skip to content

Commit 1752328

Browse files
committed
track1-auth
1 parent cf83ff0 commit 1752328

4 files changed

Lines changed: 17 additions & 67 deletions

File tree

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -385,16 +385,13 @@ 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):
388+
def get_login_credentials(self, subscription_id=None, aux_subscriptions=None, aux_tenants=None):
389389
"""Get a CredentialAdaptor instance to be used with both Track 1 and Track 2 SDKs.
390390
391-
:param resource: The resource ID to acquire an access token. Only provide it for Track 1 SDKs.
392391
:param subscription_id:
393392
:param aux_subscriptions:
394393
:param aux_tenants:
395394
"""
396-
resource = resource or self.cli_ctx.cloud.endpoints.active_directory_resource_id
397-
398395
if aux_tenants and aux_subscriptions:
399396
raise CLIError("Please specify only one of aux_subscriptions and aux_tenants, not both")
400397

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

412409
elif managed_identity_type:
413410
# managed identity
414411
if _on_azure_arc():
415412
from .auth.msal_credentials import ManagedIdentityCredential
416413
from azure.cli.core.auth.credential_adaptor import CredentialAdaptor
417414
# The credential must be wrapped by CredentialAdaptor so that it can work with Track 1 SDKs.
418-
cred = CredentialAdaptor(ManagedIdentityCredential(), resource=resource)
415+
cred = CredentialAdaptor(ManagedIdentityCredential())
419416
else:
420-
cred = MsiAccountTypes.msi_auth_factory(managed_identity_type, managed_identity_id, resource)
417+
# The resource is merely used by msrestazure to get the first access token.
418+
# It is not actually used in an API invocation.
419+
cred = MsiAccountTypes.msi_auth_factory(
420+
managed_identity_type, managed_identity_id,
421+
self.cli_ctx.cloud.endpoints.active_directory_resource_id)
421422

422423
else:
423424
# user and service principal
@@ -436,9 +437,7 @@ def get_login_credentials(self, resource=None, subscription_id=None, aux_subscri
436437
for external_tenant in external_tenants:
437438
external_credentials.append(self._create_credential(account, tenant_id=external_tenant))
438439
from azure.cli.core.auth.credential_adaptor import CredentialAdaptor
439-
cred = CredentialAdaptor(credential,
440-
auxiliary_credentials=external_credentials,
441-
resource=resource)
440+
cred = CredentialAdaptor(credential, auxiliary_credentials=external_credentials)
442441

443442
return (cred,
444443
str(account[_SUBSCRIPTION_ID]),

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

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,59 +13,33 @@
1313

1414

1515
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
16+
def __init__(self, credential, auxiliary_credentials=None):
17+
"""Cross-tenant credential adaptor. It takes a main credential and auxiliary credentials.
18+
19+
It implements Track 2 SDK's azure.core.credentials.TokenCredential by exposing get_token.
2120
2221
:param credential: Main credential from .msal_authentication
23-
:param resource: AAD resource for Track 1 only
2422
:param auxiliary_credentials: Credentials from .msal_authentication for cross tenant authentication.
2523
Details about cross tenant authentication:
2624
https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/authenticate-multi-tenant
2725
"""
2826

2927
self._credential = credential
3028
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
5629

5730
def get_token(self, *scopes, **kwargs):
31+
"""Get an access token from the main credential."""
5832
logger.debug("CredentialAdaptor.get_token: scopes=%r, kwargs=%r", scopes, kwargs)
5933

6034
# Discard unsupported kwargs: tenant_id, enable_cae
6135
filtered_kwargs = {}
6236
if 'data' in kwargs:
6337
filtered_kwargs['data'] = kwargs['data']
6438

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

6841
def get_auxiliary_tokens(self, *scopes, **kwargs):
42+
"""Get access tokens from auxiliary credentials."""
6943
# To test cross-tenant authentication, see https://github.com/Azure/azure-cli/issues/16691
7044
if self._auxiliary_credentials:
7145
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)