Skip to content

Commit 91236a1

Browse files
authored
[Profile] az account get-access-token: Allow specifying --tenant with the current tenant (#31869)
1 parent 17459d6 commit 91236a1

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,17 +360,20 @@ def get_raw_token(self, resource=None, scopes=None, subscription=None, tenant=No
360360

361361
managed_identity_type, managed_identity_id = Profile._parse_managed_identity_account(account)
362362

363+
non_current_tenant_template = ("For {} account, getting access token for non-current tenants is not "
364+
"supported. The specified tenant must be the current tenant "
365+
f"{account[_TENANT_ID]}")
363366
if in_cloud_console() and account[_USER_ENTITY].get(_CLOUD_SHELL_ID):
364367
# Cloud Shell
365-
if tenant:
366-
raise CLIError("Tenant shouldn't be specified for Cloud Shell account")
368+
if tenant and tenant != account[_TENANT_ID]:
369+
raise CLIError(non_current_tenant_template.format('Cloud Shell'))
367370
from .auth.msal_credentials import CloudShellCredential
368371
cred = CloudShellCredential()
369372

370373
elif managed_identity_type:
371374
# managed identity
372-
if tenant:
373-
raise CLIError("Tenant shouldn't be specified for managed identity account")
375+
if tenant and tenant != account[_TENANT_ID]:
376+
raise CLIError(non_current_tenant_template.format('managed identity'))
374377
cred = ManagedIdentityAuth.credential_factory(managed_identity_type, managed_identity_id)
375378
if credential_out:
376379
credential_out['credential'] = cred

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,9 +1134,15 @@ def test_get_raw_token_mi_system_assigned(self):
11341134
self.assertEqual(subscription_id, self.test_mi_subscription_id)
11351135
self.assertEqual(tenant_id, self.test_mi_tenant)
11361136

1137-
# verify tenant shouldn't be specified for MSI account
1138-
with self.assertRaisesRegex(CLIError, "Tenant shouldn't be specified"):
1139-
cred, subscription_id, _ = profile.get_raw_token(resource='http://test_resource', tenant=self.tenant_id)
1137+
# Specifying the current tenant is allowed
1138+
cred, subscription_id, tenant_id = profile.get_raw_token(tenant=self.test_mi_tenant)
1139+
self.assertEqual(tenant_id, self.test_mi_tenant)
1140+
1141+
# Specifying a non-current tenant is disallowed
1142+
with self.assertRaisesRegex(CLIError,
1143+
"For managed identity account, getting access token for non-current tenants is "
1144+
"not supported"):
1145+
profile.get_raw_token(tenant='another-tenant')
11401146

11411147
@mock.patch('azure.cli.core.auth.util.now_timestamp', new=now_timestamp_mock)
11421148
@mock.patch('azure.cli.core.auth.msal_credentials.ManagedIdentityCredential', ManagedIdentityCredentialStub)
@@ -1285,9 +1291,15 @@ def cloud_shell_credential_factory():
12851291
self.assertEqual(subscription_id, test_subscription_id)
12861292
self.assertEqual(tenant_id, test_tenant_id)
12871293

1288-
# Verify tenant shouldn't be specified for Cloud Shell account
1289-
with self.assertRaisesRegex(CLIError, 'Cloud Shell'):
1290-
profile.get_raw_token(resource='http://test_resource', tenant=self.tenant_id)
1294+
# Specifying the current tenant is allowed
1295+
cred, subscription_id, tenant_id = profile.get_raw_token(tenant=test_tenant_id)
1296+
self.assertEqual(tenant_id, test_tenant_id)
1297+
1298+
# Specifying a non-current tenant is disallowed
1299+
with self.assertRaisesRegex(CLIError,
1300+
"For Cloud Shell account, getting access token for non-current tenants is "
1301+
"not supported"):
1302+
profile.get_raw_token(tenant='another-tenant')
12911303

12921304
@mock.patch('azure.cli.core.auth.identity.Identity.get_user_credential')
12931305
def test_get_msal_token(self, get_user_credential_mock):

0 commit comments

Comments
 (0)