2021-02-22 17:27:13 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DependencyProxy
|
2022-01-26 12:08:38 +05:30
|
|
|
class FindCachedManifestService < DependencyProxy::BaseService
|
2021-02-22 17:27:13 +05:30
|
|
|
def initialize(group, image, tag, token)
|
|
|
|
@group = group
|
|
|
|
@image = image
|
|
|
|
@tag = tag
|
|
|
|
@token = token
|
|
|
|
@file_name = "#{@image}:#{@tag}.json"
|
|
|
|
@manifest = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
@manifest = @group.dependency_proxy_manifests
|
2021-11-18 22:05:49 +05:30
|
|
|
.active
|
2021-12-11 22:18:48 +05:30
|
|
|
.find_by_file_name_or_digest(file_name: @file_name, digest: @tag)
|
2021-02-22 17:27:13 +05:30
|
|
|
|
|
|
|
head_result = DependencyProxy::HeadManifestService.new(@image, @tag, @token).execute
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
return respond if cached_manifest_matches?(head_result)
|
2023-01-13 00:05:48 +05:30
|
|
|
return respond if @manifest && head_result[:status] == :error
|
2021-11-18 22:05:49 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
success(manifest: nil, from_cache: false)
|
2021-02-22 17:27:13 +05:30
|
|
|
rescue Timeout::Error, *Gitlab::HTTP::HTTP_ERRORS
|
|
|
|
respond
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def cached_manifest_matches?(head_result)
|
2021-04-17 20:07:23 +05:30
|
|
|
return false if head_result[:status] == :error
|
|
|
|
|
|
|
|
@manifest && @manifest.digest == head_result[:digest] && @manifest.content_type == head_result[:content_type]
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
def respond(from_cache: true)
|
2021-12-11 22:18:48 +05:30
|
|
|
if @manifest
|
|
|
|
@manifest.read!
|
2021-11-18 22:05:49 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
success(manifest: @manifest, from_cache: from_cache)
|
2021-02-22 17:27:13 +05:30
|
|
|
else
|
|
|
|
error('Failed to download the manifest from the external registry', 503)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|