module Proxy::Abrt

Constants

VERSION

Public Class Methods

common_name(request) click to toggle source
# 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") click to toggle source
# 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) click to toggle source

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

# 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_hex_string(nbytes) click to toggle source

Returns hex representation of random bytes-long number

# File lib/smart_proxy_abrt/abrt_lib.rb, line 14
def self.random_hex_string(nbytes)
  OpenSSL::Random.random_bytes(nbytes).unpack('H*').join
end
suggest_separator() click to toggle source

Generate multipart boundary separator

# File lib/smart_proxy_abrt/abrt_lib.rb, line 19
def self.suggest_separator
    separator = "-"*28
    separator + self.random_hex_string(16)
end