Repository navigation
providers/oauth2: speed up the discovery endpoint - #27696
Conversation
The discovery and JWKS endpoints need only a signing key's public key and algorithm, but read both from the loaded private key. Loading an RSA private key checks its private components, which takes tens of milliseconds for a 3072-bit key and more for a 4096-bit one. The per-process cache of loaded keys rarely helps these endpoints: clients fetch them a few times a day, each worker restarts after 1000 requests, and the cache holds 128 keys. Read the public key from the key data without that check. Every path that stores a key already ran it, and anything that signs still loads the validated private key. Discovery also loads the application, provider and keys in one query, and fetches the anonymous user once for all scope mappings instead of once per mapping, as userinfo already shares one user between them. Dynamic client registration stays a separate query: joining it as well makes the query slower to plan than the extra round trip.
✅ Deploy Preview for authentik-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #27696 +/- ##
=======================================
Coverage 93.04% 93.05%
=======================================
Files 1237 1237
Lines 86390 86465 +75
Branches 4894 4894
=======================================
+ Hits 80383 80458 +75
Misses 5920 5920
Partials 87 87
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
Playwright e2eDownload this run's HTML report and traces, then open the report: gh run download 43244507686 -D playwright-artifacts
npx playwright show-report playwright-artifacts/playwright-reportInstallation instructionsInstructions for docker-composeAdd the following block to your AUTHENTIK_IMAGE=ghcr.io/goauthentik/dev-server
AUTHENTIK_TAG=gh-bd6431211321036019c176db3ed9e40614141e6b
# Distroless image: AUTHENTIK_TAG=gh-bd6431211321036019c176db3ed9e40614141e6b-distroless
AUTHENTIK_OUTPOSTS__CONTAINER_IMAGE_BASE=ghcr.io/goauthentik/dev-%(type)s:gh-%(build_hash)sAfterwards, run the upgrade commands from the latest release notes. Instructions for KubernetesAdd the following block to your authentik:
outposts:
container_image_base: ghcr.io/goauthentik/dev-%(type)s:gh-%(build_hash)s
global:
image:
repository: ghcr.io/goauthentik/dev-server
tag: gh-bd6431211321036019c176db3ed9e40614141e6b
# Distroless image: tag: gh-bd6431211321036019c176db3ed9e40614141e6b-distrolessAfterwards, run the upgrade commands from the latest release notes. |
- add OAuth2Provider.jwt_alg so discovery reads the signing algorithm from the provider instead of repeating jwt_key's HS256 fallback - share one parse function and one guarded loader between public_key and private_key, reusing KEY_LOAD_ERRORS - cache public key loads like private keys and certificates - check encryption_key_id in discovery instead of joining the key row - point the JWT_SIGNING_KEY_TYPES comment at from_public_key - test that an RSA key failing validation is published but can't sign - test that public keys are cached and loaded without the RSA key check
|
@jsachs See the following: https://bench.corp.goauthentik.io/testcases/pr-27696-oidc-discovery Due to failing e2e frontend tests however, benchmarks aren't running on your latest commit as the pipeline doesn't reach that far with a failing job |
|
I never got to play with this thing before, so data might be a bit weird before ~9:30 EST while i was trying stuff out (Oh PS, Latency will probably be lower in reality. Per Claude:
and throughput should also be higher in reality:
) |
jsachs
left a comment
There was a problem hiding this comment.
lgtm, I'm going to open a few related issues but none of them need doing here
| key_data.encode("utf-8"), | ||
| password=None, | ||
| backend=default_backend(), | ||
| unsafe_skip_rsa_key_validation=not validate, |
There was a problem hiding this comment.
I'd still rather not have this at all
There was a problem hiding this comment.
I moved the location of this parameter but am still quite confident that this is fine, since this only ever gets used for pubkeys:
load_pem_private_key(..., unsafe_skip_rsa_key_validation=True) crypto/models.py:68
└─ _load_public_key(): returns only .public_key() crypto/models.py:56
└─ CertificateKeyPair.public_key crypto/models.py:206
├─ JWKSView.get_jwk_for_key: publishes n/e oauth2/views/jwks.py:118
│ (OAuth2 JWKS, SSF JWKS, agent JWKS, Apple PSSO JWKS)
├─ OAuth2Provider.jwt_alg: alg name for discovery oauth2/models.py:377
└─ EndSessionView.validate: verifies id_token_hint oauth2/views/end_session.py:82
_parse_private_key took a validate flag and returned a private key, so an unvalidated key was one argument away from the signing path. Load the private key as main does, and skip RSA validation only inside _load_public_key, which returns just the public key.
…t-info' into web/element/ak-event-info * refs/remotes/origin/web/element/ak-event-info: (44 commits) A spelling error? In this economy? Convert one-line functions into arrow functions. Just explaining a seemingly random export in one of the renderers. web/component: ak-event-info Broke out renderers and switched to pattern matching. Holding pattern. First: Turn the renderer switch into a _function_ web/elements: AttributeView and TreeView: Provide a multi-modal view of raw JSON or YAML information (#27687) providers/oauth2: speed up the discovery endpoint (#27696) providers/saml: share xml signature generation (#27786) tests/openid_conformance: upload JPEG screenshots (#27749) crypto: export certificate expiry as a Prometheus metric (#27771) core: bump openapitools/openapi-generator-cli from v7.26.0 to v7.27.0 in /packages/client-ts (#27663) core: bump openapitools/openapi-generator-cli from v7.26.0 to v7.27.0 in /packages/client-rust (#27662) core: bump goauthentik/fips-debian from `d363011` to `16c391c` in /lifecycle/container (#27368) web: bump body-parser from 1.20.6 to 1.20.9 (#27521) core: bump library/node from `6d2c7d5` to `438603b` in /website (#27709) core: bump library/nginx from `5ab5b0b` to `165ddde` in /website (#27710) web: bump postcss-selector-parser from 7.1.7 to 7.2.0 in /web (#27742) web: bump glob from 13.0.6 to 13.0.7 (#27744) ...
Details
What does this PR change?
discovery and jwks were loading the signing key's private key just to publish public info. loading an RSA private key makes openssl run
RSA_check_key(checks p and q are prime etc), which is ~70ms for a 3072-bit key and ~155ms for 4096. neither endpoint needs the private key so:CertificateKeyPair.public_keyloads the key with the check skipped and only returns the public key.private_key(what signing uses) still runs the checkJWTAlgorithms.from_public_key,from_private_keyjust calls it nowJWKSView.get_jwk_for_keyis shared so the SSF, agent connector and apple psso jwks endpoints get the fix toolru_cacheon the jwk builder, it was keyed on the private key object and saved like microsecondsselect_relateds the app, provider, signing + encryption key in one query (7 -> 4 queries with claims cached). didn't join DCR too becuase postgres took 4ms just planning the 8 table joinresponses are byte for byte the same.
Why is this change needed?
on my homelab discovery was 200-330ms p50, and ~63% of a cold request was
load_pem_private_key. there's already a per-process key cache but workers restart every 1000 requests and clients only hit discovery a few times a day, so it basicaly never hits.one edge case: an RSA key that parses but fails the check (only possible by writing to the db directly) used to 500 on discovery and get skipped in jwks. now both publish its public key, signing with it still fails.
key load times
How was this tested?
seeded a sandbox shaped like my homelab (44 apps, 14 oauth2 providers, 3072-bit keys) plus a 10x set. cold caches, against current main:
warm caches it's the same (~6ms). at 10x main is slow even warm since the key cache can't hold 150 keys, this is 3.6-5.3x faster there.
all numbers
against current main, homelab-sized data, 200 requests:
earlier run against main at a24ae91 (before the rebase, middleware was slower then), median of 3 rounds.
homelab-sized, discovery:
homelab-sized, jwks:
10x (500 apps, 150 providers), discovery:
10x, jwks:
diffed the full responses of both endpoints for every provider vs main (EC, Ed25519/448, ML-DSA, HS256, encryption key, global issuer, DCR), all identical.
new tests: discovery + jwks work when loading the private key raises, discovery query count with cached claims, claims query count doesn't grow with scope mappings. all fail on main.
Linked issues
Checklist
make all)make docs)