Skip to content

Commit d2ee8f5

Browse files
committed
Fixes #37883 - halt if remote DB does not own EVR
1 parent 67702be commit d2ee8f5

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Managed databases will be handled automatically.
2+
return if local_postgresql?
3+
4+
database = param_value('foreman', 'db_database') || 'foreman'
5+
username = param_value('foreman', 'db_username') || 'foreman'
6+
password = param_value('foreman', 'db_password')
7+
host = param_value('foreman', 'db_host')
8+
port = param_value('foreman', 'db_port') || 5432
9+
10+
# If postgres is the owner of the DB, then the permissions will not matter.
11+
return if username == 'postgres'
12+
13+
check_evr_owner_sql = "SELECT CASE" \
14+
" WHEN r.rolname = 'postgres' THEN 1" \
15+
" ELSE 0" \
16+
" END AS evr_owned_by_postgres" \
17+
" FROM pg_extension e" \
18+
" JOIN pg_roles r ON e.extowner = r.oid" \
19+
" WHERE e.extname = 'evr';"
20+
21+
command = "PGPASSWORD='#{password}' psql -U #{username} -h #{host} -p #{port} -d #{database} -t -c \"#{check_evr_owner_sql}\""
22+
logger.debug "Checking if the evr extension is owned by the postgres user via #{command}"
23+
output, = execute_command(command, false, true)
24+
unless output.nil?
25+
if output.strip == '1'
26+
fail_and_exit("The evr extension is owned by postgres and not the foreman DB owner. Please run the following command to fix it: " \
27+
"UPDATE pg_extension SET extowner = (SELECT oid FROM pg_authid WHERE rolname='#{username}');")
28+
end
29+
end
30+

0 commit comments

Comments
 (0)