class Aws::SharedCredentials
Constants
- KEY_MAP
@api private
Attributes
path[R]
@return [String]
profile_name[R]
@return [String]
Public Class Methods
new(options = {})
click to toggle source
Constructs a new SharedCredentials object. This will load AWS access credentials from an ini file, which supports profiles. The default profile name is 'default'. You can specify the profile name with the `ENV` or with the `:profile_name` option.
@option [String] :path Path to the shared file. Defaults
to "#{Dir.home}/.aws/credentials".
@option [String] :profile_name Defaults to 'default' or
`ENV['AWS_PROFILE']`.
# File lib/aws-sdk-core/shared_credentials.rb, line 22 def initialize(options = {}) @path = options[:path] || default_path @profile_name = options[:profile_name] @profile_name ||= ENV['AWS_PROFILE'] @profile_name ||= 'default' load_from_path if loadable? end
Public Instance Methods
inspect()
click to toggle source
@api private
# File lib/aws-sdk-core/shared_credentials.rb, line 37 def inspect parts = [ self.class.name, "profile_name=#{profile_name.inspect}", "path=#{path.inspect}", ] "#<#{parts.join(' ')}>" end
loadable?()
click to toggle source
@return [Boolean] Returns `true` if a credential file
exists and has appropriate read permissions at {#path}.
@note This method does not indicate if the file found at {#path}
will be parsable, only if it can be read.
# File lib/aws-sdk-core/shared_credentials.rb, line 50 def loadable? !path.nil? && File.exists?(path) && File.readable?(path) end
Private Instance Methods
default_path()
click to toggle source
# File lib/aws-sdk-core/shared_credentials.rb, line 56 def default_path File.join(Dir.home, '.aws', 'credentials') rescue ArgumentError # Dir.home raises ArgumentError when ENV['home'] is not set nil end
ini_parse(file)
click to toggle source
# File lib/aws-sdk-core/shared_credentials.rb, line 85 def ini_parse(file) current_section = {} map = {} file.lines.each 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] || {} map[current_section][item[1]] = item[2] end end end map end
load_from_path()
click to toggle source
# File lib/aws-sdk-core/shared_credentials.rb, line 63 def load_from_path profile = load_profile KEY_MAP.each do |source, target| if profile.key?(source) instance_variable_set("@#{target}", profile[source]) end end end
load_profile()
click to toggle source
# File lib/aws-sdk-core/shared_credentials.rb, line 72 def load_profile if profile = profiles[profile_name] profile else msg = "Profile `#{profile_name}' not found in #{path}" raise Errors::NoSuchProfileError, msg end end
profiles()
click to toggle source
# File lib/aws-sdk-core/shared_credentials.rb, line 81 def profiles ini_parse(File.read(path)) end