2015-04-09 22:01:23 +02:00
# encoding: utf-8
2015-07-15 15:15:18 +02:00
# copyright: 2015, Vulcano Security GmbH
2015-10-06 18:55:44 +02:00
# author: Dominik Richter
# author: Christoph Hartmann
2017-06-23 11:31:27 -04:00
# author: Aaron Lippold
2015-04-09 22:01:23 +02:00
require 'utils/simpleconfig'
2015-08-02 20:17:07 -07:00
require 'utils/find_files'
2015-08-01 09:21:32 +02:00
require 'resources/postgres'
2015-04-09 22:01:23 +02:00
2016-03-08 13:06:55 -05: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 22:01:23 +02:00
2016-03-08 13:06:55 -05:00
include FindFiles
2017-06-23 11:31:27 -04:00
include ObjectTraverser
2015-09-10 11:33:41 +02:00
2016-03-08 13:06:55 -05:00
def initialize ( conf_path = nil )
@conf_path = conf_path || inspec . postgres . conf_path
2017-06-26 06:16:09 -07:00
if @conf_path . nil?
return skip_resource 'PostgreSQL conf path is not set'
end
2016-03-09 10:39:07 +01:00
@conf_dir = File . expand_path ( File . dirname ( @conf_path ) )
2016-03-08 13:06:55 -05:00
@files_contents = { }
@content = nil
@params = nil
read_content
end
2015-04-09 22:01:23 +02:00
2016-03-08 13:06:55 -05:00
def content
@content || = read_content
end
2015-04-09 22:01:23 +02:00
2016-03-08 13:06:55 -05:00
def params ( * opts )
@params || read_content
res = @params
opts . each do | opt |
res = res [ opt ] unless res . nil?
end
res
2015-04-09 22:01:23 +02:00
end
2017-06-23 11:31:27 -04:00
def value ( key )
extract_value ( key , @params )
end
def method_missing ( * keys )
keys . shift if keys . is_a? ( Array ) && keys [ 0 ] == :[]
param = value ( keys )
2016-07-12 23:30:54 +02:00
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 13:06:55 -05:00
def read_content
@content = ''
@params = { }
2015-08-02 20:27:34 -07:00
2016-03-08 13:06:55 -05: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 16:49:16 -06:00
if raw_conf . empty? && ! inspec . file ( @conf_path ) . empty?
2016-03-08 13:06:55 -05:00
return skip_resource ( " Can't read file \" #{ @conf_path } \" " )
end
2015-08-02 20:27:34 -07:00
2016-03-08 13:06:55 -05:00
to_read = [ @conf_path ]
until to_read . empty?
2017-05-26 14:45:34 -04:00
base_dir = File . dirname ( to_read [ 0 ] )
2016-03-08 13:06:55 -05:00
raw_conf = read_file ( to_read [ 0 ] )
@content += raw_conf
2015-04-09 22:01:23 +02:00
2017-01-03 18:51:39 +01:00
opts = {
2017-04-26 23:18:14 +02:00
assignment_regex : / ^ \ s*([^=]*?) \ s*= \ s*[']? \ s*(.*?) \ s*[']? \ s*$ / ,
2017-01-03 18:51:39 +01:00
}
params = SimpleConfig . new ( raw_conf , opts ) . params
2016-03-08 13:06:55 -05:00
@params . merge! ( params )
2015-04-09 22:01:23 +02:00
2016-03-08 13:06:55 -05:00
to_read = to_read . drop ( 1 )
# see if there is more config files to include
2015-09-09 19:15:51 +02:00
2017-05-26 14:45:34 -04:00
to_read += include_files ( params , base_dir ) . find_all do | fp |
2016-03-08 13:06:55 -05:00
not @files_contents . key? fp
end
2015-04-09 22:01:23 +02:00
end
2016-03-08 13:06:55 -05:00
@content
2015-04-09 22:01:23 +02:00
end
2017-05-26 14:45:34 -04:00
def include_files ( params , base_dir )
include_files = Array ( params [ 'include' ] ) || [ ]
include_files += Array ( params [ 'include_if_exists' ] ) || [ ]
include_files . map! do | f |
Pathname . new ( f ) . absolute? ? f : File . join ( base_dir , f )
end
2017-04-27 10:17:17 -05:00
dirs = Array ( params [ 'include_dir' ] ) || [ ]
2016-03-08 13:06:55 -05:00
dirs . each do | dir |
2017-05-26 14:45:34 -04:00
dir = File . join ( base_dir , dir ) if dir [ 0 ] != '/'
2016-03-08 13:06:55 -05:00
include_files += find_files ( dir , depth : 1 , type : 'file' )
end
include_files
2015-09-09 19:15:51 +02:00
end
2016-03-08 13:06:55 -05:00
def read_file ( path )
@files_contents [ path ] || = inspec . file ( path ) . content
end
2015-10-12 13:01:58 +02:00
end
2015-04-09 22:01:23 +02:00
end