Skip to content

Commit

Permalink
Fix rubocop offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernhard committed Apr 8, 2024
1 parent 4a35a50 commit d962683
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 64 deletions.
8 changes: 7 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ Layout/EmptyLineAfterGuardClause:
Enabled: false

Layout/LineLength:
Enabled: 111 # TODO: discuss and set this
Enabled: 120

Metrics/ClassLength:
Max: 200

Metrics/MethodLength:
Max: 20

Rails:
Enabled: true
Expand Down
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_files.include('lib/**/*.rb')
end

APP_RAKEFILE = File.expand_path('../test/dummy/Rakefile', __FILE__)
APP_RAKEFILE = File.expand_path('test/dummy/Rakefile', __dir__)

Bundler::GemHelper.install_tasks

Expand All @@ -37,7 +37,7 @@ task default: :test
begin
require 'rubocop/rake_task'
RuboCop::RakeTask.new
rescue => _
rescue StandardError => _e
puts 'Rubocop not loaded.'
end

Expand Down
21 changes: 10 additions & 11 deletions app/lib/actions/cve_scanner_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ def plan(job_invocation, host, *_args)

def finalize(*_args)
host = Host.find(input[:host_id])
if host.present?
cve_scan_report = format_output(task.main_action.continuous_output.humanize)
report = Hash.new
report["host"] = host.name
report["logs"] = []
report["scan"] = cve_scan_report
report["reported_at"] = Time.now.utc.to_s
report['reporter'] = 'cve_scan'
ConfigReportImporter::import(report)
end
return if host.blank?

report = {
'host' => host.name,
'logs' => [],
'scan' => format_output(task.main_action.continuous_output.humanize),
'reported_at' => Time.now.utc.to_s,
'reporter' => 'cve_scan'
}
ConfigReportImporter.import(report)
end

private
Expand All @@ -46,4 +46,3 @@ def format_output(job_output)
end
end
end

90 changes: 43 additions & 47 deletions app/services/foreman_cve_scanner/cve_report_scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,21 @@ def generate
@logs
end

def logs
@logs
end

def status
@status
end
attr_reader :logs, :status

def metrics
res = @status
res['total'] = @status.values.sum
return res
res
end

private

def generate_log_from_unified(id, entry)
return {
{
'log': {
'level': consume_severity_level(entry['severity']),
'messages': {
'messages': {
message: "#{id}: #{entry['title']} # url: #{entry['url']}"
},
sources: {
Expand All @@ -66,68 +60,70 @@ def generate_log_from_unified(id, entry)
end

def consume_severity_level(severity)
@status[severity.downcase] = 0 unless @status.has_key?(severity.downcase)
@status[severity.downcase] = 0 unless @status.key?(severity.downcase)
@status[severity.downcase] += 1

log = case severity
when 'CRITICAL'
'err'
when 'HIGH'
'warning'
when 'MEDIUM'
'info'
when 'LOW'
'debug'
else
'info'
end
return log
case severity
when 'CRITICAL'
'err'
when 'HIGH'
'warning'
when 'MEDIUM'
'info'
when 'LOW'
'debug'
else
'info'
end
end

def generate_grype_entry(entry)
unified = {}
unified['name'] = entry['artifact']['name']
unified['version'] = entry['artifact']['version']
unified['title'] = entry['vulnerability']['description'].gsub(/[\[\]"\\]/, "")
unified['severity'] = entry['vulnerability']['severity']
unified['url'] = entry['vulnerability']['dataSource']
return unified
{
'name' => entry['artifact']['name'],
'version' => entry['artifact']['version'],
'title' => entry['vulnerability']['description'].gsub(/[\[\]"\\]/, ''),
'severity' => entry['vulnerability']['severity'],
'url' => entry['vulnerability']['dataSource']
}
end

def generate_trivy_entry(entry)
unified = {}
unified['name'] = entry['PkgName']
unified['version'] = entry['InstalledVersion']
unified['title'] = entry['Title'].gsub(/[\[\]"\\]/, "")
unified['severity'] = entry['Severity']
unified['url'] = entry['PrimaryURL']
unified['status'] = entry['Status']
unified['fixed'] = entry['FixedVersion'] || 'open'
unified['published'] = entry['PublishedDate'] if entry.has_key?('PublishedDate')
return unified
unified = {
'name' => entry['PkgName'],
'version' => entry['InstalledVersion'],
'title' => entry['Title'].gsub(/[\[\]"\\]/, ''),
'severity' => entry['Severity'],
'url' => entry['PrimaryURL'],
'status' => entry['Status'],
'fixed' => entry['FixedVersion'] || 'open'
}
unified['published'] = entry['PublishedDate'] if entry.key?('PublishedDate')
unified
end

# rubocop:disable Metrics/AbcSize
def generate_unified_vuls
j = @raw_data['scan']

vuls = {}
if j.has_key?('matches') # Grype
if j.key?('matches') # Grype
j['matches'].each do |vul|
vuls[vul['vulnerability']['id']] = generate_grype_entry(vul)
end
elsif j.has_key?('Results') # Trivy
elsif j.key?('Results') # Trivy
j['Results'].each do |r|
next unless r.has_key? 'Vulnerabilities'
next unless r.key? 'Vulnerabilities'
r['Vulnerabilities'].each do |vul|
vuls[vul['VulnerabilityID']] = generate_trivy_entry(vul)
end
end
end
else
Rails.logger.error "Unsupported cve scanner report format"
raise ::Foreman::Exception.new(_('Invalid report'))
Rails.logger.error 'Unsupported cve scanner report format'
raise ::Foreman::Exception, _('Invalid report')
end

vuls
end
# rubocop:enable Metrics/AbcSize
end
end
6 changes: 4 additions & 2 deletions foreman_cve_scanner.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require File.expand_path('lib/foreman_cve_scanner/version', __dir__)
require 'date'

# rubocop:disable Rails/Date
Gem::Specification.new do |s|
s.name = 'foreman_cve_scanner'
s.version = ForemanCveScanner::VERSION
Expand All @@ -12,13 +13,14 @@ Gem::Specification.new do |s|
s.homepage = 'https://github.com/ATIX-AG/foreman_cve_scanner'
s.summary = 'Run CVE scan on host and collect report'
# also update locale/gemspec.rb
s.description = 'Run CVE scan on host and collect report'
s.description = 'Run CVE scan on host and collect report'

s.files = Dir['{app,config,db,lib,locale,webpack}/**/*'] + ['LICENSE', 'Rakefile', 'README.md']
s.test_files = Dir['test/**/*'] + Dir['webpack/**/__tests__/*.js']

s.required_ruby_version = '>= 2.7', '< 4'

s.add_development_dependency 'rdoc'
s.add_development_dependency 'rake'
s.add_development_dependency 'rdoc'
end
# rubocop:enable Rails/Date
2 changes: 1 addition & 1 deletion test/unit/foreman_cve_scanner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class ForemanPluginTemplateTest < ActiveSupport::TestCase
setup do
User.current = User.find_by_login 'admin'
User.current = User.find_by login: 'admin'
end

test 'the truth' do
Expand Down

0 comments on commit d962683

Please sign in to comment.