# 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.33.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.33.0/policies/variables).

`globals`

Global variables declared in the [policy engine configuration](https://docs.cerbos.dev/cerbos/0.33.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`

`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.33.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.33.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_.*")
```

## 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) |

## Policy variables

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

## Accessing JWT claims

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

## Durations

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

## Timestamps

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