0

I am trying to make a reverse proxy in my application. In my case, I need the path to be transformed to include information stored in the request header.

"ReverseProxy": {
"Routes": {
  "MyRoute": {
    "ClusterId": "MyCluster",
    "AuthorizationPolicy": "DefaultPolicy",
    "Match": {
      "Path": "/api/{**remainder}"
    },
    "Transforms": [
      { "PathPattern": "/api/{item}/{**remainder}" },
      {
        "ResponseHeader": "Source",
        "Append": "YARP",
        "When": "Success"
      }
    ]
  }
},
"Clusters": {
  "MyCluster": {
    "Destinations": {
      "MyCluster/destination": {
        "Address": "https://myAddress.com/"
      }
    }
  }
}

Trying to create a custom transform:

services.AddReverseProxy()
.LoadFromConfig(_configuration.GetSection("ReverseProxy"))
.AddTransforms(builderContext =>
{
    builderContext.RequestTransforms.Add(new Yarp.ReverseProxy.Transforms.RequestTransform()
    {

    }
});

Can I replace {item} with the information included in the request header?

fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88
Chris
  • 329
  • 1
  • 4
  • 16
  • You can try creating a [custom transform](https://microsoft.github.io/reverse-proxy/articles/transforms.html#addrequesttransform) with [AddRequestTransform](https://microsoft.github.io/reverse-proxy/api/Yarp.ReverseProxy.Transforms.TransformBuilderContextFuncExtensions.html#Yarp_ReverseProxy_Transforms_TransformBuilderContextFuncExtensions_AddRequestTransform_Yarp_ReverseProxy_Transforms_Builder_TransformBuilderContext_System_Func_Yarp_ReverseProxy_Transforms_RequestTransformContext_System_Threading_Tasks_ValueTask__) in code. – Panagiotis Kanavos Aug 24 '22 at 15:25
  • In the transform method you can read the [RequestTransformContext.HttpContext](https://microsoft.github.io/reverse-proxy/api/Yarp.ReverseProxy.Transforms.RequestTransformContext.html#Yarp_ReverseProxy_Transforms_RequestTransformContext_HttpContext).Request.Headers collection and calculate a new `Path` value – Panagiotis Kanavos Aug 24 '22 at 15:27
  • I have tried that method and for some reason I do not have the AddRequestTransform method. – Chris Aug 24 '22 at 15:49
  • This method was added in YARP 1.1. Which version do you have? – Panagiotis Kanavos Aug 24 '22 at 15:50
  • I have version 1.1.1 – Chris Aug 24 '22 at 15:52
  • Post your code please – Panagiotis Kanavos Aug 24 '22 at 15:53
  • It seems I need to do builderContext.RequestTransforms.Add – Chris Aug 24 '22 at 15:53
  • VS did not automatically recognize I needed Yarp.ReverseProxy.Transforms – Chris Aug 24 '22 at 16:17

1 Answers1

1

You can create a proxy pipeline middleware.

app.UseEndpoints(endpoints =>    
{
    endpoints.MapReverseProxy(proxyPipeline =>        
    {
        proxyPipeline.Use((context, next) =>
        {
            //You can use context to change request before they are sent.
        });
    });
});

Follow this guide on adding middleware. The placement of UseRouting and UseEndpoints matters when creating middleware for authentication.

Chris
  • 329
  • 1
  • 4
  • 16