Class: Amigrind::Config

Inherits:
Settingslogic
  • Object
show all
Extended by:
Amigrind::Core::Logging::Mixin
Includes:
Amigrind::Core::Logging::Mixin
Defined in:
lib/amigrind/config.rb

Overview

Config values we care about: ‘auto_profile` - boolean; if set

Constant Summary collapse

CREDENTIAL_TYPES =
[ :default, :iam, :shared ].freeze

Instance Method Summary collapse

Instance Method Details

#aws_credentials(environment = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/amigrind/config.rb', line 39

def aws_credentials(environment = nil)
  # This is a minor disaster and should be refactored, but essentially boils down
  # to specifying a credentials_type and any related settings. If
  # `auto_profile_from_environment` is set, any time that an environment is
  # passed in, `auto_profile_prefix` will be prepended to the environment name
  # to generate the AWS profile name. This allows one to, for example, have
  # a 'foocorp_production' profile that will be automatically used when working
  # with the 'production' environment.
  aws = Config['aws'] || {}

  credential_type = (aws['credentials_type'] || :default).to_sym
  auto_profile_from_environment = !!aws['auto_profile_from_environment']
  auto_profile_prefix = aws['auto_profile_prefix'] || ''
  profile_name = aws['profile_name']

  debug_log "credentials_type: #{credential_type}"

  raise "setting error: can only use profile_name with credential_type = shared." \
    if credential_type != :shared && !profile_name.nil?

  raise "setting error: cannot use both profile_name and auto_profile_from_environment." \
    if !profile_name.nil? && auto_profile_from_environment

  case credential_type
  when :default
    debug_log 'Using default credentials.'
    nil
  when :shared
    if auto_profile_from_environment &&
        profile_name.nil? && !environment.nil?
      environment = environment.name \
        if (environment.is_a?(Amigrind::Environments::Environment))

      profile_name = "#{auto_profile_prefix}#{environment}"
      debug_log "auto_profile_from_environment enabled and environment " \
                "passed; setting profile_name to '#{profile_name}'."
    end

    p = (profile_name || 'default').strip

    debug_log "Using profile '#{p}'."
    Aws::SharedCredentials.new(profile_name: p)
  when :iam
    debug_log 'Using IAM credentials.'
    Aws::InstanceProfileCredentials.new
  else
    raise "invalid credential type '#{credential_type}' " \
          "(allowed: #{CREDENTIAL_TYPES.join(', ')})"
  end
end