2018-07-05 06:18:17 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
class WebHookService
|
|
|
|
class InternalErrorResponse
|
2020-01-27 04:08:32 -05:00
|
|
|
ERROR_MESSAGE = 'internal error'
|
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
attr_reader :body, :headers, :code
|
|
|
|
|
|
|
|
def initialize
|
2018-03-13 18:38:25 -04:00
|
|
|
@headers = Gitlab::HTTP::Response::Headers.new({})
|
2017-04-27 06:08:57 -04:00
|
|
|
@body = ''
|
2020-01-27 04:08:32 -05:00
|
|
|
@code = ERROR_MESSAGE
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-04 16:07:54 -05:00
|
|
|
GITLAB_EVENT_HEADER = 'X-Gitlab-Event'
|
|
|
|
|
2018-03-13 18:38:25 -04:00
|
|
|
attr_accessor :hook, :data, :hook_name, :request_options
|
2017-04-27 06:08:57 -04:00
|
|
|
|
2020-03-04 16:07:54 -05:00
|
|
|
def self.hook_to_event(hook_name)
|
|
|
|
hook_name.to_s.singularize.titleize
|
|
|
|
end
|
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
def initialize(hook, data, hook_name)
|
|
|
|
@hook = hook
|
|
|
|
@data = data
|
2017-09-07 07:29:12 -04:00
|
|
|
@hook_name = hook_name.to_s
|
2019-07-26 06:21:52 -04:00
|
|
|
@request_options = {
|
|
|
|
timeout: Gitlab.config.gitlab.webhook_timeout,
|
|
|
|
allow_local_requests: hook.allow_local_requests?
|
|
|
|
}
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2018-10-18 05:23:24 -04:00
|
|
|
start_time = Gitlab::Metrics::System.monotonic_time
|
2017-04-27 06:08:57 -04:00
|
|
|
|
|
|
|
response = if parsed_url.userinfo.blank?
|
|
|
|
make_request(hook.url)
|
|
|
|
else
|
|
|
|
make_request_with_auth
|
|
|
|
end
|
|
|
|
|
|
|
|
log_execution(
|
|
|
|
trigger: hook_name,
|
|
|
|
url: hook.url,
|
|
|
|
request_data: data,
|
|
|
|
response: response,
|
2018-10-18 05:23:24 -04:00
|
|
|
execution_duration: Gitlab::Metrics::System.monotonic_time - start_time
|
2017-04-27 06:08:57 -04:00
|
|
|
)
|
|
|
|
|
2017-07-20 11:12:06 -04:00
|
|
|
{
|
|
|
|
status: :success,
|
|
|
|
http_status: response.code,
|
|
|
|
message: response.to_s
|
|
|
|
}
|
2018-10-19 01:55:06 -04:00
|
|
|
rescue SocketError, OpenSSL::SSL::SSLError, Errno::ECONNRESET, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Net::OpenTimeout, Net::ReadTimeout, Gitlab::HTTP::BlockedUrlError, Gitlab::HTTP::RedirectionTooDeep => e
|
2017-04-27 06:08:57 -04:00
|
|
|
log_execution(
|
|
|
|
trigger: hook_name,
|
|
|
|
url: hook.url,
|
|
|
|
request_data: data,
|
|
|
|
response: InternalErrorResponse.new,
|
2018-10-18 05:23:24 -04:00
|
|
|
execution_duration: Gitlab::Metrics::System.monotonic_time - start_time,
|
2017-04-27 06:08:57 -04:00
|
|
|
error_message: e.to_s
|
|
|
|
)
|
|
|
|
|
2020-05-21 17:08:31 -04:00
|
|
|
Gitlab::AppLogger.error("WebHook Error => #{e}")
|
2017-04-27 06:08:57 -04:00
|
|
|
|
2017-07-20 11:12:06 -04:00
|
|
|
{
|
|
|
|
status: :error,
|
|
|
|
message: e.to_s
|
|
|
|
}
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def async_execute
|
2017-11-29 10:30:17 -05:00
|
|
|
WebHookWorker.perform_async(hook.id, data, hook_name)
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def parsed_url
|
|
|
|
@parsed_url ||= URI.parse(hook.url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_request(url, basic_auth = false)
|
2018-03-13 18:38:25 -04:00
|
|
|
Gitlab::HTTP.post(url,
|
2017-04-27 06:08:57 -04:00
|
|
|
body: data.to_json,
|
|
|
|
headers: build_headers(hook_name),
|
|
|
|
verify: hook.enable_ssl_verification,
|
2018-03-13 18:38:25 -04:00
|
|
|
basic_auth: basic_auth,
|
|
|
|
**request_options)
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def make_request_with_auth
|
|
|
|
post_url = hook.url.gsub("#{parsed_url.userinfo}@", '')
|
|
|
|
basic_auth = {
|
|
|
|
username: CGI.unescape(parsed_url.user),
|
2018-06-25 10:44:29 -04:00
|
|
|
password: CGI.unescape(parsed_url.password.presence || '')
|
2017-04-27 06:08:57 -04:00
|
|
|
}
|
|
|
|
make_request(post_url, basic_auth)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_execution(trigger:, url:, request_data:, response:, execution_duration:, error_message: nil)
|
|
|
|
WebHookLog.create(
|
|
|
|
web_hook: hook,
|
|
|
|
trigger: trigger,
|
|
|
|
url: url,
|
|
|
|
execution_duration: execution_duration,
|
|
|
|
request_headers: build_headers(hook_name),
|
|
|
|
request_data: request_data,
|
|
|
|
response_headers: format_response_headers(response),
|
2017-08-01 10:30:29 -04:00
|
|
|
response_body: safe_response_body(response),
|
2017-04-27 06:08:57 -04:00
|
|
|
response_status: response.code,
|
|
|
|
internal_error_message: error_message
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_headers(hook_name)
|
|
|
|
@headers ||= begin
|
|
|
|
{
|
|
|
|
'Content-Type' => 'application/json',
|
2020-03-04 16:07:54 -05:00
|
|
|
GITLAB_EVENT_HEADER => self.class.hook_to_event(hook_name)
|
2017-04-27 06:08:57 -04:00
|
|
|
}.tap do |hash|
|
2018-01-05 16:36:18 -05:00
|
|
|
hash['X-Gitlab-Token'] = Gitlab::Utils.remove_line_breaks(hook.token) if hook.token.present?
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Make response headers more stylish
|
|
|
|
# Net::HTTPHeader has downcased hash with arrays: { 'content-type' => ['text/html; charset=utf-8'] }
|
|
|
|
# This method format response to capitalized hash with strings: { 'Content-Type' => 'text/html; charset=utf-8' }
|
|
|
|
def format_response_headers(response)
|
|
|
|
response.headers.each_capitalized.to_h
|
|
|
|
end
|
2017-08-01 10:30:29 -04:00
|
|
|
|
|
|
|
def safe_response_body(response)
|
|
|
|
return '' unless response.body
|
|
|
|
|
|
|
|
response.body.encode('UTF-8', invalid: :replace, undef: :replace, replace: '')
|
|
|
|
end
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|