-
Notifications
You must be signed in to change notification settings - Fork 202
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
Scope to resource #785
base: dev
Are you sure you want to change the base?
Scope to resource #785
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 |
---|---|---|
|
@@ -32,8 +32,12 @@ def _scope_to_resource(scope): # This is an experimental reasonable-effort appr | |
if scope.startswith(a): | ||
return a | ||
u = urlparse(scope) | ||
if not u.scheme and not u.netloc: # Typically the "GUID/scope" case | ||
return u.path.split("/")[0] | ||
if u.scheme: | ||
return "{}://{}".format(u.scheme, u.netloc) | ||
trailer = ( # https://learn.microsoft.com/en-us/entra/identity-platform/scopes-oidc#trailing-slash-and-default | ||
"/" if u.path.startswith("//") else "") | ||
return "{}://{}{}".format(u.scheme, u.netloc, trailer) | ||
Comment on lines
+38
to
+40
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. If scopes = resource_to_scopes(resource) scope = resource + '/.default' So this scope-to-resource conversion is still not exactly the reverse of Azure CLI's process. 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. There is an edge case where the Imagine 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. This conversion logic will remove the path part. As mentioned in Azure/azure-cli#18312, valid identifier URI can also be
The trailing 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.
Not that the current
Theoretically, yes, but we never saw that before. And, most importantly, the "Cloud Shell's quirk" above contains several resources with one trailing slash, but never a double (or more) trailing slash.
That is arguably an expected behavior. Again, we have never encountered a Cloud Shell resource containing path, except the two known outliners which have already been handled properly. I will suggest we focus on this test case and verify its solving the "GUID/scope" use case. 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. There seem to be a lot of special cases here. Will there be other customers for this code path? 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. No other customer uses this code path. It is for Cloud Shell only, and not for any other managed identity. |
||
return scope # There is no much else we can do here | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import unittest | ||
from msal.cloudshell import _scope_to_resource | ||
|
||
class TestScopeToResource(unittest.TestCase): | ||
|
||
def test_expected_behaviors(self): | ||
for scope, expected_resource in { | ||
"https://analysis.windows.net/powerbi/api/foo": | ||
"https://analysis.windows.net/powerbi/api", # A special case | ||
"https://pas.windows.net/CheckMyAccess/Linux/.default": | ||
"https://pas.windows.net/CheckMyAccess/Linux/.default", # Special case | ||
"https://double-slash.com//scope": "https://double-slash.com/", | ||
"https://single-slash.com/scope": "https://single-slash.com", | ||
"guid/some/scope": "guid", | ||
rayluo marked this conversation as resolved.
Show resolved
Hide resolved
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. Is 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. Semantically, since the Cloud Shell's token endpoint is expecting a resource, so we consider the resource of a potential "GUID/path/subpath" input to be only the leading part i.e. the "GUID". Ideally, Cloud Shell will support scope directly in the future and then we will be able to remove all these workarounds. |
||
"6dae42f8-4368-4678-94ff-3960e28e3630/.default": | ||
# The real guid of AKS resource | ||
# https://learn.microsoft.com/en-us/azure/aks/kubelogin-authentication#how-to-use-kubelogin-with-aks | ||
"6dae42f8-4368-4678-94ff-3960e28e3630", | ||
}.items(): | ||
self.assertEqual(_scope_to_resource(scope), expected_resource) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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.
I don't agree with this logic. It can potentially strip important scope information and be seen as a security issue.
Since this is managed identity, we only deal with app scopes and there is 1:1 mapping between app scope and resource. i.e. scope = resource + "./default"
As such this logic should take it into account - i.e. strip "./default" if it exists. It should not attempt to remove anything else.
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.
This is the logic that MSAL.NET uses https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/blob/main//src/client/Microsoft.Identity.Client/Utils/ScopeHelper.cs#L106
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.
We discussed offline. The code path is for Azure CLI running inside Cloud Shell to get a token for the signed-in user. It is never about a managed machine. (That is also why it is misleading to expose Cloud Shell token acquisition as part of the managed identity.)
Currently, Cloud Shell's token endpoint accepts a resource parameter which shall not contain any scope; Cloud Shell also has special case that accepts "https://pas.windows.net/CheckMyAccess/Linux/.default" as-is. That .Net logic will not work here.