CEL expression extensions

The CEL expression is configured to expose parts of the request, and some custom functions to make matching easier.

In addition to the custom function extension listed below, you can craft any valid CEL expression as defined by the cel-spec language definition

String functions

The upstream CEL implementation provides extensions to the CEL specification for manipulating strings.

For example:

'refs/heads/master'.split('/') // result = list ['refs', 'heads', 'master']
'my place'.replace('my ',' ') // result = string 'place'
'this that another'.replace('th ',' ', 2) // result = 'is at another'

The replace overload allows an optional limit on replacements.

Notes on numbers in CEL expressions

One thing to be aware of is how numeric values are treated in CEL expressions, JSON numbers are decoded to CEL double values.

For example:

  "count": 2,
  "measure": 1.7

In the JSON above, both numbers are parsed as CEL double (Go float64) values.

This means that if you want to do integer arithmetic, you’ll need to use explicit conversion functions.

From the CEL specification:

Note that currently there are no automatic arithmetic conversions for the numeric types (int, uint, and double).

You can either explicitly convert the number, or add another double value e.g.

  - cel:
        - key: count_plus_1
          expression: "body.count + 1.0"
        - key: count_plus_2
          expression: "int(body.count) + 2"
        - key: measure_times_3
          expression: "body.measure * 3.0"

These will be serialised back to JSON appropriately:

  "count_plus_1": 2,
  "count_plus_2": 3,
  "measure_times_3": 5.1

Error messages in conversions

The following example will generate an error with the JSON example.

  - cel:
        - key: bad_measure_times_3
          expression: "body.measure * 3"

bad_measure_times_3 will fail with failed to evaluate overlay expression 'body.measure * 3': no such overload because there’s no automatic conversion.

cel-go extensions

All the functionality from the cel-go project’s String extension is available in your CEL expressions.

List of extensions

The body from the http.Request value is decoded to JSON and exposed, and the headers are also available.

Symbol Type Description Example
body map(string, dynamic) This is the decoded JSON body from the incoming http.Request exposed as a map of string keys to any value types.
body.value == 'test'
header map(string, list(string)) This is the request Header.
header['X-Test'][0] == 'test-value'
requestURL string This is the URL for the incoming HTTP request.

NOTE: The header value is a Go http.Header, which is defined as:

type Header map[string][]string

i.e. the header is a mapping of strings, to arrays of strings, see the match function on headers below for an extension that makes looking up headers easier.

List of extension functions

This lists custom functions that can be used from CEL expressions in the CEL interceptor.

Symbol Type Description Example
match header.match(string, string) -> bool Uses the canonical header matching from Go's http.Request to match the header against the value.
header.match('x-test', 'test-value')
canonical header.canonical(string) -> string Uses the canonical header matching from Go's http.Request to get the provided header name.
<string>.truncate(uint) -> string
Truncates a string to no more than the specified length.
<string>.split(string) -> string(dyn)
Splits a string on the provided separator value.
<string>.decodeb64() -> string
Decodes a base64 encoded string.
<string>.compareSecret(string, string, string) -> bool
Constant-time comparison of strings against secrets, this will fetch the secret using the combination of namespace/name and compare the token key to the string using a cryptographic constant-time comparison..

The event-listener service account must have access to the secret.

header.canonical('X-Secret-Token').compareSecret('', 'secret-name', 'namespace')
<string>.compareSecret(string, string) -> bool
This is almost identical to the version above, but only requires two arguments, the namespace is assumed to be the namespace for the event-listener.
header.canonical('X-Secret-Token').compareSecret('key', 'secret-name')
<string>.parseJSON() -> map<string, dyn>
This parses a string that contains a JSON body into a map which which can be subsequently used in other expressions.
'{"testing":"value"}'.parseJSON().testing == "value"
<string>.parseYAML() -> map<string, dyn>
This parses a string that contains a YAML body into a map which which can be subsequently used in other expressions.
'key1: value1\nkey2: value2\n'.parseYAML().key1 == "value"
<string>.parseURL() -> map<string, dyn>
This parses a string that contains a URL into a map with keys for the elements of the URL.
The resulting map will contain the following keys for this URL "https://user:pass@example.com/test/path?s=testing#first"
query{"s": "testing"}
queryStrings{"s": ["testing"]}
auth{"username": "user", "password": "pass"}
Note the difference between query and queryStrings, in query, multiple query params with the same name would be comma separated, for the case where a single string is provided, this will just be the single string value. For queryString the query param values are provided as a list, which can be accessed by indexing.
'https://example.com/test?query=testing'.parseURL().query['query'] == "testing"

Last modified January 1, 0001