Added fatal message check for postgres_session:query method (#7154) (#7155)

* Added fatal message check for postgres_session:query method



* Added empty new line for the new fixture



---------

Signed-off-by: Gokulakrishnan KS <Gokulakrishnan.KS@progress.com>
Co-authored-by: Gokulakrishnan KS <Gokulakrishnan.KS@progress.com>
This commit is contained in:
Nikita Mathur 2024-08-27 17:14:26 +05:30 committed by GitHub
parent 46bd4d4098
commit 0faaeb121a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 16 additions and 1 deletions

View file

@ -55,7 +55,7 @@ module Inspec::Resources
psql_cmd = create_psql_cmd(query, db)
cmd = inspec.command(psql_cmd, redact_regex: %r{(:\/\/[a-z]*:).*(@)})
out = cmd.stdout + "\n" + cmd.stderr
if cmd.exit_status != 0 && ( out =~ /could not connect to/ || out =~ /password authentication failed/ ) && out.downcase =~ /error:/
if cmd.exit_status != 0 && ( out =~ /could not connect to/ || out =~ /password authentication failed/ ) && (out.downcase =~ /error:/ || out.downcase =~ /fatal:/)
raise Inspec::Exceptions::ResourceFailed, "PostgreSQL connection error: #{out}"
elsif cmd.exit_status != 0 && out.downcase =~ /error:/
Lines.new(out, "PostgreSQL query with error: #{query}", cmd.exit_status)

View file

@ -0,0 +1 @@
psql: FATAL: password authentication failed for user "postgres"\n

View file

@ -74,6 +74,20 @@ describe "Inspec::Resources::PostgresSession" do
_(ex.message).must_include("PostgreSQL connection error")
end
it "fails when no password authentication fails with fatal message" do
resource = quick_resource(:postgres_session, :linux, "postgres", "wrongpassword", "localhost", 5432) do |cmd, opts|
cmd.strip!
case cmd
when ("psql -d postgresql://postgres:wrongpassword@localhost:5432/mydatabase -A -t -w -c Select\\ 5\\;") then
result(nil, "test/fixtures/cmd/psql-password-authentication-fatal-error", 1)
else
raise cmd.inspect
end
end
ex = assert_raises(Inspec::Exceptions::ResourceFailed) { resource.query("Select 5;", ["mydatabase"]) }
_(ex.message).must_include("PostgreSQL connection error")
end
it "returns stderr as output if there is error in the query." do
resource = quick_resource(:postgres_session, :linux, "postgres", "postgres", "localhost", 5432) do |cmd, opts|
cmd.strip!