This bug has been migrated to another issue tracking site. It has been closed here and may no longer be being monitored.

If you would like to get updates for this issue, or to participate in it, you may do so at Red Hat Issue Tracker .
Red Hat Satellite engineering is moving the tracking of its product development work on Satellite to Red Hat Jira (issues.redhat.com). If you're a Red Hat customer, please continue to file support cases via the Red Hat customer portal. If you're not, please head to the "Satellite project" in Red Hat Jira and file new tickets here. Individual Bugzilla bugs will be migrated starting at the end of May. If you cannot log in to RH Jira, please consult article #7032570. That failing, please send an e-mail to the RH Jira admins at rh-issues@redhat.com to troubleshoot your issue as a user management inquiry. The email creates a ServiceNow ticket with Red Hat. Individual Bugzilla bugs that are migrated will be moved to status "CLOSED", resolution "MIGRATED", and set with "MigratedToJIRA" in "Keywords". The link to the successor Jira issue will be found under "Links", have a little "two-footprint" icon next to it, and direct you to the "Satellite project" in Red Hat Jira (issue links are of type "https://issues.redhat.com/browse/SAT-XXXX", where "X" is a digit). This same link will be available in a blue banner at the top of the page informing you that that bug has been migrated.
Bug 1819823 - removing a content host does not remove openscap policy assets references to it
Summary: removing a content host does not remove openscap policy assets references to it
Keywords:
Status: CLOSED MIGRATED
Alias: None
Product: Red Hat Satellite
Classification: Red Hat
Component: SCAP Plugin
Version: 6.6.0
Hardware: Unspecified
OS: Unspecified
unspecified
medium
Target Milestone: stream
Assignee: Adam Ruzicka
QA Contact: Peter Ondrejka
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2020-04-01 16:18 UTC by Joniel Pasqualetto
Modified: 2024-06-05 21:37 UTC (History)
7 users (show)

Fixed In Version: rubygem-foreman_openscap-8.0.1
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
Environment:
Last Closed: 2024-06-05 21:37:56 UTC
Target Upstream Version:
Embargoed:


Attachments (Terms of Use)


Links
System ID Private Priority Status Summary Last Updated
Foreman Issue Tracker 33023 0 Normal New removing host does not remove openscap policy assets references to it 2021-07-15 09:06:40 UTC
Foreman Issue Tracker 37389 0 Normal Closed Orphaned records are left in foreman_openscap_asset_policies table when hosts are deleted 2024-05-07 10:49:32 UTC
Red Hat Issue Tracker   SAT-19501 0 None Migrated None 2024-06-05 21:37:55 UTC

Description Joniel Pasqualetto 2020-04-01 16:18:11 UTC
Description of problem:
When removing a host which is associated with an openscap policy,  its references are not removed from the DB. This makes impossible to edit the policy to which the host was associated.

Version-Release number of selected component (if applicable):
satellite 6.6

How reproducible:
Always

Steps to Reproduce:
1. Configure a host to use openscap
2. Wait until foreman_scap_client uploads the first report for that host. At this point, registries on the tables foreman_openscap_assets and foreman_openscap_asset_policies will be created:

DB before any reports:
~~~
foreman=# select name,id from hosts where name = 'lila-calliste.jpasqualetto.local';
               name               |  id  
----------------------------------+------
 lila-calliste.jpasqualetto.local | 1173
(1 row)

foreman=# select * from foreman_openscap_assets where assetable_id = '1173';
 id | created_at | updated_at | assetable_type | assetable_id 
----+------------+------------+----------------+--------------
(0 rows)
~~~

After the first report is uploaded:
~~~
foreman=# select * from foreman_openscap_assets where assetable_id = '1173';
 id |        created_at         |        updated_at         | assetable_type | assetable_id 
----+---------------------------+---------------------------+----------------+--------------
 15 | 2020-04-01 16:01:01.64441 | 2020-04-01 16:01:01.64441 | Host::Base     |         1173
(1 row)

foreman=# select * from foreman_openscap_asset_policies where asset_id = '15';
 asset_id | policy_id 
----------+-----------
       15 |         1
(1 row)
~~~

3. Delete the host:

~~~
# hammer host delete --id 1173
Host deleted.
~~~

Checking the DB:
~~~
foreman=# select name,id from hosts where name = 'lila-calliste.jpasqualetto.local' or id = '1173';
 name | id 
------+----
(0 rows)

foreman=# select * from foreman_openscap_assets where assetable_id = '1173';
 id |        created_at         |        updated_at         | assetable_type | assetable_id 
----+---------------------------+---------------------------+----------------+--------------
 15 | 2020-04-01 16:01:01.64441 | 2020-04-01 16:01:01.64441 | Host::Base     |         1173
(1 row)

foreman=# select * from foreman_openscap_asset_policies where asset_id = '15';
 asset_id | policy_id 
----------+-----------
       15 |         1
(1 row)

~~~

See that the host is gone, but it is still referenced in the tables foreman_openscap_assets and foreman_openscap_asset_policies.

4. Try editing the policy to which this host was assigned. You don't need to make any changes, just enter edit mode and try clicking the submit button. 
Error in the webUI:

~~~ 
Oops, we're sorry but something went wrong undefined method `policies' for nil:NilClass
~~~

