# Conditions

|     |     |
| --- | --- |
|  | This documentation is for<br>a previous<br>version of Cerbos. Choose 0.53.0 from the version picker at the top right or navigate to [https://docs.cerbos.dev](https://docs.cerbos.dev/) for the latest version. |

A powerful feature of Cerbos policies is the ability to define conditions that are evaluated against the data provided in the request. Conditions are written using the [Common Expression Language (CEL)](https://github.com/google/cel-spec/blob/master/doc/intro.md).

|     |     |
| --- | --- |
|  | Cerbos ships with an interactive REPL that can be used to experiment with writing CEL conditions. It can be started by running `cerbos repl`. See [the REPL documentation](https://docs.cerbos.dev/cerbos/0.41.0/cli/cerbos#repl) for more information. |

Every condition expression must evaluate to a boolean true/false value. A condition block in a policy can contain either a single condition expression, or multiple expressions combined using the `all`, `any`, or `none` operators. These logical operators may be nested.

## Condition block

```yaml
condition:
  match:
    all:
      of:
        - expr: request.resource.attr.status == "PENDING_APPROVAL"
        - expr: >
            "GB" in request.resource.attr.geographies
```

## Top-level identifiers

Within a condition expression, you have access to several top-level identifiers:

`request`

Data provided in the check or plan request (principal, resource, and auxiliary data).

`runtime`

Additional data computed while evaluating the policy.

`variables`

Variables declared in the [`variables` section of the policy](https://docs.cerbos.dev/cerbos/0.41.0/policies/variables#variables).

`constants`

Variables declared in the [`constants` section of the policy](https://docs.cerbos.dev/cerbos/0.41.0/policies/variables#constants).

`globals`

Global variables declared in the [policy engine configuration](https://docs.cerbos.dev/cerbos/0.41.0/configuration/engine#globals).

There are also single-letter aliases available to allow you to write terser expressions:

`P`

`request.principal`

`R`

`request.resource`

`V`

`variables`

`C`

`constants`

`G`

`globals`

## The `request` object

```yaml
request:
  principal: (1)
    id: alice (2)
    roles: (3)
      - employee
    attr: (4)
      geography: GB

resource: (5)
    kind: leave_request (6)
    id: XX125 (7)
    attr: (8)
      owner: alice

auxData: (9)
    jwt: (10)
      iss: acme.corp
```

|     |     |
| --- | --- |
| **1** | The principal whose permissions are being checked. |
| **2** | ID of the principal. |
| **3** | Static roles that are assigned to the principal by your identity management system. |
| **4** | Free-form context data about the principal. |
| **5** | The resource on which the principal is performing actions. |
| **6** | Resource kind. |
| **7** | ID of the resource instance. |
| **8** | Free-form context data about the resource instance. |
| **9** | [Auxiliary data sources](https://docs.cerbos.dev/cerbos/0.41.0/configuration/auxdata). |
| **10** | JWT claims. |

## The `runtime` object

```yaml
runtime:
  effectiveDerivedRoles: (1)
    - owner
    - gb_employee
```

|     |     |
| --- | --- |
| **1** | [Derived roles](https://docs.cerbos.dev/cerbos/0.41.0/policies/derived_roles) that were assigned to to the principal by Cerbos while evaluating the policy. This is only populated in expressions in resource policies, and only includes derived roles that are referenced in at least one policy rule. |

## Expressions and blocks

### Single boolean expression

```yaml
condition:
  match:
    expr: P.id.matches("^dev_.*")
```

### `all` operator: all expressions must evaluate to true (logical AND)

```yaml
condition:
  match:
    all:
      of:
        - expr: R.attr.status == "PENDING_APPROVAL"
        - expr: >
            "GB" in R.attr.geographies
        - expr: P.attr.geography == "GB"
```

### `any` operator: only one of the expressions has to evaluate to true (logical OR)

```yaml
condition:
  match:
    any:
      of:
        - expr: R.attr.status == "PENDING_APPROVAL"
        - expr: >
            "GB" in R.attr.geographies
        - expr: P.attr.geography == "GB"
```

### `none` operator: none of the expressions should evaluate to true (logical negation)

```yaml
condition:
  match:
    none:
      of:
        - expr: R.attr.status == "PENDING_APPROVAL"
        - expr: >
            "GB" in R.attr.geographies
        - expr: P.attr.geography == "GB"
```

### Nesting operators

```yaml
condition:
  match:
    all:
      of:
        - expr: R.attr.status == "DRAFT"
        - any:
            of:
              - expr: R.attr.dev == true
              - expr: R.attr.id.matches("^[98][0-9]+")
        - none:
            of:
              - expr: R.attr.qa == true
              - expr: R.attr.canary == true
```

The above nested block is equivalent to the following:

```yaml
condition:
  match:
    expr: >
      (R.attr.status == "DRAFT" &&
        (R.attr.dev == true || R.attr.id.matches("^[98][0-9]+")) &&
        !(R.attr.qa == true || R.attr.canary == true))
```

### Quotes in expressions

Single and double quotes have special meanings in YAML. To avoid parsing errors when your expression contains quotes, use the YAML block scalar syntax or wrap the expression in parentheses.

```yaml
expr: >
  "GB" in R.attr.geographies
```

```yaml
expr: ("GB" in R.attr.geographies)
```

## Policy variables

To avoid duplication in condition expressions, you can define [variables and constants in policies](https://docs.cerbos.dev/cerbos/0.41.0/policies/variables).

## Auxiliary data

If you have [auxiliary data sources configured](https://docs.cerbos.dev/cerbos/0.41.0/configuration/auxdata), they can be accessed using `request.auxData`.

### Accessing JWT claims

```yaml
"cerbie" in request.auxData.jwt.aud && request.auxData.jwt.iss == "cerbos"
```

## Operators

| Operator | Description |
| --- | --- |
| `!` | Logical negation (NOT) |
| `-` | Subtraction/numeric negation |
| `!=` | Unequals |
| `%` | Modulo |
| `&&` | Logical AND |
| `||` | Logical OR |
| `*` | Multiplication |
| `+` | Addition/concatenation |
| `/` | Division |
| `⇐` | Less than or equal to |
| `<` | Less than |
| `==` | Equals |
| `>=` | Greater than or equal to |
| `>` | Greater than |
| `in` | Membership in lists or maps |
| `? :` | Ternary condition (if-then-else) |

## Durations

| Suffix | Unit |
| --- | --- |
| `ns` | Nanoseconds |
| `us` | Microseconds |
| `ms` | Milliseconds |
| `s` | Seconds |
| `m` | Minutes |
| `h` | Hours |

### Test data

```json
...
"resource": {
  "kind": "leave_request",
  "attr": {
    "cooldownPeriod": "3750s",
    "lastAccessed": "2021-04-20T10:00:20.021-05:00"
  }
}
... 
```

| Function | Description | Example |
| --- | --- | --- |
| `duration` | Convert a string to a duration. The string must contain a valid duration suffixed by one of `ns`, `us`, `ms`, `s`, `m` or `h`. E.g. `3750s` | `duration(R.attr.cooldownPeriod).getSeconds() == 3750` |
| `getHours` | Get hours from a duration | `duration(R.attr.cooldownPeriod).getHours() == 1` |
| `getMilliseconds` | Get milliseconds from a duration | `duration(R.attr.cooldownPeriod).getMilliseconds() == 3750000` |
| `getMinutes` | Get minutes from a duration | `duration(R.attr.cooldownPeriod).getMinutes() == 62` |
| `getSeconds` | Get seconds from a duration | `duration(R.attr.cooldownPeriod).getSeconds() == 3750` |
| `timeSince` | Time elapsed since the given timestamp to current time on the server. This is a Cerbos extension to CEL | `timestamp(R.attr.lastAccessed).timeSince() > duration("1h")` |

### Hierarchies

|     |     |
| --- | --- |
|  | The hierarchy functions are Cerbos-specific extensions to CEL. |

### Test data

```json
...
"principal": {
  "id": "john",
  "roles": ["employee"],
  "attr": {
    "scope": "foo.bar.baz.qux",
  }
},
"resource": {
  "kind": "leave_request",
  "attr": {
    "scope": "foo.bar",
  }
}
...
```

| Function | Description | Example |
| --- | --- | --- |
| `hierarchy` | Convert a dotted string or a string list to a hierarchy | `hierarchy("a.b.c") == hierarchy(["a","b","c"])` |
| `hierarchy` | Convert a delimited string representation to a hierarchy | `hierarchy("a:b:c", ":").size() == 3` |
| `ancestorOf` | Returns true if the first hierarchy shares a common prefix with the second hierarchy | `hierarchy("a.b").ancestorOf(hierarchy("a.b.c.d")) == true` |
| `commonAncestors` | Returns the common ancestor hierarchy | `hierarchy(R.attr.scope).commonAncestors(hierarchy(P.attr.scope)) == hierarchy("foo.bar")` |
| `descendentOf` | Mirror function of `ancestorOf` | `hierarchy("a.b.c.d").descendentOf(hierarchy("a.b")) == true` |
| `immediateChildOf` | Returns true if the first hierarchy is a first-level child of the second hierarchy | `hierarchy("a.b.c").immediateChildOf(hierarchy("a.b")) == true && hierarchy("a.b.c.d").immediateChildOf(hierarchy("a.b")) == false` |
| `immediateParentOf` | Mirror function of `immediateChildOf` | `hierarchy("a.b").immediateParentOf(hierarchy("a.b.c")) == true && hierarchy("a.b").immediateParentOf(hierarchy("a.b.c.d")) == false` |
| `overlaps` | Returns true if one of the hierarchies is a prefix of the other | `hierarchy("a.b.c").overlaps(hierarchy("a.b.c.d.e")) == true && hierarchy("a.b.x").overlaps(hierarchy("a.b.c.d.e")) == false` |
| `siblingOf` | Returns true if both hierarchies share the same parent | `hierarchy("a.b.c").siblingOf(hierarchy("a.b.d")) == true` |
| `size` | Returns the number of levels in the hierarchy | `hierarchy("a.b.c").size() == 3` |

## IP addresses

|     |     |
| --- | --- |
|  | The IP address functions are Cerbos-specific extensions to CEL. |

### Test data

```json
...
"principal": {
  "id": "elmer_fudd",
  "attr": {
    "ipv4Address": "192.168.0.10",
    "ipv6Address": "2001:0db8:0000:0000:0000:0000:1000:0000"
  }
}
...
```

| Function | Description | Example |
| --- | --- | --- |
| `inIPAddrRange` | Check whether the IP address is in the range defined by the CIDR | `P.attr.ipv4Address.inIPAddrRange("192.168.0.0/24") && P.attr.ipv6Address.inIPAddrRange("2001:db8::/48")` |

## Lists and maps

### Test data

```json
...
"principal": {
  "id": "elmer_fudd",
  "attr": {
    "id": "125",
    "teams": ["design", "communications", "product", "commercial"],
    "limits": {
        "design": 10,
        "product": 25
    },
    "clients": {
      "acme": {"active": true},
      "bb inc": {"active": true}
    }
  }
}
...
```

| Operator/Function | Description | Example |
| --- | --- | --- |
| `+` | Concatenates lists | `P.attr.teams + ["design", "engineering"]` |
| `[]` | Index into a list or a map | `P.attr.teams[0] == "design" && P.attr.clients["acme"]["active"] == true` |
| `all` | Check whether all elements in a list match the predicate. | `P.attr.teams.all(t, size(t) > 3) && [1, 2, 3].all(i, j, i < j)` |
| `distinct` | Returns the distinct elements of a list | `[1, 2, 2, 3, 3, 3].distinct() == [1, 2, 3]` |
| `except` | Produces the set difference of two lists | `P.attr.teams.except(["design", "engineering"]) == ["communications", "product", "commercial"]` |
| `exists` | Check whether at least one element matching the predicate exists in a list or map. | `P.attr.teams.exists(t, t.startsWith("comm")) && P.attr.limits.exists(k, v, k == "design" && v > 0)` |
| `exists_one` | Check that only one element matching the predicate exists. | `P.attr.teams.exists_one(t, t.startsWith("comm")) == false && P.attr.limits.exists_one(k, v, k == "design" && v > 0) == false` |
| `filter` | Filter a list using the predicate. | `size(P.attr.teams.filter(t, t.matches("^comm"))) == 2` |
| `flatten` | Flattens a list. If an optional depth is provided, the list is flattened to the specified level | `[1,2,[],[],[3,4]].flatten() == [1, 2, 3, 4] && [1,[2,[3,[4]]]].flatten(2) == [1, 2, 3, [4]]` |
| `hasIntersection` | Checks whether the lists have at least one common element | `hasIntersection(["design", "engineering"], P.attr.teams)` |
| `in` | Check whether the given element is contained in the list or map | `("design" in P.attr.teams) && ("acme" in P.attr.clients)` |
| `intersect` | Produces the set intersection of two lists | `intersect(["design", "engineering"], P.attr.teams) == ["design"]` |
| `isSubset` | Checks whether the list is a subset of another list | `["design", "engineering"].isSubset(P.attr.teams) == false` |
| `lists.range` | Returns a list of integers from 0 to n-1 | `lists.range(5) == [0, 1, 2, 3, 4]` |
| `map` | Transform each element in a list | `"DESIGN" in P.attr.teams.map(t, t.upperAscii())` |
| `reverse` | Returns the elements of a list in reverse order | `[5, 3, 1, 2].reverse() == [2, 1, 3, 5]` |
| `size` | Number of elements in a list or map | `size(P.attr.teams) == 4 && size(P.attr.clients) == 2` |
| `slice` | Returns a new sub-list using the indexes provided | `[1,2,3,4].slice(1, 3) == [2, 3]` |
| `sort` | Sorts a list with comparable elements | `[3, 2, 1].sort() == [1, 2, 3]` |
| `transformList` | Converts a map or a list into a list value. The output expression determines the contents of the output list. Elements in the list may optionally be filtered | `[1, 2, 3].transformList(i, v, i > 0, 2 * v) == [4, 6] &&<br>[1, 2, 3].transformList(i, v, 2 * v) == [2, 4, 6]` |

## Math

| Function | Description | Example |
| --- | --- | --- |
| `math.abs` | Returns the absolute value of the numeric type provided as input | `math.abs(1.2) == 1.2 && math.abs(-2) == 2` |
| `math.ceil` | Compute the ceiling of a double value | `math.ceil(1.2) == 2.0 && math.ceil(-1.2) == -1.0` |
| `math.round` | Rounds the double value to the nearest whole number with ties rounding away from zero, e.g. 1.5 → 2.0, -1.5 → -2.0 | `math.round(1.2) == 1.0 && math.round(1.5) == 2.0 && math.round(-1.5) == -2.0` |

## Strings

### Test data

```json
...
"resource": {
  "kind": "leave_request",
  "attr": {
    "id": "125",
    "department": "marketing"
  }
}
...
```

| Function | Description | Example |
| --- | --- | --- |
| `base64.encode` | Encode as base64 | `base64.encode(bytes("hello")) == "aGVsbG8="` |
| `contains` | Check whether a string contains the given substring | `R.attr.department.contains("arket")` |
| `size` | Get the length of the string | `size(R.attr.department) == 9` |
| `trim` | Remove whitespace from beginning and end | `"  marketing  ".trim() == "marketing"` |

## Timestamps

### Test data

```json
...
"resource": {
  "kind": "leave_request",
  "attr": {
    "lastAccessed": "2021-04-20T10:00:20.021-05:00",
    "lastUpdateTime": "2021-05-01T13:34:12.024Z",
  }
}
...
```

| Function | Description | Example |
| --- | --- | --- |
| `timestamp` | Convert an RFC3339 formatted string to a timestamp | `timestamp(R.attr.lastAccessed).getFullYear() == 2021` |
| `getDate` | Get day of month from a timestamp | `timestamp(R.attr.lastAccessed).getDate() == 20` |
| `now` | Current time on the server. This is a Cerbos extension to CEL | `now() > timestamp(R.attr.lastAccessed)` |
