2015-09-20 16:33:43 +00:00
|
|
|
# encoding: utf-8
|
2015-10-06 16:55:44 +00:00
|
|
|
# author: Christoph Hartmann
|
2015-09-20 16:33:43 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
module Inspec::Resources
|
|
|
|
class KernelParameter < Inspec.resource(1)
|
|
|
|
name 'kernel_parameter'
|
|
|
|
desc 'Use the kernel_parameter InSpec audit resource to test kernel parameters on Linux platforms.'
|
|
|
|
example "
|
|
|
|
describe kernel_parameter('net.ipv4.conf.all.forwarding') do
|
2016-05-03 22:14:33 +00:00
|
|
|
its('value') { should eq 0 }
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
|
|
|
"
|
|
|
|
|
|
|
|
def initialize(parameter = nil)
|
|
|
|
@parameter = parameter
|
|
|
|
|
|
|
|
# this resource is only supported on Linux
|
|
|
|
return skip_resource 'The `kernel_parameter` resource is not supported on your OS.' if !inspec.os.linux?
|
2015-11-27 13:02:38 +00:00
|
|
|
end
|
2015-09-20 16:33:43 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def value
|
|
|
|
cmd = inspec.command("/sbin/sysctl -q -n #{@parameter}")
|
|
|
|
return nil if cmd.exit_status != 0
|
|
|
|
# remove whitespace
|
|
|
|
cmd = cmd.stdout.chomp.strip
|
|
|
|
# convert to number if possible
|
|
|
|
cmd = cmd.to_i if cmd =~ /^\d+$/
|
|
|
|
cmd
|
|
|
|
end
|
2015-09-20 16:33:43 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def to_s
|
|
|
|
"Kernel Parameter #{@parameter}"
|
|
|
|
end
|
2015-09-20 16:33:43 +00:00
|
|
|
end
|
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
# for compatability with serverspec
|
|
|
|
# this is deprecated syntax and will be removed in future versions
|
|
|
|
class LinuxKernelParameter < KernelParameter
|
|
|
|
name 'linux_kernel_parameter'
|
2015-09-20 16:33:43 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def initialize(parameter)
|
|
|
|
super(parameter)
|
|
|
|
end
|
2015-09-20 16:33:43 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def value
|
|
|
|
deprecated
|
|
|
|
super()
|
|
|
|
end
|
2015-09-20 16:33:43 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def deprecated
|
|
|
|
warn '[DEPRECATION] `linux_kernel_parameter(parameter)` is deprecated. Please use `kernel_parameter(parameter)` instead.'
|
|
|
|
end
|
2015-09-20 16:33:43 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def to_s
|
|
|
|
"Kernel Parameter #{@parameter}"
|
|
|
|
end
|
2015-09-20 16:33:43 +00:00
|
|
|
end
|
|
|
|
end
|