Skip to content

Commit

Permalink
Turn async persist off when logging in (Azure#5464)
Browse files Browse the repository at this point in the history
* turn async persist off when logging in

* profile history, version bump

* renaming

* review feedback
  • Loading branch information
williexu authored Feb 2, 2018
1 parent 314dab8 commit cc9e2ee
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 43 deletions.
7 changes: 2 additions & 5 deletions src/azure-cli-core/azure/cli/core/_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,13 @@ class CredentialType(Enum): # pylint: disable=too-few-public-methods

class Profile(object):

def __init__(self, storage=None, auth_ctx_factory=None, use_global_creds_cache=True, cli_ctx=None):
def __init__(self, storage=None, auth_ctx_factory=None, async_persist=True, cli_ctx=None):
from azure.cli.core import get_default_cli

self.cli_ctx = cli_ctx or get_default_cli()
self._storage = storage or ACCOUNT
self.auth_ctx_factory = auth_ctx_factory or _AUTH_CTX_FACTORY
if use_global_creds_cache:
self._creds_cache = CredsCache(self.cli_ctx, _AUTH_CTX_FACTORY, async_persist=True)
else:
self._creds_cache = CredsCache(self.auth_ctx_factory, async_persist=False)
self._creds_cache = CredsCache(self.cli_ctx, self.auth_ctx_factory, async_persist=async_persist)
self._management_resource_uri = self.cli_ctx.cloud.endpoints.management
self._ad_resource_uri = self.cli_ctx.cloud.endpoints.active_directory_resource_id
self._msi_creds = None
Expand Down
70 changes: 35 additions & 35 deletions src/azure-cli-core/azure/cli/core/tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def setUpClass(cls):
def test_normalize(self):
cli = TestCli()
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)
consolidated = profile._normalize_properties(self.user1,
[self.subscription1],
False)
Expand All @@ -108,7 +108,7 @@ def test_normalize(self):
def test_update_add_two_different_subscriptions(self):
cli = TestCli()
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)

# add the first and verify
consolidated = profile._normalize_properties(self.user1,
Expand Down Expand Up @@ -160,7 +160,7 @@ def test_update_add_two_different_subscriptions(self):
def test_update_with_same_subscription_added_twice(self):
cli = TestCli()
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)

# add one twice and verify we will have one but with new token
consolidated = profile._normalize_properties(self.user1,
Expand All @@ -183,7 +183,7 @@ def test_update_with_same_subscription_added_twice(self):
def test_set_active_subscription(self):
cli = TestCli()
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)

consolidated = profile._normalize_properties(self.user1,
[self.subscription1],
Expand All @@ -204,7 +204,7 @@ def test_set_active_subscription(self):
def test_default_active_subscription_to_non_disabled_one(self):
cli = TestCli()
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)

subscriptions = profile._normalize_properties(
self.user2, [self.subscription2, self.subscription1], False)
Expand All @@ -218,7 +218,7 @@ def test_default_active_subscription_to_non_disabled_one(self):
def test_get_subscription(self):
cli = TestCli()
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)

consolidated = profile._normalize_properties(self.user1,
[self.subscription1],
Expand All @@ -237,7 +237,7 @@ def test_get_subscription(self):
def test_get_auth_info_fail_on_user_account(self):
cli = TestCli()
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)

consolidated = profile._normalize_properties(self.user1,
[self.subscription1],
Expand Down Expand Up @@ -265,7 +265,7 @@ def test_get_auth_info_for_logged_in_service_principal(self, mock_auth_context):
finder = SubscriptionFinder(cli, lambda _, _1, _2: mock_auth_context, None, lambda _: mock_arm_client)

storage_mock = {'subscriptions': []}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)
profile._management_resource_uri = 'https://management.core.windows.net/'
profile.find_subscriptions_on_login(False, '1234', 'my-secret', True, self.tenant_id, False, finder)
# action
Expand All @@ -280,7 +280,7 @@ def test_get_auth_info_for_logged_in_service_principal(self, mock_auth_context):
def test_get_auth_info_for_newly_created_service_principal(self):
cli = TestCli()
storage_mock = {'subscriptions': []}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)
consolidated = profile._normalize_properties(self.user1, [self.subscription1], False)
profile._set_subscriptions(consolidated)
# action
Expand All @@ -303,7 +303,7 @@ def test_create_account_without_subscriptions_thru_service_principal(self, mock_
finder = SubscriptionFinder(cli, lambda _, _1, _2: mock_auth_context, None, lambda _: mock_arm_client)

storage_mock = {'subscriptions': []}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)
profile._management_resource_uri = 'https://management.core.windows.net/'

