From 0faaeb121a5c9fd5bafa59900b02653ff674e64b Mon Sep 17 00:00:00 2001 From: Nikita Mathur Date: Tue, 27 Aug 2024 17:14:26 +0530 Subject: [PATCH] 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 Co-authored-by: Gokulakrishnan KS --- lib/inspec/resources/postgres_session.rb | 2 +- .../cmd/psql-password-authentication-fatal-error | 1 + test/unit/resources/postgres_session_test.rb | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/cmd/psql-password-authentication-fatal-error 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!