Skip to content
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

Fixes #19552 - Fix search for hosts without arf report #258

Merged
merged 1 commit into from May 30, 2017

Conversation

xprazak2
Copy link
Contributor

I think these queries would deserve some tests. The problem is, they need joins across multiple models which would mean setting up associations in our factories and make changes in majority of our tests.

.where(cond)
.where.not(:assetable_id => host_ids_from_arf_of_policy)
.pluck(:assetable_id)
{ :conditions => "hosts.id IN (#{ result.empty? ? 'NULL' : result.join(',')})" }

Choose a reason for hiding this comment

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

Space inside string interpolation detected.

@ares
Copy link
Member

ares commented May 18, 2017

[test]

1 similar comment
@xprazak2
Copy link
Contributor Author

[test]

@xprazak2
Copy link
Contributor Author

[test]

@ares
Copy link
Member

ares commented May 24, 2017

Failures unrelated, the branch does not contain e1d06d9

WHERE foreman_openscap_arf_reports.asset_id = foreman_openscap_assets.id
AND foreman_openscap_arf_reports.policy_id = foreman_openscap_policies.id)")
.pluck(:id)).to_sql}
host_ids_from_arf_of_policy = ForemanOpenscap::ArfReport.joins(:policy).where(cond).pluck(:host_id).uniq
Copy link
Member

Choose a reason for hiding this comment

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

This seems to only search for direct Host - Policy associations, if the host has policy because it's assigned to host group, it's not in the list. It seems it didn't work previously either, could it be incorporated in the patch?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was working when I tested, the policy is never connected directly to the host - it is linked through Asset or ArfReport. It does not matter if a certain policy comes from a hostgroup - it is still linked to a host through associated asset.

@ares
Copy link
Member

ares commented May 24, 2017

It seems to work well, just one comment that would be nice to fix too.

test "should find hosts with inherited policy that were never audited" do
policy = FactoryGirl.create(:policy)
parent = FactoryGirl.create(:hostgroup)
child = FactoryGirl.create(:hostgroup, :ancestry => "#{parent.id}")

Choose a reason for hiding this comment

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

Prefer to_s over string interpolation.

@xprazak2 xprazak2 force-pushed the missing-arf branch 2 times, most recently from 9c29ff3 to 4b3064a Compare May 26, 2017 13:13
@xprazak2
Copy link
Contributor Author

It should be working for policies inherited from hostgroups now.

@xprazak2
Copy link
Contributor Author

[test]

@ares
Copy link
Member

ares commented May 29, 2017

Counts should also change at https://github.com/theforeman/foreman_openscap/blob/master/app/services/foreman_openscap/policy_dashboard/data.rb#L25-L26, we're ignoring host associated through host group there. It should probably use the same scope search_by_policy_name to which it links to. That also needs to be fixed in a same way https://github.com/theforeman/foreman_openscap/blob/master/app/models/concerns/foreman_openscap/host_extensions.rb#L107 :-( I've sent you few suggestions on how to fix it.

.joins(:policies)
.where(condition)
.pluck(:assetable_id)
descendant_ids = Hostgroup.where(:id => hostgroup_with_policy_ids).flat_map(&:descendant_ids).uniq
Copy link
Member

Choose a reason for hiding this comment

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

&:subtree_ids contains also the record itself so we'd avoid hostgroup_with_policy_ids + descendant_ids on line below

{ :conditions => Host::Managed.arel_table[:id].in(Host::Managed.select(Host::Managed.arel_table[:id]).joins(:policies).where(cond).pluck(:id)).to_sql }

host_group_host_ids = policy_assigned_using_hostgroup_host_ids cond, []
if host_group_host_ids.any?

Choose a reason for hiding this comment

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

Use the return of the conditional for variable assignment and comparison.

@xprazak2
Copy link
Contributor Author

I made changes as suggested.

@ares
Copy link
Member

ares commented May 30, 2017

Works nicely, thanks @xprazak2, merging!

@ares ares merged commit 051cf05 into theforeman:master May 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants