Class: Chef::Provisioning::FogDriver::Providers::AWS::Credentials

Inherits:
Object
  • Object
show all
Includes:
Mixin::DeepMerge, Enumerable
Defined in:
lib/chef/provisioning/fog_driver/providers/aws/credentials.rb

Overview

Reads in a credentials file in Amazon’s download format and presents the credentials to you

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCredentials

Returns a new instance of Credentials.



12
13
14
# File 'lib/chef/provisioning/fog_driver/providers/aws/credentials.rb', line 12

def initialize
  @credentials = {}
end

Class Method Details

.method_missing(name, *args, &block) ⇒ Object



103
104
105
# File 'lib/chef/provisioning/fog_driver/providers/aws/credentials.rb', line 103

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

.singletonObject



107
108
109
# File 'lib/chef/provisioning/fog_driver/providers/aws/credentials.rb', line 107

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

Instance Method Details

#[](name) ⇒ Object



30
31
32
# File 'lib/chef/provisioning/fog_driver/providers/aws/credentials.rb', line 30

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

#defaultObject



19
20
21
22
23
24
# File 'lib/chef/provisioning/fog_driver/providers/aws/credentials.rb', line 19

def default
  if @credentials.size == 0
    raise "No credentials loaded!  Do you have one of ~/.aws/credentials or ~/.aws/config?"
  end
  @credentials[ENV['AWS_DEFAULT_PROFILE'] || 'default'] || @credentials.first[1]
end

#each(&block) ⇒ Object



34
35
36
# File 'lib/chef/provisioning/fog_driver/providers/aws/credentials.rb', line 34

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

#keysObject



26
27
28
# File 'lib/chef/provisioning/fog_driver/providers/aws/credentials.rb', line 26

def keys
  @credentials.keys
end

#load_config_ini(config_ini_file) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/chef/provisioning/fog_driver/providers/aws/credentials.rb', line 45

def load_config_ini(config_ini_file)
  inifile = IniFile.load(File.expand_path(config_ini_file))
  config = {}
  if inifile
    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
        config[profile_name] = profile
      end
    end
  end
  config
end

#load_credentials_ini(credentials_ini_file) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/chef/provisioning/fog_driver/providers/aws/credentials.rb', line 64

def load_credentials_ini(credentials_ini_file)
  inifile = IniFile.load(File.expand_path(credentials_ini_file))
  config = {}
  if inifile
    inifile.each_section do |section|
      profile = inifile[section].inject({}) do |result, pair|
        result[pair[0].to_sym] = pair[1]
        result
      end
      profile[:name] = section
      config[section] = profile
    end
  end
  config
end

#load_csv(credentials_csv_file) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/chef/provisioning/fog_driver/providers/aws/credentials.rb', line 80

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

#load_defaultObject



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/chef/provisioning/fog_driver/providers/aws/credentials.rb', line 91

def load_default
  config_file = ENV['AWS_CONFIG_FILE'] || File.expand_path('~/.aws/config')
  credentials_file = ENV['AWS_CREDENTIAL_FILE'] || File.expand_path('~/.aws/credentials')
  if File.file?(config_file)
    if File.file?(credentials_file)
      load_inis(config_file, credentials_file)
    else
      load_inis(config_file)
    end
  end
end

#load_inis(config_ini_file, credentials_ini_file = nil) ⇒ Object



38
39
40
41
42
43
# File 'lib/chef/provisioning/fog_driver/providers/aws/credentials.rb', line 38

def load_inis(config_ini_file, credentials_ini_file = nil)
  @credentials = load_config_ini(config_ini_file)
  @credentials = deep_merge!(@credentials,
                             load_credentials_ini(credentials_ini_file)
                            ) if credentials_ini_file
end