# ashishmalla115/zerotrustbackend architecture

> Architecture diagram of the GitHub repository ashishmalla115/zerotrustbackend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ashishmalla115/zerotrustbackend
- Repository: https://github.com/ashishmalla115/zerotrustbackend

## Overview

#### Purpose and startup
This is a containerized Python service exposing HTTP and WebSocket interfaces around an execution/decision engine, with authentication, risk middleware, and persistent relational state. The application entry point is `app/main.py`; container deployment is defined by `Dockerfile` and `docker-compose.yml`.

#### API and connection boundary
Route modules define the externally reachable service surface:
- `app/routes/auth.py` handles authentication-oriented endpoints.
- `app/routes/admin.py` provides administrative operations.
- `app/routes/ws.py` manages WebSocket-facing interactions.

Request/response contracts are centralized in `app/schemas/api_schemas.py`, separating transport payloads from database representations and engine internals.

#### Application orchestration
`app/main.py` assembles the application, routes, and runtime dependencies. Shared core facilities reside in:
- `app/core/database.py`
- `app/core/security.py`
- `app/core/websocket.py`

These form the main boundary between API handlers and infrastructure: route code should use core services rather than directly owning connection, security, or WebSocket lifecycle logic.

#### Engine abstraction and model execution
The engine layer isolates business/model execution behind interchangeable implementations:
- `app/engine/ffi_engine.py`
- `app/engine/stub_engine.py`
- `model.isof`

`ffi_engine.py` is the production-oriented integration point for the external/native model artifact, while `stub_engine.py` supports non-production or test execution. This abstraction lets routes and middleware depend on a stable engine interface rather than the model’s implementation details.

#### Risk-control stage
`app/middleware/risk.py` is a material control boundary between incoming activity and engine/API execution. It should be treated as the policy-enforcement layer for validating, constraining, or rejecting operations before they reach downstream engine or persistence paths. Its placement makes it central to both HTTP and potentially session-level WebSocket flows.

#### Persistence and schema evolution
Relational persistence is represented by `app/models/db_models.py` and accessed through `app/core/database.py`. Database lifecycle is versioned with Alembic:
- `alembic/env.py`
- `alembic/versions/f9c3537655e6_initial_schema_all_tables.py`
- `scripts/docker_migrate.sh`

`docker-compose.yml` and `scripts/init.sql` indicate the service is intended to run with a provisioned database in containerized environments. Model/schema definitions, migrations, and API schemas are distinct layers and should remain synchronized through explicit mapping rather than shared representations.

#### Runtime dependencies and operational topology
The primary runtime is Python, packaged in `Dockerfile` and dependency-managed through `requirements.txt`. Docker Compose supplies the local/multi-container topology, environment is supplied through `.env.docker`, and migrations are run separately from application startup via `scripts/docker_migrate.sh`. External integration is concentrated in the FFI engine/model boundary and the database; client interaction occurs through HTTP and WebSocket routes.

Main entry points: [`app/middleware/risk.py`](https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/middleware/risk.py) (Risk Control), [`app/routes/auth.py`](https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/routes/auth.py) (Auth Routes), [`app/routes/ws.py`](https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/routes/ws.py) (WebSocket Routes), [`app/main.py`](https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/main.py) (Application Startup)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Container Runtime"]
  node_compose["Docker Compose<br/>container topology<br/>[docker-compose.yml]"]
  node_dockerfile["Application Image<br/>container build"]
  node_migration_runner["Migration Runner<br/>container migration script<br/>[docker_migrate.sh]"]
end

subgraph group_api["API Boundary"]
  node_auth_routes["Auth Routes<br/>HTTP routes<br/>[auth.py]"]
  node_admin_routes["Admin Routes<br/>HTTP routes<br/>[admin.py]"]
  node_ws_routes["WebSocket Routes<br/>[ws.py]"]
  node_api_schemas["API Contracts<br/>transport schemas<br/>[api_schemas.py]"]
