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

feat: Cache requests for JWKS on JWT verification #228

Merged
merged 1 commit into from
Feb 12, 2024
Merged

Conversation

gkats
Copy link
Member

@gkats gkats commented Feb 8, 2024

The jwt.Verify method needs to fetch the JSON Web Key Set from the API in order to verify the session JWT's validity.

The jwt.Verify method is used in the http.WithHeaderAuthorization middleware, which means that in an HTTP server context, the method will executed for every request.

We're adding a caching layer for the JWKS when we verify the session JWT. This way we can cache the JWKS response from the API for 1 hour.

@gkats gkats requested a review from a team as a code owner February 8, 2024 09:51
@gkats
Copy link
Member Author

gkats commented Feb 8, 2024

Please note that I'm planning to look into ways of inserting a "clock" for replacing time calls, so that we can have more control over testing.

The main concern is to figure out an API for a clock that does not depend on external libraries. I have some ideas, but I consider them out of scope for this PR.

jwt/jwt.go Outdated Show resolved Hide resolved
// Caching store.
type cache struct {
mu sync.RWMutex
entries map[string]*cacheEntry
Copy link
Member

Choose a reason for hiding this comment

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

🙃 Without specific metrics in mind, shouldn't we consider offering a way to prune expired entries? Just in case this gets heavily used and starts to fill up the memory.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea. We can optimistically prune every time we force fetch. I can add this as an enhancement in a later PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

As far as I understand we currently store always one entry here, isn't that right?

jwt/jwt.go Outdated Show resolved Hide resolved
jwt/jwt.go Outdated Show resolved Hide resolved

// Try the cache first. Make sure we have a non-expired entry and
// that the value is a valid JWKS.
entry, ok := getCache().Get(cacheKey)
Copy link
Contributor

Choose a reason for hiding this comment

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

🔧 I think the cache must have the KID (instance ID) as key (or part of it). You can then quickly lookup by KID since that's the requirement from line 114. This way you can also have a multi-instance scenario handled.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good thinking. I'm just a bit reluctant to change the existing behavior. 🤔

As far as I understand the cache stores the whole json web key set response from the server. It was like this before and I'm a bit hesitant to change it now.

Regarding multi-instance scenarios, indeed, caching per instance is not possible with this setup, but I don't think it's needed for now.

I'll toy a bit with your proposal and open a separate PR if it doesn't break anything. Assuming that's alright, since you added a 🔧 comment. 😄

jwt/jwt.go Outdated Show resolved Hide resolved
jwt/jwt.go Outdated Show resolved Hide resolved
// Caching store.
type cache struct {
mu sync.RWMutex
entries map[string]*cacheEntry
Copy link
Contributor

Choose a reason for hiding this comment

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

As far as I understand we currently store always one entry here, isn't that right?

func (c *cache) Get(key string) (*cacheEntry, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
entry, ok := c.entries[key]
Copy link
Contributor

Choose a reason for hiding this comment

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

❓ If the entry is expired, I think you can ignore it here and don't return it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Was debating this as well. The entry is still there technically, but I guess users of a cache would expect to not get back expired entries?

Copy link
Contributor

Choose a reason for hiding this comment

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

If it's going to be replaced, sounds good to me to not return it. My suggestion just transfers the concern to the cache layer. Not a big deal.

The jwt.Verify method needs to fetch the JSON Web Key Set from the
API in order to verify the session JWT's validity.
The jwt.Verify method is used in the http.WithHeaderAuthorization
middleware, which means that in an HTTP server context, the method will
executed for every request.
We're adding a caching layer for the JWKS when we verify the session
JWT. This way we can cache the JWKS response from the API for 1 hour.
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.

3 participants