2018-11-19 21:01:13 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-27 14:15:25 -05:00
|
|
|
module Gitlab
|
|
|
|
module Verify
|
|
|
|
class BatchVerifier
|
|
|
|
attr_reader :batch_size, :start, :finish
|
|
|
|
|
|
|
|
def initialize(batch_size:, start: nil, finish: nil)
|
|
|
|
@batch_size = batch_size
|
|
|
|
@start = start
|
|
|
|
@finish = finish
|
2018-06-13 13:11:43 -04:00
|
|
|
|
|
|
|
fix_google_api_logger
|
2018-02-27 14:15:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Yields a Range of IDs and a Hash of failed verifications (object => error)
|
|
|
|
def run_batches(&blk)
|
2018-06-13 13:11:43 -04:00
|
|
|
all_relation.in_batches(of: batch_size, start: start, finish: finish) do |batch| # rubocop: disable Cop/InBatches
|
|
|
|
range = batch.first.id..batch.last.id
|
|
|
|
failures = run_batch_for(batch)
|
2018-02-27 14:15:25 -05:00
|
|
|
|
|
|
|
yield(range, failures)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
2021-05-04 11:10:36 -04:00
|
|
|
raise NotImplementedError
|
2018-02-27 14:15:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def describe(_object)
|
2021-05-04 11:10:36 -04:00
|
|
|
raise NotImplementedError
|
2018-02-27 14:15:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-06-13 13:11:43 -04:00
|
|
|
def run_batch_for(batch)
|
|
|
|
batch.map { |upload| verify(upload) }.compact.to_h
|
2018-02-27 14:15:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def verify(object)
|
2018-06-13 13:11:43 -04:00
|
|
|
local?(object) ? verify_local(object) : verify_remote(object)
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => err
|
2018-06-13 13:11:43 -04:00
|
|
|
failure(object, err.inspect)
|
|
|
|
end
|
|
|
|
|
|
|
|
def verify_local(object)
|
2018-02-27 14:15:25 -05:00
|
|
|
expected = expected_checksum(object)
|
|
|
|
actual = actual_checksum(object)
|
|
|
|
|
2018-06-13 13:11:43 -04:00
|
|
|
return failure(object, 'Checksum missing') unless expected.present?
|
|
|
|
return failure(object, 'Checksum mismatch') unless expected == actual
|
|
|
|
|
|
|
|
success
|
|
|
|
end
|
2018-02-27 14:15:25 -05:00
|
|
|
|
2018-06-13 13:11:43 -04:00
|
|
|
# We don't calculate checksum for remote objects, so just check existence
|
|
|
|
def verify_remote(object)
|
|
|
|
return failure(object, 'Remote object does not exist') unless remote_object_exists?(object)
|
|
|
|
|
|
|
|
success
|
|
|
|
end
|
|
|
|
|
|
|
|
def success
|
2018-02-27 14:15:25 -05:00
|
|
|
nil
|
2018-06-13 13:11:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def failure(object, message)
|
|
|
|
[object, message]
|
|
|
|
end
|
|
|
|
|
|
|
|
# It's already set to Logger::INFO, but acts as if it is set to
|
|
|
|
# Logger::DEBUG, and this fixes it...
|
|
|
|
def fix_google_api_logger
|
2020-07-06 05:09:20 -04:00
|
|
|
require 'google/apis'
|
|
|
|
Google::Apis.logger.level = Logger::INFO
|
2018-02-27 14:15:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# This should return an ActiveRecord::Relation suitable for calling #in_batches on
|
2018-06-13 13:11:43 -04:00
|
|
|
def all_relation
|
2021-05-04 11:10:36 -04:00
|
|
|
raise NotImplementedError
|
2018-06-13 13:11:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Should return true if the object is stored locally
|
|
|
|
def local?(_object)
|
2021-05-04 11:10:36 -04:00
|
|
|
raise NotImplementedError
|
2018-02-27 14:15:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# The checksum we expect the object to have
|
|
|
|
def expected_checksum(_object)
|
2021-05-04 11:10:36 -04:00
|
|
|
raise NotImplementedError
|
2018-02-27 14:15:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# The freshly-recalculated checksum of the object
|
|
|
|
def actual_checksum(_object)
|
2021-05-04 11:10:36 -04:00
|
|
|
raise NotImplementedError
|
2018-02-27 14:15:25 -05:00
|
|
|
end
|
2018-06-13 13:11:43 -04:00
|
|
|
|
|
|
|
# Be sure to perform a hard check of the remote object (don't just check DB value)
|
|
|
|
def remote_object_exists?(object)
|
2021-05-04 11:10:36 -04:00
|
|
|
raise NotImplementedError
|
2018-06-13 13:11:43 -04:00
|
|
|
end
|
2018-02-27 14:15:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|