Browse code

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 <runcom@redhat.com>

Antonio Murdaca authored on 2016/04/22 23:54:32
Showing 2 changed files
... ...
@@ -52,6 +52,7 @@ func GetHTTPErrorStatusCode(err error) int {
52 52
 			"conflict":              http.StatusConflict,
53 53
 			"impossible":            http.StatusNotAcceptable,
54 54
 			"wrong login/password":  http.StatusUnauthorized,
55
+			"unauthorized":          http.StatusUnauthorized,
55 56
 			"hasn't been activated": http.StatusForbidden,
56 57
 		} {
57 58
 			if strings.Contains(errStr, keyword) {
... ...
@@ -6,10 +6,8 @@ import (
6 6
 	"fmt"
7 7
 	"io"
8 8
 	"net/http"
9
-	"net/url"
10 9
 	"strings"
11 10
 
12
-	"github.com/docker/distribution/registry/api/errcode"
13 11
 	"github.com/docker/docker/api/server/httputils"
14 12
 	"github.com/docker/docker/api/types/backend"
15 13
 	"github.com/docker/docker/pkg/ioutils"
... ...
@@ -106,13 +104,6 @@ func (s *imageRouter) postImagesCreate(ctx context.Context, w http.ResponseWrite
106 106
 		}
107 107
 
108 108
 		err = s.backend.PullImage(ctx, image, tag, metaHeaders, authConfig, output)
109
-
110
-		// Check the error from pulling an image to make sure the request
111
-		// was authorized. Modify the status if the request was
112
-		// unauthorized to respond with 401 rather than 500.
113
-		if err != nil && isAuthorizedError(err) {
114
-			err = errcode.ErrorCodeUnauthorized.WithMessage(fmt.Sprintf("Authentication is required: %s", err))
115
-		}
116 109
 	} else { //import
117 110
 		src := r.Form.Get("fromSrc")
118 111
 		// '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
316 316
 	}
317 317
 	return httputils.WriteJSON(w, http.StatusOK, query.Results)
318 318
 }
319
-
320
-func isAuthorizedError(err error) bool {
321
-	if urlError, ok := err.(*url.Error); ok {
322
-		err = urlError.Err
323
-	}
324
-
325
-	if dError, ok := err.(errcode.Error); ok {
326
-		if dError.ErrorCode() == errcode.ErrorCodeUnauthorized {
327
-			return true
328
-		}
329
-	}
330
-	return false
331
-}