2015-07-27 23:24:46 +02:00
|
|
|
# encoding: utf-8
|
|
|
|
# copyright: 2015, Vulcano Security GmbH
|
|
|
|
# license: All rights reserved
|
|
|
|
|
|
|
|
require 'utils/simpleconfig'
|
2015-08-02 20:11:01 -07:00
|
|
|
require 'utils/find_files'
|
2015-07-27 23:24:46 +02:00
|
|
|
|
2015-08-28 13:02:18 -07:00
|
|
|
class ApacheConf < Vulcano.resource(1)
|
|
|
|
name 'apache_conf'
|
2015-07-27 23:24:46 +02:00
|
|
|
|
2015-09-03 20:43:58 +02:00
|
|
|
def initialize(conf_path)
|
2015-07-27 23:24:46 +02:00
|
|
|
@conf_path = conf_path
|
2015-08-28 13:02:18 -07:00
|
|
|
@conf_dir = File.dirname(@conf_path)
|
2015-07-27 23:24:46 +02:00
|
|
|
@files_contents = {}
|
|
|
|
@content = nil
|
|
|
|
@params = nil
|
|
|
|
read_content
|
|
|
|
end
|
|
|
|
|
|
|
|
def content
|
|
|
|
@content ||= read_content
|
|
|
|
end
|
|
|
|
|
2015-09-03 20:43:58 +02:00
|
|
|
def params(*opts)
|
2015-07-27 23:24:46 +02:00
|
|
|
@params || read_content
|
|
|
|
res = @params
|
|
|
|
opts.each do |opt|
|
|
|
|
res = res[opt] unless res.nil?
|
|
|
|
end
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
2015-09-03 20:43:58 +02:00
|
|
|
def filter_comments(data)
|
2015-09-03 20:35:23 +02:00
|
|
|
content = ''
|
2015-07-27 23:24:46 +02:00
|
|
|
data.each_line do |line|
|
|
|
|
if (!line.match(/^\s*#/)) then
|
2015-08-29 02:10:36 -07:00
|
|
|
content << line
|
2015-07-27 23:24:46 +02:00
|
|
|
end
|
|
|
|
end
|
2015-09-03 20:45:37 +02:00
|
|
|
content
|
2015-07-27 23:24:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def read_content
|
2015-09-03 20:35:23 +02:00
|
|
|
@content = ''
|
2015-07-27 23:24:46 +02:00
|
|
|
@params = {}
|
2015-08-02 20:28:32 -07:00
|
|
|
|
|
|
|
# skip if the main configuration file doesn't exist
|
2015-08-29 19:33:15 -07:00
|
|
|
file = vulcano.file(@conf_path)
|
2015-08-29 17:13:53 -07:00
|
|
|
if !file.file?
|
2015-08-02 20:28:32 -07:00
|
|
|
return skip_resource "Can't find file \"#{@conf_path}\""
|
|
|
|
end
|
2015-08-28 13:02:18 -07:00
|
|
|
|
2015-08-29 02:10:36 -07:00
|
|
|
raw_conf = file.content
|
2015-08-28 13:02:18 -07:00
|
|
|
if raw_conf.empty? && file.size > 0
|
2015-08-02 20:35:18 -07:00
|
|
|
return skip_resource("Can't read file \"#{@conf_path}\"")
|
|
|
|
end
|
2015-08-02 20:28:32 -07:00
|
|
|
|
2015-07-27 23:24:46 +02:00
|
|
|
to_read = [@conf_path]
|
|
|
|
while !to_read.empty?
|
|
|
|
raw_conf = read_file(to_read[0])
|
|
|
|
@content += raw_conf
|
|
|
|
|
|
|
|
# parse include file parameters
|
2015-09-05 16:07:54 +02:00
|
|
|
params = SimpleConfig.new(
|
|
|
|
raw_conf,
|
2015-07-27 23:24:46 +02:00
|
|
|
assignment_re: /^\s*(\S+)\s+(.*)\s*$/,
|
|
|
|
multiple_values: true
|
|
|
|
).params
|
|
|
|
@params.merge!(params)
|
|
|
|
|
|
|
|
to_read = to_read.drop(1)
|
|
|
|
# see if there is more config files to include
|
|
|
|
include_files = params['Include'] || []
|
|
|
|
include_files_optional = params['IncludeOptional'] || []
|
|
|
|
|
2015-09-04 09:59:30 +02:00
|
|
|
required = []
|
2015-07-27 23:24:46 +02:00
|
|
|
include_files.each do |f|
|
|
|
|
id = File.join(@conf_dir, f)
|
2015-08-02 20:17:07 -07:00
|
|
|
required.push(FindFiles.find(id, depth: 1, type: 'file'))
|
2015-07-27 23:24:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
required.flatten!
|
|
|
|
to_read += required.find_all do |fp|
|
|
|
|
not @files_contents.key? fp
|
|
|
|
end
|
|
|
|
|
2015-09-04 09:59:30 +02:00
|
|
|
optional = []
|
2015-07-27 23:24:46 +02:00
|
|
|
include_files_optional.each do |f|
|
|
|
|
id = File.join(@conf_dir, f)
|
2015-08-02 20:17:07 -07:00
|
|
|
optional.push(FindFiles.find(id, depth: 1, type: 'file'))
|
2015-07-27 23:24:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
optional.flatten!
|
|
|
|
to_read += optional.find_all do |fp|
|
|
|
|
not @files_contents.key? fp
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# fiter comments
|
|
|
|
@content = filter_comments @content
|
|
|
|
|
|
|
|
@content
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_file(path)
|
2015-08-29 19:33:15 -07:00
|
|
|
@files_contents[path] ||= vulcano.file(path).content
|
2015-07-27 23:24:46 +02:00
|
|
|
end
|
|
|
|
end
|