From 61044892ad168d8ca0382ff6ca3a60f649fd24ff Mon Sep 17 00:00:00 2001 From: Alex Schultz Date: Fri, 23 Jun 2017 08:45:07 -0600 Subject: [PATCH] Ignore extra bash code from openrc If a user has additional code in their openrc file that isn't just bash variables, the auth provider might fail while parsing it. This change updates the logic to only try and parse lines with OS_ in it to make sure that extra things like bash code are ignored. Change-Id: Id6f5406dcf15642bc0d70caeac30224114bb0669 Closes-Bug: #1699950 --- lib/puppet/provider/openstack/auth.rb | 11 +++++++---- .../fix-openrc-parsing-2cc3a95b3bf2b259.yaml | 5 +++++ spec/unit/provider/openstack/auth_spec.rb | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 releasenotes/notes/fix-openrc-parsing-2cc3a95b3bf2b259.yaml 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'