-
Notifications
You must be signed in to change notification settings - Fork 83
Fix roletemplate.rules validation, added standard k8s checks #328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
andreas-kupries
merged 8 commits into
rancher:release/v0.5
from
andreas-kupries:ak-gh40584-rules-validation
Feb 2, 2024
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a13fbcb
refactor: moved globalrole.validateRules to common.ValidateRules
andreas-kupries c26d327
fix: added proper rule validation to roletemplate
andreas-kupries aa922f0
chore: updated roletemplae, globalrole documentation
andreas-kupries d7910fe
address comment: simplify unit tests, drop dependency on exact error …
andreas-kupries 1d29e93
address comment: simplify ValidateRules
andreas-kupries 9884b4f
address comment: drop superfluous CheckForVerbs
andreas-kupries 0b32a46
chore: extended integration tests to cover the new checks for resourc…
andreas-kupries 142fd5c
chore: ditto for global roles (previous was role templates)
andreas-kupries File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package common | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| rbacv1 "k8s.io/api/rbac/v1" | ||
| v1 "k8s.io/api/rbac/v1" | ||
| "k8s.io/apimachinery/pkg/util/validation/field" | ||
| ) | ||
|
|
||
| func TestValidateRules(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| gResource := "something-global" | ||
| nsResource := "something-namespaced" | ||
|
|
||
| gField := field.NewPath(gResource) | ||
| nsField := field.NewPath(nsResource) | ||
|
|
||
| // Note: The partial error message is prefixed with the resource name during test execution | ||
| tests := []struct { | ||
| name string // label for testcase | ||
| data rbacv1.PolicyRule // policy rule to be validated | ||
| err string // partial error returned for a resource, empty string -> no error | ||
| }{ | ||
| { | ||
| name: "ok", | ||
| data: rbacv1.PolicyRule{ | ||
| Verbs: []string{"*"}, | ||
| APIGroups: []string{""}, | ||
| Resources: []string{"*"}, | ||
| }, | ||
| err: "", | ||
| }, | ||
| { | ||
| name: "no-verbs", | ||
| data: rbacv1.PolicyRule{ | ||
| Verbs: []string{}, | ||
| APIGroups: []string{""}, | ||
| Resources: []string{"*"}, | ||
| }, | ||
| err: "[0].verbs: Required value: verbs must contain at least one value", | ||
andreas-kupries marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
| { | ||
| name: "no-api-groups", | ||
| data: rbacv1.PolicyRule{ | ||
| Verbs: []string{"*"}, | ||
| APIGroups: []string{}, | ||
| Resources: []string{"*"}, | ||
| }, | ||
| err: "[0].apiGroups: Required value: resource rules must supply at least one api group", | ||
| }, | ||
| { | ||
| name: "no-resources", | ||
| data: rbacv1.PolicyRule{ | ||
| Verbs: []string{"*"}, | ||
| APIGroups: []string{""}, | ||
| Resources: []string{}, | ||
| }, | ||
| err: "[0].resources: Required value: resource rules must supply at least one resource", | ||
| }, | ||
| } | ||
|
|
||
| for _, testcase := range tests { | ||
| t.Run("global/"+testcase.name, func(t *testing.T) { | ||
| err := ValidateRules([]v1.PolicyRule{ | ||
| testcase.data, | ||
| }, false, gField) | ||
| if testcase.err == "" { | ||
| require.NoError(t, err) | ||
| return | ||
| } | ||
| require.Error(t, err) | ||
| require.Equal(t, err.Error(), gResource+testcase.err) | ||
| }) | ||
| t.Run("namespaced/"+testcase.name, func(t *testing.T) { | ||
| err := ValidateRules([]v1.PolicyRule{ | ||
| testcase.data, | ||
| }, true, nsField) | ||
| if testcase.err == "" { | ||
| require.NoError(t, err) | ||
| return | ||
| } | ||
| require.Error(t, err) | ||
| require.Equal(t, err.Error(), nsResource+testcase.err) | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.