BigW Consortium Gitlab

client.rb 2.76 KB
Newer Older
1 2 3
require 'faraday'
require 'faraday_middleware'

4
module ContainerRegistry
5 6 7 8 9
  class Client
    attr_accessor :uri

    MANIFEST_VERSION = 'application/vnd.docker.distribution.manifest.v2+json'

10
    # Taken from: FaradayMiddleware::FollowRedirects
Kamil Trzcinski committed
11
    REDIRECT_CODES = Set.new [301, 302, 303, 307]
12

13 14
    def initialize(base_uri, options = {})
      @base_uri = base_uri
15
      @options = options
16 17 18
    end

    def repository_tags(name)
19
      response_body faraday.get("/v2/#{name}/tags/list")
20 21 22
    end

    def repository_manifest(name, reference)
23
      response_body faraday.get("/v2/#{name}/manifests/#{reference}")
24 25 26
    end

    def repository_tag_digest(name, reference)
27
      response = faraday.head("/v2/#{name}/manifests/#{reference}")
28 29 30 31
      response.headers['docker-content-digest'] if response.success?
    end

    def delete_repository_tag(name, reference)
32
      faraday.delete("/v2/#{name}/manifests/#{reference}").success?
33 34 35
    end

    def blob(name, digest, type = nil)
36 37
      type ||= 'application/octet-stream'
      response_body faraday_blob.get("/v2/#{name}/blobs/#{digest}", nil, 'Accept' => type), allow_redirect: true
38 39 40
    end

    def delete_blob(name, digest)
41
      faraday.delete("/v2/#{name}/blobs/#{digest}").success?
42
    end
43

Kamil Trzcinski committed
44
    private
45

46
    def initialize_connection(conn, options)
Kamil Trzcinski committed
47 48 49 50 51 52 53 54 55 56
      conn.request :json

      if options[:user] && options[:password]
        conn.request(:basic_auth, options[:user].to_s, options[:password].to_s)
      elsif options[:token]
        conn.request(:authorization, :bearer, options[:token].to_s)
      end

      conn.adapter :net_http
    end
Kamil Trzcinski committed
57

58 59 60 61 62 63 64 65 66
    def accept_manifest(conn)
      conn.headers['Accept'] = MANIFEST_VERSION

      conn.response :json, content_type: 'application/json'
      conn.response :json, content_type: 'application/vnd.docker.distribution.manifest.v1+prettyjws'
      conn.response :json, content_type: 'application/vnd.docker.distribution.manifest.v1+json'
      conn.response :json, content_type: 'application/vnd.docker.distribution.manifest.v2+json'
    end

67 68
    def response_body(response, allow_redirect: false)
      if allow_redirect && REDIRECT_CODES.include?(response.status)
69
        response = redirect_response(response.headers['location'])
70 71 72 73 74
      end

      response.body if response && response.success?
    end

75
    def redirect_response(location)
76 77
      return unless location

78 79 80 81 82
      # We explicitly remove authorization token
      faraday_blob.get(location) do |req|
        req['Authorization'] = ''
      end
    end
83

84 85 86 87 88
    def faraday
      @faraday ||= Faraday.new(@base_uri) do |conn|
        initialize_connection(conn, @options)
        accept_manifest(conn)
      end
89 90
    end

91 92 93 94
    def faraday_blob
      @faraday_blob ||= Faraday.new(@base_uri) do |conn|
        initialize_connection(conn, @options)
      end
Kamil Trzcinski committed
95
    end
96 97
  end
end