diff options
author | Noah Kantrowitz <noah@coderanger.net> | 2018-06-23 21:57:37 -0700 |
---|---|---|
committer | Noah Kantrowitz <noah@coderanger.net> | 2018-06-23 21:57:37 -0700 |
commit | e47ea80c9ef408e9bf8d4efef851899fceff7b6c (patch) | |
tree | c34cb3eb80ffb3af5f9b443a2801a11aac91b2b7 | |
parent | c3bda57fcef745b1c570a654e9a633dcf842b96f (diff) | |
download | chef-e47ea80c9ef408e9bf8d4efef851899fceff7b6c.tar.gz |
Fill in some default config values for workstation commands.
node_name defaults to the current username. client_key defaults to either $node_name.pem or user.pem. validation_key to either $validation_client_name.pem, validator.pem, or validation.pem.
Signed-off-by: Noah Kantrowitz <noah@coderanger.net>
-rw-r--r-- | chef-config/lib/chef-config/workstation_config_loader.rb | 50 | ||||
-rw-r--r-- | chef-config/spec/unit/workstation_config_loader_spec.rb | 64 |
2 files changed, 114 insertions, 0 deletions
diff --git a/chef-config/lib/chef-config/workstation_config_loader.rb b/chef-config/lib/chef-config/workstation_config_loader.rb index 13187d9975..2afe8de547 100644 --- a/chef-config/lib/chef-config/workstation_config_loader.rb +++ b/chef-config/lib/chef-config/workstation_config_loader.rb @@ -83,6 +83,8 @@ module ChefConfig end load_dot_d(Config[:config_d_dir]) if Config[:config_d_dir] + + apply_defaults end # (Private API, public for test purposes) @@ -210,6 +212,54 @@ module ChefConfig raise ChefConfig::ConfigurationError, message end + # Apply default configuration values for workstation-style tools. + # + # Global defaults should go in {ChefConfig::Config} instead, this is only + # for things like `knife` and `chef`. + # + # @api private + # @since 14.3 + # @return [void] + def apply_defaults + # If we don't have a better guess use the username. + Config[:node_name] ||= Etc.getlogin + # If we don't have a key (path or inline) check user.pem and $node_name.pem. + unless Config.has_key?(:client_key) || Config.has_key?(:client_key_contents) + Config[:client_key] = find_default_key(["#{Config[:node_name]}.pem", "user.pem"]) + end + # Similarly look for a validation key file, though this should be less + # common these days. + unless Config.has_key?(:validation_key) || Config.has_key?(:validation_key_contents) + Config[:validation_key] = find_default_key(["#{Config[:validation_client_name]}.pem", "validator.pem", "validation.pem"]) + end + end + + # Look for a default key file. + # + # This searches for any of a list of possible default keys, checking both + # the local `.chef/` folder and the home directory `~/.chef/`. Returns `nil` + # if no matching file is found. + # + # @api private + # @since 14.3 + # @param key_names [Array<String>] A list of possible filenames to check for. + # The first one found will be returned. + # @return [String, nil] + def find_default_key(key_names) + key_names.each do |filename| + path = Pathname.new(filename) + # If we have a config location (like ./.chef/), look there first. + if config_location + local_path = path.expand_path(File.dirname(config_location)) + return local_path.to_s if local_path.exist? + end + # Then check ~/.chef. + home_path = path.expand_path(home_chef_dir) + return home_path.to_s if home_path.exist? + end + nil + end + def highlight_config_error(file, line) config_file_lines = [] IO.readlines(file).each_with_index { |l, i| config_file_lines << "#{(i + 1).to_s.rjust(3)}: #{l.chomp}" } diff --git a/chef-config/spec/unit/workstation_config_loader_spec.rb b/chef-config/spec/unit/workstation_config_loader_spec.rb index f02b1c5016..024cb3e928 100644 --- a/chef-config/spec/unit/workstation_config_loader_spec.rb +++ b/chef-config/spec/unit/workstation_config_loader_spec.rb @@ -271,6 +271,70 @@ RSpec.describe ChefConfig::WorkstationConfigLoader do config_loader.load expect(ChefConfig::Config.config_file).to eq(explicit_config_location) end + + it "loads a default value for node_name" do + allow(Etc).to receive(:getlogin).and_return("notauser") + config_loader.load + expect(ChefConfig::Config.node_name).to eq("notauser") + end + + context "with a user.pem" do + before do + allow(Etc).to receive(:getlogin).and_return("notauser") + allow(FileTest).to receive(:exist?).and_call_original + allow(FileTest).to receive(:exist?).with(File.expand_path("../notauser.pem", explicit_config_location)).and_return(false) + allow(FileTest).to receive(:exist?).with(File.expand_path("../user.pem", explicit_config_location)).and_return(true) + end + + it "loads a default value for client_key" do + config_loader.load + expect(ChefConfig::Config.client_key).to eq(File.expand_path("../user.pem", explicit_config_location)) + end + end + + context "with a notauser.pem" do + before do + allow(Etc).to receive(:getlogin).and_return("notauser") + allow(FileTest).to receive(:exist?).and_call_original + allow(FileTest).to receive(:exist?).with(File.expand_path("../notauser.pem", explicit_config_location)).and_return(true) + allow(FileTest).to receive(:exist?).with(File.expand_path("../user.pem", explicit_config_location)).and_return(false) + end + + it "loads a default value for client_key" do + config_loader.load + expect(ChefConfig::Config.client_key).to eq(File.expand_path("../notauser.pem", explicit_config_location)) + end + end + + context "with a valclient.pem" do + before do + ChefConfig::Config.validation_client_name = "valclient" + allow(FileTest).to receive(:exist?).and_call_original + allow(FileTest).to receive(:exist?).with(File.expand_path("../valclient.pem", explicit_config_location)).and_return(true) + allow(FileTest).to receive(:exist?).with(File.expand_path("../validator.pem", explicit_config_location)).and_return(false) + allow(FileTest).to receive(:exist?).with(File.expand_path("../validation.pem", explicit_config_location)).and_return(false) + end + + it "loads a default value for validation_key" do + config_loader.load + expect(ChefConfig::Config.validation_key).to eq(File.expand_path("../valclient.pem", explicit_config_location)) + end + end + + context "with a validator.pem" do + before do + ChefConfig::Config.validation_client_name = "valclient" + allow(FileTest).to receive(:exist?).and_call_original + allow(FileTest).to receive(:exist?).with(File.expand_path("../valclient.pem", explicit_config_location)).and_return(false) + allow(FileTest).to receive(:exist?).with(File.expand_path("../validator.pem", explicit_config_location)).and_return(true) + allow(FileTest).to receive(:exist?).with(File.expand_path("../validation.pem", explicit_config_location)).and_return(false) + end + + it "loads a default value for validation_key" do + config_loader.load + expect(ChefConfig::Config.validation_key).to eq(File.expand_path("../validator.pem", explicit_config_location)) + end + end end context "and has a syntax error" do |