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
This commit is contained in:
Alex Schultz 2017-06-23 08:45:07 -06:00
parent 93b8e7d365
commit 61044892ad
3 changed files with 28 additions and 4 deletions

View File

@ -16,10 +16,13 @@ module Puppet::Provider::Openstack::Auth
rcfile = [filename, '/root/openrc'].detect { |f| File.exists? f } rcfile = [filename, '/root/openrc'].detect { |f| File.exists? f }
unless rcfile.nil? unless rcfile.nil?
File.open(rcfile).readlines.delete_if{|l| l=~ /^#|^$/ }.each do |line| File.open(rcfile).readlines.delete_if{|l| l=~ /^#|^$/ }.each do |line|
key, value = line.split('=') # we only care about the OS_ vars from the file LP#1699950
key = key.split(' ').last if line =~ /OS_/
value = value.chomp.gsub(/'/, '') key, value = line.split('=')
env.merge!(key => value) if key =~ /OS_/ key = key.split(' ').last
value = value.chomp.gsub(/'/, '')
env.merge!(key => value) if key =~ /OS_/
end
end end
end end
return env return env

View File

@ -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

View File

@ -102,6 +102,22 @@ describe Puppet::Provider::Openstack::Auth do
end end
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 context 'with an empty file' do
it 'provides an empty hash' do it 'provides an empty hash' do
filename = 'file' filename = 'file'