Skip to content

Allow specifying authz policies on endpoints #41153

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 12 commits into from
Apr 19, 2022
Merged

Allow specifying authz policies on endpoints #41153

merged 12 commits into from
Apr 19, 2022

Conversation

HaoK
Copy link
Member

@HaoK HaoK commented Apr 12, 2022

Continuation of #39892

davidfowl and others added 4 commits January 30, 2022 21:00
- The user can specify additional requirements or policies on the endpoint and they will be combined with existing IAuthorizeData.
@ghost ghost added the area-auth Includes: Authn, Authz, OAuth, OIDC, Bearer label Apr 12, 2022
@HaoK HaoK requested a review from davidfowl April 12, 2022 21:08
// REVIEW: should we check if there already is any authorize attributes first?
builder.Add(endpointBuilder =>
{
endpointBuilder.Metadata.Add(new AuthorizeAttribute());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check if any IAuthorizeData is already in the metadata so we don't add extra empty [Authorize]s?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I'm not sure where. Can we do it in this callback? We need an "only add metadata if not already added"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added a Any query inline here to prevent dupes, does that work?

@davidfowl
Copy link
Member

Can you update the code in SignalR as well?

cc @BrennanConroy

@HaoK
Copy link
Member Author

HaoK commented Apr 13, 2022

Sure what's the equivalent way to get endpoint metadata in SignalR? Looks like we just need to get the metadata policies and add that to the HubMethodDescriptor to flow it to the authZ logic, somewhere like here after we get the Authorize attributes on the method:

_methods[methodName] = new HubMethodDescriptor(executor, serviceProviderIsService, authorizeAttributes);

@BrennanConroy
Copy link
Member

Sounds about right, and then update

var authorizePolicy = await AuthorizationPolicy.CombineAsync(policyProvider, policies);
to use the policies.

@HaoK
Copy link
Member Author

HaoK commented Apr 13, 2022

So for SignalR, what's the best way for me to get access to the new endpoint metadata, I see the tests are using EndpointDataSource, should I be doing this as well to find the new auth policy metadata in DefaultHubDispatcher?

@BrennanConroy
Copy link
Member

Test for Hub method auth is at

public async Task AuthorizedConnectionCanInvokeHubMethodWithAuthorization()

@@ -549,21 +551,26 @@ private void InitializeHub(THub hub, HubConnectionContext connection)

private static Task<bool> IsHubMethodAuthorized(IServiceProvider provider, HubConnectionContext hubConnectionContext, HubMethodDescriptor descriptor, object?[] hubMethodArguments, Hub hub)
{
var endpoint = hubConnectionContext.Features.Get<IEndpointFeature>()?.Endpoint;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to where HubMethodDescriptor gets its AuthorizeData, we don't want the cost of these calls per Hub method invoke since it's static information.

@@ -549,21 +551,26 @@ private void InitializeHub(THub hub, HubConnectionContext connection)

private static Task<bool> IsHubMethodAuthorized(IServiceProvider provider, HubConnectionContext hubConnectionContext, HubMethodDescriptor descriptor, object?[] hubMethodArguments, Hub hub)
{
var endpoint = hubConnectionContext.Features.Get<IEndpointFeature>()?.Endpoint;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here was what I was asking about, how do I get the endpoint metadata out, this doesn't look correct because looking at the current auth tests, I don't think it even exposes anything like an HttpContext in the TestClient that the tests use, or at least other than stuffing in my own IEndpointFeature into the test client, it feels like I'm doing something wrong

Copy link
Member

@BrennanConroy BrennanConroy Apr 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so there is no endpoint for a Hub method. The endpoint is already running at this point in the code and has run AuthN/AuthZ for the endpoint.

We grab the metadata off the Hub method and use that. So if you look at DiscoverHubMethods(...) we grab the AuthorizeAttribute off of the methodInfo and will do the same for policies now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So how does that work for these new policies, since they only exist as metadata off an Endpoint, there is no authorize attribute equivalent I can look at without the endpoint metadata?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it doesn't apply until we do the [RequireClaims] change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidfowl what do we want to do exactly for signalR here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok confirmed in standup that we don't have to do anything here since it won't work, so I will remove these changes in the dispatcher for now

@HaoK HaoK marked this pull request as ready for review April 19, 2022 17:28
@HaoK HaoK requested review from halter73 and Tratcher as code owners April 19, 2022 17:28
@HaoK HaoK enabled auto-merge (squash) April 19, 2022 23:10
@HaoK HaoK merged commit 4f1c90b into main Apr 19, 2022
@HaoK HaoK deleted the haok/authz-meta branch April 19, 2022 23:22
@ghost ghost added this to the 7.0-preview4 milestone Apr 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-auth Includes: Authn, Authz, OAuth, OIDC, Bearer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants