Module: Proxy::Abrt

Defined in:
lib/smart_proxy_abrt/abrt_version.rb,
lib/smart_proxy_abrt/abrt_plugin.rb,
lib/smart_proxy_abrt/abrt_lib.rb,
lib/smart_proxy_abrt/abrt_api.rb,
lib/smart_proxy_abrt.rb

Defined Under Namespace

Classes: Api, HostReport, Plugin

Constant Summary collapse

VERSION =
'0.0.1'

Class Method Summary collapse

Class Method Details

.common_name(request) ⇒ Object

Raises:

  • (Proxy::Error::Unauthorized)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/smart_proxy_abrt/abrt_lib.rb', line 70

def self.common_name(request)
  client_cert = request.env['SSL_CLIENT_CERT']
  raise Proxy::Error::Unauthorized, "Client certificate required" if client_cert.to_s.empty?

  begin
    client_cert = OpenSSL::X509::Certificate.new(client_cert)
  rescue OpenSSL::OpenSSLError => e
    raise Proxy::Error::Unauthorized, e.message
  end

  cn = client_cert.subject.to_a.detect { |name, value| name == 'CN' }
  cn = cn[1] unless cn.nil?
  raise Proxy::Error::Unauthorized, "Common Name not found in the certificate" unless cn

  return cn
end

.faf_request(path, content, content_type = "application/json") ⇒ Object



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/smart_proxy_abrt/abrt_lib.rb', line 46

def self.faf_request(path, content, content_type="application/json")
  uri              = URI.parse(Proxy::Abrt::Plugin.settings.server_url.to_s)
  http             = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl     = uri.scheme == 'https'
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER

  if Proxy::Abrt::Plugin.settings.server_ssl_noverify
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  if Proxy::Abrt::Plugin.settings.server_ssl_cert && !Proxy::Abrt::Plugin.settings.server_ssl_cert.to_s.empty? \
      && Proxy::Abrt::Plugin.settings.server_ssl_key && !Proxy::Abrt::Plugin.settings.server_ssl_key.to_s.empty?
    http.cert = OpenSSL::X509::Certificate.new(File.read(Proxy::Abrt::Plugin.settings.server_ssl_cert))
    http.key  = OpenSSL::PKey::RSA.new(File.read(Proxy::Abrt::Plugin.settings.server_ssl_key), nil)
  end

  headers, body = self.form_data_file content, content_type

  path = [uri.path, path].join unless uri.path.empty?
  response = http.start { |con| con.post(path, body, headers) }

  response
end

.form_data_file(content, file_content_type) ⇒ Object

It seems that Net::HTTP does not support multipart/form-data - this function is adapted from stackoverflow.com/a/213276 and lib/proxy/request.rb



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/smart_proxy_abrt/abrt_lib.rb', line 26

def self.form_data_file(content, file_content_type)
  # Assemble the request body using the special multipart format
  thepart =  "Content-Disposition: form-data; name=\"file\"; filename=\"*buffer*\"\r\n" +
             "Content-Type: #{ file_content_type }\r\n\r\n#{ content }\r\n"

  boundary = self.suggest_separator
  while thepart.include? boundary
    boundary = self.suggest_separator
  end

  body = "--" + boundary + "\r\n" + thepart + "--" + boundary + "--\r\n"
  headers = {
    "User-Agent"     => "foreman-proxy/#{Proxy::VERSION}",
    "Content-Type"   => "multipart/form-data; boundary=#{ boundary }",
    "Content-Length" => body.length.to_s
  }

  return headers, body
end

.random_alpha_string(length) ⇒ Object



11
12
13
14
15
16
# File 'lib/smart_proxy_abrt/abrt_lib.rb', line 11

def self.random_alpha_string(length)
  base = ('a'..'z').to_a
  result = ""
  length.times { result << base[rand(base.size)] }
  result
end

.suggest_separatorObject

Generate multipart boundary separator



19
20
21
22
# File 'lib/smart_proxy_abrt/abrt_lib.rb', line 19

def self.suggest_separator
    separator = "-"*28
    separator + self.random_alpha_string(16)
end