Class: InspecPlugins::Compliance::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/plugins/inspec-compliance/lib/inspec-compliance/http.rb

Overview

implements a simple http abstraction on top of Net::HTTP

Class Method Summary collapse

Class Method Details

._parse_url(url) ⇒ Object



111
112
113
114
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/http.rb', line 111

def self._parse_url(url)
  url = "https://#{url}" if URI.parse(url).scheme.nil?
  URI.parse(url)
end

.get(url, headers = nil, insecure) ⇒ Object

generic get requires



10
11
12
13
14
15
16
17
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/http.rb', line 10

def self.get(url, headers = nil, insecure)
  uri = _parse_url(url)
  req = Net::HTTP::Get.new(uri.path)
  headers&.each do |key, value|
    req.add_field(key, value)
  end
  send_request(uri, req, insecure)
end

.post(url, token, insecure, basic_auth = false) ⇒ Object

generic post request



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/http.rb', line 20

def self.post(url, token, insecure, basic_auth = false)
  # form request
  uri = _parse_url(url)
  req = Net::HTTP::Post.new(uri.path)
  if basic_auth
    req.basic_auth token, ""
  else
    req["Authorization"] = "Bearer #{token}"
  end
  req.form_data = {}

  send_request(uri, req, insecure)
end

.post_file(url, headers, file_path, insecure) ⇒ Object

post a file



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/http.rb', line 45

def self.post_file(url, headers, file_path, insecure)
  uri = _parse_url(url)
  raise "Unable to parse URL: #{url}" if uri.nil? || uri.host.nil?

  http = Net::HTTP.new(uri.host, uri.port)

  # set connection flags
  http.use_ssl = (uri.scheme == "https")
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE if insecure

  req = Net::HTTP::Post.new(uri.path)
  headers.each do |key, value|
    req.add_field(key, value)
  end

  req.body_stream = File.open(file_path, "rb")
  req.add_field("Content-Length", File.size(file_path))
  req.add_field("Content-Type", "application/x-gzip")

  boundary = "INSPEC-PROFILE-UPLOAD"
  req.add_field("session", boundary)
  res = http.request(req)
  res
end

.post_multipart_file(url, headers, file_path, insecure) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/http.rb', line 70

def self.post_multipart_file(url, headers, file_path, insecure)
  uri = _parse_url(url)
  raise "Unable to parse URL: #{url}" if uri.nil? || uri.host.nil?

  http = Net::HTTP.new(uri.host, uri.port)

  # set connection flags
  http.use_ssl = (uri.scheme == "https")
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE if insecure

  File.open(file_path) do |tar|
    req = Net::HTTP::Post::Multipart.new(uri, "file" => UploadIO.new(tar, "application/x-gzip", File.basename(file_path)))
    headers.each do |key, value|
      req.add_field(key, value)
    end
    res = http.request(req)
    return res
  end
end

.post_with_headers(url, headers, body, insecure) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/http.rb', line 34

def self.post_with_headers(url, headers, body, insecure)
  uri = _parse_url(url)
  req = Net::HTTP::Post.new(uri.path)
  req.body = body unless body.nil?
  headers&.each do |key, value|
    req.add_field(key, value)
  end
  send_request(uri, req, insecure)
end

.send_request(uri, req, insecure) ⇒ Object

sends a http requests



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/http.rb', line 91

def self.send_request(uri, req, insecure)
  opts = {
    use_ssl: uri.scheme == "https",
  }
  opts[:verify_mode] = OpenSSL::SSL::VERIFY_NONE if insecure

  raise "Unable to parse URI: #{uri}" if uri.nil? || uri.host.nil?

  res = Net::HTTP.start(uri.host, uri.port, opts) do |http|
    http.request(req)
  end
  res
rescue OpenSSL::SSL::SSLError => e
  raise e unless e.message.include? "certificate verify failed"

  puts "Error: Failed to connect to #{uri}."
  puts "If the server uses a self-signed certificate, please re-run the login command with the --insecure option."
  exit 1
end