-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[Core] Conditional Access: Show --scope for az login message when failed to refresh the access token
#17738
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
Merged
Merged
[Core] Conditional Access: Show --scope for az login message when failed to refresh the access token
#17738
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def aad_error_handler(error, **kwargs): | ||
| """ Handle the error from AAD server returned by ADAL or MSAL. """ | ||
|
|
||
| # https://docs.microsoft.com/en-us/azure/active-directory/develop/reference-aadsts-error-codes | ||
| # Search for an error code at https://login.microsoftonline.com/error | ||
| msg = error.get('error_description') | ||
| login_message = _generate_login_message(**kwargs) | ||
|
|
||
| from azure.cli.core.azclierror import AuthenticationError | ||
| raise AuthenticationError(msg, recommendation=login_message) | ||
|
|
||
|
|
||
| def _generate_login_command(scopes=None): | ||
| login_command = ['az login'] | ||
|
|
||
| if scopes: | ||
| login_command.append('--scope {}'.format(' '.join(scopes))) | ||
|
|
||
| return ' '.join(login_command) | ||
|
|
||
|
|
||
| def _generate_login_message(**kwargs): | ||
| from azure.cli.core.util import in_cloud_console | ||
| login_command = _generate_login_command(**kwargs) | ||
|
|
||
| msg = "To re-authenticate, please {}" .format( | ||
| "refresh Azure Portal." if in_cloud_console() else "run:\n{}".format(login_command)) | ||
|
|
||
| return msg | ||
|
|
||
|
|
||
| def decode_access_token(access_token): | ||
| # Decode the access token. We can do the same with https://jwt.ms | ||
| from msal.oauth2cli.oidc import decode_part | ||
| import json | ||
|
|
||
| # Access token consists of headers.claims.signature. Decode the claim part | ||
| decoded_str = decode_part(access_token.split('.')[1]) | ||
| return json.loads(decoded_str) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/azure-cli/azure/cli/command_modules/profile/tests/latest/test_auth_e2e.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| from azure.cli.core.azclierror import AuthenticationError | ||
| from azure.cli.testsdk import LiveScenarioTest | ||
| from azure.cli.core.auth.util import decode_access_token | ||
|
|
||
| ARM_URL = "https://eastus2euap.management.azure.com/" # ARM canary | ||
| ARM_MAX_RETRY = 30 | ||
| ARM_RETRY_INTERVAL = 10 | ||
|
|
||
|
|
||
| class ConditionalAccessScenarioTest(LiveScenarioTest): | ||
|
|
||
| def setUp(self): | ||
| super().setUp() | ||
| # Clear MSAL cache to avoid unexpected tokens from cache | ||
| self.cmd('az account clear') | ||
|
|
||
| def test_conditional_access_mfa(self): | ||
| """ | ||
| This test should be run using a user account that | ||
| - doesn't require MFA for ARM | ||
| - requires MFA for data-plane resource | ||
|
|
||
| The result ATs are checked per https://docs.microsoft.com/en-us/azure/active-directory/develop/access-tokens | ||
|
|
||
| Following claims are checked: | ||
| - aud (Audience): https://tools.ietf.org/html/rfc7519#section-4.1.3 | ||
| - amr (Authentication Method Reference): https://tools.ietf.org/html/rfc8176 | ||
| """ | ||
|
|
||
| resource = 'https://pas.windows.net/CheckMyAccess/Linux' | ||
| scope = resource + '/.default' | ||
|
|
||
| self.kwargs['scope'] = scope | ||
| self.kwargs['resource'] = resource | ||
|
|
||
| # region non-MFA session | ||
|
|
||
| # Login to ARM (MFA not required) | ||
| # In the browser, if the user already exists, make sure to logout first and re-login to clear browser cache | ||
| self.cmd('az login') | ||
|
|
||
| # Getting ARM AT and check claims | ||
| result = self.cmd('az account get-access-token').get_output_in_json() | ||
| decoded = decode_access_token(result['accessToken']) | ||
| assert decoded['aud'] == self.cli_ctx.cloud.endpoints.active_directory_resource_id | ||
| assert decoded['amr'] == ['pwd'] | ||
|
|
||
| # Getting data-plane AT with ARM RT (step-up) fails | ||
| with self.assertRaises(AuthenticationError) as cm: | ||
| self.cmd('az account get-access-token --resource {resource}') | ||
|
|
||
| # Check re-login recommendation | ||
| re_login_command = 'az login --scope {scope}'.format(**self.kwargs) | ||
| assert 'AADSTS50076' in cm.exception.error_msg | ||
| assert re_login_command in cm.exception.recommendations[0] | ||
|
|
||
| # endregion | ||
|
|
||
| # region MFA session | ||
|
|
||
| # Re-login with data-plane scope (MFA required) | ||
| # Getting ARM AT with data-plane RT (step-down) succeeds | ||
| self.cmd(re_login_command) | ||
|
|
||
| # Getting ARM AT and check claims | ||
| result = self.cmd('az account get-access-token').get_output_in_json() | ||
| decoded = decode_access_token(result['accessToken']) | ||
| assert decoded['aud'] == self.cli_ctx.cloud.endpoints.active_directory_resource_id | ||
| assert decoded['amr'] == ['pwd'] | ||
|
|
||
| # Getting data-plane AT and check claims | ||
| result = self.cmd('az account get-access-token --resource {resource}').get_output_in_json() | ||
| decoded = decode_access_token(result['accessToken']) | ||
| assert decoded['aud'] in scope | ||
| assert decoded['amr'] == ['pwd', 'mfa'] | ||
|
|
||
| # endregion |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
azure.cli.core.util.handle_exceptionis patched forScenarioTest. We have to patchazure.cli.core.util.handle_exceptionforLiveScenarioTestas well. Otherwise,AuthenticationErrorcan't be checked in tests.