-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Performance: avoid loading expensive modules until needed #2871
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
Changes from 7 commits
4aef1a4
d87a60e
af1e90a
e8b993a
7337dd9
9652f95
dce8f81
925ff1c
7fcebae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,9 +95,10 @@ class CredentialType(Enum): # pylint: disable=too-few-public-methods | |
| class Profile(object): | ||
| def __init__(self, storage=None, auth_ctx_factory=None): | ||
| self._storage = storage or ACCOUNT | ||
| factory = auth_ctx_factory or _AUTH_CTX_FACTORY | ||
| self._creds_cache = CredsCache(factory) | ||
| self._subscription_finder = SubscriptionFinder(factory, self._creds_cache.adal_token_cache) | ||
| self.auth_ctx_factory = auth_ctx_factory or _AUTH_CTX_FACTORY | ||
| self._creds_cache = CredsCache(self.auth_ctx_factory) | ||
| self._management_resource_uri = CLOUD.endpoints.management | ||
| self._subscription_finder_attr = None | ||
| self._ad_resource_uri = CLOUD.endpoints.active_directory_resource_id | ||
|
|
||
| def find_subscriptions_on_login(self, # pylint: disable=too-many-arguments | ||
|
|
@@ -110,17 +111,17 @@ def find_subscriptions_on_login(self, # pylint: disable=too-many-arguments | |
| allow_debug_adal_connection() | ||
| subscriptions = [] | ||
| if interactive: | ||
| subscriptions = self._subscription_finder.find_through_interactive_flow( | ||
| subscriptions = self.subscription_finder.find_through_interactive_flow( | ||
| tenant, self._ad_resource_uri) | ||
| else: | ||
| if is_service_principal: | ||
| if not tenant: | ||
| raise CLIError('Please supply tenant using "--tenant"') | ||
| sp_auth = ServicePrincipalAuth(password) | ||
| subscriptions = self._subscription_finder.find_from_service_principal_id( | ||
| subscriptions = self.subscription_finder.find_from_service_principal_id( | ||
| username, sp_auth, tenant, self._ad_resource_uri) | ||
| else: | ||
| subscriptions = self._subscription_finder.find_from_user_account( | ||
| subscriptions = self.subscription_finder.find_from_user_account( | ||
| username, password, tenant, self._ad_resource_uri) | ||
|
|
||
| if not subscriptions: | ||
|
|
@@ -132,13 +133,20 @@ def find_subscriptions_on_login(self, # pylint: disable=too-many-arguments | |
|
|
||
| if self._creds_cache.adal_token_cache.has_state_changed: | ||
| self._creds_cache.persist_cached_creds() | ||
| consolidated = Profile._normalize_properties(self._subscription_finder.user_id, | ||
| consolidated = Profile._normalize_properties(self.subscription_finder.user_id, | ||
| subscriptions, | ||
| is_service_principal) | ||
| self._set_subscriptions(consolidated) | ||
| # use deepcopy as we don't want to persist these changes to file. | ||
| return deepcopy(consolidated) | ||
|
|
||
| @property | ||
| def subscription_finder(self): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And we don't need this one any more if you agree with my previous comment
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| if self._subscription_finder_attr is None: | ||
| self._subscription_finder_attr = SubscriptionFinder(self.auth_ctx_factory, | ||
| self._creds_cache.adal_token_cache) | ||
| return self._subscription_finder_attr | ||
|
|
||
| @staticmethod | ||
| def _normalize_properties(user, subscriptions, is_service_principal): | ||
| consolidated = [] | ||
|
|
@@ -256,6 +264,9 @@ def get_subscription(self, subscription=None): # take id or name | |
| raise CLIError("Please run 'az account set' to select active account.") | ||
| return result[0] | ||
|
|
||
| def get_subscription_id(self): | ||
| return self.get_subscription()[_SUBSCRIPTION_ID] | ||
|
|
||
| def get_login_credentials(self, resource=CLOUD.endpoints.active_directory_resource_id, | ||
| subscription_id=None): | ||
| account = self.get_subscription(subscription_id) | ||
|
|
@@ -428,8 +439,7 @@ def __init__(self, auth_ctx_factory=None): | |
| os.path.join(get_config_dir(), 'accessTokens.json')) | ||
| self._service_principal_creds = [] | ||
| self._auth_ctx_factory = auth_ctx_factory or _AUTH_CTX_FACTORY | ||
| self.adal_token_cache = None | ||
| self._load_creds() | ||
| self._adal_token_cache_attr = None | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
|
||
| def persist_cached_creds(self): | ||
| with os.fdopen(os.open(self._token_file, os.O_RDWR | os.O_CREAT | os.O_TRUNC, 0o600), | ||
|
|
@@ -459,6 +469,7 @@ def retrieve_token_for_user(self, username, tenant, resource): | |
| return (token_entry[_TOKEN_ENTRY_TOKEN_TYPE], token_entry[_ACCESS_TOKEN]) | ||
|
|
||
| def retrieve_token_for_service_principal(self, sp_id, resource): | ||
| self.load_adal_token_cache() | ||
| matched = [x for x in self._service_principal_creds if sp_id == x[_SERVICE_PRINCIPAL_ID]] | ||
| if not matched: | ||
| raise CLIError("Please run 'az account set' to select active account.") | ||
|
|
@@ -471,23 +482,28 @@ def retrieve_token_for_service_principal(self, sp_id, resource): | |
| return (token_entry[_TOKEN_ENTRY_TOKEN_TYPE], token_entry[_ACCESS_TOKEN]) | ||
|
|
||
| def retrieve_secret_of_service_principal(self, sp_id): | ||
| self.load_adal_token_cache() | ||
| matched = [x for x in self._service_principal_creds if sp_id == x[_SERVICE_PRINCIPAL_ID]] | ||
| if not matched: | ||
| raise CLIError("No matched service principal found") | ||
| cred = matched[0] | ||
| return cred[_ACCESS_TOKEN] | ||
|
|
||
| def _load_creds(self): | ||
| import adal | ||
| if self.adal_token_cache is not None: | ||
| return self.adal_token_cache | ||
| all_entries = _load_tokens_from_file(self._token_file) | ||
| self._load_service_principal_creds(all_entries) | ||
| real_token = [x for x in all_entries if x not in self._service_principal_creds] | ||
| self.adal_token_cache = adal.TokenCache(json.dumps(real_token)) | ||
| return self.adal_token_cache | ||
| @property | ||
| def adal_token_cache(self): | ||
| return self.load_adal_token_cache() | ||
|
|
||
| def load_adal_token_cache(self): | ||
| if self._adal_token_cache_attr is None: | ||
| import adal | ||
| all_entries = _load_tokens_from_file(self._token_file) | ||
| self._load_service_principal_creds(all_entries) | ||
| real_token = [x for x in all_entries if x not in self._service_principal_creds] | ||
| self._adal_token_cache_attr = adal.TokenCache(json.dumps(real_token)) | ||
| return self._adal_token_cache_attr | ||
|
|
||
| def save_service_principal_cred(self, sp_entry): | ||
| self.load_adal_token_cache() | ||
| matched = [x for x in self._service_principal_creds | ||
| if sp_entry[_SERVICE_PRINCIPAL_ID] == x[_SERVICE_PRINCIPAL_ID] and | ||
| sp_entry[_SERVICE_PRINCIPAL_TENANT] == x[_SERVICE_PRINCIPAL_TENANT]] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an existing code defect, but since you are improving around, maybe you would not mind going one step further:
subscription_findercompletely since it is only being used in this routine.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yugangw-msft, that's a good suggestion. Done.