From d86def9e324c09239d2c0ab5e60438a06068378e Mon Sep 17 00:00:00 2001 From: Ian Ballou Date: Mon, 18 Nov 2024 13:48:42 -0500 Subject: [PATCH] Allow different foreman DB usernames in evr check --- .../check_external_db_evr_permissions.rb | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/definitions/checks/foreman/check_external_db_evr_permissions.rb b/definitions/checks/foreman/check_external_db_evr_permissions.rb index fa1e0b2df..29dbea6e6 100644 --- a/definitions/checks/foreman/check_external_db_evr_permissions.rb +++ b/definitions/checks/foreman/check_external_db_evr_permissions.rb @@ -12,20 +12,24 @@ class CheckExternalDbEvrPermissions < ForemanMaintain::Check end def run - return unless evr_exists? + return true unless evr_exists? error_msg = 'The evr extension is not owned by the foreman DB owner. Please run the ' \ - 'following command to fix it: ' \ + 'following command on the external foreman database to fix it: ' \ 'UPDATE pg_extension SET extowner = (SELECT oid FROM pg_authid WHERE ' \ - "rolname='foreman') WHERE extname='evr';" + "rolname='#{foreman_db_user}') WHERE extname='evr';" fail!(error_msg) unless foreman_owns_evr? end private + def foreman_db_user + feature(:foreman_database).configuration['username'] || 'foreman' + end + def evr_exists? evr_exists = feature(:foreman_database).query(query_for_evr_existence) - if !evr_exists.empty? && evr_exists.first['evr_exists'] == '1' + unless evr_exists.empty? return evr_exists.first['evr_exists'] == '1' end return false @@ -36,7 +40,10 @@ def foreman_owns_evr? unless evr_owned_by_postgres.empty? return evr_owned_by_postgres.first['evr_owned_by_postgres'] == '0' end - fail!('Could not determine if the evr extension is owned by the foreman DB owner') + failure_msg = 'Could not determine if the evr extension is owned by the ' \ + 'foreman DB owner. Check that the foreman database is accessible ' \ + "and that the database connection configuration is up to date." + fail!(failure_msg) end def query_for_evr_existence @@ -47,7 +54,7 @@ def query_for_evr_existence def query_if_postgres_owns_evr <<-SQL - SELECT CASE WHEN r.rolname = 'foreman' THEN 0 ELSE 1 END AS evr_owned_by_postgres + SELECT CASE WHEN r.rolname = '#{foreman_db_user}' THEN 0 ELSE 1 END AS evr_owned_by_postgres FROM pg_extension e JOIN pg_roles r ON e.extowner = r.oid WHERE e.extname = 'evr' SQL end