How koon-kiat/sitbank works
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: security Security core · deploy Deploy scripts · wsgi.py WSGI app · workflows CI workflows
Components 18
Public portal
Customer-facing Flask surface and its request flow
- WSGI app · Customer portal WSGI entrypoint
wsgi.py - Main pages · General customer pages and dashboard routes
app/main - Web routes · Additional customer-facing web handlers
app/web - Auth flows · Registration, login, MFA, reset, and recovery
app/auth - Banking flows · Payees, transfers, limits, disputes, and statements
app/banking
Admin control plane
Private staff Flask surface with separate access boundaries
- Admin WSGI · Private admin WSGI entrypoint
admin_wsgi.py - Admin app · Staff operations, dashboards, invites, and recovery
app/admin
Security core
Cross-cutting policy, crypto, sessions, and enforcement
- Security core · Shared security services and runtime guards
app/security - App wiring · Shared Flask extensions and application wiring
app/extensions.py
Ops and infra
Deployment, runtime, observability, and edge controls
- Deploy scripts · Container bootstrap, deploy, verification, and cutover scripts
ops/deploy - Container ops · Compose validation, smoke tests, and DAST session helpers
ops/container - Nginx edge · Public and staging proxy boundaries and rate limits
ops/nginx - Tailscale access · Private admin reachability and zero-trust access tooling
ops/tailscale - Observability · Grafana, Loki, Prometheus, and Alloy configuration
ops/observability - CI workflows · GitHub Actions checks, scans, deployment gates, and verification
.github/workflows
State and migrations
Primary database state and schema evolution
- Data models · Central application model definitions
app/models.py - Migrations · Alembic-managed schema and security-state changes
migrations - PostgreSQL · Primary application and security state store
Connections 26
- 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