Class: ChefHandlerForeman::ForemanUploader
- Inherits:
-
Object
- Object
- ChefHandlerForeman::ForemanUploader
- Defined in:
- lib/chef_handler_foreman/foreman_uploader.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #build_request(method, uri, path) ⇒ Object
- #foreman_request(path, body, client_name, method = 'post') ⇒ Object
-
#initialize(opts) ⇒ ForemanUploader
constructor
A new instance of ForemanUploader.
- #sign_data(data) ⇒ Object
- #signature(request) ⇒ Object
Constructor Details
#initialize(opts) ⇒ ForemanUploader
Returns a new instance of ForemanUploader.
25 26 27 |
# File 'lib/chef_handler_foreman/foreman_uploader.rb', line 25 def initialize(opts) = opts end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
23 24 25 |
# File 'lib/chef_handler_foreman/foreman_uploader.rb', line 23 def end |
Instance Method Details
#build_request(method, uri, path) ⇒ Object
57 58 59 60 61 |
# File 'lib/chef_handler_foreman/foreman_uploader.rb', line 57 def build_request(method, uri, path) Net::HTTP.const_get(method.capitalize).new("#{uri.path}/#{path}") rescue NameError => e raise "unsupported method #{method}, try one of get, post, delete, put" end |
#foreman_request(path, body, client_name, method = 'post') ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/chef_handler_foreman/foreman_uploader.rb', line 29 def foreman_request(path, body, client_name, method = 'post') uri = URI.parse([:url]) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme == 'https' http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl? if [:foreman_ssl_ca] && ![:foreman_ssl_ca].empty? http.ca_file = [:foreman_ssl_ca] http.verify_mode = OpenSSL::SSL::VERIFY_PEER end if [:foreman_ssl_cert] && ![:foreman_ssl_cert].empty? && [:foreman_ssl_key] && ![:foreman_ssl_key].empty? http.cert = OpenSSL::X509::Certificate.new(File.read([:foreman_ssl_cert])) http.key = OpenSSL::PKey::RSA.new(File.read([:foreman_ssl_key]), nil) end end req = build_request(method, uri, path) req.add_field('Accept', 'application/json,version=2') req.add_field('X-Foreman-Client', client_name) req.body = body.to_json req.content_type = 'application/json' # signature can be computed once we set body and X-Foreman-Client req.add_field('X-Foreman-Signature', signature(req)) response = http.request(req) end |
#sign_data(data) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/chef_handler_foreman/foreman_uploader.rb', line 74 def sign_data(data) hash_to_sign = Digest::SHA256.hexdigest(data) key = OpenSSL::PKey::RSA.new(File.read([:client_key])) # Base64.encode64 is adding \n in the string signature = Base64.encode64(key.sign(OpenSSL::Digest::SHA256.new, hash_to_sign)).gsub("\n",'') end |
#signature(request) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/chef_handler_foreman/foreman_uploader.rb', line 63 def signature(request) case request when Net::HTTP::Post, Net::HTTP::Patch, Net::HTTP::Put sign_data(request.body) when Net::HTTP::Get, Net::HTTP::Delete sign_data(request['X-Foreman-Client']) else raise "Don't know how to sign #{req.class} requests" end end |