Skip to content
ecdsa.com
Envoy / IstioJWKS and key discovery

Error message

Jwks doesn't have key to match kid or alg from Jwt

Also printed as

  • Jwt verification fails
  • Jwks remote fetch is failed

Response body of the 401; the `WWW-Authenticate` header carries `error="invalid_token"`. A provider whose `remote_jwks` cannot be fetched answers "Jwks remote fetch is failed".

Envoy / Istio — envoy.filters.http.jwt_authn (401 response body; Istio RequestAuthentication)

What it means

Envoy's `jwt_authn` filter (also used by Istio's `RequestAuthentication`) looked in the provider's JWKS for a key with the token's `kid` and a type that fits its `alg`, and found none, so it answered 401 with this text as the body. The signature was never checked. If a matching key exists but the signature does not verify, the body is "Jwt verification fails" instead.

Why it happens

How to fix it

  1. 1.

    Fetch the JWKS from the issuer

    Use `remote_jwks` with a sensible `cache_duration` and asynchronous fetching, so keys are present before tokens use them.

    yaml
    providers:
      issuer_example:
        issuer: https://issuer.example.com
        audiences: ["api://orders"]
        remote_jwks:
          http_uri:
            uri: https://issuer.example.com/.well-known/jwks.json
            cluster: issuer_jwks          # a cluster with TLS to the issuer
            timeout: 5s
          cache_duration: 600s
          async_fetch: { fast_listener: false }   # prefetch, and refetch in the background
  2. 2.

    In Istio, point to jwksUri

    Istiod fetches and pushes the keys; an inline `jwks` value must be updated by hand on every rotation.

    yaml
    apiVersion: security.istio.io/v1
    kind: RequestAuthentication
    metadata:
      name: orders-jwt
    spec:
      selector:
        matchLabels: { app: orders }
      jwtRules:
      - issuer: https://issuer.example.com
        jwksUri: https://issuer.example.com/.well-known/jwks.json   # not an inline "jwks:" copy

Related errors

Browse the full signature error database