Class: ForemanOpenscap::ArfReport

Inherits:
Report
  • Object
show all
Includes:
ComplianceStatusScopedSearch, OpenscapProxyExtensions, Taxonomix
Defined in:
app/models/foreman_openscap/arf_report.rb

Constant Summary collapse

METRIC =

attr_accessible :host_id, :reported_at, :status, :metrics

%w[passed othered failed].freeze
BIT_NUM =
10
MAX =
(1 << BIT_NUM) - 1

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OpenscapProxyExtensions

#openscap_proxy_api

Class Method Details

.bit_mask(status) ⇒ Object



64
65
66
# File 'app/models/foreman_openscap/arf_report.rb', line 64

def self.bit_mask(status)
  ComplianceStatus.bit_mask(status)
end

.create_arf(asset, proxy, params) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/models/foreman_openscap/arf_report.rb', line 107

def self.create_arf(asset, proxy, params)
  arf_report = nil
  policy = Policy.find_by :id => params[:policy_id]
  return unless policy

  ArfReport.transaction do
    arf_report = ArfReport.create(:host => asset.host,
                                  :reported_at => Time.at(params[:date].to_i),
                                  :status => params[:metrics],
                                  :metrics => params[:metrics],
                                  :openscap_proxy => proxy)
    return arf_report unless arf_report.persisted?
    PolicyArfReport.where(:arf_report_id => arf_report.id, :policy_id => policy.id, :digest => params[:digest]).first_or_create!
    if params[:logs]
      params[:logs].each do |log|
        src = Source.find_or_create_by(value: log[:source])
        msg = nil
        if src.logs.count > 0
          msg = Log.where(:source_id => src.id).order(:id => :desc).first.message
          update_msg_with_changes(msg, log)
        else
          if (msg = Message.find_by(:value => log[:title]))
            msg.attributes = {
              :value => N_(log[:title]),
              :severity => log[:severity],
              :description => newline_to_space(log[:description]),
              :rationale => newline_to_space(log[:rationale]),
              :scap_references => references_links(log[:references]),
              :fixes => fixes(log[:fixes])
            }
          else
            msg = Message.new(:value => N_(log[:title]),
                              :severity => log[:severity],
                              :description => newline_to_space(log[:description]),
                              :rationale => newline_to_space(log[:rationale]),
                              :scap_references => references_links(log[:references]),
                              :fixes => fixes(log[:fixes]))
          end
          msg.save!
        end
        # TODO: log level
        Log.create!(:source_id => src.id,
                    :message_id => msg.id,
                    :level => :info,
                    :result => log[:result],
                    :report => arf_report)
      end
    end
  end
  arf_report
end

.fixes(raw_fixes) ⇒ Object



227
228
229
230
231
# File 'app/models/foreman_openscap/arf_report.rb', line 227

def self.fixes(raw_fixes)
  return if raw_fixes.empty?

  JSON.fast_generate(raw_fixes)
end

.newline_to_space(string) ⇒ Object



212
213
214
# File 'app/models/foreman_openscap/arf_report.rb', line 212

def self.newline_to_space(string)
  string.gsub(/ *\n+/, " ")
end


216
217
218
219
220
221
222
223
224
225
# File 'app/models/foreman_openscap/arf_report.rb', line 216

def self.references_links(references)
  return if references.nil?
  html_links = []
  references.each do |reference|
    next if reference['title'] == 'test_attestation' # A blank url created by OpenSCAP
    reference['html_link'] = "<a href='#{reference['href']}'>#{reference['href']}</a>" if reference['title'].blank?
    html_links << reference['html_link']
  end
  html_links.join(', ')
end

.report_status_columnObject



68
69
70
# File 'app/models/foreman_openscap/arf_report.rb', line 68

def self.report_status_column
  "status"
end

.update_msg_with_changes(msg, incoming_data) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
# File 'app/models/foreman_openscap/arf_report.rb', line 233

