Caretaker Express · PostgreSQL · Cedar

Module 0 · Orientation and environment setup

0.1 What we are building

A tour of the finished Caretaker application, the people in it, and the route we take from an empty folder to Cedar.

≈ 15 min 0 steps 0 lines of code Replay verified

Starting point

You have nothing yet: no folder, no code, no database. That is on purpose. Every file in this course is created by you, in your own editor, in the order the lessons show it.

Keep this website in one window and your editor plus a terminal in another. Each lesson tells you the exact folder, file and command. When a lesson shows terminal output, that output came from a real run of the same step. The course tooling replays every lesson from an empty directory and records what happened, so you can compare your screen with ours line by line.

Outcome

By the end of this lesson you will know:

  • what Caretaker does and who uses it;
  • the shape of the finished application and its main parts;
  • the seeded scenario (two companies, four buildings, six people) that we use in every module;
  • why authorization is the hard part of this application, and where Cedar fits.

The problem

Imagine a company that manages offices and apartment blocks. Each building records maintenance issues (a stuck lift, a leaking pipe, a broken door lock) and runs inspections (a scheduled check that someone must approve). Several companies use the same application. That means one database holds data from many tenants.

The basic features are ordinary: forms, lists, a database. The difficult question is this one:

Who may see or change this record, and how do we prove it?

Alice is a building administrator at Dublin Offices. She can also view issues at Cork Apartments, but she must not change anything there. Bob works for a different company. He must never see Alice's data, even if he types an issue ID into the address bar. Somebody had access last week but it was revoked today. That person must lose access on the very next request.

Code that answers these questions with if statements in every route becomes hard to read and easy to break. In this course we first build it that way on purpose, so you feel the pain. Then we move the rules into Cedar, an open-source policy language and engine, and we keep the same tests passing while we do it.

Build it

There is no code in this lesson. Instead we build the picture that the rest of the course fills in.

The finished application

This is the issues list for Alice, taken from the reference implementation at the end of the course:

localhost:3000
Alice's issues list: issues from building 10 (where she is an admin) and building 20 (where she is a viewer).
Alice's issues list: issues from building 10 (where she is an admin) and building 20 (where she is a viewer).

The same page for Bob shows only his company's building:

localhost:3000
Bob's issues list: only building 30. He cannot see Organisation A at all.
Bob's issues list: only building 30. He cannot see Organisation A at all.

An inspection that Alice created cannot be approved by Alice, even though she is a building admin. A forbid policy enforces separation of duties:

localhost:3000
Inspection 202 for Alice: no Approve button. The page asks Cedar before it shows each action.
Inspection 202 for Alice: no Approve button. The page asks Cedar before it shows each action.

Building admins manage who can do what at their building. Every grant and revoke is itself authorized and written to an audit log:

localhost:3000
The access page for building 10, as Alice sees it.
The access page for building 10, as Alice sees it.

User stories

These are the stories the application satisfies. We come back to each one when we build it.

As a... I want to... So that...
reporter record a maintenance issue at my building the building can act on it
viewer browse, filter and sort issues at buildings I am assigned to I can see what is happening
inspector create an inspection, fill it in, and submit it it can be reviewed
building admin edit any issue at my building and approve submitted inspections the building record is correct
building admin grant and revoke roles at my building the right people have access
organisation admin manage buildings and admins across my organisation I do not need a role at every building
anyone register and log in I have an identity the server can trust
everyone never see another organisation's data tenants stay isolated

The architecture

