-
Notifications
You must be signed in to change notification settings - Fork 72
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
Halt upgrades if evr is not owned by foreman on external DB #953
Merged
ehelms
merged 2 commits into
theforeman:master
from
ianballou:halt-upgrade-remote-db-evr-permissions
Nov 20, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
definitions/checks/foreman/check_external_db_evr_permissions.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
module Checks | ||
module Foreman | ||
class CheckExternalDbEvrPermissions < ForemanMaintain::Check | ||
metadata do | ||
label :external_db_evr_permissions | ||
for_feature :foreman_database | ||
description 'Check that external databases have proper EVR extension permissions' | ||
tags :pre_upgrade | ||
confine do | ||
feature(:foreman_database) && !feature(:foreman_database).local? && feature(:katello) | ||
end | ||
end | ||
|
||
def run | ||
return true unless evr_exists? | ||
|
||
error_msg = 'The evr extension is not owned by the foreman database owner. ' \ | ||
'Please run the following command on the external foreman database to fix it: ' \ | ||
'UPDATE pg_extension SET extowner = (SELECT oid FROM pg_authid WHERE ' \ | ||
"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) | ||
return false if evr_exists.empty? | ||
return evr_exists.first['evr_exists'] == '1' | ||
end | ||
|
||
def foreman_owns_evr? | ||
evr_owned_by_postgres = feature(:foreman_database).query(query_if_postgres_owns_evr) | ||
unless evr_owned_by_postgres.empty? | ||
return evr_owned_by_postgres.first['evr_owned_by_postgres'] == '0' | ||
end | ||
failure_msg = 'Could not determine if the evr extension is owned by the ' \ | ||
'foreman database 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 | ||
<<-SQL | ||
SELECT 1 AS evr_exists FROM pg_extension WHERE extname = 'evr' | ||
SQL | ||
end | ||
|
||
def query_if_postgres_owns_evr | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically, this doesn't check whether |
||
<<-SQL | ||
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 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it won't ever be empty, given at this point we do know the extension exists, so it must have some owner?
But it also doesn't hurt to play safe here :)