Skip to content

Commit 641b568

Browse files
chaenchelnak
authored andcommitted
loadjson: do not send http_basic_authentication if not needed
1 parent b97c657 commit 641b568

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

lib/puppet/parser/functions/loadjson.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,22 @@ module Puppet::Parser::Functions
2828
require 'open-uri'
2929
begin
3030
if args[0].start_with?('http://', 'https://')
31-
username = ''
32-
password = ''
31+
http_options = {}
3332
if (match = args[0].match(%r{(http\://|https\://)(.*):(.*)@(.*)}))
3433
# If URL is in the format of https://username:[email protected]/my_hash.yaml
3534
protocol, username, password, path = match.captures
3635
url = "#{protocol}#{path}"
36+
http_options[:http_basic_authentication] = [username, password]
3737
elsif (match = args[0].match(%r{(http\:\/\/|https\:\/\/)(.*)@(.*)}))
3838
# If URL is in the format of https://[email protected]/my_hash.yaml
3939
protocol, username, path = match.captures
4040
url = "#{protocol}#{path}"
41+
http_options[:http_basic_authentication] = [username, '']
4142
else
4243
url = args[0]
4344
end
4445
begin
45-
contents = OpenURI.open_uri(url, http_basic_authentication: [username, password])
46+
contents = OpenURI.open_uri(url, **http_options)
4647
rescue OpenURI::HTTPError => err
4748
res = err.io
4849
warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'")

spec/functions/loadjson_spec.rb

+3-6
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,11 @@
7676
let(:filename) do
7777
'https://example.local/myhash.json'
7878
end
79-
let(:basic_auth) { { http_basic_authentication: ['', ''] } }
8079
let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
8180
let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
8281

8382
it {
84-
expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_return(json)
83+
expect(OpenURI).to receive(:open_uri).with(filename).and_return(json)
8584
expect(PSON).to receive(:load).with(json).and_return(data).once
8685
is_expected.to run.with_params(filename).and_return(data)
8786
}
@@ -123,11 +122,10 @@
123122
let(:filename) do
124123
'https://example.local/myhash.json'
125124
end
126-
let(:basic_auth) { { http_basic_authentication: ['', ''] } }
127125
let(:json) { ',;{"key":"value"}' }
128126

129127
it {
130-
expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_return(json)
128+
expect(OpenURI).to receive(:open_uri).with(filename).and_return(json)
131129
expect(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
132130
is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
133131
}
@@ -137,10 +135,9 @@
137135
let(:filename) do
138136
'https://example.local/myhash.json'
139137
end
140-
let(:basic_auth) { { http_basic_authentication: ['', ''] } }
141138

142139
it {
143-
expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_raise OpenURI::HTTPError, '404 File not Found'
140+
expect(OpenURI).to receive(:open_uri).with(filename).and_raise OpenURI::HTTPError, '404 File not Found'
144141
is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
145142
}
146143
end

0 commit comments

Comments
 (0)