diff --git a/dao/role.go b/dao/role.go index 43062a1..8bd9b9e 100644 --- a/dao/role.go +++ b/dao/role.go @@ -58,6 +58,10 @@ func IsAdminRole(userIDOrUsername interface{}) (bool, error) { return false, fmt.Errorf("invalid parameter, only int and string are supported: %v", userIDOrUsername) } + if len(u.Username) == 0 { + return false, nil + } + user, err := GetUser(u) if err != nil { return false, err diff --git a/utils/registry/auth/handler.go b/utils/registry/auth/handler.go index de1e22b..4d1e609 100644 --- a/utils/registry/auth/handler.go +++ b/utils/registry/auth/handler.go @@ -19,12 +19,14 @@ import ( "encoding/json" "errors" "fmt" + "io/ioutil" "net/http" "net/url" "strings" token_util "github.com/vmware/harbor/service/token" "github.com/vmware/harbor/utils/log" + "github.com/vmware/harbor/utils/registry/errors" ) // Handler authorizes the request when encounters a 401 error @@ -109,12 +111,19 @@ func (t *standardTokenHandler) AuthorizeRequest(req *http.Request, params map[st return err } - if resp.StatusCode != http.StatusOK { - return fmt.Errorf("error occured when get token from %s, status code: %d, status info: %s", - realm, resp.StatusCode, resp.Status) + defer resp.Body.Close() + + b, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err } - defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return error.Error{ + StatusCode: resp.StatusCode, + Message: string(b), + } + } decoder := json.NewDecoder(resp.Body) diff --git a/utils/registry/error.go b/utils/registry/error.go deleted file mode 100644 index dda4e44..0000000 --- a/utils/registry/error.go +++ /dev/null @@ -1,38 +0,0 @@ -/* - Copyright (c) 2016 VMware, Inc. All Rights Reserved. - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package registry - -import ( - "fmt" -) - -// Error : if response's status code is not 200 or does not meet requirement, -// an Error instance will be returned -type Error struct { - StatusCode int - Message string -} - -// Error ... -func (e Error) Error() string { - return fmt.Sprintf("%d %s", e.StatusCode, e.Message) -} - -// ParseError parses err, if err is type Error, convert it to Error -func ParseError(err error) (Error, bool) { - e, ok := err.(Error) - return e, ok -}