abp icon indicating copy to clipboard operation
abp copied to clipboard

Deploy, reverse proxy and cloudfare

Open gvassao opened this issue 2 years ago • 4 comments

To start with, I understand that this issue is not related to ABP. I'm looking for help to understand what's going on.

TLDR;

I have a layered solution deployed on a VPS using Caddy as a reverse proxy. Everything worked until I added my domain to cloudflare and enable proxying. After enabling proxying on Cloudfare, authentication between api.mydomain.com and auth.mydomain.com failed. I fixed it by adjusting token validation parameters in my code, but I don't understand why this is necessary with Cloudflare. Why does the issue occur with Cloudflare but not without it? Is this a common issue?

LONG VERSION:

I've searched through here and found some related posts, but the solutions didn't fully work for me. I also searched the internet (including stackoverflow) and went through many posts. I did find a solution, but I'd like to understand what is happening and why.

Some info:

  • Version: ABP 8.1.3
  • Layered solution with auth server and api deployed as two separate apps
    • API deployed to api.mydomain.com
    • Authserver deployed to auth.mydomain.com
  • Angular
  • SQL Server

Both the auth server and the API are running on the same Ubuntu Linux VPS and are set up as a service using systemd. I also have Caddy working as a reverse proxy. Both apps have SSL setup by Caddy so they run over SSL. Caddy reverses the proxy to localhost which is using a self sign certificate for localhost https.

With this setup, everything was working great until I decided to add my domain to Cloudflare and enable their proxy so basically Cloudflare proxies the requests to my VPS and my VPS stays "hidden". When I did this initially, I encountered some issues that I managed to resolve using the Cloudflare panel (SSL redirections and others). Then I thought everything was working, but I realized it was not.

I can go to api.mydomain.com, hit the swagger authorize button, which takes me to auth.mydomain.com where I log-in and return to swagger. If I try to hit a protected endpoint, I get an error:

