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



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

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



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

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



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

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



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

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



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 71

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



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

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
# 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) { |http|
    http.request(req)
  }
  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