2015-09-20 16:32:50 +00:00
# encoding: utf-8
2015-10-06 16:55:44 +00:00
# author: Christoph Hartmann
# author: Dominik Richter
2015-09-20 16:32:50 +00:00
# license: All rights reserved
2016-03-08 18:06:55 +00:00
module Inspec::Resources
class KernelModule < Inspec . resource ( 1 )
name 'kernel_module'
desc 'Use the kernel_module InSpec audit resource to test kernel modules on Linux platforms. These parameters are located under /lib/modules. Any submodule may be tested using this resource.'
example "
describe kernel_module ( 'bridge' ) do
it { should be_loaded }
end
"
2015-09-20 16:32:50 +00:00
2016-03-08 18:06:55 +00:00
def initialize ( modulename = nil )
@module = modulename
2015-09-20 16:32:50 +00:00
2016-03-08 18:06:55 +00:00
# this resource is only supported on Linux
return skip_resource 'The `kernel_parameter` resource is not supported on your OS.' if ! inspec . os . linux?
end
2015-09-20 16:32:50 +00:00
2016-03-08 18:06:55 +00:00
def loaded?
2017-02-24 20:30:00 +00:00
if inspec . os . redhat? || inspec . os . name == 'fedora'
2017-02-24 11:32:57 +00:00
lsmod_cmd = '/sbin/lsmod'
else
lsmod_cmd = 'lsmod'
end
2015-10-23 11:57:37 +00:00
2016-03-08 18:06:55 +00:00
# get list of all modules
cmd = inspec . command ( lsmod_cmd )
return false if cmd . exit_status != 0
2015-09-20 16:32:50 +00:00
2016-03-08 18:06:55 +00:00
# check if module is loaded
re = Regexp . new ( '^' + Regexp . quote ( @module ) + '\s' )
found = cmd . stdout . match ( re )
! found . nil?
end
2015-09-20 16:32:50 +00:00
2017-01-25 20:47:39 +00:00
def version
2017-02-24 20:30:00 +00:00
if inspec . os . redhat? || inspec . os . name == 'fedora'
2017-01-27 10:49:21 +00:00
modinfo_cmd = " /sbin/modinfo -F version #{ @module } "
else
modinfo_cmd = " modinfo -F version #{ @module } "
end
2017-01-25 20:47:39 +00:00
cmd = inspec . command ( modinfo_cmd )
2017-01-27 10:49:21 +00:00
cmd . exit_status . zero? ? cmd . stdout . delete ( " \n " ) : nil
2017-01-25 20:47:39 +00:00
end
2016-03-08 18:06:55 +00:00
def to_s
" Kernel Module #{ @module } "
end
2015-09-20 16:32:50 +00:00
end
end