class Chef::Knife
Public Instance Methods
aws_cred_file_location()
click to toggle source
the path to the aws credentials file. if passed via cli config use that if default location exists on disk fallback to that @return [String, nil] location to aws credentials file or nil if none exists
# File lib/chef/knife/helpers/ec2_base.rb, line 236 def aws_cred_file_location @cred_file ||= begin if !config[:aws_credential_file].nil? config[:aws_credential_file] else Chef::Util::PathHelper.home(".aws", "credentials") if ::File.exist?(Chef::Util::PathHelper.home(".aws", "credentials")) end end end
custom_warnings!()
click to toggle source
Custom Warning
# File lib/chef/knife/helpers/ec2_base.rb, line 282 def custom_warnings! unless config[:region] ui.warn "No region was specified in knife.rb/config.rb or as an argument. The default region, us-east-1, will be used:" end end
find_server_platform(server_name)
click to toggle source
Get the platform from server name @return [String]
# File lib/chef/knife/helpers/ec2_base.rb, line 276 def find_server_platform(server_name) get_platform = VALID_PLATFORMS.select { |name| server_name.downcase.include?(name) } get_platform.first end
iam_name_from_profile(profile)
click to toggle source
@return [String]
# File lib/chef/knife/helpers/ec2_base.rb, line 247 def iam_name_from_profile(profile) # The IAM profile object only contains the name as part of the arn if profile && profile.key?("arn") name = profile["arn"].split("/")[-1] end name || "" end
ini_parse(file)
click to toggle source
# File lib/chef/knife/helpers/ec2_base.rb, line 255 def ini_parse(file) current_section = {} map = {} file.each_line do |line| line = line.split(/^|\s;/).first # remove comments section = line.match(/^\s*\[([^\[\]]+)\]\s*$/) unless line.nil? if section current_section = section[1] elsif current_section item = line.match(/^\s*(.+?)\s*=\s*(.+?)\s*$/) unless line.nil? if item map[current_section] ||= {} map[current_section][item[1]] = item[2] end end end map end
Private Instance Methods
mask(key, from = 4)
click to toggle source
Mask the given string with char `X` Discard the chars based on from value
# File lib/chef/knife/helpers/ec2_base.rb, line 344 def mask(key, from = 4) str = key.dup if str && str.length > from str[from...str.length] = "X" * (str[from...str.length].length) end str end
validate_aws_config_file!()
click to toggle source
validate the contents of the aws configuration file @return [void]
# File lib/chef/knife/helpers/ec2_base.rb, line 292 def validate_aws_config_file! config_file = config[:aws_config_file] Chef::Log.debug "Using AWS config file at #{config_file}" raise ArgumentError, "The provided --aws_config_file (#{config_file}) cannot be found on disk." unless File.exist?(config_file) aws_config = ini_parse(File.read(config_file)) profile_key = config[:aws_profile] profile_key = "profile #{profile_key}" if profile_key != "default" unless aws_config.values.empty? if aws_config[profile_key] config[:region] = aws_config[profile_key]["region"] else raise ArgumentError, "The provided --aws-profile '#{profile_key}' is invalid." end end end
validate_aws_credential_file!()
click to toggle source
validate the contents of the aws credentials file @return [void]
# File lib/chef/knife/helpers/ec2_base.rb, line 313 def validate_aws_credential_file! Chef::Log.debug "Using AWS credential file at #{aws_cred_file_location}" raise ArgumentError, "The provided --aws_credential_file (#{aws_cred_file_location}) cannot be found on disk." unless File.exist?(aws_cred_file_location) # File format: # AWSAccessKeyId=somethingsomethingdarkside # AWSSecretKey=somethingsomethingcomplete # OR # [default] # aws_access_key_id = somethingsomethingdarkside # aws_secret_access_key = somethingsomethingdarkside aws_creds = ini_parse(File.read(aws_cred_file_location)) profile = config[:aws_profile] Chef::Log.debug "Using AWS profile #{profile}" entries = if aws_creds.values.first.key?("AWSAccessKeyId") aws_creds.values.first else aws_creds[profile] end if entries config[:aws_access_key_id] = entries["AWSAccessKeyId"] || entries["aws_access_key_id"] config[:aws_secret_access_key] = entries["AWSSecretKey"] || entries["aws_secret_access_key"] config[:aws_session_token] = entries["AWSSessionToken"] || entries["aws_session_token"] else raise ArgumentError, "The provided --aws-profile '#{profile}' is invalid. Does the credential file at '#{aws_cred_file_location}' contain this profile?" end end