Welcome to mirror list, hosted at ThFree Co, Russian Federation.

template_helpers.rb « ci « helpers « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1818dec5fc7b3b90d638c7973a6fd7e91d50472d (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# frozen_string_literal: true

module Ci
  module TemplateHelpers
    def template_registry_host
      'registry.gitlab.com'
    end

    def auto_build_image_repository
      "gitlab-org/cluster-integration/auto-build-image"
    end

    def public_image_exist?(registry, repository, image)
      public_image_manifest(registry, repository, image).present?
    end

    def public_image_manifest(registry, repository, reference)
      token = public_image_repository_token(registry, repository)

      headers = {
        'Authorization' => "Bearer #{token}",
        'Accept' => 'application/vnd.docker.distribution.manifest.list.v2+json, application/vnd.oci.image.index.v1+json'
      }
      response = with_net_connect_allowed do
        Gitlab::HTTP.get(image_manifest_url(registry, repository, reference), headers: headers)
      end

      if response.success?
        Gitlab::Json.parse(response.body)
      elsif response.not_found?
        nil
      else
        raise "Could not retrieve manifest: #{response.body}"
      end
    end

    def public_image_repository_token(registry, repository)
      @public_image_repository_tokens ||= {}
      @public_image_repository_tokens[[registry, repository]] ||=
        begin
          response = with_net_connect_allowed do
            Gitlab::HTTP.get(image_manifest_url(registry, repository, 'latest'))
          end

          raise 'Unauthorized' unless response.unauthorized?

          www_authenticate = response.headers['www-authenticate']
          raise 'Missing www-authenticate' unless www_authenticate

          realm, service, scope = www_authenticate.split(',').map { |s| s[/\w+="(.*)"/, 1] }
          token_response = with_net_connect_allowed do
            Gitlab::HTTP.get(realm, query: { service: service, scope: scope })
          end

          raise "Could not get token: #{token_response.body}" unless token_response.success?

          token_response['token']
        end
    end

    def image_manifest_url(registry, repository, reference)
      "#{registry}/v2/#{repository}/manifests/#{reference}"
    end
  end
end

Ci::TemplateHelpers.prepend_mod