2015-07-15 14:33:39 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# copyright: 2015, Vulcano Security GmbH
|
|
|
|
# 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-07-15 14:33:39 +00:00
|
|
|
class PConfig < Vulcano::Resource
|
|
|
|
|
2015-07-26 20:45:18 +00:00
|
|
|
def initialize ( content=nil, useropts = {} )
|
|
|
|
|
2015-08-01 07:23:02 +00:00
|
|
|
default_options = {}
|
2015-07-26 20:45:18 +00:00
|
|
|
@opts = default_options.merge(useropts)
|
2015-07-15 14:33:39 +00:00
|
|
|
@runner = Specinfra::Runner
|
|
|
|
@content = content
|
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
|
|
|
|
|
|
|
if @content != nil then
|
|
|
|
read_content
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
"parse_config #{@conf_path}"
|
2015-07-15 14:33:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing name
|
|
|
|
@params || read_content
|
|
|
|
@params[name.to_s]
|
|
|
|
end
|
|
|
|
|
2015-07-26 20:45:18 +00:00
|
|
|
def parse_file conf_path
|
|
|
|
@conf_path = conf_path
|
|
|
|
|
|
|
|
# read the file
|
|
|
|
if !@runner.check_file_is_file(conf_path)
|
|
|
|
return skip_resource "Can't find file \"#{conf_path}\""
|
|
|
|
end
|
|
|
|
@content = read_file(conf_path)
|
|
|
|
if @content.empty? && @runner.get_file_size(conf_path).stdout.strip.to_i > 0
|
|
|
|
return skip_resource "Can't read file \"#{conf_path}\""
|
|
|
|
end
|
|
|
|
|
|
|
|
read_content
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_file(path)
|
|
|
|
@files_contents[path] ||= @runner.get_file_content(path).stdout
|
|
|
|
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-08-03 00:25:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
module Serverspec::Type
|
|
|
|
def parse_config(content, opts={})
|
|
|
|
PConfig.new(content, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_config_file(file, opts={})
|
|
|
|
p = PConfig.new(nil, opts)
|
|
|
|
p.parse_file(file)
|
|
|
|
p
|
|
|
|
end
|
|
|
|
end
|