# action
Expand Down Expand Up @@ -337,7 +337,7 @@ def test_create_account_without_subscriptions_thru_common_tenant(self, mock_auth
finder = SubscriptionFinder(cli, lambda _, _1, _2: mock_auth_context, None, lambda _: mock_arm_client)

storage_mock = {'subscriptions': []}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)
profile._management_resource_uri = 'https://management.core.windows.net/'

# action
Expand All @@ -362,7 +362,7 @@ def test_create_account_without_subscriptions_without_tenant(self, mock_auth_con
finder = mock.MagicMock()
finder.find_through_interactive_flow.return_value = []
storage_mock = {'subscriptions': []}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)

# action
result = profile.find_subscriptions_on_login(True,
Expand All @@ -383,7 +383,7 @@ def test_get_current_account_user(self, mock_read_cred_file):
mock_read_cred_file.return_value = [TestProfile.token_entry1]

storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)
consolidated = profile._normalize_properties(self.user1,
[self.subscription1],
False)
Expand All @@ -398,7 +398,7 @@ def test_get_current_account_user(self, mock_read_cred_file):
def test_create_token_cache(self, mock_read_file):
cli = TestCli()
mock_read_file.return_value = []
profile = Profile(cli_ctx=cli, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, async_persist=False)
cache = profile._creds_cache.adal_token_cache
self.assertFalse(cache.read_items())
self.assertTrue(mock_read_file.called)
Expand All @@ -407,7 +407,7 @@ def test_create_token_cache(self, mock_read_file):
def test_load_cached_tokens(self, mock_read_file):
cli = TestCli()
mock_read_file.return_value = [TestProfile.token_entry1]
profile = Profile(cli_ctx=cli, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, async_persist=False)
cache = profile._creds_cache.adal_token_cache
matched = cache.find({
"_authority": "https://login.microsoftonline.com/common",
Expand All @@ -426,7 +426,7 @@ def test_get_login_credentials(self, mock_get_token, mock_read_cred_file):
mock_get_token.return_value = (some_token_type, TestProfile.raw_token1)
# setup
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)
consolidated = profile._normalize_properties(self.user1,
[self.subscription1],
False)
Expand All @@ -453,7 +453,7 @@ def test_get_login_credentials_msi_system_assigned(self, mock_post, mock_read_cr

# setup an existing msi subscription
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)
test_subscription_id = '12345678-1bf0-4dda-aec3-cb9272f09590'
test_tenant_id = '12345678-38d6-4fb2-bad9-b7b93a3e1234'
test_port = '12345'
Expand Down Expand Up @@ -498,7 +498,7 @@ def test_get_login_credentials_msi_user_assigned_with_client_id(self, mock_post,

# setup an existing msi subscription
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)
test_subscription_id = '12345678-1bf0-4dda-aec3-cb9272f09590'
test_tenant_id = '12345678-38d6-4fb2-bad9-b7b93a3e1234'
test_port = '12345'
Expand Down Expand Up @@ -545,7 +545,7 @@ def test_get_login_credentials_msi_user_assigned_with_object_id(self, mock_post,

# setup an existing msi subscription
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)
test_object_id = '12345678-38d6-4fb2-bad9-b7b93a3e9999'
msi_subscription = SubscriptionStub('/subscriptions/12345678-1bf0-4dda-aec3-cb9272f09590',
'MSIObject-{}@12345'.format(test_object_id),
Expand Down Expand Up @@ -584,7 +584,7 @@ def test_get_login_credentials_msi_user_assigned_with_res_id(self, mock_post, mo

# setup an existing msi subscription
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)
test_sub_id = '12345678-1bf0-4dda-aec3-cb9272f09590'
test_res_id = ('/subscriptions/{}/resourceGroups/r1/providers/Microsoft.ManagedIdentity/'
'userAssignedIdentities/id1').format(test_sub_id)
Expand Down Expand Up @@ -654,7 +654,7 @@ def test_get_raw_token(self, mock_get_token, mock_read_cred_file):
TestProfile.token_entry1)
# setup
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)
consolidated = profile._normalize_properties(self.user1,
[self.subscription1],
False)
Expand All @@ -681,7 +681,7 @@ def test_get_raw_token_msi_system_assigned(self, mock_post, mock_read_cred_file)

# setup an existing msi subscription
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)
test_subscription_id = '12345678-1bf0-4dda-aec3-cb9272f09590'
test_tenant_id = '12345678-38d6-4fb2-bad9-b7b93a3e1234'
test_port = '12345'
Expand Down Expand Up @@ -728,7 +728,7 @@ def test_get_login_credentials_for_graph_client(self, mock_get_token, mock_read_
mock_get_token.return_value = (some_token_type, TestProfile.raw_token1)
# setup
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)
consolidated = profile._normalize_properties(self.user1, [self.subscription1],
False)
profile._set_subscriptions(consolidated)
Expand Down Expand Up @@ -768,7 +768,7 @@ def test_cloud_console_login(self):
setattr(test_sub, 'tenant_id', '54826b22-38d6-4fb2-bad9-b7b93a3e9c5a')

