lib/docker/remote/client.rb in docker-remote-0.2.0 vs lib/docker/remote/client.rb in docker-remote-0.3.0

- old
+ new

@@ -2,10 +2,14 @@ require 'net/http' require 'uri' module Docker module Remote + class DockerRemoteError < StandardError; end + class UnsupportedVersionError < DockerRemoteError; end + class UnexpectedResponseCodeError < DockerRemoteError; end + class Client include Utils attr_reader :registry_url, :repo, :username, :password @@ -41,38 +45,58 @@ def auth @auth ||= begin request = Net::HTTP::Get.new('/v2/') response = registry_http.request(request) - auth = response['www-authenticate'] - idx = auth.index(' ') - auth_type = auth[0..idx].strip - - params = auth[idx..-1].split(',').each_with_object({}) do |param, ret| - key, value = param.split('=') - ret[key.strip] = value.strip[1..-2] # remove quotes - end - - case auth_type.downcase - when 'bearer' - BearerAuth.new(params, repo, username, password) - when 'basic' - BasicAuth.new(username, password) + case response.code + when '200' + NoAuth.instance + when '401' + www_auth(response) + when '404' + raise UnsupportedVersionError, + "the registry at #{registry_url} doesn't support v2 "\ + 'of the Docker registry API' else - raise UnsupportedAuthTypeError, "unsupported Docker auth type '#{auth_type}'" + raise UnexpectedResponseCodeError, + "the registry at #{registry_url} responded with an "\ + "unexpected HTTP status code of #{response.code}" end end end + def www_auth(response) + auth = response['www-authenticate'] + + idx = auth.index(' ') + auth_type = auth[0..idx].strip + + params = auth[idx..-1].split(',').each_with_object({}) do |param, ret| + key, value = param.split('=') + ret[key.strip] = value.strip[1..-2] # remove quotes + end + + case auth_type.downcase + when 'bearer' + BearerAuth.new(params, repo, username, password) + when 'basic' + BasicAuth.new(username, password) + else + raise UnsupportedAuthTypeError, + "unsupported Docker auth type '#{auth_type}'" + end + end + def registry_uri @registry_uri ||= URI.parse(registry_url) end def registry_http - @registry_http ||= Net::HTTP.new(registry_uri.host, registry_uri.port).tap do |http| - http.use_ssl = true if registry_uri.scheme == 'https' - end + @registry_http ||= + Net::HTTP.new(registry_uri.host, registry_uri.port).tap do |http| + http.use_ssl = true if registry_uri.scheme == 'https' + end end end end end