2015-07-15 13:15:18 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# copyright: 2015, Vulcano Security GmbH
|
2015-10-06 16:55:44 +00:00
|
|
|
# author: Christoph Hartmann
|
|
|
|
# author: Dominik Richter
|
2015-07-15 13:15:18 +00:00
|
|
|
# license: All rights reserved
|
|
|
|
|
2015-09-05 18:09:55 +00:00
|
|
|
# Usage:
|
|
|
|
#
|
2015-10-30 13:37:00 +00:00
|
|
|
# describe os_env('PATH') do
|
2015-09-05 18:09:55 +00:00
|
|
|
# its(:split) { should_not include('') }
|
|
|
|
# its(:split) { should_not include('.') }
|
|
|
|
# end
|
|
|
|
|
2015-11-13 10:53:21 +00:00
|
|
|
require 'utils/simpleconfig'
|
|
|
|
|
2015-10-26 03:04:18 +00:00
|
|
|
class OsEnv < Inspec.resource(1)
|
2015-08-28 20:02:18 +00:00
|
|
|
name 'os_env'
|
2015-07-14 22:47:04 +00:00
|
|
|
|
2015-08-28 20:02:18 +00:00
|
|
|
attr_reader :content
|
2015-11-13 10:53:21 +00:00
|
|
|
def initialize(env = nil)
|
2015-09-04 07:59:30 +00:00
|
|
|
@osenv = env
|
2015-11-13 10:53:21 +00:00
|
|
|
@content = nil
|
2015-11-19 02:50:38 +00:00
|
|
|
@content = value_for(env) unless env.nil?
|
2015-07-26 10:30:12 +00:00
|
|
|
end
|
2015-07-14 22:47:04 +00:00
|
|
|
|
2015-07-26 10:30:12 +00:00
|
|
|
def split
|
2015-11-19 02:50:38 +00:00
|
|
|
# we can't take advantage of `File::PATH_SEPARATOR` as code is
|
|
|
|
# evaluated on the host machine
|
|
|
|
path_separator = inspec.os.windows? ? ';' : ':'
|
2015-07-26 10:30:12 +00:00
|
|
|
# -1 is required to catch cases like dir1::dir2:
|
|
|
|
# where we have a trailing :
|
2015-11-19 02:50:38 +00:00
|
|
|
@content.nil? ? [] : @content.split(path_separator, -1)
|
2015-07-26 10:30:12 +00:00
|
|
|
end
|
2015-07-14 22:47:04 +00:00
|
|
|
|
2015-07-26 10:30:12 +00:00
|
|
|
def to_s
|
2015-11-13 10:53:21 +00:00
|
|
|
if @osenv.nil?
|
|
|
|
'Environment variables'
|
|
|
|
else
|
|
|
|
"Environment variable #{@osenv}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2015-11-19 02:50:38 +00:00
|
|
|
def value_for(env)
|
|
|
|
command = if inspec.os.windows?
|
|
|
|
"$Env:#{env}"
|
|
|
|
else
|
|
|
|
'env'
|
|
|
|
end
|
|
|
|
|
|
|
|
out = inspec.command(command)
|
|
|
|
|
2015-11-13 10:53:21 +00:00
|
|
|
unless out.exit_status == 0
|
|
|
|
skip_resource "Can't read environment variables on #{os[:family]}. "\
|
2015-11-19 02:50:38 +00:00
|
|
|
"Tried `#{command}` which returned #{out.exit_status}"
|
2015-11-13 10:53:21 +00:00
|
|
|
end
|
|
|
|
|
2015-11-19 02:50:38 +00:00
|
|
|
if inspec.os.windows?
|
|
|
|
out.stdout.strip
|
|
|
|
else
|
|
|
|
params = SimpleConfig.new(out.stdout).params
|
|
|
|
params[env]
|
|
|
|
end
|
2015-07-14 22:47:04 +00:00
|
|
|
end
|
2015-07-26 10:30:12 +00:00
|
|
|
end
|