Support Vault JWT auth method for gateway deployments - #129
Open
yatuk wants to merge 1 commit into
Open
Conversation
Adds VAULT_AUTH_METHOD=jwt as an alternative to the current static VAULT_TOKEN / X-Vault-Token auth. In gateway-fronted deployments (Toolhive, any OIDC-terminating reverse proxy) every user currently reaches Vault as the same service-account identity, since only a static token is supported. That collapses per-user RBAC into a single shared identity and breaks audit attribution at the Vault layer. With JWT mode enabled, VaultContextMiddleware reads the incoming Authorization: Bearer <jwt> header, exchanges it via Vault's POST /v1/auth/<mount>/login (role + jwt), and injects the resulting short-lived, user-scoped Vault token into the request context in place of a static one. The exchanged token is cached in memory, keyed by a hash of the JWT, until close to its lease expiry (or a configured VAULT_AUTH_JWT_CACHE_TTL cap), so repeat requests from the same session don't re-hit /login every time. Static-token mode remains the default and is unchanged; the new code path only runs when VAULT_AUTH_METHOD=jwt is set. A failed exchange (missing JWT, Vault rejecting it, Vault unreachable) returns 401/403/503 with a reason and never falls back to a static token. New env vars: VAULT_AUTH_METHOD, VAULT_AUTH_JWT_PATH, VAULT_AUTH_JWT_ROLE, VAULT_AUTH_JWT_HEADER, VAULT_AUTH_JWT_CACHE_TTL.
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.
Closes #107.
Summary
Right now vault-mcp-server only authenticates to Vault with a static token, VAULT_TOKEN or X-Vault-Token. Behind a gateway that already terminates user OIDC (Toolhive, or any similar reverse proxy), that means every user reaches Vault as the same service account. Audit logs can't tell who actually made a request, the static token has to carry the union of everyone's permissions, and per-user policy at the Vault layer is impossible even if the gateway is doing its own authorization correctly.
This adds VAULT_AUTH_METHOD=jwt as an alternative. When set:
The exchanged token is cached in memory, keyed by a hash of the JWT, until shortly before its lease expires (or a configured VAULT_AUTH_JWT_CACHE_TTL, whichever is shorter), so a session making several requests in a row doesn't hit /login every time.
Static-token mode is untouched and stays the default. VAULT_TOKEN and X-Vault-Token are only ignored when JWT mode is explicitly turned on. If the JWT is missing, or Vault rejects it, or Vault can't be reached, the request fails with 401, 403, or 503 and a reason. It does not fall back to a static token.
New env vars, matching what's proposed in the issue: VAULT_AUTH_METHOD, VAULT_AUTH_JWT_PATH (default jwt), VAULT_AUTH_JWT_ROLE, VAULT_AUTH_JWT_HEADER (default Authorization), VAULT_AUTH_JWT_CACHE_TTL (optional).
A couple of judgment calls
Testing
makeisn't on PATH in my environment, unrelated to this change.cc @gastoncan since this is your issue, would appreciate a look at whether this matches your Toolhive + Keycloak setup.