Refresh Vault token from disk on each Consult.load call#57
Open
Refresh Vault token from disk on each Consult.load call#57
Conversation
d67cbe1 to
f2de018
Compare
f2de018 to
3803261
Compare
There was a problem hiding this comment.
Pull request overview
This PR updates Consult’s Vault client configuration so a running process can pick up a renewed Vault token (written by vault-agent to ~/.vault-token) instead of continuing to use a stale in-memory token.
Changes:
- Update
Consult.configure_vaultto setVault’s token fromVault::Defaults.tokenwhen no token is present in Consult config. - Add RSpec coverage to validate token refresh behavior across repeated
configure_vaultcalls when config omits:token.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| lib/consult.rb | Re-reads/sets the Vault client token via Vault::Defaults.token when config does not explicitly include :token. |
| spec/consult_spec.rb | Adds specs to verify token refresh behavior when config omits Vault token. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+82
to
+83
| # Only fall back to Vault::Defaults.token when no token is explicitly configured | ||
| c.token = Vault::Defaults.token unless @config[:vault].key?(:token) |
Comment on lines
+51
to
+57
| it 'refreshes the Vault client token from Defaults.token on each load when no token is in config' do | ||
| # Simulate production consult.yml (no explicit token; vault-agent manages it) | ||
| Consult.config[:vault].delete(:token) | ||
|
|
||
| allow(Vault::Defaults).to receive(:token).and_return('new-refreshed-token') | ||
|
|
||
| # Re-load consult (as reload_templates_and_routes would do) |
Comment on lines
+63
to
+78
| it 'picks up token from disk even when config does not specify a token' do | ||
| # Remove the token from config to simulate production consult.yml | ||
| # (which only has address and ssl_ca_path, no token) | ||
| original_config = Consult.config[:vault].dup | ||
| Consult.config[:vault].delete(:token) | ||
|
|
||
| allow(Vault::Defaults).to receive(:token).and_return('token-from-disk') | ||
|
|
||
| Consult.configure_vault | ||
|
|
||
| expect(Vault.client.token).to eq('token-from-disk') | ||
|
|
||
| # Restore | ||
| Consult.config[:vault].merge!(original_config) | ||
| end | ||
|
|
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.
The Vault Ruby gem caches the token in memory at client initialization and never re-reads. The token is being renewed and written to a shared volume (symlinked to ~/.vault-token), but the running Rails process keeps using the stale cached token. This causes 403 "invalid token" errors when templates are re-rendered at runtime.
Adding
c.token = Vault::Defaults.tokento configure_vault forces the client to re-read the token from ~/.vault-token on every Consult.load call, picking up vault-agent's renewed token.Test Plan
1. Start a console session
2. Load config and remove the explicit token
This simulates a production consult.yml key, relying on a local token agent instead.
3. Write a first token to disk
4. Call configure_vault and verify
5. Simulate a token rotation
In a separate terminal:
6. Call configure_vault again and confirm the new token is picked up
7. Restore your real Vault token when done