2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
class WebHookLog < ApplicationRecord
|
2020-01-01 13:55:28 +05:30
|
|
|
include SafeUrl
|
|
|
|
include Presentable
|
2020-03-13 15:44:24 +05:30
|
|
|
include DeleteWithLimit
|
|
|
|
include CreatedAtFilterable
|
2021-06-08 01:23:25 +05:30
|
|
|
include PartitionedTable
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
OVERSIZE_REQUEST_DATA = { 'oversize' => true }.freeze
|
|
|
|
|
2023-06-09 08:11:10 +05:30
|
|
|
attr_accessor :interpolated_url
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
self.primary_key = :id
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
partitioned_by :created_at, strategy: :monthly, retain_for: 3.months
|
2021-06-08 01:23:25 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
belongs_to :web_hook
|
|
|
|
|
|
|
|
serialize :request_headers, Hash # rubocop:disable Cop/ActiveRecordSerialize
|
|
|
|
serialize :request_data, Hash # rubocop:disable Cop/ActiveRecordSerialize
|
|
|
|
serialize :response_headers, Hash # rubocop:disable Cop/ActiveRecordSerialize
|
|
|
|
|
|
|
|
validates :web_hook, presence: true
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
before_save :obfuscate_basic_auth
|
2022-10-02 17:18:49 +05:30
|
|
|
before_save :redact_user_emails
|
2023-06-09 08:11:10 +05:30
|
|
|
before_save :set_url_hash, if: -> { interpolated_url.present? }
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def self.recent
|
2022-10-11 01:57:18 +05:30
|
|
|
where(created_at: 2.days.ago.beginning_of_day..Time.zone.now)
|
2018-11-08 19:23:39 +05:30
|
|
|
.order(created_at: :desc)
|
|
|
|
end
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
# Delete a batch of log records. Returns true if there may be more remaining.
|
|
|
|
def self.delete_batch_for(web_hook, batch_size:)
|
|
|
|
raise ArgumentError, 'batch_size is too small' if batch_size < 1
|
|
|
|
|
|
|
|
where(web_hook: web_hook).limit(batch_size).delete_all == batch_size
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def success?
|
|
|
|
response_status =~ /^2/
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def internal_error?
|
|
|
|
response_status == WebHookService::InternalErrorResponse::ERROR_MESSAGE
|
|
|
|
end
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
def oversize?
|
|
|
|
request_data == OVERSIZE_REQUEST_DATA
|
|
|
|
end
|
|
|
|
|
2023-01-10 11:22:00 +05:30
|
|
|
def request_headers
|
|
|
|
super unless web_hook.token?
|
|
|
|
super if self[:request_headers]['X-Gitlab-Token'] == _('[REDACTED]')
|
|
|
|
|
|
|
|
self[:request_headers].merge('X-Gitlab-Token' => _('[REDACTED]'))
|
|
|
|
end
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def obfuscate_basic_auth
|
|
|
|
self.url = safe_url
|
|
|
|
end
|
2022-07-29 17:44:30 +05:30
|
|
|
|
2022-10-02 17:18:49 +05:30
|
|
|
def redact_user_emails
|
|
|
|
self.request_data.deep_transform_values! do |value|
|
2023-01-13 00:05:48 +05:30
|
|
|
value.to_s =~ URI::MailTo::EMAIL_REGEXP ? _('[REDACTED]') : value
|
2022-10-02 17:18:49 +05:30
|
|
|
end
|
2022-07-29 17:44:30 +05:30
|
|
|
end
|
2023-06-09 08:11:10 +05:30
|
|
|
|
|
|
|
def set_url_hash
|
|
|
|
self.url_hash = Gitlab::CryptoHelper.sha256(interpolated_url)
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|