Skip to content

Commit

Permalink
pkg/filters: add k8s CIDR and IP helpers to CEL filters
Browse files Browse the repository at this point in the history
This simple PR adds CIDR and IP helpers from the k8s CEL libraries to our CEL filters.
This should enable users to write export filters in CEL that match IPs in kprobe events
using CIDR ranges etc.

For example, the following CEL expression written as a CEL filter would match kprobe
events where the first argument contains a socket with source address in CIDR range
10.0.0.0/16:

    cidr('10.0.0.0/16').containsIP(process_kprobe.args[0].sock_arg.saddr)

The specific documentation for these CEL libraries is available here:

- https://pkg.go.dev/k8s.io/apiserver/pkg/cel/library#CIDR
- https://pkg.go.dev/k8s.io/apiserver/pkg/cel/library#IP

Signed-off-by: William Findlay <[email protected]>
  • Loading branch information
will-isovalent committed Dec 11, 2024
1 parent 93db38d commit 04225f1
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 20 deletions.
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ require (
google.golang.org/grpc v1.68.1
google.golang.org/protobuf v1.35.2
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.31.3
k8s.io/api v0.31.4
k8s.io/apiextensions-apiserver v0.31.3
k8s.io/apimachinery v0.31.3
k8s.io/client-go v0.31.3
k8s.io/apimachinery v0.31.4
k8s.io/apiserver v0.31.4
k8s.io/client-go v0.31.4
k8s.io/code-generator v0.31.3
k8s.io/cri-api v0.30.7
k8s.io/klog/v2 v2.130.1
Expand Down Expand Up @@ -167,8 +168,7 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/apiserver v0.31.3 // indirect
k8s.io/component-base v0.31.3 // indirect
k8s.io/component-base v0.31.4 // indirect
k8s.io/gengo/v2 v2.0.0-20240228010128-51d4e06bde70 // indirect
k8s.io/utils v0.0.0-20240921022957-49e7df575cb6 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -586,20 +586,20 @@ gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
k8s.io/api v0.31.3 h1:umzm5o8lFbdN/hIXbrK9oRpOproJO62CV1zqxXrLgk8=
k8s.io/api v0.31.3/go.mod h1:UJrkIp9pnMOI9K2nlL6vwpxRzzEX5sWgn8kGQe92kCE=
k8s.io/api v0.31.4 h1:I2QNzitPVsPeLQvexMEsj945QumYraqv9m74isPDKhM=
k8s.io/api v0.31.4/go.mod h1:d+7vgXLvmcdT1BCo79VEgJxHHryww3V5np2OYTr6jdw=
k8s.io/apiextensions-apiserver v0.31.3 h1:+GFGj2qFiU7rGCsA5o+p/rul1OQIq6oYpQw4+u+nciE=
k8s.io/apiextensions-apiserver v0.31.3/go.mod h1:2DSpFhUZZJmn/cr/RweH1cEVVbzFw9YBu4T+U3mf1e4=
k8s.io/apimachinery v0.31.3 h1:6l0WhcYgasZ/wk9ktLq5vLaoXJJr5ts6lkaQzgeYPq4=
k8s.io/apimachinery v0.31.3/go.mod h1:rsPdaZJfTfLsNJSQzNHQvYoTmxhoOEofxtOsF3rtsMo=
k8s.io/apiserver v0.31.3 h1:+1oHTtCB+OheqFEz375D0IlzHZ5VeQKX1KGXnx+TTuY=
k8s.io/apiserver v0.31.3/go.mod h1:PrxVbebxrxQPFhJk4powDISIROkNMKHibTg9lTRQ0Qg=
k8s.io/client-go v0.31.3 h1:CAlZuM+PH2cm+86LOBemaJI/lQ5linJ6UFxKX/SoG+4=
k8s.io/client-go v0.31.3/go.mod h1:2CgjPUTpv3fE5dNygAr2NcM8nhHzXvxB8KL5gYc3kJs=
k8s.io/apimachinery v0.31.4 h1:8xjE2C4CzhYVm9DGf60yohpNUh5AEBnPxCryPBECmlM=
k8s.io/apimachinery v0.31.4/go.mod h1:rsPdaZJfTfLsNJSQzNHQvYoTmxhoOEofxtOsF3rtsMo=
k8s.io/apiserver v0.31.4 h1:JbtnTaXVYEAYIHJil6Wd74Wif9sd8jVcBw84kwEmp7o=
k8s.io/apiserver v0.31.4/go.mod h1:JJjoTjZ9PTMLdIFq7mmcJy2B9xLN3HeAUebW6xZyIP0=
k8s.io/client-go v0.31.4 h1:t4QEXt4jgHIkKKlx06+W3+1JOwAFU/2OPiOo7H92eRQ=
k8s.io/client-go v0.31.4/go.mod h1:kvuMro4sFYIa8sulL5Gi5GFqUPvfH2O/dXuKstbaaeg=
k8s.io/code-generator v0.31.3 h1:Pj0fYOBms+ZrsulLi4DMsCEx1jG8fWKRLy44onHsLBI=
k8s.io/code-generator v0.31.3/go.mod h1:/umCIlT84g1+Yu5ZXtP1KGSRTnGiIzzX5AzUAxsNlts=
k8s.io/component-base v0.31.3 h1:DMCXXVx546Rfvhj+3cOm2EUxhS+EyztH423j+8sOwhQ=
k8s.io/component-base v0.31.3/go.mod h1:xME6BHfUOafRgT0rGVBGl7TuSg8Z9/deT7qq6w7qjIU=
k8s.io/component-base v0.31.4 h1:wCquJh4ul9O8nNBSB8N/o8+gbfu3BVQkVw9jAUY/Qtw=
k8s.io/component-base v0.31.4/go.mod h1:G4dgtf5BccwiDT9DdejK0qM6zTK0jwDGEKnCmb9+u/s=
k8s.io/cri-api v0.30.7 h1:4SRl/zLF+FuzQ6sUkrI5c6U8drlF3xF6/ad/Qs0AMuE=
k8s.io/cri-api v0.30.7/go.mod h1://4/umPJSW1ISNSNng4OwjpkvswJOQwU8rnkvO8P+xg=
k8s.io/gengo/v2 v2.0.0-20240228010128-51d4e06bde70 h1:NGrVE502P0s0/1hudf8zjgwki1X/TByhmAoILTarmzo=
Expand Down
4 changes: 4 additions & 0 deletions pkg/filters/cel_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/cilium/tetragon/api/v1/tetragon/codegen/helpers"
"github.com/google/cel-go/cel"
"github.com/sirupsen/logrus"
celk8s "k8s.io/apiserver/pkg/cel/library"
)

