Class: Hrw::Formatter
- Inherits:
-
Object
- Object
- Hrw::Formatter
- Defined in:
- lib/hrw/formatter.rb
Overview
Format result and pretty print it
Instance Method Summary collapse
-
#filter(result) ⇒ Array
Filter result.
-
#initialize ⇒ Formatter
constructor
Class constructor.
-
#pretty_print(result) ⇒ Boolean
Pretty print result.
Constructor Details
#initialize ⇒ Formatter
Class constructor
19 20 21 |
# File 'lib/hrw/formatter.rb', line 19 def initialize @count = 0 end |
Instance Method Details
#filter(result) ⇒ Array
Filter result
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/hrw/formatter.rb', line 27 def filter(result) deps = [] result['ancestry']['layers'].each do |layer| layer['detected_features'].each do |feature| @count += 1 deps << feature if feature.key?('vulnerabilities') end end deps end |
#pretty_print(result) ⇒ Boolean
Pretty print result
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/hrw/formatter.rb', line 44 def pretty_print(result) high = medium = low = unknown = 0 puts puts("scanning #{Dir.pwd} ...".foreground(:aliceblue)) puts deps = filter(result) deps.each do |dep| dep['vulnerabilities'].each do |vuln| # TODO: bad smell here begin patched_version = JSON.parse(vuln['fixed_by'])['spec'].join(', ') rescue JSON::ParserError patched_version = vuln['fixed_by'] end case vuln['severity'] when 'Defcon1', 'Critical', 'High' color = :red high += 1 when 'Medium' color = :yellow medium += 1 when 'Unknown' color = :magenta unknown += 1 else color = :aliceblue low += 1 end pkg = "#{dep['name']}@#{dep['version']}" puts "✗ #{vuln['severity']} severity vulnerability found in #{pkg.underline.bright}".foreground(color) puts 'Name:'.underline puts " #{vuln['name']}" puts puts 'Description:'.underline puts vuln['description'].gsub(/^/, ' ') puts puts 'Severity:'.underline puts " #{vuln['severity']}" puts puts 'Link:'.underline puts " #{vuln['link']}" puts puts 'Patched version:'.underline puts " #{patched_version}" puts puts end end puts "Tested #{@count} dependencies for known vulnerabilities".foreground(:aliceblue) puts if deps.empty? puts '✓ no vulnerabilities found.'.foreground(:green) puts false else puts "✗ found #{high + medium + low + unknown} vulnerabilities:".foreground(:red) puts color = high > 0 ? :red : :aliceblue puts " high: #{high}".foreground(color) color = medium > 0 ? :yellow : :aliceblue puts " medium: #{medium}".foreground(color) puts " low: #{low}".foreground(:aliceblue) color = unknown > 0 ? :magenta : :aliceblue puts " unknown: #{unknown}".foreground(color) puts true end end |