From 896e5fa261c9971bd0ff9e5aa3f321559bd02a51 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Fri, 27 Nov 2020 20:33:09 +0530 Subject: [PATCH 1/4] Fix issue where absent HOME would break Dir.home Signed-off-by: Vivek Singh --- lib/inspec/globals.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/inspec/globals.rb b/lib/inspec/globals.rb index b41470a7c..21150454b 100644 --- a/lib/inspec/globals.rb +++ b/lib/inspec/globals.rb @@ -1,11 +1,20 @@ require_relative "utils/install_context" +require 'etc' unless defined?(Etc) + module Inspec extend Inspec::InstallContextHelpers - + def self.config_dir - ENV["INSPEC_CONFIG_DIR"] ? ENV["INSPEC_CONFIG_DIR"] : File.join(Dir.home, ".inspec") + begin + home = Dir.home + rescue ArgumentError, NoMethodError + # $HOME is not set in systemd service File.expand_path('~') will not work here + home = Etc.getpwuid(Process.uid).dir + end + + ENV["INSPEC_CONFIG_DIR"] ? ENV["INSPEC_CONFIG_DIR"] : File.join(home, ".inspec") end def self.src_root From 5162ab4904f7417aacf4d53c35db3ce7fe72651c Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Wed, 2 Dec 2020 11:11:02 +0530 Subject: [PATCH 2/4] Fixes chefstyle & move etc require to block Signed-off-by: Vivek Singh --- lib/inspec/globals.rb | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/lib/inspec/globals.rb b/lib/inspec/globals.rb index 21150454b..48dbbcac3 100644 --- a/lib/inspec/globals.rb +++ b/lib/inspec/globals.rb @@ -1,24 +1,21 @@ require_relative "utils/install_context" -require 'etc' unless defined?(Etc) - module Inspec - extend Inspec::InstallContextHelpers - - def self.config_dir - begin - home = Dir.home - rescue ArgumentError, NoMethodError - # $HOME is not set in systemd service File.expand_path('~') will not work here - home = Etc.getpwuid(Process.uid).dir - end - ENV["INSPEC_CONFIG_DIR"] ? ENV["INSPEC_CONFIG_DIR"] : File.join(home, ".inspec") + def self.config_dir + ENV["INSPEC_CONFIG_DIR"] || File.join(home_path, ".inspec") end def self.src_root @src_root ||= File.expand_path(File.join(__FILE__, "../../..")) end + def self.home_path + Dir.home + rescue ArgumentError, NoMethodError + # $HOME is not set in systemd service File.expand_path('~') will not work here + require "etc" unless defined?(Etc) + Etc.getpwuid(Process.uid).dir + end end From fd0708076f8d77f95b98391926c0bc13bc7afcd8 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Wed, 2 Dec 2020 11:14:12 +0530 Subject: [PATCH 3/4] replace with Etc.getpwuid.dir Signed-off-by: Vivek Singh --- lib/inspec/globals.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/inspec/globals.rb b/lib/inspec/globals.rb index 48dbbcac3..78a677ef8 100644 --- a/lib/inspec/globals.rb +++ b/lib/inspec/globals.rb @@ -16,6 +16,6 @@ module Inspec rescue ArgumentError, NoMethodError # $HOME is not set in systemd service File.expand_path('~') will not work here require "etc" unless defined?(Etc) - Etc.getpwuid(Process.uid).dir + Etc.getpwuid.dir end end From b0ad73c973bd2998a1e3c620dd7e01e34113cc08 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Wed, 2 Dec 2020 12:31:16 +0530 Subject: [PATCH 4/4] minor comment updates Signed-off-by: Vivek Singh --- lib/inspec/globals.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/inspec/globals.rb b/lib/inspec/globals.rb index 78a677ef8..9b9c0fc2d 100644 --- a/lib/inspec/globals.rb +++ b/lib/inspec/globals.rb @@ -14,7 +14,7 @@ module Inspec def self.home_path Dir.home rescue ArgumentError, NoMethodError - # $HOME is not set in systemd service File.expand_path('~') will not work here + # If ENV['HOME'] is not set, Dir.home will fail due to expanding the ~. Fallback to Etc. require "etc" unless defined?(Etc) Etc.getpwuid.dir end