summaryrefslogtreecommitdiff
path: root/app/services/dependency_proxy/pull_manifest_service.rb
blob: 31494773cc0dbde56529ddf1cbc2f1e58a5b81b3 (plain)
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
# frozen_string_literal: true

module DependencyProxy
  class PullManifestService < DependencyProxy::BaseService
    def initialize(image, tag, token)
      @image = image
      @tag = tag
      @token = token
    end

    def execute_with_manifest
      raise ArgumentError, 'Block must be provided' unless block_given?

      response = Gitlab::HTTP.get(manifest_url, headers: auth_headers.merge(Accept: ::ContainerRegistry::Client::ACCEPTED_TYPES.join(',')))

      if response.success?
        file = Tempfile.new

        begin
          file.write(response.body)
          file.flush

          yield(success(file: file, digest: response.headers['docker-content-digest'], content_type: response.headers['content-type']))
        ensure
          file.close
          file.unlink
        end
      else
        yield(error(response.body, response.code))
      end
    rescue Timeout::Error => exception
      error(exception.message, 599)
    end

    private

    def manifest_url
      registry.manifest_url(@image, @tag)
    end
  end
end