with mock.patch('azure.cli.core._profile.SubscriptionFinder._find_using_specific_tenant', autospec=True, return_value=[test_sub]):
profile = Profile(cli_ctx=cli, use_global_creds_cache=False, storage=test_account)
profile = Profile(cli_ctx=cli, async_persist=False, storage=test_account)
result_accounts = profile.find_subscriptions_in_cloud_console([arm_token, kv_token])

# verify the local account
Expand Down Expand Up @@ -833,7 +833,7 @@ def test_get_login_credentials_for_data_lake_client(self, mock_get_token, mock_r
mock_get_token.return_value = (some_token_type, TestProfile.raw_token1)
# setup
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)
consolidated = profile._normalize_properties(self.user1, [self.subscription1],
False)
profile._set_subscriptions(consolidated)
Expand All @@ -854,7 +854,7 @@ def test_logout(self, mock_persist_creds, mock_read_cred_file):
mock_read_cred_file.return_value = [TestProfile.token_entry1]

storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)
consolidated = profile._normalize_properties(self.user1,
[self.subscription1],
False)
Expand All @@ -873,7 +873,7 @@ def test_logout_all(self, mock_delete_cred_file):
cli = TestCli()
# setup
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)
consolidated = profile._normalize_properties(self.user1,
[self.subscription1],
False)
Expand Down Expand Up @@ -923,7 +923,7 @@ def __init__(self, *args, **kwargs):
mock_get_client_class.return_value = ClientStub
cli = TestCli()
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)

test_token_entry = {
'token_type': 'Bearer',
Expand Down Expand Up @@ -959,7 +959,7 @@ def __init__(self, *args, **kwargs):
mock_get_client_class.return_value = ClientStub
cli = TestCli()
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)

test_token_entry = {
'token_type': 'Bearer',
Expand Down Expand Up @@ -996,7 +996,7 @@ def __init__(self, *args, **kwargs):
mock_get_client_class.return_value = ClientStub
cli = TestCli()
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)

test_token_entry = {
'token_type': 'Bearer',
Expand Down Expand Up @@ -1031,7 +1031,7 @@ def __init__(self, *args, **kwargs):
mock_get_client_class.return_value = ClientStub
cli = TestCli()
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)

test_token_entry = {
'token_type': 'Bearer',
Expand Down Expand Up @@ -1201,7 +1201,7 @@ def test_find_subscriptions_from_service_principal_using_cert(self, mock_auth_co
def test_refresh_accounts_one_user_account(self, mock_auth_context):
cli = TestCli()
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)
consolidated = profile._normalize_properties(self.user1, deepcopy([self.subscription1]), False)
profile._set_subscriptions(consolidated)
mock_auth_context.acquire_token_with_username_password.return_value = self.token_entry1
Expand All @@ -1224,7 +1224,7 @@ def test_refresh_accounts_one_user_account(self, mock_auth_context):
def test_refresh_accounts_one_user_account_one_sp_account(self, mock_auth_context):
cli = TestCli()
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)
sp_subscription1 = SubscriptionStub('sp-sub/3', 'foo-subname', self.state1, 'foo_tenant.onmicrosoft.com')
consolidated = profile._normalize_properties(self.user1, deepcopy([self.subscription1]), False)
consolidated += profile._normalize_properties('http://foo', [sp_subscription1], True)
Expand Down Expand Up @@ -1253,7 +1253,7 @@ def test_refresh_accounts_one_user_account_one_sp_account(self, mock_auth_contex
def test_refresh_accounts_with_nothing(self, mock_auth_context):
cli = TestCli()
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)
consolidated = profile._normalize_properties(self.user1, deepcopy([self.subscription1]), False)
profile._set_subscriptions(consolidated)
mock_auth_context.acquire_token_with_username_password.return_value = self.token_entry1
Expand Down Expand Up @@ -1462,7 +1462,7 @@ def test_detect_adfs_authority_url(self):
adfs_url_1 = 'https://adfs.redmond.ext-u15f2402.masd.stbtest.microsoft.com/adfs/'
cli.cloud.endpoints.active_directory = adfs_url_1
storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock, use_global_creds_cache=False)
profile = Profile(cli_ctx=cli, storage=storage_mock, async_persist=False)

# test w/ trailing slash
r = profile.auth_ctx_factory(cli, 'common', None)
Expand Down
Loading

0 comments on commit cc9e2ee

Please sign in to comment.