Skip to content

Commit

Permalink
filters: implement parent binary export filter
Browse files Browse the repository at this point in the history
[upstream commit: 11bb4ba]
[backporter's note: opted to use same field number as later versions for compatibility]

Implement a new export filter that can filter over parent binary names
using RE2 regular expressions.

Signed-off-by: willfindlay <will@isovalent.com>
  • Loading branch information
willfindlay authored and kkourt committed Jul 10, 2024
1 parent 2fb7021 commit b0f8427
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 10 deletions.
1 change: 1 addition & 0 deletions api/v1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,7 @@ AggregationOptions defines configuration options for aggregating events.
| pod_regex | [string](#string) | repeated | Filter by process.pod.name field using RE2 regular expression syntax: https://github.com/google/re2/wiki/Syntax |
| arguments_regex | [string](#string) | repeated | Filter by process.arguments field using RE2 regular expression syntax: https://github.com/google/re2/wiki/Syntax |
| labels | [string](#string) | repeated | Filter events by pod labels using Kubernetes label selector syntax: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors Note that this filter never matches events without the pod field (i.e. host process events). |
| parent_binary_regex | [string](#string) | repeated | Filter parent process&#39; binary using RE2 regular expression syntax. |



Expand Down
16 changes: 14 additions & 2 deletions api/v1/tetragon/events.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/v1/tetragon/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ message Filter {
// Note that this filter never matches events without the pod field (i.e.
// host process events).
repeated string labels = 9;
// Filter parent process' binary using RE2 regular expression syntax.
repeated string parent_binary_regex = 12;
}

message RedactionFilter {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/content/en/docs/concepts/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ flags, or environment variables.
| `pod_regex` | Filter by pod name using a list of regular expressions. You can find the full syntax [here](https://github.com/google/re2/wiki/Syntax). |
| `arguments_regex` | Filter by pod name using a list of regular expressions. You can find the full syntax [here](https://github.com/google/re2/wiki/Syntax). |
| `labels` | Filter events by pod labels using [Kubernetes label selector syntax](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors) Note that this filter never matches events without the pod field (i.e. host process events). |
| `parent_binary_regex` | Filter process events by a list of regular expressions of parent process binary names (e.g. `"^/home/kubernetes/bin/kubelet$"`). You can find the full syntax [here](https://github.com/google/re2/wiki/Syntax). |

#### Field Filtering

Expand Down
1 change: 1 addition & 0 deletions docs/content/en/docs/reference/grpc-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ AggregationOptions defines configuration options for aggregating events.
| pod_regex | [string](#string) | repeated | Filter by process.pod.name field using RE2 regular expression syntax: https://github.com/google/re2/wiki/Syntax |
| arguments_regex | [string](#string) | repeated | Filter by process.arguments field using RE2 regular expression syntax: https://github.com/google/re2/wiki/Syntax |
| labels | [string](#string) | repeated | Filter events by pod labels using Kubernetes label selector syntax: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors Note that this filter never matches events without the pod field (i.e. host process events). |
| parent_binary_regex | [string](#string) | repeated | Filter parent process&#39; binary using RE2 regular expression syntax. |

<a name="tetragon-GetEventsRequest"></a>

Expand Down
28 changes: 24 additions & 4 deletions pkg/filters/binary_regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
hubbleFilters "github.com/cilium/tetragon/pkg/oldhubble/filters"
)

func filterByBinaryRegex(binaryPatterns []string) (hubbleFilters.FilterFunc, error) {
func filterByBinaryRegex(binaryPatterns []string, parent bool) (hubbleFilters.FilterFunc, error) {
var binaries []*regexp.Regexp
for _, pattern := range binaryPatterns {
query, err := regexp.Compile(pattern)
Expand All @@ -23,7 +23,13 @@ func filterByBinaryRegex(binaryPatterns []string) (hubbleFilters.FilterFunc, err
binaries = append(binaries, query)
}
return func(ev *v1.Event) bool {
process := GetProcess(ev)
var process *tetragon.Process
if parent {
process = GetParent(ev)

} else {
process = GetProcess(ev)
}
if process == nil {
return false
}
Expand All @@ -41,11 +47,25 @@ type BinaryRegexFilter struct{}
func (f *BinaryRegexFilter) OnBuildFilter(_ context.Context, ff *tetragon.Filter) ([]hubbleFilters.FilterFunc, error) {
var fs []hubbleFilters.FilterFunc
if ff.BinaryRegex != nil {
dnsFilters, err := filterByBinaryRegex(ff.BinaryRegex)
filters, err := filterByBinaryRegex(ff.BinaryRegex, false)
if err != nil {
return nil, err
}
fs = append(fs, filters)
}
return fs, nil
}

type ParentBinaryRegexFilter struct{}

func (f *ParentBinaryRegexFilter) OnBuildFilter(_ context.Context, ff *tetragon.Filter) ([]hubbleFilters.FilterFunc, error) {
var fs []hubbleFilters.FilterFunc
if ff.ParentBinaryRegex != nil {
filters, err := filterByBinaryRegex(ff.ParentBinaryRegex, true)
if err != nil {
return nil, err
}
fs = append(fs, dnsFilters)
fs = append(fs, filters)
}
return fs, nil
}
49 changes: 49 additions & 0 deletions pkg/filters/binary_regex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,52 @@ func TestBinaryRegexFilterInvalidEvent(t *testing.T) {
Event: &tetragon.GetEventsResponse_ProcessExec{ProcessExec: &tetragon.ProcessExec{Process: nil}},
}}))
}

func TestParentBinaryRegexFilter(t *testing.T) {
f := []*tetragon.Filter{{ParentBinaryRegex: []string{"bash", "zsh"}}}
fl, err := BuildFilterList(context.Background(), f, []OnBuildFilter{&ParentBinaryRegexFilter{}})
assert.NoError(t, err)
ev := v1.Event{
Event: &tetragon.GetEventsResponse{
Event: &tetragon.GetEventsResponse_ProcessExec{
ProcessExec: &tetragon.ProcessExec{
Process: &tetragon.Process{Binary: "/sbin/iptables"},
},
},
},
}
assert.False(t, fl.MatchOne(&ev))
ev = v1.Event{
Event: &tetragon.GetEventsResponse{
Event: &tetragon.GetEventsResponse_ProcessExec{
ProcessExec: &tetragon.ProcessExec{
Parent: &tetragon.Process{Binary: "/bin/foo"},
Process: &tetragon.Process{Binary: "/sbin/iptables"},
},
},
},
}
assert.False(t, fl.MatchOne(&ev))
ev = v1.Event{
Event: &tetragon.GetEventsResponse{
Event: &tetragon.GetEventsResponse_ProcessExec{
ProcessExec: &tetragon.ProcessExec{
Parent: &tetragon.Process{Binary: "/bin/bash"},
Process: &tetragon.Process{Binary: "/sbin/iptables"},
},
},
},
}
assert.True(t, fl.MatchOne(&ev))
ev = v1.Event{
Event: &tetragon.GetEventsResponse{
Event: &tetragon.GetEventsResponse_ProcessExec{
ProcessExec: &tetragon.ProcessExec{
Parent: &tetragon.Process{Binary: "/bin/zsh"},
Process: &tetragon.Process{Binary: "/sbin/iptables"},
},
},
},
}
assert.True(t, fl.MatchOne(&ev))
}
1 change: 1 addition & 0 deletions pkg/filters/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func BuildFilterList(ctx context.Context, ff []*tetragon.Filter, filterFuncs []O
// Filters is the list of default filters
var Filters = []OnBuildFilter{
&BinaryRegexFilter{},
&ParentBinaryRegexFilter{},
&HealthCheckFilter{},
&NamespaceFilter{},
&PidFilter{},
Expand Down
16 changes: 14 additions & 2 deletions vendor/github.com/cilium/tetragon/api/v1/tetragon/events.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b0f8427

Please sign in to comment.