Skip to content
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

Cache RS256 JWKS by url to allow for multiple Auth0 tenants per runtime #325

Merged
merged 2 commits into from
Mar 25, 2022

Conversation

rmm5t
Copy link
Contributor

@rmm5t rmm5t commented Mar 23, 2022

Fixes #324

Changes

The underlying issue is discussed in #324, but in summary, JWT validation using RS256 caches the JWKS for the entire runtime, even if multiple Auth0 tenants (varying JWKS URLs are in use). This change caches the JWKS per JWKS url instead of using just a generic cache key. This allows applications that use multiple Auth0 tenants to properly cache their JWKS and avoid unnecessary HTTP requests to pull the well-known JWKS. Otherwise, the JWKS cache thrashes itself while switching between multiple Auth0 tenants.

References

Fixes #324

Testing

  • This change adds unit test coverage
  • This change has been tested on the latest version of Ruby

Checklist

Copy link
Contributor

@stevehobbsdev stevehobbsdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution here 🎉 just left a comment, interested in your thoughts on it.

@@ -286,7 +286,7 @@ def jwks_url(url, lifetime: 10 * 60)

# Clear the JWK set cache.
def remove_jwks
@@cache.remove(:jwks)
@@cache.remove_by { true }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only issue I have here is this now removes everything from the shared cache. Granted, currently the only things being stored there are JWKS documents but could introduce bugs later if we have a need to start storing other things in the cache and forget about this.

However, not sure what you can do about it at this stage unless you also start tracking the URLs you're adding into it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cache only exists for RS256 validation, and purging a cache should always be recoverable regardless of what’s in it. Also, this is only an improvement over the existing implementation, because the cache wasn’t behaving properly when more than one tenant was in play.

With that said, I could prefix cache-keys with something like ”jwks-“ and only purge keys that start with that.

If you feel that’s important, I am happy to do that, but I don’t actually see this cache being used for much else.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, if that issue happens to occur later, we can solve it then.

@stevehobbsdev stevehobbsdev merged commit 6f11a5d into auth0:master Mar 25, 2022
@stevehobbsdev stevehobbsdev mentioned this pull request Mar 25, 2022
@rmm5t rmm5t deleted the 324-fix-jwks-cache-thrash branch March 25, 2022 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RS256 JWKS cache thrashes when using multiple Auth0 clients/tenants in same application.
2 participants