diff --git a/lib/puppet/provider/openstack/auth.rb b/lib/puppet/provider/openstack/auth.rb index 584bfc72..743071db 100644 --- a/lib/puppet/provider/openstack/auth.rb +++ b/lib/puppet/provider/openstack/auth.rb @@ -16,10 +16,13 @@ module Puppet::Provider::Openstack::Auth rcfile = [filename, '/root/openrc'].detect { |f| File.exists? f } unless rcfile.nil? File.open(rcfile).readlines.delete_if{|l| l=~ /^#|^$/ }.each do |line| - key, value = line.split('=') - key = key.split(' ').last - value = value.chomp.gsub(/'/, '') - env.merge!(key => value) if key =~ /OS_/ + # we only care about the OS_ vars from the file LP#1699950 + if line =~ /OS_/ + key, value = line.split('=') + key = key.split(' ').last + value = value.chomp.gsub(/'/, '') + env.merge!(key => value) if key =~ /OS_/ + end end end return env diff --git a/releasenotes/notes/fix-openrc-parsing-2cc3a95b3bf2b259.yaml b/releasenotes/notes/fix-openrc-parsing-2cc3a95b3bf2b259.yaml new file mode 100644 index 00000000..ebe609c5 --- /dev/null +++ b/releasenotes/notes/fix-openrc-parsing-2cc3a95b3bf2b259.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Improve openrc parsing to prevent issues when additional bash code is + present in a user's openrc file. LP#1699950 diff --git a/spec/unit/provider/openstack/auth_spec.rb b/spec/unit/provider/openstack/auth_spec.rb index 413c81a4..31008117 100644 --- a/spec/unit/provider/openstack/auth_spec.rb +++ b/spec/unit/provider/openstack/auth_spec.rb @@ -102,6 +102,22 @@ describe Puppet::Provider::Openstack::Auth do end end + context 'with a valid RC file with extra code in it' do + it 'provides a hash' do + mock = "export OS_USERNAME='test'\nexport OS_PASSWORD='abc123'\nexport OS_PROJECT_NAME='test'\nexport OS_AUTH_URL='http://127.0.0.1:5000'\n_openstack() {\n foo\n} " + filename = 'file' + File.expects(:exists?).with('file').returns(true) + File.expects(:open).with('file').returns(StringIO.new(mock)) + + response = klass.get_os_vars_from_rcfile(filename) + expect(response).to eq({ + "OS_AUTH_URL" => "http://127.0.0.1:5000", + "OS_PASSWORD" => "abc123", + "OS_PROJECT_NAME" => "test", + "OS_USERNAME" => "test"}) + end + end + context 'with an empty file' do it 'provides an empty hash' do filename = 'file'