Level 10 / Project 04 - Multi Tenant Data Guard¶
Home: README
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| Concept | This project | — | Quiz | Flashcards | — | — |
Focus¶
- Proxy pattern for transparent tenant isolation
- Role-based access control (RBAC) with permission matrix
- Immutable tenant context to prevent mutation during operations
- Audit logging for every data access
Why this project exists¶
In SaaS systems, tenant data leakage is catastrophic. This project makes tenant context mandatory at the data-access layer — not optional middleware — so cross-tenant access is structurally impossible. The Proxy pattern wraps a raw store, injecting filtering on every operation.
Run (copy/paste)¶
Expected terminal output¶
Acme sees 2 records
Super admin sees 3 records
Blocked: Tenant 'acme' cannot access record owned by 'globex'
Audit log (4 entries):
[acme] alice -> INSERT inv-001
...
Expected artifacts¶
- Demo output showing isolation and blocking
- Passing tests (
pytest -vshows ~13 passed)
Alter it (required)¶
- Add a
FIELD_LEVELpermission that lets certain roles see only specific fields (e.g., viewers cannot seeamount). - Add a
bulk_insertmethod that accepts multiple records and applies tenant tagging to each. - Re-run tests to verify isolation holds for bulk operations.
Break it (required)¶
- Try to read a record from another tenant — observe the
TenantViolationError. - Use a
VIEWERrole to attempt an insert — observePermissionDeniedError. - Mutate a
TenantContextafter creation (it should fail because it is frozen).
Fix it (required)¶
- Add rate limiting to the audit log (cap at N entries to prevent memory growth in long-running processes).
- Add a
get_or_nonemethod that returnsNoneinstead of raising when a record belongs to another tenant. - Write tests for both fixes.
Explain it (teach-back)¶
- Why is
TenantContextfrozen (immutable)? What attacks does this prevent? - How does the Proxy pattern differ from middleware-based tenant filtering?
- Why does the system log all access attempts, including denied ones?
- How would you adapt this for a database-backed store using SQL WHERE clauses?
Mastery check¶
You can move on when you can: - explain why tenant isolation must happen at the data layer not the API layer, - trace a cross-tenant access attempt through the code to see where it is blocked, - add a new role with custom permissions and verify access control, - describe the difference between RBAC and ABAC (attribute-based access control).
Related Concepts¶
| ← Prev | Home | Next → |
|---|---|---|