Skip to content

Commit

Permalink
Please RuboCop
Browse files Browse the repository at this point in the history
  • Loading branch information
ekohl committed Nov 21, 2024
1 parent 27bad57 commit db8a68c
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions hooks/pre_commit/42-evr_extension_permissions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,34 @@ def check_remote_evr_extension_permissions
# Managed databases will be handled automatically.
return if local_postgresql?
return unless katello_enabled?

config = load_db_config('foreman')

# If postgres is the owner of the DB, then the permissions will not matter.
return if config[:username] == 'postgres'

evr_existence_command = pg_sql_statement("SELECT 1 FROM pg_extension WHERE extname = 'evr';")
logger.debug "Checking if the evr extension exists via #{evr_existence_command}"
evr_existence_output, = execute_command(evr_existence_command, false, true, pg_env(config))

# If the evr extension does not exist, then we can skip this check.
return if evr_existence_output&.strip != '1'
check_evr_owner_sql = "SELECT CASE" \
" WHEN r.rolname = '#{config[:username]}' 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';"

check_evr_owner_sql = "SELECT CASE " \
"WHEN r.rolname = '#{config[:username]}' 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';"

command = pg_sql_statement(check_evr_owner_sql)
logger.debug "Checking if the evr extension is owned by the postgres user via #{command}"
output, = execute_command(command, false, true, pg_env(config))

case output&.strip
when '0'
# The evr extension is owned by the foreman DB owner, so we can skip this check.
return
logger.debug('PostgreSQL EVR extension owned by Foreman DB owner')
when '1'
fail_and_exit("The evr extension is not owned by the foreman DB owner. Please run the following command to fix it: " \
"UPDATE pg_extension SET extowner = (SELECT oid FROM pg_authid WHERE rolname='#{config[:username]}') WHERE extname='evr';")
Expand Down

0 comments on commit db8a68c

Please sign in to comment.