Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for puppetdb behind basic auth #159

Merged
merged 1 commit into from
Nov 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/octocatalog-diff/puppetdb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ def _get(path)

begin
more_options = { headers: { 'Accept' => 'application/json' }, timeout: @timeout }
if connection[:username] || connection[:password]
more_options[:basic_auth] = { username: connection[:username], password: connection[:password] }
end
response = OctocatalogDiff::Util::HTTParty.get(complete_url, @options.merge(more_options), 'puppetdb')

# Handle all non-200's from PuppetDB
Expand Down Expand Up @@ -153,7 +156,13 @@ def parse_url(url)
end

raise ArgumentError, "URL #{url} has invalid scheme" unless uri.scheme =~ /^https?$/
{ ssl: uri.scheme == 'https', host: uri.host, port: uri.port }
parsed_url = { ssl: uri.scheme == 'https', host: uri.host, port: uri.port }
if uri.user || uri.password
parsed_url[:username] = uri.user
parsed_url[:password] = uri.password
end

parsed_url
rescue URI::InvalidURIError => exc
raise exc.class, "Invalid URL: #{url} (#{exc.message})"
end
Expand Down
65 changes: 65 additions & 0 deletions spec/octocatalog-diff/tests/puppetdb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ def ssl_test(server_opts, opts = {})
test_server.stop
end

def basic_auth_test(server_opts, opts = {})
server_opts[:rsa_key] ||= File.read(OctocatalogDiff::Spec.fixture_path('ssl/generated/server.key'))
server_opts[:cert] ||= File.read(OctocatalogDiff::Spec.fixture_path('ssl/generated/server.crt'))
server_opts[:require_header] ||= {}
server_opts[:require_header]['Authorization'] = 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=' # username:password
test_server = nil
3.times do
test_server = SSLTestServer.new(server_opts)
test_server.start
break if test_server.port > 0
end
raise OctocatalogDiff::Spec::FixtureError, 'Unable to instantiate SSLTestServer' unless test_server.port > 0
testobj = OctocatalogDiff::PuppetDB.new(opts.merge(puppetdb_url: "https://username:password@localhost:#{test_server.port}"))
return testobj.get('/foo')
ensure
test_server.stop
end

describe OctocatalogDiff::PuppetDB do
# Test constructor's ability to create @connections
describe '#initialize' do
Expand Down Expand Up @@ -272,6 +290,53 @@ def ssl_test(server_opts, opts = {})
expect { testobj.send(:parse_url, test_url) }.to raise_error(URI::InvalidURIError)
end
end

context 'basic auth' do
it 'should detect a username:password combination' do
test_url = 'https://username:password@foo.bar.host:8090'
testobj = OctocatalogDiff::PuppetDB.new
result = testobj.send(:parse_url, test_url)
expect(result[:username]).to eq('username')
expect(result[:password]).to eq('password')
end

it 'should allow usernames without passwords' do
test_url = 'https://username:@foo.bar.host:8090'
testobj = OctocatalogDiff::PuppetDB.new
result = testobj.send(:parse_url, test_url)
expect(result[:username]).to eq('username')
expect(result[:password]).to eq('')
end

it 'should allow passwords without usernames' do
test_url = 'https://:password@foo.bar.host:8090'
testobj = OctocatalogDiff::PuppetDB.new
result = testobj.send(:parse_url, test_url)
expect(result[:username]).to eq('')
expect(result[:password]).to eq('password')
end

it 'should not parse a username or password when none are provided' do
test_url = 'https://foo.bar.host:8090'
testobj = OctocatalogDiff::PuppetDB.new
result = testobj.send(:parse_url, test_url)
expect(result[:username]).to eq(nil)
expect(result[:password]).to eq(nil)
end
end
end

context 'basic auth connection options' do
context 'with basic auth on' do
let(:server_opts) { {} }
let(:client_opts) { {} }
describe '#get' do
it 'should not fail with basic auth on' do
result = basic_auth_test(server_opts, client_opts)
expect(result.key?('success')).to eq(true)
end
end
end
end

context 'puppetdb ssl connection options' do
Expand Down