Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow credential file to not set subscription_id and use the param sp… #1380

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
22 changes: 12 additions & 10 deletions plugins/module_utils/azure_rm_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1514,7 +1514,7 @@ def _get_env(self, module_key, default=None):
"Read envvar matching module parameter"
return os.environ.get(AZURE_CREDENTIAL_ENV_MAPPING[module_key], default)

def _get_profile(self, profile="default"):
def _get_profile(self, profile="default", subscription_id=None):
path = expanduser("~/.azure/credentials")
try:
config = configparser.ConfigParser()
Expand All @@ -1529,8 +1529,10 @@ def _get_profile(self, profile="default"):
except Exception:
pass

if credentials.get('subscription_id'):
return credentials
if credentials.get('subscription_id') is None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

The profile will be None if credentials.get('subscription_id') not None. credentials.get('subscription_id') and subscription_id which one is the first priority?

Copy link
Author

Choose a reason for hiding this comment

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

In order to not change the behavior I give credentials.get('subscription_id') the first priority

Copy link
Collaborator

Choose a reason for hiding this comment

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

credentials.get('subscription_id') not None not handled, profile will returns as None

Copy link
Collaborator

@xuzhang3 xuzhang3 Mar 22, 2024

Choose a reason for hiding this comment

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

@is-consulting ping credentials.get('subscription_id') not None not handled.

Suggested change
if credentials.get('subscription_id') is None:
if credentials.get('subscription_id') is None:
...
else:
return credentials

if subscription_id is not None:
credentials['subscription_id'] = subscription_id
return credentials

return None

Expand Down Expand Up @@ -1594,13 +1596,13 @@ def _get_azure_cli_credentials(self, subscription_id=None, resource=None):
}
return cli_credentials

def _get_env_credentials(self):
def _get_env_credentials(self, subscription_id=None):
env_credentials = dict()
for attribute, env_variable in AZURE_CREDENTIAL_ENV_MAPPING.items():
env_credentials[attribute] = os.environ.get(env_variable, None)

if env_credentials['profile']:
credentials = self._get_profile(env_credentials['profile'])
credentials = self._get_profile(env_credentials['profile'], subscription_id=subscription_id)
return credentials

if env_credentials.get('subscription_id') is not None:
Expand Down Expand Up @@ -1634,34 +1636,34 @@ def _get_credentials(self, auth_source=None, **params):

if auth_source == 'env':
self.log('Retrieving credentials from environment')
env_credentials = self._get_env_credentials()
env_credentials = self._get_env_credentials(params.get('subscription_id'))
return env_credentials

if auth_source == 'credential_file':
self.log("Retrieving credentials from credential file")
profile = params.get('profile') or 'default'
default_credentials = self._get_profile(profile)
default_credentials = self._get_profile(profile, subscription_id=params.get('subscription_id'))
return default_credentials

# auto, precedence: module parameters -> environment variables -> default profile in ~/.azure/credentials -> azure cli
# try module params
if arg_credentials['profile'] is not None:
self.log('Retrieving credentials with profile parameter.')
credentials = self._get_profile(arg_credentials['profile'])
credentials = self._get_profile(arg_credentials['profile'], subscription_id=params.get('subscription_id'))
return credentials

if arg_credentials['client_id'] or arg_credentials['ad_user']:
self.log('Received credentials from parameters.')
return arg_credentials

# try environment
env_credentials = self._get_env_credentials()
env_credentials = self._get_env_credentials(params.get('subscription_id'))
if env_credentials:
self.log('Received credentials from env.')
return env_credentials

# try default profile from ~./azure/credentials
default_credentials = self._get_profile()
default_credentials = self._get_profile(subscription_id=params.get('subscription_id'))
if default_credentials:
self.log('Retrieved default profile credentials from ~/.azure/credentials.')
return default_credentials
Expand Down