Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring of many classes #35

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/chelsea.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@

# Lazy loading
require_relative 'chelsea/cli'
require_relative 'chelsea/deps'
require_relative 'chelsea/lockfile'
require_relative 'chelsea/bom'
require_relative 'chelsea/iq_client'
require_relative 'chelsea/oss_index'
require_relative 'chelsea/oi_client'
require_relative 'chelsea/oi_response'
require_relative 'chelsea/config'
require_relative 'chelsea/version'
# module Chelsea
# autoload :CLI, 'chelsea/cli'
# autoload :Deps, 'chelsea/deps'
# autoload :Lockfile, 'chelsea/lockfile'
# autoload :Bom, 'chelsea/bom'
# autoload :IQClient, 'chelsea/iq_client'
# autoload :OSSIndex, 'chelsea/oss_index'
# autoload :OIClient, 'chelsea/oi_client'
# autoload :OIResponse, 'chelsea/oi_response'
# autoload :Config, 'chelsea/config'
# autoload :Version, 'chelsea/version'
# end
4 changes: 2 additions & 2 deletions lib/chelsea/bom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def collect
end

def xml
@xml ||= _get_xml
@xml ||= _xml
end

def to_s
Expand All @@ -45,7 +45,7 @@ def random_urn_uuid

private

def _get_xml
def _xml
doc = Ox::Document.new
doc << _root_xml
bom = _bom_xml
Expand Down
24 changes: 11 additions & 13 deletions lib/chelsea/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
require 'tty-font'

require_relative 'version'
require_relative 'gems'
require_relative 'report'
require_relative 'iq_client'
require_relative 'config'

module Chelsea
##
# This class provides an interface to the oss index, gems and deps
# This class provides an interface to the oss index, gems and lockfile
class CLI
def initialize(opts)
@opts = opts
Expand All @@ -39,11 +39,12 @@ def process!
_set_config # move to init
elsif @opts.clear?
require_relative 'db'
# Class Method?
Chelsea::DB.new().clear_cache
puts "OSS Index cache cleared"
puts 'OSS Index cache cleared'
elsif @opts.file? && @opts.iq?
dependencies = _process_file_iq
_submit_sbom(dependencies)
report = _process_file
_submit_sbom(report.dependencies)
elsif @opts.file?
_process_file
elsif @opts.help? # quit on opts.help earlier
Expand All @@ -57,7 +58,7 @@ def self.version

private

def _submit_sbom(gems)
def _submit_sbom(report)
iq = Chelsea::IQClient.new(
options: {
public_application_id: @opts[:application],
Expand All @@ -66,32 +67,29 @@ def _submit_sbom(gems)
auth_token: @opts[:iqpass]
}
)
bom = Chelsea::Bom.new(gems.deps.dependencies).collect
bom = Chelsea::Bom.new(report.lockfile.dependencies).collect

status_url = iq.post_sbom(bom)

return unless status_url

iq.poll_status(status_url)
end

