-
Notifications
You must be signed in to change notification settings - Fork 26
feat(rulemanager): add rule filtering based on context #911
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
matthyx
merged 2 commits into
kubescape:main
from
khuswant18:rule-filtering-based-on-context
Aug 22, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package rulecreator | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| "github.com/kubescape/node-agent/pkg/contextdetection" | ||
| typesv1 "github.com/kubescape/node-agent/pkg/rulemanager/types/v1" | ||
| ) | ||
|
|
||
| // RuleMatchesContext is the single source of truth for context-based rule matching. | ||
| func RuleMatchesContext(rule *typesv1.Rule, currentContext contextdetection.EventSourceContext) bool { | ||
| isContainerContext := currentContext == contextdetection.Kubernetes || | ||
| currentContext == contextdetection.Standalone || | ||
| currentContext == contextdetection.Container || | ||
| currentContext == contextdetection.ECS | ||
|
|
||
| var hasContextTags bool | ||
| for _, tag := range rule.Tags { | ||
| if ctx, found := strings.CutPrefix(tag, "context:"); found { | ||
| if ctx == string(currentContext) { | ||
| return true | ||
| } | ||
| if ctx == string(contextdetection.Container) && isContainerContext { | ||
| return true | ||
| } | ||
| hasContextTags = true | ||
| } | ||
| } | ||
|
|
||
| if !hasContextTags { | ||
| return currentContext == contextdetection.Kubernetes | ||
| } | ||
|
|
||
| return false | ||
| } |
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,157 @@ | ||
| package rulecreator | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/kubescape/node-agent/pkg/contextdetection" | ||
| typesv1 "github.com/kubescape/node-agent/pkg/rulemanager/types/v1" | ||
| ) | ||
|
|
||
| func TestRuleMatchesContext(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| rule typesv1.Rule | ||
| context contextdetection.EventSourceContext | ||
| expected bool | ||
| }{ | ||
| { | ||
| name: "host tag matches host context", | ||
| rule: typesv1.Rule{Tags: []string{"context:host"}}, | ||
| context: contextdetection.Host, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "host tag does not match kubernetes context", | ||
| rule: typesv1.Rule{Tags: []string{"context:host"}}, | ||
| context: contextdetection.Kubernetes, | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "kubernetes tag matches kubernetes context", | ||
| rule: typesv1.Rule{Tags: []string{"context:kubernetes"}}, | ||
| context: contextdetection.Kubernetes, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "kubernetes tag does not match host context", | ||
| rule: typesv1.Rule{Tags: []string{"context:kubernetes"}}, | ||
| context: contextdetection.Host, | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "standalone tag matches standalone context", | ||
| rule: typesv1.Rule{Tags: []string{"context:standalone"}}, | ||
| context: contextdetection.Standalone, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "ecs tag matches ecs context", | ||
| rule: typesv1.Rule{Tags: []string{"context:ecs"}}, | ||
| context: contextdetection.ECS, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "container tag matches kubernetes context", | ||
| rule: typesv1.Rule{Tags: []string{"context:container"}}, | ||
| context: contextdetection.Kubernetes, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "container tag matches standalone context", | ||
| rule: typesv1.Rule{Tags: []string{"context:container"}}, | ||
| context: contextdetection.Standalone, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "container tag matches ecs context", | ||
| rule: typesv1.Rule{Tags: []string{"context:container"}}, | ||
| context: contextdetection.ECS, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "container tag matches container context", | ||
| rule: typesv1.Rule{Tags: []string{"context:container"}}, | ||
| context: contextdetection.Container, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "container tag does not match host context", | ||
| rule: typesv1.Rule{Tags: []string{"context:container"}}, | ||
| context: contextdetection.Host, | ||
| expected: false, | ||
| }, | ||
|
|
||
| { | ||
| name: "no context tags defaults to kubernetes", | ||
| rule: typesv1.Rule{Tags: []string{"some-other-tag"}}, | ||
| context: contextdetection.Kubernetes, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "no context tags rejects host", | ||
| rule: typesv1.Rule{Tags: []string{"some-other-tag"}}, | ||
| context: contextdetection.Host, | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "no context tags rejects standalone", | ||
| rule: typesv1.Rule{Tags: []string{}}, | ||
| context: contextdetection.Standalone, | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "nil tags defaults to kubernetes", | ||
| rule: typesv1.Rule{}, | ||
| context: contextdetection.Kubernetes, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "nil tags rejects host", | ||
| rule: typesv1.Rule{}, | ||
| context: contextdetection.Host, | ||
| expected: false, | ||
| }, | ||
|
|
||
| { | ||
| name: "multiple context tags: host+kubernetes matches host", | ||
| rule: typesv1.Rule{Tags: []string{"context:host", "context:kubernetes"}}, | ||
| context: contextdetection.Host, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "multiple context tags: host+kubernetes matches kubernetes", | ||
| rule: typesv1.Rule{Tags: []string{"context:host", "context:kubernetes"}}, | ||
| context: contextdetection.Kubernetes, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "multiple context tags: host+kubernetes rejects standalone", | ||
| rule: typesv1.Rule{Tags: []string{"context:host", "context:kubernetes"}}, | ||
| context: contextdetection.Standalone, | ||
| expected: false, | ||
| }, | ||
|
|
||
| { | ||
| name: "mixed tags with context:host matches host", | ||
| rule: typesv1.Rule{Tags: []string{"severity:high", "context:host", "category:network"}}, | ||
| context: contextdetection.Host, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "mixed tags with context:host rejects kubernetes", | ||
| rule: typesv1.Rule{Tags: []string{"severity:high", "context:host", "category:network"}}, | ||
| context: contextdetection.Kubernetes, | ||
| expected: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result := RuleMatchesContext(&tt.rule, tt.context) | ||
| if result != tt.expected { | ||
| t.Errorf("RuleMatchesContext() = %v, want %v (rule tags: %v, context: %s)", | ||
| result, tt.expected, tt.rule.Tags, tt.context) | ||
| } | ||
| }) | ||
| } | ||
| } |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.