Class: Inspec::Reporters::Automate

Inherits:
JsonAutomate show all
Defined in:
lib/inspec/reporters/automate.rb

Instance Attribute Summary

Attributes inherited from Base

#enhanced_outcomes, #run_data

Instance Method Summary collapse

Methods inherited from JsonAutomate

#render, #report

Methods inherited from Json

#render, #report

Methods inherited from Base

#output, #render, #rendered_output

Methods included from Utils::RunDataFilters

#apply_report_resize_options, #apply_run_data_filters_to_hash, #filter_empty_profiles, #redact_sensitive_inputs, #sort_controls, #suppress_diff_output

Constructor Details

#initialize(config) ⇒ Automate

Returns a new instance of Automate.



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

def initialize(config)
  super(config)

  # allow the insecure flag
  @config["verify_ssl"] = !@config["insecure"] if @config.key?("insecure")

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

Instance Method Details

#enriched_reportObject



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

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"] || report[:platform][:target_id]
  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] = @config["report_uuid"] || uuid_from_string(final_report[:end_time] + final_report[:node_uuid])

  final_report
end

#send_reportObject



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

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

    res = http.request(req)
    if res.is_a?(Net::HTTPSuccess)
      true
    else
      Inspec::Log.error "send_report: POST to #{uri.path} returned: #{res.body}"
      false
    end
  rescue => e
    Inspec::Log.error "send_report: POST to #{uri.path} returned: #{e.message}"
    false
  end
end