Sha256: 96dae3145362279f9be11610db842d8ef0bb02ac9940decc2413cce9045d4530

Contents?: true

Size: 1.01 KB

Versions: 13

Compression:

Stored size: 1.01 KB

Contents

require 'aws-sdk-core'

module CfnManage

  class AWSCredentials

    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
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
cfn_manage-0.8.3 lib/cfn_manage/aws_credentials.rb
cfn_manage-0.8.2 lib/cfn_manage/aws_credentials.rb
cfn_manage-0.8.1 lib/cfn_manage/aws_credentials.rb
cfn_manage-0.8.0 lib/cfn_manage/aws_credentials.rb
cfn_manage-0.7.1 lib/cfn_manage/aws_credentials.rb
cfn_manage-0.7.0 lib/cfn_manage/aws_credentials.rb
cfn_manage-0.6.0 lib/cfn_manage/aws_credentials.rb
cfn_manage-0.5.5 lib/cfn_manage/aws_credentials.rb
cfn_manage-0.5.4 lib/cfn_manage/aws_credentials.rb
cfn_manage-0.5.3 lib/cfn_manage/aws_credentials.rb
cfn_manage-0.5.2 lib/cfn_manage/aws_credentials.rb
cfn_manage-0.5.1 lib/cfn_manage/aws_credentials.rb
cfn_manage-0.5.0 lib/cfn_manage/aws_credentials.rb