Module: AbrtProxy
- Defined in:
- lib/smart_proxy_abrt.rb,
lib/smart_proxy_abrt/abrt_api.rb,
lib/smart_proxy_abrt/abrt_lib.rb,
lib/smart_proxy_abrt/abrt_plugin.rb,
lib/smart_proxy_abrt/abrt_version.rb
Defined Under Namespace
Modules: Error Classes: Api, HostReport, Plugin
Constant Summary collapse
- VERSION =
'0.0.5'
Class Method Summary collapse
- .common_name(request) ⇒ Object
- .faf_request(path, content, content_type = "application/json") ⇒ Object
-
.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.
-
.random_hex_string(nbytes) ⇒ Object
Returns hex representation of random bytes-long number.
-
.suggest_separator ⇒ Object
Generate multipart boundary separator.
Class Method Details
.common_name(request) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/smart_proxy_abrt/abrt_lib.rb', line 77 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. 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 |
.faf_request(path, content, content_type = "application/json") ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# 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 = if AbrtProxy::Plugin.settings.server_ssl_noverify OpenSSL::SSL::VERIFY_NONE else OpenSSL::SSL::VERIFY_PEER end 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 |
.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
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# 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 |
.random_hex_string(nbytes) ⇒ Object
Returns hex representation of random bytes-long number
20 21 22 |
# 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 |
.suggest_separator ⇒ Object
Generate multipart boundary separator
25 26 27 28 |
# File 'lib/smart_proxy_abrt/abrt_lib.rb', line 25 def self.suggest_separator separator = "-"*28 separator + self.random_hex_string(16) end |