Cerbos PDP v0.58.0: CEL set functions, JSON and YAML parsing

AAlex OlivierNovember 27, 20265 min read
Cerbos PDP v0.58.0: CEL set functions, JSON and YAML parsing

With Cerbos PDP v0.58.0 you can write a clearance check like “every label on this document is one the user holds” as R.attr.labels.hasOnly(P.attr.clearances), and read attributes that arrive as JSON or YAML strings without reshaping them first. Teams that run several policy versions can now use version conditions in query plans and give each version of a role policy its own parent roles. Everything is listed in the v0.58.0 release notes.

New CEL list functions for set comparisons in policy conditions

Four new functions cover the list comparisons that come up most often in policy conditions. hasAll checks that a list contains every element of another list, hasAny checks that two lists share at least one element, hasExactly checks that two lists hold the same elements regardless of order or duplicates, and hasOnly checks that every element of a list appears in another list.

Take a document that carries a set of labels, where a user may only open it if they're cleared for every label on it.

condition:
  match:
    expr: R.attr.labels.hasOnly(P.attr.clearances)

isSubset and the sets functions added in v0.55.0 already cover some of these checks. The new methods give each comparison a name that reads the way you’d say the rule. hasAny does the same job as hasIntersection and hasIntersection, so new policies can use it once your PDPs are on v0.58.0.

Parse JSON and YAML strings inside policy conditions

Attributes don't always arrive as structured data. A database column, a JWT claim or a field passed through from another system can hold a JSON or YAML document as a plain string. The new json.parse and yaml.parse functions turn that string into a value your condition can read, and yaml.encode joins the existing json.encode for going the other way.

Both parse functions return an optional value, which is empty when the string isn't valid. That lets a condition handle bad input explicitly, and pairing it with a local variable keeps the expression readable.

variables:
  local:
    metadata: json.parse(R.attr.metadata)
rules:
  - actions: ["view"]
    effect: EFFECT_ALLOW
    roles: ["user"]
    condition:
      match:
        expr: V.metadata.hasValue() && V.metadata.value().owner == P.id

Pass a type as the second argument, as in json.parse('123', int), to parse directly into that type. Query plans can't use json.parse or yaml.parse when the string comes from resource attributes, so keep those conditions to CheckResources or parse principal attributes instead. The conditions reference has examples for every new function.

Run several policy versions side by side in checks, query plans and role policies

Policy versions let you keep several versions of a policy for the same resource kind, such as default and v2, and pick one per request. In v0.58.0 the version carries through query plans, principal policies and role policies.

The query planner now passes principal and resource policy versions through to plan evaluation. A condition such as request.principal.policyVersion == request.resource.policyVersion now evaluates in PlanResources the same way it does in CheckResources. The planner treats the resource policy version as known, in the same way as the resource kind and scope, so R.policyVersion no longer appears in plan filters. If you translate query plans into database queries, the filters you get for these policies are now simpler.

Principal policies are now resolved using the principal policy version when it differs from the resource policy version.

Role policies now track parent roles per policy version. Parent roles declared in a role policy apply only to requests for that version, so you can run two versions of a role policy side by side with different parent roles. If a bundle compiled by an earlier PDP version holds several versions of a role policy for the same role and scope, recompile it to give each version its own parent roles. The PDP logs a warning until you do.

Scoped policy resolution, lenient scope search and and schema validation fixes

Scoped policies let you keep a shared base set of rules and override them per tenant, team or region. v0.58.0 makes three changes to how scoped requests resolve.

When the requested scope has no policy for the requested resource kind, principal or version, a scoped request no longer falls through to a policy in a parent scope. If you want requests to resolve to the nearest scope that does have a policy, turn on lenient scope search, which exists for exactly that case. With lenient scope search turned on, a request for a scope with no policies now also picks up the parent roles from the role policies in the scope it resolves to.

Requests in a scope that only contains role policies are now validated against the schema of the nearest resource policy, so schema enforcement covers those scopes too.

PlanResources now reports an empty matched scope for each action when no policy matches the requested scope, which gives query plan consumers a consistent value to check.

Path matching and npm exit codes

pathHasPrefix, one of the CEL path functions added in v0.52.0, now treats names that start with a dot as being under the prefix. pathHasPrefix("/secrets/.env", "/secrets") returns true.

Running cerbos or cerbosctl through npm exec, or the equivalent for other package managers, now preserves the binary's original exit status, so CI scripts that branch on specific exit codes get the right value.

Notes on upgrading to Cerbos PDP v0.58.0

Compare results in staging before promoting v0.58.0 to production, and check the cases below.
If any of your requests name a scope that has no policy of its own for the resource kind, check how they resolve on v0.58.0, since they no longer pick up a parent scope's policy. Turn on lenient scope search where that fallback is what you want. If you use lenient scope search with role policies, check requests that resolve to a parent scope, since they now pick up the parent roles from that scope’s role policies.

If you run several versions of a role policy for the same role and scope, recompile your policies after upgrading to clear the migration warning and give each version its own parent roles.

If you have rules that use pathHasPrefix over paths that can contain dot-prefixed names, run your policy tests against v0.58.0, since those names now match.

Upgrade to Cerbos PDP v0.58.0

v0.58.0 is available now. The release notes cover every change, and the v0.57.0 post and v0.56.0 post catch you up if you're coming from an older version.

FAQ

Free policy workshop

Get your first Cerbos policy written by our team.

Book a session to talk through your requirements and walk away with a working policy.

Book a session