From 0cc627a3db6f523fa198255d009d378c2ccad378 Mon Sep 17 00:00:00 2001 From: dheerajd-msys Date: Tue, 26 Nov 2019 17:00:37 +0530 Subject: [PATCH 1/3] Fix to collect region from aws_config_file Signed-off-by: dheerajd-msys --- lib/chef/knife/ec2_base.rb | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/chef/knife/ec2_base.rb b/lib/chef/knife/ec2_base.rb index e4f0ed40..230f42e1 100644 --- a/lib/chef/knife/ec2_base.rb +++ b/lib/chef/knife/ec2_base.rb @@ -81,8 +81,8 @@ def self.included(includer) def connection_string conn = {} - conn[:region] = locate_config_value(:region) - Chef::Log.debug "Using AWS region #{locate_config_value(:region)}" + conn[:region] = region_from_config_file(locate_config_value(:aws_config_file)) || locate_config_value(:region) + Chef::Log.debug "Using AWS region #{conn[:region]}" conn[:credentials] = if locate_config_value(:use_iam_profile) Chef::Log.debug "Using iam profile for authentication as use_iam_profile set" @@ -290,6 +290,23 @@ def custom_warnings! end end + # Return region from aws_config_file + # @return [String] + def region_from_config_file(config_file) + return if config_file.nil? + aws_config = ini_parse(File.read(config_file)) + profile_key = locate_config_value(:aws_profile) + profile_key = "profile #{profile_key}" if profile_key != "default" + + unless aws_config.values.empty? + if aws_config[profile_key] + Chef::Config[:knife][:region] = aws_config[profile_key]["region"] + else + raise ArgumentError, "The provided --aws-profile '#{profile_key}' is invalid." + end + end + end + private # validate the contents of the aws configuration file From b452de8d515b5645176520469f34f4e7a78c8b50 Mon Sep 17 00:00:00 2001 From: dheerajd-msys Date: Tue, 26 Nov 2019 17:37:44 +0530 Subject: [PATCH 2/3] fix chefstyle offense Signed-off-by: dheerajd-msys --- lib/chef/knife/ec2_base.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/chef/knife/ec2_base.rb b/lib/chef/knife/ec2_base.rb index 230f42e1..bc801898 100644 --- a/lib/chef/knife/ec2_base.rb +++ b/lib/chef/knife/ec2_base.rb @@ -294,6 +294,7 @@ def custom_warnings! # @return [String] def region_from_config_file(config_file) return if config_file.nil? + aws_config = ini_parse(File.read(config_file)) profile_key = locate_config_value(:aws_profile) profile_key = "profile #{profile_key}" if profile_key != "default" From 445bc0c4c1808487b276caf55095d1ca3399fc5c Mon Sep 17 00:00:00 2001 From: dheerajd-msys Date: Mon, 2 Dec 2019 17:35:33 +0530 Subject: [PATCH 3/3] minor fix to collect the region Signed-off-by: dheerajd-msys --- lib/chef/knife/ec2_base.rb | 27 +++------------------------ 1 file changed, 3 insertions(+), 24 deletions(-) diff --git a/lib/chef/knife/ec2_base.rb b/lib/chef/knife/ec2_base.rb index bc801898..f5b973fc 100644 --- a/lib/chef/knife/ec2_base.rb +++ b/lib/chef/knife/ec2_base.rb @@ -1,3 +1,4 @@ + # # Author:: Seth Chisamore () # Copyright:: Copyright (c) 2011-2019 Chef Software, Inc. @@ -67,8 +68,7 @@ def self.included(includer) option :region, long: "--region REGION", description: "Your AWS region", - proc: Proc.new { |key| Chef::Config[:knife][:region] = key }, - default: "us-east-1" + proc: Proc.new { |key| Chef::Config[:knife][:region] = key } option :use_iam_profile, long: "--use-iam-profile", @@ -81,7 +81,7 @@ def self.included(includer) def connection_string conn = {} - conn[:region] = region_from_config_file(locate_config_value(:aws_config_file)) || locate_config_value(:region) + conn[:region] = locate_config_value(:region) || "us-east-1" Chef::Log.debug "Using AWS region #{conn[:region]}" conn[:credentials] = if locate_config_value(:use_iam_profile) @@ -145,7 +145,6 @@ def server_hashes(server_obj) %w{ebs_optimized image_id instance_id instance_type key_name platform public_dns_name public_ip_address private_dns_name private_ip_address root_device_type}.each do |id| server_data[id] = server_obj.instances[0].send(id) end - server_data["availability_zone"] = server_obj.instances[0].placement.availability_zone server_data["groups"] = server_obj.groups.map(&:name) server_data["iam_instance_profile"] = ( server_obj.instances[0].iam_instance_profile.nil? ? nil : server_obj.instances[0].iam_instance_profile.arn[%r{instance-profile/(.*)}] ) @@ -204,7 +203,6 @@ def validate_aws_config!(keys = %i{aws_access_key_id aws_secret_access_key}) errors = [] # track all errors so we report on all of them validate_aws_config_file! if locate_config_value(:aws_config_file) - unless locate_config_value(:use_iam_profile) # skip config file / key validation if we're using iam profile # validate the creds file if: # aws keys have not been passed in config / CLI and the default cred file location does exist @@ -290,24 +288,6 @@ def custom_warnings! end end - # Return region from aws_config_file - # @return [String] - def region_from_config_file(config_file) - return if config_file.nil? - - aws_config = ini_parse(File.read(config_file)) - profile_key = locate_config_value(:aws_profile) - profile_key = "profile #{profile_key}" if profile_key != "default" - - unless aws_config.values.empty? - if aws_config[profile_key] - Chef::Config[:knife][:region] = aws_config[profile_key]["region"] - else - raise ArgumentError, "The provided --aws-profile '#{profile_key}' is invalid." - end - end - end - private # validate the contents of the aws configuration file @@ -349,7 +329,6 @@ def validate_aws_credential_file! profile = locate_config_value(:aws_profile) Chef::Log.debug "Using AWS profile #{profile}" - entries = if aws_creds.values.first.key?("AWSAccessKeyId") aws_creds.values.first else