What are Policies?
Formal’s policy system uses Open Policy Agent (OPA), an open-source policy engine that enables you to express access control rules as code. Policies are evaluated in real-time as users connect to resources and execute queries. Your policies can:- Block connections to resources or specific tables/schemas
- Redact query results automatically
- Encrypt and decrypt data fields
- Rewrite queries before they reach the database
- Filter and limit result sets
- Enforce MFA for sensitive operations
How Policies Work
Policies are written in Rego, OPA’s purpose-built language for access control. They are evaluated at three stages:- Session - When a connection is established
- Pre-request - Before a query reaches the resource
- Post-request - When data returns from the resource
Policy Structure
Every policy consists of these essential components:Name
Name
A unique identifier for the policy. No two policies can share the same
name.
Description
Description
A clear explanation of the policy’s purpose and scope. This helps other
admins understand what the policy does.
Owners
Owners
The administrators responsible for maintaining and enforcing the policy.
Owners receive notifications when policies are triggered (if configured).
Code
Code
The policy logic, written in
Rego, OPA’s
purpose-built language for access control.
Policy Status
Policies can be in one of three states:| Status | Description | Use Case |
|---|---|---|
| Draft | Under development, not enforced | Writing and testing new policies |
| Dry-run | Logs violations without enforcing | Testing policies with real traffic before enforcement |
| Active | Fully enforced and monitoring | Production enforcement |
Notifications
Configure notifications for policy triggers:- None: No notifications sent
- Consumer: Notify the user who triggered the policy
- Owners: Notify policy owners
- All: Notify both user and owners
- Slack (requires integration setup)
End-User Identity Propagation
Formal can extract end-user identity from applications like Looker, Metabase, or Retool, enabling policies that work for both direct connections and application-mediated access:- Direct connection: Alice uses
psqlto query a resource → user and end-user are both Alice - Application connection: Katie uses Metabase → user is the machine user, end-user is Katie
Quick Example
Block all connections by default, but allow admins using a specific machine user:Managing Policies
- Web Console
- Terraform
- Navigate to Policies
- Click New Policy
- Fill in name, description, and owners
- Write Rego code in the editor
- Set status (Draft, Dry-run, or Active)
- Configure notifications
- Save the policy
Best Practices
Start with Dry-Run
Start with Dry-Run
Always test new policies in dry-run mode first. Review logs to ensure they
work as expected before activating.
Use Groups for User Policies
Use Groups for User Policies
Instead of writing policies for individual users, use groups. This makes
policies scalable and easier to maintain.
Document Your Policies
Document Your Policies
Write clear descriptions and use meaningful reason fields. Future you (and
your team) will thank you during audits.
Leverage External Data
Leverage External Data
Use Policy Data Loaders to fetch dynamic approval lists, compliance data, or
business logic from external systems.