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
33 changes: 7 additions & 26 deletions src/azure-cli-core/azure/cli/core/_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,13 +840,10 @@ def create_arm_client_factory(credentials):

def find_from_user_account(self, username, password, tenant, resource):
context = self._create_auth_context(tenant)
try:
if password:
token_entry = context.acquire_token_with_username_password(resource, username, password, _CLIENT_ID)
else: # when refresh account, we will leverage local cached tokens
token_entry = context.acquire_token(resource, username, _CLIENT_ID)
except Exception as err: # pylint: disable=broad-except
_login_exception_handler(err)
if password:
token_entry = context.acquire_token_with_username_password(resource, username, password, _CLIENT_ID)
else: # when refresh account, we will leverage local cached tokens
token_entry = context.acquire_token(resource, username, _CLIENT_ID)

if not token_entry:
return []
Expand All @@ -867,11 +864,8 @@ def find_through_authorization_code_flow(self, tenant, resource, authority_url):

# exchange the code for the token
context = self._create_auth_context(tenant)
try:
token_entry = context.acquire_token_with_authorization_code(results['code'], results['reply_url'],
resource, _CLIENT_ID, None)
except Exception as err: # pylint: disable=broad-except
_login_exception_handler(err)
token_entry = context.acquire_token_with_authorization_code(results['code'], results['reply_url'],
resource, _CLIENT_ID, None)
self.user_id = token_entry[_TOKEN_ENTRY_USER_ID]
logger.warning("You have logged in. Now let us find all the subscriptions to which you have access...")
if tenant is None:
Expand All @@ -882,10 +876,7 @@ def find_through_authorization_code_flow(self, tenant, resource, authority_url):

def find_through_interactive_flow(self, tenant, resource):
context = self._create_auth_context(tenant)
try:
code = context.acquire_user_code(resource, _CLIENT_ID)
except Exception as err: # pylint: disable=broad-except
_login_exception_handler(err)
code = context.acquire_user_code(resource, _CLIENT_ID)
logger.warning(code['message'])
token_entry = context.acquire_token_with_device_code(resource, code, _CLIENT_ID)
self.user_id = token_entry[_TOKEN_ENTRY_USER_ID]
Expand Down Expand Up @@ -1343,13 +1334,3 @@ def _get_authorization_code(resource, authority_url):
if results.get('no_browser'):
raise RuntimeError()
return results


def _login_exception_handler(ex):
from requests.exceptions import InvalidURL
if isinstance(ex, InvalidURL):
import traceback
from azure.cli.core.azclierror import UnclassifiedUserFault
logger.debug('Invalid url when acquiring token\n%s', traceback.format_exc())
raise UnclassifiedUserFault(error_msg='Invalid url when acquiring token',
recommendation='Please make sure the cloud is registered with valid url')
Copy link
Member Author

Choose a reason for hiding this comment

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

There should be raise ex here

26 changes: 0 additions & 26 deletions src/azure-cli-core/azure/cli/core/tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1046,32 +1046,6 @@ def test_find_subscriptions_thru_username_non_password(self, mock_auth_context):
# assert
self.assertEqual([], subs)

@mock.patch('adal.AuthenticationContext', autospec=True)
@mock.patch('azure.cli.core._profile._get_authorization_code', autospec=True)
def test_find_subscriptions_with_invalid_authority_url(self, _get_authorization_code_mock, mock_auth_context):
Copy link
Member

Choose a reason for hiding this comment

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

adal.AuthenticationContext will not be called, since SubscriptionFinder is using lambda _, _1, _2: mock_auth_context.

mock_auth_context can simply be created as a MagicMock().

from requests.exceptions import InvalidURL
from azure.cli.core.azclierror import UnclassifiedUserFault

def mock_acquire(*args, **kwargs):
raise InvalidURL(request='http://some.unknown.endpoints')

cli = DummyCli()
mock_auth_context.acquire_token_with_username_password.side_effect = mock_acquire
mock_auth_context.acquire_token_with_authorization_code.side_effect = mock_acquire
mock_auth_context.acquire_user_code.side_effect = mock_acquire
_get_authorization_code_mock.return_value = {
'code': 'code1',
'reply_url': 'http://localhost:8888'
}

finder = SubscriptionFinder(cli, lambda _, _1, _2: mock_auth_context, None, lambda _: None)
with self.assertRaisesRegexp(UnclassifiedUserFault, 'Invalid url when acquiring token'):
finder.find_from_user_account(self.user1, 'bar', None, 'http://goo-resource')
with self.assertRaisesRegexp(UnclassifiedUserFault, 'Invalid url when acquiring token'):
finder.find_through_authorization_code_flow(None, 'https://management.core.windows.net/', 'https:/some_aad_point/common')
with self.assertRaisesRegexp(UnclassifiedUserFault, 'Invalid url when acquiring token'):
finder.find_through_interactive_flow(None, 'https://management.core.windows.net/')

@mock.patch('azure.cli.core.adal_authentication.MSIAuthenticationWrapper', autospec=True)
@mock.patch('azure.cli.core.profiles._shared.get_client_class', autospec=True)
@mock.patch('azure.cli.core._profile._get_cloud_console_token_endpoint', autospec=True)
Expand Down