Class: Inspec::Reporters::Automate

Inherits:
Json
  • Object
show all
Defined in:
lib/inspec/reporters/automate.rb

Instance Attribute Summary

Attributes inherited from Base

#run_data

Instance Method Summary collapse

Methods inherited from Json

#render, #report

Methods inherited from Base

#output, #render, #rendered_output

Constructor Details

#initialize(config) ⇒ Automate

Returns a new instance of Automate.



8
9
10
11
12
13
# File 'lib/inspec/reporters/automate.rb', line 8

def initialize(config)
  super(config)

  # default to not verifying ssl for sending reports
  @config['verify_ssl'] = @config['verify_ssl'] || false
end

Instance Method Details

#enriched_reportObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/inspec/reporters/automate.rb', line 15

def enriched_report
  # grab the report from the parent class
  final_report = report

  # Label this content as an inspec_report
  final_report[:type] = 'inspec_report'

  final_report[:end_time] = Time.now.utc.strftime('%FT%TZ')
  final_report[:node_uuid] = @config['node_uuid'] || @run_data[:platform][:uuid]
  raise Inspec::ReporterError, 'Cannot find a UUID for your node. Please specify one via json-config.' if final_report[:node_uuid].nil?

  final_report[:report_uuid] = uuid_from_string(final_report[:end_time] + final_report[:node_uuid])

  # optional json-config passthrough options
  %w{node_name environment roles recipies}.each do |option|
    final_report[option.to_sym] = @config[option] unless @config[option].nil?
  end
  final_report
end

#send_reportObject



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
# File 'lib/inspec/reporters/automate.rb', line 35

def send_report
  headers = { 'Content-Type' => 'application/json' }
  headers['x-data-collector-token'] = @config['token']
  headers['x-data-collector-auth'] = 'version=1.0'

  uri = URI(@config['url'])
  req = Net::HTTP::Post.new(uri.path, headers)
  req.body = enriched_report.to_json
  begin
    Inspec::Log.debug "Posting report to Chef Automate: #{uri.path}"
    http = Net::HTTP.new(uri.hostname, uri.port)
    http.use_ssl = uri.scheme == 'https'
    if @config['verify_ssl'] == true
      http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    else
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end

    http.request(req)
    return true
  rescue => e
    Inspec::Log.error "send_report: POST to #{uri.path} returned: #{e.message}"
    return false
  end
end