def self.update_msg_with_changes(msg, incoming_data)
  msg.severity = incoming_data['severity']
  msg.description = incoming_data['description']
  msg.rationale = incoming_data['rationale']
  msg.scap_references = incoming_data['references']
  msg.value = incoming_data['title']
  msg.fixes = fixes(incoming_data['fixes'])

  return unless msg.changed?
  msg.save
end

Instance Method Details

#assign_locations_organizationsObject



159
160
161
162
163
164
# File 'app/models/foreman_openscap/arf_report.rb', line 159

def assign_locations_organizations
  if host
    self.location_ids = [host.location_id]
    self.organization_ids = [host.organization_id]
  end
end

#calculatorObject



87
88
89
# File 'app/models/foreman_openscap/arf_report.rb', line 87

def calculator
  ArfReportStatusCalculator.new(:bit_field => read_attribute(self.class.report_status_column))
end

#destroy_from_proxyObject



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

def destroy_from_proxy
  if !host
    destroy_from_proxy_warning "host"
  elsif !policy
    destroy_from_proxy_warning "policy"
  elsif !openscap_proxy
    destroy_from_proxy_warning "OpenSCAP proxy"
  else
    openscap_proxy_api.destroy_report(self, ForemanOpenscap::Helper::find_name_or_uuid_by_host(host))
  end
end

#destroy_from_proxy_warning(associated) ⇒ Object



207
208
209
210
# File 'app/models/foreman_openscap/arf_report.rb', line 207

def destroy_from_proxy_warning(associated)
  logger.warn "Skipping deletion of report with id #{id} from proxy, no #{associated} associated with report"
  true
end

#equal?(other) ⇒ Boolean

Returns:

  • (Boolean)


186
187
188
189
190
191
192
193
# File 'app/models/foreman_openscap/arf_report.rb', line 186

def equal?(other)
  results = [logs, other.logs].flatten.group_by(&:source_id).values
  # for each rule, there should be one result from both reports
  return false unless results.map(&:length).all? { |item| item == 2 }
  results.all? { |result| result.first.source_id == result.last.source_id } &&
    host_id == other.host_id &&
    policy.id == other.policy.id
end

#failedObject



95
96
97
# File 'app/models/foreman_openscap/arf_report.rb', line 95

def failed
  status_of "failed"
end

#failed?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'app/models/foreman_openscap/arf_report.rb', line 166

def failed?
  failed > 0
end

#otheredObject



99
100
101
# File 'app/models/foreman_openscap/arf_report.rb', line 99

def othered
  status_of "othered"
end

#othered?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'app/models/foreman_openscap/arf_report.rb', line 174

def othered?
  !passed? && !failed?
end

#passedObject



91
92
93
# File 'app/models/foreman_openscap/arf_report.rb', line 91

def passed
  status_of "passed"
end

#passed?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'app/models/foreman_openscap/arf_report.rb', line 170

def passed?
  passed > 0 && failed == 0 && othered == 0
end

#rules_countObject



103
104
105
# File 'app/models/foreman_openscap/arf_report.rb', line 103

def rules_count
  status.values.sum
end

#status=(st) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/foreman_openscap/arf_report.rb', line 72

def status=(st)
  s = case st
      when Integer
        st
      when Hash, ActionController::Parameters
        ArfReportStatusCalculator.new(:counters => st).calculate
      else
        raise "Unsupported report status format #{st.class}"
      end
  write_attribute(:status, s)
end

#to_bzipObject



182
183
184
# File 'app/models/foreman_openscap/arf_report.rb', line 182

def to_bzip
  openscap_proxy_api.arf_report_bzip(self, ForemanOpenscap::Helper::find_name_or_uuid_by_host(host))
end

#to_htmlObject



178
179
180
# File 'app/models/foreman_openscap/arf_report.rb', line 178

def to_html
  openscap_proxy_api.arf_report_html(self, ForemanOpenscap::Helper::find_name_or_uuid_by_host(host))
end