Class: ForemanWebhooks::WebhookService
- Inherits:
-
Object
- Object
- ForemanWebhooks::WebhookService
- Defined in:
- app/services/foreman_webhooks/webhook_service.rb
Instance Attribute Summary collapse
-
#event_name ⇒ Object
Returns the value of attribute event_name.
-
#payload ⇒ Object
Returns the value of attribute payload.
-
#rendered_headers ⇒ Object
Returns the value of attribute rendered_headers.
-
#rendered_url ⇒ Object
Returns the value of attribute rendered_url.
-
#webhook ⇒ Object
Returns the value of attribute webhook.
Class Method Summary collapse
Instance Method Summary collapse
- #execute ⇒ Object
- #foreman_ssl_auth_params ⇒ Object
-
#initialize(webhook:, event_name:, payload:, headers:, url:) ⇒ WebhookService
constructor
A new instance of WebhookService.
Constructor Details
#initialize(webhook:, event_name:, payload:, headers:, url:) ⇒ WebhookService
Returns a new instance of WebhookService.
9 10 11 12 13 14 15 |
# File 'app/services/foreman_webhooks/webhook_service.rb', line 9 def initialize(webhook:, event_name:, payload:, headers:, url:) @webhook = webhook @event_name = event_name @payload = payload @rendered_headers = headers @rendered_url = url end |
Instance Attribute Details
#event_name ⇒ Object
Returns the value of attribute event_name.
5 6 7 |
# File 'app/services/foreman_webhooks/webhook_service.rb', line 5 def event_name @event_name end |
#payload ⇒ Object
Returns the value of attribute payload.
5 6 7 |
# File 'app/services/foreman_webhooks/webhook_service.rb', line 5 def payload @payload end |
#rendered_headers ⇒ Object
Returns the value of attribute rendered_headers.
5 6 7 |
# File 'app/services/foreman_webhooks/webhook_service.rb', line 5 def rendered_headers @rendered_headers end |
#rendered_url ⇒ Object
Returns the value of attribute rendered_url.
5 6 7 |
# File 'app/services/foreman_webhooks/webhook_service.rb', line 5 def rendered_url @rendered_url end |
#webhook ⇒ Object
Returns the value of attribute webhook.
5 6 7 |
# File 'app/services/foreman_webhooks/webhook_service.rb', line 5 def webhook @webhook end |
Class Method Details
.request(url:, payload: '', http_method: :GET, user: nil, password: nil, content_type: 'application/json', headers: {}, ca_string: nil, ca_file: nil, cert: nil, key: nil, ca_verify: false, follow_redirects: true, redirect_limit: 3) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'app/services/foreman_webhooks/webhook_service.rb', line 91 def self.request(url:, payload: '', http_method: :GET, user: nil, password: nil, content_type: 'application/json', headers: {}, ca_string: nil, ca_file: nil, cert: nil, key: nil, ca_verify: false, follow_redirects: true, redirect_limit: 3) uri = URI.parse(url) request = Object.const_get("Net::HTTP::#{http_method.to_s.capitalize}").new(uri.request_uri) request.basic_auth(user, password) if !user.blank? && !password.blank? request['Content-Type'] = content_type request['X-Request-Id'] = ::Logging.mdc['request'] || SecureRandom.uuid request['X-Session-Id'] = ::Logging.mdc['session'] || SecureRandom.uuid headers.each_pair do |hkey, value| request[hkey.to_s] = value.to_s end request.body = payload Rails.logger.debug("Webhook #{http_method.to_s.upcase} request: #{uri}") Rails.logger.debug("Headers: #{request.to_hash.inspect}") Rails.logger.debug("Body: #{request.body.inspect}") http = Net::HTTP.new(uri.host, uri.port) http.open_timeout = Setting[:proxy_request_timeout] http.read_timeout = Setting[:proxy_request_timeout] http.ssl_timeout = Setting[:proxy_request_timeout] if uri.scheme == 'https' http.use_ssl = true http.verify_mode = ca_verify ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE http.cert_store = ca_string if ca_string http.ca_file = ca_file if ca_file http.cert = cert if cert http.key = key if key end http.request(request) do |response| case response when Net::HTTPRedirection new_location = response['location'] Rails.logger.debug "Redirected to #{new_location} (redirects left: #{redirect_limit})" if redirect_limit <= 0 raise(::Foreman::Exception, N_(format('Too many HTTP redirects when calling %{uri}', uri: uri, code: response.code))) end self.request(url: new_location, payload: payload, http_method: http_method, user: user, password: password, content_type: content_type, ca_string: ca_string, ca_verify: ca_verify, headers: headers, follow_redirects: follow_redirects, redirect_limit: redirect_limit - 1) else response end end end |
Instance Method Details
#execute ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/services/foreman_webhooks/webhook_service.rb', line 33 def execute logger.info("Performing '#{webhook.name}' webhook request for event '#{event_name}'") Foreman::Logging.blob("Payload for '#{event_name}'", payload) headers = {} begin headers = JSON.parse(rendered_headers) rescue StandardError => e logger.warn("Could not parse HTTP headers JSON, ignoring: #{e}") logger.debug("Headers: #{rendered_headers}") end verify = webhook.verify_ssl? ca_string = webhook.ca_certs_store if webhook. foreman_ssl = foreman_ssl_auth_params verify = true ca_file = foreman_ssl[:ca_file] cert = foreman_ssl[:cert] key = foreman_ssl[:key] end response = self.class.request(url: rendered_url, payload: payload, http_method: webhook.http_method, user: webhook.user, password: webhook.password, content_type: webhook.http_content_type, headers: headers, ca_verify: verify, ca_string: ca_string, ca_file: ca_file, cert: cert, key: key, follow_redirects: true) status = case response.code.to_i when 400..599 logger.error("#{webhook.http_method.to_s.upcase} response was #{response.code}") :error else logger.info("#{webhook.http_method.to_s.upcase} response was #{response.code}") :success end { status: status, message: response., http_status: response.code.to_i } rescue SocketError, OpenSSL::SSL::SSLError, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::EHOSTUNREACH, Net::OpenTimeout, Net::ReadTimeout => e Foreman::Logging.exception("Failed to execute the webhook #{webhook.name} -> #{event_name}", e) { status: :error, message: e.to_s } end |
#foreman_ssl_auth_params ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/services/foreman_webhooks/webhook_service.rb', line 17 def foreman_ssl_auth_params cert = Setting[:ssl_certificate] ca_cert = Setting[:ssl_ca_file] hostprivkey = Setting[:ssl_priv_key] { cert: OpenSSL::X509::Certificate.new(File.read(cert)), key: OpenSSL::PKey.read(File.read(hostprivkey)), ca_file: ca_cert } rescue StandardError => e msg = 'Unable to read SSL proxy CA, cert or key' Foreman::Logging.exception(msg, e) raise Foreman::WrappedException.new(e, msg) end |