inspec/lib/resources/os_env.rb

75 lines
1.8 KiB
Ruby
Raw Normal View History

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'
module Inspec::Resources
class OsEnv < Inspec.resource(1)
name 'os_env'
desc 'Use the os_env InSpec audit resource to test the environment variables for the platform on which the system is running.'
example "
describe os_env('VARIABLE') do
its('matcher') { should eq 1 }
end
"
2015-07-14 22:47:04 +00:00
attr_reader :content
def initialize(env = nil)
@osenv = env
@content = nil
@content = value_for(env) unless env.nil?
end
2015-07-14 22:47:04 +00:00
def split
# we can't take advantage of `File::PATH_SEPARATOR` as code is
# evaluated on the host machine
path_separator = inspec.os.windows? ? ';' : ':'
# -1 is required to catch cases like dir1::dir2:
# where we have a trailing :
@content.nil? ? [] : @content.split(path_separator, -1)
end
2015-07-14 22:47:04 +00:00
def to_s
if @osenv.nil?
'Environment variables'
else
"Environment variable #{@osenv}"
end
2015-11-13 10:53:21 +00:00
end
private
2015-11-13 10:53:21 +00:00
def value_for(env)
command = if inspec.os.windows?
"$Env:#{env}"
else
'env'
end
out = inspec.command(command)
unless out.exit_status == 0
skip_resource "Can't read environment variables on #{os[:family]}. "\
"Tried `#{command}` which returned #{out.exit_status}"
end
2015-11-13 10:53:21 +00:00
if inspec.os.windows?
out.stdout.strip
else
params = SimpleConfig.new(out.stdout).params
params[env]
end
end
2015-07-14 22:47:04 +00:00
end
2015-07-26 10:30:12 +00:00
end