diff options
-rw-r--r-- | chef-config/lib/chef-config/config.rb | 43 | ||||
-rw-r--r-- | chef-config/lib/chef-config/dist.rb | 6 | ||||
-rw-r--r-- | chef-config/spec/unit/config_spec.rb | 51 | ||||
-rw-r--r-- | lib/chef/dist.rb | 4 |
4 files changed, 65 insertions, 39 deletions
diff --git a/chef-config/lib/chef-config/config.rb b/chef-config/lib/chef-config/config.rb index a024d14eff..89bf625ec0 100644 --- a/chef-config/lib/chef-config/config.rb +++ b/chef-config/lib/chef-config/config.rb @@ -74,6 +74,31 @@ module ChefConfig path end + # On *nix, /etc/chef + def self.etc_chef_dir + path = ChefUtils.windows? ? c_chef_dir : PathHelper.join("/etc", ChefConfig::Dist::DIR_SUFFIX) + PathHelper.cleanpath(path) + end + + # On *nix, /var/chef + def self.var_chef_dir + path = ChefUtils.windows? ? c_chef_dir : PathHelper.join("/var", ChefConfig::Dist::DIR_SUFFIX) + PathHelper.cleanpath(path) + end + + # On *nix, the root of /var/, used to test if we can create and write in /var/chef + def self.var_root_dir + path = ChefUtils.windows? ? c_chef_dir : "/var" + PathHelper.cleanpath(path) + end + + # On windows, C:/chef/ + def self.c_chef_dir + drive = windows_installation_drive || "C:" + path = PathHelper.join(drive, ChefConfig::Dist::DIR_SUFFIX) + PathHelper.cleanpath(path) + end + # the drive where Chef is installed on a windows host. This is determined # either by the drive containing the current file or by the SYSTEMDRIVE ENV # variable @@ -285,8 +310,8 @@ module ChefConfig if local_mode PathHelper.join(config_dir, "local-mode-cache") else - primary_cache_root = platform_specific_path("/var") - primary_cache_path = platform_specific_path("/var/chef") + primary_cache_root = var_root_dir + primary_cache_path = var_chef_dir # Use /var/chef as the cache path only if that folder exists and we can read and write # into it, or /var exists and we can read and write into it (we'll create /var/chef later). # Otherwise, we'll create .chef under the user's home directory and use that as @@ -646,9 +671,9 @@ module ChefConfig if chef_zero.enabled nil elsif target_mode? - platform_specific_path("#{ChefConfig::Dist::CONF_DIR}/#{target_mode.host}/client.pem") + PathHelper.cleanpath("#{etc_chef_dir}/#{target_mode.host}/client.pem") else - platform_specific_path("#{ChefConfig::Dist::CONF_DIR}/client.pem") + PathHelper.cleanpath("#{etc_chef_dir}/client.pem") end end @@ -670,10 +695,10 @@ module ChefConfig # This secret is used to decrypt encrypted data bag items. default(:encrypted_data_bag_secret) do - if target_mode? && File.exist?(platform_specific_path("#{ChefConfig::Dist::CONF_DIR}/#{target_mode.host}/encrypted_data_bag_secret")) - platform_specific_path("#{ChefConfig::Dist::CONF_DIR}/#{target_mode.host}/encrypted_data_bag_secret") - elsif File.exist?(platform_specific_path("#{ChefConfig::Dist::CONF_DIR}/encrypted_data_bag_secret")) - platform_specific_path("#{ChefConfig::Dist::CONF_DIR}/encrypted_data_bag_secret") + if target_mode? && File.exist?(PathHelper.cleanpath("#{etc_chef_dir}/#{target_mode.host}/encrypted_data_bag_secret")) + PathHelper.cleanpath("#{etc_chef_dir}/#{target_mode.host}/encrypted_data_bag_secret") + elsif File.exist?(PathHelper.cleanpath("#{etc_chef_dir}/encrypted_data_bag_secret")) + PathHelper.cleanpath("#{etc_chef_dir}/encrypted_data_bag_secret") else nil end @@ -700,7 +725,7 @@ module ChefConfig # The `validation_key` is never used if the `client_key` exists. # # If chef-zero is enabled, this defaults to nil (no authentication). - default(:validation_key) { chef_zero.enabled ? nil : platform_specific_path("#{ChefConfig::Dist::CONF_DIR}/validation.pem") } + default(:validation_key) { chef_zero.enabled ? nil : PathHelper.cleanpath("#{etc_chef_dir}/validation.pem") } default :validation_client_name do # If the URL is set and looks like a normal Chef Server URL, extract the # org name and use that as part of the default. diff --git a/chef-config/lib/chef-config/dist.rb b/chef-config/lib/chef-config/dist.rb index 571940d32b..01db6765fb 100644 --- a/chef-config/lib/chef-config/dist.rb +++ b/chef-config/lib/chef-config/dist.rb @@ -2,7 +2,9 @@ module ChefConfig class Dist # The chef executable name. Also used in directory names. EXEC = "chef".freeze - # The Chef configuration directory. It will be used by Chef's dist.rb. - CONF_DIR = "/etc/#{ChefConfig::Dist::EXEC}".freeze + + # The suffix for Chef's /etc/chef, /var/chef and C:\\Chef directories + # "cinc" => /etc/cinc, /var/cinc, C:\\cinc + DIR_SUFFIX = "chef".freeze end end diff --git a/chef-config/spec/unit/config_spec.rb b/chef-config/spec/unit/config_spec.rb index 674246dfd1..c0cd08893d 100644 --- a/chef-config/spec/unit/config_spec.rb +++ b/chef-config/spec/unit/config_spec.rb @@ -196,9 +196,6 @@ RSpec.describe ChefConfig::Config do [ false, true ].each do |is_windows| context "On #{is_windows ? "Windows" : "Unix"}" do - def to_platform(*args) - ChefConfig::Config.platform_specific_path(*args) - end before :each do allow(ChefUtils).to receive(:windows?).and_return(is_windows) @@ -277,7 +274,7 @@ RSpec.describe ChefConfig::Config do end describe "ChefConfig::Config[:client_key]" do - let(:path_to_client_key) { to_platform("/etc/chef") + ChefConfig::PathHelper.path_separator } + let(:path_to_client_key) { ChefConfig::Config.etc_chef_dir + ChefConfig::PathHelper.path_separator } it "sets the default path to the client key" do expect(ChefConfig::Config.client_key).to eq(path_to_client_key + "client.pem") @@ -412,7 +409,7 @@ RSpec.describe ChefConfig::Config do context "when /var/chef exists and is accessible" do before do - allow(ChefConfig::Config).to receive(:path_accessible?).with(to_platform("/var/chef")).and_return(true) + allow(ChefConfig::Config).to receive(:path_accessible?).with(ChefConfig::Config.var_chef_dir).and_return(true) end it "defaults to /var/chef" do @@ -430,25 +427,25 @@ RSpec.describe ChefConfig::Config do context "when /var/chef does not exist and /var is accessible" do it "defaults to /var/chef" do - allow(File).to receive(:exists?).with(to_platform("/var/chef")).and_return(false) - allow(ChefConfig::Config).to receive(:path_accessible?).with(to_platform("/var")).and_return(true) + allow(File).to receive(:exists?).with(ChefConfig::Config.var_chef_dir).and_return(false) + allow(ChefConfig::Config).to receive(:path_accessible?).with(ChefConfig::Config.var_root_dir).and_return(true) expect(ChefConfig::Config[:cache_path]).to eq(primary_cache_path) end end context "when /var/chef does not exist and /var is not accessible" do it "defaults to $HOME/.chef" do - allow(File).to receive(:exists?).with(to_platform("/var/chef")).and_return(false) - allow(ChefConfig::Config).to receive(:path_accessible?).with(to_platform("/var")).and_return(false) + allow(File).to receive(:exists?).with(ChefConfig::Config.var_chef_dir).and_return(false) + allow(ChefConfig::Config).to receive(:path_accessible?).with(ChefConfig::Config.var_root_dir).and_return(false) expect(ChefConfig::Config[:cache_path]).to eq(secondary_cache_path) end end context "when /var/chef exists and is not accessible" do before do - allow(File).to receive(:exists?).with(to_platform("/var/chef")).and_return(true) - allow(File).to receive(:readable?).with(to_platform("/var/chef")).and_return(true) - allow(File).to receive(:writable?).with(to_platform("/var/chef")).and_return(false) + allow(File).to receive(:exists?).with(ChefConfig::Config.var_chef_dir).and_return(true) + allow(File).to receive(:readable?).with(ChefConfig::Config.var_chef_dir).and_return(true) + allow(File).to receive(:writable?).with(ChefConfig::Config.var_chef_dir).and_return(false) end it "defaults to $HOME/.chef" do @@ -471,21 +468,21 @@ RSpec.describe ChefConfig::Config do context "and config_dir is /a/b/c" do before do - ChefConfig::Config.config_dir to_platform("/a/b/c") + ChefConfig::Config.config_dir ChefConfig::PathHelper.cleanpath("/a/b/c") end it "cache_path is /a/b/c/local-mode-cache" do - expect(ChefConfig::Config.cache_path).to eq(to_platform("/a/b/c/local-mode-cache")) + expect(ChefConfig::Config.cache_path).to eq(ChefConfig::PathHelper.cleanpath("/a/b/c/local-mode-cache")) end end context "and config_dir is /a/b/c/" do before do - ChefConfig::Config.config_dir to_platform("/a/b/c/") + ChefConfig::Config.config_dir ChefConfig::PathHelper.cleanpath("/a/b/c/") end it "cache_path is /a/b/c/local-mode-cache" do - expect(ChefConfig::Config.cache_path).to eq(to_platform("/a/b/c/local-mode-cache")) + expect(ChefConfig::Config.cache_path).to eq(ChefConfig::PathHelper.cleanpath("/a/b/c/local-mode-cache")) end end end @@ -651,15 +648,15 @@ RSpec.describe ChefConfig::Config do end it "expands the path when determining config_dir" do - # config_dir goes through PathHelper.canonical_path, which + # config_dir goes through ChefConfig::PathHelper.canonical_path, which # downcases on windows because the FS is case insensitive, so we # have to downcase expected and actual to make the tests work. - expect(ChefConfig::Config.config_dir.downcase).to eq(to_platform(Dir.pwd).downcase) + expect(ChefConfig::Config.config_dir.downcase).to eq(ChefConfig::PathHelper.cleanpath(Dir.pwd).downcase) end it "does not set derived paths at FS root" do ChefConfig::Config.local_mode = true - expect(ChefConfig::Config.cache_path.downcase).to eq(to_platform(File.join(Dir.pwd, "local-mode-cache")).downcase) + expect(ChefConfig::Config.cache_path.downcase).to eq(ChefConfig::PathHelper.cleanpath(File.join(Dir.pwd, "local-mode-cache")).downcase) end end @@ -667,13 +664,13 @@ RSpec.describe ChefConfig::Config do context "when the config file is /etc/chef/client.rb" do before do - config_location = to_platform("/etc/chef/client.rb").downcase + config_location = ChefConfig::PathHelper.cleanpath(ChefConfig::PathHelper.join(ChefConfig::Config.etc_chef_dir, "client.rb")).downcase allow(File).to receive(:absolute_path).with(config_location).and_return(config_location) ChefConfig::Config.config_file = config_location end it "config_dir is /etc/chef" do - expect(ChefConfig::Config.config_dir).to eq(to_platform("/etc/chef").downcase) + expect(ChefConfig::Config.config_dir).to eq(ChefConfig::Config.etc_chef_dir.downcase) end context "and chef is running in local mode" do @@ -682,17 +679,17 @@ RSpec.describe ChefConfig::Config do end it "config_dir is /etc/chef" do - expect(ChefConfig::Config.config_dir).to eq(to_platform("/etc/chef").downcase) + expect(ChefConfig::Config.config_dir).to eq(ChefConfig::Config.etc_chef_dir.downcase) end end context "when config_dir is set to /other/config/dir/" do before do - ChefConfig::Config.config_dir = to_platform("/other/config/dir/") + ChefConfig::Config.config_dir = ChefConfig::PathHelper.cleanpath("/other/config/dir/") end it "yields the explicit value" do - expect(ChefConfig::Config.config_dir).to eq(to_platform("/other/config/dir/")) + expect(ChefConfig::Config.config_dir).to eq(ChefConfig::PathHelper.cleanpath("/other/config/dir/")) end end @@ -721,7 +718,7 @@ RSpec.describe ChefConfig::Config do if is_windows context "when the user's home dir is windows specific" do before do - ChefConfig::Config.user_home = to_platform("/home/charlie/") + ChefConfig::Config.user_home = ChefConfig::PathHelper.cleanpath("/home/charlie/") end it "config_dir is with backslashes" do @@ -777,7 +774,7 @@ RSpec.describe ChefConfig::Config do describe "ChefConfig::Config[:user_home]" do it "should set when HOME is provided" do - expected = to_platform("/home/kitten") + expected = ChefConfig::PathHelper.cleanpath("/home/kitten") allow(ChefConfig::PathHelper).to receive(:home).and_return(expected) expect(ChefConfig::Config[:user_home]).to eq(expected) end @@ -789,7 +786,7 @@ RSpec.describe ChefConfig::Config do end describe "ChefConfig::Config[:encrypted_data_bag_secret]" do - let(:db_secret_default_path) { to_platform("/etc/chef/encrypted_data_bag_secret") } + let(:db_secret_default_path) { ChefConfig::PathHelper.cleanpath("#{ChefConfig::Config.etc_chef_dir}/encrypted_data_bag_secret") } before do allow(File).to receive(:exist?).with(db_secret_default_path).and_return(secret_exists) diff --git a/lib/chef/dist.rb b/lib/chef/dist.rb index 03c4fdc7f8..86653735f2 100644 --- a/lib/chef/dist.rb +++ b/lib/chef/dist.rb @@ -1,6 +1,8 @@ class Chef class Dist require "chef-config/dist" + require "chef-config/config" + # This class is not fully implemented, depending on it is not recommended! # When referencing a product directly, like Chef (Now Chef Infra) PRODUCT = "Chef Infra Client".freeze @@ -48,7 +50,7 @@ class Chef SHELL_CONF = "chef_shell.rb".freeze # The configuration directory - CONF_DIR = ChefConfig::Dist::CONF_DIR.freeze + CONF_DIR = ChefConfig::Config::etc_chef_dir.freeze # The user's configuration directory USER_CONF_DIR = ".chef".freeze |