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-04-09 20:01:23 +00:00
|
|
|
# license: All rights reserved
|
|
|
|
|
2015-08-28 19:37:03 +00:00
|
|
|
class Lines
|
|
|
|
def initialize raw, desc
|
|
|
|
@raw = raw
|
|
|
|
@desc = desc
|
|
|
|
end
|
2015-04-09 20:01:23 +00:00
|
|
|
|
2015-08-28 19:37:03 +00:00
|
|
|
def output
|
|
|
|
@raw
|
|
|
|
end
|
2015-04-09 20:01:23 +00:00
|
|
|
|
2015-08-28 19:37:03 +00:00
|
|
|
def lines
|
|
|
|
@raw.split("\n")
|
|
|
|
end
|
2015-04-09 20:01:23 +00:00
|
|
|
|
2015-08-28 19:37:03 +00:00
|
|
|
def to_s
|
|
|
|
@desc
|
2015-04-09 20:01:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class PostgresSession
|
|
|
|
def initialize user, pass
|
|
|
|
@user = user || 'postgres'
|
|
|
|
@pass = pass
|
|
|
|
end
|
|
|
|
|
|
|
|
def describe(query, db = [], &block)
|
|
|
|
dbs = db.map{|x| "-d #{x}" }.join(' ')
|
|
|
|
# TODO: simple escape, must be handled by a library
|
|
|
|
# that does this securely
|
|
|
|
escaped_query = query.gsub(/\\/, '\\\\').gsub(/"/,'\\"').gsub(/\$/,'\\$')
|
|
|
|
# run the query
|
2015-08-30 02:33:15 +00:00
|
|
|
cmd = vulcano.run_command("PGPASSWORD='#{@pass}' psql -U #{@user} #{dbs} -c \"#{escaped_query}\"")
|
2015-04-09 20:01:23 +00:00
|
|
|
out = cmd.stdout + "\n" + cmd.stderr
|
2015-05-14 16:29:55 +00:00
|
|
|
if out =~ /could not connect to .*/ or
|
2015-04-09 20:01:23 +00:00
|
|
|
out.downcase =~ /^error/
|
|
|
|
# skip this test if the server can't run the query
|
|
|
|
RSpec.describe( cmd ) do
|
|
|
|
it "is skipped", skip: out do
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
lines = cmd.stdout.
|
|
|
|
# remove the whole header (i.e. up to the first ^-----+------+------$)
|
|
|
|
sub(/(.*\n)+([-]+[+])*[-]+\n/,'').
|
|
|
|
# remove the tail
|
|
|
|
sub(/\n[^\n]*\n\n$/,'')
|
2015-08-28 19:37:03 +00:00
|
|
|
l = Lines.new(lines.strip, "PostgreSQL query: #{query}")
|
2015-04-09 20:01:23 +00:00
|
|
|
RSpec.__send__( 'describe', l, &block )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|