Temp
Need to define two things:
- How to access built in records with zanzibar permissions/filtered to allowed.
- How to handle custom records in a generic way.
Maybe if I work out (2) first, then it will answer regarding the best way to handle (1).
Me next steps: think about the generic way I want to be able to define access to resources. There could be a join table, linking resources to roles, but that's too simplistic. There might be some standard patterns:
- Resource is linked to role directly
- Resource is owned by role
-
This strategy moves security from the "leaky" application layer and the "complex" RLS layer into a structured View-Based Security Architecture. It ensures a "Pit of Success" by making the raw data physically inaccessible to the application, forcing all access through permission-aware views.
Subject: Architectural Summary: Zanzibar-style Permissions via Postgres Views
-
The "Dual-Schema" Isolation
-
data Schema (Private): Contains the raw tables (e.g., transactions, accounts). The application’s database user has zero permissions here.
-
api Schema (Public): Contains Security Definer Views. This is the only schema the application can query. Because the views are "Security Definer," they act as a controlled bridge into the private data.
-
The Permission Engine (Zanzibar-in-DB)
-
Instead of scalar boolean functions (which kill performance), create Set-Returning Functions (e.g., get_permitted_ids(resource_type, user_id)).
-
This function performs the Zanzibar relationship lookups and returns a simple list (or table) of UUIDs/IDs the user is allowed to access.
-
The "Pit of Success" View Pattern
Devs define views in the api schema using a standard "Permission Join" template.
-
Filtering: The view joins the raw table against the get_permitted_ids function.
-
Partial Visibility (The Excel Problem): To show an account_name on a transaction without exposing the accounts table, the transactions view performs a join to data.accounts internally. It exports only the name column.
-
Result: The user sees the name in context but still receives an "Access Denied" if they try to query the accounts table directly.
-
Performance Optimization
-
By using Joins or CTEs inside the views (rather than calling a function for every row in a WHERE clause), the Postgres Query Planner can optimize the execution.
-
This avoids the "Black Box" problem where the database performs a full table scan because it can't see inside a security function.
-
Why this wins over RLS/Middleware:
-
No Leakage: Unlike RLS, where a misconfigured policy might grant too much "SELECT" power, a View only exposes the specific columns you explicitly write.
-
Impossible to Forget: If a dev doesn't create a view, the data doesn't exist to the API.
-
Centralized Logic: Permissions are still managed in one place (your Zanzibar logic), but enforced at the lowest possible level (the DB).
Next Steps
This approach is solid for enterprise scale. Would you like me to provide a SQL template for the get_permitted_ids function and a sample view to test the performance with the Query Planner?
Fields:
You are spot on. Trying to jam field-level permissions into a Zanzibar-style system is a recipe for "Relationship Bloat." If a table has 50 columns and 1 million rows, you don't want to store 50 million "user_can_view_column" tuples.
Zanzibar is the "bouncer" at the door (Row Level); field-level security is the "NDA" once you're inside (Column Level).
Here is how you can layer this in later without tearing down your current View-based progress.
- Why Zanzibar struggles with Fields
Zanzibar (ReBAC) excels at hierarchies (e.g., "If you own the Folder, you own the File"). Columns don't usually follow hierarchies; they are properties of the object itself.
-
The Cost: Checking a permission for every column in a result set would turn a simple query into a massive computational overhead for the Auth service.
-
The Solution: Use Attribute-Based Access Control (ABAC) or simple Metadata for fields.
-
The "Application Tailoring" Approach (Cleanest)
Since you are already using a "Public API Schema" with views, your application is the gatekeeper.
-
Metadata Store: Keep a small table/config file that maps Roles to allowed Fields (e.g., Role: Junior_Accountant -> Excludes: [social_security_number, profit_margin]).
-
Dynamic Selection: When the API queries your api.transactions view, the application layer strips the forbidden keys from the response JSON or modifies the SELECT clause before sending it to Postgres.
-
The "Database Masking" Approach (Most Secure)
If you want to keep the "Pit of Success" inside the database, you can use Redaction Logic inside your views.
Instead of just SELECT amount, you use a helper function:
SELECT
CASE
WHEN user_has_field_perm('view_sensitive') THEN sensitive_column
ELSE '[REDACTED]'
END AS sensitive_column
-
Pros: The sensitive data literally never leaves the database if the user isn't authorized.
-
Cons: It makes the views slightly harder to read and can occasionally interfere with certain types of column-based indexing (though less so than row-level filtering).
-
The "Pit of Success" for Field Security
To make it hard for devs to forget field-level security later:
-
The "Deny by Default" DTO: Use Data Transfer Objects (DTOs) in your code. The DTO only has "safe" fields. To add a "sensitive" field, the dev must explicitly call a decorator like @RequiresPermission('view_ssn').
-
GraphQL/Projections: If you use something like GraphQL or a projection-based API, you can bake the permission check into the "resolver" for that specific field.
Summary of the "Add it Later" Plan
-
Now: Focus on the Schema + Zanzibar-Join Views to handle row-level tenancy and object access.
-
Later: Introduce a Metadata Layer in your application that filters the output of those views based on the user's role.
-
Result: You get the performance of the DB for the "heavy lifting" (filtering millions of rows) and the flexibility of the App for the "fine-tuning" (hiding 5 columns).
Would you like me to show a quick example of how an Application Middleware would "scrub" forbidden fields from a Postgres result set before it hits the user?