# koon-kiat/sitbank architecture

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

- Interactive diagram: https://gitdiagram.com/koon-kiat/sitbank
- Repository: https://github.com/koon-kiat/sitbank

## Overview

1. **Top-level shape**
   SITBank is a Python Flask application split into two runtime surfaces: a **customer-facing portal** and a **private admin portal**. Both are deployed as hardened Docker containers and served through Gunicorn, but they use separate WSGI entrypoints and separate security/configuration boundaries.

2. **Runtime entrypoints and request boundaries**
   The public app is launched from `wsgi.py`; the admin app uses `admin_wsgi.py`. The codebase is organized so the customer portal and admin portal are not just different route groups, but distinct operational boundaries with separate secrets, session keys, database roles, and access controls. This separation is a central architectural feature.

3. **Application modules**
   The main Flask application is divided into domain packages:
   - `app/auth` for registration, login, MFA, password reset, recovery, and auth decorators/schemas/forms
   - `app/banking` for payees, transfers, limits, disputes, and transaction-related flows
   - `app/admin` for staff operations, bootstrap/root controls, and admin dashboards
   - `app/main` and `app/web` for general customer pages and web routes
   - `app/security` for cross-cutting policy, crypto, sessions, rate limits, alerts, audit, and runtime guards
   - `app/ops` for operational commands and database privilege helpers

4. **Data persistence layer**
   PostgreSQL is the primary state store. Beyond normal application tables, the design places security-critical state in dedicated tables: server sessions, auth failure counters, TOTP replay markers, registration OTP challenges, password-reset transactions, alert dedupe windows, and password/breach-related controls. Migrations are managed with Alembic under `migrations/`, and the repository explicitly treats schema evolution as a controlled security-sensitive boundary.

5. **Customer-facing product flows**
   The customer portal covers account lifecycle and banking workflows: registration, login, MFA setup/verification, password change/reset, session management, payee management, transfers, statements, disputes, freezes, and profile/dashboard pages. The templates and JS in `app/templates` and `app/static/js` support these flows with mostly server-rendered UI plus small client-side behaviors.

6. **Authentication and session architecture**
   Authentication is intentionally security-heavy. Passwords use PBKDF2 with a pepper; MFA uses TOTP with encrypted seed storage and recovery codes; sessions are server-backed and signed/HMAC-bound; lookup identifiers are opaque and HMAC-derived. The architecture includes replay protection, session risk binding, absolute lifetime controls, and separate customer/admin session namespaces.

7. **Banking and transaction integrity**
   The banking subsystem is not just CRUD. It enforces transfer limits, payee cooldowns, idempotency for public transaction actions, transaction integrity metadata, and dispute handling. This means the data flow from user action to database write is guarded by domain-specific controls designed to prevent tampering, replay, and IDOR-style access issues.

8. **Administrative control plane**
   The admin portal is isolated from the customer domain and is intended for operator use only. It supports root bootstrap, invite-based staff onboarding, audit review, alerts, disputes, manual recovery, and security-state management. The admin side has its own secrets, CSRF/session config, DB role, and access path, which makes it a separate control plane rather than an internal customer page set.

9. **Security services as a first-class subsystem**
   `app/security` contains the reusable policy layer for the whole repository: audit hashing/anchoring, alerting, email/security notifications, Cloudflare Access validation, Turnstile verification, production runtime guards, retention, sensitive-value handling, session HMAC management, and transaction integrity checks. These modules are the main enforcement boundary between HTTP handlers and the underlying state.

10. **External dependencies and edge controls**
    The deployment architecture relies on several external services and host-managed edges:
    - **Nginx** as the host boundary in front of Gunicorn
    - **PostgreSQL** for application and security state
    - **Cloudflare Turnstile** for anti-abuse checks
    - **SMTP** for password reset and security emails
    - **Tailscale** for the private admin access path
    - **Grafana/Loki/Prometheus/Alloy** for observability
    - **GitHub Actions** for CI, security scans, deployment gates, and verification workflows

