Skip to content

Commit e29f9dd

Browse files
Matias Bertanimatiasbertani
authored andcommitted
defaults: add support for token_file env
1 parent 05852d9 commit e29f9dd

File tree

2 files changed

+52
-20
lines changed

2 files changed

+52
-20
lines changed

lib/vault/defaults.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ module Defaults
1010
# @return [String]
1111
VAULT_ADDRESS = "https://127.0.0.1:8200".freeze
1212

13-
# The path to the vault token on disk.
13+
# The default path to the vault token on disk.
1414
# @return [String]
15-
VAULT_DISK_TOKEN = Pathname.new("#{ENV["HOME"]}/.vault-token").expand_path.freeze
15+
DEFAULT_VAULT_DISK_TOKEN = Pathname.new("#{ENV["HOME"]}/.vault-token").expand_path.freeze
1616

1717
# The list of SSL ciphers to allow. You should not change this value unless
1818
# you absolutely know what you are doing!
@@ -56,18 +56,16 @@ def address
5656
# The vault token to use for authentiation.
5757
# @return [String, nil]
5858
def token
59-
if !ENV["VAULT_TOKEN"].nil?
60-
return ENV["VAULT_TOKEN"]
61-
end
59+
ENV["VAULT_TOKEN"] || fetch_from_disk("VAULT_TOKEN_FILE")
60+
end
6261

63-
if VAULT_DISK_TOKEN.exist? && VAULT_DISK_TOKEN.readable?
64-
return VAULT_DISK_TOKEN.read.chomp
62+
def fetch_from_disk(env_var)
63+
path = ENV[env_var] ? Pathname.new(ENV[env_var]) : DEFAULT_VAULT_DISK_TOKEN
64+
if path&.exist? && path.readable?
65+
path.read.chomp
6566
end
66-
67-
nil
6867
end
6968

70-
7169
# Vault Namespace, if any.
7270
# @return [String, nil]
7371
def namespace

spec/unit/defaults_spec.rb

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,40 @@ module Vault
2626
end
2727

2828
describe ".token" do
29+
it "uses ENV['VAULT_TOKEN'] if present" do
30+
with_stubbed_env("VAULT_TOKEN" => "testing") do
31+
expect(Defaults.token).to eq("testing")
32+
end
33+
end
34+
35+
it "delegates to fetch_from_disk if ENV['VAULT_TOKEN'] is not present" do
36+
with_stubbed_env("VAULT_TOKEN" => nil) do
37+
allow(Defaults).to receive(:fetch_from_disk).with("VAULT_TOKEN_FILE").and_return("fetch_from_disk_token")
38+
expect(Defaults.token).to eq("fetch_from_disk_token")
39+
expect(Defaults).to have_received(:fetch_from_disk)
40+
end
41+
end
42+
43+
it "prefers the environment over local token" do
44+
with_stubbed_env("VAULT_TOKEN" => "testing2") do
45+
allow(Defaults).to receive(:fetch_from_disk)
46+
expect(Defaults.token).to eq("testing2")
47+
expect(Defaults).to_not have_received(:fetch_from_disk)
48+
end
49+
end
50+
51+
it "returns nil if ENV['VAULT_TOKEN'] is not present and fetch_from_disk return nil" do
52+
with_stubbed_env("VAULT_TOKEN" => nil) do
53+
allow(Defaults).to receive(:fetch_from_disk).and_return(nil)
54+
expect(Defaults.token).to be_nil
55+
end
56+
end
57+
end
58+
59+
describe ".fetch_from_disk" do
2960
let(:token) { File.expand_path("~/.vault-token") }
3061
let(:backup_token) { File.expand_path("~/.vault-token.old") }
62+
let(:custom_token_path) { File.expand_path("~/custom_token_path") }
3163

3264
before do
3365
if File.exist?(token)
@@ -41,21 +73,23 @@ module Vault
4173
end
4274
end
4375

44-
it "uses ~/.vault-token when present" do
45-
File.open(token, "w") { |f| f.write("testing\n") }
46-
expect(Defaults.token).to eq("testing")
76+
it "reads from ENV specified path if present and file is readable" do
77+
File.open(custom_token_path, "w") { |f| f.write("token_from_custom_path\n") }
78+
with_stubbed_env("VAULT_TOKEN_FILE" => custom_token_path) do
79+
expect(Defaults.fetch_from_disk("VAULT_TOKEN_FILE")).to eq("token_from_custom_path")
80+
end
4781
end
4882

49-
it "uses ENV['VAULT_TOKEN'] if present" do
50-
with_stubbed_env("VAULT_TOKEN" => "testing") do
51-
expect(Defaults.token).to eq("testing")
83+
it "reads from default path if ENV specified path is not present" do
84+
File.open(Defaults::DEFAULT_VAULT_DISK_TOKEN, "w") { |f| f.write("default_path_token\n") }
85+
with_stubbed_env("VAULT_TOKEN_FILE" => nil) do
86+
expect(Defaults.fetch_from_disk("VAULT_TOKEN_FILE")).to eq("default_path_token")
5287
end
5388
end
5489

55-
it "prefers the environment over local token" do
56-
File.open(token, "w") { |f| f.write("testing1\n") }
57-
with_stubbed_env("VAULT_TOKEN" => "testing2") do
58-
expect(Defaults.token).to eq("testing2")
90+
it "returns nil if no readable file is found" do
91+
with_stubbed_env("VAULT_TOKEN_FILE" => "/non/existent/path") do
92+
expect(Defaults.fetch_from_disk("VAULT_TOKEN_FILE")).to be_nil
5993
end
6094
end
6195
end

0 commit comments

Comments
 (0)