Class: CfnManage::AWSCredentials

Inherits:
Object
  • Object
show all
Defined in:
lib/cfn_manage/aws_credentials.rb

Class Method Summary collapse

Class Method Details

.get_session_credentials(session_name) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cfn_manage/aws_credentials.rb', line 7

def self.get_session_credentials(session_name)

  #check if AWS_ASSUME_ROLE exists
  session_name =  "#{session_name.gsub('_','-')}-#{Time.now.getutc.to_i}"
  if session_name.length > 64
    session_name = session_name[-64..-1]
  end
  assume_role = ENV['AWS_ASSUME_ROLE'] or nil
  if not assume_role.nil?
    return Aws::AssumeRoleCredentials.new(
        role_arn: assume_role,
        role_session_name: session_name
    )
  end

  # check if explicitly set shared credentials profile
  if ENV.key?('CFN_AWS_PROFILE')
    return Aws::SharedCredentials.new(profile_name: ENV['CFN_AWS_PROFILE'])
  end

  # check if Instance Profile available
  credentials = Aws::InstanceProfileCredentials.new(retries: 2, http_open_timeout:1)
  return credentials unless credentials.credentials.access_key_id.nil?

  # use default profile
  return Aws::SharedCredentials.new()

end