From 9e6f452a453b5e5c9f590959b4625faefdf1656b Mon Sep 17 00:00:00 2001 From: Alexandre Constantino Date: Fri, 5 Feb 2016 18:59:51 +0000 Subject: [PATCH] Replace regex for reading AWS files with iniparse --- Gemfile | 1 + lib/vagrant-aws/config.rb | 47 +++++++++++++-------------------- spec/vagrant-aws/config_spec.rb | 3 +++ 3 files changed, 23 insertions(+), 28 deletions(-) diff --git a/Gemfile b/Gemfile index 4d52f3a..5da3a8b 100644 --- a/Gemfile +++ b/Gemfile @@ -7,6 +7,7 @@ group :development do # gem dependency because we expect to be installed within the # Vagrant environment itself using `vagrant plugin`. gem "vagrant", :git => "https://github.com/mitchellh/vagrant.git" + gem 'iniparse', '~> 1.4', '>= 1.4.2' end group :plugins do diff --git a/lib/vagrant-aws/config.rb b/lib/vagrant-aws/config.rb index 759918b..2e2aa3d 100644 --- a/lib/vagrant-aws/config.rb +++ b/lib/vagrant-aws/config.rb @@ -1,4 +1,5 @@ require "vagrant" +require "iniparse" module VagrantPlugins module AWS @@ -505,7 +506,9 @@ module VagrantPlugins aws_config = location + 'config' aws_creds = location + 'credentials' end - aws_region, aws_id, aws_secret, aws_token = read_aws_files(profile, aws_config, aws_creds) + if File.exist?(aws_config) and File.exist?(aws_creds) + aws_region, aws_id, aws_secret, aws_token = read_aws_files(profile, aws_config, aws_creds) + end end aws_region = nil if aws_region == '' aws_id = nil if aws_id == '' @@ -519,37 +522,25 @@ module VagrantPlugins private def read_aws_files(profile, aws_config, aws_creds) - # profile line to match in the config file - pat = '' + # determine section in config ini file if profile == 'default' - pat = '\[default\]' + ini_profile = profile else - pat = '\[profile ' + profile + '\]' - end - # read config file for selected profile - begin - data = File.read(aws_config) - regex = Regexp.new('^' + pat + '$\n^region\s*=\s*(.*)$') - aws_region = regex.match(data)[1] - rescue - aws_region = '' + ini_profile = 'profile ' + profile end + # get info from config ini file for selected profile + data = File.read(aws_config) + doc_cfg = IniParse.parse(data) + aws_region = doc_cfg[ini_profile]['region'] - # profile line to match in the credentials file - pat = '\[' + profile + '\]' - # read credentials file for selected profile - begin - aws_token = '' - data = File.read(aws_creds) - regex = Regexp.new('^' + pat + '$\n^aws_access_key_id\s*=\s*(.*)$\naws_secret_access_key\s*=\s*(.*)$(\n^aws_session_token\s*=\s*(.*)$)?') - matches = regex.match(data) - aws_id = matches[1] - aws_secret = matches[2] - aws_token = matches[4] - rescue - aws_id = '' - aws_secret = '' - end + # determine section in credentials ini file + ini_profile = profile + # get info from credentials ini file for selected profile + data = File.read(aws_creds) + doc_cfg = IniParse.parse(data) + aws_id = doc_cfg[ini_profile]['aws_access_key_id'] + aws_secret = doc_cfg[ini_profile]['aws_secret_access_key'] + aws_token = doc_cfg[ini_profile]['aws_session_token'] return aws_region, aws_id, aws_secret, aws_token end diff --git a/spec/vagrant-aws/config_spec.rb b/spec/vagrant-aws/config_spec.rb index 29f728d..eb30974 100644 --- a/spec/vagrant-aws/config_spec.rb +++ b/spec/vagrant-aws/config_spec.rb @@ -190,6 +190,7 @@ aws_session_token= TOKuser3 context "without EC2 credential environment variables but with AWS_CONFIG_FILE and AWS_SHARED_CREDENTIALS_FILE set" do subject do + allow(File).to receive(:exist?).and_return(true) allow(File).to receive(:read).with(filename_cfg).and_return(data_cfg) allow(File).to receive(:read).with(filename_keys).and_return(data_keys) ENV.stub(:[]).with("AWS_CONFIG_FILE").and_return(sh_filename_cfg) @@ -208,6 +209,7 @@ aws_session_token= TOKuser3 context "without any credential environment variables and fallback to default profile at default location" do subject do + allow(File).to receive(:exist?).and_return(true) allow(File).to receive(:read).with(filename_cfg).and_return(data_cfg) allow(File).to receive(:read).with(filename_keys).and_return(data_keys) instance.tap do |o| @@ -221,6 +223,7 @@ aws_session_token= TOKuser3 context "without any credential environment variables and chosing a profile" do subject do + allow(File).to receive(:exist?).and_return(true) allow(File).to receive(:read).with(filename_cfg).and_return(data_cfg) allow(File).to receive(:read).with(filename_keys).and_return(data_keys) instance.aws_profile = "user3"