Module: ForemanOpenscap::HostExtensions::ClassMethods
- Defined in:
- app/models/concerns/foreman_openscap/host_extensions.rb
Instance Method Summary collapse
- #policy_assigned_directly_host_ids(condition, host_ids_from_arf) ⇒ Object
- #policy_assigned_using_hostgroup_host_ids(condition, host_ids_from_arf) ⇒ Object
- #query_conditions(query) ⇒ Object
- #search_assigned_all(condition, not_in_host_ids, negate = false) ⇒ Object
- #search_by_compliance(key, operator, policy_name, method) ⇒ Object
- #search_by_comply_with(key, operator, policy_name) ⇒ Object
- #search_by_inconclusive_with(key, operator, policy_name) ⇒ Object
- #search_by_missing_arf(key, operator, policy_name) ⇒ Object
- #search_by_not_comply_with(key, operator, policy_name) ⇒ Object
- #search_by_policy_id(key, operator, policy_id) ⇒ Object
- #search_by_policy_name(key, operator, policy_name) ⇒ Object
- #search_by_removed_from_policy(key, operator, policy_name) ⇒ Object
- #search_by_rule(rule_name, rule_result) ⇒ Object
- #search_by_rule_failed(key, operator, rule_name) ⇒ Object
- #search_by_rule_othered(key, operator, rule_name) ⇒ Object
- #search_by_rule_passed(key, operator, rule_name) ⇒ Object
- #search_for_any_with_policy(key, operator, value) ⇒ Object
Instance Method Details
#policy_assigned_directly_host_ids(condition, host_ids_from_arf) ⇒ Object
239 240 241 242 243 244 245 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 239 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
247 248 249 250 251 252 253 254 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 247 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
198 199 200 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 198 def query_conditions(query) { :conditions => "hosts.id IN (#{query})" } end |
#search_assigned_all(condition, not_in_host_ids, negate = false) ⇒ Object
231 232 233 234 235 236 237 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 231 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
157 158 159 160 161 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 157 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
163 164 165 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 163 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
171 172 173 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 171 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
219 220 221 222 223 224 225 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 219 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
167 168 169 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 167 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
214 215 216 217 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 214 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
202 203 204 205 206 207 208 209 210 211 212 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 202 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
151 152 153 154 155 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 151 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
187 188 189 190 191 192 193 194 195 196 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 187 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
179 180 181 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 179 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
183 184 185 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 183 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
175 176 177 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 175 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
227 228 229 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 227 def search_for_any_with_policy(key, operator, value) search_assigned_all nil, [], (value == "false") end |