Skip to content

Commit 4d5bf3b

Browse files
committed
track1-auth
1 parent d3d7dea commit 4d5bf3b

File tree

5 files changed

+13
-68
lines changed

5 files changed

+13
-68
lines changed

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -328,18 +328,14 @@ def logout_all(self):
328328
identity.logout_all_users()
329329
identity.logout_all_service_principal()
330330

331-
def get_login_credentials(self, resource=None, client_id=None, subscription_id=None, aux_subscriptions=None,
332-
aux_tenants=None):
331+
def get_login_credentials(self, client_id=None, subscription_id=None, aux_subscriptions=None, aux_tenants=None):
333332
"""Get a CredentialAdaptor instance to be used with both Track 1 and Track 2 SDKs.
334333
335-
:param resource: The resource ID to acquire an access token. Only provide it for Track 1 SDKs.
336334
:param client_id:
337335
:param subscription_id:
338336
:param aux_subscriptions:
339337
:param aux_tenants:
340338
"""
341-
resource = resource or self.cli_ctx.cloud.endpoints.active_directory_resource_id
342-
343339
if aux_tenants and aux_subscriptions:
344340
raise CLIError("Please specify only one of aux_subscriptions and aux_tenants, not both")
345341

@@ -368,11 +364,10 @@ def get_login_credentials(self, resource=None, client_id=None, subscription_id=N
368364
for external_tenant in external_tenants:
369365
external_credentials.append(self._create_credential(account, external_tenant, client_id=client_id))
370366
from azure.cli.core.auth.credential_adaptor import CredentialAdaptor
371-
cred = CredentialAdaptor(credential,
372-
auxiliary_credentials=external_credentials,
373-
resource=resource)
367+
cred = CredentialAdaptor(credential, auxiliary_credentials=external_credentials)
374368
else:
375369
# managed identity
370+
# TODO: Migrate MSIAuthentication to MSAL
376371
cred = MsiAccountTypes.msi_auth_factory(managed_identity_type, managed_identity_id, resource)
377372
return (cred,
378373
str(account[_SUBSCRIPTION_ID]),

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

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,69 +3,42 @@
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
97

10-
from .util import resource_to_scopes, _normalize_scopes
8+
from .util import _normalize_scopes
119

1210
logger = get_logger(__name__)
1311

1412

1513
class CredentialAdaptor:
16-
def __init__(self, credential, resource=None, auxiliary_credentials=None):
14+
def __init__(self, credential, auxiliary_credentials=None):
1715
"""
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+
Cross-tenant credential adaptor. It takes a main credential and auxiliary credentials.
17+
18+
It implements Track 2 SDK's azure.core.credentials.TokenCredential by exposing get_token.
2119
2220
:param credential: Main credential from .msal_authentication
23-
:param resource: AAD resource for Track 1 only
2421
:param auxiliary_credentials: Credentials from .msal_authentication for cross tenant authentication.
2522
Details about cross tenant authentication:
2623
https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/authenticate-multi-tenant
2724
"""
2825

2926
self._credential = credential
3027
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
5628

5729
def get_token(self, *scopes, **kwargs):
30+
"""Get an access token from the main credential."""
5831
logger.debug("CredentialAdaptor.get_token: scopes=%r, kwargs=%r", scopes, kwargs)
5932

6033
# SDK azure-keyvault-keys 4.5.0b5 passes tenant_id as kwargs, but we don't support tenant_id for now,
6134
# so discard it.
6235
kwargs.pop('tenant_id', None)
6336

6437
scopes = _normalize_scopes(scopes)
65-
token, _ = self._get_token(scopes, **kwargs)
66-
return token
38+
return self._credential.get_token(*scopes, **kwargs)
6739

6840
def get_auxiliary_tokens(self, *scopes, **kwargs):
41+
"""Get access tokens from auxiliary credentials."""
6942
# To test cross-tenant authentication, see https://github.com/Azure/azure-cli/issues/16691
7043
if self._auxiliary_credentials:
7144
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/auth/util.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ def scopes_to_resource(scopes):
9797

9898

9999
def _normalize_scopes(scopes):
100+
# TODO: Drop this function
100101
"""Normalize scopes to workaround some SDK issues."""
101102

102103
# Track 2 SDKs generated before https://github.com/Azure/autorest.python/pull/239 don't maintain

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

Lines changed: 1 addition & 7 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,8 +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,
238-
aux_subscriptions=aux_subscriptions, aux_tenants=aux_tenants)
232+
subscription_id=subscription_id, aux_subscriptions=aux_subscriptions, aux_tenants=aux_tenants)
239233

240234
client_kwargs = {}
241235
if base_url_bound:

0 commit comments

Comments
 (0)