Module: ForemanOpenscap::HostExtensions::ClassMethods

Defined in:
app/models/concerns/foreman_openscap/host_extensions.rb

Instance Method Summary collapse

Instance Method Details

#policy_assigned_directly_host_ids(condition, host_ids_from_arf) ⇒ Object



233
234
235
236
237
238
239
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 233

def policy_assigned_directly_host_ids(condition, host_ids_from_arf)
  ForemanOpenscap::Asset.where(:assetable_type => 'Host::Base')
                        .joins(:policies)
                        .where(condition)
                        .where.not(:assetable_id => host_ids_from_arf)
                        .pluck(:assetable_id)
end

#policy_assigned_using_hostgroup_host_ids(condition, host_ids_from_arf) ⇒ Object



241
242
243
244
245
246
247
248
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 241

def policy_assigned_using_hostgroup_host_ids(condition, host_ids_from_arf)
  hostgroup_with_policy_ids = ForemanOpenscap::Asset.where(:assetable_type => 'Hostgroup')
                                                    .joins(:policies)
                                                    .where(condition)
                                                    .pluck(:assetable_id)
  subtree_ids = ::Hostgroup.where(:id => hostgroup_with_policy_ids).flat_map(&:subtree_ids).uniq
  ::Host.where(:hostgroup_id => subtree_ids).where.not(:id => host_ids_from_arf).pluck(:id)
end

#query_conditions(query) ⇒ Object



192
193
194
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 192

def query_conditions(query)
  { :conditions => "hosts.id IN (#{query})" }
end

#search_assigned_all(condition, not_in_host_ids, negate = false) ⇒ Object



225
226
227
228
229
230
231
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 225

def search_assigned_all(condition, not_in_host_ids, negate = false)
  sql_not = negate ? "NOT" : ""
  direct_result = policy_assigned_directly_host_ids condition, not_in_host_ids
  hg_result = policy_assigned_using_hostgroup_host_ids condition, not_in_host_ids
  result = (direct_result + hg_result).uniq
  { :conditions => "hosts.id #{sql_not} IN (#{result.empty? ? 'NULL' : result.join(',')})" }
end

#search_by_compliance(key, operator, policy_name, method) ⇒ Object



151
152
153
154
155
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 151

def search_by_compliance(key, operator, policy_name, method)
  policy = ForemanOpenscap::Policy.find_by :name => policy_name
  host_ids = policy ? public_send(method, policy).pluck(:id) : []
  { :conditions => ::Host::Managed.arel_table[:id].in(host_ids).to_sql }
end

#search_by_comply_with(key, operator, policy_name) ⇒ Object



157
158
159
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 157

def search_by_comply_with(key, operator, policy_name)
  search_by_compliance key, operator, policy_name, :comply_with
end

#search_by_inconclusive_with(key, operator, policy_name) ⇒ Object



165
166
167
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 165

def search_by_inconclusive_with(key, operator, policy_name)
  search_by_compliance key, operator, policy_name, :inconclusive_with
end

#search_by_missing_arf(key, operator, policy_name) ⇒ Object



213
214
215
216
217
218
219
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 213

def search_by_missing_arf(key, operator, policy_name)
  cond = sanitize_sql_for_conditions(["foreman_openscap_policies.name #{operator} ?", value_to_sql(operator, policy_name)])

  host_ids_from_arf_of_policy = ForemanOpenscap::ArfReport.joins(:policy).where(cond).pluck(:host_id).uniq

  search_assigned_all cond, host_ids_from_arf_of_policy
end

#search_by_not_comply_with(key, operator, policy_name) ⇒ Object



161
162
163
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 161

def search_by_not_comply_with(key, operator, policy_name)
  search_by_compliance key, operator, policy_name, :not_comply_with
end

#search_by_policy_id(key, operator, policy_id) ⇒ Object



208
209
210
211
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 208

def search_by_policy_id(key, operator, policy_id)
  cond = sanitize_sql_for_conditions(["foreman_openscap_policies.id #{operator} ?", value_to_sql(operator, policy_id)])
  search_assigned_all cond, []
end

#search_by_policy_name(key, operator, policy_name) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 196

def search_by_policy_name(key, operator, policy_name)
  cond = sanitize_sql_for_conditions(["foreman_openscap_policies.name #{operator} ?", value_to_sql(operator, policy_name)])

  host_group_host_ids = policy_assigned_using_hostgroup_host_ids cond, []
  host_group_cond = if host_group_host_ids.any?
                      ' OR ' + sanitize_sql_for_conditions("hosts.id IN (#{host_group_host_ids.join(',')})")
                    else
                      ''
                    end
  { :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_cond }
end

#search_by_removed_from_policy(key, operator, policy_name) ⇒ Object



145
146
147
148
149
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 145

def search_by_removed_from_policy(key, operator, policy_name)
  policy = ForemanOpenscap::Policy.find_by :name => policy_name
  host_ids = policy ? removed_from_policy(policy).pluck(:id) : []
  { :conditions => ::Host::Managed.arel_table[:id].in(host_ids).to_sql }
end

#search_by_rule(rule_name, rule_result) ⇒ Object



181
182
183
184
185
186
187
188
189
190
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 181

def search_by_rule(rule_name, rule_result)
  query = ::Host.joins(:arf_reports)
                .merge(ArfReport.latest
                                .by_rule_result(rule_name, rule_result)
                                .unscope(:order))
                .distinct
                .select(::Host.arel_table[:id]).to_sql

  query_conditions query
end

#search_by_rule_failed(key, operator, rule_name) ⇒ Object



173
174
175
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 173

def search_by_rule_failed(key, operator, rule_name)
  search_by_rule rule_name, 'fail'
end

#search_by_rule_othered(key, operator, rule_name) ⇒ Object



177
178
179
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 177

def search_by_rule_othered(key, operator, rule_name)
  search_by_rule rule_name, LogExtensions.othered_result_constants
end

#search_by_rule_passed(key, operator, rule_name) ⇒ Object



169
170
171
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 169

def search_by_rule_passed(key, operator, rule_name)
  search_by_rule rule_name, 'pass'
end

#search_for_any_with_policy(key, operator, value) ⇒ Object



221
222
223
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 221

def search_for_any_with_policy(key, operator, value)
  search_assigned_all nil, [], (value == "false")
end