Class: Audit

Inherits:
Object
  • Object
show all
Defined in:
lib/audit/lib/audit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Audit

Create a new audit. The audit will be initialized, but not started.

  • benchmark is a path string that points to the benchmark file that should be used for the audit.

  • attachment_dir is a path string that points to the directory where incoming files will be saved (that directory needs to be writable). If a null value is passed, ATTACH_FILE requests will be ignored and no files will be saved.

  • connection_type is a symbol for the connection type that will be used. Anything that can be given to the ConnectionFactory (connection/ConnectionFactory::create) is valid (:ssh, …)

  • connection_params is a dictionary of connection parameters that are specific to the connection type chosen with connection_type. See the ConnectionFactory class for more datails.

  • logger is an optional logger that the debug output is logged to



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/audit/lib/audit.rb', line 33

def initialize(options)
	raise "Option :benchmark is required" unless options[:benchmark]
#		raise "Option :attachment_dir is required" unless options[:attachment_dir]
	raise "Option :connection_type is required" unless options[:connection_type]
	raise "Option :connection_params is required" unless options[:connection_params]

	if options[:logger] then
		@logger = options[:logger]
	else
		@logger = Logger.new(STDOUT)
	end
	
	@benchmark = BenchmarkFactory.new(:logger => @logger).load(:benchmark => options[:benchmark])
	@connection = ConnectionFactory.new(:logger => @logger).create(:connection_type => options[:connection_type], 
	                                                               :connection_params => options[:connection_params])
	@results = {}
	@exceptions = []
	@attachment_dir = options[:attachment_dir]
end

Instance Attribute Details

#benchmarkObject (readonly)

Returns the value of attribute benchmark.



12
13
14
# File 'lib/audit/lib/audit.rb', line 12

def benchmark
  @benchmark
end

#connectionObject (readonly)

Returns the value of attribute connection.



13
14
15
# File 'lib/audit/lib/audit.rb', line 13

def connection
  @connection
end

#end_timeObject (readonly)

Returns the value of attribute end_time.



15
16
17
# File 'lib/audit/lib/audit.rb', line 15

def end_time
  @end_time
end

#exceptionsObject (readonly)

Returns the value of attribute exceptions.



17
18
19
# File 'lib/audit/lib/audit.rb', line 17

def exceptions
  @exceptions
end

#resultsObject (readonly)

Returns the value of attribute results.



16
17
18
# File 'lib/audit/lib/audit.rb', line 16

def results
  @results
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



14
15
16
# File 'lib/audit/lib/audit.rb', line 14

def start_time
  @start_time
end

Instance Method Details

#abortObject



109
110
111
# File 'lib/audit/lib/audit.rb', line 109

def abort()
	@connection.abort() if @connection
end

#finished?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/audit/lib/audit.rb', line 113

def finished?()
	return !end_time.nil?
end

#nameObject



117
118
119
# File 'lib/audit/lib/audit.rb', line 117

def name()
	return (@benchmark.name || @benchmark.id)  + "#" + @connection.to_s() + (@start_time ? "#" + @start_time.to_s() : "")
end

#on_check_completed(&block) ⇒ Object



97
98
99
# File 'lib/audit/lib/audit.rb', line 97

def on_check_completed(&block)
	@check_completed_handler = block
end

#on_finished(&block) ⇒ Object



101
102
103
# File 'lib/audit/lib/audit.rb', line 101

def on_finished(&block)
	@finished_handler = block
end

#progressObject



105
106
107
# File 'lib/audit/lib/audit.rb', line 105

def progress()
	return ((@response_parser.progress() unless @response_parser.nil?) or 0.0)
end

#remaining_timeObject



121
122
123
124
# File 'lib/audit/lib/audit.rb', line 121

def remaining_time()
	return @benchmark.duration() if @response_parser.nil?
	return @response_parser.remaining_time()
end

#start(parallel = true) ⇒ Object



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
# File 'lib/audit/lib/audit.rb', line 53

def start(parallel = true)
	@start_time = Time.now.utc

	launch_audit = Proc.new do
		remote_script_path = "/tmp/" + RandomString::generate() + ".sh"
		script = LinearScriptGenerator.generate(@benchmark)

		@connection.open() do|conn|
			conn.write_to_remote_file(script, remote_script_path)
			@response_parser = ScriptOutputParser.new(:benchmark => @benchmark, 
			                                          :connection => conn, 
			                                          :attachment_dir => @attachment_dir, 
			                                          :logger => @logger)
			@response_parser.on_check_completed() do|rule_result|
				@results[rule_result.rule_idref] = rule_result
				@check_completed_handler.call(rule_result) unless @check_completed_handler.nil?
			end
			@response_parser.on_finished() do|benchmark, rule_results|
				@end_time = Time.now.utc
				@finished_handler.call(benchmark, rule_results) unless @finished_handler.nil?
			end
				
			conn.exec("/bin/sh " + remote_script_path)
		end
	end

	if (parallel) then
		begin
			Thread.new {launch_audit.call}
		rescue Exception => ex
			exceptions << ex
			@logger.error {"Exception type: #{ex.class.name}"}
			@logger.error {"=== stack trace of exception #{ex.message}"}
			ex.backtrace.each do|line|
				@logger.error {line}
			end
			@logger.error {"=== end stack trace"}
		end
	else
		launch_audit.call
	end
	return self
end

#to_hashObject



126
127
128
129
130
131
132
133
134
135
# File 'lib/audit/lib/audit.rb', line 126

def to_hash()
	return {
		:type => :AUDIT,
		:start_time => @start_time,
		:end_time => @end_time,
		:connection => @connection.to_hash(),
		:benchmark => @benchmark.to_hash(),
		:results => Lazy.new(@results.values(), :map) {|x| Lazy.new(x, :to_hash)}
	}
end