Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/azure-cli-core/azure/cli/core/_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,17 +360,20 @@ def get_raw_token(self, resource=None, scopes=None, subscription=None, tenant=No

managed_identity_type, managed_identity_id = Profile._parse_managed_identity_account(account)

non_current_tenant_template = ("For {} account, getting access token for non-current tenants is not "
"supported. The specified tenant must be the current tenant "
Comment on lines +363 to +364
Copy link

Copilot AI Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The template string construction is unnecessarily complex and hard to read. Consider using a single f-string or format method call instead of concatenating strings and mixing f-string syntax.

Suggested change
non_current_tenant_template = ("For {} account, getting access token for non-current tenants is not "
"supported. The specified tenant must be the current tenant "
non_current_tenant_template = (f"For {{}} account, getting access token for non-current tenants is not "
f"supported. The specified tenant must be the current tenant "

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is incorrect. Prefixing the substring with f is unnecessary.

f"{account[_TENANT_ID]}")
Copy link

Copilot AI Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The f-string formatting here is redundant since you're just converting a variable to string. You can directly use {account[_TENANT_ID]} in the format method call.

Suggested change
f"{account[_TENANT_ID]}")
"{}").format(account[_TENANT_ID])

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This copilot comment is incorrect. f-string is preferred in this case.

if in_cloud_console() and account[_USER_ENTITY].get(_CLOUD_SHELL_ID):
# Cloud Shell
if tenant:
raise CLIError("Tenant shouldn't be specified for Cloud Shell account")
if tenant and tenant != account[_TENANT_ID]:
raise CLIError(non_current_tenant_template.format('Cloud Shell'))
from .auth.msal_credentials import CloudShellCredential
cred = CloudShellCredential()

elif managed_identity_type:
# managed identity
if tenant:
raise CLIError("Tenant shouldn't be specified for managed identity account")
if tenant and tenant != account[_TENANT_ID]:
raise CLIError(non_current_tenant_template.format('managed identity'))
Comment on lines +375 to +376
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of raising an error, another possible solution is to ignore the specified tenant and just return the access token for the current tenant, but it will result in wrong access token being returned, making it difficult to troubleshoot. I don't agree with this solution.

cred = ManagedIdentityAuth.credential_factory(managed_identity_type, managed_identity_id)
if credential_out:
credential_out['credential'] = cred
Expand Down
24 changes: 18 additions & 6 deletions src/azure-cli-core/azure/cli/core/tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1134,9 +1134,15 @@ def test_get_raw_token_mi_system_assigned(self):
self.assertEqual(subscription_id, self.test_mi_subscription_id)
self.assertEqual(tenant_id, self.test_mi_tenant)

# verify tenant shouldn't be specified for MSI account
with self.assertRaisesRegex(CLIError, "Tenant shouldn't be specified"):
cred, subscription_id, _ = profile.get_raw_token(resource='http://test_resource', tenant=self.tenant_id)
# Specifying the current tenant is allowed
cred, subscription_id, tenant_id = profile.get_raw_token(tenant=self.test_mi_tenant)
self.assertEqual(tenant_id, self.test_mi_tenant)

# Specifying a non-current tenant is disallowed
with self.assertRaisesRegex(CLIError,
"For managed identity account, getting access token for non-current tenants is "
"not supported"):
profile.get_raw_token(tenant='another-tenant')

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

# Verify tenant shouldn't be specified for Cloud Shell account
with self.assertRaisesRegex(CLIError, 'Cloud Shell'):
profile.get_raw_token(resource='http://test_resource', tenant=self.tenant_id)
# Specifying the current tenant is allowed
cred, subscription_id, tenant_id = profile.get_raw_token(tenant=test_tenant_id)
self.assertEqual(tenant_id, test_tenant_id)

# Specifying a non-current tenant is disallowed
with self.assertRaisesRegex(CLIError,
"For Cloud Shell account, getting access token for non-current tenants is "
"not supported"):
profile.get_raw_token(tenant='another-tenant')

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