Sandor Molnar created KNOX-3627:
-----------------------------------

             Summary: Make JWKS HTTP connect/read timeouts configurable; the 
hard-coded 500 ms Nimbus defaults cause spurious 401s
                 Key: KNOX-3627
                 URL: https://issues.apache.org/jira/browse/KNOX-3627
             Project: Apache Knox
          Issue Type: Improvement
          Components: Server
            Reporter: Sandor Molnar
            Assignee: Sandor Molnar


h3. Problem

When a JWT/SSO topology verifies tokens against {{knox.token.jwks.url}} (or 
{{{}sso.token.verification.jwks.urls{}}}, or a trusted OIDC issuer's JWKS), 
{{DefaultTokenAuthorityService}} downloads the key set with the Nimbus defaults:
 * {{JWKSourceBuilder.DEFAULT_HTTP_CONNECT_TIMEOUT}} = 500 ms
 * {{JWKSourceBuilder.DEFAULT_HTTP_READ_TIMEOUT}} = 500 ms

Both the gateway-truststore retriever ({{{}jwksHttpClient(){}}}) and the 
issuer-CA retriever ({{{}IssuerTrustResourceRetriever{}}}) use these values. 
The JWK source is built with {{{}retrying(true){}}}, so it makes at most two 
attempts, about 1 s in total, before giving up. Neither timeout can be set in 
the topology or in gateway-site.

If the JWKS endpoint takes longer than that, verification fails and the request 
gets a 401 even though the token is valid. One example is another Knox instance 
serving {{/knoxtoken/api/v1/jwks.json}} right after a restart, when its first 
responses take about 1 s. Knox logs:
{noformat}
Failed to verify token using JWKS endpoint 
http://.../gateway/<topology>/knoxtoken/api/v1/jwks.json, reason: 
com.nimbusds.jose.RemoteKeySourceException: Couldn't retrieve JWK set from URL: 
Read timed out
{noformat}
{{outageTolerant}} does not help on a cold start, because nothing is cached yet.
h3. Aggravating factor

Knox keeps Nimbus's default rate limiter on: {{RateLimitedJWKSetSource}} allows 
2 retrievals per 30 s and counts failed ones. If the first cold fetches time 
out, later requests in that 30 s window fail at once with 
{{{}RateLimitReachedException{}}}. One slow response can therefore lead to up 
to about 30 s of 401s on that instance.
h3. Proposed fix
 * Add gateway-site properties, used by both JWKS retrievers:
 ** {{gateway.jwks.http.connect.timeout.ms}} (default 2000)
 ** {{gateway.jwks.http.read.timeout.ms}} (default 5000)
 * With the single retry, the worst case is 2 x (2 s + 5 s) = 14 s. This stays 
below the default {{gateway.jwks.cache.refresh.interval}} (15 s), so threads 
waiting on a cache load do not time out first.
 * For comparison, Knox's other outbound OIDC clients (trusted-issuer 
discovery, federated OP) already default to 3 s connect / 10 s read.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to