Video in progress

In a system with hundreds of services, should each service make the "can this user do this action" authorization decision itself, or do you hand it to a central policy service?

Authentication is centralized (one IdP, a token), but authorization is split: the coarse scope/role check lives in the gateway or a shared library, and the fine-grained decision that depends on the resource lives in the service that owns the data. A central policy engine helps you write policy in one place, but it must not turn into a network dependency on every request.

📖 Answer

The usual starting point: checks like if (user.hasRole("ADMIN")) in every service, each a little different. When an auditor asks "who can refund a payment?", the answer is scattered across 15 repositories.

I split the decision into three layers:

  1. Identity. One IdP (Keycloak, Okta, etc.), OAuth2/OIDC, short-lived JWTs. Services verify the token themselves and don't go to the center. For service-to-service calls, the service's own identity (mTLS or client credentials), rather than blindly passing the user's token down the chain.

  2. Coarse permission. "Does the token have the orders:write scope?": the gateway or a shared Spring Security configuration: @PreAuthorize("hasAuthority('SCOPE_orders:write')"). Cheap, and needs no context.

  3. Fine-grained permission. "Can this manager refund an order from exactly this store, if the amount is under 5 million?" This depends on resource data, and the owning service knows it, so the decision is made there.

Where a central policy engine (like OPA, Cedar, OpenFGA) helps:

  • policy is separate from code, versioned and tested, and the security team can review it;
  • one place to answer the audit question "who is allowed what";
  • relationship-based permissions ("the folder owner sees every document inside"), which is painful to write in every service.

Risks:

  • going over the network to a central service on every request means latency and a single point of failure. So the engine runs as a sidecar or a library, with policy distributed from the center and evaluated locally;
  • getting the data the engine needs (resource owner, amount) to it is often the hardest part;
  • if the engine doesn't respond, fail-closed is safe, but everything stops. You have to choose this deliberately for each action.

My usual recommendation: authn central; coarse authz in a shared library; fine-grained authz in the service. And when complex relationship models needed by many services appear, add a Zanzibar-style permission service as a platform.