-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[Core] Use MSAL for Cloud Shell authentication #29637
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| AZURE_CLI_CLIENT_ID = '04b07795-8ddb-461a-bbee-02f9e1bf7b46' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| from knack.util import CLIError | ||
| from msal import PublicClientApplication, ConfidentialClientApplication | ||
|
|
||
| from .constants import AZURE_CLI_CLIENT_ID | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As Although circular imports can also be avoided by importing modules within a function or method, this is not as good as the current solution. |
||
| from .util import check_result, build_sdk_access_token | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
@@ -108,3 +109,25 @@ def get_token(self, *scopes, **kwargs): | |
| result = self._msal_app.acquire_token_for_client(list(scopes), **kwargs) | ||
| check_result(result) | ||
| return build_sdk_access_token(result) | ||
|
|
||
|
|
||
| class CloudShellCredential: # pylint: disable=too-few-public-methods | ||
| # Cloud Shell acts as a "broker" to obtain access token for the user account, so even though it uses | ||
| # managed identity protocol, it returns a user token. | ||
| # That's why MSAL uses acquire_token_interactive to retrieve an access token in Cloud Shell. | ||
| # See https://github.com/Azure/azure-cli/pull/29637 | ||
|
|
||
| def __init__(self): | ||
| self._msal_app = PublicClientApplication( | ||
| AZURE_CLI_CLIENT_ID, # Use a real client_id, so that cache would work | ||
| # TODO: We currently don't maintain an MSAL token cache as Cloud Shell already has its own token cache. | ||
| # Ideally we should also use an MSAL token cache. | ||
| # token_cache=... | ||
| ) | ||
|
Comment on lines
+121
to
+126
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I personally don't agree with MSAL's design and explanation (#29637 (comment)). Squeezing Cloud Shell authentication into Even though MSAL uses Also, |
||
|
|
||
| def get_token(self, *scopes, **kwargs): | ||
| logger.debug("CloudShellCredential.get_token: scopes=%r, kwargs=%r", scopes, kwargs) | ||
| # kwargs is already sanitized by CredentialAdaptor, so it can be safely passed to MSAL | ||
| result = self._msal_app.acquire_token_interactive(list(scopes), prompt="none", **kwargs) | ||
|
Comment on lines
+130
to
+131
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| check_result(result, scopes=scopes) | ||
| return build_sdk_access_token(result) | ||
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.
The name
credsconflicts with the "credential" concept (msal_credentials.py), so I rename it to better reflect its content.