Class: NexposeReportHelper::ReportOps

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

Instance Method Summary collapse

Constructor Details

#initialize(nsc, timeout) ⇒ ReportOps

Returns a new instance of ReportOps.



9
10
11
12
# File 'lib/nexpose_ticketing/report_helper.rb', line 9

def initialize(nsc, timeout)
  @timeout = timeout
  @nsc = nsc
end

Instance Method Details

#generate_sql_report_configObject



14
15
16
17
# File 'lib/nexpose_ticketing/report_helper.rb', line 14

def generate_sql_report_config
  random_name = "Nexpose-ticketing-Temp-#{SecureRandom.uuid}"
  Nexpose::ReportConfig.new(random_name, nil, 'sql')
end

#save_generate_cleanup_report_config(report_config) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/nexpose_ticketing/report_helper.rb', line 19

def save_generate_cleanup_report_config(report_config)
  report_id = report_config.save(@nsc, false)
  @nsc.generate_report(report_id, true)
  wait_for_report(report_id)
  report_details = @nsc.last_report(report_id)
  file = Tempfile.new("#{report_id}")
  file.binmode
  file.write(@nsc.download(report_details.uri))
  file.flush

  #Got the report, cleanup server-side
  @nsc.delete_report_config(report_id)
  file
end

#wait_for_report(id) ⇒ Object

Wait for report generation to complete.

Parameters:

  • id (Fixnum)

    Report configuration ID of the report waiting to generate.



38
39
40
41
42
43
44
45
# File 'lib/nexpose_ticketing/report_helper.rb', line 38

def wait_for_report(id)
  wait_until(:fail_on_exceptions => TRUE, :on_timeout => "Report generation timed out. Status: #{r = @nsc.last_report(id); r ? r.status : 'unknown'}") {
    if %w(Failed Aborted Unknown).include?(@nsc.last_report(id).status)
      raise "Report failed to generate! Status <#{@nsc.last_report(id).status}>"
    end
    @nsc.last_report(id).status == 'Generated'
  }
end

#wait_until(options = {}) ⇒ Object

Wait for a given block to evaluate to true.

The following flags are accepted as arguments:

:timeout Number of seconds to wait before timing out. Defaults to 90.
:polling_interval Number of seconds to wait between checking block again.
   Defaults to 5.
:fail_on_exceptions Whether to fail fast. Defaults to off.
:on_timeout Message to raise with the exception when a timeout occurs.

Example usage:

wait_until { site.id > 0 }
wait_until(:timeout => 30, :polling_interval => 0.5) { 1 == 2 }
wait_until(:on_timeout => 'Unable to confirm scan integration.') { console.sites.find { |site| site[:site_id] == site_id.to_i }[:risk_score] > 0.0 }


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/nexpose_ticketing/report_helper.rb', line 61

def wait_until(options = {})
  polling_interval = 90
  time_limit = Time.now + @timeout
  loop do
    begin
      val = yield
      return val if val
    rescue Exception => error
      raise error if options[:fail_on_exceptions]
    end
    if Time.now >= time_limit
      raise options[:on_timeout] if options[:on_timeout]
      error ||= 'Timed out waiting for condition.'
      raise error
    end
    sleep polling_interval
  end
end