diff options
author | Thom May <thom@may.lt> | 2018-06-21 13:51:34 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-21 13:51:34 +0100 |
commit | 75ae664406d7277095a429ef2f02d4bcc3411c83 (patch) | |
tree | 7e77ed771bf061b33f7d325c141e523ca30cc82e | |
parent | f960c7f3b7c58c80a2e104ed631d97190b272b4e (diff) | |
parent | 581e153966a0a89b7eafd0ccbf7770dd0a0e909a (diff) | |
download | chef-75ae664406d7277095a429ef2f02d4bcc3411c83.tar.gz |
Merge pull request #7387 from coderanger/more-creds
Rework the credentials file system to support any config keys.
-rw-r--r-- | chef-config/lib/chef-config/workstation_config_loader.rb | 39 | ||||
-rw-r--r-- | chef-config/spec/unit/workstation_config_loader_spec.rb | 2 |
2 files changed, 24 insertions, 17 deletions
diff --git a/chef-config/lib/chef-config/workstation_config_loader.rb b/chef-config/lib/chef-config/workstation_config_loader.rb index 99d2ab198f..13187d9975 100644 --- a/chef-config/lib/chef-config/workstation_config_loader.rb +++ b/chef-config/lib/chef-config/workstation_config_loader.rb @@ -147,31 +147,36 @@ module ChefConfig end def apply_credentials(creds, profile) + # Store the profile used in case other things want it. Config.profile ||= profile + # Validate the credentials data. if creds.key?("node_name") && creds.key?("client_name") raise ChefConfig::ConfigurationError, "Do not specify both node_name and client_name. You should prefer client_name." end - Config.node_name = creds.fetch("node_name") if creds.key?("node_name") - Config.node_name = creds.fetch("client_name") if creds.key?("client_name") - Config.chef_server_url = creds.fetch("chef_server_url") if creds.key?("chef_server_url") - Config.validation_client_name = creds.fetch("validation_client_name") if creds.key?("validation_client_name") - - Config.knife.merge!(Hash[creds.fetch("knife", {}).map { |k, v| [k.to_sym, v] }]) - - extract_key(creds, "validation_key", :validation_key, :validation_key_contents) - extract_key(creds, "validator_key", :validation_key, :validation_key_contents) - extract_key(creds, "client_key", :client_key, :client_key_contents) + # Load credentials data into the Chef configuration. + creds.each do |key, value| + case key.to_s + when "client_name" + # Special case because it's weird to set your username via `node_name`. + Config.node_name = value + when "validation_key", "validator_key" + extract_key(value, :validation_key, :validation_key_contents) + when "client_key" + extract_key(value, :client_key, :client_key_contents) + when "knife" + Config.knife.merge!(Hash[value.map { |k, v| [k.to_sym, v] }]) + else + Config[key.to_sym] = value + end + end @credentials_found = true end - def extract_key(creds, name, config_path, config_contents) - return unless creds.has_key?(name) - - val = creds.fetch(name) - if val.start_with?("-----BEGIN RSA PRIVATE KEY-----") - Config.send(config_contents, val) + def extract_key(key_value, config_path, config_contents) + if key_value.start_with?("-----BEGIN RSA PRIVATE KEY-----") + Config.send(config_contents, key_value) else - abs_path = Pathname.new(val).expand_path(home_chef_dir) + abs_path = Pathname.new(key_value).expand_path(home_chef_dir) Config.send(config_path, abs_path) end end diff --git a/chef-config/spec/unit/workstation_config_loader_spec.rb b/chef-config/spec/unit/workstation_config_loader_spec.rb index 8f1cde17e6..f02b1c5016 100644 --- a/chef-config/spec/unit/workstation_config_loader_spec.rb +++ b/chef-config/spec/unit/workstation_config_loader_spec.rb @@ -394,6 +394,7 @@ RSpec.describe ChefConfig::WorkstationConfigLoader do node_name = 'barney' client_key = "barney_rubble.pem" chef_server_url = "https://api.chef.io/organizations/bedrock" +invalid_config_option1234 = "foobar" EOH content end @@ -403,6 +404,7 @@ EOH expect(ChefConfig::Config.chef_server_url).to eq("https://api.chef.io/organizations/bedrock") expect(ChefConfig::Config.client_key.to_s).to eq("#{home}/.chef/barney_rubble.pem") expect(ChefConfig::Config.profile.to_s).to eq("default") + expect(ChefConfig::Config[:invalid_config_option1234]).to eq("foobar") end end |