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
234 235 236 237 238 239 240 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 234 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
242 243 244 245 246 247 248 249 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 242 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
193 194 195 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 193 def query_conditions(query) { :conditions => "hosts.id IN (#{query})" } end |
#search_assigned_all(condition, not_in_host_ids, negate = false) ⇒ Object
226 227 228 229 230 231 232 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 226 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
152 153 154 155 156 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 152 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
158 159 160 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 158 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
166 167 168 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 166 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
214 215 216 217 218 219 220 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 214 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
162 163 164 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 162 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
209 210 211 212 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 209 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
197 198 199 200 201 202 203 204 205 206 207 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 197 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
146 147 148 149 150 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 146 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
182 183 184 185 186 187 188 189 190 191 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 182 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
174 175 176 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 174 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
178 179 180 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 178 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
170 171 172 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 170 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
222 223 224 |
# File 'app/models/concerns/foreman_openscap/host_extensions.rb', line 222 def search_for_any_with_policy(key, operator, value) search_assigned_all nil, [], (value == "false") end |