Class: Dev::Audit::Report

Inherits:
Object show all
Defined in:
lib/firespring_dev_commands/audit/report.rb,
lib/firespring_dev_commands/audit/report/item.rb,
lib/firespring_dev_commands/audit/report/levels.rb

Overview

The class containing standardized information about an audit report

Defined Under Namespace

Classes: Item, Level

Constant Summary collapse

LEVELS =

All supported audit report levels in ascending order of severity

[
  Level::INFO,
  Level::LOW,
  Level::MODERATE,
  Level::HIGH,
  Level::CRITICAL,
  Level::UNKNOWN
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items, min_severity: ENV.fetch('MIN_SEVERITY', nil), ignorelist: ENV['IGNORELIST'].to_s.split(/\s*,\s*/)) ⇒ Report

Returns a new instance of Report.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/firespring_dev_commands/audit/report.rb', line 8

def initialize(
  items,
  min_severity: ENV.fetch('MIN_SEVERITY', nil),
  ignorelist: ENV['IGNORELIST'].to_s.split(/\s*,\s*/)
)
  # Items should be an array of Item objects
  @items = Array(items)
  raise 'items must all be report items' unless @items.all?(Dev::Audit::Report::Item)

  @min_severity = min_severity || Level::HIGH
  @ignorelist = Array(ignorelist).compact
end

Instance Attribute Details

#filtered_itemsObject

Run the filters against the report items and filter out any which should be excluded



27
28
29
# File 'lib/firespring_dev_commands/audit/report.rb', line 27

def filtered_items
  @filtered_items
end

#ignorelistObject

Returns the value of attribute ignorelist.



6
7
8
# File 'lib/firespring_dev_commands/audit/report.rb', line 6

def ignorelist
  @ignorelist
end

#itemsObject

Returns the value of attribute items.



6
7
8
# File 'lib/firespring_dev_commands/audit/report.rb', line 6

def items
  @items
end

#min_severityObject

Returns the value of attribute min_severity.



6
7
8
# File 'lib/firespring_dev_commands/audit/report.rb', line 6

def min_severity
  @min_severity
end

Instance Method Details

#checkObject

Output the text of the filtered report items Exit with a non-zero status if any vulnerabilities were found



33
34
35
36
37
38
# File 'lib/firespring_dev_commands/audit/report.rb', line 33

def check
  puts(self)
  return if filtered_items.empty?

  at_exit { exit(1) }
end

#desired_severitiesObject

Get all severities greater than or equal to the minimum severity



22
23
24
# File 'lib/firespring_dev_commands/audit/report.rb', line 22

def desired_severities
  LEVELS.slice(LEVELS.find_index(min_severity)..-1)
end

#to_sObject

Returns a string representation of this audit report



41
42
43
44
45
46
47
48
# File 'lib/firespring_dev_commands/audit/report.rb', line 41

def to_s
  return 'No security vulnerabilities found'.green if filtered_items.empty?

  [].tap do |ary|
    ary << "Found #{filtered_items.length} security vulnerabilities:".white.on_red
    filtered_items.each { |item| ary << item.to_s }
  end.join("\n")
end