# File lib/smart_proxy_abrt/abrt_lib.rb, line 73 def self.common_name(request) client_cert = request.env['SSL_CLIENT_CERT'] raise AbrtProxy::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 AbrtProxy::Error::CertificateError, e.message end cn = client_cert.subject.to_a.detect { |name, value| name == 'CN' } cn = cn[1] unless cn.nil? raise AbrtProxy::Error::CertificateError, "Common Name not found in the certificate" unless cn return cn end
# File lib/smart_proxy_abrt/abrt_lib.rb, line 52 def self.faf_request(path, content, content_type="application/json") uri = URI.parse(AbrtProxy::Plugin.settings.server_url.to_s) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme == 'https' http.verify_mode = AbrtProxy::Plugin.settings.server_ssl_noverify ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER if AbrtProxy::Plugin.settings.server_ssl_cert && !AbrtProxy::Plugin.settings.server_ssl_cert.to_s.empty? && AbrtProxy::Plugin.settings.server_ssl_key && !AbrtProxy::Plugin.settings.server_ssl_key.to_s.empty? http.cert = OpenSSL::X509::Certificate.new(File.read(AbrtProxy::Plugin.settings.server_ssl_cert)) http.key = OpenSSL::PKey::RSA.new(File.read(AbrtProxy::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
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 32 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
Returns hex representation of random bytes-long number
# File lib/smart_proxy_abrt/abrt_lib.rb, line 20 def self.random_hex_string(nbytes) OpenSSL::Random.random_bytes(nbytes).unpack('H*').join end
Generate multipart boundary separator
# File lib/smart_proxy_abrt/abrt_lib.rb, line 25 def self.suggest_separator separator = "-"*28 separator + self.random_hex_string(16) end