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

* Added fatal message check for postgres_session:query method

Signed-off-by: Gokulakrishnan KS <Gokulakrishnan.KS@progress.com>

* Added empty new line for the new fixture

Signed-off-by: Gokulakrishnan KS <Gokulakrishnan.KS@progress.com>

---------

Signed-off-by: Gokulakrishnan KS <Gokulakrishnan.KS@progress.com>
This commit is contained in:
Gokulakrishnan KS 2024-08-27 14:52:32 +05:30 committed by GitHub
parent 763c94862e
commit 14f546391f
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) psql_cmd = create_psql_cmd(query, db)
cmd = inspec.command(psql_cmd, redact_regex: %r{(:\/\/[a-z]*:).*(@)}) cmd = inspec.command(psql_cmd, redact_regex: %r{(:\/\/[a-z]*:).*(@)})
out = cmd.stdout + "\n" + cmd.stderr 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}" raise Inspec::Exceptions::ResourceFailed, "PostgreSQL connection error: #{out}"
elsif cmd.exit_status != 0 && out.downcase =~ /error:/ elsif cmd.exit_status != 0 && out.downcase =~ /error:/
Lines.new(out, "PostgreSQL query with error: #{query}", cmd.exit_status) 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") _(ex.message).must_include("PostgreSQL connection error")
end 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 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| resource = quick_resource(:postgres_session, :linux, "postgres", "postgres", "localhost", 5432) do |cmd, opts|
cmd.strip! cmd.strip!