Skip to content

Commit 0dc8195

Browse files
authored
Merge pull request #3299 from donoghuc/prefer-token
(GH-3296) Prefer cert auth to token auth for puppetdb client
2 parents 8266293 + 6f0c8f8 commit 0dc8195

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/bolt/puppetdb/config.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def self.default_config
6060
end
6161

6262
def token
63-
return @token if @token
63+
return @token if @token_computed
6464
# Allow nil in config to skip loading a token
6565
if @settings.include?('token')
6666
if @settings['token']
@@ -69,6 +69,12 @@ def token
6969
elsif File.exist?(DEFAULT_TOKEN)
7070
@token = File.read(DEFAULT_TOKEN)
7171
end
72+
# Only use cert based auth in the case token and cert are both configured
73+
if @token && cert
74+
Bolt::Logger.logger(self).debug("Both cert and token based auth configured, using cert only")
75+
@token = nil
76+
end
77+
@token_computed = true
7278
@token = @token.strip if @token
7379
end
7480

spec/unit/puppetdb/config_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
context "token" do
7373
context "token is valid" do
7474
before :each do
75+
options.delete('cert')
76+
options.delete('key')
7577
allow(File).to receive(:read).with(token).and_return 'footoken'
7678
allow(File).to receive(:read).with(Bolt::PuppetDB::Config::DEFAULT_TOKEN).and_return 'bartoken'
7779
end
@@ -97,6 +99,8 @@
9799

98100
context "token is invalid" do
99101
before :each do
102+
options.delete('cert')
103+
options.delete('key')
100104
allow(File).to receive(:read).with(token).and_return "footoken\n"
101105
allow(File).to receive(:read).with(Bolt::PuppetDB::Config::DEFAULT_TOKEN).and_return "bartoken\n"
102106
end
@@ -112,6 +116,14 @@
112116
expect(config.token).to eq('bartoken')
113117
end
114118
end
119+
120+
context "both token and cert" do
121+
it "returns nil for token when cert is configured" do
122+
allow(config).to receive(:validate_file_exists).with('cert').and_return true
123+
allow(File).to receive(:read).with(token).and_return 'footoken'
124+
expect(config.token).to be_nil
125+
end
126+
end
115127
end
116128

117129
context "cacert" do

0 commit comments

Comments
 (0)