Caretaker architecture The browser sends requests with a session cookie to Express. Middleware loads the session and user. A route handler reads PostgreSQL through parameterized SQL and asks the authorization module, which builds Cedar entities from database rows and calls the Cedar WebAssembly engine. The route renders an EJS view. Browser HTML forms session cookie untrusted input Node.js process (one server) Express middleware static files body parsers session (cookie → row) loadUser → req.user csrf check rate limit (login) Module 1, 4 Route handler load record authorize act + respond PEP EJS views HTML response Authorization module 1. load assignments (PIP) 2. map rows → Cedar entities 3. isAuthorized(request, policies, entities) policies/*.cedar schema.cedarschema Module 6–9 Cedar engine WebAssembly in-process (PDP) no database access PostgreSQL organisations buildings, issues inspections, users assignments sessions Docker request HTML parameterized SQL can() rows for entities (PIP)

Read the diagram from left to right:

  1. The browser sends HTTP requests. It holds only a session cookie, never roles or permissions.
  2. Express receives each request and runs it through middleware: parsing, sessions, the current user, CSRF checks.
  3. A route handler loads the record it needs from PostgreSQL with plain, parameterized SQL.
  4. Before it acts, the route asks the authorization module. That module loads the user's assignments from the database, maps rows into Cedar entities, and calls the Cedar engine (compiled to WebAssembly and running inside the same Node.js process).
  5. Cedar returns allow or deny plus the IDs of the policies that decided. The route then renders a page, redirects, or returns 403/404.

Cedar never reads the database. It only evaluates the data that our code gives it. A large part of this course is about making sure that data is true.

The data model

Caretaker data model (final) Organisations own buildings. Maintenance issues and inspections belong to a building. Users join organisations through memberships and get roles at buildings through building role assignments. Permission changes and inspection status changes keep history. organisations id integer PKname text NOT NULLcreated_at, updated_at memberships user_id → users PKorganisation_id → orgs PKrole member | org_admin users id integer PKemail text UNIQUEname textpassword_hash textcreated_at buildings id integer PKorganisation_id → orgsname, created_at, updated_at building_role_assignments id integer PKuser_id → usersbuilding_id → buildingsrole viewer | reporter | inspector | building_admingranted_by, granted_atrevoked_by, revoked_at permission_changes id integer PKactor_id, target_user_idbuilding_id, rolechange grant | revokecreated_at issues id integer PKbuilding_id → buildingsreported_by → users (null ok)title, descriptionpriority low…criticalstatus open | investigating | closedoccurred_atcreated_at, updated_at inspections id integer PKbuilding_id → buildingscreated_by → userstitle, findingsstatus draft | submitted | approvedapproved_by, approved_atcreated_at, updated_at inspection_status_changes inspection_id → inspectionsfrom_status, to_statuschanged_by, changed_at owns has roles member of
  • An organisation owns buildings.
  • Maintenance issues and inspections belong to exactly one building.
  • Users join organisations through memberships (role member or org_admin).
  • Building role assignments give a user a role (viewer, reporter, inspector, building_admin) at one building. Revoking sets revoked_at instead of deleting the row, so we keep history.
  • Permission changes record who granted or revoked what, and when.

The scenario we use everywhere

Organisation Buildings
1. Liffey Estates (Org A) 10 Dublin Offices, 20 Cork Apartments
2. Harbour Lets (Org B) 30 Galway Studios, 40 Limerick House
User Membership Building roles
alice Org A member building_admin at 10, viewer at 20
bob Org B member building_admin at 30
carol Org A org_admin none: inherits admin rights at 10 and 20
dave Org A member inspector at 10, reporter at 20
nora none none: registered, no access
ravi Org B member viewer at 30, revoked

Notice what is not inherited. Alice's admin role at building 10 gives her nothing extra at building 20; she needed a separate viewer assignment there. Bob is admin at building 30 but has no access to building 40, even though it belongs to the same company. Carol is different: her role is on the organisation, and in Module 7 we tell Cedar that buildings belong to organisations, so her role reaches every building of Org A.

The route through the course

Module What you add
0 Tools, terminal, an empty project folder
1 Express server, routes, middleware, EJS pages, first tests (data in memory)
2 PostgreSQL in Docker, tables, migrations, seed data, pg and parameterized SQL
3 Full CRUD for organisations, buildings, issues and inspections; pagination, sorting, filters, transactions
4 Users, password hashing, sessions, login/logout, CSRF, rate limits
5 Authorization in plain JavaScript, and why it stops scaling
6 Cedar: the smallest real check, schema, entities, the first protected route
7 Roles per building, attributes, parents, forbid rules, templates, grant/revoke
8 Lists, creates and moves without leaking data across tenants
9 Trust boundaries, PDP/PIP/PEP/PAP, defense in depth
10 Logging, the full test matrix, a clean-checkout run, and a capstone

Technology choices

Layer Choice Why
Runtime Node.js 22 or 24 LTS, ES modules Built-in test runner, --watch and --env-file: fewer dependencies
Web Express 5 Small, explicit, and async errors reach the error handler
Database PostgreSQL 18 in Docker Compose Real constraints and transactions; same setup on every machine
SQL pg with parameterized queries You see every query; no ORM hides what runs
Views EJS, HTML, CSS, a little JavaScript Server-rendered pages keep the trust boundary obvious
Sessions express-session + connect-pg-simple Sessions survive restarts; you can read them in SQL
Passwords argon2 (argon2id) A memory-hard password hash recommended by OWASP
Authorization @cedar-policy/cedar-wasm 4.14.0 The official Cedar engine compiled to WebAssembly; runs locally, no cloud account
Tests node:test + Supertest, separate test database Real HTTP requests against the real app

Run and inspect

Nothing to run yet. Instead, look at the screenshots above and answer two questions for yourself:

  1. Which data does the server need to decide whether Alice may edit issue 101?
  2. Which of that data could a malicious browser try to fake?

Write your answers down. In Module 9 we compare them with what the finished application actually trusts.

Experiment

Use the access table above to predict the answer to each request. Then open the reveal.

Predict Can Bob open issue 106 at building 40? He reported it himself.
Reveal Answer

No. Bob has no role at building 40. Reporting an issue in the past is a fact about the issue, not a permission. In the finished app Bob gets 404 Not Found for issue 106, because the app does not reveal that the issue exists. Module 8 explains the choice between 403 and 404.

Predict Can Carol approve inspection 202, which Alice created?
Reveal Answer

Yes. Carol is an organisation admin of Org A. Her rights reach building 10 through the organisation, and she did not create the inspection, so the separation-of-duties rule does not stop her.

Checkpoint

You are ready for the next lesson if you can explain, in one sentence each:

  • the difference between an organisation membership and a building role assignment;
  • why Alice can view building 20 but cannot administer it;
  • what Cedar receives from our code, and what it does not fetch by itself.
Hint Stuck on the last one?

Cedar receives a request (principal, action, resource, context), a set of policies, and a list of entities with attributes and parents. It does not connect to PostgreSQL. Our code builds the entity list.

Recap and next step

Caretaker is a multi-tenant building-maintenance application: organisations own buildings, buildings own issues and inspections, and people get roles per building. The CRUD is ordinary; the authorization is the real subject. We will build every layer ourselves, and we will meet Cedar only when our own if statements start to hurt.

Next, we install and verify the tools: Node.js, npm, Git, Docker and an editor.