Skip to content

Commit

Permalink
validate nonResourceURL in create clusterrole
Browse files Browse the repository at this point in the history
  • Loading branch information
xilabao committed Aug 18, 2017
1 parent 2820b45 commit 42c41a0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
15 changes: 15 additions & 0 deletions pkg/kubectl/cmd/create_clusterrole.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cmd
import (
"fmt"
"io"
"strings"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -133,6 +134,20 @@ func (c *CreateClusterRoleOptions) Validate() error {
return fmt.Errorf("invalid verb: '%s' for nonResourceURL", v)
}
}

for _, nonResourceURL := range c.NonResourceURLs {
if nonResourceURL == "*" {
continue
}

if nonResourceURL == "" || !strings.HasPrefix(nonResourceURL, "/") {
return fmt.Errorf("nonResourceURL should start with /")
}

if strings.ContainsRune(nonResourceURL[:len(nonResourceURL)-1], '*') {
return fmt.Errorf("nonResourceURL only supports wildcard matches when '*' is at the end")
}
}
}

return nil
Expand Down
42 changes: 41 additions & 1 deletion pkg/kubectl/cmd/create_clusterrole_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,46 @@ func TestClusterRoleValidate(t *testing.T) {
},
expectErr: false,
},
"test-invalid-empty-non-resource-url": {
clusterRoleOptions: &CreateClusterRoleOptions{
CreateRoleOptions: &CreateRoleOptions{
Name: "my-clusterrole",
Verbs: []string{"create"},
},
NonResourceURLs: []string{""},
},
expectErr: true,
},
"test-invalid-non-resource-url": {
clusterRoleOptions: &CreateClusterRoleOptions{
CreateRoleOptions: &CreateRoleOptions{
Name: "my-clusterrole",
Verbs: []string{"create"},
},
NonResourceURLs: []string{"logs"},
},
expectErr: true,
},
"test-invalid-non-resource-url-with-*": {
clusterRoleOptions: &CreateClusterRoleOptions{
CreateRoleOptions: &CreateRoleOptions{
Name: "my-clusterrole",
Verbs: []string{"create"},
},
NonResourceURLs: []string{"/logs/*/"},
},
expectErr: true,
},
"test-invalid-non-resource-url-with-multiple-*": {
clusterRoleOptions: &CreateClusterRoleOptions{
CreateRoleOptions: &CreateRoleOptions{
Name: "my-clusterrole",
Verbs: []string{"create"},
},
NonResourceURLs: []string{"/logs*/*"},
},
expectErr: true,
},
"test-invalid-verb-for-non-resource-url": {
clusterRoleOptions: &CreateClusterRoleOptions{
CreateRoleOptions: &CreateRoleOptions{
Expand All @@ -397,7 +437,7 @@ func TestClusterRoleValidate(t *testing.T) {
},
},
},
NonResourceURLs: []string{"/logs/"},
NonResourceURLs: []string{"/logs/", "/logs/*"},
},
expectErr: false,
},
Expand Down

0 comments on commit 42c41a0

Please sign in to comment.