| 1 |
# CEL Policy |
| 2 |
|
| 3 |
The Common Expression Language (CEL) supports simple expressions: no variables, |
| 4 |
functions, or modules. However, CEL expression graphs can be composed together, |
| 5 |
allowing for reuse and development clarity which is not otherwise possible |
| 6 |
within CEL. |
| 7 |
|
| 8 |
To address this case, we're introducing the CEL Policy format which is fully |
| 9 |
runtime compatible with CEL. All of the same performance and safety hardening |
| 10 |
guarantees which apply to CEL also apply to CEL Policy. The net effect is |
| 11 |
significantly improved authoring and testability. The YAML-based policy format |
| 12 |
is easily extensible and inspired by [Kubernetes Admission |
| 13 |
Policy](https://kubernetes.io/docs/reference/access-authn-authz/validating-admission-policy/) |
| 14 |
with CEL. |
| 15 |
|
| 16 |
## Policy Language |
| 17 |
|
| 18 |
A policy is a named instance of a rule which consists of a set of conditional |
| 19 |
outputs and conditional sub-rules. Matches within the rule and subrules are |
| 20 |
combined and ordered according to the policy evaluation semantic. The default |
| 21 |
semantic is `FIRST_MATCH`. The supported top-level fields in a policy include: |
| 22 |
`name`, `description`, `imports`, and `rule.` |
| 23 |
|
| 24 |
- `name (string)`: a system-specific identifier for the policy |
| 25 |
- `description (string)`: a human-readable description of the policy |
| 26 |
- `imports (list[string])`: a list of aliases. see description below |
| 27 |
- `rule (object)`: the entry point for the policy. see description below |
| 28 |
|
| 29 |
### Rule |
| 30 |
|
| 31 |
The `rule` node in a policy is the primary entry point to CEL computations. |
| 32 |
Fields above the `rule` are intended to simplify or support the CEL expressions |
| 33 |
within the `rule` block. For example, the [`imports` list refers to a set of |
| 34 |
type names](#Imports) who should be imported by their simple name within the CEL |
| 35 |
expressions contained in the `rule`. |
| 36 |
|
| 37 |
#### Variables |
| 38 |
|
| 39 |
A `rule` has a single `variables` block. Variables are written as an ordered |
| 40 |
list. Variables may refer to another variable; however, the variable must be |
| 41 |
declared before use, i.e. be defined before it is referenced. A variable has a |
| 42 |
`name` and an `expression`. |
| 43 |
|
| 44 |
``` |
| 45 |
variables: |
| 46 |
- name: first_item |
| 47 |
expression: "1" |
| 48 |
- name: list_of_items |
| 49 |
expression: "[variables.first_item, 2, 3, 4]" |
| 50 |
``` |
| 51 |
|
| 52 |
Variables in CEL Policy are lazily evaluated and memoized as CEL is side-effect |
| 53 |
free. Only the variables which are accessed during a `match` `condition` or an |
| 54 |
`output` are evaluated. The use of a variable is equivalent to using the |
| 55 |
`cel.bind()` macro to introduce local computations within a CEL expression. |
| 56 |
|
| 57 |
#### Match |
| 58 |
|
| 59 |
A `rule` has a single `match` block. The match block should have at least one |
| 60 |
`output` value, though `output` expressions may be `condition`al. The default |
| 61 |
evaluation order for the sequence of matches is top-down, first-match. |
| 62 |
|
| 63 |
``` |
| 64 |
rule: |
| 65 |
match: |
| 66 |
- condition: "request.user.name.startsWith('j')" |
| 67 |
output: "Hi, J!" |
| 68 |
- output: "Hi, " + request.user.name + "!" |
| 69 |
``` |
| 70 |
|
| 71 |
In the example, the policy will alternate the decision based on the user's first |
| 72 |
name, choosing either to greet them by first initial or by full name if the name |
| 73 |
does not start with `j`. This is equivalent to the following CEL expression: |
| 74 |
|
| 75 |
``` |
| 76 |
request.user.name.startsWith('j') |
| 77 |
? "Hi, J!" |
| 78 |
: "Hi, " + request.user.name + "!" |
| 79 |
``` |
| 80 |
|
| 81 |
For simple cases, this ternary may be simpler to write; however, as the number |
| 82 |
of cases grows the ternary becomes less and less readable and the policy format |
| 83 |
allows for simpler edits in addition to expression composition: |
| 84 |
|
| 85 |
``` |
| 86 |
rule: |
| 87 |
variables: |
| 88 |
- name: name |
| 89 |
expression: "request.user.name" |
| 90 |
match: |
| 91 |
- condition: "variables.name.startsWith('j')" |
| 92 |
output: "Hi, J!" |
| 93 |
- output: "Hi, " + variables.name + "!" |
| 94 |
``` |
| 95 |
|
| 96 |
When the `condition` is absent it defaults to `true`. Since the evaluation |
| 97 |
algorithm is first-match, an `output` without a `condition` behaves like a |
| 98 |
default evaluation result if no other match conditions are satisfied. |
| 99 |
|
| 100 |
#### Condition |
| 101 |
|
| 102 |
A `condition` expression must type-check to a `bool` return type. When a |
| 103 |
`condition` predicate evaluates to `true`, either an `output` expression is |
| 104 |
returned or a nested `rule` result is returned. Using a `condition` with nested |
| 105 |
`rule` values allows for the declaration of `rule` blocks with local `variables` |
| 106 |
and reduces the complexity of `condition` expressions within the nested `rule`. |
| 107 |
|
| 108 |
If all `output` expressions within a `rule` have associated `condition` |
| 109 |
predicates, then the return type of the policy is `optional_type(type(output))`. |
| 110 |
In other words, if the policy is evaluating `true` or `false` output |
| 111 |
expressions, but all output values are conditional, then the output type of the |
| 112 |
policy is `optional_type(bool)`. If the nested `rule` does not result in an |
| 113 |
output, then the `optional.none()` value is returned as the overall policy |
| 114 |
result. |
| 115 |
|
| 116 |
Taking our example from earlier, since the `match` is exhaustive and includes a |
| 117 |
default `output`, then the result type of this policy is `string` |
| 118 |
|
| 119 |
``` |
| 120 |
rule: |
| 121 |
variables: |
| 122 |
- name: name |
| 123 |
expression: "request.user.name" |
| 124 |
match: |
| 125 |
- condition: "variables.name.startsWith('j')" |
| 126 |
output: "Hi, J!" |
| 127 |
- output: "Hi, " + variables.name + "!" |
| 128 |
``` |
| 129 |
|
| 130 |
If we remove the last output, then the result type is `optional_type(string)` |
| 131 |
since not all evaluation paths will result in an `output`. |
| 132 |
|
| 133 |
``` |
| 134 |
rule: |
| 135 |
match: |
| 136 |
- condition: "request.user.name.startsWith('j')" |
| 137 |
output: "Hi, J!" |
| 138 |
``` |
| 139 |
|
| 140 |
For more information on optionals, see |
| 141 |
https://github.com/google/cel-spec/wiki/proposal-246 for more information about |
| 142 |
`optional` values within CEL. |
| 143 |
|
| 144 |
#### Output |
| 145 |
|
| 146 |
The `output` field is optional and is, effectively, just like any other CEL |
| 147 |
expression; however, the output expression types must all agree within the |
| 148 |
policy expression graph. An output expression may be simple, such as a `bool` or |
| 149 |
`string` value, or it may be much more complex such as a JSON-like `map` or a |
| 150 |
strongly-typed object like a protocol buffer message. |
| 151 |
|
| 152 |
The following example presents a very subtle distinction between the output |
| 153 |
types with a `bool` or a `string` as the possible output type. |
| 154 |
|
| 155 |
``` |
| 156 |
rule: |
| 157 |
match: |
| 158 |
- condition: "true" |
| 159 |
output: "true" |
| 160 |
- output: "'true'" |
| 161 |
``` |
| 162 |
|
| 163 |
This configuration is invalid and will trigger a compilation error: |
| 164 |
|
| 165 |
``` |
| 166 |
incompatible output types: bool not assignable to string |
| 167 |
``` |
| 168 |
|
| 169 |
#### Nesting |
| 170 |
|
| 171 |
Rules may be nested as part of a `match`. A `match` that nests a rule specifies |
| 172 |
only a `condition` and the nested `rule`. |
| 173 |
|
| 174 |
Example: |
| 175 |
``` |
| 176 |
rule: |
| 177 |
match: |
| 178 |
- condition: "outer == 'condition_a'" |
| 179 |
rule: |
| 180 |
match: |
| 181 |
- condition: "inner == 'condition_a_1'" |
| 182 |
output: "'outer_a_inner_1'" |
| 183 |
- output: "'outer_a_inner_default'" |
| 184 |
- output: "'outer_default'" |
| 185 |
``` |
| 186 |
|
| 187 |
### Imports |
| 188 |
|
| 189 |
When constructing complex object types such as protocol buffers, `imports` can |
| 190 |
be useful in simplifying object construction. |
| 191 |
|
| 192 |
As an example, let's use the following protocol buffer message definitions: |
| 193 |
|
| 194 |
``` |
| 195 |
package dev.cel.example; |
| 196 |
|
| 197 |
message ComplexDocument { |
| 198 |
message Section { |
| 199 |
string name = 1; |
| 200 |
string author = 2; |
| 201 |
google.protobuf.Timestamp created_at = 3; |
| 202 |
google.protobuf.Timestamp last_modified = 4; |
| 203 |
} |
| 204 |
string title = 1; |
| 205 |
Section sections = 2; |
| 206 |
} |
| 207 |
``` |
| 208 |
|
| 209 |
To construct an instance of a document like this within CEL, the fully |
| 210 |
qualified type names must be used: |
| 211 |
|
| 212 |
``` |
| 213 |
rule: |
| 214 |
match: |
| 215 |
- output: > |
| 216 |
dev.cel.example.ComplexDocument{ |
| 217 |
title: "Example Document" |
| 218 |
sections: [ |
| 219 |
dev.cel.example.ComplexDocument.Section{ |
| 220 |
name: "Overview", |
| 221 |
author: "[email protected]", |
| 222 |
created_at: timestamp("2024-09-20T16:50:00Z") |
| 223 |
} |
| 224 |
] |
| 225 |
} |
| 226 |
``` |
| 227 |
|
| 228 |
Using the `imports` clause the policy, the type name and expression can be |
| 229 |
simplified: |
| 230 |
|
| 231 |
``` |
| 232 |
imports: |
| 233 |
- name: dev.cel.example.ComplexDocument |
| 234 |
- name: dev.cel.example.ComplexDocument.Section |
| 235 |
|
| 236 |
rule: |
| 237 |
match: |
| 238 |
- output: > |
| 239 |
ComplexDocument{ |
| 240 |
title: "Example Document" |
| 241 |
sections: [Section{ |
| 242 |
name: "Overview", |
| 243 |
author: "[email protected]", |
| 244 |
created_at: timestamp("2024-09-20T16:50:00Z") |
| 245 |
}] |
| 246 |
} |
| 247 |
``` |
| 248 |
|
| 249 |
### Non-standard YAML Behaviors |
| 250 |
|
| 251 |
CEL Expression values follow special parsing rules to preserve source location |
| 252 |
information inside the policy document. This means that the indent and any |
| 253 |
newline characters are included in the effective CEL expression. There is no |
| 254 |
difference in behavior for the literal or folded style for multiline |
| 255 |
expressions. |
| 256 |
|
| 257 |
Example: |
| 258 |
|
| 259 |
```yaml |
| 260 |
name: policy |
| 261 |
|
| 262 |
rule: |
| 263 |
match: |
| 264 |
- condition: 'true' |
| 265 |
- output: > |
| 266 |
strings.quote('''A mutiline string |
| 267 |
second line''') |
| 268 |
|
| 269 |
``` |
| 270 |
|
| 271 |
Results in: |
| 272 |
|
| 273 |
``` |
| 274 |
"A multiline string\n second line" |
| 275 |
``` |