-
Notifications
You must be signed in to change notification settings - Fork 124
/
base.rb
77 lines (62 loc) · 2.02 KB
/
base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
require 'open3'
module Salus::Scanners
# Super class for all scanner objects.
class Base
def initialize(repository:, report:, config:)
@repository = repository
@report = report
@config = config
end
def name
self.class.name.sub('Salus::Scanners::', '')
end
# The scanning logic or something that calls a scanner.
def run
raise NotImplementedError
end
# Returns TRUE if this scanner is appropriate for this repo, ELSE false.
def should_run?
raise NotImplementedError
end
# Runs a command on the terminal.
def run_shell(command, env: {}, stdin_data: '')
# If we're passed a string, convert it to an array beofre passing to capture3
command = command.split unless command.is_a?(Array)
stdout, stderr, exit_status = Open3.capture3(env, *command, stdin_data: stdin_data)
{ stdout: stdout, stderr: stderr, exit_status: exit_status }
end
# Add a log to the report that this scanner had no findings.
def report_success
@report.scan_passed(name, true)
end
# Add a log to the report that this scanner had findings.
def report_failure
@report.scan_passed(name, false)
end
# Report information about this scan.
def report_info(type, message)
@report.scan_info(name, type, message)
end
# Report the STDOUT from the scanner.
def report_stdout(stdout)
@report.scan_stdout(name, stdout)
end
# Report the STDERR from the scanner.
def report_stderr(stderr)
@report.scan_stderr(name, stderr)
end
# Report an error in a scanner.
def report_error(error_data)
unless error_data.is_a?(Hash)
raise "`report_error` must take in a hash, not a #{error_data.class}"
end
@report.salus_error(name, error_data)
end
def report_recorded_failure?
@report.has_failure?(name)
end
def record_dependency_info(info, dependency_file)
report_info('dependency', { dependency_file: dependency_file }.merge(info))
end
end
end