reverse-proxy icon indicating copy to clipboard operation
reverse-proxy copied to clipboard

Setting and forwarding Client Cert directly from YARP

Open Octania opened this issue 3 years ago • 10 comments

Is it possible for YARP to get a Certificate from disk/Cert Store and then forward with a response? Based on the information here: https://microsoft.github.io/reverse-proxy/articles/transforms.html#clientcert The client certificate should already be present on the connection. My interpretation is that the Client App/Caller needs to forward the Cert to YARP.

I also attempted to implement the AddCertificateForwarding middleware: https://learn.microsoft.com/en-us/aspnet/core/security/authentication/certauth?view=aspnetcore-7.0 However, the Client Cert in never included in the request Header. This makes sense given that the middleware is instantiated once and not per request.

Octania avatar Sep 11 '23 23:09 Octania

Yes

Here's an example:

https://damienbod.com/2022/02/07/using-blazor-with-a-yarp-downstream-api-protected-using-certificate-authentication/

damienbod avatar Sep 12 '23 06:09 damienbod

Yes

Here's an example:

https://damienbod.com/2022/02/07/using-blazor-with-a-yarp-downstream-api-protected-using-certificate-authentication/

Saw this article and tried it. But I do not see a client cert in the response Header.

Octania avatar Sep 12 '23 13:09 Octania

What's the purpose of a certificate in a response header? Client certs are supposed to be sent with the request from the client to YARP and then the server, not from the server or YARP to the client.

Tratcher avatar Sep 12 '23 16:09 Tratcher

What's the purpose of a certificate in a response header? Client certs are supposed to be sent with the request from the client to YARP and then the server, not from the server or YARP to the client.

You are right, not the response but the request.

Let's assumed I have YARP running on a machine and making REQUESTS to a backend server. I need to add a Client Certificate to YARP so that the REQUEST from YARP are authenticated at the Backend Server. I do not want a cert included in the REQUEST to YARP but rather contained into YARP itself.

When a REQUEST is made to YARP, then YARP takes that REQUEST and include a CLIENT Cert and makes a request to the backend Server.

Octania avatar Sep 12 '23 17:09 Octania

Oh, you want to authenticate the proxy to the destination with a client cert. Try this: https://microsoft.github.io/reverse-proxy/articles/http-client-config.html#configuring-the-http-client

Tratcher avatar Sep 12 '23 18:09 Tratcher

Oh, you want to authenticate the proxy to the destination with a client cert. Try this: https://microsoft.github.io/reverse-proxy/articles/http-client-config.html#configuring-the-http-client

var clientCert = new X509Certificate2("path"); services.AddReverseProxy() .ConfigureHttpClient((context, handler) => { handler.SslOptions.ClientCertificates.Add(clientCert); })

I get object not set to instance of an object:

Octania avatar Sep 12 '23 22:09 Octania

I had the same error, which I fixed by calling handler.SslOptions.ClientCertificates = new X509Certificate2Collection(clientCert);

I'm not sure if this is the correct fix, because it is the first time I'm using YARP

Staijn1 avatar Sep 19 '23 11:09 Staijn1

I had the same error, which I fixed by calling handler.SslOptions.ClientCertificates = new X509Certificate2Collection(clientCert);

I'm not sure if this is the correct fix, because it is the first time I'm using YARP

Yes, this was also my solution and you beat me to it. We need to instantiate a new X509CertificateCollection as you pointed out before adding any certificate to the collection. The documentation clearly omits this information.

Thank you also.

Octania avatar Sep 19 '23 12:09 Octania

I had the same error, which I fixed by calling handler.SslOptions.ClientCertificates = new X509Certificate2Collection(clientCert); I'm not sure if this is the correct fix, because it is the first time I'm using YARP

Yes, this was also my solution and you beat me to it. We need to instantiate a new X509CertificateCollection as you pointed out before adding any certificate to the collection. The documentation clearly omits this information.

Thank you also.

Where does this line of code go?

pastrasigns avatar Dec 05 '24 00:12 pastrasigns

Hi there,

You can put this code in the .ConfigureHttpClient, after you call .AddReverseProxy().

In my case:

public void ConfigureServices(IServiceCollection services) {
      var certCollection = LoadClientCertificates(appSettings.ClientCertificateThumbprint);

      services
                .AddReverseProxy()
                .ConfigureHttpClient((context, handler) =>
                {
                    handler.SslOptions.ClientCertificates = certCollection;
                })
                .LoadFromConfig(Configuration.GetSection("ReverseProxy"));
}

private static X509Certificate2Collection LoadClientCertificates(string thumbprint)
{
            var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);

            try
            {
                store.Open(OpenFlags.ReadOnly);

                var certCollection = store.Certificates;
                var signingCert = certCollection.Find(X509FindType.FindByThumbprint, thumbprint, false);
                if (signingCert.Count == 0)
                {
                    throw new FileNotFoundException($"Cert with thumbprint: '{thumbprint}' not found in local machine cert store.");
                }

                Log.Logger.Information("Loaded client certificate with thumbprint: {thumbprint} successfully", thumbprint);
                return signingCert;
            }
            finally
            {
                store.Close();
            }
}

Staijn1 avatar Dec 07 '24 12:12 Staijn1