From 1ff60b8f2025667557e57b0b83fdfbfae0e7b509 Mon Sep 17 00:00:00 2001 From: evelyn-ys Date: Mon, 19 Oct 2020 14:45:17 +0800 Subject: [PATCH 01/11] Improvement for most frenquent errors in `az login` --- src/azure-cli-core/azure/cli/core/_profile.py | 4 ++-- .../azure/cli/core/adal_authentication.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/azure-cli-core/azure/cli/core/_profile.py b/src/azure-cli-core/azure/cli/core/_profile.py index ca1281fbd72..9b9c2acf8a4 100644 --- a/src/azure-cli-core/azure/cli/core/_profile.py +++ b/src/azure-cli-core/azure/cli/core/_profile.py @@ -1140,7 +1140,7 @@ def __init__(self, password_arg_value, use_cert_sn_issuer=None): 'authenticate through a service principal') if os.path.isfile(password_arg_value): certificate_file = password_arg_value - from OpenSSL.crypto import load_certificate, FILETYPE_PEM + from OpenSSL.crypto import load_certificate, FILETYPE_PEM, Error self.certificate_file = certificate_file self.public_certificate = None try: @@ -1154,7 +1154,7 @@ def __init__(self, password_arg_value, use_cert_sn_issuer=None): match = re.search(r'\-+BEGIN CERTIFICATE.+\-+(?P[^-]+)\-+END CERTIFICATE.+\-+', self.cert_file_string, re.I) self.public_certificate = match.group('public').strip() - except UnicodeDecodeError: + except [UnicodeDecodeError, Error]: raise CLIError('Invalid certificate, please use a valid PEM file.') else: self.secret = password_arg_value diff --git a/src/azure-cli-core/azure/cli/core/adal_authentication.py b/src/azure-cli-core/azure/cli/core/adal_authentication.py index 68ba4b08b0d..2e84946945f 100644 --- a/src/azure-cli-core/azure/cli/core/adal_authentication.py +++ b/src/azure-cli-core/azure/cli/core/adal_authentication.py @@ -93,3 +93,16 @@ class MSIAuthenticationWrapper(MSIAuthentication): def get_token(self, *scopes, **kwargs): # pylint:disable=unused-argument self.set_token() return AccessToken(self.token['access_token'], int(self.token['expires_on'])) + + def set_token(self): + from .azclierror import AzureConnectionError, AzureResponseError + try: + super(MSIAuthenticationWrapper, self).set_token() + except requests.exceptions.ConnectionError as err: + raise AzureConnectionError('Authentication failed: may have network connection issues. ' + 'Error detail: {}'.format(str(err))) + except requests.exceptions.HTTPError as err: + raise AzureResponseError('Authentication failed: got an error response when visit {}: {}' + .format(err.request, err.response)) + except TimeoutError as err: + raise AzureConnectionError('Authentication timeout. Error detail: {}'.format(str(err))) From 3ad23525ee260ff2015b0ccfe32491d1697881cc Mon Sep 17 00:00:00 2001 From: evelyn-ys Date: Mon, 19 Oct 2020 16:42:34 +0800 Subject: [PATCH 02/11] reformat --- src/azure-cli-core/azure/cli/core/adal_authentication.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/azure-cli-core/azure/cli/core/adal_authentication.py b/src/azure-cli-core/azure/cli/core/adal_authentication.py index 2e84946945f..710ed083e33 100644 --- a/src/azure-cli-core/azure/cli/core/adal_authentication.py +++ b/src/azure-cli-core/azure/cli/core/adal_authentication.py @@ -99,10 +99,11 @@ def set_token(self): try: super(MSIAuthenticationWrapper, self).set_token() except requests.exceptions.ConnectionError as err: - raise AzureConnectionError('Authentication failed: may have network connection issues. ' + raise AzureConnectionError('Authentication failed: May have network connection issues. \n' 'Error detail: {}'.format(str(err))) except requests.exceptions.HTTPError as err: - raise AzureResponseError('Authentication failed: got an error response when visit {}: {}' - .format(err.request, err.response)) + raise AzureResponseError('Authentication failed: Got an error response when visit {}.\n' + 'Error code: {}, reason: {}' + .format(err.request, err.response.status, err.response.reason)) except TimeoutError as err: raise AzureConnectionError('Authentication timeout. Error detail: {}'.format(str(err))) From f8652b56fa0220ffc3873004a807724738a78499 Mon Sep 17 00:00:00 2001 From: evelyn-ys Date: Mon, 19 Oct 2020 16:58:29 +0800 Subject: [PATCH 03/11] modify error msg --- src/azure-cli-core/azure/cli/core/adal_authentication.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/azure-cli-core/azure/cli/core/adal_authentication.py b/src/azure-cli-core/azure/cli/core/adal_authentication.py index 710ed083e33..83b3dc137eb 100644 --- a/src/azure-cli-core/azure/cli/core/adal_authentication.py +++ b/src/azure-cli-core/azure/cli/core/adal_authentication.py @@ -99,11 +99,12 @@ def set_token(self): try: super(MSIAuthenticationWrapper, self).set_token() except requests.exceptions.ConnectionError as err: - raise AzureConnectionError('Authentication failed: May have network connection issues. \n' + raise AzureConnectionError('Failed to connect to MSI. Please make sure MSI is configured correctly.\n' 'Error detail: {}'.format(str(err))) except requests.exceptions.HTTPError as err: - raise AzureResponseError('Authentication failed: Got an error response when visit {}.\n' + raise AzureResponseError('Failed to connect to MSI. Please make sure MSI is configured correctly.\n' 'Error code: {}, reason: {}' - .format(err.request, err.response.status, err.response.reason)) + .format(err.response.status, err.response.reason)) except TimeoutError as err: - raise AzureConnectionError('Authentication timeout. Error detail: {}'.format(str(err))) + raise AzureConnectionError('MSI endpoint is not responding. Please make sure MSI is configured correctly.\n' + 'Error detail: {}'.format(str(err))) From 96b7fda949334896b667680b646786078d57bee0 Mon Sep 17 00:00:00 2001 From: evelyn-ys Date: Mon, 19 Oct 2020 17:31:43 +0800 Subject: [PATCH 04/11] modify err msg --- src/azure-cli-core/azure/cli/core/adal_authentication.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/azure-cli-core/azure/cli/core/adal_authentication.py b/src/azure-cli-core/azure/cli/core/adal_authentication.py index 83b3dc137eb..e993adbfa50 100644 --- a/src/azure-cli-core/azure/cli/core/adal_authentication.py +++ b/src/azure-cli-core/azure/cli/core/adal_authentication.py @@ -99,11 +99,11 @@ def set_token(self): try: super(MSIAuthenticationWrapper, self).set_token() except requests.exceptions.ConnectionError as err: - raise AzureConnectionError('Failed to connect to MSI. Please make sure MSI is configured correctly.\n' - 'Error detail: {}'.format(str(err))) + raise AzureConnectionError('Failed to connect to MSI. Please make sure MSI is configured correctly ' + 'and check the network connection.\nError detail: {}'.format(str(err))) except requests.exceptions.HTTPError as err: raise AzureResponseError('Failed to connect to MSI. Please make sure MSI is configured correctly.\n' - 'Error code: {}, reason: {}' + 'Get Token request returned http error: {}, reason: {}' .format(err.response.status, err.response.reason)) except TimeoutError as err: raise AzureConnectionError('MSI endpoint is not responding. Please make sure MSI is configured correctly.\n' From 26388067c44dd382007b21f7cdd72df189191ed0 Mon Sep 17 00:00:00 2001 From: evelyn-ys Date: Wed, 21 Oct 2020 13:22:16 +0800 Subject: [PATCH 05/11] add debug info --- src/azure-cli-core/azure/cli/core/adal_authentication.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/azure-cli-core/azure/cli/core/adal_authentication.py b/src/azure-cli-core/azure/cli/core/adal_authentication.py index e993adbfa50..7333d7be17e 100644 --- a/src/azure-cli-core/azure/cli/core/adal_authentication.py +++ b/src/azure-cli-core/azure/cli/core/adal_authentication.py @@ -95,16 +95,25 @@ def get_token(self, *scopes, **kwargs): # pylint:disable=unused-argument return AccessToken(self.token['access_token'], int(self.token['expires_on'])) def set_token(self): + import traceback + from knack.log import get_logger + logger = get_logger(__name__) from .azclierror import AzureConnectionError, AzureResponseError try: super(MSIAuthenticationWrapper, self).set_token() except requests.exceptions.ConnectionError as err: + logger.debug('throw requests.exceptions.ConnectionError when doing MSIAuthentication: \n{}' + .format(traceback.format_exc())) raise AzureConnectionError('Failed to connect to MSI. Please make sure MSI is configured correctly ' 'and check the network connection.\nError detail: {}'.format(str(err))) except requests.exceptions.HTTPError as err: + logger.debug('throw requests.exceptions.HTTPError when doing MSIAuthentication: \n{}' + .format(traceback.format_exc())) raise AzureResponseError('Failed to connect to MSI. Please make sure MSI is configured correctly.\n' 'Get Token request returned http error: {}, reason: {}' .format(err.response.status, err.response.reason)) except TimeoutError as err: + logger.debug('throw TimeoutError when doing MSIAuthentication: \n{}' + .format(traceback.format_exc())) raise AzureConnectionError('MSI endpoint is not responding. Please make sure MSI is configured correctly.\n' 'Error detail: {}'.format(str(err))) From 5ec26b76e8678a6d08a21657a662a681ecc03111 Mon Sep 17 00:00:00 2001 From: evelyn-ys Date: Wed, 21 Oct 2020 14:42:08 +0800 Subject: [PATCH 06/11] pylint --- .../azure/cli/core/adal_authentication.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/azure-cli-core/azure/cli/core/adal_authentication.py b/src/azure-cli-core/azure/cli/core/adal_authentication.py index 7333d7be17e..e13930f3c7c 100644 --- a/src/azure-cli-core/azure/cli/core/adal_authentication.py +++ b/src/azure-cli-core/azure/cli/core/adal_authentication.py @@ -102,18 +102,18 @@ def set_token(self): try: super(MSIAuthenticationWrapper, self).set_token() except requests.exceptions.ConnectionError as err: - logger.debug('throw requests.exceptions.ConnectionError when doing MSIAuthentication: \n{}' - .format(traceback.format_exc())) + logger.debug('throw requests.exceptions.ConnectionError when doing MSIAuthentication: \n%s', + traceback.format_exc()) raise AzureConnectionError('Failed to connect to MSI. Please make sure MSI is configured correctly ' 'and check the network connection.\nError detail: {}'.format(str(err))) except requests.exceptions.HTTPError as err: - logger.debug('throw requests.exceptions.HTTPError when doing MSIAuthentication: \n{}' - .format(traceback.format_exc())) + logger.debug('throw requests.exceptions.HTTPError when doing MSIAuthentication: \n%s', + traceback.format_exc()) raise AzureResponseError('Failed to connect to MSI. Please make sure MSI is configured correctly.\n' 'Get Token request returned http error: {}, reason: {}' .format(err.response.status, err.response.reason)) except TimeoutError as err: - logger.debug('throw TimeoutError when doing MSIAuthentication: \n{}' - .format(traceback.format_exc())) + logger.debug('throw TimeoutError when doing MSIAuthentication: \n%s', + traceback.format_exc()) raise AzureConnectionError('MSI endpoint is not responding. Please make sure MSI is configured correctly.\n' 'Error detail: {}'.format(str(err))) From baab056f912fae0f61100253a39ac245aa949e45 Mon Sep 17 00:00:00 2001 From: evelyn-ys Date: Wed, 21 Oct 2020 18:17:24 +0800 Subject: [PATCH 07/11] profile catch logic --- src/azure-cli-core/azure/cli/core/_profile.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/azure-cli-core/azure/cli/core/_profile.py b/src/azure-cli-core/azure/cli/core/_profile.py index 9b9c2acf8a4..e9c4cee792c 100644 --- a/src/azure-cli-core/azure/cli/core/_profile.py +++ b/src/azure-cli-core/azure/cli/core/_profile.py @@ -317,12 +317,13 @@ def find_subscriptions_in_vm_with_msi(self, identity_id=None, allow_no_subscript identity_type = MsiAccountTypes.user_assigned_resource_id else: authenticated = False + from .azclierror import AzureResponseError try: msi_creds = MSIAuthenticationWrapper(resource=resource, client_id=identity_id) identity_type = MsiAccountTypes.user_assigned_client_id authenticated = True - except HTTPError as ex: - if ex.response.reason == 'Bad Request' and ex.response.status == 400: + except AzureResponseError as ex: + if 'http error: 400, reason: Bad Request' in ex.error_msg: logger.info('Sniff: not an MSI client id') else: raise @@ -332,8 +333,8 @@ def find_subscriptions_in_vm_with_msi(self, identity_id=None, allow_no_subscript identity_type = MsiAccountTypes.user_assigned_object_id msi_creds = MSIAuthenticationWrapper(resource=resource, object_id=identity_id) authenticated = True - except HTTPError as ex: - if ex.response.reason == 'Bad Request' and ex.response.status == 400: + except AzureResponseError as ex: + if 'http error: 400, reason: Bad Request' in ex.error_msg: logger.info('Sniff: not an MSI object id') else: raise From 170d73e7f768593e019045a81ec34f673519b371 Mon Sep 17 00:00:00 2001 From: Yishi Wang Date: Wed, 21 Oct 2020 21:37:52 +0800 Subject: [PATCH 08/11] fix tests --- src/azure-cli-core/azure/cli/core/tests/test_profile.py | 7 +++---- .../azure/cli/core/tests/test_profile_v2016_06_01.py | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/azure-cli-core/azure/cli/core/tests/test_profile.py b/src/azure-cli-core/azure/cli/core/tests/test_profile.py index 7022fca706d..16173c3a72b 100644 --- a/src/azure-cli-core/azure/cli/core/tests/test_profile.py +++ b/src/azure-cli-core/azure/cli/core/tests/test_profile.py @@ -1191,7 +1191,7 @@ def __init__(self, *args, **kwargs): @mock.patch('azure.cli.core._profile.SubscriptionFinder', autospec=True) def test_find_subscriptions_in_vm_with_msi_user_assigned_with_object_id(self, mock_subscription_finder, mock_get_client_class, mock_msi_auth): - from requests import HTTPError + from ..azclierror import AzureResponseError class SubscriptionFinderStub: def find_from_raw_token(self, tenant, token): @@ -1216,9 +1216,8 @@ def set_token(self): 'access_token': TestProfile.test_msi_access_token } else: - mock_obj = mock.MagicMock() - mock_obj.status, mock_obj.reason = 400, 'Bad Request' - raise HTTPError(response=mock_obj) + raise AzureResponseError('Failed to connect to MSI. Please make sure MSI is configured correctly.\n' + 'Get Token request returned http error: 400, reason: Bad Request') profile = Profile(cli_ctx=DummyCli(), storage={'subscriptions': None}, use_global_creds_cache=False, async_persist=False) diff --git a/src/azure-cli-core/azure/cli/core/tests/test_profile_v2016_06_01.py b/src/azure-cli-core/azure/cli/core/tests/test_profile_v2016_06_01.py index d4cf59042cd..756911f1100 100644 --- a/src/azure-cli-core/azure/cli/core/tests/test_profile_v2016_06_01.py +++ b/src/azure-cli-core/azure/cli/core/tests/test_profile_v2016_06_01.py @@ -1053,7 +1053,7 @@ def __init__(self, *args, **kwargs): @mock.patch('azure.cli.core._profile.SubscriptionFinder', autospec=True) def test_find_subscriptions_in_vm_with_msi_user_assigned_with_object_id(self, mock_subscription_finder, mock_get_client_class, mock_msi_auth): - from requests import HTTPError + from ..azclierror import AzureResponseError class SubscriptionFinderStub: def find_from_raw_token(self, tenant, token): @@ -1078,9 +1078,8 @@ def set_token(self): 'access_token': TestProfile.test_msi_access_token } else: - mock_obj = mock.MagicMock() - mock_obj.status, mock_obj.reason = 400, 'Bad Request' - raise HTTPError(response=mock_obj) + raise AzureResponseError('Failed to connect to MSI. Please make sure MSI is configured correctly.\n' + 'Get Token request returned http error: 400, reason: Bad Request') profile = Profile(cli_ctx=DummyCli(), storage={'subscriptions': None}, use_global_creds_cache=False, async_persist=False) From 45b1bee3205dade668153bbcee456cafc147731d Mon Sep 17 00:00:00 2001 From: Yishi Wang Date: Wed, 21 Oct 2020 22:06:37 +0800 Subject: [PATCH 09/11] remove unused import --- src/azure-cli-core/azure/cli/core/_profile.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/azure-cli-core/azure/cli/core/_profile.py b/src/azure-cli-core/azure/cli/core/_profile.py index e9c4cee792c..02a087f48fc 100644 --- a/src/azure-cli-core/azure/cli/core/_profile.py +++ b/src/azure-cli-core/azure/cli/core/_profile.py @@ -306,7 +306,6 @@ def find_subscriptions_in_vm_with_msi(self, identity_id=None, allow_no_subscript # pylint: disable=too-many-statements import jwt - from requests import HTTPError from msrestazure.tools import is_valid_resource_id from azure.cli.core.adal_authentication import MSIAuthenticationWrapper resource = self.cli_ctx.cloud.endpoints.active_directory_resource_id From 94a1be2fb93a154c6739e0f43fa10ae974ae47e6 Mon Sep 17 00:00:00 2001 From: Yishi Wang Date: Thu, 22 Oct 2020 14:31:49 +0800 Subject: [PATCH 10/11] add test for Invalid PEM file --- src/azure-cli-core/azure/cli/core/_profile.py | 2 +- .../azure/cli/core/adal_authentication.py | 2 +- .../azure/cli/core/tests/err_sp_cert.pem | 49 +++++++++++++++++++ .../azure/cli/core/tests/test_profile.py | 6 +++ 4 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 src/azure-cli-core/azure/cli/core/tests/err_sp_cert.pem diff --git a/src/azure-cli-core/azure/cli/core/_profile.py b/src/azure-cli-core/azure/cli/core/_profile.py index 02a087f48fc..0b50234453c 100644 --- a/src/azure-cli-core/azure/cli/core/_profile.py +++ b/src/azure-cli-core/azure/cli/core/_profile.py @@ -1154,7 +1154,7 @@ def __init__(self, password_arg_value, use_cert_sn_issuer=None): match = re.search(r'\-+BEGIN CERTIFICATE.+\-+(?P[^-]+)\-+END CERTIFICATE.+\-+', self.cert_file_string, re.I) self.public_certificate = match.group('public').strip() - except [UnicodeDecodeError, Error]: + except (UnicodeDecodeError, Error): raise CLIError('Invalid certificate, please use a valid PEM file.') else: self.secret = password_arg_value diff --git a/src/azure-cli-core/azure/cli/core/adal_authentication.py b/src/azure-cli-core/azure/cli/core/adal_authentication.py index e13930f3c7c..7ff90a1bd31 100644 --- a/src/azure-cli-core/azure/cli/core/adal_authentication.py +++ b/src/azure-cli-core/azure/cli/core/adal_authentication.py @@ -98,7 +98,7 @@ def set_token(self): import traceback from knack.log import get_logger logger = get_logger(__name__) - from .azclierror import AzureConnectionError, AzureResponseError + from azure.cli.core.azclierror import AzureConnectionError, AzureResponseError try: super(MSIAuthenticationWrapper, self).set_token() except requests.exceptions.ConnectionError as err: diff --git a/src/azure-cli-core/azure/cli/core/tests/err_sp_cert.pem b/src/azure-cli-core/azure/cli/core/tests/err_sp_cert.pem new file mode 100644 index 00000000000..874b1b34f4e --- /dev/null +++ b/src/azure-cli-core/azure/cli/core/tests/err_sp_cert.pem @@ -0,0 +1,49 @@ + +MIIEowIBAAKCAQEAxec32tnXNiPz2WBTpv7ccZvYqBR2Gr8vimQbiNgT3aHY/dzV +26pYv/88X5PbkibAr3YXJP64nGI/0MGvFWYi6c6C0Ar6QL/MgRLIGIO8JePTxKu9 +ZDx+5CrwbeJRQgz7nEtCWsIx5WiIx5/yjUR5AqrNwSxNWo6Ct3E1YWzGyI03gEEr +82tEG9VdObIRq05v1hHKTm27xln41JZI1aUMzd/K/pckb6nQLtV6OpOmzZQILMOV +95SKJ8+k1gnxfOX2t9JPgTuiVmwvgYLb1k7Hfqs1/KZt4IyIRkBaXPy2j5Guz09u +R1Dg4tOcoSPwDeN0aQQSucRsk0iaof3DXMfVLQIDAQABAoIBAE5wrvrXjS2wYl6u +h3mRWt7M3rsAIS/Ix5caxq8etgEKvW8hsMI4aqTvDYb0m/r50TW6oMHRFGsnphOg +MT1SFlSFveOn9jjjzLL8NsNAs7pw6ubxC0hHkiPtwQ5MvdhoPPJeAhbnEc/T9yLo +isLjePNNr+/5F0/lXHpJLNdIviGO5OzAUhgbyZXUO3NXgsYEEw0fU9NwKnSLC4h4 +h5yYMQklcKO/sVj6cGWmF6AzY0P8E9XXeH9wVXZsKnWEf3Tzz8tdZQ+HXYOV2S7O +ZrhwaNw3NFWWi2Yc6pCu2rS9MfCQ9mgk0GrEAUAs2nDEF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a1Qv3ivKDK2H +2LOdn2Xs3/ZGsi1ySfjzxjTbuPhUaEUy+ZfV2dgmqiS//BAWI5opo7TgeplrGk2P +h5Fwbt0FxaqFCNZdrPI7FRnbKZwvGx0A+Zj8ZpNjft3QjuUg+xqMKMs= +-----END CERTIFICATE----- diff --git a/src/azure-cli-core/azure/cli/core/tests/test_profile.py b/src/azure-cli-core/azure/cli/core/tests/test_profile.py index 16173c3a72b..8c64ab742ae 100644 --- a/src/azure-cli-core/azure/cli/core/tests/test_profile.py +++ b/src/azure-cli-core/azure/cli/core/tests/test_profile.py @@ -1800,6 +1800,12 @@ def test_service_principal_auth_client_cert(self): 'thumbprint': 'F0:6A:53:84:8B:BE:71:4A:42:90:D6:9D:33:52:79:C1:D0:10:73:FD' }) + def test_service_principal_auth_client_cert_err(self): + curr_dir = os.path.dirname(os.path.realpath(__file__)) + test_cert_file = os.path.join(curr_dir, 'err_sp_cert.pem') + with self.assertRaisesRegexp(CLIError, 'Invalid certificate'): + ServicePrincipalAuth(test_cert_file) + def test_detect_adfs_authority_url(self): cli = DummyCli() adfs_url_1 = 'https://adfs.redmond.ext-u15f2402.masd.stbtest.microsoft.com/adfs/' From 5712e3a9b739b65ec6de314237e6ef9f3a47ca7f Mon Sep 17 00:00:00 2001 From: Yishi Wang Date: Thu, 22 Oct 2020 15:15:06 +0800 Subject: [PATCH 11/11] fix import --- src/azure-cli-core/azure/cli/core/_profile.py | 2 +- src/azure-cli-core/azure/cli/core/tests/test_profile.py | 2 +- .../azure/cli/core/tests/test_profile_v2016_06_01.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/azure-cli-core/azure/cli/core/_profile.py b/src/azure-cli-core/azure/cli/core/_profile.py index 0b50234453c..3dee2b62679 100644 --- a/src/azure-cli-core/azure/cli/core/_profile.py +++ b/src/azure-cli-core/azure/cli/core/_profile.py @@ -316,7 +316,7 @@ def find_subscriptions_in_vm_with_msi(self, identity_id=None, allow_no_subscript identity_type = MsiAccountTypes.user_assigned_resource_id else: authenticated = False - from .azclierror import AzureResponseError + from azure.cli.core.azclierror import AzureResponseError try: msi_creds = MSIAuthenticationWrapper(resource=resource, client_id=identity_id) identity_type = MsiAccountTypes.user_assigned_client_id diff --git a/src/azure-cli-core/azure/cli/core/tests/test_profile.py b/src/azure-cli-core/azure/cli/core/tests/test_profile.py index 8c64ab742ae..b96af36a0ee 100644 --- a/src/azure-cli-core/azure/cli/core/tests/test_profile.py +++ b/src/azure-cli-core/azure/cli/core/tests/test_profile.py @@ -1191,7 +1191,7 @@ def __init__(self, *args, **kwargs): @mock.patch('azure.cli.core._profile.SubscriptionFinder', autospec=True) def test_find_subscriptions_in_vm_with_msi_user_assigned_with_object_id(self, mock_subscription_finder, mock_get_client_class, mock_msi_auth): - from ..azclierror import AzureResponseError + from azure.cli.core.azclierror import AzureResponseError class SubscriptionFinderStub: def find_from_raw_token(self, tenant, token): diff --git a/src/azure-cli-core/azure/cli/core/tests/test_profile_v2016_06_01.py b/src/azure-cli-core/azure/cli/core/tests/test_profile_v2016_06_01.py index 756911f1100..22d795da86f 100644 --- a/src/azure-cli-core/azure/cli/core/tests/test_profile_v2016_06_01.py +++ b/src/azure-cli-core/azure/cli/core/tests/test_profile_v2016_06_01.py @@ -1053,7 +1053,7 @@ def __init__(self, *args, **kwargs): @mock.patch('azure.cli.core._profile.SubscriptionFinder', autospec=True) def test_find_subscriptions_in_vm_with_msi_user_assigned_with_object_id(self, mock_subscription_finder, mock_get_client_class, mock_msi_auth): - from ..azclierror import AzureResponseError + from azure.cli.core.azclierror import AzureResponseError class SubscriptionFinderStub: def find_from_raw_token(self, tenant, token):