Fix 2411 on branch 1.1.0 (#2431)

* fixes #2411
This commit is contained in:
Daniel Jiang 2017-06-06 13:41:43 +08:00 committed by GitHub
parent 0b02231093
commit b3b95fbf7b
2 changed files with 23 additions and 7 deletions

View File

@ -16,12 +16,14 @@ package token
import ( import (
"fmt" "fmt"
"net/http"
"net/url"
"strings"
"github.com/docker/distribution/registry/auth/token" "github.com/docker/distribution/registry/auth/token"
"github.com/vmware/harbor/src/common/dao" "github.com/vmware/harbor/src/common/dao"
"github.com/vmware/harbor/src/common/utils/log" "github.com/vmware/harbor/src/common/utils/log"
"github.com/vmware/harbor/src/ui/config" "github.com/vmware/harbor/src/ui/config"
"net/http"
"strings"
) )
var creatorMap map[string]Creator var creatorMap map[string]Creator
@ -200,11 +202,7 @@ func (e *unauthorizedError) Error() string {
func (g generalCreator) Create(r *http.Request) (*tokenJSON, error) { func (g generalCreator) Create(r *http.Request) (*tokenJSON, error) {
var user *userInfo var user *userInfo
var err error var err error
var scopes []string scopes := parseScopes(r.URL)
scopeParm := r.URL.Query()["scope"]
if len(scopeParm) > 0 {
scopes = strings.Split(r.URL.Query()["scope"][0], " ")
}
log.Debugf("scopes: %v", scopes) log.Debugf("scopes: %v", scopes)
for _, v := range g.validators { for _, v := range g.validators {
user, err = v.validate(r) user, err = v.validate(r)
@ -228,3 +226,12 @@ func (g generalCreator) Create(r *http.Request) (*tokenJSON, error) {
} }
return makeToken(user.name, g.service, access) return makeToken(user.name, g.service, access)
} }
func parseScopes(u *url.URL) []string {
var sector string
var result []string
for _, sector = range u.Query()["scope"] {
result = append(result, strings.Split(sector, " ")...)
}
return result
}

View File

@ -25,6 +25,7 @@ import (
"github.com/vmware/harbor/src/common/utils/test" "github.com/vmware/harbor/src/common/utils/test"
"github.com/vmware/harbor/src/ui/config" "github.com/vmware/harbor/src/ui/config"
"io/ioutil" "io/ioutil"
"net/url"
"os" "os"
"path" "path"
"runtime" "runtime"
@ -228,3 +229,11 @@ func TestFilterAccess(t *testing.T) {
assert.Nil(t, err, "Unexpected error: %v", err) assert.Nil(t, err, "Unexpected error: %v", err)
assert.Equal(t, ra2, *a3[0], "Mismatch after registry filter Map") assert.Equal(t, ra2, *a3[0], "Mismatch after registry filter Map")
} }
func TestParseScopes(t *testing.T) {
assert := assert.New(t)
u1 := "/service/token?account=admin&scope=repository%3Alibrary%2Fregistry%3Apush%2Cpull&scope=repository%3Ahello-world%2Fregistry%3Apull&service=harbor-registry"
r1, _ := url.Parse(u1)
l1 := parseScopes(r1)
assert.Equal([]string{"repository:library/registry:push,pull", "repository:hello-world/registry:pull"}, l1)
}