Bug #33023
closedremoving host does not remove openscap policy assets references to it
Description
Cloned from https://bugzilla.redhat.com/show_bug.cgi?id=1819823
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):
Foreman 1.24
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 = 'example.local';
name | id
----------------------------------+------
example.local | 1111
(1 row)
foreman=# select * from foreman_openscap_assets where assetable_id = '1111';
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 = '1111';
id | created_at | updated_at | assetable_type | assetable_id
----+---------------------------+---------------------------+----------------+--------------
5 | some_time | some_time | Host::Base | 1111
(1 row)
foreman=# select * from foreman_openscap_asset_policies where asset_id = '5';
asset_id | policy_id
----------+-----------
5 | 1
(1 row)
~~~
3. Delete the host:
~~~- hammer host delete --id 1111
Host deleted.
~~~
Checking the DB:
~~~
foreman=# select name,id from hosts where name = 'example.local' or id = '1111';
name | id
------+----
(0 rows)
foreman=# select * from foreman_openscap_assets where assetable_id = '1173';
id | created_at | updated_at | assetable_type | assetable_id
----+---------------------------+---------------------------+----------------+--------------
5 | some_time | some_time (still the same) | Host::Base | 1111
(1 row)
foreman=# select * from foreman_openscap_asset_policies where asset_id = '15';
asset_id | policy_id
----------+-----------
5 | 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.