2024-06-10 20:39:16.596 -05:00 [INF] Request starting HTTP/2 GET https://api.mydomain.com/api/my/endpoint - null null
2024-06-10 20:39:16.599 -05:00 [INF] Failed to validate the token.
Microsoft.IdentityModel.Tokens.SecurityTokenInvalidIssuerException: IDX10204: Unable to validate issuer. validationParameters.ValidIssuer is null or whitespace AND validationParameters.ValidIssuers is null or empty.
   at Microsoft.IdentityModel.Tokens.Validators.ValidateIssuerAsync(String issuer, SecurityToken securityToken, TokenValidationParameters validationParameters, BaseConfiguration configuration)
   at Microsoft.IdentityModel.Tokens.Validators.ValidateIssuer(String issuer, SecurityToken securityToken, TokenValidationParameters validationParameters, BaseConfiguration configuration)
   at Microsoft.IdentityModel.Tokens.InternalValidators.ValidateAfterSignatureFailed(SecurityToken securityToken, Nullable`1 notBefore, Nullable`1 expires, IEnumerable`1 audiences, TokenValidationParameters validationParameters, BaseConfiguration configuration)
   at Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler.ValidateSignature(JsonWebToken jwtToken, TokenValidationParameters validationParameters, BaseConfiguration configuration)
   at Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler.ValidateSignatureAndIssuerSecurityKey(JsonWebToken jsonWebToken, TokenValidationParameters validationParameters, BaseConfiguration configuration)
   at Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler.ValidateJWSAsync(JsonWebToken jsonWebToken, TokenValidationParameters validationParameters, BaseConfiguration configuration)
2024-06-10 20:39:16.601 -05:00 [INF] Bearer was not authenticated. Failure message: IDX10204: Unable to validate issuer. validationParameters.ValidIssuer is null or whitespace AND validationParameters.ValidIssuers is null or empty.
2024-06-10 20:39:16.604 -05:00 [INF] Authorization failed. These requirements were not met:
DenyAnonymousAuthorizationRequirement: Requires an authenticated user.
2024-06-10 20:39:16.607 -05:00 [INF] AuthenticationScheme: Bearer was challenged.
2024-06-10 20:39:16.611 -05:00 [INF] Request finished HTTP/2 GET https://api.mydomain.com/api/my/endpoint - 401 0 null 14.2465ms

At this point, I thought it was something with headers. I tried all sorts of solutions without success. I didn't want to change the application code because it works without the Cloudflare proxy. If I go to their panel and disable the proxy which makes the requests hit my VPS directly it works. After around 10 hours of trying to get this to work, I gave up and decided to try and make the code happy. As suggested by some other posts here, the first thing I did was to add:

options.TokenValidationParameters.ValidIssuer = configuration["AuthServer:Authority"];

to the ConfigureAuthentication of the API. This made the error go away, but then I started to see another issue:

2024-06-10 22:09:49.215 -05:00 [INF] Request starting HTTP/2 GET https://api.mydomain.com/api/my/endpoint - null null
2024-06-10 22:09:49.366 -05:00 [INF] Failed to validate the token.
Microsoft.IdentityModel.Tokens.SecurityTokenSignatureKeyNotFoundException: IDX10500: Signature validation failed. No security keys were provided to validate the signature.
   at Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler.ValidateSignature(JsonWebToken jwtToken, TokenValidationParameters validationParameters, BaseConfiguration configuration)
   at Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler.ValidateSignatureAndIssuerSecurityKey(JsonWebToken jsonWebToken, TokenValidationParameters validationParameters, BaseConfiguration configuration)
   at Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler.ValidateJWSAsync(JsonWebToken jsonWebToken, TokenValidationParameters validationParameters, BaseConfiguration configuration)
2024-06-10 22:09:49.366 -05:00 [INF] Bearer was not authenticated. Failure message: IDX10500: Signature validation failed. No security keys were provided to validate the signature.
2024-06-10 22:09:49.377 -05:00 [INF] Authorization failed. These requirements were not met:
DenyAnonymousAuthorizationRequirement: Requires an authenticated user.
2024-06-10 22:09:49.381 -05:00 [INF] AuthenticationScheme: Bearer was challenged.
2024-06-10 22:09:49.381 -05:00 [INF] Request finished HTTP/2 GET https://api.mydomain.com/api/my/endpoint - 401 0 null 166.7045ms

Next, I added (to the same ConfigureAuthentication method of the API):

var cert = new X509Certificate2("openiddict.pfx", "XXX....");
var securityKey = new X509SecurityKey(cert);

options.TokenValidationParameters.IssuerSigningKey = securityKey;

Then it started working again! My question is why? Why is this needed? Why is it only a problem when there seems to be two proxies (user > Cloudflare > my server > reverse proxy) but it works when it's (user > my server > reverse proxy)? Is this a common issue that people encounter, but it's not just on the default template? ValidIssuers are not setup on the default template but it still works when it's not behind Cloudfare. I'm at a loss to understand why this is needed only when I enable my domain on Cloudfare and if I'm on the right track for a production environment.

gvassao avatar Jun 11 '24 06:06 gvassao

hi

IDX10204

Have you configured your app to Forwarded headers? https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-8.0#fhmo

validationParameters.ValidIssuer will be set as request.Scheme + Uri.SchemeDelimiter + host + request.PathBase

You can add middleware to output the HTTP request info to logs so you can see the requesting host.

IDX10500

Make sure your API can get openid well-known info from AuthServer(Authority URL) project.

The IssuerSigningKey is from the well-known endpoint.

https://account.abp.io/.well-known/openid-configuration https://account.abp.io/.well-known/jwks

maliming avatar Jun 11 '24 07:06 maliming

@maliming Thanks for your answer. It pointed me in the right direction.

I tried accessing the well-known endpoints from the VPS, but it wasn't working due to a firewall rule on Cloudflare's side. That's why it worked when the traffic wasn't proxied through Cloudflare. Once I fixed that rule, I removed all the custom changes I mentioned, as well as the forward headers I had added to Caddy, and everything seems to be working now.

Long story short, the problem was that the API couldn't reach the external address for my auth server.

gvassao avatar Jun 11 '24 19:06 gvassao

Great.

maliming avatar Jun 12 '24 00:06 maliming

abp version is 8.1.4. when i use the IP to access, it doesn't work properly. failed to validate the token. IDX10204: Unable to validate issuer. validationParameters.ValidIssuer is null or whitespace AND validationParameters.ValidIssuers is null or empty.

2024-06-13_193101 2024-06-13_193335

wangyue20075 avatar Jun 13 '24 11:06 wangyue20075