dotnet / dotnet/yarp

Support `AuthorizationRoles` in RouteConfig to Avoid Policy Explosion

Open
#2,917 3 comments 0 reactions 0 assignees View on GitHub
Type: Idea
Dominant language
C#
Stars
9.6k
Forks
933
Avg merge
12d 18h
Merged PRs (30d)
2

Description

#### Current Situation

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

```cs
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:

```cs
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`](https://github.com/dotnet/yarp/blob/main/src/ReverseProxy/Routing/ProxyEndpointFactory.cs) this snippet:

```cs
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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.