-
Notifications
You must be signed in to change notification settings - Fork 60
feat: add namespace support to Rego driver via input.review.namespaceObject #646
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
Changes from 6 commits
6a1f9a8
7268285
f656fff
7099316
1223dc4
caacf32
8e4f824
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1130,3 +1130,193 @@ func TestE2E_Client_GetDescriptionForStat(t *testing.T) { | |
| } | ||
| } | ||
| } | ||
|
|
||
| // TestClient_Review_Namespace tests that namespace data is properly passed | ||
| // to the Rego driver via input.review.namespaceObject for namespace-based policy decisions. | ||
| func TestClient_Review_Namespace(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| namespace map[string]interface{} | ||
| wantEnv string | ||
| wantResults int | ||
| wantMsg string | ||
| }{ | ||
| { | ||
| name: "no namespace provided - expects violation for missing namespace", | ||
| namespace: nil, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a case for empty namespace too
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added a test for this. |
||
| wantEnv: "production", | ||
| wantResults: 1, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we want this to be 0 or 1?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really, the test policy is written so that it will result in violation when namespace is nil or empty. |
||
| wantMsg: "namespace is missing environment label", | ||
| }, | ||
| { | ||
| name: "empty namespace object - expects violation for missing namespace", | ||
| namespace: map[string]interface{}{}, | ||
| wantEnv: "production", | ||
| wantResults: 1, | ||
| wantMsg: "namespace is missing environment label", | ||
| }, | ||
| { | ||
| name: "namespace with matching environment label", | ||
| namespace: map[string]interface{}{ | ||
| "metadata": map[string]interface{}{ | ||
| "name": "test-ns", | ||
| "labels": map[string]interface{}{ | ||
| "environment": "production", | ||
| }, | ||
| }, | ||
| }, | ||
| wantEnv: "production", | ||
| wantResults: 0, // No violation - environment matches | ||
| }, | ||
| { | ||
| name: "namespace with wrong environment label", | ||
| namespace: map[string]interface{}{ | ||
| "metadata": map[string]interface{}{ | ||
| "name": "test-ns", | ||
| "labels": map[string]interface{}{ | ||
| "environment": "staging", | ||
| }, | ||
| }, | ||
| }, | ||
| wantEnv: "production", | ||
| wantResults: 1, | ||
| wantMsg: "namespace has environment staging but want production", | ||
| }, | ||
| { | ||
| name: "namespace missing environment label", | ||
| namespace: map[string]interface{}{ | ||
| "metadata": map[string]interface{}{ | ||
| "name": "test-ns", | ||
| "labels": map[string]interface{}{ | ||
| "team": "platform", | ||
| }, | ||
| }, | ||
| }, | ||
| wantEnv: "production", | ||
| wantResults: 1, | ||
| wantMsg: "namespace is missing environment label", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| c := clienttest.New(t) | ||
|
|
||
| ct := clienttest.TemplateCheckNamespace() | ||
| _, err := c.AddTemplate(ctx, ct) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| constraint := cts.MakeConstraint(t, clienttest.KindCheckNamespace, "constraint", cts.WantEnvironment(tt.wantEnv)) | ||
| _, err = c.AddConstraint(ctx, constraint) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| review := handlertest.NewReview("test-ns", "test-obj", "test-data") | ||
|
|
||
| // Pass namespace via reviews.Namespace option | ||
| var opts []reviews.ReviewOpt | ||
| if tt.namespace != nil { | ||
| opts = append(opts, reviews.Namespace(tt.namespace)) | ||
| } | ||
|
|
||
| responses, err := c.Review(ctx, review, opts...) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| results := responses.Results() | ||
| if len(results) != tt.wantResults { | ||
| t.Errorf("got %d results, want %d. Results: %v", len(results), tt.wantResults, results) | ||
| } | ||
|
|
||
| if tt.wantResults > 0 && len(results) > 0 { | ||
| if results[0].Msg != tt.wantMsg { | ||
| t.Errorf("got message %q, want %q", results[0].Msg, tt.wantMsg) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestClient_Review_ClusterScopedResource verifies that cluster-scoped resources | ||
| // (resources without a namespace, like ClusterRole, PersistentVolume, etc.) | ||
| // can be reviewed correctly. This ensures empty namespace handling doesn't cause | ||
| // issues for cluster-scoped resources, including when namespace-aware policies are used. | ||
| func TestClient_Review_ClusterScopedResource(t *testing.T) { | ||
| t.Run("with deny policy", func(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| c := clienttest.New(t) | ||
|
|
||
| // Use TemplateDeny which unconditionally denies - doesn't depend on namespace | ||
| ct := clienttest.TemplateDeny() | ||
| _, err := c.AddTemplate(ctx, ct) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| constraint := cts.MakeConstraint(t, clienttest.KindDeny, "deny-all") | ||
| _, err = c.AddConstraint(ctx, constraint) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| // Create a cluster-scoped review (empty namespace in the object itself) | ||
| review := handlertest.NewReview("", "cluster-resource", "test-data") | ||
|
|
||
| responses, err := c.Review(ctx, review) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error during review: %v", err) | ||
| } | ||
|
|
||
| results := responses.Results() | ||
| if len(results) != 1 { | ||
| t.Errorf("got %d results, want 1. Results: %v", len(results), results) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("with namespace-aware policy", func(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| c := clienttest.New(t) | ||
|
|
||
| // Use TemplateCheckNamespace which checks namespace labels | ||
| ct := clienttest.TemplateCheckNamespace() | ||
| _, err := c.AddTemplate(ctx, ct) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| constraint := cts.MakeConstraint(t, clienttest.KindCheckNamespace, "check-ns", | ||
| cts.WantEnvironment("production")) | ||
| _, err = c.AddConstraint(ctx, constraint) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| // Create a cluster-scoped review (empty namespace in the object) | ||
| review := handlertest.NewReview("", "cluster-resource", "test-data") | ||
|
|
||
| responses, err := c.Review(ctx, review) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error during review: %v", err) | ||
| } | ||
|
|
||
| results := responses.Results() | ||
| if len(results) != 1 { | ||
| t.Errorf("got %d results, want 1. Results: %v", len(results), results) | ||
| } | ||
|
|
||
| if len(results) > 0 { | ||
| wantMsg := "namespace is missing environment label" | ||
| if results[0].Msg != wantMsg { | ||
| t.Errorf("got message %q, want %q", results[0].Msg, wantMsg) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need the guard if object doesn't have namespace?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added the check, if its nil then the variable results in undefined. So it should be alright, but added the check regardless.