diff --git a/src/ui/service/token/authutils.go b/src/ui/service/token/authutils.go index b7d35d251..22069fd89 100644 --- a/src/ui/service/token/authutils.go +++ b/src/ui/service/token/authutils.go @@ -53,16 +53,21 @@ func GetResourceActions(scopes []string) []*token.ResourceActions { items := strings.Split(s, ":") length := len(items) - typee := items[0] - + typee := "" name := "" - if length > 1 { - name = items[1] - } - 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{ diff --git a/src/ui/service/token/token_test.go b/src/ui/service/token/token_test.go index c9029abbb..7410b506f 100644 --- a/src/ui/service/token/token_test.go +++ b/src/ui/service/token/token_test.go @@ -55,22 +55,53 @@ func TestMain(m *testing.M) { } func TestGetResourceActions(t *testing.T) { - s := []string{"registry:catalog:*", "repository:10.117.4.142/notary-test/hello-world-2:pull,push"} - expectedRA := [2]token.ResourceActions{ - token.ResourceActions{ + cases := map[string]*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", Name: "catalog", Actions: []string{"*"}, }, - token.ResourceActions{ + "repository:192.168.0.1:443/library/hello-world:pull,push": &token.ResourceActions{ 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"}, }, } - ra := GetResourceActions(s) - assert.Equal(t, *ra[0], expectedRA[0], "The Resource Action mismatch") - assert.Equal(t, *ra[1], expectedRA[1], "The Resource Action mismatch") + + for k, v := range cases { + r := GetResourceActions([]string{k})[0] + assert.EqualValues(t, v, r) + } } func getKeyAndCertPath() (string, string) {