2015-04-09 20:01:23 +00:00
|
|
|
# encoding: utf-8
|
2015-07-15 13:15:18 +00:00
|
|
|
# copyright: 2015, Vulcano Security GmbH
|
2015-04-09 20:01:23 +00:00
|
|
|
# license: All rights reserved
|
|
|
|
|
|
|
|
require 'utils/simpleconfig'
|
2015-08-03 03:17:07 +00:00
|
|
|
require 'utils/find_files'
|
2015-08-01 07:21:32 +00:00
|
|
|
require 'resources/postgres'
|
2015-04-09 20:01:23 +00:00
|
|
|
|
2015-08-28 19:37:03 +00:00
|
|
|
class PostgresConf < Vulcano.resource(1)
|
|
|
|
name 'postgres_conf'
|
2015-04-09 20:01:23 +00:00
|
|
|
|
|
|
|
def initialize( conf_path )
|
|
|
|
@conf_path = conf_path
|
|
|
|
@conf_dir = File.expand_path(File.dirname @conf_path)
|
|
|
|
@files_contents = {}
|
|
|
|
@content = nil
|
|
|
|
@params = nil
|
|
|
|
read_content
|
|
|
|
end
|
|
|
|
|
|
|
|
def content
|
|
|
|
@content ||= read_content
|
|
|
|
end
|
|
|
|
|
|
|
|
def params *opts
|
|
|
|
@params || read_content
|
|
|
|
res = @params
|
|
|
|
opts.each do |opt|
|
|
|
|
res = res[opt] unless res.nil?
|
|
|
|
end
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_content
|
|
|
|
@content = ""
|
|
|
|
@params = {}
|
2015-08-03 03:27:34 +00:00
|
|
|
|
|
|
|
# skip if the main configuration file doesn't exist
|
2015-08-30 02:33:15 +00:00
|
|
|
if !vulcano.file(@conf_path).file?
|
2015-08-03 03:27:34 +00:00
|
|
|
return skip_resource "Can't find file \"#{@conf_path}\""
|
|
|
|
end
|
2015-08-03 03:35:18 +00:00
|
|
|
raw_conf = read_file(@conf_path)
|
2015-08-30 02:33:15 +00:00
|
|
|
if raw_conf.empty? && vulcano.file(@conf_path).size > 0
|
2015-08-03 03:35:18 +00:00
|
|
|
return skip_resource("Can't read file \"#{@conf_path}\"")
|
|
|
|
end
|
2015-08-03 03:27:34 +00:00
|
|
|
|
2015-04-09 20:01:23 +00:00
|
|
|
to_read = [@conf_path]
|
|
|
|
while !to_read.empty?
|
|
|
|
raw_conf = read_file(to_read[0])
|
|
|
|
@content += raw_conf
|
|
|
|
|
|
|
|
params = SimpleConfig.new(raw_conf).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 += params['include_if_exists'] || []
|
2015-08-03 03:17:07 +00:00
|
|
|
dirs = params['include_dir'] || []
|
|
|
|
dirs.each do |dir|
|
|
|
|
dir = File.join(@conf_dir, dir) if dir[0] != '/'
|
|
|
|
include_files += FindFiles.find(dir, depth: 1, type: 'file')
|
2015-04-09 20:01:23 +00:00
|
|
|
end
|
|
|
|
to_read += include_files.find_all do |fp|
|
|
|
|
not @files_contents.key? fp
|
|
|
|
end
|
|
|
|
end
|
|
|
|
#
|
|
|
|
@content
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_file(path)
|
2015-08-30 02:33:15 +00:00
|
|
|
@files_contents[path] ||= vulcano.file(path).content
|
2015-04-09 20:01:23 +00:00
|
|
|
end
|
|
|
|
end
|