Skip to content

Commit

Permalink
Merge pull request moby#22253 from runcom/restore-bug
Browse files Browse the repository at this point in the history
api: server: check for unauthorized error
  • Loading branch information
calavera committed Apr 22, 2016
2 parents 717f0c9 + 4316ae2 commit 8c17d58
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 22 deletions.
1 change: 1 addition & 0 deletions api/server/httputils/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 0 additions & 22 deletions api/server/router/image/image_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 8c17d58

Please sign in to comment.