// compile will parse and check an expression `expr` against a given
Expand Down Expand Up @@ -99,6 +100,9 @@ func NewCELExpressionFilter(log logrus.FieldLogger) *CELExpressionFilter {
responseTypeMap := helpers.ResponseTypeMap()
options := []cel.EnvOption{
cel.Container("tetragon"),
// Import IP and CIDR related helpers from k8s CEL library
celk8s.IP(),
celk8s.CIDR(),
}
for key, val := range responseTypeMap {
name := string(val.ProtoReflect().Descriptor().FullName())
Expand Down
42 changes: 42 additions & 0 deletions pkg/filters/cel_expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,45 @@ func TestProcessKprobeFilter(t *testing.T) {
}
assert.False(t, fl.MatchOne(&ev))
}

func TestCIDR(t *testing.T) {
log := logrus.New()
f := []*tetragon.Filter{{CelExpression: []string{"cidr('10.0.0.0/16').containsIP(process_kprobe.args[0].sock_arg.saddr)"}}}
fl, err := BuildFilterList(context.Background(), f, []OnBuildFilter{NewCELExpressionFilter(log)})
assert.NoError(t, err)
ev := v1.Event{
Event: &tetragon.GetEventsResponse{
Event: &tetragon.GetEventsResponse_ProcessKprobe{
ProcessKprobe: &tetragon.ProcessKprobe{Args: []*tetragon.KprobeArgument{{Arg: &tetragon.KprobeArgument_SockArg{SockArg: &tetragon.KprobeSock{Saddr: "10.0.2.21"}}}}}},
},
}
assert.True(t, fl.MatchOne(&ev))
ev = v1.Event{
Event: &tetragon.GetEventsResponse{
Event: &tetragon.GetEventsResponse_ProcessKprobe{
ProcessKprobe: &tetragon.ProcessKprobe{Args: []*tetragon.KprobeArgument{{Arg: &tetragon.KprobeArgument_SockArg{SockArg: &tetragon.KprobeSock{Saddr: "192.0.2.21"}}}}}},
},
}
assert.False(t, fl.MatchOne(&ev))
}

func TestIP(t *testing.T) {
log := logrus.New()
f := []*tetragon.Filter{{CelExpression: []string{"ip(process_kprobe.args[0].sock_arg.saddr).family() == 4"}}}
fl, err := BuildFilterList(context.Background(), f, []OnBuildFilter{NewCELExpressionFilter(log)})
assert.NoError(t, err)
ev := v1.Event{
Event: &tetragon.GetEventsResponse{
Event: &tetragon.GetEventsResponse_ProcessKprobe{
ProcessKprobe: &tetragon.ProcessKprobe{Args: []*tetragon.KprobeArgument{{Arg: &tetragon.KprobeArgument_SockArg{SockArg: &tetragon.KprobeSock{Saddr: "10.0.2.21"}}}}}},
},
}
assert.True(t, fl.MatchOne(&ev))
ev = v1.Event{
Event: &tetragon.GetEventsResponse{
Event: &tetragon.GetEventsResponse_ProcessKprobe{
ProcessKprobe: &tetragon.ProcessKprobe{Args: []*tetragon.KprobeArgument{{Arg: &tetragon.KprobeArgument_SockArg{SockArg: &tetragon.KprobeSock{Saddr: "2001:db8::abcd"}}}}}},
},
}
assert.False(t, fl.MatchOne(&ev))
}
10 changes: 5 additions & 5 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ gopkg.in/yaml.v2
# gopkg.in/yaml.v3 v3.0.1
## explicit
gopkg.in/yaml.v3
# k8s.io/api v0.31.3
# k8s.io/api v0.31.4
## explicit; go 1.22.0
k8s.io/api/admission/v1
k8s.io/api/admission/v1beta1
Expand Down Expand Up @@ -913,7 +913,7 @@ k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions/apiextensio
k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions/internalinterfaces
k8s.io/apiextensions-apiserver/pkg/client/listers/apiextensions/v1
k8s.io/apiextensions-apiserver/pkg/features
# k8s.io/apimachinery v0.31.3
# k8s.io/apimachinery v0.31.4
## explicit; go 1.22.0
k8s.io/apimachinery/pkg/api/equality
k8s.io/apimachinery/pkg/api/errors
Expand Down Expand Up @@ -978,7 +978,7 @@ k8s.io/apimachinery/pkg/watch
k8s.io/apimachinery/third_party/forked/golang/json
k8s.io/apimachinery/third_party/forked/golang/netutil
k8s.io/apimachinery/third_party/forked/golang/reflect
# k8s.io/apiserver v0.31.3
# k8s.io/apiserver v0.31.4
## explicit; go 1.22.0
k8s.io/apiserver/pkg/apis/cel
k8s.io/apiserver/pkg/authentication/serviceaccount
Expand All @@ -994,7 +994,7 @@ k8s.io/apiserver/pkg/features
k8s.io/apiserver/pkg/util/feature
k8s.io/apiserver/pkg/util/version
k8s.io/apiserver/pkg/warning
# k8s.io/client-go v0.31.3
# k8s.io/client-go v0.31.4
## explicit; go 1.22.0
k8s.io/client-go/applyconfigurations
k8s.io/client-go/applyconfigurations/admissionregistration/v1
Expand Down Expand Up @@ -1366,7 +1366,7 @@ k8s.io/code-generator/cmd/register-gen/generators
k8s.io/code-generator/pkg/namer
k8s.io/code-generator/pkg/util
k8s.io/code-generator/third_party/forked/golang/reflect
# k8s.io/component-base v0.31.3
# k8s.io/component-base v0.31.4
## explicit; go 1.22.0
k8s.io/component-base/cli/flag
k8s.io/component-base/featuregate
Expand Down

0 comments on commit 04225f1

Please sign in to comment.