end

subgraph group_application["Application Core"]
  node_main["Application Startup<br/>Python entry point<br/>[main.py]"]
  node_risk{{"Risk Control<br/>policy middleware<br/>[risk.py]"}}
  node_security["Security Services<br/>authentication core<br/>[security.py]"]
  node_websocket_core["WebSocket Lifecycle<br/>connection core<br/>[websocket.py]"]
end

subgraph group_engine["Execution Engine"]
  node_ffi_engine["FFI Engine<br/>production engine adapter<br/>[ffi_engine.py]"]
  node_stub_engine["Stub Engine<br/>alternate engine adapter<br/>[stub_engine.py]"]
  node_model["Native Model<br/>model artifact<br/>[model.isof]"]
end

subgraph group_data["Persistence"]
  node_database_core["Database Access<br/>persistence core<br/>[database.py]"]
  node_db_models["Relational Models<br/>database models<br/>[db_models.py]"]
  node_migration["Schema Migration<br/>Alembic revision"]
  node_database_init[("Database Bootstrap<br/>SQL initialization<br/>[init.sql]")]
end

node_compose -->|"builds"| node_dockerfile
node_compose -->|"provisions database"| node_database_init
node_dockerfile -->|"runs"| node_main
node_main -->|"registers"| node_auth_routes
node_main -->|"registers"| node_admin_routes
node_main -->|"registers"| node_ws_routes
node_auth_routes -->|"uses"| node_api_schemas
node_admin_routes -->|"uses"| node_api_schemas
node_ws_routes -->|"uses"| node_api_schemas
node_auth_routes -->|"authenticates through"| node_security
node_ws_routes -->|"manages through"| node_websocket_core
node_risk -->|"permits execution to"| node_ffi_engine
node_risk -->|"permits persistence to"| node_database_core
node_auth_routes -->|"passes activity through"| node_risk
node_admin_routes -->|"passes activity through"| node_risk
node_ws_routes -.->|"passes activity through"| node_risk
node_ffi_engine -->|"invokes"| node_model
node_stub_engine -.->|"alternative to"| node_ffi_engine
node_database_core -->|"persists"| node_db_models
node_migration_runner -->|"applies"| node_migration
node_migration -->|"versions schema for"| node_db_models
node_database_init -->|"initializes"| node_database_core

