Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 83
✨ Make RBACPreAuthorizer collection verbs configurable#2539
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
openshift-merge-bot
merged 1 commit into
operator-framework:main
from
perdasilva:generic-preflightsMar 9, 2026
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
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
241 changes: 236 additions & 5 deletions
241 internal/operator-controller/authorization/rbac_test.go
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 |
|---|---|---|
| @@ -396,7 +396,7 @@ func setupFakeClient(role client.Object) client.Client { | ||
| func TestPreAuthorize_Success(t *testing.T) { | ||
| t.Run("preauthorize succeeds with no missing rbac rules", func(t *testing.T) { | ||
| fakeClient := setupFakeClient(privilegedClusterRole) | ||
| preAuth := NewRBACPreAuthorizer(fakeClient) | ||
| preAuth := NewRBACPreAuthorizer(fakeClient, WithClusterCollectionVerbs("list", "watch"), WithNamespacedCollectionVerbs("create")) | ||
| missingRules, err := preAuth.PreAuthorize(context.TODO(), testUser, strings.NewReader(testManifest)) | ||
perdasilva marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| require.NoError(t, err) | ||
| require.Equal(t, []ScopedPolicyRules{}, missingRules) | ||
| @@ -406,7 +406,7 @@ func TestPreAuthorize_Success(t *testing.T) { | ||
| func TestPreAuthorize_MissingRBAC(t *testing.T) { | ||
| t.Run("preauthorize fails and finds missing rbac rules", func(t *testing.T) { | ||
| fakeClient := setupFakeClient(limitedClusterRole) | ||
| preAuth := NewRBACPreAuthorizer(fakeClient) | ||
| preAuth := NewRBACPreAuthorizer(fakeClient, WithClusterCollectionVerbs("list", "watch"), WithNamespacedCollectionVerbs("create")) | ||
| missingRules, err := preAuth.PreAuthorize(context.TODO(), testUser, strings.NewReader(testManifest)) | ||
| require.NoError(t, err) | ||
| require.Equal(t, expectedSingleNamespaceMissingRules, missingRules) | ||
| @@ -416,7 +416,7 @@ func TestPreAuthorize_MissingRBAC(t *testing.T) { | ||
| func TestPreAuthorizeMultiNamespace_MissingRBAC(t *testing.T) { | ||
| t.Run("preauthorize fails and finds missing rbac rules in multiple namespaces", func(t *testing.T) { | ||
| fakeClient := setupFakeClient(limitedClusterRole) | ||
| preAuth := NewRBACPreAuthorizer(fakeClient) | ||
| preAuth := NewRBACPreAuthorizer(fakeClient, WithClusterCollectionVerbs("list", "watch"), WithNamespacedCollectionVerbs("create")) | ||
| missingRules, err := preAuth.PreAuthorize(context.TODO(), testUser, strings.NewReader(testManifestMultiNamespace)) | ||
| require.NoError(t, err) | ||
| require.Equal(t, expectedMultiNamespaceMissingRules, missingRules) | ||
| @@ -426,7 +426,7 @@ func TestPreAuthorizeMultiNamespace_MissingRBAC(t *testing.T) { | ||
| func TestPreAuthorize_CheckEscalation(t *testing.T) { | ||
| t.Run("preauthorize succeeds with no missing rbac rules", func(t *testing.T) { | ||
| fakeClient := setupFakeClient(escalatingClusterRole) | ||
| preAuth := NewRBACPreAuthorizer(fakeClient) | ||
| preAuth := NewRBACPreAuthorizer(fakeClient, WithClusterCollectionVerbs("list", "watch"), WithNamespacedCollectionVerbs("create")) | ||
| missingRules, err := preAuth.PreAuthorize(context.TODO(), testUser, strings.NewReader(testManifest)) | ||
| require.NoError(t, err) | ||
| require.Equal(t, []ScopedPolicyRules{}, missingRules) | ||
| @@ -436,7 +436,7 @@ func TestPreAuthorize_CheckEscalation(t *testing.T) { | ||
| func TestPreAuthorize_AdditionalRequiredPerms_MissingRBAC(t *testing.T) { | ||
| t.Run("preauthorize fails and finds missing rbac rules coming from the additional required permissions", func(t *testing.T) { | ||
| fakeClient := setupFakeClient(escalatingClusterRole) | ||
| preAuth := NewRBACPreAuthorizer(fakeClient) | ||
| preAuth := NewRBACPreAuthorizer(fakeClient, WithClusterCollectionVerbs("list", "watch"), WithNamespacedCollectionVerbs("create")) | ||
| missingRules, err := preAuth.PreAuthorize(context.TODO(), testUser, strings.NewReader(testManifest), func(user user.Info) []authorizer.AttributesRecord { | ||
| return []authorizer.AttributesRecord{ | ||
| { | ||
| @@ -465,6 +465,237 @@ func TestPreAuthorize_AdditionalRequiredPerms_MissingRBAC(t *testing.T) { | ||
| }) | ||
| } | ||
| func TestPreAuthorize_WithClusterCollectionVerbs(t *testing.T) { | ||
| // expectedNamespacedMissingRules are the missing rules expected in the "test-namespace" | ||
| // namespace regardless of cluster collection verb configuration. These come from object | ||
| // verbs (get, patch, update, delete), namespaced collection verbs (create), and the | ||
| // escalation check for the role/rolebinding in the manifest. | ||
| expectedNamespacedMissingRules := ScopedPolicyRules{ | ||
| Namespace: "test-namespace", | ||
| MissingRules: []rbacv1.PolicyRule{ | ||
| { | ||
| Verbs: []string{"create"}, | ||
| APIGroups: []string{"*"}, | ||
| Resources: []string{"certificates"}}, | ||
| { | ||
| Verbs: []string{"create"}, | ||
| APIGroups: []string{""}, | ||
| Resources: []string{"services"}}, | ||
| { | ||
| Verbs: []string{"create"}, | ||
| APIGroups: []string{"rbac.authorization.k8s.io"}, | ||
| Resources: []string{"rolebindings"}}, | ||
| { | ||
| Verbs: []string{"create"}, | ||
| APIGroups: []string{"rbac.authorization.k8s.io"}, | ||
| Resources: []string{"roles"}}, | ||
| { | ||
| Verbs: []string{"delete", "get", "patch", "update"}, | ||
| APIGroups: []string{""}, | ||
| Resources: []string{"services"}, | ||
| ResourceNames: []string{"test-service"}}, | ||
| { | ||
| Verbs: []string{"delete", "get", "patch", "update"}, | ||
| APIGroups: []string{"rbac.authorization.k8s.io"}, | ||
| Resources: []string{"rolebindings"}, | ||
| ResourceNames: []string{"test-extension-binding"}}, | ||
| { | ||
| Verbs: []string{"delete", "get", "patch", "update"}, | ||
| APIGroups: []string{"rbac.authorization.k8s.io"}, | ||
| Resources: []string{"roles"}, | ||
| ResourceNames: []string{"test-extension-role"}}, | ||
| { | ||
| Verbs: []string{"watch"}, | ||
| APIGroups: []string{"*"}, | ||
| Resources: []string{"serviceaccounts"}, | ||
| }, | ||
| }, | ||
| } | ||
| t.Run("no cluster collection verbs option omits cluster-scoped collection rules", func(t *testing.T) { | ||
| fakeClient := setupFakeClient(limitedClusterRole) | ||
| preAuth := NewRBACPreAuthorizer(fakeClient, WithNamespacedCollectionVerbs("create")) | ||
| missingRules, err := preAuth.PreAuthorize(context.TODO(), testUser, strings.NewReader(testManifest)) | ||
| require.NoError(t, err) | ||
| // With no cluster collection verbs, there should be no cluster-scoped (namespace="") missing rules | ||
| require.Equal(t, []ScopedPolicyRules{expectedNamespacedMissingRules}, missingRules) | ||
| }) | ||
| t.Run("cluster verbs option only checks those verbs at cluster scope", func(t *testing.T) { | ||
| fakeClient := setupFakeClient(limitedClusterRole) | ||
| preAuth := NewRBACPreAuthorizer(fakeClient, WithClusterCollectionVerbs("get", "patch", "update"), WithNamespacedCollectionVerbs("create")) | ||
| missingRules, err := preAuth.PreAuthorize(context.TODO(), testUser, strings.NewReader(testManifest)) | ||
| require.NoError(t, err) | ||
| require.Equal(t, []ScopedPolicyRules{ | ||
| { | ||
| Namespace: "", | ||
| MissingRules: []rbacv1.PolicyRule{ | ||
| { | ||
| Verbs: []string{"get", "patch", "update"}, | ||
| APIGroups: []string{""}, | ||
| Resources: []string{"services"}, | ||
| ResourceNames: []string(nil), | ||
| NonResourceURLs: []string(nil)}, | ||
| { | ||
| Verbs: []string{"get", "patch", "update"}, | ||
| APIGroups: []string{"rbac.authorization.k8s.io"}, | ||
| Resources: []string{"rolebindings"}, | ||
| ResourceNames: []string(nil), | ||
| NonResourceURLs: []string(nil)}, | ||
| { | ||
| Verbs: []string{"get", "patch", "update"}, | ||
| APIGroups: []string{"rbac.authorization.k8s.io"}, | ||
| Resources: []string{"roles"}, | ||
| ResourceNames: []string(nil), | ||
| NonResourceURLs: []string(nil), | ||
| }, | ||
| }, | ||
| }, | ||
| expectedNamespacedMissingRules, | ||
| }, missingRules) | ||
| }) | ||
| t.Run("privileged user with no cluster collection verbs succeeds", func(t *testing.T) { | ||
| fakeClient := setupFakeClient(privilegedClusterRole) | ||
| preAuth := NewRBACPreAuthorizer(fakeClient, WithNamespacedCollectionVerbs("create")) | ||
| missingRules, err := preAuth.PreAuthorize(context.TODO(), testUser, strings.NewReader(testManifest)) | ||
| require.NoError(t, err) | ||
| require.Equal(t, []ScopedPolicyRules{}, missingRules) | ||
| }) | ||
| } | ||
| func TestPreAuthorize_WithNamespacedCollectionVerbs(t *testing.T) { | ||
| // expectedClusterMissingRules are the missing rules expected at cluster scope | ||
| // when cluster collection verbs are configured as "list", "watch". | ||
| expectedClusterMissingRules := ScopedPolicyRules{ | ||
| Namespace: "", | ||
| MissingRules: []rbacv1.PolicyRule{ | ||
| { | ||
| Verbs: []string{"list", "watch"}, | ||
| APIGroups: []string{""}, | ||
| Resources: []string{"services"}, | ||
| ResourceNames: []string(nil), | ||
| NonResourceURLs: []string(nil)}, | ||
| { | ||
| Verbs: []string{"list", "watch"}, | ||
| APIGroups: []string{"rbac.authorization.k8s.io"}, | ||
| Resources: []string{"rolebindings"}, | ||
| ResourceNames: []string(nil), | ||
| NonResourceURLs: []string(nil)}, | ||
| { | ||
| Verbs: []string{"list", "watch"}, | ||
| APIGroups: []string{"rbac.authorization.k8s.io"}, | ||
| Resources: []string{"roles"}, | ||
| ResourceNames: []string(nil), | ||
| NonResourceURLs: []string(nil), | ||
| }, | ||
| }, | ||
| } | ||
| t.Run("no namespaced collection verbs option omits namespaced collection rules", func(t *testing.T) { | ||
| fakeClient := setupFakeClient(limitedClusterRole) | ||
| preAuth := NewRBACPreAuthorizer(fakeClient, WithClusterCollectionVerbs("list", "watch")) | ||
| missingRules, err := preAuth.PreAuthorize(context.TODO(), testUser, strings.NewReader(testManifest)) | ||
| require.NoError(t, err) | ||
| // Without namespaced collection verbs, no "create" rules from collection verbs should appear, | ||
| // but object verbs (get, patch, update, delete) and escalation checks still apply | ||
| require.Equal(t, []ScopedPolicyRules{ | ||
| expectedClusterMissingRules, | ||
| { | ||
| Namespace: "test-namespace", | ||
| MissingRules: []rbacv1.PolicyRule{ | ||
| { | ||
| Verbs: []string{"create"}, | ||
| APIGroups: []string{"*"}, | ||
| Resources: []string{"certificates"}}, | ||
| { | ||
| Verbs: []string{"delete", "get", "patch", "update"}, | ||
| APIGroups: []string{""}, | ||
| Resources: []string{"services"}, | ||
| ResourceNames: []string{"test-service"}}, | ||
| { | ||
| Verbs: []string{"delete", "get", "patch", "update"}, | ||
| APIGroups: []string{"rbac.authorization.k8s.io"}, | ||
| Resources: []string{"rolebindings"}, | ||
| ResourceNames: []string{"test-extension-binding"}}, | ||
| { | ||
| Verbs: []string{"delete", "get", "patch", "update"}, | ||
| APIGroups: []string{"rbac.authorization.k8s.io"}, | ||
| Resources: []string{"roles"}, | ||
| ResourceNames: []string{"test-extension-role"}}, | ||
| { | ||
| Verbs: []string{"watch"}, | ||
| APIGroups: []string{"*"}, | ||
| Resources: []string{"serviceaccounts"}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, missingRules) | ||
| }) | ||
| t.Run("namespaced collection verbs option checks those verbs per namespace", func(t *testing.T) { | ||
| fakeClient := setupFakeClient(limitedClusterRole) | ||
| preAuth := NewRBACPreAuthorizer(fakeClient, WithClusterCollectionVerbs("list", "watch"), WithNamespacedCollectionVerbs("create", "deletecollection")) | ||
| missingRules, err := preAuth.PreAuthorize(context.TODO(), testUser, strings.NewReader(testManifest)) | ||
| require.NoError(t, err) | ||
| // Should have cluster-scoped missing rules plus namespaced rules with both create and deletecollection. | ||
| // Note: "certificates" with apiGroup "*" comes from the escalation check on the Role, not | ||
| // from namespaced collection verbs, so it only has "create". | ||
| require.Equal(t, []ScopedPolicyRules{ | ||
| expectedClusterMissingRules, | ||
| { | ||
| Namespace: "test-namespace", | ||
| MissingRules: []rbacv1.PolicyRule{ | ||
| { | ||
| Verbs: []string{"create", "deletecollection"}, | ||
| APIGroups: []string{""}, | ||
| Resources: []string{"services"}}, | ||
| { | ||
| Verbs: []string{"create", "deletecollection"}, | ||
| APIGroups: []string{"rbac.authorization.k8s.io"}, | ||
| Resources: []string{"rolebindings"}}, | ||
| { | ||
| Verbs: []string{"create", "deletecollection"}, | ||
| APIGroups: []string{"rbac.authorization.k8s.io"}, | ||
| Resources: []string{"roles"}}, | ||
| { | ||
| Verbs: []string{"create"}, | ||
| APIGroups: []string{"*"}, | ||
| Resources: []string{"certificates"}}, | ||
| { | ||
| Verbs: []string{"delete", "get", "patch", "update"}, | ||
| APIGroups: []string{""}, | ||
| Resources: []string{"services"}, | ||
| ResourceNames: []string{"test-service"}}, | ||
| { | ||
| Verbs: []string{"delete", "get", "patch", "update"}, | ||
| APIGroups: []string{"rbac.authorization.k8s.io"}, | ||
| Resources: []string{"rolebindings"}, | ||
| ResourceNames: []string{"test-extension-binding"}}, | ||
| { | ||
| Verbs: []string{"delete", "get", "patch", "update"}, | ||
| APIGroups: []string{"rbac.authorization.k8s.io"}, | ||
| Resources: []string{"roles"}, | ||
| ResourceNames: []string{"test-extension-role"}}, | ||
| { | ||
| Verbs: []string{"watch"}, | ||
| APIGroups: []string{"*"}, | ||
| Resources: []string{"serviceaccounts"}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, missingRules) | ||
| }) | ||
| t.Run("privileged user with custom namespaced collection verbs succeeds", func(t *testing.T) { | ||
| fakeClient := setupFakeClient(privilegedClusterRole) | ||
| preAuth := NewRBACPreAuthorizer(fakeClient, WithNamespacedCollectionVerbs("create", "deletecollection")) | ||
| missingRules, err := preAuth.PreAuthorize(context.TODO(), testUser, strings.NewReader(testManifest)) | ||
| require.NoError(t, err) | ||
| require.Equal(t, []ScopedPolicyRules{}, missingRules) | ||
| }) | ||
| } | ||
| // TestParseEscalationErrorForMissingRules Are tests with respect to https://github.com/kubernetes/api/blob/e8d4d542f6a9a16a694bfc8e3b8cd1557eecfc9d/rbac/v1/types.go#L49-L74 | ||
| // Goal is: prove the regex works as planned AND that if the error messages ever change we'll learn about it with these tests | ||
| func TestParseEscalationErrorForMissingRules_ParsingLogic(t *testing.T) { | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.