inspec/lib/resources/postgres_ident_conf.rb

78 lines
1.9 KiB
Ruby
Raw Normal View History

New postgres_ident_conf resource (#1963) * Initial commit of pg_ident_conf resource Signed-off-by: Rony Xavier <rx294@nyu.edu> * Initial commit of pg_ident_conf resource Signed-off-by: Rony Xavier <rx294@nyu.edu> * Small updates to organization of code Signed-off-by: Aaron Lippold <lippold@gmail.com> Signed-off-by: Rony Xaiver <rx294@nyu.edu> * updated `conf_path` instance var to `conf_file` since we are returning a file. Signed-off-by: Aaron Lippold <lippold@gmail.com> * Updated few bugs on pg_ident_conf added test files and docs Signed-off-by: Rony Xavier <rx294@nyu.edu> * Updated docs Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> Signed-off-by: Aaron Lippold <lippold@gmail.com> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> Signed-off-by: Aaron Lippold <lippold@gmail.com> * Added OS check Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock file Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> Signed-off-by: Aaron Lippold <lippold@gmail.com> * added windows mock file Signed-off-by: Aaron Lippold <lippold@gmail.com> * Changed resource name from pg_ident_conf to postgres_ident_conf Signed-off-by: Rony Xavier <rx294@nyu.edu> * Completed corrections reccomended on PR Signed-off-by: Rony Xavier <rx294@nyu.edu> * removed copyright information Signed-off-by: Aaron Lippold <lippold@gmail.com>
2017-07-03 18:01:40 +00:00
# encoding: utf-8
require 'utils/file_reader'
New postgres_ident_conf resource (#1963) * Initial commit of pg_ident_conf resource Signed-off-by: Rony Xavier <rx294@nyu.edu> * Initial commit of pg_ident_conf resource Signed-off-by: Rony Xavier <rx294@nyu.edu> * Small updates to organization of code Signed-off-by: Aaron Lippold <lippold@gmail.com> Signed-off-by: Rony Xaiver <rx294@nyu.edu> * updated `conf_path` instance var to `conf_file` since we are returning a file. Signed-off-by: Aaron Lippold <lippold@gmail.com> * Updated few bugs on pg_ident_conf added test files and docs Signed-off-by: Rony Xavier <rx294@nyu.edu> * Updated docs Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> Signed-off-by: Aaron Lippold <lippold@gmail.com> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> Signed-off-by: Aaron Lippold <lippold@gmail.com> * Added OS check Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock file Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> Signed-off-by: Aaron Lippold <lippold@gmail.com> * added windows mock file Signed-off-by: Aaron Lippold <lippold@gmail.com> * Changed resource name from pg_ident_conf to postgres_ident_conf Signed-off-by: Rony Xavier <rx294@nyu.edu> * Completed corrections reccomended on PR Signed-off-by: Rony Xavier <rx294@nyu.edu> * removed copyright information Signed-off-by: Aaron Lippold <lippold@gmail.com>
2017-07-03 18:01:40 +00:00
require 'resources/postgres'
module Inspec::Resources
class PostgresIdentConf < Inspec.resource(1)
name 'postgres_ident_conf'
supports platform: 'unix'
New postgres_ident_conf resource (#1963) * Initial commit of pg_ident_conf resource Signed-off-by: Rony Xavier <rx294@nyu.edu> * Initial commit of pg_ident_conf resource Signed-off-by: Rony Xavier <rx294@nyu.edu> * Small updates to organization of code Signed-off-by: Aaron Lippold <lippold@gmail.com> Signed-off-by: Rony Xaiver <rx294@nyu.edu> * updated `conf_path` instance var to `conf_file` since we are returning a file. Signed-off-by: Aaron Lippold <lippold@gmail.com> * Updated few bugs on pg_ident_conf added test files and docs Signed-off-by: Rony Xavier <rx294@nyu.edu> * Updated docs Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> Signed-off-by: Aaron Lippold <lippold@gmail.com> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> Signed-off-by: Aaron Lippold <lippold@gmail.com> * Added OS check Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock file Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> Signed-off-by: Aaron Lippold <lippold@gmail.com> * added windows mock file Signed-off-by: Aaron Lippold <lippold@gmail.com> * Changed resource name from pg_ident_conf to postgres_ident_conf Signed-off-by: Rony Xavier <rx294@nyu.edu> * Completed corrections reccomended on PR Signed-off-by: Rony Xavier <rx294@nyu.edu> * removed copyright information Signed-off-by: Aaron Lippold <lippold@gmail.com>
2017-07-03 18:01:40 +00:00
desc 'Use the postgres_ident_conf InSpec audit resource to test the client
authentication data is controlled by a pg_ident.conf file.'
example "
describe postgres_ident_conf.where { pg_username == 'acme_user' } do
its('map_name') { should eq ['ssl-test'] }
end
"
include FileReader
New postgres_ident_conf resource (#1963) * Initial commit of pg_ident_conf resource Signed-off-by: Rony Xavier <rx294@nyu.edu> * Initial commit of pg_ident_conf resource Signed-off-by: Rony Xavier <rx294@nyu.edu> * Small updates to organization of code Signed-off-by: Aaron Lippold <lippold@gmail.com> Signed-off-by: Rony Xaiver <rx294@nyu.edu> * updated `conf_path` instance var to `conf_file` since we are returning a file. Signed-off-by: Aaron Lippold <lippold@gmail.com> * Updated few bugs on pg_ident_conf added test files and docs Signed-off-by: Rony Xavier <rx294@nyu.edu> * Updated docs Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> Signed-off-by: Aaron Lippold <lippold@gmail.com> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> Signed-off-by: Aaron Lippold <lippold@gmail.com> * Added OS check Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock file Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> Signed-off-by: Aaron Lippold <lippold@gmail.com> * added windows mock file Signed-off-by: Aaron Lippold <lippold@gmail.com> * Changed resource name from pg_ident_conf to postgres_ident_conf Signed-off-by: Rony Xavier <rx294@nyu.edu> * Completed corrections reccomended on PR Signed-off-by: Rony Xavier <rx294@nyu.edu> * removed copyright information Signed-off-by: Aaron Lippold <lippold@gmail.com>
2017-07-03 18:01:40 +00:00
attr_reader :params, :conf_file
def initialize(ident_conf_path = nil)
@conf_file = ident_conf_path || File.expand_path('pg_ident.conf', inspec.postgres.conf_dir)
@content = nil
@params = nil
read_content
end
filter = FilterTable.create
filter.register_column(:map_name, field: 'map_name')
.register_column(:system_username, field: 'system_username')
.register_column(:pg_username, field: 'pg_username')
New postgres_ident_conf resource (#1963) * Initial commit of pg_ident_conf resource Signed-off-by: Rony Xavier <rx294@nyu.edu> * Initial commit of pg_ident_conf resource Signed-off-by: Rony Xavier <rx294@nyu.edu> * Small updates to organization of code Signed-off-by: Aaron Lippold <lippold@gmail.com> Signed-off-by: Rony Xaiver <rx294@nyu.edu> * updated `conf_path` instance var to `conf_file` since we are returning a file. Signed-off-by: Aaron Lippold <lippold@gmail.com> * Updated few bugs on pg_ident_conf added test files and docs Signed-off-by: Rony Xavier <rx294@nyu.edu> * Updated docs Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> Signed-off-by: Aaron Lippold <lippold@gmail.com> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> Signed-off-by: Aaron Lippold <lippold@gmail.com> * Added OS check Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock file Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> Signed-off-by: Aaron Lippold <lippold@gmail.com> * added windows mock file Signed-off-by: Aaron Lippold <lippold@gmail.com> * Changed resource name from pg_ident_conf to postgres_ident_conf Signed-off-by: Rony Xavier <rx294@nyu.edu> * Completed corrections reccomended on PR Signed-off-by: Rony Xavier <rx294@nyu.edu> * removed copyright information Signed-off-by: Aaron Lippold <lippold@gmail.com>
2017-07-03 18:01:40 +00:00
filter.install_filter_methods_on_resource(self, :params)
New postgres_ident_conf resource (#1963) * Initial commit of pg_ident_conf resource Signed-off-by: Rony Xavier <rx294@nyu.edu> * Initial commit of pg_ident_conf resource Signed-off-by: Rony Xavier <rx294@nyu.edu> * Small updates to organization of code Signed-off-by: Aaron Lippold <lippold@gmail.com> Signed-off-by: Rony Xaiver <rx294@nyu.edu> * updated `conf_path` instance var to `conf_file` since we are returning a file. Signed-off-by: Aaron Lippold <lippold@gmail.com> * Updated few bugs on pg_ident_conf added test files and docs Signed-off-by: Rony Xavier <rx294@nyu.edu> * Updated docs Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> Signed-off-by: Aaron Lippold <lippold@gmail.com> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> Signed-off-by: Aaron Lippold <lippold@gmail.com> * Added OS check Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock file Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> Signed-off-by: Aaron Lippold <lippold@gmail.com> * added windows mock file Signed-off-by: Aaron Lippold <lippold@gmail.com> * Changed resource name from pg_ident_conf to postgres_ident_conf Signed-off-by: Rony Xavier <rx294@nyu.edu> * Completed corrections reccomended on PR Signed-off-by: Rony Xavier <rx294@nyu.edu> * removed copyright information Signed-off-by: Aaron Lippold <lippold@gmail.com>
2017-07-03 18:01:40 +00:00
def to_s
"PostgreSQL Ident Config #{@conf_file}"
end
private
def filter_comments(data)
content = []
data.each do |line|
line.chomp!
content << line unless line.match(/^\s*#/) || line.empty?
end
content
end
def read_content
@content = ''
@params = {}
@content = filter_comments(read_file(@conf_file))
@params = parse_conf(@content)
end
def parse_conf(content)
content.map do |line|
parse_line(line)
end.compact
end
def parse_line(line)
x = line.split(/\s+/)
{
'map_name' => x[0],
'system_username' => x[1],
'pg_username' => x[2],
}
end
def read_file(conf_file = @conf_file)
read_file_content(conf_file, allow_empty: true).lines
New postgres_ident_conf resource (#1963) * Initial commit of pg_ident_conf resource Signed-off-by: Rony Xavier <rx294@nyu.edu> * Initial commit of pg_ident_conf resource Signed-off-by: Rony Xavier <rx294@nyu.edu> * Small updates to organization of code Signed-off-by: Aaron Lippold <lippold@gmail.com> Signed-off-by: Rony Xaiver <rx294@nyu.edu> * updated `conf_path` instance var to `conf_file` since we are returning a file. Signed-off-by: Aaron Lippold <lippold@gmail.com> * Updated few bugs on pg_ident_conf added test files and docs Signed-off-by: Rony Xavier <rx294@nyu.edu> * Updated docs Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> Signed-off-by: Aaron Lippold <lippold@gmail.com> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> Signed-off-by: Aaron Lippold <lippold@gmail.com> * Added OS check Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock file Signed-off-by: Rony Xavier <rx294@nyu.edu> * Added mock folders Signed-off-by: Rony Xavier <rx294@nyu.edu> Signed-off-by: Aaron Lippold <lippold@gmail.com> * added windows mock file Signed-off-by: Aaron Lippold <lippold@gmail.com> * Changed resource name from pg_ident_conf to postgres_ident_conf Signed-off-by: Rony Xavier <rx294@nyu.edu> * Completed corrections reccomended on PR Signed-off-by: Rony Xavier <rx294@nyu.edu> * removed copyright information Signed-off-by: Aaron Lippold <lippold@gmail.com>
2017-07-03 18:01:40 +00:00
end
end
end