-
Notifications
You must be signed in to change notification settings - Fork 15
feat(exporters): add eventFieldFilter to HTTP exporter #802
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
Open
RohanKaran
wants to merge
5
commits into
kubescape:main
Choose a base branch
from
RohanKaran:feature/event-field-filter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cbe6580
feat(exporters): add EventFieldFilter to HTTP exporter
RohanKaran 4f28362
docs(config): enhance HTTP exporter config with detailed headers, fil…
RohanKaran 3605799
docs(CONFIGURATION): update example HTTP exporter URLs and add eventF…
RohanKaran 736903d
fix(exporters): disable HTML escaping, omit empty slice items, add tests
RohanKaran 85ac441
fix(exporters): preserve scalar values in allow list filtering
RohanKaran 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,151 @@ | ||
| package exporters | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| // EventFieldFilterConfig configures field-level filtering for exported alert events. | ||
| // AllowList takes precedence over DenyList. Both support dot notation (e.g. "spec.processTree"). | ||
| type EventFieldFilterConfig struct { | ||
| AllowList []string `json:"allowList,omitempty" mapstructure:"allowList"` | ||
| DenyList []string `json:"denyList,omitempty" mapstructure:"denyList"` | ||
| } | ||
|
|
||
| // EventFieldFilter applies allow/deny list filtering to JSON payloads. | ||
| type EventFieldFilter struct { | ||
| allowSet map[string]struct{} | ||
| denySet map[string]struct{} | ||
| useAllow bool | ||
| } | ||
|
|
||
| // NewEventFieldFilter creates a filter from config. Returns nil if no fields are configured. | ||
| func NewEventFieldFilter(config *EventFieldFilterConfig) *EventFieldFilter { | ||
| if config == nil { | ||
| return nil | ||
| } | ||
| if len(config.AllowList) == 0 && len(config.DenyList) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| f := &EventFieldFilter{} | ||
| if len(config.AllowList) > 0 { | ||
| f.useAllow = true | ||
| f.allowSet = make(map[string]struct{}, len(config.AllowList)) | ||
| for _, field := range config.AllowList { | ||
| f.allowSet[field] = struct{}{} | ||
| } | ||
| } else { | ||
| f.denySet = make(map[string]struct{}, len(config.DenyList)) | ||
| for _, field := range config.DenyList { | ||
| f.denySet[field] = struct{}{} | ||
| } | ||
| } | ||
| return f | ||
| } | ||
|
|
||
| // FilterJSON applies the field filter to marshaled JSON bytes and returns filtered bytes. | ||
| func (f *EventFieldFilter) FilterJSON(data []byte) ([]byte, error) { | ||
| var m map[string]any | ||
| dec := json.NewDecoder(bytes.NewReader(data)) | ||
| dec.UseNumber() | ||
| if err := dec.Decode(&m); err != nil { | ||
| return nil, fmt.Errorf("field filter: failed to unmarshal: %w", err) | ||
| } | ||
|
|
||
| if f.useAllow { | ||
| m = applyAllowList(m, f.allowSet) | ||
| } else { | ||
| for key := range f.denySet { | ||
| parts := strings.SplitN(key, ".", 2) | ||
| if len(parts) == 1 { | ||
| delete(m, key) | ||
| } else { | ||
| removePath(m, parts[0], parts[1]) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| var buf bytes.Buffer | ||
| enc := json.NewEncoder(&buf) | ||
| enc.SetEscapeHTML(false) | ||
| if err := enc.Encode(m); err != nil { | ||
| return nil, fmt.Errorf("field filter: failed to marshal: %w", err) | ||
| } | ||
| return bytes.TrimRight(buf.Bytes(), "\n"), nil | ||
| } | ||
|
|
||
| func applyAllowList(m map[string]any, allowSet map[string]struct{}) map[string]any { | ||
| groups := make(map[string]map[string]struct{}) | ||
| for path := range allowSet { | ||
| parts := strings.SplitN(path, ".", 2) | ||
| topKey := parts[0] | ||
| if _, ok := groups[topKey]; !ok { | ||
| groups[topKey] = make(map[string]struct{}) | ||
| } | ||
| if len(parts) > 1 { | ||
| groups[topKey][parts[1]] = struct{}{} | ||
| } | ||
| } | ||
|
|
||
| result := make(map[string]any) | ||
| for topKey, subSet := range groups { | ||
| val, exists := m[topKey] | ||
| if !exists { | ||
| continue | ||
| } | ||
| if _, ok := allowSet[topKey]; ok { | ||
| result[topKey] = val | ||
| continue | ||
| } | ||
| if len(subSet) > 0 { | ||
| if nested, ok := val.(map[string]any); ok { | ||
| result[topKey] = applyAllowList(nested, subSet) | ||
| } else if slice, ok := val.([]any); ok { | ||
| newSlice := make([]any, 0, len(slice)) | ||
| for _, item := range slice { | ||
| if itemMap, ok := item.(map[string]any); ok { | ||
| filtered := applyAllowList(itemMap, subSet) | ||
| if len(filtered) > 0 { | ||
| newSlice = append(newSlice, filtered) | ||
| } | ||
| } else { | ||
| newSlice = append(newSlice, item) | ||
| } | ||
| } | ||
| result[topKey] = newSlice | ||
| } else { | ||
| // scalar value (string, number, bool) — keep it as-is | ||
| result[topKey] = val | ||
| } | ||
| } | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| func removePath(m map[string]any, topKey, rest string) { | ||
| val, exists := m[topKey] | ||
| if !exists { | ||
| return | ||
| } | ||
| parts := strings.SplitN(rest, ".", 2) | ||
| if nested, ok := val.(map[string]any); ok { | ||
| if len(parts) == 1 { | ||
| delete(nested, rest) | ||
| } else { | ||
| removePath(nested, parts[0], parts[1]) | ||
| } | ||
| } else if slice, ok := val.([]any); ok { | ||
| for _, item := range slice { | ||
| if itemMap, ok := item.(map[string]any); ok { | ||
| if len(parts) == 1 { | ||
| delete(itemMap, rest) | ||
| } else { | ||
| removePath(itemMap, parts[0], parts[1]) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.