Class: ForemanCveScanner::CveReportScanner

Inherits:
Object
  • Object
show all
Defined in:
app/services/foreman_cve_scanner/cve_report_scanner.rb

Overview

Parses raw CVE scanner reports and produces unified logs/metrics.

Constant Summary collapse

SEVERITY_ORDER =
%w[CRITICAL HIGH MEDIUM LOW UNKNOWN].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ CveReportScanner

Returns a new instance of CveReportScanner.



16
17
18
19
# File 'app/services/foreman_cve_scanner/cve_report_scanner.rb', line 16

def initialize(raw)
  @raw_data = raw
  @cve_report_data = generate_unified_vuls
end

Instance Attribute Details

#logsObject (readonly)

Returns the value of attribute logs.



30
31
32
# File 'app/services/foreman_cve_scanner/cve_report_scanner.rb', line 30

def logs
  @logs
end

#statusObject (readonly)

Returns the value of attribute status.



30
31
32
# File 'app/services/foreman_cve_scanner/cve_report_scanner.rb', line 30

def status
  @status
end

Class Method Details

.cve_scanner_report?(raw) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'app/services/foreman_cve_scanner/cve_report_scanner.rb', line 12

def self.cve_scanner_report?(raw)
  raw['reporter'] == 'cve_scan'
end

.detect_scanner(scan_json) ⇒ Object



36
37
38
39
40
41
# File 'app/services/foreman_cve_scanner/cve_report_scanner.rb', line 36

def self.detect_scanner(scan_json)
  return 'grype' if scan_json.is_a?(Hash) && scan_json.key?('matches')
  return 'trivy' if scan_json.is_a?(Hash) && scan_json.key?('Results')

  'unknown'
end

.identify_origin(raw) ⇒ Object



8
9
10
# File 'app/services/foreman_cve_scanner/cve_report_scanner.rb', line 8

def self.identify_origin(raw)
  'CveScanner' if cve_scanner_report?(raw)
end

Instance Method Details

#generateObject



21
22
23
24
25
26
27
28
# File 'app/services/foreman_cve_scanner/cve_report_scanner.rb', line 21

def generate
  @status = {}
  @logs = []
  @cve_report_data.each do |id, cve|
    @logs << generate_log_from_unified(id, cve)
  end
  @logs
end

#metricsObject



43
44
45
46
47
# File 'app/services/foreman_cve_scanner/cve_report_scanner.rb', line 43

def metrics
  res = @status.slice(*::ForemanCveScanner::CveScan::SEVERITY_LEVELS)
  res['total'] = res.values.sum
  res
end

#unified_vulnerabilitiesObject



32
33
34
# File 'app/services/foreman_cve_scanner/cve_report_scanner.rb', line 32

def unified_vulnerabilities
  @cve_report_data
end