Skip to content

Commit

Permalink
tests:filters: Add test for ancestor binary export filter
Browse files Browse the repository at this point in the history
Add TestAncestorBinaryRegexFilter test to pkg/filters to ensure that new ancestor binary export filter works as expected.

Signed-off-by: t0x01 <[email protected]>
  • Loading branch information
t0x01 committed Feb 5, 2025
1 parent 6f7d352 commit 74cf752
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
86 changes: 86 additions & 0 deletions pkg/filters/binary_regex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,89 @@ func TestParentBinaryRegexFilter(t *testing.T) {
}
assert.True(t, fl.MatchOne(&ev))
}

func TestAncestorBinaryRegexFilter(t *testing.T) {
f := []*tetragon.Filter{{
EventSet: []tetragon.EventType{tetragon.EventType_PROCESS_EXEC, tetragon.EventType_PROCESS_EXIT},
AncestorBinaryRegex: []string{"bash", "zsh"},
}}
fl, err := BuildFilterList(context.Background(), f, []OnBuildFilter{&AncestorBinaryRegexFilter{}})
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/bash"},
},
},
},
}
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.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"},
Ancestors: []*tetragon.Process{
&tetragon.Process{Binary: "/bin/foo"},
&tetragon.Process{Binary: "/bin/bar"},
},
},
},
},
}
assert.False(t, fl.MatchOne(&ev))
ev = v1.Event{
Event: &tetragon.GetEventsResponse{
Event: &tetragon.GetEventsResponse_ProcessExec{
ProcessExec: &tetragon.ProcessExec{
Parent: &tetragon.Process{Binary: "/bin/sh"},
Process: &tetragon.Process{Binary: "/sbin/iptables"},
Ancestors: []*tetragon.Process{
&tetragon.Process{Binary: "/bin/foo"},
&tetragon.Process{Binary: "/bin/bash"},
},
},
},
},
}
assert.True(t, fl.MatchOne(&ev))
ev = v1.Event{
Event: &tetragon.GetEventsResponse{
Event: &tetragon.GetEventsResponse_ProcessExec{
ProcessExec: &tetragon.ProcessExec{
Parent: &tetragon.Process{Binary: "/bin/sh"},
Process: &tetragon.Process{Binary: "/sbin/iptables"},
Ancestors: []*tetragon.Process{
&tetragon.Process{Binary: "/bin/zsh"},
&tetragon.Process{Binary: "/bin/foo"},
},
},
},
},
}
assert.True(t, fl.MatchOne(&ev))
}
1 change: 1 addition & 0 deletions pkg/filters/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
func TestMain(m *testing.M) {
// Needed for cap filters
option.Config.EnableProcessCred = true
option.Config.EnableProcessAncestors = true

code := m.Run()
os.Exit(code)
Expand Down

0 comments on commit 74cf752

Please sign in to comment.