debian-mirror-gitlab/lib/gitlab/ci/artifact_file_reader.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

71 lines
2.2 KiB
Ruby
Raw Permalink Normal View History

2020-04-08 14:13:33 +05:30
# frozen_string_literal: true
# This class takes in input a Ci::Build object and an artifact path to read.
# It downloads and extracts the artifacts archive, then returns the content
# of the artifact, if found.
module Gitlab
module Ci
class ArtifactFileReader
Error = Class.new(StandardError)
MAX_ARCHIVE_SIZE = 5.megabytes
2023-02-26 17:17:37 +05:30
TMP_ARTIFACT_EXTRACTION_DIR = "extracted_artifacts_job_%{id}"
2020-04-08 14:13:33 +05:30
def initialize(job)
@job = job
raise ArgumentError, 'Job does not have artifacts' unless @job.artifacts?
validate!
end
def read(path)
return unless job.artifacts_metadata
metadata_entry = job.artifacts_metadata_entry(path)
if metadata_entry.total_size > MAX_ARCHIVE_SIZE
raise Error, "Artifacts archive for job `#{job.name}` is too large: max #{max_archive_size_in_mb}"
end
read_zip_file!(path)
end
private
attr_reader :job
def validate!
if job.job_artifacts_archive.size > MAX_ARCHIVE_SIZE
raise Error, "Artifacts archive for job `#{job.name}` is too large: max #{max_archive_size_in_mb}"
end
unless job.artifacts_metadata?
raise Error, "Job `#{job.name}` has missing artifacts metadata and cannot be extracted!"
end
end
def read_zip_file!(file_path)
2023-02-26 17:17:37 +05:30
dir_name = format(TMP_ARTIFACT_EXTRACTION_DIR, id: job.id.to_i)
2020-11-24 15:15:51 +05:30
2023-02-26 17:17:37 +05:30
job.artifacts_file.use_open_file(unlink_early: false) do |tmp_open_file|
Dir.mktmpdir(dir_name) do |tmp_dir|
SafeZip::Extract.new(tmp_open_file.file_path).extract(files: [file_path], to: tmp_dir)
File.read(File.join(tmp_dir, file_path))
2020-11-24 15:15:51 +05:30
end
end
2023-02-26 17:17:37 +05:30
rescue SafeZip::Extract::NoMatchingError
raise Error, "Path `#{file_path}` does not exist inside the `#{job.name}` artifacts archive!"
rescue SafeZip::Extract::EntrySizeError
raise Error, "Path `#{file_path}` has invalid size in the zip!"
rescue Errno::EISDIR
raise Error, "Path `#{file_path}` was expected to be a file but it was a directory!"
2020-11-24 15:15:51 +05:30
end
2020-04-08 14:13:33 +05:30
def max_archive_size_in_mb
ActiveSupport::NumberHelper.number_to_human_size(MAX_ARCHIVE_SIZE)
end
end
end
end