11. **Operations and infrastructure code**
    The `ops/` tree is effectively an infrastructure subsystem: deployment scripts, EC2 bootstrap, container validation, runtime contract checks, PostgreSQL role initialization, Nginx configs, observability stacks, Tailscale setup/verification, sudoers/systemd units, and security tooling. This repository is therefore not only application code; it also contains the infrastructure-as-code and operational runbooks needed to deploy and verify the service.

12. **Testing and assurance architecture**
    The test suite is broad and maps closely to the architecture: unit and integration tests for auth, banking, sessions, security, migrations, and admin boundaries; Playwright browser tests for end-to-end customer flows; and specialized tests for deployment, production guards, Cloudflare/Tailscale boundaries, and workflow policy. CI is part of the design, not an afterthought: GitHub Actions enforce linting, secret scanning, SBOM generation, code quality, deployment verification, and private-admin reachability checks.

13. **Deployment topology**
    Production and staging are explicitly separated, with different hostnames, runtime directories, containers, and volumes. Production also has a private admin access path exposed through Tailscale Serve rather than public internet routing. The architecture is therefore a triad of environments/boundaries: public customer app, private admin control plane, and staging/test infrastructure with similar but isolated controls.

14. **Key diagram takeaway**
    If you draw this system, the most important boundaries are:
    - public customer traffic vs private admin access
    - HTTP edge (Nginx/Cloudflare/Tailscale) vs Flask/Gunicorn app logic
    - application logic vs PostgreSQL-backed security state
    - customer domain vs admin/control-plane domain
    - runtime application vs ops/CI/observability/security-verification tooling