def _process_file
gems = Chelsea::Gems.new(
report = Chelsea::Report.new(
file: @opts[:file],
verbose: @opts[:verbose],
options: @opts
)
gems.execute ? (exit 1) : (exit 0)
report.generate ? (exit 1) : (exit 0)
end

def _process_file_iq
gems = Chelsea::Gems.new(
Chelsea::Report.new(
file: @opts[:file],
verbose: @opts[:verbose],
options: @opts
)
gems.collect_iq
gems
end

def _flags_error
Expand Down
6 changes: 3 additions & 3 deletions lib/chelsea/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#

require 'yaml'
require_relative 'oss_index'
require_relative 'oi_client'

module Chelsea
@oss_index_config_location = File.join(Dir.home.to_s, '.ossindex')
Expand All @@ -27,14 +27,14 @@ def self.to_purl(name, version)

def self.config(options = {})
if !options[:user].nil? && !options[:token].nil?
Chelsea::OSSIndex.new(
Chelsea::OIClient.new(
options: {
oss_index_user_name: options[:user],
oss_index_user_token: options[:token]
}
)
else
Chelsea::OSSIndex.new(options: oss_index_config)
Chelsea::OIClient.new(options: oss_index_config)
end
end

Expand Down
18 changes: 11 additions & 7 deletions lib/chelsea/formatters/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
# limitations under the License.
#

class Formatter
module Chelsea
# Abstract class to format server response at runtime
class Formatter
def initialize
@pastel = Pastel.new
@pastel = Pastel.new
end
def get_results
raise 'must implement get_results method in subclass'

def format_response(*)
raise 'must implement format_response method in subclass'
end

def do_print
raise 'must implement do_print method in subclass'
raise 'must implement do_print method in subclass'
end
end
end
end
16 changes: 11 additions & 5 deletions lib/chelsea/formatters/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,23 @@
require_relative 'formatter'

module Chelsea
# Formats Server response to JSON
class JsonFormatter < Formatter
def initialize(options)
attr_accessor :oi_response, :reverse_dependencies
attr_reader :output

def initialize(**options)
@options = options
@output = JSON.dump([])
end

def get_results(server_response, reverse_deps)
server_response.to_json
def format_response
@output = @oi_response.coords
end

def do_print(result)
puts result
def do_print
format_response
puts JSON.dump(@output)
end
end
end
69 changes: 34 additions & 35 deletions lib/chelsea/formatters/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,59 +19,58 @@
require_relative 'formatter'

module Chelsea
# Formats Server response and reverse dependencies to JSON
class TextFormatter < Formatter
attr_accessor :oi_response, :reverse_dependencies
def initialize(options)
@options = options
@pastel = Pastel.new
@output = ''
end

def get_results(server_response, reverse_dependencies)
response = ''
if @options[:verbose]
response += "\n"\
"Audit Results\n"\
"=============\n"
end

vuln_count = server_response.count do |vuln|
vuln['vulnerabilities'].length.positive?
end
server_response.sort! { |x| x['vulnerabilities'].count }
server_response.each.with_index do |r, idx|
name, version = r['coordinates'].sub('pkg:gem/', '').split('@')
reverse_deps = reverse_dependencies["#{name}-#{version}"]
if r['vulnerabilities'].length.positive?
response += @pastel.red(
"[#{idx}/#{server_response.count}] - #{r['coordinates']} "
)
response += @pastel.red.bold("Vulnerable.\n")
response += _get_reverse_deps(reverse_deps, name) if reverse_deps
r['vulnerabilities'].each do |k, _|
response += _format_vuln(k)
end
def format_response
@oi_response.coords.each.with_index do |dep, idx|
reverse_deps = @reverse_dependencies["#{dep[:name]}-#{dep[:version]}"]
header = "[#{idx}/#{oi_response.dep_count}] - #{dep[:coordinates]} "
if dep[:vulnerable]
@output += @pastel.red(header)
@output += @pastel.red.bold("Vulnerable.\n")
@output += _parse_reverse_deps(reverse_deps, dep[:name]) if reverse_deps
_write_vulnerable_coordinates
elsif @options[:verbose]
response += @pastel.white(
"[#{idx}/#{server_response.count}] - #{r['coordinates']} "
)
response += @pastel.green.bold("No vulnerabilities found!\n")
response += _get_reverse_deps(reverse_deps, name) if reverse_deps
@output += @pastel.red(header)
@output += @pastel.green.bold("No vulnerabilities found!\n")
@output += _parse_reverse_deps(reverse_deps, dep[:name]) if reverse_deps
end
end

table = TTY::Table.new(
['Dependencies Audited', 'Vulnerable Dependencies'],
[[server_response.count, vuln_count]]
[[oi_response.dep_count, oi_response.vuln_count]]
)
response += table.render(:unicode)
response
@output += table.render(:unicode)
end

def do_print(results)
puts results
def do_print
_write_header if @options[:verbose]
format_response
puts @output
end

private

def _write_header
@output += "\n"\
"Audit Results\n"\
"=============\n"
end

def _write_vulnerable_coordinates(res)
res['vulnerabilities'].each do |k, _|
@output += _format_vuln(k)
end
end

def _format_vuln(vuln)
vuln_response = "\n\tVulnerability Details:\n"
_color_method = _color_based_on_cvss_score(vuln['cvssScore'])
Expand Down Expand Up @@ -106,7 +105,7 @@ def _color_based_on_cvss_score(cvss_score)
end
end

def _get_reverse_deps(coords, name)
def _parse_reverse_deps(coords, name)
coords.each_with_object('') do |dep, s|
dep.each do |gran|
if gran.class == String && !gran.include?(name)
Expand Down
55 changes: 29 additions & 26 deletions lib/chelsea/formatters/xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@
require 'ox'
require_relative 'formatter'
module Chelsea
# class to format OIResponse and Reverse Deps as an XML Document
# where are we sourcing XML spec?
class XMLFormatter < Formatter
def initialize(options)
attr_accessor :oi_response, :reverse_dependencies
attr_reader :output
def initialize(**options)
@options = options
@output = Ox::Document.new
end

def get_results(server_response, reverse_deps)
def format_response
doc = Ox::Document.new
instruct = Ox::Instruct.new(:xml)
instruct[:version] = '1.0'
Expand All @@ -32,46 +37,44 @@ def get_results(server_response, reverse_deps)

testsuite = Ox::Element.new('testsuite')
testsuite[:name] = 'purl'
testsuite[:tests] = server_response.count
doc << testsuite
testsuite[:tests] = @oi_response.dep_count

server_response.each do |coord|
@oi_response.coords.each do |coord|
testcase = Ox::Element.new('testcase')
testcase[:classname] = coord["coordinates"]
testcase[:name] = coord["coordinates"]

if coord['vulnerabilities'].length.positive?
testcase[:classname] = coord[:coordinates]
testcase[:name] = coord[:coordinates]
if coord[:vulnerabilities].length.positive?
failure = Ox::Element.new('failure')
failure[:type] = "Vulnerable Dependency"
failure << get_vulnerability_block(coord["vulnerabilities"])
failure[:type] = 'Vulnerable Dependency'
failure << get_vulnerability_block(coord[:vulnerabilities])
testcase << failure
testsuite << testcase
elsif @options[:verbose]
testsuite << testcase
end
end

doc
doc << testsuite
@output = doc
end

def do_print(results)
puts Ox.dump(results)
def do_print
Ox.dump(@output)
end

def get_vulnerability_block(vulnerabilities)
vulnBlock = String.new
vuln_block = ''
vulnerabilities.each do |vuln|
vulnBlock += "Vulnerability Title: #{vuln["title"]}\n"\
"ID: #{vuln["id"]}\n"\
"Description: #{vuln["description"]}\n"\
"CVSS Score: #{vuln["cvssScore"]}\n"\
"CVSS Vector: #{vuln["cvssVector"]}\n"\
"CVE: #{vuln["cve"]}\n"\
"Reference: #{vuln["reference"]}"\
"\n"
vuln_block += \
"Vulnerability Title: #{vuln['title']}\n"\
"ID: #{vuln['id']}\n"\
"Description: #{vuln['description']}\n"\
"CVSS Score: #{vuln['cvssScore']}\n"\
"CVSS Vector: #{vuln['cvssVector']}\n"\
"CVE: #{vuln['cve']}\n"\
"Reference: #{vuln['reference']}"\
"\n"
end

vulnBlock
vuln_block
end
end
end
Loading