Class: Chef::Provisioning::AWSDriver::Credentials
- Inherits:
-
Object
- Object
- Chef::Provisioning::AWSDriver::Credentials
- Includes:
- Mixin::DeepMerge, Enumerable
- Defined in:
- lib/chef/provisioning/aws_driver/credentials.rb
Overview
Reads in credential files in Amazon’s download format and presents the credentials to you
Class Method Summary collapse
Instance Method Summary collapse
- #[](name) ⇒ Object
- #default ⇒ Object
- #each(&block) ⇒ Object
-
#initialize ⇒ Credentials
constructor
A new instance of Credentials.
- #keys ⇒ Object
- #load_config_ini(config_ini_file) ⇒ Object
- #load_credentials_ini(credentials_ini_file) ⇒ Object
- #load_csv(credentials_csv_file) ⇒ Object
- #load_default ⇒ Object
- #load_env_variables ⇒ Object
- #load_inis(config_ini_file, credentials_ini_file = nil) ⇒ Object
Constructor Details
#initialize ⇒ Credentials
Returns a new instance of Credentials.
10 11 12 13 |
# File 'lib/chef/provisioning/aws_driver/credentials.rb', line 10 def initialize @credentials = {} load_default end |
Class Method Details
.method_missing(name, *args, &block) ⇒ Object
110 111 112 |
# File 'lib/chef/provisioning/aws_driver/credentials.rb', line 110 def self.method_missing(name, *args, &block) singleton.send(name, *args, &block) end |
.singleton ⇒ Object
114 115 116 |
# File 'lib/chef/provisioning/aws_driver/credentials.rb', line 114 def self.singleton @aws_credentials ||= Credentials.new end |
Instance Method Details
#[](name) ⇒ Object
29 30 31 |
# File 'lib/chef/provisioning/aws_driver/credentials.rb', line 29 def [](name) @credentials[name] end |
#default ⇒ Object
18 19 20 21 22 23 |
# File 'lib/chef/provisioning/aws_driver/credentials.rb', line 18 def default if @credentials.empty? raise "No credentials loaded! Do you have a ~/.aws/config?" end @credentials[ENV["AWS_DEFAULT_PROFILE"] || "default"] || @credentials.first[1] end |
#each(&block) ⇒ Object
33 34 35 |
# File 'lib/chef/provisioning/aws_driver/credentials.rb', line 33 def each(&block) @credentials.each(&block) end |
#keys ⇒ Object
25 26 27 |
# File 'lib/chef/provisioning/aws_driver/credentials.rb', line 25 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 |
# File 'lib/chef/provisioning/aws_driver/credentials.rb', line 45 def load_config_ini(config_ini_file) inifile = IniFile.load(File.(config_ini_file)) config = {} if inifile inifile.each_section do |section| next unless section =~ /^\s*profile\s+(.+)$/ || section =~ /^\s*(default)\s*/ profile_name = Regexp.last_match(1).strip profile = inifile[section].each_with_object({}) do |pair, result| result[pair[0].to_sym] = pair[1] end profile[:name] = profile_name config[profile_name] = profile end end config end |
#load_credentials_ini(credentials_ini_file) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/chef/provisioning/aws_driver/credentials.rb', line 62 def load_credentials_ini(credentials_ini_file) inifile = IniFile.load(File.(credentials_ini_file)) config = {} if inifile inifile.each_section do |section| profile = inifile[section].each_with_object({}) do |pair, result| result[pair[0].to_sym] = pair[1] end profile[:name] = section config[section] = profile end end config end |
#load_csv(credentials_csv_file) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/chef/provisioning/aws_driver/credentials.rb', line 77 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_default ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/chef/provisioning/aws_driver/credentials.rb', line 97 def load_default config_file = ENV["AWS_CONFIG_FILE"] || File.("~/.aws/config") credentials_file = ENV["AWS_SHARED_CREDENTIALS_FILE"] || ENV["AWS_CREDENTIAL_FILE"] || File.("~/.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 load_env_variables if @credentials.empty? end |
#load_env_variables ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/chef/provisioning/aws_driver/credentials.rb', line 88 def load_env_variables if ENV["AWS_ACCESS_KEY_ID"] && ENV["AWS_SECRET_ACCESS_KEY"] @credentials["default"] = { aws_access_key_id: ENV["AWS_ACCESS_KEY_ID"], aws_secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"] } end end |
#load_inis(config_ini_file, credentials_ini_file = nil) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/chef/provisioning/aws_driver/credentials.rb', line 37 def load_inis(config_ini_file, credentials_ini_file = nil) @credentials = load_config_ini(config_ini_file) if credentials_ini_file @credentials = deep_merge!(@credentials, load_credentials_ini(credentials_ini_file)) end end |