Access Management System
Role-based access control
Design a role-based access control system: users hold roles, roles hold permissions and inherit from other roles, and one check answers whether a user may do an action on a resource.
Overview
An access management system answers one question many times a second: may this user do this action on this resource? Role-based access control (RBAC) answers it in two hops. Permissions are attached to roles, and roles are given to users. Changing what a job title may do means editing one role, not thousands of users.
RBAC was formalized at NIST; the NIST model became the American National Standard INCITS 359 in 2004 (revised in 2012). The standard splits into Core RBAC (users, roles, permissions and the two assignments between them), Hierarchical RBAC (roles that inherit from other roles) and separation-of-duty constraints. The interview problem covers the first two.
Interviewers use this problem to see how you model a small graph (roles inheriting from roles), how you resolve permissions through it without cycles or repeated work, and how you make the check both fast and safe to call from many threads. A strong answer draws the role graph, writes the resolution as a graph walk with a visited set, caches the result, and explains when the cache is thrown away.
Scoping the problem
Ask a few questions before you write code. Each answer shapes the design, and the usual answers are the ones built here.
- Can a user hold several roles? Then the check becomes a union over roles. The usual answer is yes.
- Do roles inherit from other roles? That adds a graph walk and cycle detection. Usually yes, with one or more parents.
- What is a permission: an action, or an action on a named resource? This decides the key, (resource, action). Usually it is an action on a resource, with a wildcard resource.
- Are there deny rules? They change resolution from 'any allow' to 'no deny, then any allow'. Not in the core; they are a follow-up.
- What is the ratio of checks to changes? It justifies caching the resolved set. Checks far outnumber changes.
- Are there many threads? Then role and assignment changes need a lock. Usually yes.
Functional requirements
- Create roles; a role may inherit from one or more parent roles
- Grant and revoke permissions, each an action on a resource, on a role
- Assign and unassign roles to users; a user may hold several roles
- check(user, resource, action) returns whether any of the user's roles, directly or through inheritance, holds a covering permission
- A wildcard resource (*) covers every resource for that action
- Every check is recorded in an audit list with its decision
Non-functional requirements
- A check is a few dict lookups after the first resolution of each role; the graph walk is cached
- Any change to roles or permissions invalidates the cache before the change becomes visible
- Cycles in the role graph are refused when the edge is added
- All changes and checks are safe under concurrent access from many threads
- Unknown users fail closed: the check returns False
Interview questions
- How would you add deny rules?
- Resolve permissions on every check, or precompute them per user?
- How do resource-level permissions work at scale? Listing every document is not possible.
- What happens when a role or a permission is removed while requests are in flight?
- Why is AccessManager not a Singleton, as in many sample solutions?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio