Skip to content
Merged
Show file tree
Hide file tree
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
220 changes: 220 additions & 0 deletions database_admin/migrations/151_drop_system_platform_view.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
-- system_platform
DROP TABLE IF EXISTS system_platform;
CREATE OR REPLACE VIEW system_platform AS SELECT
si.id,
si.inventory_id,
si.rh_account_id,
si.vmaas_json,
si.json_checksum,
si.last_updated,
si.unchanged_since,
sp.last_evaluation,
sp.installable_advisory_count_cache,
sp.installable_advisory_enh_count_cache,
sp.installable_advisory_bug_count_cache,
sp.installable_advisory_sec_count_cache,
si.last_upload,
si.stale_timestamp,
si.stale_warning_timestamp,
si.culled_timestamp,
si.stale,
si.display_name,
sp.packages_installed,
sp.packages_installable,
si.reporter_id,
sp.third_party,
si.yum_updates,
sp.applicable_advisory_count_cache,
sp.applicable_advisory_enh_count_cache,
sp.applicable_advisory_bug_count_cache,
sp.applicable_advisory_sec_count_cache,
si.satellite_managed,
si.built_pkgcache,
sp.packages_applicable,
sp.template_id,
si.yum_checksum,
si.arch,
si.bootc
FROM system_inventory si JOIN system_patch sp
ON si.id = sp.system_id AND si.rh_account_id = sp.rh_account_id;

CREATE OR REPLACE FUNCTION system_platform_insert()
RETURNS TRIGGER AS
$system_platform_insert$
DECLARE
new_system_id BIGINT;
created TIMESTAMPTZ := CURRENT_TIMESTAMP;
BEGIN
INSERT INTO system_inventory (
inventory_id,
rh_account_id,
vmaas_json,
json_checksum,
last_updated,
unchanged_since,
last_upload,
stale_timestamp,
stale_warning_timestamp,
culled_timestamp,
stale,
display_name,
reporter_id,
yum_updates,
satellite_managed,
built_pkgcache,
yum_checksum,
arch,
bootc,
tags,
created
) VALUES (
NEW.inventory_id,
NEW.rh_account_id,
NEW.vmaas_json,
NEW.json_checksum,
NEW.last_updated,
NEW.unchanged_since,
NEW.last_upload,
NEW.stale_timestamp,
NEW.stale_warning_timestamp,
NEW.culled_timestamp,
COALESCE(NEW.stale, false),
NEW.display_name,
NEW.reporter_id,
NEW.yum_updates,
COALESCE(NEW.satellite_managed, false),
COALESCE(NEW.built_pkgcache, false),
NEW.yum_checksum,
NEW.arch,
COALESCE(NEW.bootc, false),
'[]'::JSONB,
created
)
RETURNING id INTO new_system_id;

INSERT INTO system_patch (
system_id,
rh_account_id,
last_evaluation,
installable_advisory_count_cache,
installable_advisory_enh_count_cache,
installable_advisory_bug_count_cache,
installable_advisory_sec_count_cache,
packages_installed,
packages_installable,
packages_applicable,
third_party,
applicable_advisory_count_cache,
applicable_advisory_enh_count_cache,
applicable_advisory_bug_count_cache,
applicable_advisory_sec_count_cache,
template_id
) VALUES (
new_system_id,
NEW.rh_account_id,
NEW.last_evaluation,
COALESCE(NEW.installable_advisory_count_cache, 0),
COALESCE(NEW.installable_advisory_enh_count_cache, 0),
COALESCE(NEW.installable_advisory_bug_count_cache, 0),
COALESCE(NEW.installable_advisory_sec_count_cache, 0),
COALESCE(NEW.packages_installed, 0),
COALESCE(NEW.packages_installable, 0),
COALESCE(NEW.packages_applicable, 0),
COALESCE(NEW.third_party, false),
COALESCE(NEW.applicable_advisory_count_cache, 0),
COALESCE(NEW.applicable_advisory_enh_count_cache, 0),
COALESCE(NEW.applicable_advisory_bug_count_cache, 0),
COALESCE(NEW.applicable_advisory_sec_count_cache, 0),
NEW.template_id
);

NEW.id := new_system_id;
RETURN NEW;
END;
$system_platform_insert$
LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION system_platform_update()
RETURNS TRIGGER AS
$system_platform_update$
BEGIN
UPDATE system_inventory SET
inventory_id = NEW.inventory_id,
vmaas_json = NEW.vmaas_json,
json_checksum = NEW.json_checksum,
last_updated = NEW.last_updated,
unchanged_since = NEW.unchanged_since,
last_upload = NEW.last_upload,
stale_timestamp = NEW.stale_timestamp,
stale_warning_timestamp = NEW.stale_warning_timestamp,
culled_timestamp = NEW.culled_timestamp,
stale = NEW.stale,
display_name = NEW.display_name,
reporter_id = NEW.reporter_id,
yum_updates = NEW.yum_updates,
satellite_managed = NEW.satellite_managed,
built_pkgcache = NEW.built_pkgcache,
yum_checksum = NEW.yum_checksum,
arch = NEW.arch,
bootc = NEW.bootc
WHERE id = OLD.id AND rh_account_id = OLD.rh_account_id;

