debian-mirror-gitlab/app/models/hooks/web_hook.rb

112 lines
3.1 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2019-07-07 11:18:12 +05:30
class WebHook < ApplicationRecord
2015-04-26 12:48:37 +05:30
include Sortable
2014-09-02 18:07:02 +05:30
2021-09-04 01:27:46 +05:30
MAX_FAILURES = 100
2021-06-08 01:23:25 +05:30
FAILURE_THRESHOLD = 3 # three strikes
INITIAL_BACKOFF = 10.minutes
MAX_BACKOFF = 1.day
BACKOFF_GROWTH_FACTOR = 2.0
2018-12-05 23:21:45 +05:30
attr_encrypted :token,
mode: :per_attribute_iv,
algorithm: 'aes-256-gcm',
2019-02-15 15:39:39 +05:30
key: Settings.attr_encrypted_db_key_base_32
2018-12-05 23:21:45 +05:30
attr_encrypted :url,
mode: :per_attribute_iv,
algorithm: 'aes-256-gcm',
2019-02-15 15:39:39 +05:30
key: Settings.attr_encrypted_db_key_base_32
2018-12-05 23:21:45 +05:30
2019-12-21 20:55:43 +05:30
has_many :web_hook_logs
2014-09-02 18:07:02 +05:30
2019-10-12 21:52:04 +05:30
validates :url, presence: true
validates :url, public_url: true, unless: ->(hook) { hook.is_a?(SystemHook) }
2018-11-08 19:23:39 +05:30
2018-03-17 18:26:18 +05:30
validates :token, format: { without: /\n/ }
2018-11-20 20:47:30 +05:30
validates :push_events_branch_filter, branch_filter: true
2014-09-02 18:07:02 +05:30
2021-06-08 01:23:25 +05:30
scope :executable, -> do
next all unless Feature.enabled?(:web_hooks_disable_failed)
where('recent_failures <= ? AND (disabled_until IS NULL OR disabled_until < ?)', FAILURE_THRESHOLD, Time.current)
end
def executable?
return true unless web_hooks_disable_failed?
recent_failures <= FAILURE_THRESHOLD && (disabled_until.nil? || disabled_until < Time.current)
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ServiceClass
2015-09-11 14:41:01 +05:30
def execute(data, hook_name)
2021-06-08 01:23:25 +05:30
WebHookService.new(self, data, hook_name).execute if executable?
2014-09-02 18:07:02 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ServiceClass
2014-09-02 18:07:02 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ServiceClass
2015-09-11 14:41:01 +05:30
def async_execute(data, hook_name)
2021-06-08 01:23:25 +05:30
WebHookService.new(self, data, hook_name).async_execute if executable?
2016-06-02 11:05:42 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ServiceClass
2018-11-08 19:23:39 +05:30
# Allow urls pointing localhost and the local network
def allow_local_requests?
2019-10-12 21:52:04 +05:30
Gitlab::CurrentSettings.allow_local_requests_from_web_hooks_and_services?
2018-11-08 19:23:39 +05:30
end
2020-01-01 13:55:28 +05:30
def help_path
'user/project/integrations/webhooks'
end
2021-06-08 01:23:25 +05:30
def next_backoff
return MAX_BACKOFF if backoff_count >= 8 # optimization to prevent expensive exponentiation and possible overflows
(INITIAL_BACKOFF * (BACKOFF_GROWTH_FACTOR**backoff_count))
.clamp(INITIAL_BACKOFF, MAX_BACKOFF)
.seconds
end
def disable!
2021-10-27 15:23:28 +05:30
update_attribute(:recent_failures, FAILURE_THRESHOLD + 1)
2021-06-08 01:23:25 +05:30
end
def enable!
2021-09-04 01:27:46 +05:30
return if recent_failures == 0 && disabled_until.nil? && backoff_count == 0
2021-10-27 15:23:28 +05:30
assign_attributes(recent_failures: 0, disabled_until: nil, backoff_count: 0)
save(validate: false)
2021-06-08 01:23:25 +05:30
end
2021-09-04 01:27:46 +05:30
def backoff!
2021-11-11 11:23:49 +05:30
return if backoff_count >= MAX_FAILURES && disabled_until && disabled_until > Time.current
2021-10-27 15:23:28 +05:30
assign_attributes(disabled_until: next_backoff.from_now, backoff_count: backoff_count.succ.clamp(0, MAX_FAILURES))
save(validate: false)
2021-09-04 01:27:46 +05:30
end
def failed!
2021-10-27 15:23:28 +05:30
return unless recent_failures < MAX_FAILURES
assign_attributes(recent_failures: recent_failures + 1)
save(validate: false)
2021-09-04 01:27:46 +05:30
end
2021-06-08 01:23:25 +05:30
# Overridden in ProjectHook and GroupHook, other webhooks are not rate-limited.
def rate_limit
nil
end
2021-09-04 01:27:46 +05:30
# Custom attributes to be included in the worker context.
def application_context
{ related_class: type }
end
2021-06-08 01:23:25 +05:30
private
def web_hooks_disable_failed?
Feature.enabled?(:web_hooks_disable_failed)
end
2014-09-02 18:07:02 +05:30
end