File: web_hook_log.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (95 lines) | stat: -rw-r--r-- 2,603 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
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
90
91
92
93
94
95
# frozen_string_literal: true

class WebHookLog < ApplicationRecord
  include Presentable
  include DeleteWithLimit
  include CreatedAtFilterable
  include PartitionedTable

  OVERSIZE_REQUEST_DATA = { 'oversize' => true }.freeze
  MAX_RECENT_DAYS = 7

  attr_accessor :interpolated_url

  self.primary_key = :id

  partitioned_by :created_at, strategy: :monthly, retain_for: 1.month

  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

  before_save :obfuscate_basic_auth
  before_save :redact_user_emails
  before_save :set_url_hash, if: -> { interpolated_url.present? }

  scope :by_status_code, ->(status_code) { where(response_status: status_code) }

  def self.recent(number_of_days = 2)
    if number_of_days > MAX_RECENT_DAYS
      raise ArgumentError,
        "`recent` scope can only provide up to #{MAX_RECENT_DAYS} days of log records"
    end

    where(created_at: number_of_days.days.ago.beginning_of_day..Time.zone.now)
      .order(created_at: :desc)
  end

  # 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

  def success?
    response_status =~ /^2/
  end

  def internal_error?
    response_status == WebHookService::InternalErrorResponse::ERROR_MESSAGE
  end

  def oversize?
    request_data == OVERSIZE_REQUEST_DATA
  end

  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

  def idempotency_key
    self[:request_headers]['Idempotency-Key']
  end

  def url_current?
    # URL hash hasn't been set, so we must assume there's no prior value to
    # compare to.
    return true if url_hash.nil?

    Gitlab::CryptoHelper.sha256(web_hook.interpolated_url) == url_hash
  end

  private

  def obfuscate_basic_auth
    self.url = Gitlab::UrlSanitizer.sanitize_masked_url(url)
  end

  def redact_user_emails
    self.request_data.deep_transform_values! do |value|
      URI::MailTo::EMAIL_REGEXP.match?(value.to_s) ? _('[REDACTED]') : value
    end
  end

  def set_url_hash
    self.url_hash = Gitlab::CryptoHelper.sha256(interpolated_url)
  end
end