UPDATE system_patch SET
last_evaluation = NEW.last_evaluation,
installable_advisory_count_cache = NEW.installable_advisory_count_cache,
installable_advisory_enh_count_cache = NEW.installable_advisory_enh_count_cache,
installable_advisory_bug_count_cache = NEW.installable_advisory_bug_count_cache,
installable_advisory_sec_count_cache = NEW.installable_advisory_sec_count_cache,
packages_installed = NEW.packages_installed,
packages_installable = NEW.packages_installable,
packages_applicable = NEW.packages_applicable,
third_party = NEW.third_party,
applicable_advisory_count_cache = NEW.applicable_advisory_count_cache,
applicable_advisory_enh_count_cache = NEW.applicable_advisory_enh_count_cache,
applicable_advisory_bug_count_cache = NEW.applicable_advisory_bug_count_cache,
applicable_advisory_sec_count_cache = NEW.applicable_advisory_sec_count_cache,
template_id = NEW.template_id
WHERE system_id = OLD.id AND rh_account_id = OLD.rh_account_id;

RETURN NEW;
END;
$system_platform_update$
LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION system_platform_delete()
RETURNS TRIGGER AS
$system_platform_delete$
BEGIN
DELETE FROM system_patch
WHERE system_id = OLD.id AND rh_account_id = OLD.rh_account_id;

DELETE FROM system_inventory
WHERE id = OLD.id AND rh_account_id = OLD.rh_account_id;

RETURN OLD;
END;
$system_platform_delete$
LANGUAGE 'plpgsql';

CREATE TRIGGER system_platform_insert_trigger
INSTEAD OF INSERT ON system_platform
FOR EACH ROW
EXECUTE FUNCTION system_platform_insert();

CREATE TRIGGER system_platform_update_trigger
INSTEAD OF UPDATE ON system_platform
FOR EACH ROW
EXECUTE FUNCTION system_platform_update();

CREATE TRIGGER system_platform_delete_trigger
INSTEAD OF DELETE ON system_platform
FOR EACH ROW
EXECUTE FUNCTION system_platform_delete();

GRANT SELECT, INSERT, UPDATE, DELETE ON system_platform TO listener;
-- evaluator needs to update last_evaluation
GRANT UPDATE ON system_platform TO evaluator;
-- manager needs to update cache and delete systems
GRANT SELECT, UPDATE, DELETE ON system_platform TO manager;
-- VMaaS sync needs to be able to perform system culling tasks
GRANT SELECT, UPDATE, DELETE ON system_platform to vmaas_sync;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DROP TRIGGER IF EXISTS system_platform_delete_trigger ON system_platform;
DROP TRIGGER IF EXISTS system_platform_update_trigger ON system_platform;
DROP TRIGGER IF EXISTS system_platform_insert_trigger ON system_platform;

DROP FUNCTION IF EXISTS system_platform_delete();
DROP FUNCTION IF EXISTS system_platform_update();
DROP FUNCTION IF EXISTS system_platform_insert();

DROP VIEW IF EXISTS system_platform;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
REVOKE UPDATE ON system_patch FROM manager;
GRANT UPDATE ON system_patch TO manager;

REVOKE UPDATE ON system_inventory FROM manager;
GRANT UPDATE ON system_inventory TO manager;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of revoking and granting UPDATE back?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The column-wide and table-wide privileges are distinct in postgres. So just granting privileges on the table level will leave privileges on the column level. REVOKE on the table level however also removes the column level privileges. So this is done to be consistent - remove all update privileges (column and table) and then assign just table privileges. Same approach is done in the "up" migration.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- Restore least-privilege grants for manager after migration 145 temporarily
-- broadened them for system_platform view/trigger updates (removed in 151).
REVOKE UPDATE ON system_inventory FROM manager;
GRANT UPDATE (stale) ON system_inventory TO manager;

REVOKE UPDATE ON system_patch FROM manager;
GRANT UPDATE (
installable_advisory_count_cache,
installable_advisory_enh_count_cache,
installable_advisory_bug_count_cache,
installable_advisory_sec_count_cache,
applicable_advisory_count_cache,
applicable_advisory_enh_count_cache,
applicable_advisory_bug_count_cache,
applicable_advisory_sec_count_cache,
template_id) ON system_patch TO manager;
Loading
Loading