-
Notifications
You must be signed in to change notification settings - Fork 25.1k
YARP migration /2 #34657
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
YARP migration /2 #34657
Changes from 4 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
26c5805
YARP migration /2
Rick-Anderson b7d7b3b
YARP migration /2
Rick-Anderson 3f1c708
YARP migration /2
Rick-Anderson b04ba90
YARP migration /2
Rick-Anderson 095c73d
YARP migration /2
Rick-Anderson 06684dd
YARP migration /2
Rick-Anderson 9e0f24a
YARP migration /2
Rick-Anderson 25e9b80
YARP migration /2
Rick-Anderson ffb2a7e
YARP migration /2
Rick-Anderson 3d6bda4
YARP migration /2
Rick-Anderson fb8c87a
YARP migration /2
Rick-Anderson be78d8c
YARP migration /2
Rick-Anderson f054deb
YARP migration /2
Rick-Anderson 027b7cd
YARP migration /2
Rick-Anderson 7e7e136
YARP migration /2
Rick-Anderson 7faeea1
YARP migration /2
Rick-Anderson a1be05d
YARP migration /2
Rick-Anderson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| --- | ||
| uid: fundamentals/servers/yarp/ab-testing | ||
| title: A/B Testing and Rolling Upgrades | ||
| description: A/B Testing and Rolling Upgrades | ||
| author: rick-anderson | ||
| ms.author: riande | ||
| ms.date: 02/06/2025 | ||
| ms.topic: article | ||
| content_well_notification: AI-contribution | ||
| ms.prod: aspnet-core | ||
| ai-usage: ai-assisted | ||
| --- | ||
|
|
||
| # A/B Testing and Rolling Upgrades | ||
|
|
||
| ## Introduction | ||
|
|
||
| A/B testing and rolling upgrades require procedures for dynamically assigning incoming traffic to evaluate changes in the destination application. YARP does not have a built-in model for this, but it does expose some infrastructure useful for building such a system. See [issue #126](https://github.com/microsoft/reverse-proxy/issues/126) for additional details about this scenario. | ||
|
|
||
| ## Example | ||
|
|
||
| ``` | ||
| app.MapReverseProxy(proxyPipeline => | ||
| { | ||
| // Custom cluster selection | ||
| proxyPipeline.Use((context, next) => | ||
| { | ||
| var lookup = context.RequestServices.GetRequiredService<IProxyStateLookup>(); | ||
| if (lookup.TryGetCluster(ChooseCluster(context), out var cluster)) | ||
| { | ||
| context.ReassignProxyRequest(cluster); | ||
| } | ||
|
|
||
| return next(); | ||
| }); | ||
| proxyPipeline.UseSessionAffinity(); | ||
| proxyPipeline.UseLoadBalancing(); | ||
| }); | ||
|
|
||
| string ChooseCluster(HttpContext context) | ||
| { | ||
| // Decide which cluster to use. This could be random, weighted, based on headers, etc. | ||
| return Random.Shared.Next(2) == 1 ? "cluster1" : "cluster2"; | ||
| } | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| This scenario makes use of two APIs, [IProxyStateLookup](xref:Yarp.ReverseProxy.IProxyStateLookup) and [ReassignProxyRequest](xref:Microsoft.AspNetCore.Http.HttpContextFeaturesExtensions.ReassignProxyRequest(Microsoft.AspNetCore.Http.HttpContext,Yarp.ReverseProxy.Model.ClusterState)), called from a custom proxy middleware as shown in the sample above. | ||
|
|
||
| `IProxyStateLookup` is a service available in the Dependency Injection container that can be used to look up or enumerate the current routes and clusters. Note this data may change if the configuration changes. An A/B orchestration algorithm can examine the request, decide which cluster to send it to, and then retrieve that cluster from `IProxyStateLookup.TryGetCluster`. | ||
|
|
||
| Once the cluster is selected, `ReassignProxyRequest` can be called to assign the request to that cluster. This updates the [IReverseProxyFeature](xref:Yarp.ReverseProxy.Model.IReverseProxyFeature) with the new cluster and destination information needed for the rest of the proxy middleware pipeline to handle the request. | ||
|
|
||
| ## Session affinity | ||
|
|
||
| Note that session affinity functionality is split between middleware, which reads it settings from the current cluster, and transforms, which are part of the original route. Clusters used for A/B testing should use the same session affinity configuration to avoid conflicts. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| --- | ||
| uid: fundamentals/servers/yarp/authn-authz | ||
| title: Authentication and Authorization | ||
| description: Authentication and Authorization | ||
| author: rick-anderson | ||
| ms.author: riande | ||
| ms.date: 02/06/2025 | ||
| ms.topic: article | ||
| content_well_notification: AI-contribution | ||
| ms.prod: aspnet-core | ||
| ai-usage: ai-assisted | ||
| --- | ||
|
|
||
| # Authentication and Authorization | ||
|
|
||
| ## Introduction | ||
| The reverse proxy can be used to authenticate and authorize requests before they are proxied to the destination servers. This can reduce load on the destination servers, add a layer of protection, and ensure consistent policies are implemented across your applications. | ||
|
|
||
| ## Defaults | ||
|
|
||
| No authentication or authorization is performed on requests unless enabled in the route or application configuration. | ||
|
|
||
| ## Configuration | ||
| Authorization policies can be specified per route via [RouteConfig.AuthorizationPolicy](xref:Yarp.ReverseProxy.Configuration.RouteConfig) and can be bound from the `Routes` sections of the config file. As with other route properties, this can be modified and reloaded without restarting the proxy. Policy names are case insensitive. | ||
|
|
||
| Example: | ||
| ```JSON | ||
| { | ||
| "ReverseProxy": { | ||
| "Routes": { | ||
| "route1" : { | ||
| "ClusterId": "cluster1", | ||
| "AuthorizationPolicy": "customPolicy", | ||
| "Match": { | ||
| "Hosts": [ "localhost" ] | ||
| } | ||
| } | ||
| }, | ||
| "Clusters": { | ||
| "cluster1": { | ||
| "Destinations": { | ||
| "cluster1/destination1": { | ||
| "Address": "https://localhost:10001/" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| [Authorization policies](/aspnet/core/security/authorization/policies) are an ASP.NET Core concept that the proxy utilizes. The proxy provides the above configuration to specify a policy per route and the rest is handled by existing ASP.NET Core authentication and authorization components. | ||
|
|
||
| Authorization policies can be configured in the application as follows: | ||
| ``` | ||
| services.AddAuthorization(options => | ||
| { | ||
| options.AddPolicy("customPolicy", policy => | ||
| policy.RequireAuthenticatedUser()); | ||
| }); | ||
| ``` | ||
|
|
||
| In Program.cs add the Authorization and Authentication middleware. | ||
|
|
||
| ``` | ||
| app.UseAuthentication(); | ||
| app.UseAuthorization(); | ||
|
|
||
| app.MapReverseProxy(); | ||
| ``` | ||
|
|
||
| See the [Authentication](/aspnet/core/security/authentication/) docs for setting up your preferred kind of authentication. | ||
|
|
||
| ### Special values: | ||
|
|
||
| In addition to custom policy names, there are two special values that can be specified in a route's authorization parameter: `default` and `anonymous`. ASP.NET Core also has a FallbackPolicy setting that applies to routes that do not specify a policy. | ||
|
|
||
| #### DefaultPolicy | ||
|
|
||
| Specifying the value `default` in a route's authorization parameter means that route will use the policy defined in [AuthorizationOptions.DefaultPolicy](https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.authorization.authorizationoptions.defaultpolicy?#Microsoft_AspNetCore_Authorization_AuthorizationOptions_DefaultPolicy). That policy is pre-configured to require authenticated users. | ||
|
|
||
| #### Anonymous | ||
|
|
||
| Specifying the value `anonymous` in a route's authorization parameter means that route will not require authorization regardless of any other configuration in the application such as the FallbackPolicy. | ||
|
|
||
| #### FallbackPolicy | ||
|
|
||
| [AuthorizationOptions.FallbackPolicy](https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.authorization.authorizationoptions.fallbackpolicy) is the policy that will be used for any request or route that was not configured with a policy. FallbackPolicy does not have a value by default, any request will be allowed. | ||
|
|
||
| ## Flowing Credentials | ||
|
|
||
| Even after a request has been authorized in the proxy, the destination server may still need to know who the user is (authentication) and what they're allowed to do (authorization). How you flow that information will depend on the type of authentication being used. | ||
|
|
||
| ### Cookie, bearer, API keys | ||
|
|
||
| These authentication types already pass their values in the request headers and these will flow to the destination server by default. That server will still need to verify and interpret those values, causing some double work. | ||
|
|
||
| ### OAuth2, OpenIdConnect, WsFederation | ||
|
|
||
| These protocols are commonly used with remote identity providers. The authentication process can be configured in the proxy application and will result in an authentication cookie. That cookie will flow to the destination server as a normal request header. | ||
|
|
||
| ### Windows, Negotiate, NTLM, Kerberos | ||
|
|
||
| These authentication types are often bound to a specific connection. They are not supported as means of authenticating a user in a destination server behind the YARP proxy (see [#166](https://github.com/microsoft/reverse-proxy/issues/166). They can be used to authenticate an incoming request to the proxy, but that identity information will have to be communicated to the destination server in another form. They can also be used to authenticate the proxy to the destination servers, but only as the proxy's own user, impersonating the client is not supported. | ||
|
|
||
| ### Client Certificates | ||
|
|
||
| Client certificates are a TLS feature and are negotiated as part of a connection. See [these docs](/aspnet/core/security/authentication/certauth) for additional information. The certificate can be forwarded to the destination server as an HTTP header using the [ClientCert](transforms.md#clientcert) transform. | ||
|
|
||
| ### Swapping authentication types | ||
|
|
||
| Authentication types like Windows that don't flow naturally to the destination server will need to be converted in the proxy to an alternate form. For example a JWT bearer token can be created with the user information and set on the proxy request. | ||
|
|
||
| These swaps can be performed using [custom request transforms](transforms.md#from-code). Detailed examples can be developed for specific scenarios if there is enough community interest. We need more community feedback on how you want to convert and flow identity information. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should these code blocks have ```csharp so the syntax highlighting is correct?
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.
yes, next pass. This pass is to get a clean build. The PR merges into a staging branch.
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.
@adityamandaleeka
Ultimately, we'll move all the code snippets to project files and import the code. That way we can make sure they compile. That allows highlights too.