-
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6a1f9a8
feat: add namespace support to Rego driver via input.namespace
JaydipGabani 7268285
updating input.namespace to input.review.namespaceObject
JaydipGabani f656fff
addressing copilot feedbacl
JaydipGabani 7099316
Addressing copilot comments
JaydipGabani 1223dc4
refactoring setting namespace
JaydipGabani caacf32
adding tests
JaydipGabani 8e4f824
Merge branch 'master' into fix#3777
JaydipGabani 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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1130,3 +1130,108 @@ 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, // Violation because input.namespace is nil, missing environment label check triggers | ||
JaydipGabani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
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.