2015-07-15 14:33:39 +00:00
# encoding: utf-8
# copyright: 2015, Vulcano Security GmbH
2015-10-06 16:55:44 +00:00
# author: Dominik Richter
# author: Christoph Hartmann
2015-07-15 14:33:39 +00:00
# license: All rights reserved
2015-07-26 20:45:18 +00:00
# Usage example:
#
# audit = command('/sbin/auditctl -l').stdout
# options = {
# assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
# multiple_values: true
# }
# describe parse_config(audit, options ) do
2015-10-26 03:04:18 +00:00
class PConfig < Inspec . resource ( 1 )
2015-08-28 19:32:17 +00:00
name 'parse_config'
2015-11-27 13:02:38 +00:00
desc 'Use the parse_config InSpec audit resource to test arbitrary configuration files.'
example "
output = command ( 'some-command' ) . stdout
describe parse_config ( output , { data_config_option : value } ) do
its ( 'setting' ) { should eq 1 }
end
"
2015-07-15 14:33:39 +00:00
2015-10-26 15:13:48 +00:00
def initialize ( content = nil , useropts = nil )
@opts = { }
@opts = useropts . dup unless useropts . nil?
2015-07-26 20:45:18 +00:00
@files_contents = { }
2015-07-15 14:33:39 +00:00
@params = nil
2015-07-26 20:45:18 +00:00
2015-09-09 16:52:27 +00:00
@content = content
read_content if @content . nil?
2015-07-26 20:45:18 +00:00
end
2015-09-03 18:43:58 +00:00
def method_missing ( name )
2015-07-15 14:33:39 +00:00
@params || read_content
@params [ name . to_s ]
end
2015-09-03 18:43:58 +00:00
def parse_file ( conf_path )
2015-07-26 20:45:18 +00:00
@conf_path = conf_path
# read the file
2015-10-26 03:04:18 +00:00
if ! inspec . file ( conf_path ) . file?
2015-07-26 20:45:18 +00:00
return skip_resource " Can't find file \" #{ conf_path } \" "
end
@content = read_file ( conf_path )
2015-10-26 03:04:18 +00:00
if @content . empty? && inspec . file ( conf_path ) . size > 0
2015-07-26 20:45:18 +00:00
return skip_resource " Can't read file \" #{ conf_path } \" "
end
read_content
end
def read_file ( path )
2015-10-26 03:04:18 +00:00
@files_contents [ path ] || = inspec . file ( path ) . content
2015-07-26 20:45:18 +00:00
end
2015-07-15 14:33:39 +00:00
def read_content
# parse the file
2015-07-26 20:45:18 +00:00
@params = SimpleConfig . new ( @content , @opts ) . params
2015-07-15 14:33:39 +00:00
@content
end
2015-10-12 11:01:58 +00:00
def to_s
" Parse Config #{ @conf_path } "
end
2015-08-03 00:25:27 +00:00
end
2015-08-28 19:32:17 +00:00
class PConfigFile < PConfig
name 'parse_config_file'
2015-11-27 13:02:38 +00:00
desc 'Use the parse_config_file InSpec audit resource to test arbitrary configuration files. It works identiacal to parse_config. Instead of using a command output, this resource works with files.'
example "
describe parse_config_file ( '/path/to/file' ) do
its ( 'setting' ) { should eq 1 }
end
"
2015-08-03 00:25:27 +00:00
2015-10-26 15:13:48 +00:00
def initialize ( path , opts = nil )
2015-08-28 19:32:17 +00:00
super ( nil , opts )
parse_file ( path )
2015-08-03 00:25:27 +00:00
end
2015-10-12 11:01:58 +00:00
def to_s
" Parse Config File #{ @conf_path } "
end
2015-08-03 00:25:27 +00:00
end