Skip to content

Commit bc6c4d9

Browse files
authored
{Core} Decouple get_raw_token from SDK token protocol (#31063)
1 parent 1ee4df8 commit bc6c4d9

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ def get_raw_token(self, resource=None, scopes=None, subscription=None, tenant=No
365365
if tenant:
366366
raise CLIError("Tenant shouldn't be specified for Cloud Shell account")
367367
from .auth.msal_credentials import CloudShellCredential
368-
sdk_cred = CredentialAdaptor(CloudShellCredential())
368+
cred = CloudShellCredential()
369369

370370
elif managed_identity_type:
371371
# managed identity
@@ -374,25 +374,27 @@ def get_raw_token(self, resource=None, scopes=None, subscription=None, tenant=No
374374
cred = ManagedIdentityAuth.credential_factory(managed_identity_type, managed_identity_id)
375375
if credential_out:
376376
credential_out['credential'] = cred
377-
sdk_cred = CredentialAdaptor(cred)
378377

379378
else:
380-
sdk_cred = CredentialAdaptor(self._create_credential(account, tenant_id=tenant))
379+
cred = self._create_credential(account, tenant_id=tenant)
381380

382-
sdk_token = sdk_cred.get_token(*scopes)
381+
msal_token = cred.acquire_token(scopes)
383382
# Convert epoch int 'expires_on' to datetime string 'expiresOn' for backward compatibility
384383
# WARNING: expiresOn is deprecated and will be removed in future release.
385384
import datetime
386-
expiresOn = datetime.datetime.fromtimestamp(sdk_token.expires_on).strftime("%Y-%m-%d %H:%M:%S.%f")
385+
from .auth.util import now_timestamp
386+
from .auth.constants import EXPIRES_IN, ACCESS_TOKEN
387+
expires_on = now_timestamp() + msal_token[EXPIRES_IN]
388+
expiresOn = datetime.datetime.fromtimestamp(expires_on).strftime("%Y-%m-%d %H:%M:%S.%f")
387389

388390
token_entry = {
389-
'accessToken': sdk_token.token,
390-
'expires_on': sdk_token.expires_on, # epoch int, like 1605238724
391+
'accessToken': msal_token[ACCESS_TOKEN],
392+
'expires_on': expires_on, # epoch int, like 1605238724
391393
'expiresOn': expiresOn # datetime string, like "2020-11-12 13:50:47.114324"
392394
}
393395

394396
# Build a tuple of (token_type, token, token_entry)
395-
token_tuple = 'Bearer', sdk_token.token, token_entry
397+
token_tuple = 'Bearer', msal_token[ACCESS_TOKEN], token_entry
396398

397399
# Return a tuple of (token_tuple, subscription, tenant)
398400
return (token_tuple,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def _build_sdk_access_token_info(token_entry):
7676
# 'token_source': 'cache'
7777
# }
7878
from .constants import ACCESS_TOKEN, EXPIRES_IN
79-
from .util import _now_timestamp
79+
from .util import now_timestamp
8080
from azure.core.credentials import AccessTokenInfo
8181

82-
return AccessTokenInfo(token_entry[ACCESS_TOKEN], _now_timestamp() + token_entry[EXPIRES_IN])
82+
return AccessTokenInfo(token_entry[ACCESS_TOKEN], now_timestamp() + token_entry[EXPIRES_IN])

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def build_sdk_access_token(token_entry):
151151
# This can slow down commands that doesn't need azure.core, like `az account get-access-token`.
152152
# So We define our own AccessToken.
153153
from .constants import ACCESS_TOKEN, EXPIRES_IN
154-
return AccessToken(token_entry[ACCESS_TOKEN], _now_timestamp() + token_entry[EXPIRES_IN])
154+
return AccessToken(token_entry[ACCESS_TOKEN], now_timestamp() + token_entry[EXPIRES_IN])
155155

156156

157157
def decode_access_token(access_token):
@@ -177,6 +177,6 @@ def read_response_templates():
177177
return success_template, error_template
178178

179179

180-
def _now_timestamp():
180+
def now_timestamp():
181181
import time
182182
return int(time.time())

src/azure-cli-core/azure/cli/core/tests/test_profile.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def _build_test_jwt(claims):
3939
return '.'.join(base64.urlsafe_b64encode(p.encode('utf-8')).decode('utf-8').replace('=', '') for p in parts)
4040

4141

42-
def _now_timestamp_mock():
42+
def now_timestamp_mock():
4343
# 2021-09-06 08:55:23
4444
return 1630918523
4545

@@ -1013,7 +1013,7 @@ def test_get_login_credentials_mi_user_assigned_resource_id(self):
10131013
assert cred._credential.object_id is None
10141014
assert cred._credential.resource_id == self.test_mi_resource_id
10151015

1016-
@mock.patch('azure.cli.core.auth.util._now_timestamp', new=_now_timestamp_mock)
1016+
@mock.patch('azure.cli.core.auth.util.now_timestamp', new=now_timestamp_mock)
10171017
@mock.patch('azure.cli.core.auth.identity.Identity.get_user_credential')
10181018
def test_get_raw_token(self, get_user_credential_mock):
10191019
credential_mock_temp = MsalCredentialStub()
@@ -1061,7 +1061,7 @@ def test_get_raw_token(self, get_user_credential_mock):
10611061
self.assertIsNone(sub)
10621062
self.assertEqual(tenant, self.tenant_id)
10631063

1064-
@mock.patch('azure.cli.core.auth.util._now_timestamp', new=_now_timestamp_mock)
1064+
@mock.patch('azure.cli.core.auth.util.now_timestamp', new=now_timestamp_mock)
10651065
@mock.patch('azure.cli.core.auth.identity.Identity.get_service_principal_credential')
10661066
def test_get_raw_token_for_sp(self, get_service_principal_credential_mock):
10671067
credential_mock_temp = MsalCredentialStub()
@@ -1102,7 +1102,7 @@ def test_get_raw_token_for_sp(self, get_service_principal_credential_mock):
11021102
self.assertIsNone(sub)
11031103
self.assertEqual(tenant, self.tenant_id)
11041104

1105-
@mock.patch('azure.cli.core.auth.util._now_timestamp', new=_now_timestamp_mock)
1105+
@mock.patch('azure.cli.core.auth.util.now_timestamp', new=now_timestamp_mock)
11061106
@mock.patch('azure.cli.core.auth.msal_credentials.ManagedIdentityCredential', ManagedIdentityCredentialStub)
11071107
def test_get_raw_token_mi_system_assigned(self):
11081108
profile = Profile(cli_ctx=DummyCli(), storage={'subscriptions': None})
@@ -1136,7 +1136,7 @@ def test_get_raw_token_mi_system_assigned(self):
11361136
with self.assertRaisesRegex(CLIError, "Tenant shouldn't be specified"):
11371137
cred, subscription_id, _ = profile.get_raw_token(resource='http://test_resource', tenant=self.tenant_id)
11381138

1139-
@mock.patch('azure.cli.core.auth.util._now_timestamp', new=_now_timestamp_mock)
1139+
@mock.patch('azure.cli.core.auth.util.now_timestamp', new=now_timestamp_mock)
11401140
@mock.patch('azure.cli.core.auth.msal_credentials.ManagedIdentityCredential', ManagedIdentityCredentialStub)
11411141
def test_get_raw_token_mi_user_assigned_client_id(self):
11421142
profile = Profile(cli_ctx=DummyCli(), storage={'subscriptions': None})
@@ -1167,7 +1167,7 @@ def test_get_raw_token_mi_user_assigned_client_id(self):
11671167
self.assertEqual(subscription_id, self.test_mi_subscription_id)
11681168
self.assertEqual(tenant_id, self.test_mi_tenant)
11691169

1170-
@mock.patch('azure.cli.core.auth.util._now_timestamp', new=_now_timestamp_mock)
1170+
@mock.patch('azure.cli.core.auth.util.now_timestamp', new=now_timestamp_mock)
11711171
@mock.patch('azure.cli.core.auth.msal_credentials.ManagedIdentityCredential', ManagedIdentityCredentialStub)
11721172
def test_get_raw_token_mi_user_assigned_object_id(self):
11731173
profile = Profile(cli_ctx=DummyCli(), storage={'subscriptions': None})
@@ -1198,7 +1198,7 @@ def test_get_raw_token_mi_user_assigned_object_id(self):
11981198
self.assertEqual(subscription_id, self.test_mi_subscription_id)
11991199
self.assertEqual(tenant_id, self.test_mi_tenant)
12001200

1201-
@mock.patch('azure.cli.core.auth.util._now_timestamp', new=_now_timestamp_mock)
1201+
@mock.patch('azure.cli.core.auth.util.now_timestamp', new=now_timestamp_mock)
12021202
@mock.patch('azure.cli.core.auth.msal_credentials.ManagedIdentityCredential', ManagedIdentityCredentialStub)
12031203
def test_get_raw_token_mi_user_assigned_resource_id(self):
12041204
profile = Profile(cli_ctx=DummyCli(), storage={'subscriptions': None})
@@ -1229,7 +1229,7 @@ def test_get_raw_token_mi_user_assigned_resource_id(self):
12291229
self.assertEqual(subscription_id, self.test_mi_subscription_id)
12301230
self.assertEqual(tenant_id, self.test_mi_tenant)
12311231

1232-
@mock.patch('azure.cli.core.auth.util._now_timestamp', new=_now_timestamp_mock)
1232+
@mock.patch('azure.cli.core.auth.util.now_timestamp', new=now_timestamp_mock)
12331233
@mock.patch('azure.cli.core._profile.in_cloud_console', autospec=True)
12341234
@mock.patch('azure.cli.core.auth.msal_credentials.CloudShellCredential', autospec=True)
12351235
def test_get_raw_token_in_cloud_shell(self, cloud_shell_credential_mock, mock_in_cloud_console):

0 commit comments

Comments
 (0)