2015-07-15 16:33:39 +02:00
# encoding: utf-8
# copyright: 2015, Vulcano Security GmbH
2015-10-06 18:55:44 +02:00
# author: Dominik Richter
# author: Christoph Hartmann
2015-07-15 16:33:39 +02:00
2015-07-26 22:45:18 +02:00
# Usage example:
#
# audit = command('/sbin/auditctl -l').stdout
# options = {
2017-04-26 23:18:14 +02:00
# assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
2015-07-26 22:45:18 +02:00
# multiple_values: true
# }
# describe parse_config(audit, options ) do
2016-03-08 13:06:55 -05:00
module Inspec::Resources
class PConfig < Inspec . resource ( 1 )
name 'parse_config'
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
2016-09-25 14:02:40 +02:00
output2 = command ( 'curl http://127.0.0.1/php_status' ) . stdout
# php status is in format 'key : value', and we do not allow for multiple values
options2 = {
2017-04-26 23:18:14 +02:00
assignment_regex : / ^ \ s*([^:]*?) \ s*: \ s*(.*?) \ s*$ / ,
2016-09-25 14:02:40 +02:00
multiple_values : false
}
describe parse_config ( output2 , options2 ) do
its ( 'pool' ) { should eq 'www' }
its ( 'process manager' ) { should eq process_manager }
end
# getting specific key from the output above, convert it to integer and then compare
# make sure 'listen queue' is below 100
describe parse_config ( output2 , options2 ) . params [ 'listen queue' ] . to_i do
it { should be < 100 }
end
2016-03-08 13:06:55 -05:00
"
2015-07-15 16:33:39 +02:00
2016-08-05 12:07:14 +02:00
attr_reader :content
2016-03-08 13:06:55 -05:00
def initialize ( content = nil , useropts = nil )
@opts = { }
@opts = useropts . dup unless useropts . nil?
@files_contents = { }
2015-07-26 22:45:18 +02:00
2016-03-08 13:06:55 -05:00
@content = content
2016-08-05 12:07:14 +02:00
read_params unless @content . nil?
2016-03-08 13:06:55 -05:00
end
2015-07-26 22:45:18 +02:00
2016-03-08 13:06:55 -05:00
def method_missing ( name )
2016-08-05 12:07:14 +02:00
read_params [ name . to_s ]
2016-03-08 13:06:55 -05:00
end
2015-07-15 16:33:39 +02:00
2016-08-05 12:07:14 +02:00
def params ( * opts )
opts . inject ( read_params ) do | res , nxt |
res . respond_to? ( :key ) ? res [ nxt ] : nil
end
end
def to_s
" Parse Config #{ @conf_path } "
end
private
2016-03-08 13:06:55 -05:00
def parse_file ( conf_path )
@conf_path = conf_path
2015-07-26 22:45:18 +02:00
2016-03-08 13:06:55 -05:00
# read the file
if ! inspec . file ( conf_path ) . file?
return skip_resource " Can't find file \" #{ conf_path } \" "
end
2016-03-22 18:42:50 +01:00
@content = read_file ( conf_path ) . to_s
2017-02-08 16:49:16 -06:00
if @content . empty? && ! inspec . file ( conf_path ) . empty?
2016-03-08 13:06:55 -05:00
return skip_resource " Can't read file \" #{ conf_path } \" "
end
2016-08-05 12:07:14 +02:00
read_params
2015-07-26 22:45:18 +02:00
end
2016-03-08 13:06:55 -05:00
def read_file ( path )
@files_contents [ path ] || = inspec . file ( path ) . content
end
2015-07-26 22:45:18 +02:00
2016-08-05 12:07:14 +02:00
def read_params
2016-08-05 12:29:34 +02:00
@params || = if content . nil?
{ }
else
SimpleConfig . new ( content , @opts ) . params
end
2016-03-08 13:06:55 -05:00
end
2015-07-15 16:33:39 +02:00
end
2015-10-12 13:01:58 +02:00
2016-03-08 13:06:55 -05:00
class PConfigFile < PConfig
name 'parse_config_file'
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-02 17:25:27 -07:00
2016-03-08 13:06:55 -05:00
def initialize ( path , opts = nil )
super ( nil , opts )
parse_file ( path )
2015-11-27 14:02:38 +01:00
end
2015-10-12 13:01:58 +02:00
2016-03-08 13:06:55 -05:00
def to_s
" Parse Config File #{ @conf_path } "
end
2015-10-12 13:01:58 +02:00
end
2015-08-02 17:25:27 -07:00
end