Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #37883 - halt if remote DB does not own EVR #984

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions hooks/pre_commit/42-evr_extension_permissions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Managed databases will be handled automatically.
return if local_postgresql?

database = param_value('foreman', 'db_database') || 'foreman'
username = param_value('foreman', 'db_username') || 'foreman'
password = param_value('foreman', 'db_password')
host = param_value('foreman', 'db_host')
port = param_value('foreman', 'db_port') || 5432

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

check_evr_owner_sql = "SELECT CASE" \
" WHEN r.rolname = 'postgres' THEN 1" \
" ELSE 0" \
" END AS evr_owned_by_postgres" \
" FROM pg_extension e" \
" JOIN pg_roles r ON e.extowner = r.oid" \
" WHERE e.extname = 'evr';"

command = "PGPASSWORD='#{password}' psql -U #{username} -h #{host} -p #{port} -d #{database} -t -c \"#{check_evr_owner_sql}\""
logger.debug "Checking if the evr extension is owned by the postgres user via #{command}"
output = execute!(command, false, true).strip
if output != '0'
fail_and_exit("The evr extension is owned by postgres and not the foreman DB owner. Please run the following on the foreman DB to fix it: " \
"UPDATE pg_extension SET extowner = (SELECT oid FROM pg_authid WHERE rolname='#{username}');")
end