2017-07-03 18:13:51 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
require 'resources/postgres'
|
2018-03-22 12:25:45 +00:00
|
|
|
require 'utils/file_reader'
|
2017-07-03 18:13:51 +00:00
|
|
|
|
|
|
|
module Inspec::Resources
|
|
|
|
class PostgresHbaConf < Inspec.resource(1)
|
|
|
|
name 'postgres_hba_conf'
|
2018-02-19 14:26:49 +00:00
|
|
|
supports platform: 'unix'
|
2017-07-03 18:13:51 +00:00
|
|
|
desc 'Use the `postgres_hba_conf` InSpec audit resource to test the client
|
|
|
|
authentication data defined in the pg_hba.conf file.'
|
|
|
|
example "
|
|
|
|
describe postgres_hba_conf.where { type == 'local' } do
|
|
|
|
its('auth_method') { should eq ['peer'] }
|
|
|
|
end
|
|
|
|
"
|
|
|
|
|
2018-03-22 12:25:45 +00:00
|
|
|
include FileReader
|
|
|
|
|
2017-07-03 18:13:51 +00:00
|
|
|
attr_reader :conf_file, :params
|
|
|
|
|
|
|
|
# @todo add checks to ensure that we have data in our file
|
|
|
|
def initialize(hba_conf_path = nil)
|
|
|
|
@conf_file = hba_conf_path || File.expand_path('pg_hba.conf', inspec.postgres.conf_dir)
|
|
|
|
@content = ''
|
|
|
|
@params = {}
|
|
|
|
read_content
|
|
|
|
end
|
|
|
|
|
|
|
|
filter = FilterTable.create
|
2018-06-26 19:14:21 +00:00
|
|
|
filter.register_column(:type, field: 'type')
|
|
|
|
.register_column(:database, field: 'database')
|
|
|
|
.register_column(:user, field: 'user')
|
|
|
|
.register_column(:address, field: 'address')
|
|
|
|
.register_column(:auth_method, field: 'auth_method')
|
|
|
|
.register_column(:auth_params, field: 'auth_params')
|
|
|
|
|
|
|
|
filter.install_filter_methods_on_resource(self, :params)
|
2017-07-03 18:13:51 +00:00
|
|
|
|
|
|
|
def to_s
|
|
|
|
"Postgres Hba Config #{@conf_file}"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def clean_conf_file(conf_file = @conf_file)
|
2018-03-22 12:25:45 +00:00
|
|
|
data = read_file_content(conf_file).to_s.lines
|
2017-07-03 18:13:51 +00:00
|
|
|
content = []
|
|
|
|
data.each do |line|
|
|
|
|
line.chomp!
|
|
|
|
content << line unless line.match(/^\s*#/) || line.empty?
|
|
|
|
end
|
|
|
|
content
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_content(config_file = @conf_file)
|
|
|
|
# @todo use SimpleConfig here if we can
|
|
|
|
# ^\s*(\S+)\s+(\S+)\s+(\S+)\s(?:(\d*.\d*.\d*.\d*\/\d*)|(::\/\d+))\s+(\S+)\s*(.*)?\s*$
|
|
|
|
|
2018-03-22 12:25:45 +00:00
|
|
|
@content = clean_conf_file(config_file)
|
2017-07-03 18:13:51 +00:00
|
|
|
@params = parse_conf(@content)
|
|
|
|
@params.each do |line|
|
|
|
|
if line['type'] == 'local'
|
|
|
|
line['auth_method'] = line['address']
|
|
|
|
line['address'] = ''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_conf(content)
|
|
|
|
content.map do |line|
|
|
|
|
parse_line(line)
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_line(line)
|
|
|
|
x = line.split(/\s+/)
|
|
|
|
{
|
|
|
|
'type' => x[0],
|
|
|
|
'database' => x[1],
|
|
|
|
'user' => x[2],
|
|
|
|
'address' => x[3],
|
|
|
|
'auth_method' => x[4],
|
|
|
|
'auth_params' => ('' if x.length == 4) || x[5..-1].join(' '),
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|