mirror of
https://github.com/inspec/inspec
synced 2025-02-17 06:28:40 +00:00
migrated all postgres resources
Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
This commit is contained in:
parent
83d846ac7f
commit
1a45f32f0b
4 changed files with 27 additions and 49 deletions
|
@ -2,14 +2,16 @@
|
|||
# copyright: 2015, Vulcano Security GmbH
|
||||
# license: All rights reserved
|
||||
|
||||
class Postgres
|
||||
class Postgres < Vulcano.resource(1)
|
||||
name 'postgres'
|
||||
|
||||
attr_reader :service, :data_dir, :conf_dir, :conf_path
|
||||
def initialize
|
||||
case os[:family]
|
||||
when 'ubuntu', 'debian'
|
||||
@service = 'postgresql'
|
||||
@data_dir = '/var/lib/postgresql'
|
||||
@version = command('ls /etc/postgresql/').stdout.chomp
|
||||
@version = @vulcano.run_command('ls /etc/postgresql/').stdout.chomp
|
||||
@conf_dir = "/etc/postgresql/#{@version}/main"
|
||||
@conf_path = File.join @conf_dir, 'postgresql.conf'
|
||||
|
||||
|
@ -27,9 +29,3 @@ class Postgres
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
module Serverspec::Type
|
||||
def postgres
|
||||
@postgres ||= Postgres.new()
|
||||
end
|
||||
end
|
|
@ -6,10 +6,10 @@ require 'utils/simpleconfig'
|
|||
require 'utils/find_files'
|
||||
require 'resources/postgres'
|
||||
|
||||
class PostgresConf
|
||||
class PostgresConf < Vulcano.resource(1)
|
||||
name 'postgres_conf'
|
||||
|
||||
def initialize( conf_path )
|
||||
@runner = Specinfra::Runner
|
||||
@conf_path = conf_path
|
||||
@conf_dir = File.expand_path(File.dirname @conf_path)
|
||||
@files_contents = {}
|
||||
|
@ -36,11 +36,11 @@ class PostgresConf
|
|||
@params = {}
|
||||
|
||||
# skip if the main configuration file doesn't exist
|
||||
if !@runner.check_file_is_file(@conf_path)
|
||||
if !@vulcano.file(@conf_path).is_file?
|
||||
return skip_resource "Can't find file \"#{@conf_path}\""
|
||||
end
|
||||
raw_conf = read_file(@conf_path)
|
||||
if raw_conf.empty? && @runner.get_file_size(@conf_path).stdout.strip.to_i > 0
|
||||
if raw_conf.empty? && @vulcano.file(@conf_path).size > 0
|
||||
return skip_resource("Can't read file \"#{@conf_path}\"")
|
||||
end
|
||||
|
||||
|
@ -70,15 +70,6 @@ class PostgresConf
|
|||
end
|
||||
|
||||
def read_file(path)
|
||||
@files_contents[path] ||= @runner.get_file_content(path).stdout
|
||||
@files_contents[path] ||= @vulcano.file(path).contents
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
module Serverspec::Type
|
||||
def postgres_conf(path = nil)
|
||||
@postgres_conf ||= {}
|
||||
dpath = path || postgres.conf_path
|
||||
@postgres_conf[dpath] ||= PostgresConf.new( dpath )
|
||||
end
|
||||
end
|
|
@ -2,25 +2,22 @@
|
|||
# copyright: 2015, Vulcano Security GmbH
|
||||
# license: All rights reserved
|
||||
|
||||
module Serverspec end
|
||||
module Serverspec::Type
|
||||
class Lines
|
||||
def initialize raw, desc
|
||||
@raw = raw
|
||||
@desc = desc
|
||||
end
|
||||
class Lines
|
||||
def initialize raw, desc
|
||||
@raw = raw
|
||||
@desc = desc
|
||||
end
|
||||
|
||||
def output
|
||||
@raw
|
||||
end
|
||||
def output
|
||||
@raw
|
||||
end
|
||||
|
||||
def lines
|
||||
@raw.split("\n")
|
||||
end
|
||||
def lines
|
||||
@raw.split("\n")
|
||||
end
|
||||
|
||||
def to_s
|
||||
@desc
|
||||
end
|
||||
def to_s
|
||||
@desc
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -36,7 +33,7 @@ class PostgresSession
|
|||
# that does this securely
|
||||
escaped_query = query.gsub(/\\/, '\\\\').gsub(/"/,'\\"').gsub(/\$/,'\\$')
|
||||
# run the query
|
||||
cmd = Serverspec::Type::Command.new("PGPASSWORD='#{@pass}' psql -U #{@user} #{dbs} -c \"#{escaped_query}\"")
|
||||
cmd = @vulcano.run_command("PGPASSWORD='#{@pass}' psql -U #{@user} #{dbs} -c \"#{escaped_query}\"")
|
||||
out = cmd.stdout + "\n" + cmd.stderr
|
||||
if out =~ /could not connect to .*/ or
|
||||
out.downcase =~ /^error/
|
||||
|
@ -51,15 +48,9 @@ class PostgresSession
|
|||
sub(/(.*\n)+([-]+[+])*[-]+\n/,'').
|
||||
# remove the tail
|
||||
sub(/\n[^\n]*\n\n$/,'')
|
||||
l = Serverspec::Type::Lines.new(lines.strip, "PostgreSQL query: #{query}")
|
||||
l = Lines.new(lines.strip, "PostgreSQL query: #{query}")
|
||||
RSpec.__send__( 'describe', l, &block )
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
module Serverspec::Type
|
||||
def postgres_session( user, password )
|
||||
PostgresSession.new(user, password)
|
||||
end
|
||||
end
|
|
@ -19,9 +19,9 @@ require 'vulcano/resource'
|
|||
require 'resources/ntp_conf'
|
||||
require 'resources/parse_config'
|
||||
require 'resources/passwd'
|
||||
# require 'resources/postgres'
|
||||
# require 'resources/postgres_conf'
|
||||
# require 'resources/postgres_session'
|
||||
require 'resources/postgres'
|
||||
require 'resources/postgres_conf'
|
||||
require 'resources/postgres_session'
|
||||
require 'resources/processes'
|
||||
require 'resources/registry_key'
|
||||
require 'resources/security_policy'
|
||||
|
|
Loading…
Add table
Reference in a new issue