Skip to content

Refresh Vault token from disk on each Consult.load call#57

Open
nogazr wants to merge 1 commit intomasterfrom
fix-rendering-error
Open

Refresh Vault token from disk on each Consult.load call#57
nogazr wants to merge 1 commit intomasterfrom
fix-rendering-error

Conversation

@nogazr
Copy link
Copy Markdown
Contributor

@nogazr nogazr commented Apr 22, 2026

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.token to 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

bin/console

2. Load config and remove the explicit token

Consult.load config_dir: 'spec/support'
Consult.config[:vault].delete(:token)

This simulates a production consult.yml key, relying on a local token agent instead.

3. Write a first token to disk

echo 'first-token' > ~/.vault-token

4. Call configure_vault and verify

Consult.configure_vault
Vault.client.token # => "first-token"

5. Simulate a token rotation

In a separate terminal:

echo 'second-token' > ~/.vault-token

6. Call configure_vault again and confirm the new token is picked up

Consult.configure_vault
Vault.client.token # => "second-token"

7. Restore your real Vault token when done

vcli init --force

@nogazr nogazr self-assigned this Apr 22, 2026
@nogazr nogazr force-pushed the fix-rendering-error branch 2 times, most recently from d67cbe1 to f2de018 Compare April 22, 2026 21:30
@nogazr nogazr marked this pull request as ready for review April 22, 2026 22:23
@nogazr nogazr requested a review from a team as a code owner April 22, 2026 22:23
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_vault to set Vault’s token from Vault::Defaults.token when no token is present in Consult config.
  • Add RSpec coverage to validate token refresh behavior across repeated configure_vault calls 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 thread lib/consult.rb
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 thread spec/consult_spec.rb
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 thread spec/consult_spec.rb
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants