Class: AwsRunAs::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_runas/config.rb

Overview

Manages the configuartion file, including loading and retrieving values.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, profile:) ⇒ Config

Returns a new instance of Config.



30
31
32
33
34
35
# File 'lib/aws_runas/config.rb', line 30

def initialize(path:, profile:)
  @path = path
  @path = self.class.find_config_file unless @path
  fail(Errno::ENOENT, "#{@path}") unless File.exist?(@path.to_s)
  @profile = profile
end

Instance Attribute Details

#profileObject (readonly)

Returns the value of attribute profile.



20
21
22
# File 'lib/aws_runas/config.rb', line 20

def profile
  @profile
end

Class Method Details

.find_config_fileObject

Finds the configuration file (used if no file is specified). paths searched: ./aws_config, and ~/.aws/config.



23
24
25
26
27
28
# File 'lib/aws_runas/config.rb', line 23

def self.find_config_file
  local_config = File.expand_path('aws_config')
  user_config = File.expand_path('~/.aws/config')
  return local_config if File.exist?(local_config)
  user_config if File.exist?(user_config)
end

Instance Method Details

#load_config_value(key:) ⇒ Object

Loads the config section for a specific profile.



38
39
40
41
42
43
44
45
46
# File 'lib/aws_runas/config.rb', line 38

def load_config_value(key:)
  section = @profile
  section = "profile #{@profile}" unless @profile == 'default'
  aws_config = IniFile.load(@path)
  unless aws_config.has_section?(section)
    fail(NameError, "Profile #{@profile} not found in #{@path}")
  end
  aws_config[section][key]
end

#load_source_profileObject

loads the soruce credentials profile based on the supplied profile.



55
56
57
58
59
# File 'lib/aws_runas/config.rb', line 55

def load_source_profile
  source_profile = load_config_value(key: 'source_profile')
  return source_profile if source_profile
  @profile
end

#mfa_required?Boolean

Checks to see if MFA is required for a specific profile.

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/aws_runas/config.rb', line 49

def mfa_required?
  return true if load_config_value(key: 'mfa_serial') && !ENV.include?('AWS_SESSION_TOKEN')
  false
end