Skip to content
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

Bump CEL to v0.20.1 #1730

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions cmd/cel-eval/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func init() {
// revive:disable:unused-parameter

func rootRun(cmd *cobra.Command, args []string) {
if err := evalCEL(os.Stdout, expressionPath, httpPath); err != nil {
if err := evalCEL(cmd.Context(), os.Stdout, expressionPath, httpPath); err != nil {
log.Fatal(err)
}
}
Expand All @@ -54,7 +54,7 @@ func (sg secretGetter) Get(ctx context.Context, triggerNS string, sr *triggersv1
return nil, nil
}

func evalCEL(w io.Writer, expressionPath, httpPath string) error {
func evalCEL(ctx context.Context, w io.Writer, expressionPath, httpPath string) error {
// Read expression
expression, err := readExpression(expressionPath)
if err != nil {
Expand All @@ -74,9 +74,12 @@ func evalCEL(w io.Writer, expressionPath, httpPath string) error {

mapStrDyn := decls.NewMapType(decls.String, decls.Dyn)
env, err := cel.NewEnv(
triggerscel.Triggers(context.Background(), "default", secretGetter{}),
triggerscel.Triggers(ctx, "default", secretGetter{}),
celext.Strings(),
celext.Encoders(),
celext.Sets(),
celext.Lists(),
celext.Math(),
cel.Declarations(
decls.NewVar("body", mapStrDyn),
decls.NewVar("header", mapStrDyn),
Expand Down
3 changes: 2 additions & 1 deletion cmd/cel-eval/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package cmd

import (
"bytes"
"context"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestEvalCEL(t *testing.T) {
out := new(bytes.Buffer)
if err := evalCEL(out, "../testdata/expression.txt", "../testdata/http.txt"); err != nil {
if err := evalCEL(context.TODO(), out, "../testdata/expression.txt", "../testdata/http.txt"); err != nil {
t.Fatalf("evalCEL: %v", err)
}

Expand Down
25 changes: 19 additions & 6 deletions docs/cel_expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ You can either explicitly convert the number, or add another double value e.g.

```yaml
interceptors:
- cel:
overlays:
- ref:
name: cel
params:
- name: overlays:
- key: count_plus_1
expression: "body.count + 1.0"
- key: count_plus_2
Expand All @@ -85,10 +87,13 @@ The following example will generate an error with the JSON example.

```yaml
interceptors:
- cel:
overlays:
- key: bad_measure_times_3
expression: "body.measure * 3"
- ref:
name: cel
params:
- name: overlays:
values:
- key: bad_measure_times_3
expression: "body.measure * 3"
```

**bad_measure_times_3** will fail with
Expand Down Expand Up @@ -151,6 +156,14 @@ This would add an extensions key `filtered` with only one of the labels.
All the functionality from the cel-go project's [CEL extension](https://github.com/google/cel-go/tree/master/ext) is available in
your CEL expressions.

The following extensions are available:

* [Strings](https://pkg.go.dev/github.com/google/cel-go@v0.20.1/ext#Strings).
* [Encoders](https://pkg.go.dev/github.com/google/cel-go@v0.20.1/ext#Encoders)
* [Sets](https://pkg.go.dev/github.com/google/cel-go@v0.20.1/ext#Sets)
* [Lists](https://pkg.go.dev/github.com/google/cel-go@v0.20.1/ext#Lists)
* [Math](https://pkg.go.dev/github.com/google/cel-go@v0.20.1/ext#Math)

### cel-go Bytes

The cel-go project function `base64.decode` returns a [CEL `Bytes`](https://github.com/google/cel-spec/blob/master/doc/langdef.md#string-and-bytes-values) value.
Expand Down
3 changes: 3 additions & 0 deletions pkg/interceptors/cel/cel.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ func makeCelEnv(ctx context.Context, ns string, sg interceptors.SecretGetter) (*
Triggers(ctx, ns, sg),
celext.Strings(),
celext.Encoders(),
celext.Sets(),
celext.Lists(),
celext.Math(),
cel.Declarations(
decls.NewVar("body", mapStrDyn),
decls.NewVar("header", mapStrDyn),
Expand Down
16 changes: 16 additions & 0 deletions pkg/interceptors/cel/cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,23 @@ func TestExpressionEvaluation(t *testing.T) {
expr: `body.numbers.first()`,
want: types.Int(1),
},
{
name: "sets extension sets.contains",
expr: `sets.contains(body.numbers, [1, 6])`,
want: types.False,
},
{
name: "slicing arrays",
expr: `[1,2,3,4].slice(1,3)`,
want: reg.NativeToValue([]int{2, 3}),
},
{
name: "cel maths",
expr: `math.greatest(body.numbers)`,
want: types.Int(5),
},
}

for _, tt := range tests {
t.Run(tt.name, func(rt *testing.T) {
ctx, _ := test.SetupFakeContext(rt)
Expand Down