Sha256: 81a6675cc4650f703a945b85a0a8160a7b49bc3abd7eb46a987f2d58af4a62fa

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

require 'inifile'
require 'csv'

module ChefMetalFog
  module Providers
    class AWS
      # Reads in a credentials file in Amazon's download format and presents the credentials to you
      class Credentials
        def initialize
          @credentials = {}
        end

        include Enumerable

        def default
          @credentials[ENV['AWS_DEFAULT_PROFILE'] || 'default'] || @credentials.first[1]
        end

        def keys
          @credentials.keys
        end

        def [](name)
          @credentials[name]
        end

        def each(&block)
          @credentials.each(&block)
        end

        def load_ini(credentials_ini_file)
          inifile = IniFile.load(File.expand_path(credentials_ini_file))
          inifile.each_section do |section|
            if section =~ /^\s*profile\s+(.+)$/ || section =~ /^\s*(default)\s*/
              profile_name = $1.strip
              profile = inifile[section].inject({}) do |result, pair|
                result[pair[0].to_sym] = pair[1]
                result
              end
              profile[:name] = profile_name
              @credentials[profile_name] = profile
            end
          end
        end

        def load_csv(credentials_csv_file)
          CSV.new(File.open(credentials_csv_file), :headers => :first_row).each do |row|
            @credentials[row['User Name']] = {
              :name => row['User Name'],
              :user_name => row['User Name'],
              :aws_access_key_id => row['Access Key Id'],
              :aws_secret_access_key => row['Secret Access Key']
            }
          end
        end

        def load_default
          load_ini(ENV['AWS_CONFIG_FILE'] || File.expand_path('~/.aws/config'))
        end

        def self.method_missing(name, *args, &block)
          singleton.send(name, *args, &block)
        end

        def self.singleton
          @aws_credentials ||= Credentials.new
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
chef-metal-fog-0.6.1 lib/chef_metal_fog/providers/aws/credentials.rb
chef-metal-fog-0.6 lib/chef_metal_fog/providers/aws/credentials.rb