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-10-06 16:55:44 +00:00
# author: Dominik Richter
# author: Christoph Hartmann
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
2016-03-08 18:06:55 +00:00
module Inspec::Resources
class PostgresConf < Inspec . resource ( 1 )
name 'postgres_conf'
desc 'Use the postgres_conf InSpec audit resource to test the contents of the configuration file for PostgreSQL, typically located at /etc/postgresql/<version>/main/postgresql.conf or /var/lib/postgres/data/postgresql.conf, depending on the platform.'
example "
describe postgres_conf do
its ( 'max_connections' ) { should eq '5' }
end
"
2015-04-09 20:01:23 +00:00
2016-03-08 18:06:55 +00:00
include FindFiles
2015-09-10 09:33:41 +00:00
2016-03-08 18:06:55 +00:00
def initialize ( conf_path = nil )
@conf_path = conf_path || inspec . postgres . conf_path
2016-03-09 09:39:07 +00:00
@conf_dir = File . expand_path ( File . dirname ( @conf_path ) )
2016-03-08 18:06:55 +00:00
@files_contents = { }
@content = nil
@params = nil
read_content
end
2015-04-09 20:01:23 +00:00
2016-03-08 18:06:55 +00:00
def content
@content || = read_content
end
2015-04-09 20:01:23 +00:00
2016-03-08 18:06:55 +00:00
def params ( * opts )
@params || read_content
res = @params
opts . each do | opt |
res = res [ opt ] unless res . nil?
end
res
2015-04-09 20:01:23 +00:00
end
2016-07-12 21:30:54 +00:00
def method_missing ( name )
param = params [ name . to_s ]
return nil if param . nil?
# extract first value if we have only one value in array
return param [ 0 ] if param . length == 1
param
end
def to_s
'PostgreSQL Configuration'
end
private
2016-03-08 18:06:55 +00:00
def read_content
@content = ''
@params = { }
2015-08-03 03:27:34 +00:00
2016-03-08 18:06:55 +00:00
# skip if the main configuration file doesn't exist
if ! inspec . file ( @conf_path ) . file?
return skip_resource " Can't find file \" #{ @conf_path } \" "
end
raw_conf = read_file ( @conf_path )
2017-02-08 22:49:16 +00:00
if raw_conf . empty? && ! inspec . file ( @conf_path ) . empty?
2016-03-08 18:06:55 +00:00
return skip_resource ( " Can't read file \" #{ @conf_path } \" " )
end
2015-08-03 03:27:34 +00:00
2016-03-08 18:06:55 +00:00
to_read = [ @conf_path ]
until to_read . empty?
raw_conf = read_file ( to_read [ 0 ] )
@content += raw_conf
2015-04-09 20:01:23 +00:00
2017-01-03 17:51:39 +00:00
opts = {
2017-04-26 21:18:14 +00:00
assignment_regex : / ^ \ s*([^=]*?) \ s*= \ s*[']? \ s*(.*?) \ s*[']? \ s*$ / ,
2017-01-03 17:51:39 +00:00
}
params = SimpleConfig . new ( raw_conf , opts ) . params
2016-03-08 18:06:55 +00:00
@params . merge! ( params )
2015-04-09 20:01:23 +00:00
2016-03-08 18:06:55 +00:00
to_read = to_read . drop ( 1 )
# see if there is more config files to include
2015-09-09 17:15:51 +00:00
2016-03-08 18:06:55 +00:00
to_read += include_files ( params ) . find_all do | fp |
not @files_contents . key? fp
end
2015-04-09 20:01:23 +00:00
end
2016-03-08 18:06:55 +00:00
@content
2015-04-09 20:01:23 +00:00
end
2016-03-08 18:06:55 +00:00
def include_files ( params )
include_files = params [ 'include' ] || [ ]
include_files += params [ 'include_if_exists' ] || [ ]
dirs = params [ 'include_dir' ] || [ ]
dirs . each do | dir |
dir = File . join ( @conf_dir , dir ) if dir [ 0 ] != '/'
include_files += find_files ( dir , depth : 1 , type : 'file' )
end
include_files
2015-09-09 17:15:51 +00:00
end
2016-03-08 18:06:55 +00:00
def read_file ( path )
@files_contents [ path ] || = inspec . file ( path ) . content
end
2015-10-12 11:01:58 +00:00
end
2015-04-09 20:01:23 +00:00
end