Lion Cipher Lab

Write and unit-test an authorization policy with OPA and Rego

Why it matters

Almost every breach story has an authorization failure somewhere in it: a user who could reach a record that was not theirs, a role that quietly did more than anyone intended, an endpoint that forgot to check who was calling. In most applications those decisions -- 'can this subject do this action on this resource?' -- are scattered as if/else branches through the codebase. That is a bad place for them. Nobody tests them, the same rule gets copied into three services and then only two get updated, and a single mistyped condition -- an OR where you meant AND, a role check you forgot to add --…

What you'll learn

Steps in this lab

  1. The problem: authorization decisions scattered through code
  2. The words you need: OPA, Rego, PDP vs PEP, RBAC vs ABAC, deny-by-default
  3. Install OPA: one binary, no Docker, no service
  4. The BEFORE: a naive in-app check that over-permits
  5. Write authz.rego: deny by default, then RBAC rules
  6. Add an ABAC rule: the owner of a resource may edit it
  7. Evaluate a decision: opa eval with an input document
  8. The key discipline: unit-test the policy, and test the DENIES
  9. Watch a test catch the privilege bug (red), then fix it (green)
  10. Wire it into an app (PDP/PEP) and log decisions
  11. The honest negatives and the judgment that makes this worth doing

Open the full interactive lab →