Skip to content

Support AuthorizationRoles in RouteConfig to Avoid Policy Explosion #2917

@gumbarros

Description

@gumbarros

Current Situation

When configuring routes dynamically from the database, I’m using this pattern:

routes.Add(new RouteConfig
{
    RouteId = $"{route.AppId}_{route.Id}",
    ClusterId = clusterId,
    Match = new RouteMatch
    {
        Path = routePath,
        QueryParameters = queryParameters.Count > 0 ? queryParameters : null
    },
    AuthorizationPolicy = RoutePrefix + route.Id,
    Transforms = transforms
});

This forces me to create one authorization policy per route. In our case, we have ~300 routes, resulting in:

  • 300 custom authorization policies
  • 300 DB calls on startup to resolve those policies

Proposed Change

It would be significantly more efficient if I could instead define roles directly in the route config like this:

routes.Add(new RouteConfig
{
    RouteId = $"{route.AppId}_{route.Id}",
    ClusterId = clusterId,
    Match = new RouteMatch
    {
        Path = routePath,
        QueryParameters = queryParameters.Count > 0 ? queryParameters : null
    },
    AuthorizationRoles = route.Roles.Select(r => r.Name),
    Transforms = transforms
});

It made sense in my head, just add at ProxyEndpointFactory this snippet:

else if (config.AuthorizationRoles != null && config.AuthorizationRoles.Length > 0)
{
    endpointBuilder.Metadata.Add(new AuthorizeAttribute
    {
        Roles = string.Join(",", config.AuthorizationRoles)
    });
}

Why This Matters

  • Reduces the need to define and register hundreds of policies dynamically
  • Simplifies route configuration and improves maintainability
  • Aligns with how [Authorize(Roles = "...")] already works in controllers and endpoints

If there’s a way supported by YARP for role-based auth at the route level (without needing per-route policies), I’m open to it. I checked the Metadata prop, but idk how to consume it at a single authorization policy.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type: IdeaThis issue is a high-level idea for discussion.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions