From 4316ae2ed33d158e1d8d994646a75e25a70d9320 Mon Sep 17 00:00:00 2001 From: Antonio Murdaca Date: Fri, 22 Apr 2016 16:54:32 +0200 Subject: [PATCH] api: server: check for unauthorized error This functionality has been fixed by 7bca93218291767c5dd8782de0ad630dbcda9995 but then it has been broken again by a793564b2591035aec5412fbcbcccf220c773a4c and finally refixed here. Basically the functionality was to prompt for login when trying to pull from the official docker hub. Signed-off-by: Antonio Murdaca --- api/server/httputils/errors.go | 1 + api/server/router/image/image_routes.go | 22 ---------------------- 2 files changed, 1 insertion(+), 22 deletions(-) diff --git a/api/server/httputils/errors.go b/api/server/httputils/errors.go index 92b615b8fc..5f0f5caa52 100644 --- a/api/server/httputils/errors.go +++ b/api/server/httputils/errors.go @@ -52,6 +52,7 @@ func GetHTTPErrorStatusCode(err error) int { "conflict": http.StatusConflict, "impossible": http.StatusNotAcceptable, "wrong login/password": http.StatusUnauthorized, + "unauthorized": http.StatusUnauthorized, "hasn't been activated": http.StatusForbidden, } { if strings.Contains(errStr, keyword) { diff --git a/api/server/router/image/image_routes.go b/api/server/router/image/image_routes.go index f21c22835b..b9168bc89d 100644 --- a/api/server/router/image/image_routes.go +++ b/api/server/router/image/image_routes.go @@ -6,10 +6,8 @@ import ( "fmt" "io" "net/http" - "net/url" "strings" - "github.com/docker/distribution/registry/api/errcode" "github.com/docker/docker/api/server/httputils" "github.com/docker/docker/api/types/backend" "github.com/docker/docker/pkg/ioutils" @@ -106,13 +104,6 @@ func (s *imageRouter) postImagesCreate(ctx context.Context, w http.ResponseWrite } err = s.backend.PullImage(ctx, image, tag, metaHeaders, authConfig, output) - - // Check the error from pulling an image to make sure the request - // was authorized. Modify the status if the request was - // unauthorized to respond with 401 rather than 500. - if err != nil && isAuthorizedError(err) { - err = errcode.ErrorCodeUnauthorized.WithMessage(fmt.Sprintf("Authentication is required: %s", err)) - } } else { //import src := r.Form.Get("fromSrc") // 'err' MUST NOT be defined within this block, we need any error @@ -316,16 +307,3 @@ func (s *imageRouter) getImagesSearch(ctx context.Context, w http.ResponseWriter } return httputils.WriteJSON(w, http.StatusOK, query.Results) } - -func isAuthorizedError(err error) bool { - if urlError, ok := err.(*url.Error); ok { - err = urlError.Err - } - - if dError, ok := err.(errcode.Error); ok { - if dError.ErrorCode() == errcode.ErrorCodeUnauthorized { - return true - } - } - return false -}