click node_compose "https://github.com/ashishmalla115/zerotrustbackend/blob/master/docker-compose.yml"
click node_dockerfile "https://github.com/ashishmalla115/zerotrustbackend/blob/master/Dockerfile"
click node_main "https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/main.py"
click node_auth_routes "https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/routes/auth.py"
click node_admin_routes "https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/routes/admin.py"
click node_ws_routes "https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/routes/ws.py"
click node_api_schemas "https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/schemas/api_schemas.py"
click node_risk "https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/middleware/risk.py"
click node_security "https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/core/security.py"
click node_websocket_core "https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/core/websocket.py"
click node_database_core "https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/core/database.py"
click node_db_models "https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/models/db_models.py"
click node_ffi_engine "https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/engine/ffi_engine.py"
click node_stub_engine "https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/engine/stub_engine.py"
click node_model "https://github.com/ashishmalla115/zerotrustbackend/blob/master/model.isof"
click node_migration "https://github.com/ashishmalla115/zerotrustbackend/blob/master/alembic/versions/f9c3537655e6_initial_schema_all_tables.py"
click node_migration_runner "https://github.com/ashishmalla115/zerotrustbackend/blob/master/scripts/docker_migrate.sh"
click node_database_init "https://github.com/ashishmalla115/zerotrustbackend/blob/master/scripts/init.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_compose,node_dockerfile,node_migration_runner toneBlue
class node_auth_routes,node_admin_routes,node_ws_routes,node_api_schemas toneAmber
class node_main,node_risk,node_security,node_websocket_core toneMint
class node_ffi_engine,node_stub_engine,node_model toneRose
class node_database_core,node_db_models,node_migration,node_database_init toneIndigo
```

## Components

### Container Runtime

- [Docker Compose](https://github.com/ashishmalla115/zerotrustbackend/blob/master/docker-compose.yml): container topology · Provisions the application and relational database environment. · `docker-compose.yml`
- [Application Image](https://github.com/ashishmalla115/zerotrustbackend/blob/master/Dockerfile): container build · `Dockerfile`
- [Migration Runner](https://github.com/ashishmalla115/zerotrustbackend/blob/master/scripts/docker_migrate.sh): container migration script · Runs migrations separately from application startup. · `scripts/docker_migrate.sh`

### API Boundary

- [Auth Routes](https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/routes/auth.py): HTTP routes · `app/routes/auth.py`
- [Admin Routes](https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/routes/admin.py): HTTP routes · `app/routes/admin.py`
- [WebSocket Routes](https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/routes/ws.py): WebSocket routes · `app/routes/ws.py`
- [API Contracts](https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/schemas/api_schemas.py): transport schemas · Separates transport payloads from database and engine representations. · `app/schemas/api_schemas.py`

### Application Core

- [Application Startup](https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/main.py): Python entry point · Assembles routes and runtime dependencies. · `app/main.py`
- [Risk Control](https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/middleware/risk.py): policy middleware · Validates, constrains, or rejects activity before downstream execution. · `app/middleware/risk.py`
- [Security Services](https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/core/security.py): authentication core · `app/core/security.py`
- [WebSocket Lifecycle](https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/core/websocket.py): connection core · `app/core/websocket.py`

### Execution Engine

- [FFI Engine](https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/engine/ffi_engine.py): production engine adapter · Integrates the external/native model artifact. · `app/engine/ffi_engine.py`
- [Stub Engine](https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/engine/stub_engine.py): alternate engine adapter · Supports non-production execution. · `app/engine/stub_engine.py`
- [Native Model](https://github.com/ashishmalla115/zerotrustbackend/blob/master/model.isof): model artifact · `model.isof`

### Persistence

- [Database Access](https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/core/database.py): persistence core · `app/core/database.py`
- [Relational Models](https://github.com/ashishmalla115/zerotrustbackend/blob/master/app/models/db_models.py): database models · `app/models/db_models.py`
- [Schema Migration](https://github.com/ashishmalla115/zerotrustbackend/blob/master/alembic/versions/f9c3537655e6_initial_schema_all_tables.py): Alembic revision · Initial versioned relational schema. · `alembic/versions/f9c3537655e6_initial_schema_all_tables.py`
- [Database Bootstrap](https://github.com/ashishmalla115/zerotrustbackend/blob/master/scripts/init.sql): SQL initialization · `scripts/init.sql`

## Connections

- Docker Compose → Application Image: builds
- Docker Compose → Database Bootstrap: provisions database
- Application Image → Application Startup: runs
- Application Startup → Auth Routes: registers
- Application Startup → Admin Routes: registers
- Application Startup → WebSocket Routes: registers
- Auth Routes → API Contracts: uses
- Admin Routes → API Contracts: uses
- WebSocket Routes → API Contracts: uses
- Auth Routes → Security Services: authenticates through
- WebSocket Routes → WebSocket Lifecycle: manages through
- Risk Control → FFI Engine: permits execution to
- Risk Control → Database Access: permits persistence to
- Auth Routes → Risk Control: passes activity through
- Admin Routes → Risk Control: passes activity through
- WebSocket Routes → Risk Control: passes activity through
- FFI Engine → Native Model: invokes
- Stub Engine → FFI Engine: alternative to
- Database Access → Relational Models: persists
- Migration Runner → Schema Migration: applies
- Schema Migration → Relational Models: versions schema for
- Database Bootstrap → Database Access: initializes