Main entry points: [`app/security`](https://github.com/koon-kiat/sitbank/tree/main/app/security) (Security core), [`ops/deploy`](https://github.com/koon-kiat/sitbank/tree/main/ops/deploy) (Deploy scripts), [`wsgi.py`](https://github.com/koon-kiat/sitbank/blob/main/wsgi.py) (WSGI app), [`.github/workflows`](https://github.com/koon-kiat/sitbank/blob/main/.github/workflows) (CI workflows)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime_public["Public portal"]
  node_wsgi_py["WSGI app<br/>entrypoint<br/>[wsgi.py]"]
  node_app_main["Main pages<br/>customer routes"]
  node_app_web["Web routes<br/>customer routes"]
  node_app_auth["Auth flows<br/>identity domain"]
  node_app_banking["Banking flows<br/>transaction domain"]
end

subgraph group_runtime_admin["Admin control plane"]
  node_admin_wsgi_py["Admin WSGI<br/>entrypoint<br/>[admin_wsgi.py]"]
  node_app_admin["Admin app<br/>staff domain"]
end

subgraph group_core_security["Security core"]
  node_app_security["Security core<br/>policy layer"]
  node_app_extensions["App wiring<br/>flask plumbing<br/>[extensions.py]"]
end

subgraph group_ops_infra["Ops and infra"]
  node_ops_deploy["Deploy scripts<br/>infra automation"]
  node_ops_container["Container ops<br/>runtime validation"]
  node_ops_nginx["Nginx edge<br/>edge config"]
  node_ops_tailscale["Tailscale access<br/>private edge"]
  node_ops_observability["Observability<br/>monitoring stack"]
  node_github_actions["CI workflows<br/>automation<br/>[workflows]"]
end

subgraph group_data_state["State and migrations"]
  node_app_models["Data models<br/>domain models<br/>[models.py]"]
  node_migrations["Migrations<br/>schema evolution"]
  node_postgres[("PostgreSQL<br/>state store")]
end

node_wsgi_py -->|"serves"| node_app_main
node_wsgi_py -->|"serves"| node_app_web
node_wsgi_py -->|"uses"| node_app_auth
node_wsgi_py -->|"uses"| node_app_banking
node_admin_wsgi_py -->|"serves"| node_app_admin
node_admin_wsgi_py -->|"enforces"| node_app_security
node_app_main -->|"calls"| node_app_security
node_app_web -->|"calls"| node_app_security
node_app_auth -->|"calls"| node_app_security
node_app_banking -->|"calls"| node_app_security
node_app_admin -->|"calls"| node_app_security
node_app_security -->|"persists"| node_postgres
node_app_auth -->|"persists"| node_postgres
node_app_banking -->|"persists"| node_postgres
node_app_admin -->|"persists"| node_postgres
node_app_extensions -->|"wires"| node_app_models
node_app_models -->|"maps to"| node_postgres
node_migrations -->|"evolves"| node_postgres
node_ops_deploy -->|"configures"| node_ops_nginx
node_ops_deploy -->|"validates"| node_ops_container
node_ops_deploy -->|"prepares"| node_postgres
node_ops_tailscale -->|"exposes"| node_admin_wsgi_py
node_ops_observability -->|"verifies"| node_ops_deploy
node_github_actions -->|"gates"| node_ops_deploy
node_github_actions -->|"checks"| node_ops_tailscale
node_github_actions -->|"checks"| node_ops_observability

click node_wsgi_py "https://github.com/koon-kiat/sitbank/blob/main/wsgi.py"
click node_admin_wsgi_py "https://github.com/koon-kiat/sitbank/blob/main/admin_wsgi.py"
click node_app_main "https://github.com/koon-kiat/sitbank/tree/main/app/main"
click node_app_web "https://github.com/koon-kiat/sitbank/tree/main/app/web"
click node_app_auth "https://github.com/koon-kiat/sitbank/tree/main/app/auth"
click node_app_banking "https://github.com/koon-kiat/sitbank/tree/main/app/banking"
click node_app_admin "https://github.com/koon-kiat/sitbank/tree/main/app/admin"
click node_app_security "https://github.com/koon-kiat/sitbank/tree/main/app/security"
click node_app_extensions "https://github.com/koon-kiat/sitbank/blob/main/app/extensions.py"
click node_app_models "https://github.com/koon-kiat/sitbank/blob/main/app/models.py"
click node_migrations "https://github.com/koon-kiat/sitbank/tree/main/migrations"
click node_ops_deploy "https://github.com/koon-kiat/sitbank/tree/main/ops/deploy"
click node_ops_container "https://github.com/koon-kiat/sitbank/tree/main/ops/container"
click node_ops_nginx "https://github.com/koon-kiat/sitbank/tree/main/ops/nginx"
click node_ops_tailscale "https://github.com/koon-kiat/sitbank/tree/main/ops/tailscale"
click node_ops_observability "https://github.com/koon-kiat/sitbank/tree/main/ops/observability"
click node_github_actions "https://github.com/koon-kiat/sitbank/blob/main/.github/workflows"

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_wsgi_py,node_app_main,node_app_web,node_app_auth,node_app_banking toneBlue
class node_admin_wsgi_py,node_app_admin toneAmber
class node_app_security,node_app_extensions toneMint
class node_ops_deploy,node_ops_container,node_ops_nginx,node_ops_tailscale,node_ops_observability,node_github_actions toneRose
class node_app_models,node_migrations,node_postgres toneIndigo
```

## Components

### Public portal

Customer-facing Flask surface and its request flow

- [WSGI app](https://github.com/koon-kiat/sitbank/blob/main/wsgi.py): entrypoint · Customer portal WSGI entrypoint · `wsgi.py`
- [Main pages](https://github.com/koon-kiat/sitbank/tree/main/app/main): customer routes · General customer pages and dashboard routes · `app/main`
- [Web routes](https://github.com/koon-kiat/sitbank/tree/main/app/web): customer routes · Additional customer-facing web handlers · `app/web`
- [Auth flows](https://github.com/koon-kiat/sitbank/tree/main/app/auth): identity domain · Registration, login, MFA, reset, and recovery · `app/auth`
- [Banking flows](https://github.com/koon-kiat/sitbank/tree/main/app/banking): transaction domain · Payees, transfers, limits, disputes, and statements · `app/banking`

### Admin control plane

Private staff Flask surface with separate access boundaries

- [Admin WSGI](https://github.com/koon-kiat/sitbank/blob/main/admin_wsgi.py): entrypoint · Private admin WSGI entrypoint · `admin_wsgi.py`
- [Admin app](https://github.com/koon-kiat/sitbank/tree/main/app/admin): staff domain · Staff operations, dashboards, invites, and recovery · `app/admin`

### Security core

Cross-cutting policy, crypto, sessions, and enforcement

- [Security core](https://github.com/koon-kiat/sitbank/tree/main/app/security): policy layer · Shared security services and runtime guards · `app/security`
- [App wiring](https://github.com/koon-kiat/sitbank/blob/main/app/extensions.py): flask plumbing · Shared Flask extensions and application wiring · `app/extensions.py`

### Ops and infra

Deployment, runtime, observability, and edge controls

- [Deploy scripts](https://github.com/koon-kiat/sitbank/tree/main/ops/deploy): infra automation · Container bootstrap, deploy, verification, and cutover scripts · `ops/deploy`
- [Container ops](https://github.com/koon-kiat/sitbank/tree/main/ops/container): runtime validation · Compose validation, smoke tests, and DAST session helpers · `ops/container`
- [Nginx edge](https://github.com/koon-kiat/sitbank/tree/main/ops/nginx): edge config · Public and staging proxy boundaries and rate limits · `ops/nginx`
- [Tailscale access](https://github.com/koon-kiat/sitbank/tree/main/ops/tailscale): private edge · Private admin reachability and zero-trust access tooling · `ops/tailscale`
- [Observability](https://github.com/koon-kiat/sitbank/tree/main/ops/observability): monitoring stack · Grafana, Loki, Prometheus, and Alloy configuration · `ops/observability`
- [CI workflows](https://github.com/koon-kiat/sitbank/blob/main/.github/workflows): automation · GitHub Actions checks, scans, deployment gates, and verification · `.github/workflows`

### State and migrations

Primary database state and schema evolution

- [Data models](https://github.com/koon-kiat/sitbank/blob/main/app/models.py): domain models · Central application model definitions · `app/models.py`
- [Migrations](https://github.com/koon-kiat/sitbank/tree/main/migrations): schema evolution · Alembic-managed schema and security-state changes · `migrations`
- **PostgreSQL**: state store · Primary application and security state store

## Connections

- WSGI app → Main pages: serves · Customer entrypoint dispatches into portal routes
- WSGI app → Web routes: serves · Customer entrypoint also exposes web handlers
- WSGI app → Auth flows: uses · Public portal depends on authentication flows
- WSGI app → Banking flows: uses · Public portal depends on banking flows
- Admin WSGI → Admin app: serves · Admin entrypoint dispatches into staff routes
- Admin WSGI → Security core: enforces · Admin surface uses hardened security services
- Main pages → Security core: calls · Customer pages rely on shared security policies
- Web routes → Security core: calls · Web handlers rely on shared security policies
- Auth flows → Security core: calls · Authentication uses crypto, sessions, rate limits, and alerts
- Banking flows → Security core: calls · Banking uses transaction integrity and identity controls
- Admin app → Security core: calls · Admin control plane uses shared enforcement and audit
- Security core → PostgreSQL: persists · Security state is stored in PostgreSQL tables
- Auth flows → PostgreSQL: persists · Auth and recovery flows read/write application state
- Banking flows → PostgreSQL: persists · Banking transactions and disputes persist to PostgreSQL
- Admin app → PostgreSQL: persists · Admin actions and review workflows touch operational state
- App wiring → Data models: wires · Shared Flask plumbing binds the model layer into the app
- Data models → PostgreSQL: maps to · Central models represent persisted application state
- Migrations → PostgreSQL: evolves · Alembic manages schema and security-state changes
- Deploy scripts → Nginx edge: configures · Deploy tooling renders and verifies edge configuration
- Deploy scripts → Container ops: validates · Deployment scripts rely on container validation and smoke tests
- Deploy scripts → PostgreSQL: prepares · Deployment includes database cutover and role setup
- Tailscale access → Admin WSGI: exposes · Private access path reaches the admin surface
- Observability → Deploy scripts: verifies · Monitoring stack supports deployment and runtime checks
- CI workflows → Deploy scripts: gates · CI workflows enforce deployment, security, and verification steps
- CI workflows → Tailscale access: checks · Workflows verify private admin reachability and policy
- CI workflows → Observability: checks · Workflows validate observability and operational readiness
