Video in progress
In a multi-service system, where should authorization rules (who can do what) live: in the gateway, in each service, or in a central policy engine?
The gateway does only coarse checks (authentication, a general role). Decisions that depend on the resource are made in the service that owns the data. A central policy engine (OPA, Cedar, OpenFGA) pays off when the rules are complex and repeated across many services.
📖 Answer
There are really two kinds of authorization questions:
- Coarse: "Can this user get into
/admin/at all?" A role or scope in the token is enough. - Fine-grained: "Can this user edit this particular contract?" You need the contract's owner, status, branch and the user's delegations.
For the second, the data exists only in the service itself. So authorization in the gateway alone is a mistake: if a call reaches the service directly from the internal network, there is no protection.
Options:
-
In each service, in code.
@PreAuthorize("@contractAccess.canEdit(#id, authentication)"). Simple, fast, next to the data. The problem: 30 services mean 30 interpretations, and if the "branch manager" rule changes, that's 10 PRs, and it's hard to show an auditor "who is allowed to do what". -
A central policy engine, decision made locally. The rules are in a separate language (Rego, Cedar), in one repo, versioned and tested. It runs next to the service as a sidecar or library, and the service passes it attributes: the user, the action, the resource. Latency is within milliseconds.
-
Relationship-based (ReBAC, Zanzibar style: OpenFGA, SpiceDB). When there is hierarchical sharing like "document → folder → team → user". Powerful, but syncing relationship data into a central system is its own complexity.
How I decide:
- Rules are simple and specific to a service → in code, but with one shared style and library (an
AccessDecisioninterface). - Rules are repeated across many services, change often or auditors need to see them (banking, insurance) → a policy engine.
- Sharing and hierarchy are the core feature → ReBAC.
In every case:
- Don't forget list queries: for "the list of documents I'm allowed to see", checking each record separately doesn't work; the rule has to turn into a SQL filter, or you need the engine's "list" function.
- Every denied decision and every important granted one is written to the audit log.
- Tests for the rules ("this role must not be able to do this action") run in CI.
Sign up to mark studied questions and save your progress.