Method: Inspec::Reporters::Automate#send_report

Defined in:
lib/inspec/reporters/automate.rb

#send_reportObject



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

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)
      return true
    else
      Inspec::Log.error "send_report: POST to #{uri.path} returned: #{res.body}"
      return false
    end
  rescue => e
    Inspec::Log.error "send_report: POST to #{uri.path} returned: #{e.message}"
    return false
  end
end