-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Core] Conditional Access: Show
--scope
for az login
message when…
… failed to refresh the access token (#17738)
- Loading branch information
Showing
7 changed files
with
161 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 |