api: stamp kid/alg/use on Transit JWKS so Vault's go-oidc picks the key#192
Merged
Conversation
…cks the key After #190 fixed the /oauth2/jwks 500, the next Vault sign-in failed with: Provider.VerifyIDToken: invalid id_token: failed to verify signature: failed to verify id token signature: invalid signature Diagnosed by signing a known payload via Vault Transit and verifying with the published JWKS key directly — the crypto is fine, the JWKS public key matches the Transit-engine public key, RSA-PKCS1v15-SHA256 verifies cleanly. So the failure was upstream of the actual signature check. Root cause: VaultTransitJwtEncoder writes kid="api-jwt:v1" into the JWT header, but VaultTransitJwkSource published JWK objects with no kid/alg/ use (Nimbus's RSAKey.parseFromPEMEncodedObjects leaves those unset). The verifier (coreos/go-oidc-v3, used by Vault's OIDC auth) filters JWKS keys by kid: for _, key := range keys { if keyID == "" || key.KeyID == keyID { if payload, err := jws.Verify(&key); err == nil { return payload } } } return errors.New("failed to verify id token signature") JWT header.kid="api-jwt:v1" != JWK.KeyID="" → every key skipped → error. Fix is mechanical: rebuild each published JWK with kid="$keyName:v$version" (matching the encoder), alg=RS256, use=sig. Unit test now asserts kid/alg/use on the published JWK. System test (VaultTransitJwksPlaywrightTest) asserts the same on /oauth2/jwks against the live api. Verified locally: signing-then-verifying round-trips through the JWKS public key.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Vault sign-in after #190 + #191 still failed:
Diagnosed cryptographically against the live cluster: I signed a known payload via
transit/sign/api-jwt, then verified it directly with both the JWKS-published public key and the Transit-engine PEM. Both verify cleanly. Same RSA modulus, RS256 with PKCS1v15+SHA256, signature length 256 — the crypto is correct end-to-end. So the failure was upstream of the actual signature check.Root cause. The published JWK had no
kid, noalg, nouse— just{kty, e, n}.VaultTransitJwtEncoderwriteskid="api-jwt:v1"into the JWT header. Vault's verifier is coreos/go-oidc-v3, which filters JWKS keys bykid:Every key skipped → error. Nimbus's
RSAKey.parseFromPEMEncodedObjectsleaves kid/alg/use unset, andVaultTransitJwkSource.refresh()published the parsed key as-is.Fix
Three lines in
VaultTransitJwkSource.refresh()— rebuild each parsed RSAKey with kid/alg/use stamped to match what the encoder writes:Unit test now asserts
kid="$keyName:v1",alg="RS256",use="sig"on the published JWK. System test (VaultTransitJwksPlaywrightTest) asserts the same on/oauth2/jwksagainst the live api in theoidc-e2ecompose stack.Verified locally
After fix,
/oauth2/jwksreturns:{"kty":"RSA","e":"AQAB","use":"sig","kid":"api-jwt:v1","alg":"RS256","n":"..."}Test plan
./gradlew :services:api:test --tests VaultTransitJwkSourceTest(4/4)./gradlew :services:system-tests:vaultOidcLiveTestagainst compose stack — greenvault login -method=oidccompletes