From 17ce88b63d6989b4f2a34bf49dbd683334d95448 Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Fri, 13 Nov 2015 11:53:21 +0100 Subject: [PATCH] api: don't force root on os_env --- lib/resources/os_env.rb | 29 ++++++++++++++++++++++++----- test/helper.rb | 2 +- test/unit/mock/cmd/PATH | 1 - test/unit/mock/cmd/env | 1 + 4 files changed, 26 insertions(+), 7 deletions(-) delete mode 100644 test/unit/mock/cmd/PATH create mode 100644 test/unit/mock/cmd/env diff --git a/lib/resources/os_env.rb b/lib/resources/os_env.rb index fe7a0bf15..1e4aceca8 100644 --- a/lib/resources/os_env.rb +++ b/lib/resources/os_env.rb @@ -11,15 +11,16 @@ # its(:split) { should_not include('.') } # end +require 'utils/simpleconfig' + class OsEnv < Inspec.resource(1) name 'os_env' attr_reader :content - def initialize(env) + def initialize(env = nil) @osenv = env - cmd = inspec.command("su - root -c 'echo $#{env}'") - @content = cmd.stdout.chomp - @content = nil if cmd.exit_status != 0 + @content = nil + @content = params[env] unless env.nil? end def split @@ -29,6 +30,24 @@ class OsEnv < Inspec.resource(1) end def to_s - "Environment Variable #{@osenv}" + if @osenv.nil? + 'Environment variables' + else + "Environment variable #{@osenv}" + end + end + + private + + def params + return @params if defined? @params + out = inspec.command('env') + out = inspec.command('printenv') unless out.exit_status == 0 + unless out.exit_status == 0 + skip_resource "Can't read environment variables on #{os[:family]}. "\ + "Tried `env` and `printenv` which returned #{out.exit_status}" + end + + @params = SimpleConfig.new(out.stdout).params end end diff --git a/test/helper.rb b/test/helper.rb index 769a493c8..1999e9cdb 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -108,7 +108,7 @@ class MockLoader 'type win_secpol.cfg' => cmd.call('secedit-export'), 'secedit /export /cfg win_secpol.cfg' => cmd.call('success'), 'del win_secpol.cfg' => cmd.call('success'), - 'su - root -c \'echo $PATH\'' => cmd.call('PATH'), + 'env' => cmd.call('env'), '(Get-Item \'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Schedule\').GetValue(\'Start\')' => cmd.call('reg_schedule'), 'Auditpol /get /subcategory:\'User Account Management\' /r' => cmd.call('auditpol'), '/sbin/auditctl -l' => cmd.call('auditctl'), diff --git a/test/unit/mock/cmd/PATH b/test/unit/mock/cmd/PATH deleted file mode 100644 index 63b737ffa..000000000 --- a/test/unit/mock/cmd/PATH +++ /dev/null @@ -1 +0,0 @@ -/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin diff --git a/test/unit/mock/cmd/env b/test/unit/mock/cmd/env new file mode 100644 index 000000000..6540f0235 --- /dev/null +++ b/test/unit/mock/cmd/env @@ -0,0 +1 @@ +PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin