Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[Core] Consider env var auth before cached auth
  • Loading branch information
VoxSecundus committed Jun 24, 2025
commit f54f6991f9d1106f9603bd9a34189246975c6b38
32 changes: 17 additions & 15 deletions src/azure-cli-core/azure/cli/core/_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,23 @@ def get_current_account_user(self):
return active_account[_USER_ENTITY][_USER_NAME]

def get_subscription(self, subscription=None, allow_null_subscription=False): # take id or name
# Attempt to use env vars
if env_var_auth_configured():
logger.debug("Using subscription configured in environment variables.")
env_var_sub = load_env_var_subscription()
if subscription:
# Subscription ID must be a GUID
assert_guid(subscription, _AZURE_SUBSCRIPTION_ID)
# Overwrite env var subscription if given as argument to get_subscription()
env_var_sub[_SUBSCRIPTION_ID] = subscription

if not env_var_sub[_SUBSCRIPTION_ID] and not allow_null_subscription:
error = """Subscription is undefined.
Please specific the subscription ID with either {} or --subscription."""
raise CLIError(error.format(_AZURE_SUBSCRIPTION_ID))
return env_var_sub

# Attempt to use cached subscriptions
subscriptions = self.load_cached_subscriptions()

if subscriptions:
Expand All @@ -611,21 +628,6 @@ def get_subscription(self, subscription=None, allow_null_subscription=False): #
"Specify the subscription ID.".format(subscription))
return result[0]

# Attempt to use env vars
if env_var_auth_configured():
logger.debug("Using subscription configured in environment variables.")
env_var_sub = load_env_var_subscription()
if subscription:
# Subscription ID must be a GUID
assert_guid(subscription, _AZURE_SUBSCRIPTION_ID)
# Overwrite env var subscription if given as argument to get_subscription()
env_var_sub[_SUBSCRIPTION_ID] = subscription

if not env_var_sub[_SUBSCRIPTION_ID] and not allow_null_subscription:
error = """Subscription is undefined.
Please specific the subscription ID with either {} or --subscription."""
raise CLIError(error.format(_AZURE_SUBSCRIPTION_ID))
return env_var_sub
raise CLIError(_AZ_LOGIN_MESSAGE)

def get_subscription_id(self, subscription=None): # take id or name
Expand Down
11 changes: 6 additions & 5 deletions src/azure-cli/azure/cli/command_modules/profile/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ def list_subscriptions(cmd, all=False, refresh=False): # pylint: disable=redefi
from azure.cli.core.api import load_subscriptions
from azure.cli.core._profile import load_env_var_subscription, env_var_auth_configured

# Attempt to fetch subscription configured in environment variables
if env_var_auth_configured():
logger.warning("Fetching subscription configured in environment variables.")
subscriptions = [load_env_var_subscription()]
return subscriptions

subscriptions = load_subscriptions(cmd.cli_ctx, all_clouds=all, refresh=refresh)

if subscriptions:
Expand All @@ -48,11 +54,6 @@ def list_subscriptions(cmd, all=False, refresh=False): # pylint: disable=redefi
subscriptions = enabled_ones
return subscriptions

# If logged out, fetch subscription configured in environment variables
if env_var_auth_configured():
logger.warning("Fetching subscription configured in environment variables.")
subscriptions = [load_env_var_subscription()]
return subscriptions

if not subscriptions:
logger.warning('Please run "az login" to access your accounts.')
Expand Down
Loading