summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoah Kantrowitz <noah@coderanger.net>2018-06-20 13:52:07 -0700
committerNoah Kantrowitz <noah@coderanger.net>2018-06-20 13:52:07 -0700
commit581e153966a0a89b7eafd0ccbf7770dd0a0e909a (patch)
tree1ba427e5eecf9a98ce15c6bf85ce1e9fbc73f462
parentaa4f3285f3e49919e29f40337183fd0510baaee5 (diff)
downloadchef-581e153966a0a89b7eafd0ccbf7770dd0a0e909a.tar.gz
Rework the credentials file system to support any config keys.
This won't work with 100% of Chef config options, but it should cover the majority and avoids situations where we forget to add important options in here in the future. Signed-off-by: Noah Kantrowitz <noah@coderanger.net>
-rw-r--r--chef-config/lib/chef-config/workstation_config_loader.rb39
-rw-r--r--chef-config/spec/unit/workstation_config_loader_spec.rb2
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