Commit aa8d3dd8 authored by Vitali Tatarintev's avatar Vitali Tatarintev

Merge branch '259619-md5-hexdigest-on-checksummable' into 'master'

Add md5_hexdigest to compute MD5 hashes for files

See merge request gitlab-org/gitlab!71178
parents 1c2f4bae 6147b0dc
......@@ -8,8 +8,12 @@ module Checksummable
Zlib.crc32(data)
end
def hexdigest(path)
def sha256_hexdigest(path)
::Digest::SHA256.file(path).hexdigest
end
def md5_hexdigest(path)
::Digest::MD5.file(path).hexdigest
end
end
end
......@@ -49,7 +49,7 @@ class LfsObject < ApplicationRecord
end
def self.calculate_oid(path)
self.hexdigest(path)
self.sha256_hexdigest(path)
end
end
......
......@@ -67,7 +67,7 @@ class Upload < ApplicationRecord
self.checksum = nil
return unless needs_checksum?
self.checksum = self.class.hexdigest(absolute_path)
self.checksum = self.class.sha256_hexdigest(absolute_path)
end
# Initialize the associated Uploader class with current model
......
......@@ -84,7 +84,7 @@ module Geo
def calculate_checksum
raise 'File is not checksummable' unless checksummable?
model.hexdigest(blob_path)
model.sha256_hexdigest(blob_path)
end
# Returns whether the file exists on disk or in remote storage
......
......@@ -36,7 +36,7 @@ module Gitlab
# Remove this when we implement checksums for files on the Object Storage
return true unless recorded_file.local?
extra_params[:checksum] == Upload.hexdigest(recorded_file.absolute_path)
extra_params[:checksum] == Upload.sha256_hexdigest(recorded_file.absolute_path)
end
end
end
......
......@@ -3,7 +3,7 @@ require 'spec_helper'
RSpec.describe Gitlab::Geo::Replication::BlobRetriever, :aggregate_failures do
let(:package_file) { create(:package_file, :npm) }
let(:package_checksum) { package_file.class.hexdigest(package_file.file.path) }
let(:package_checksum) { package_file.class.sha256_hexdigest(package_file.file.path) }
let(:replicator_class) { Geo::PackageFileReplicator }
let(:replicator) { replicator_class.new(model_record_id: package_file.id) }
......
......@@ -207,7 +207,7 @@ RSpec.shared_examples 'a blob replicator' do
context 'when the file is locally stored' do
context 'when the file exists' do
it 'returns hexdigest of the file' do
expected = described_class.model.hexdigest(subject.blob_path)
expected = described_class.model.sha256_hexdigest(subject.blob_path)
expect(subject.calculate_checksum).to eq(expected)
end
......
......@@ -261,7 +261,7 @@ module Gitlab
project: job.project,
file_type: :trace,
file: stream,
file_sha256: self.class.hexdigest(path))
file_sha256: self.class.sha256_hexdigest(path))
trace_metadata.track_archival!(trace_artifact.id)
end
......
......@@ -28,7 +28,7 @@ module Gitlab
end
def actual_checksum(upload)
Upload.hexdigest(upload.absolute_path)
Upload.sha256_hexdigest(upload.absolute_path)
end
def remote_object_exists?(upload)
......
......@@ -13,11 +13,19 @@ RSpec.describe Checksummable do
end
end
describe ".hexdigest" do
describe ".sha256_hexdigest" do
it 'returns the SHA256 sum of the file' do
expected = Digest::SHA256.file(__FILE__).hexdigest
expect(subject.hexdigest(__FILE__)).to eq(expected)
expect(subject.sha256_hexdigest(__FILE__)).to eq(expected)
end
end
describe ".md5_hexdigest" do
it 'returns the MD5 sum of the file' do
expected = Digest::MD5.file(__FILE__).hexdigest
expect(subject.md5_hexdigest(__FILE__)).to eq(expected)
end
end
end
......@@ -497,7 +497,7 @@ RSpec.shared_examples 'trace with disabled live trace feature' do
expect(build.job_artifacts_trace.file.filename).to eq('job.log')
expect(File.exist?(src_path)).to be_falsy
expect(src_checksum)
.to eq(described_class.hexdigest(build.job_artifacts_trace.file.path))
.to eq(described_class.sha256_hexdigest(build.job_artifacts_trace.file.path))
expect(build.job_artifacts_trace.file_sha256).to eq(src_checksum)
end
end
......@@ -523,7 +523,7 @@ RSpec.shared_examples 'trace with disabled live trace feature' do
expect(build.job_artifacts_trace.file.filename).to eq('job.log')
expect(build.old_trace).to be_nil
expect(src_checksum)
.to eq(described_class.hexdigest(build.job_artifacts_trace.file.path))
.to eq(described_class.sha256_hexdigest(build.job_artifacts_trace.file.path))
expect(build.job_artifacts_trace.file_sha256).to eq(src_checksum)
end
end
......@@ -861,7 +861,7 @@ RSpec.shared_examples 'trace with enabled live trace feature' do
expect(build.job_artifacts_trace.file.filename).to eq('job.log')
expect(Ci::BuildTraceChunk.where(build: build)).not_to be_exist
expect(src_checksum)
.to eq(described_class.hexdigest(build.job_artifacts_trace.file.path))
.to eq(described_class.sha256_hexdigest(build.job_artifacts_trace.file.path))
expect(build.job_artifacts_trace.file_sha256).to eq(src_checksum)
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment