diff options
author | Tim Smith <tsmith@chef.io> | 2019-12-09 10:16:22 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-09 10:16:22 -0800 |
commit | 3a37b2bb0d4b3f9ec3361b61120d107fd66955cc (patch) | |
tree | 0e86229e34b651ec6268b273b7c05d24b9871d9f /chef-config | |
parent | c084eecf08dfc41e453f71e43c7ce1f3d7473146 (diff) | |
parent | 6c93fb3efe1440ee07de76e8ad1f7b1646bf7cd0 (diff) | |
download | chef-3a37b2bb0d4b3f9ec3361b61120d107fd66955cc.tar.gz |
Merge pull request #9060 from cinc-project/dist_conf_dir
Replace hardcoded /etc/chef with Chef::Dist::CONF_DIR
Diffstat (limited to 'chef-config')
-rw-r--r-- | chef-config/lib/chef-config/config.rb | 44 | ||||
-rw-r--r-- | chef-config/lib/chef-config/dist.rb | 10 | ||||
-rw-r--r-- | chef-config/spec/unit/config_spec.rb | 51 |
3 files changed, 69 insertions, 36 deletions
diff --git a/chef-config/lib/chef-config/config.rb b/chef-config/lib/chef-config/config.rb index 8efdf16d7d..374d548d31 100644 --- a/chef-config/lib/chef-config/config.rb +++ b/chef-config/lib/chef-config/config.rb @@ -34,6 +34,7 @@ require "uri" unless defined?(URI) require "addressable/uri" unless defined?(Addressable::URI) require "openssl" unless defined?(OpenSSL) require "yaml" +require_relative "dist" module ChefConfig @@ -73,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 @@ -284,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 @@ -655,9 +681,9 @@ module ChefConfig if chef_zero.enabled nil elsif target_mode? - platform_specific_path("/etc/chef/#{target_mode.host}/client.pem") + PathHelper.cleanpath("#{etc_chef_dir}/#{target_mode.host}/client.pem") else - platform_specific_path("/etc/chef/client.pem") + PathHelper.cleanpath("#{etc_chef_dir}/client.pem") end end @@ -679,10 +705,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("/etc/chef/#{target_mode.host}/encrypted_data_bag_secret")) - platform_specific_path("/etc/chef/#{target_mode.host}/encrypted_data_bag_secret") - elsif File.exist?(platform_specific_path("/etc/chef/encrypted_data_bag_secret")) - platform_specific_path("/etc/chef/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 @@ -709,7 +735,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("/etc/chef/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 new file mode 100644 index 0000000000..01db6765fb --- /dev/null +++ b/chef-config/lib/chef-config/dist.rb @@ -0,0 +1,10 @@ +module ChefConfig + class Dist + # The chef executable name. Also used in directory names. + EXEC = "chef".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) |