# anish-anish/opa-sql-governance architecture

> Architecture diagram of the GitHub repository anish-anish/opa-sql-governance, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/anish-anish/opa-sql-governance
- Repository: https://github.com/anish-anish/opa-sql-governance

## Overview

This repository is a SQL governance PEP/PDP prototype. Its intended workflow inspects client SQL, requests an OPA decision using user and query attributes, rejects prohibited columns, and scopes authorized execution through PostgreSQL RLS. The graph also preserves the mock authorization service and the documented governance-contract and self-access capabilities. Source excerpts show SQL extraction, decision requests, SQL rewriting, database execution, and mock endpoints; the README documents native RLS and the real OPA deployment. The README describes an end-to-end path, but the shown sql_auth.py main exits before executing its rewritten query, so that connection is marked unverified.

Main entry points: [`sql_auth.py`](https://github.com/anish-anish/opa-sql-governance/blob/main/sql_auth.py) (PEP workflow), [`column_extract.py`](https://github.com/anish-anish/opa-sql-governance/blob/main/column_extract.py) (SQL AST extractor), [`mock_opa_server.py`](https://github.com/anish-anish/opa-sql-governance/blob/main/mock_opa_server.py) (Mock OPA API), [`query_executor.py`](https://github.com/anish-anish/opa-sql-governance/blob/main/query_executor.py) (Postgres executor)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_inspection["SQL inspection"]
  node_pep["PEP workflow<br/>[sql_auth.py]"]
  node_extractor["SQL AST extractor<br/>[column_extract.py]"]
  node_rewriter["SQL rewriter<br/>[sql_auth.py]"]
end

subgraph group_authorization["Policy authorization"]
  node_opa{{"OPA PDP"}}
  node_rego["Rego policies<br/>[policy.rego]"]
  node_policydata["Policy data<br/>[policy_data.json]"]
  node_mockopa["Mock OPA API<br/>[mock_opa_server.py]"]
end

subgraph group_execution["SQL execution"]
  node_executor["Postgres executor<br/>[query_executor.py]"]
  node_postgres[("PostgreSQL")]
  node_rls["Native row security<br/>[demp.sql]"]
  node_results["Filtered results"]
end

subgraph group_governance["Governance controls"]
  node_contracts["Governance contracts<br/>[mock_opa_server.py]"]
  node_selfaccess["Self-access rules<br/>[policy_data.json]"]
end

node_client(("SQL client"))
node_profile["User profile"]

node_client -->|"submits SQL"| node_pep
node_pep -->|"extracts columns"| node_extractor
node_extractor -->|"returns references"| node_pep
node_profile -->|"provides attributes"| node_pep
node_pep -->|"requests decision"| node_opa
node_opa -->|"evaluates policies"| node_rego
node_opa -->|"uses policy data"| node_policydata
node_pep -.->|"posts decision request"| node_mockopa
node_mockopa -->|"returns decision"| node_contracts
node_selfaccess -->|"defines exceptions"| node_opa
node_opa -->|"returns decision"| node_pep
node_pep -->|"builds scoped SQL"| node_rewriter
node_rewriter -.->|"passes authorized SQL"| node_executor
node_executor -->|"executes SQL"| node_postgres
node_postgres -->|"enforces row policies"| node_rls
node_rls -->|"filters rows"| node_results

click node_pep "https://github.com/anish-anish/opa-sql-governance/blob/main/sql_auth.py"
click node_extractor "https://github.com/anish-anish/opa-sql-governance/blob/main/column_extract.py"
click node_rewriter "https://github.com/anish-anish/opa-sql-governance/blob/main/sql_auth.py"
click node_rego "https://github.com/anish-anish/opa-sql-governance/blob/main/policy.rego"
click node_policydata "https://github.com/anish-anish/opa-sql-governance/blob/main/policy_data.json"
click node_mockopa "https://github.com/anish-anish/opa-sql-governance/blob/main/mock_opa_server.py"
click node_contracts "https://github.com/anish-anish/opa-sql-governance/blob/main/mock_opa_server.py"
click node_selfaccess "https://github.com/anish-anish/opa-sql-governance/blob/main/policy_data.json"
click node_executor "https://github.com/anish-anish/opa-sql-governance/blob/main/query_executor.py"
click node_rls "https://github.com/anish-anish/opa-sql-governance/blob/main/demp.sql"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_pep,node_extractor,node_rewriter,node_client,node_profile toneBlue
class node_opa,node_rego,node_policydata,node_mockopa toneAmber
class node_executor,node_postgres,node_rls,node_results toneMint
class node_contracts,node_selfaccess toneRose
```

## Components

### SQL inspection

- [PEP workflow](https://github.com/anish-anish/opa-sql-governance/blob/main/sql_auth.py): `sql_auth.py`
- [SQL AST extractor](https://github.com/anish-anish/opa-sql-governance/blob/main/column_extract.py): `column_extract.py`
- [SQL rewriter](https://github.com/anish-anish/opa-sql-governance/blob/main/sql_auth.py): `sql_auth.py`

### Policy authorization

- **OPA PDP**
- [Rego policies](https://github.com/anish-anish/opa-sql-governance/blob/main/policy.rego): `policy.rego`
- [Policy data](https://github.com/anish-anish/opa-sql-governance/blob/main/policy_data.json): `policy_data.json`
- [Mock OPA API](https://github.com/anish-anish/opa-sql-governance/blob/main/mock_opa_server.py): `mock_opa_server.py`

### SQL execution

- [Postgres executor](https://github.com/anish-anish/opa-sql-governance/blob/main/query_executor.py): `query_executor.py`
- **PostgreSQL**
- [Native row security](https://github.com/anish-anish/opa-sql-governance/blob/main/demp.sql): `demp.sql`
- **Filtered results**

### Governance controls

- [Governance contracts](https://github.com/anish-anish/opa-sql-governance/blob/main/mock_opa_server.py): `mock_opa_server.py`
- [Self-access rules](https://github.com/anish-anish/opa-sql-governance/blob/main/policy_data.json): `policy_data.json`

### Other components

- **SQL client**
- **User profile**

## Connections

- SQL client → PEP workflow: submits SQL (evidence: `README.md`)
- PEP workflow → SQL AST extractor: extracts columns (evidence: `sql_auth.py`)
- SQL AST extractor → PEP workflow: returns references (evidence: `sql_auth.py`)
- User profile → PEP workflow: provides attributes (evidence: `README.md`)
- PEP workflow → OPA PDP: requests decision (evidence: `sql_auth.py`)
- OPA PDP → Rego policies: evaluates policies (evidence: `README.md`)
- OPA PDP → Policy data: uses policy data (evidence: `README.md`)
- PEP workflow → Mock OPA API: posts decision request (evidence: `README.md`)
- Mock OPA API → Governance contracts: returns decision (evidence: `mock_opa_server.py`)
- Self-access rules → OPA PDP: defines exceptions (evidence: `README.md`)
- OPA PDP → PEP workflow: returns decision (evidence: `README.md`)
- PEP workflow → SQL rewriter: builds scoped SQL (evidence: `sql_auth.py`)
- SQL rewriter → Postgres executor: passes authorized SQL
- Postgres executor → PostgreSQL: executes SQL (evidence: `query_executor.py`)
- PostgreSQL → Native row security: enforces row policies (evidence: `README.md`)
- Native row security → Filtered results: filters rows (evidence: `README.md`)