Snippet of the backtrace + sql logs on production.log, when trying to load the host with id 1173 (which was just deleted):
~~~
2020-04-01T12:06:50 [D|sql|c530f7a6]   Host::Base Load (0.5ms)  SELECT  "hosts".* FROM "hosts" WHERE "hosts"."organization_id" = $1 AND "hosts"."location_id" = $2 AND "hosts"."id" = $3 LIMIT $4  [["organization_id", 1], ["location_id", 2], ["id", 1173], ["LIMIT", 1]]
2020-04-01T12:06:50 [D|sql|c530f7a6]    (0.2ms)  ROLLBACK
2020-04-01T12:06:50 [W|app|c530f7a6] Action failed
2020-04-01T12:06:50 [D|app|c530f7a6] Backtrace for 'Action failed' error (NoMethodError): undefined method `policies' for nil:NilClass
/opt/theforeman/tfm/root/usr/share/gems/gems/foreman_openscap-1.0.8/app/models/foreman_openscap/policy.rb:315:in `block in no_mixed_deployments'
/opt/theforeman/tfm-ror52/root/usr/share/gems/gems/activerecord-5.2.1/lib/active_record/relation/delegation.rb:41:in `each'
/opt/theforeman/tfm-ror52/root/usr/share/gems/gems/activerecord-5.2.1/lib/active_record/relation/delegation.rb:41:in `each'
~~~

Actual results:
Host is still referenced in the policy and policy cannot be edited anymore.

Expected results:
Host would have all references removed from any components and nothing would be broken.

Additional info:

Easy to fix the DB inconsistency (as a workaround, one could schedule a script to run this on Satellite to keep the DB consistent):

~~~
foreman=# delete from foreman_openscap_assets where id in (select foa.id from foreman_openscap_assets as foa left join hosts as h on foa.assetable_id = h.id where h.id is NULL and assetable_type = 'Host::Base');
DELETE 1
foreman=# delete from foreman_openscap_asset_policies where asset_id in (select foap.asset_id from foreman_openscap_asset_policies as foap left join foreman_openscap_assets as foa on foap.asset_id = foa.id where foa.id is NULL);
DELETE 1
~~~

To simply generate a list of inconsistencies:
~~~
select foa.id as Assetable_ID, foa.assetable_type as Assetable_Type from foreman_openscap_assets as foa left join hosts as h on foa.assetable_id = h.id where h.id is NULL and assetable_type = 'Host::Base';
select foap.asset_id, foap.policy_id from foreman_openscap_asset_policies as foap left join foreman_openscap_assets as foa on foap.asset_id = foa.id where foa.id is NULL;
~~~

Comment 3 Ondřej Ezr 2021-07-15 09:06:38 UTC
Created redmine issue https://projects.theforeman.org/issues/33023 from this bug

Comment 4 Bryan Kearney 2021-07-15 12:03:57 UTC
Upstream bug assigned to oezr

Comment 5 Bryan Kearney 2021-07-15 12:04:01 UTC
Upstream bug assigned to oezr

Comment 7 Bryan Kearney 2023-08-11 12:01:01 UTC
Upstream bug assigned to oezr

Comment 8 Bryan Kearney 2023-08-11 12:01:04 UTC
Upstream bug assigned to oezr

Comment 9 Brad Buckingham 2023-09-01 21:10:52 UTC
Upon review of our valid but aging backlog the Satellite Team has concluded that this Bugzilla does not meet the criteria for a resolution in the near term, and are planning to close in a month. This message may be a repeat of a previous update and the bug is again being considered to be closed. If you have any concerns about this, please contact your Red Hat Account team.  Thank you.

Comment 10 Oleh Fedorenko 2023-09-04 14:19:35 UTC
Closing this as duplicate of https://bugzilla.redhat.com/show_bug.cgi?id=1753313, which was fixed in Satellite 6.7.0.

If you think this is still valid, please feel free to re-open.

*** This bug has been marked as a duplicate of bug 1753313 ***

Comment 11 Adam Ruzicka 2024-04-26 12:06:01 UTC
I'll revive this one. It is true that we're removing records from foreman_openscap_assets when hosts are deleted, but we still keep around records in foreman_openscap_asset_policies. This does not prevent the policy from being updated, but can lead to unnecessary db bloat.

Comment 12 Eric Helms 2024-06-05 21:37:56 UTC
This BZ has been automatically migrated to the issues.redhat.com Red Hat Issue Tracker. All future work related to this report will be managed there.

Due to differences in account names between systems, some fields were not replicated.  Be sure to add yourself to Jira issue's "Watchers" field to continue receiving updates and add others to the "Need Info From" field to continue requesting information.

To find the migrated issue, look in the "Links" section for a direct link to the new issue location. The issue key will have an icon of 2 footprints next to it, and begin with "SAT-" followed by an integer.  You can also find this issue by visiting https://issues.redhat.com/issues/?jql= and searching the "Bugzilla Bug" field for this BZ's number, e.g. a search like:

"Bugzilla Bug" = 1234567

In the event you have trouble locating or viewing this issue, you can file an issue by sending mail to rh-issues. You can also visit https://access.redhat.com/articles/7032570 for general account information.


Note You need to log in before you can comment on or make changes to this bug.