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

Login: skip erroneous tenant #2634

Merged
merged 2 commits into from
Mar 27, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/azure-cli-core/azure/cli/core/_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,14 @@ def _find_using_common_tenant(self, access_token, resource):
for t in tenants:
tenant_id = t.tenant_id
temp_context = self._create_auth_context(tenant_id)
temp_credentials = temp_context.acquire_token(resource, self.user_id, _CLIENT_ID)
try:
temp_credentials = temp_context.acquire_token(resource, self.user_id, _CLIENT_ID)
except adal.AdalError as ex:
# because user creds went through the 'common' tenant, the error here must be
# tenant specific, like the account was disabled. For such errors, we will continue
# with other tenants.
logger.warning("Failed to authenticate '%s' due to error '%s'", t, ex)
continue
subscriptions = self._find_using_specific_tenant(
tenant_id,
temp_credentials[_ACCESS_TOKEN])
Expand Down
21 changes: 21 additions & 0 deletions src/azure-cli-core/tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import os
import unittest
import mock

from adal import AdalError
from azure.mgmt.resource.subscriptions.models import (SubscriptionState, Subscription,
SubscriptionPolicies, spendingLimit)
from azure.cli.core._profile import (Profile, CredsCache, SubscriptionFinder,
Expand Down Expand Up @@ -397,6 +399,25 @@ def test_find_subscriptions_thru_username_password(self, mock_auth_context):
mock_auth_context.acquire_token.assert_called_once_with(
mgmt_resource, self.user1, mock.ANY)

@mock.patch('adal.AuthenticationContext', autospec=True)
@mock.patch('azure.cli.core._profile.logger', autospec=True)
def test_find_subscriptions_thru_username_password_with_account_disabled(self, mock_logger,
mock_auth_context):
mock_auth_context.acquire_token_with_username_password.return_value = self.token_entry1
mock_auth_context.acquire_token.side_effect = AdalError('Account is disabled')
mock_arm_client = mock.MagicMock()
mock_arm_client.tenants.list.return_value = [TenantStub(self.tenant_id)]
finder = SubscriptionFinder(lambda _, _2: mock_auth_context,
None,
lambda _: mock_arm_client)
mgmt_resource = 'https://management.core.windows.net/'
# action
subs = finder.find_from_user_account(self.user1, 'bar', None, mgmt_resource)

# assert
self.assertEqual([], subs)
mock_logger.warning.assert_called_once_with(mock.ANY, mock.ANY, mock.ANY)

@mock.patch('adal.AuthenticationContext', autospec=True)
def test_find_subscriptions_from_particular_tenent(self, mock_auth_context):
def just_raise(ex):
Expand Down