diff --git a/lib/inspec/resources/postgres_session.rb b/lib/inspec/resources/postgres_session.rb index b7b962581..0d40170a2 100644 --- a/lib/inspec/resources/postgres_session.rb +++ b/lib/inspec/resources/postgres_session.rb @@ -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) diff --git a/test/fixtures/cmd/psql-password-authentication-fatal-error b/test/fixtures/cmd/psql-password-authentication-fatal-error new file mode 100644 index 000000000..bcaa57f6e --- /dev/null +++ b/test/fixtures/cmd/psql-password-authentication-fatal-error @@ -0,0 +1 @@ +psql: FATAL: password authentication failed for user "postgres"\n diff --git a/test/unit/resources/postgres_session_test.rb b/test/unit/resources/postgres_session_test.rb index 0f22991c5..352921e77 100644 --- a/test/unit/resources/postgres_session_test.rb +++ b/test/unit/resources/postgres_session_test.rb @@ -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!