Skip to content

feat: add support for cacheing JWKS - #579

Open
ianroberts wants to merge 5 commits into
openpubkey:mainfrom
ianroberts:jwks-cache
Open

ianroberts wants to merge 5 commits into
openpubkey:mainfrom
ianroberts:jwks-cache

Conversation

@ianroberts

@ianroberts ianroberts commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

This PR builds on the work in openpubkey/openpubkey#364 - the openpubkey dep has been updated to the commit where that PR was merged, it will need to be updated again once the openpubkey changes make it into a tagged version

Add the option to configure a cache directory and max age settings in the server YAML config file, and feed these through to create a FilesystemDiscoveryCache used by the providers to cache JWKS entries. This avoids the providers having to fetch the JWKS from the .well-known discovery URL for every single SSH login.

Note this creates a bit of a race condition because

  • we can't parse the server config until the NewVerifyCmd has been created
  • NewVerifyCmd expects the verifier as a parameter
  • we can't create the verifier until we have parsed the server config file

So I've had to change the flow in main.go to create the VerifyCmd without a verifier, then parse the server config, then use the config to create a verifier, then assign that verifier to the right field of the VerifyCmd.

Design notes

I've deliberately designed things so that the FilesystemDiscoveryCache methods called by opkssh verify can read and create files under the cache folder, but will never delete anything. I've added a separate command opkssh cache clean that is responsible for cleaning up stale cache entries, it should be run periodically as a cron job.

Testing

I've added a unit test for the FilesystemDiscoveryCache itself but I'm not sure how to wire in proper end to end testing.

Fixes #440

@ianroberts
ianroberts force-pushed the jwks-cache branch 4 times, most recently from 94572f1 to 11f8904 Compare September 6, 2026 14:55
Add the option to configure a cache directory and max age settings in the server YAML config file, and feed these through to create a FilesystemDiscoveryCache used by the providers to cache JWKS entries.  This avoids the providers having to fetch the JWKS from the provider for every single SSH login.

Note this creates a bit of a race condition because

- we can't parse the server config until the NewVerifyCmd has been created
- NewVerifyCmd expects the verifier as a parameter
- we can't create the verifier until we have parsed the server config file

So I've had to change the flow in main.go to create the VerifyCmd without a verifier, then parse the server config, then use the config to create a verifier, then assign that verifier to the right field of the VerifyCmd.

Signed-off-by: Ian Roberts <ian@roberts.gb.net>
Signed-off-by: Ian Roberts <ian@roberts.gb.net>
Signed-off-by: Ian Roberts <ian@roberts.gb.net>
Signed-off-by: Ian Roberts <ian@roberts.gb.net>
I've made a deliberate design decision that `oppkssh verify` will never itself delete anything from the cache, since multiple instances may potentially be running at the same time.  Instead I've introduced a specific command `opkssh cache clean` that will delete anything in the cache that is older than the `fallback_max_age` - this should be set up to run regularly as a cron job under the `opkssh` UID.

Signed-off-by: Ian Roberts <ian@roberts.gb.net>
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.

Support for caching JWKS

1 participant