Skip to content

Commit fe0ecc5

Browse files
committed
update
1 parent b4c552d commit fe0ecc5

File tree

4 files changed

+28
-148
lines changed

4 files changed

+28
-148
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ def _prepare_msal_kwargs(options=None):
5757
# Both get_token's kwargs and get_token_info's options are accepted as their schema is the same (at least for now).
5858
msal_kwargs = {}
5959
if options:
60+
# For VM SSH. 'data' support is a CLI-specific extension.
61+
# SDK doesn't support 'data': https://github.com/Azure/azure-sdk-for-python/pull/16397
62+
if 'data' in options:
63+
msal_kwargs['data'] = options['data']
6064
# For CAE
6165
if 'claims' in options:
6266
msal_kwargs['claims_challenge'] = options['claims']

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

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,16 @@ def __init__(self, client_id, username, **kwargs):
4343

4444
self._account = accounts[0]
4545

46-
def acquire_token(self, scopes, claims_challenge=None, data=None, **kwargs):
46+
def acquire_token(self, scopes, claims_challenge=None, **kwargs):
4747
# scopes must be a list.
4848
# For acquiring SSH certificate, scopes is ['https://pas.windows.net/CheckMyAccess/Linux/.default']
49-
# data is only used for acquiring VM SSH certificate. DO NOT use it for other purposes.
5049
# kwargs is already sanitized by CredentialAdaptor, so it can be safely passed to MSAL
51-
logger.debug("UserCredential.acquire_token: scopes=%r, claims_challenge=%r, data=%r, kwargs=%r",
52-
scopes, claims_challenge, data, kwargs)
50+
logger.debug("UserCredential.acquire_token: scopes=%r, claims_challenge=%r, kwargs=%r",
51+
scopes, claims_challenge, kwargs)
5352

5453
if claims_challenge:
5554
logger.warning('Acquiring new access token silently for tenant %s with claims challenge: %s',
5655
self._msal_app.authority.tenant, claims_challenge)
57-
58-
# Only pass data to MSAL if it is set. Passing data=None will cause failure in MSAL:
59-
# AttributeError: 'NoneType' object has no attribute 'get'
60-
if data is not None:
61-
kwargs['data'] = data
62-
6356
result = self._msal_app.acquire_token_silent_with_error(
6457
scopes, self._account, claims_challenge=claims_challenge, **kwargs)
6558

@@ -112,13 +105,8 @@ def __init__(self, client_id, client_credential, **kwargs):
112105
"""
113106
self._msal_app = ConfidentialClientApplication(client_id, client_credential=client_credential, **kwargs)
114107

115-
def acquire_token(self, scopes, data=None, **kwargs):
116-
logger.debug("ServicePrincipalCredential.acquire_token: scopes=%r, data=%r, kwargs=%r",
117-
scopes, data, kwargs)
118-
119-
if data is not None:
120-
kwargs['data'] = data
121-
108+
def acquire_token(self, scopes, **kwargs):
109+
logger.debug("ServicePrincipalCredential.acquire_token: scopes=%r, kwargs=%r", scopes, kwargs)
122110
result = self._msal_app.acquire_token_for_client(scopes, **kwargs)
123111
check_result(result)
124112
return result
@@ -138,13 +126,8 @@ def __init__(self):
138126
# token_cache=...
139127
)
140128

141-
def acquire_token(self, scopes, data=None, **kwargs):
142-
logger.debug("CloudShellCredential.acquire_token: scopes=%r, data=%r, kwargs=%r",
143-
scopes, data, kwargs)
144-
145-
if data is not None:
146-
kwargs['data'] = data
147-
129+
def acquire_token(self, scopes, **kwargs):
130+
logger.debug("CloudShellCredential.acquire_token: scopes=%r, kwargs=%r", scopes, kwargs)
148131
result = self._msal_app.acquire_token_interactive(scopes, prompt="none", **kwargs)
149132
check_result(result, scopes=scopes)
150133
return result
@@ -164,13 +147,8 @@ def __init__(self, client_id=None, resource_id=None, object_id=None):
164147
managed_identity = SystemAssignedManagedIdentity()
165148
self._msal_client = ManagedIdentityClient(managed_identity, http_client=requests.Session())
166149

167-
def acquire_token(self, scopes, data=None, **kwargs):
168-
logger.debug("ManagedIdentityCredential.acquire_token: scopes=%r, data=%r, kwargs=%r",
169-
scopes, data, kwargs)
170-
171-
if data is not None:
172-
from azure.cli.core.azclierror import AuthenticationError
173-
raise AuthenticationError("VM SSH currently doesn't support managed identity.")
150+
def acquire_token(self, scopes, **kwargs):
151+
logger.debug("ManagedIdentityCredential.acquire_token: scopes=%r, kwargs=%r", scopes, kwargs)
174152

175153
from .util import scopes_to_resource
176154
result = self._msal_client.acquire_token_for_client(resource=scopes_to_resource(scopes))

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212

1313
MOCK_ACCESS_TOKEN = "mock_access_token"
14+
MOCK_DATA = {
15+
'key_id': 'test',
16+
'req_cnf': 'test',
17+
'token_type': 'ssh-cert'
18+
}
1419
MOCK_CLAIMS = {"test_claims": "value2"}
1520

1621
class MsalCredentialStub:
@@ -39,7 +44,7 @@ def _now_timestamp_mock():
3944

4045
class TestCredentialAdaptor(unittest.TestCase):
4146

42-
@mock.patch('azure.cli.core.auth.util.now_timestamp', new=_now_timestamp_mock)
47+
@mock.patch('azure.cli.core.auth.util._now_timestamp', new=_now_timestamp_mock)
4348
def test_get_token(self):
4449
msal_cred = MsalCredentialStub()
4550
sdk_cred = CredentialAdaptor(msal_cred)
@@ -51,11 +56,15 @@ def test_get_token(self):
5156
assert access_token.token == MOCK_ACCESS_TOKEN
5257
assert access_token.expires_on == 1630920323
5358

59+
# Note that SDK doesn't support 'data'. This is a CLI-specific extension.
60+
sdk_cred.get_token('https://management.core.windows.net//.default', data=MOCK_DATA)
61+
assert msal_cred.acquire_token_kwargs['data'] == MOCK_DATA
62+
5463
sdk_cred.get_token('https://management.core.windows.net//.default', claims=MOCK_CLAIMS)
5564
assert msal_cred.acquire_token_claims_challenge == MOCK_CLAIMS
5665

5766

58-
@mock.patch('azure.cli.core.auth.util.now_timestamp', new=_now_timestamp_mock)
67+
@mock.patch('azure.cli.core.auth.util._now_timestamp', new=_now_timestamp_mock)
5968
def test_get_token_info(self):
6069
msal_cred = MsalCredentialStub()
6170
sdk_cred = CredentialAdaptor(msal_cred)
@@ -69,6 +78,10 @@ def test_get_token_info(self):
6978

7079
assert msal_cred.acquire_token_scopes == ['https://management.core.windows.net//.default']
7180

81+
# Note that SDK doesn't support 'data'. If 'data' were supported, it should be tested with:
82+
sdk_cred.get_token_info('https://management.core.windows.net//.default', options={'data': MOCK_DATA})
83+
assert msal_cred.acquire_token_kwargs['data'] == MOCK_DATA
84+
7285
sdk_cred.get_token_info('https://management.core.windows.net//.default', options={'claims': MOCK_CLAIMS})
7386
assert msal_cred.acquire_token_claims_challenge == MOCK_CLAIMS
7487

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

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

0 commit comments

Comments
 (0)