How ashishmalla115/zerotrustbackend works
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: risk.py Risk Control · auth.py Auth Routes · ws.py WebSocket Routes · main.py Application Startup
Components 18
Container Runtime
- Docker Compose · Provisions the application and relational database environment.
docker-compose.yml - Application Image
Dockerfile - Migration Runner · Runs migrations separately from application startup.
scripts/docker_migrate.sh
API Boundary
- Auth Routes
app/routes/auth.py - Admin Routes
app/routes/admin.py - WebSocket Routes
app/routes/ws.py - API Contracts · Separates transport payloads from database and engine representations.
app/schemas/api_schemas.py
Application Core
- Application Startup · Assembles routes and runtime dependencies.
app/main.py - Risk Control · Validates, constrains, or rejects activity before downstream execution.
app/middleware/risk.py - Security Services
app/core/security.py - WebSocket Lifecycle
app/core/websocket.py
Execution Engine
- FFI Engine · Integrates the external/native model artifact.
app/engine/ffi_engine.py - Stub Engine · Supports non-production execution.
app/engine/stub_engine.py - Native Model
model.isof
Persistence
- Database Access
app/core/database.py - Relational Models
app/models/db_models.py - Schema Migration · Initial versioned relational schema.
alembic/versions/f9c3537655e6_initial_schema_all_tables.py - Database Bootstrap
scripts/init.sql
Connections 22
- 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