fix bug in parsing scope

This commit is contained in:
Wenkai Yin 2017-08-11 14:08:50 +08:00
parent 9b6ad0e90f
commit 98d8b7e246
2 changed files with 52 additions and 16 deletions

View File

@ -53,16 +53,21 @@ func GetResourceActions(scopes []string) []*token.ResourceActions {
items := strings.Split(s, ":") items := strings.Split(s, ":")
length := len(items) length := len(items)
typee := items[0] typee := ""
name := "" name := ""
if length > 1 {
name = items[1]
}
actions := []string{} actions := []string{}
if length > 2 {
actions = strings.Split(items[2], ",") if length == 1 {
typee = items[0]
} else if length == 2 {
typee = items[0]
name = items[1]
} else {
typee = items[0]
name = strings.Join(items[1:length-1], ":")
if len(items[length-1]) > 0 {
actions = strings.Split(items[length-1], ",")
}
} }
res = append(res, &token.ResourceActions{ res = append(res, &token.ResourceActions{

View File

@ -55,22 +55,53 @@ func TestMain(m *testing.M) {
} }
func TestGetResourceActions(t *testing.T) { func TestGetResourceActions(t *testing.T) {
s := []string{"registry:catalog:*", "repository:10.117.4.142/notary-test/hello-world-2:pull,push"} cases := map[string]*token.ResourceActions{
expectedRA := [2]token.ResourceActions{ "::": &token.ResourceActions{
token.ResourceActions{ Type: "",
Name: "",
Actions: []string{},
},
"repository": &token.ResourceActions{
Type: "repository",
Name: "",
Actions: []string{},
},
"repository:": &token.ResourceActions{
Type: "repository",
Name: "",
Actions: []string{},
},
"repository:library/hello-world": &token.ResourceActions{
Type: "repository",
Name: "library/hello-world",
Actions: []string{},
},
"repository:library/hello-world:": &token.ResourceActions{
Type: "repository",
Name: "library/hello-world",
Actions: []string{},
},
"repository:library/hello-world:pull,push": &token.ResourceActions{
Type: "repository",
Name: "library/hello-world",
Actions: []string{"pull", "push"},
},
"registry:catalog:*": &token.ResourceActions{
Type: "registry", Type: "registry",
Name: "catalog", Name: "catalog",
Actions: []string{"*"}, Actions: []string{"*"},
}, },
token.ResourceActions{ "repository:192.168.0.1:443/library/hello-world:pull,push": &token.ResourceActions{
Type: "repository", Type: "repository",
Name: "10.117.4.142/notary-test/hello-world-2", Name: "192.168.0.1:443/library/hello-world",
Actions: []string{"pull", "push"}, Actions: []string{"pull", "push"},
}, },
} }
ra := GetResourceActions(s)
assert.Equal(t, *ra[0], expectedRA[0], "The Resource Action mismatch") for k, v := range cases {
assert.Equal(t, *ra[1], expectedRA[1], "The Resource Action mismatch") r := GetResourceActions([]string{k})[0]
assert.EqualValues(t, v, r)
}
} }
func getKeyAndCertPath() (string, string) { func getKeyAndCertPath() (string, string) {