diff --git a/AGENTS.md b/AGENTS.md index a4fdd0c..808785c 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -31,7 +31,7 @@ make test # whole repo make -f example/example.mk example-test # this part only, standalone ``` -Expected: `example suite: 37 passed, 0 failed`. Failure action: reproduce +Expected: `example suite: 105 passed, 0 failed`. Failure action: reproduce through the real stack first (failing route-level test), then fix; the reproduction stays in the suite. Conventions: `tests/README.md`. diff --git a/README.md b/README.md new file mode 100644 index 0000000..25fad8f --- /dev/null +++ b/README.md @@ -0,0 +1,225 @@ +# MoloApp + +Native C++23 web framework: HTTP/1.1 + HTTP/2 runtime, routing, views, +validation, templates, ORM (SQLite + Postgres), sessions, realtime, +supervisor, clients, observability, multi-tenancy, audit, governance. One +third-party dependency planned for TLS (wolfSSL). One CLI binary: `molo`. + +This README states what works today. Gaps are listed at the bottom and in +the spec next to the code that will close them. Where README and a spec +disagree, the spec wins: `plan.md`, `plans/`, `src/*/SPEC.md`, `reverse.md`, +`example/SPEC.md`. + +## Requirements + +- Linux, `g++` 15+ with `-std=c++23`, GNU or BSD `make` +- `libsqlite3` and `libpq` (the CLI links `-lsqlite3 -l:libpq.so.5`) +- Optional: Postgres 18 for the postgres backend (`docker compose up -d postgres`) + +```sh +g++ --version && make --version +``` + +## Build and test + +```sh +make # everything: libs, tests, build/molo +make test # whole suite, every part +make lint # warning-free build plus em dash ban +make clean # rm -rf build +``` + +Everything lands under `build/`. Warnings are errors in spirit: fix the +cause, never silence them. `make deploy` is referenced in `AGENTS.md` but +is not defined in the Makefile yet. + +## The living example + +A route-level demo of every feature, with 105 integration tests on the real +stack (no mocks). Standalone, no other part needed. + +```sh +make -f example/example.mk example # build +./build/example/living-server 8080 # run +make -f example/example.mk example-test # expect: 105 passed, 0 failed +``` + +Open `http://127.0.0.1:8080/` and click through the matrix. Narrow the +window to 360px: the nav collapses into a dropdown. Quick probe: + +```sh +curl localhost:8080/health # {"status":"ok","tls":"stub-no-wolfssl"} +``` + +Open `/stats` for live Chart.js charts of requests, latency, bytes and +connections (Chart.js is embedded into the binary at build time). The +feature -> route -> test -> plan matrix lives in `example/README.md`. +Test conventions (one test file per URL, Arrange/Act/Assert, integration +only) are in `tests/README.md`. + +### How an app is written + +`example/app/App.cpp` is the reference: an `Application`, middleware slots, +and `Router`s mounted with a prefix. Apps chain, so mounting three apps +under each other serves `/app1/app1/app1/...`. + +```cpp +auto app = std::make_shared(); +app->use(fw::MiddlewareSlot::Errors, fw::errors_middleware()); + +fw::Router routing; +register_routing_demo(routing, services); +app->include_router("/routing", routing, "routing."); +``` + +Note: `example/fw/` is a skeleton of the public API that the real parts +under `src/` are being re-bound to, route by route. URLs and tests stay +stable while that happens. + +## The CLI: `molo` + +Built by `make` to `build/molo`. + +```sh +./build/molo --help +./build/molo help # full help for one command +``` + +Global flags: `--format json|text`, `-v` (repeat up to 3), `-q`, `-h`. +Exit codes: `0` ok, `1` runtime, `2` usage, `69` unavailable. Stdout is the +primary output only; logs go to stderr. `--format json` is always parseable. + +| Command | What it does | +|---|---| +| `new [--backend postgres\|sqlite] [--force]` | Scaffold a project (Makefile, AGENTS.md, `.env.json`, assets, templates, locales, `git init`). Refuses a non-empty dir without `--force`. | +| `serve [--port N] [--reload] [--config F]` | Dev server. `--reload` watches and rebuilds; refused on prod config without `--allow-prod`. | +| `build [--embed dir] [--out dir]` | Build, optionally embedding assets first. | +| `embed --out f.cpp [--hpp f.hpp] [--namespace ns] [--gzip]` | Turn a static dir into constexpr C++ arrays. Output is byte-identical across runs. | +| `migrate [--check \| --apply]` | Preview or apply database migrations. | +| `health` | Local liveness and readiness summary. | +| `supervisor start\|stop\|restart\|status\|logs` | Process supervisor. | +| `task enqueue\|retry\|cancel\|inspect` | Background task queue (`--id` for inspect). | +| `ai new\|continue` | The AI agent, see below. | +| `expose serve\|agent\|list\|stats\|expose-key` | Reverse tunnels, see below. | +| `verify`, `supply-check`, `release` | Offline artifact verification, pin and advisory gate, ordered release gate. | +| `rollout`, `rollback`, `experiment`, `debug` | Operability: deploys, chaos, debug helpers. | + +Fixed health paths on a running server, no auth: `/healthz`, `/readyz`, +`/startupz`, `/metrics` (Prometheus text 0.0.4). + +Start a new app: + +```sh +./build/molo new shop --backend sqlite +cd shop && ls -A # .env.json AGENTS.md Makefile assets locales src templates +``` + +## The agent: `molo ai` + +OpenAI-compatible vibe-coding harness that knows the framework's features +and conventions. Streams by default. + +```sh +export DEVPLACE_API_KEY=... # env only, never a flag or a file +./build/molo ai new --prompt "a todo app with sqlite and a json api" +./build/molo ai continue --prompt "add login" +``` + +- Defaults: base `https://devplace.net/openai/v1`, model `molodetz`; + override with `--base-url` and `--model`. +- Secrets are redacted before the prompt is built. If redaction fails, the + run aborts. +- Plan then apply: file writes are confirmed by a human. +- Caps: 30 s connect, 300 s session, 8 MB transcript, 50 actions. + +## Reverse tunnels: `molo expose` + +Publish a local endpoint through a MoloApp server over one outbound +connection. The spec is `reverse.md` (claims, keys, upstream tables, +routing precedence, caps). + +```sh +molo expose serve --slug /hook1 --upstream http://127.0.0.1:3000 # server side +molo expose agent --slug /hook1 --key # exposer side +molo expose list +molo expose stats # or --format json +molo expose expose-key rotate +``` + +Stats are zero-cost on the hot path (atomics), flushed in one transaction to +SQLite by default (`system.db`) or Postgres. Per method: count, kB up/down, +windowed kB/s, average latency. + +## Configuration + +`.env.json` in the workdir is loaded automatically; environment variables +override it. + +```json +{ + "DATABASE_URL": "sqlite://system.db", + "STATS_FLUSH_SECS": "5" +} +``` + +| Key | Default | Meaning | +|---|---|---| +| `DATABASE_URL` | `sqlite://system.db` | `sqlite://...` or a postgres URL | +| `STATS_FLUSH_SECS` | `5` | Stats flush interval | +| `DEVPLACE_API_KEY` | none | Env only. Required by `molo ai` | +| `MOLO_EXPOSE_KEY` | none | Claim key for the expose agent | + +Postgres for local tests: `docker compose up -d postgres` (user/password +`molo`, db `molo_test`, bound to 127.0.0.1:5432). + +## Repo map + +```text +src/runtime event loop, HTTP/1.1 + HTTP/2, compression, static +src/observability logging, events, metrics, tracing (zero-cost when off) +src/security auth, hashing, crypto, sessions, guards, uploads +src/data ORM, drivers, KV, cache, pubsub +src/app application, routers, views, forms, validation, templates +src/realtime WebSocket, SSE, Socket.IO, streaming, WebDAV, expose tunnels +src/supervisor process supervisor, tasks, replication +src/clients HTTP/REST client, OpenAPI, test double +src/cli molo binary: CLI, dev server, embedder, AI, ops, i18n, CSP +src/tenancy multi-tenancy +src/audit hash-chained audit log, GDPR export/erasure, retention +src/governance API keys, versioning, OpenAPI diff gate, surface linter +src/operability deploys, drains, locks, breakers, SLO, chaos +src/supplychain SBOM, provenance, pin policy, release attestation +example/ living example (skeleton fw + demo routes) +tests/ integration tests, mirrors URLs +plans/ 15 detailed plans, index in plans/README.md +``` + +Each part ships a `.mk` fragment that the root Makefile includes, and +most ship a `SPEC.md`. Every knob has a hard static cap, declared in that +part's `caps` header (CLI: `src/cli/caps.hpp`). + +## Working with agents on this repo + +`AGENTS.md` is the entry point for autonomous work. Pipeline order: +lint, suite, bump, changelog, commit, tag, deploy, verify. Commits are +`[(scope)][!]: ` (subject max 100 chars). Versions are +derived from commits, never hand-chosen. Never cross alone: `specs/`, +`CHANGELOG.md`, `VERSION`, tags, history rewrite, force push. + +## Known gaps + +Measured on this checkout, not aspirational: + +- `molo serve` parks the loop; full HTTP serving waits on runtime lifecycle + wiring (`src/cli/dev/dev_server.cpp`). +- `molo ai continue` resumes within one process only; no durable session + store (`src/cli/cli/cli_app.cpp`). HTTPS transport for the agent is a stub + until wolfSSL lands. +- TLS is a stub everywhere (`/health` reports `stub-no-wolfssl`). +- `molo expose key@server /claim` shorthand from `reverse.md` is + not wired; use the `serve` and `agent` subcommands with flags. +- `example/fw/` is a skeleton, not the real framework; the swap to `src/` + namespaces is incremental. +- No root `deploy` target, no `CHANGELOG.md` or `VERSION` yet. +- No benchmark numbers are published. Any performance claim without a + measurement behind it is not a claim. diff --git a/example/README.md b/example/README.md index 181db8a..a23dd41 100644 --- a/example/README.md +++ b/example/README.md @@ -10,10 +10,11 @@ make -f example/example.mk example ./build/example/living-server 8080 ``` -Open `http://127.0.0.1:8080/` and click through the matrix. Narrow the +Open `http://127.0.0.1:8080/` and click through the matrix; `/stats` has the +charts (Chart.js is embedded in the binary, no CDN). Narrow the window to 360px: the top nav collapses into a dropdown menu. -Run the suite (37 route-level integration tests, real stack, no mocks): +Run the suite (105 route-level integration tests, real stack, no mocks): ```sh make -f example/example.mk example-test @@ -78,6 +79,86 @@ on a missing row. | workflow | `/workflow/release` | `tests/example/workflow/release_test.cpp` | `plans/10-workflow-example.md` 6.2 | | redirects | `/redirect/here` | `tests/example/redirect/here_test.cpp` | `plans/05-request-routing.md` | +| statistics | `/stats` | `tests/example/stats/page_test.cpp` | `plans/02-observability.md` | +| statistics data | `/stats/data` | `tests/example/stats/data_test.cpp` | `plans/02-observability.md` | + +### Extended features + +Added in the second pass: security, platform, enterprise, resilience and +data-depth demos. Each row is a plain GET that answers 2xx; actions are +buttons or forms on the page. Per-module gaps versus `src/` are in the +header of `example/fw/{SecurityExtra,Platform,Enterprise,Resilience}.hpp`. +`tests/example/matrix_test.cpp` crawls every row of the index. + +| Feature | Route | Test file | +|---|---|---| +| CSRF token form | `/security/csrf` | `tests/example/security/csrf_test.cpp` | +| CORS preflight allowlist | `/security/cors` | `tests/example/security/cors_test.cpp` | +| Rate limiter (429 + Retry-After) | `/security/rate` | `tests/example/security/rate_test.cpp` | +| Security headers | `/security/headers` | `tests/example/security/headers_test.cpp` | +| CSP nonce page | `/csp/page` | `tests/example/csp/page_test.cpp` | +| CSP report endpoint | `/csp/report` | `tests/example/csp/report_test.cpp` | +| SHA-256 and HMAC | `/crypto/hash` | `tests/example/crypto/hash_test.cpp` | +| Secure random token | `/crypto/random` | `tests/example/crypto/random_test.cpp` | +| Password hashing (demo) | `/crypto/password` | `tests/example/crypto/password_test.cpp` | +| Roles and permissions | `/security/roles` | `tests/example/security/` | +| Secrets provider (masked) | `/security/secrets` | `tests/example/security/secrets_test.cpp` | +| Upload validation | `/security/upload` | `tests/example/security/upload_test.cpp` | +| SQL and path guard | `/security/guard` | `tests/example/security/guard_test.cpp` | +| Denial sink | `/security/denials` | `tests/example/security/denials_test.cpp` | +| Login form with session cookie | `/security/login` | `tests/example/security/login_test.cpp` | +| i18n | `/i18n` | `tests/example/i18n/` | +| gzip compression | `/compress/demo` | `tests/example/compress/demo_test.cpp` | +| conditional GET and ranges | `/conditional/doc` | `tests/example/conditional/doc_test.cpp` | +| liveness probe | `/healthz` | `tests/example/healthz/` | +| readiness probe | `/readyz` | `tests/example/readyz/` | +| startup gate | `/startupz` | `tests/example/startupz/` | +| prometheus metrics | `/metrics` | `tests/example/metrics/` | +| structured logging | `/logging/records` | `tests/example/logging/records_test.cpp` | +| trace context | `/context/trace` | `tests/example/context/trace_test.cpp` | +| DI container | `/di/services` | `tests/example/di/services_test.cpp` | +| flash messages | `/flash` | `tests/example/flash/` | +| middleware builtins | `/middleware/demo` | `tests/example/middleware/demo_test.cpp` | +| expose stats | `/expose/stats` | `tests/example/expose/stats_test.cpp` | +| CLI commands | `/cli/commands` | `tests/example/cli/commands_test.cpp` | +| Tenant resolution (header, subdomain, path) | `/tenancy/resolve` | `tests/example/tenancy/resolve_test.cpp` | +| Tenant path resolution | `/tenancy/t/demo/whoami` | `tests/example/tenancy/` | +| Tenant data isolation | `/tenancy/data` | `tests/example/tenancy/data_test.cpp` | +| Tenant quotas | `/tenancy/quota` | `tests/example/tenancy/quota_test.cpp` | +| Tenant feature flags | `/tenancy/flags` | `tests/example/tenancy/flags_test.cpp` | +| Tenant config overlay | `/tenancy/config` | `tests/example/tenancy/config_test.cpp` | +| Per-tenant metrics | `/tenancy/metrics` | `tests/example/tenancy/metrics_test.cpp` | +| Tenant provisioning form | `/tenancy/provision` | `tests/example/tenancy/provision_test.cpp` | +| Hash-chained audit log | `/audit/log` | `tests/example/audit/log_test.cpp` | +| Audit tamper detection | `/audit/verify` | `tests/example/audit/verify_test.cpp` | +| PII masking and redaction | `/audit/mask` | `tests/example/audit/mask_test.cpp` | +| Retention and legal hold | `/audit/retention` | `tests/example/audit/retention_test.cpp` | +| Data subject export and erasure | `/audit/subject` | `tests/example/audit/subject_test.cpp` | +| API keys with scopes | `/governance/keys` | `tests/example/governance/keys_test.cpp` | +| Tiers and rate limit headers | `/governance/tiers` | `tests/example/governance/tiers_test.cpp` | +| API versions (Deprecation, Sunset) | `/v1/items` | `tests/example/v1/items_test.cpp` | +| API version list | `/governance/versions` | `tests/example/governance/versions_test.cpp` | +| Route linter | `/governance/lint` | `tests/example/governance/lint_test.cpp` | +| OpenAPI spec differ | `/governance/diff` | `tests/example/governance/diff_test.cpp` | +| RFC 7807 problem+json | `/governance/problem` | `tests/example/governance/problem_test.cpp` | +| Circuit breaker | `/operability/breaker` | `tests/example/operability/breaker_test.cpp` | +| Bulkhead | `/operability/bulkhead` | `tests/example/operability/bulkhead_test.cpp` | +| Load shedder | `/operability/shedder` | `tests/example/operability/shedder_test.cpp` | +| Deadline propagation | `/operability/deadline` | `tests/example/operability/deadline_test.cpp` | +| SLO error budget | `/operability/slo` | `tests/example/operability/slo_test.cpp` | +| Chaos experiment | `/operability/chaos` | `tests/example/operability/chaos_test.cpp` | +| Deploy strategies | `/operability/deploy` | `tests/example/operability/deploy_test.cpp` | +| Lease, election, fenced lock | `/operability/coord` | `tests/example/operability/coord_test.cpp` | +| Supply chain gate | `/supplychain` | `tests/example/supplychain/` | +| Client retry and backoff | `/clients/retry` | `tests/example/clients/retry_test.cpp` | +| Client session: cookies, pool, proxy, auth | `/clients/session` | `tests/example/clients/session_test.cpp` | +| Migrations: check then apply | `/data/migrations` | `tests/example/data/migrations_test.cpp` | +| Transaction rollback | `/data/transaction` | `tests/example/data/transaction_test.cpp` | +| Query builder and relations | `/data/query` | `tests/example/data/query_test.cpp` | +| Singleflight | `/data/singleflight` | `tests/example/data/singleflight_test.cpp` | +| TTL store, LRU, pubsub | `/data/ttl` | `tests/example/data/ttl_test.cpp` | +| Debug snapshot | `/operability/debug` | `tests/example/operability/debug_test.cpp` | + ## Layout ```text @@ -89,5 +170,7 @@ example/ fw/ # skeletal framework public API (namespace molo::example::fw) app/ # demo routes (namespace molo::example::app) static/ # disk static assets (responsive CSS, tiny JS) + assets/ # embedded at build time (Chart.js, stats.js) via src/cli/embed + tools/ # build helper wrapping the embedder tests/example/ # route-level integration tests mirroring URLs ``` diff --git a/example/SPEC.md b/example/SPEC.md index 43b4875..f9cce07 100644 --- a/example/SPEC.md +++ b/example/SPEC.md @@ -19,6 +19,31 @@ owning sibling plan and the file that will close it. 9. The release demo parses Conventional Commits, maps the bump, renders a changelog entry, and gates on checks (dry-run: never writes). 10. All pages render on a 360px viewport; wide nav collapses to a dropdown. +11. Browser form posts that fail validation answer 422 with the same form: + a summary box (count, one link per failing field) and the message next + to each invalid input, entered values kept. Never raw JSON in a page. + JSON clients (Accept or Content-Type `application/json`) keep the + `validation_error` body. One partial (`form_page.html`) and one helper + (`app/FormUi.hpp`) serve every form. +12. `GET /stats` renders Chart.js charts from `GET /stats/data`: requests, + errors, latency avg/p50/p95/p99 and histogram, bytes in/out, status + codes, methods, top routes, connections (total, active, peak, timeouts, + malformed, cap left), plus service state (stores, cache hit/miss, tasks, + workers, ORM, metrics registry). History: 90 ten-second buckets. +13. Chart.js 4.4.7 and the page script are embedded at build time: + `example/assets` -> `build/example/gen/assets.{cpp,hpp}` through the + MoloApp embedder (`src/cli/embed`), served at `/embedded/{name}`. +14. Every matrix row answers a plain GET with 2xx/3xx (401 for `/auth/me` + by design). `tests/example/matrix_test.cpp` crawls them all. +15. Feature groups added: security extras (csrf, cors, rate limit, headers, + csp, crypto, roles, secrets, upload, guards, denials, login), platform + (i18n, compression, conditional GET and ranges, probes, prometheus, + logging, trace context, DI, flash, middleware, expose stats, CLI + reference), enterprise (tenancy, audit, governance), resilience (breaker, + bulkhead, shedder, deadline, slo, chaos, deploy, coord, debug, supply + chain, client retry and session, migrations, transactions, query + builder, singleflight, ttl store). Each is a skeleton of the real `src/` + module; per-module gaps are in the header of its `fw/` file. ## Caps @@ -51,7 +76,8 @@ auth, 403 bad credentials, 404 unknown, 405 wrong method, 406 no format, ## Isolation -May depend on: C++23 stdlib, POSIX sockets, `example/fw/` only. May NOT +May depend on: C++23 stdlib, POSIX sockets, `example/fw/` only. Build-time +only: the embedder sources in `src/cli/embed` (asset embedding). May NOT depend on: any `src/` tree (does not exist yet in this worktree), wolfSSL (stub behind `ITlsContext`), real Postgres/SQLite (in-memory doubles), real network except loopback. Namespaces mirror directories: @@ -72,6 +98,11 @@ real network except loopback. Namespaces mirror directories: - Full validation coercions, template sandbox, DI container, flash messages: owner 05-request-routing.md, files `example/fw/Validate.*`, `example/fw/Templates.*`, `example/app/App.*`. +- Server accepts sequentially and stops after 512 connections (cap above); + the stats page shows connections left. Owner: 01-runtime.md, + `example/fw/Server.*`. +- Template engine has no `elif`, `not`, or macros. Owner: + 05-request-routing.md, `example/fw/Templates.*`. - Real WS/Socket.IO/SSE framing, rooms, namespaces, realtime clients: owner 06-realtime.md, file `example/app/RealtimeDemo.*`. - Worker processes, durable streams, conflict hooks, multi-instance: diff --git a/example/app/App.cpp b/example/app/App.cpp index c981c70..0de7af3 100644 --- a/example/app/App.cpp +++ b/example/app/App.cpp @@ -51,6 +51,22 @@ std::shared_ptr build_application(Services& services) { register_stats_demo(stats, services); app->include_router("", stats); + fw::Router security_extra; + register_security_extra_demo(security_extra, services); + app->include_router("", security_extra); + + fw::Router platform; + register_platform_demo(platform, services); + app->include_router("", platform); + + fw::Router enterprise; + register_enterprise_demo(enterprise, services); + app->include_router("", enterprise); + + fw::Router resilience; + register_resilience_demo(resilience, services); + app->include_router("", resilience); + register_chain_demo(app, services); return app; } diff --git a/example/app/App.hpp b/example/app/App.hpp index 0768d26..fe819f2 100644 --- a/example/app/App.hpp +++ b/example/app/App.hpp @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include "../fw/App.hpp" #include "../fw/Data.hpp" @@ -47,6 +49,12 @@ struct Services { std::shared_ptr openapi = std::make_shared(); std::shared_ptr templates = std::make_shared(); + // Feature matrix rows (feature, route) shown on the index page. Each + // register_*_demo adds its own rows with add_feature(). + std::vector> features; + void add_feature(std::string feature, std::string route) { + features.emplace_back(std::move(feature), std::move(route)); + } std::map embedded; std::string static_root = "example/static"; std::string cookie_secret = "living-example-secret"; @@ -64,6 +72,10 @@ void register_formats_demo(fw::Router& router, Services& services); void register_realtime_demo(fw::Router& router, Services& services); void register_ops_demo(fw::Router& router, Services& services); void register_stats_demo(fw::Router& router, Services& services); +void register_security_extra_demo(fw::Router& router, Services& services); +void register_platform_demo(fw::Router& router, Services& services); +void register_enterprise_demo(fw::Router& router, Services& services); +void register_resilience_demo(fw::Router& router, Services& services); void load_templates(Services& services); void load_embedded(Services& services); diff --git a/example/app/DataDemo.cpp b/example/app/DataDemo.cpp index d9733bd..5267c05 100644 --- a/example/app/DataDemo.cpp +++ b/example/app/DataDemo.cpp @@ -72,7 +72,10 @@ void register_data_demo(fw::Router& router, Services& services) { router.get("/kv/items", "kv-get", [kv](fw::Request req) { std::string key = req.query_or("key", ""); - if (key.empty()) return fw::JsonResponse("{\"error\":\"key required\"}", 400); + if (key.empty()) + return fw::JsonResponse("{\"entries\":" + std::to_string(kv->size()) + + ",\"usage\":\"GET ?key=, POST key+value\"}", + 200); std::string value; if (!kv->get(key, value)) return fw::JsonResponse("{\"error\":\"not found\"}", 404); diff --git a/example/app/EnterpriseDemo.cpp b/example/app/EnterpriseDemo.cpp new file mode 100644 index 0000000..5e864d3 --- /dev/null +++ b/example/app/EnterpriseDemo.cpp @@ -0,0 +1,691 @@ +#include "App.hpp" + +#include +#include + +#include "FormUi.hpp" +#include "../fw/Enterprise.hpp" +#include "../fw/Router.hpp" +#include "../fw/Templates.hpp" +#include "../fw/Validate.hpp" + +namespace molo::example::app { + +namespace { + +using fw::Response; +using fw::Request; + +// All enterprise demo state lives here, owned through one shared_ptr. +struct Hub { + fw::TenantResolver resolver; + fw::TenantCatalog catalog; + fw::TenantData data; + fw::TenantQuota quota; + fw::FeatureFlags flags; + fw::ConfigOverlay config; + fw::TenantMetrics metrics; + fw::AuditChain chain; + fw::RetentionStore retention; + fw::SubjectStore subjects; + fw::ApiKeyStore keys; + fw::QuotaLedger ledger; + fw::VersionTable versions; + Hub() { + chain.append("system", "boot", "audit", "chain started"); + chain.append("ada", "login", "session", "from [IP]"); + chain.append("bob", "export", "report", "monthly report"); + } +}; +using HubPtr = std::shared_ptr; + +std::string q(const std::string& s) { return "\"" + fw::json_escape(s) + "\""; } + +std::map input(const Request& req) { + auto in = fw::parse_json_object_flat(req.body); + if (in.empty()) in = fw::parse_form(req.body); + return in; +} + +std::string get_or(const std::map& m, const std::string& k, + const std::string& d = "") { + auto it = m.find(k); + return it == m.end() ? d : it->second; +} + +bool json_client(const Request& req) { return !wants_html(req); } + +Response page(fw::Environment* env, const std::string& heading, const std::string& body, + int status = 200) { + fw::TemplateCtx c; + c["title"] = heading; + c["heading"] = heading; + c["body"] = body; + return Response(status, env->render("ent_page.html", c), "text/html; charset=utf-8"); +} + +Response form_page(fw::Environment* env, FormView& v, int status = 200) { + return Response(status, env->render("form_page.html", v.ctx()), "text/html; charset=utf-8"); +} + +Response form_fail(fw::Environment* env, const Request& req, FormView& v, + const fw::ValidationError& e, + const std::map& in) { + if (json_client(req)) return fw::JsonResponse(e.to_json(), 422); + v.values = in; + v.error = &e; + return form_page(env, v, 422); +} + +Response form_ok(fw::Environment* env, const Request& req, FormView& v, + const std::string& notice, const std::string& json, int status = 200) { + if (json_client(req)) return fw::JsonResponse(json, status); + v.notice = notice; + v.done = true; + return form_page(env, v, status); +} + +std::string table(const std::vector& head, + const std::vector>& rows) { + std::string t = "
"; + for (const auto& h : head) t += ""; + t += ""; + for (const auto& r : rows) { + t += ""; + for (const auto& c : r) t += ""; + t += ""; + } + return t + "
" + fw::html_escape(h) + "
" + fw::html_escape(c) + "
"; +} + +std::string form_html(const std::string& action, const std::string& label, + const std::vector>& fields, + const std::string& button) { + std::string f = "

" + + fw::html_escape(label) + "

"; + for (const auto& [name, lab] : fields) + f += "
"; + return f + "
"; +} + +// ---- tenancy --------------------------------------------------------------- +struct Tctx { + fw::TenantRef ref; + const fw::Tenant* tenant = nullptr; + std::optional failure; +}; + +Tctx tenant_of(const Hub& h, const Request& req, const std::string& path_tenant = "") { + Tctx c; + c.ref = h.resolver.resolve(req, path_tenant); + if (!c.ref.error.empty()) { + c.failure = fw::problem_response(400, "Bad tenant id", c.ref.error, req.path); + return c; + } + c.tenant = h.catalog.find(c.ref.id); + if (!c.tenant) + c.failure = fw::problem_response(404, "Unknown tenant", "no tenant " + c.ref.id, req.path); + return c; +} + +std::string tenant_json(const Tctx& c) { + return "{\"tenant\":" + q(c.ref.id) + ",\"source\":" + q(c.ref.source) + + ",\"plan\":" + q(c.tenant ? c.tenant->plan : "") + "}"; +} + +void register_tenancy(fw::Router& router, Services& services, const HubPtr& hub, + fw::Environment* env) { + router.get("/tenancy/resolve", "tenancy-resolve", [hub](Request req) -> Response { + Tctx c = tenant_of(*hub, req); + if (c.failure) return *c.failure; + hub->metrics.inc(c.ref.id, "resolve"); + return fw::JsonResponse(tenant_json(c)); + }); + router.get("/tenancy/t/{tenant}/whoami", "tenancy-path", [hub](Request req) -> Response { + Tctx c = tenant_of(*hub, req, req.param_or("tenant", "")); + if (c.failure) return *c.failure; + hub->metrics.inc(c.ref.id, "whoami"); + return fw::JsonResponse(tenant_json(c)); + }); + + router.get("/tenancy/data", "tenancy-data", [hub](Request req) -> Response { + Tctx c = tenant_of(*hub, req); + if (c.failure) return *c.failure; + hub->metrics.inc(c.ref.id, "data.list"); + std::string items; + for (const auto& i : hub->data.list(c.ref.id)) items += (items.empty() ? "" : ",") + q(i); + return fw::JsonResponse("{\"tenant\":" + q(c.ref.id) + ",\"items\":[" + items + "]}"); + }); + router.post("/tenancy/data", "tenancy-data-add", [hub](Request req) -> Response { + Tctx c = tenant_of(*hub, req); + if (c.failure) return *c.failure; + std::string item = get_or(input(req), "item"); + if (item.empty() || item.size() > 200) + return fw::problem_response(422, "Invalid item", "item must be 1-200 chars", req.path); + if (!hub->data.add(c.ref.id, item)) + return fw::problem_response(429, "Tenant data cap", "50 items per tenant", req.path); + hub->metrics.inc(c.ref.id, "data.add"); + return fw::JsonResponse("{\"ok\":true,\"tenant\":" + q(c.ref.id) + "}", 201); + }); + + auto quota_json = [hub](const Tctx& c) { + return "{\"tenant\":" + q(c.ref.id) + ",\"plan\":" + q(c.tenant->plan) + + ",\"used\":" + std::to_string(hub->quota.used(c.ref.id)) + + ",\"limit\":" + std::to_string(fw::TenantQuota::limit_for(c.tenant->plan)) + "}"; + }; + router.get("/tenancy/quota", "tenancy-quota", [hub, quota_json](Request req) -> Response { + Tctx c = tenant_of(*hub, req); + if (c.failure) return *c.failure; + return fw::JsonResponse(quota_json(c)); + }); + router.post("/tenancy/quota", "tenancy-quota-use", [hub, quota_json](Request req) -> Response { + Tctx c = tenant_of(*hub, req); + if (c.failure) return *c.failure; + if (!hub->quota.consume(c.ref.id, c.tenant->plan)) { + hub->metrics.inc(c.ref.id, "quota.denied"); + Response r = fw::problem_response(429, "Quota exceeded", quota_json(c), req.path); + r.headers.set("Retry-After", "60"); + return r; + } + hub->metrics.inc(c.ref.id, "quota.used"); + return fw::JsonResponse(quota_json(c)); + }); + + router.get("/tenancy/flags", "tenancy-flags", [hub](Request req) -> Response { + Tctx c = tenant_of(*hub, req); + if (c.failure) return *c.failure; + std::string out; + for (const auto& [f, on] : hub->flags.effective(c.ref.id)) + out += (out.empty() ? "" : ",") + q(f) + ":" + (on ? "true" : "false"); + return fw::JsonResponse("{\"tenant\":" + q(c.ref.id) + ",\"flags\":{" + out + "}}"); + }); + router.post("/tenancy/flags", "tenancy-flags-set", [hub](Request req) -> Response { + Tctx c = tenant_of(*hub, req); + if (c.failure) return *c.failure; + auto in = input(req); + std::string flag = get_or(in, "flag"), on = get_or(in, "enabled"); + if (!hub->flags.known(flag)) + return fw::problem_response(422, "Unknown flag", "flag " + flag, req.path); + if (on != "true" && on != "false") + return fw::problem_response(422, "Invalid value", "enabled must be true or false", req.path); + if (!hub->flags.set(c.ref.id, flag, on == "true")) + return fw::problem_response(429, "Flag cap", "override cap reached", req.path); + hub->metrics.inc(c.ref.id, "flags.set"); + return fw::JsonResponse("{\"ok\":true,\"flag\":" + q(flag) + ",\"enabled\":" + on + "}"); + }); + + router.get("/tenancy/config", "tenancy-config", [hub](Request req) -> Response { + Tctx c = tenant_of(*hub, req); + if (c.failure) return *c.failure; + std::string out; + for (const auto& [k, v] : hub->config.effective(c.ref.id)) + out += (out.empty() ? "" : ",") + q(k) + ":" + q(v); + return fw::JsonResponse("{\"tenant\":" + q(c.ref.id) + ",\"config\":{" + out + "}}"); + }); + router.post("/tenancy/config", "tenancy-config-set", [hub](Request req) -> Response { + Tctx c = tenant_of(*hub, req); + if (c.failure) return *c.failure; + auto in = input(req); + std::string key = get_or(in, "key"), value = get_or(in, "value"); + if (!hub->config.known(key)) + return fw::problem_response(422, "Unknown key", "key " + key, req.path); + if (value.empty() || value.size() > 64) + return fw::problem_response(422, "Invalid value", "value must be 1-64 chars", req.path); + if (!hub->config.set(c.ref.id, key, value)) + return fw::problem_response(429, "Config cap", "override cap reached", req.path); + hub->metrics.inc(c.ref.id, "config.set"); + return fw::JsonResponse("{\"ok\":true}"); + }); + + router.get("/tenancy/metrics", "tenancy-metrics", [hub](Request req) -> Response { + (void)req; + std::string out; + for (const auto& [t, m] : hub->metrics.all()) { + std::string inner; + for (const auto& [k, v] : m) + inner += (inner.empty() ? "" : ",") + q(k) + ":" + std::to_string(v); + out += (out.empty() ? "" : ",") + q(t) + ":{" + inner + "}"; + } + return fw::JsonResponse("{\"tenants\":{" + out + "}}"); + }); + + auto prov_view = [] { + FormView v; + v.title = "Provision a tenant"; + v.action = "/tenancy/provision"; + v.submit = "Provision"; + v.fields = {{"id", "Tenant id", "text", "2-32 chars: a-z, 0-9, dash"}, + {"name", "Name", "text", ""}, + {"plan", "Plan", "text", "free or pro"}}; + return v; + }; + router.get("/tenancy/provision", "tenancy-provision-form", [env, prov_view](Request) { + FormView v = prov_view(); + return form_page(env, v); + }); + router.post("/tenancy/provision", "tenancy-provision", + [hub, env, prov_view](Request req) -> Response { + FormView v = prov_view(); + auto in = input(req); + fw::Model m({{"id", fw::FieldType::String, true, 2, 32, 0, 0, false, ""}, + {"name", fw::FieldType::String, true, 1, 80, 0, 0, false, ""}, + {"plan", fw::FieldType::String, false, 0, 8, 0, 0, false, "free"}}); + try { + auto clean = m.validate(in); + std::vector bad; + if (!fw::TenantResolver::valid_id(clean["id"])) + bad.push_back({"id", "use a-z, 0-9 and dash only"}); + if (clean["plan"] != "free" && clean["plan"] != "pro") + bad.push_back({"plan", "must be free or pro"}); + if (!bad.empty()) throw fw::ValidationError(bad); + std::string why = hub->catalog.provision(clean["id"], clean["name"], clean["plan"]); + if (!why.empty()) throw fw::ValidationError({{"id", why}}); + return form_ok(env, req, v, "Tenant " + clean["id"] + " provisioned.", + "{\"ok\":true,\"tenant\":" + q(clean["id"]) + "}", 201); + } catch (const fw::ValidationError& e) { + return form_fail(env, req, v, e, in); + } + }); + + services.add_feature("Tenant resolution (header, subdomain, path)", "/tenancy/resolve"); + services.add_feature("Tenant path resolution", "/tenancy/t/demo/whoami"); + services.add_feature("Tenant data isolation", "/tenancy/data"); + services.add_feature("Tenant quotas", "/tenancy/quota"); + services.add_feature("Tenant feature flags", "/tenancy/flags"); + services.add_feature("Tenant config overlay", "/tenancy/config"); + services.add_feature("Per-tenant metrics", "/tenancy/metrics"); + services.add_feature("Tenant provisioning form", "/tenancy/provision"); +} + +// ---- audit ----------------------------------------------------------------- +std::string records_json(const fw::AuditChain& chain) { + std::string out; + for (const auto& r : chain.records()) + out += std::string(out.empty() ? "" : ",") + "{\"index\":" + std::to_string(r.index) + + ",\"actor\":" + q(r.actor) + ",\"action\":" + q(r.action) + + ",\"subject\":" + q(r.subject) + ",\"detail\":" + q(r.detail) + + ",\"hash\":" + q(r.hash) + "}"; + return out; +} + +void register_audit(fw::Router& router, Services& services, const HubPtr& hub, + fw::Environment* env) { + auto log_view = [] { + FormView v; + v.title = "Append audit record"; + v.action = "/audit/log"; + v.submit = "Append"; + v.fields = {{"actor", "Actor", "text", ""}, + {"action", "Action", "text", ""}, + {"subject", "Subject", "text", ""}, + {"detail", "Detail", "textarea", "PII is redacted before storage"}}; + return v; + }; + + router.get("/audit/log", "audit-log", [hub, env](Request req) -> Response { + if (json_client(req)) + return fw::JsonResponse("{\"records\":[" + records_json(hub->chain) + "]}"); + std::vector> rows; + for (const auto& r : hub->chain.records()) + rows.push_back({std::to_string(r.index), r.actor, r.action, r.subject, r.detail, + r.hash.substr(0, 12)}); + auto bad = hub->chain.verify(); + std::string status = bad ? "

Chain broken at index " + + std::to_string(*bad) + "

" + : "

Chain intact (" + + std::to_string(hub->chain.records().size()) + " records)

"; + return page(env, "Audit log", + status + table({"#", "Actor", "Action", "Subject", "Detail", "Hash"}, rows) + + form_html("/audit/log", "Append record", + {{"actor", "Actor"}, {"action", "Action"}, + {"subject", "Subject"}, {"detail", "Detail"}}, + "Append") + + "

Verify

"); + }); + router.post("/audit/log", "audit-log-append", [hub, env, log_view](Request req) -> Response { + FormView v = log_view(); + auto in = input(req); + fw::Model m({{"actor", fw::FieldType::String, true, 1, 40, 0, 0, false, ""}, + {"action", fw::FieldType::String, true, 1, 40, 0, 0, false, ""}, + {"subject", fw::FieldType::String, true, 1, 80, 0, 0, false, ""}, + {"detail", fw::FieldType::String, false, 0, 500, 0, 0, false, ""}}); + try { + auto c = m.validate(in); + if (!hub->chain.append(c["actor"], c["action"], c["subject"], + fw::PiiMasker::redact(c["detail"]))) + throw fw::ValidationError({{"actor", "audit log is full (500 records, append-only)"}}); + return form_ok(env, req, v, "Record appended.", + "{\"ok\":true,\"index\":" + std::to_string(hub->chain.records().size() - 1) + "}", + 201); + } catch (const fw::ValidationError& e) { + return form_fail(env, req, v, e, in); + } + }); + + router.get("/audit/verify", "audit-verify", [hub](Request) { + auto bad = hub->chain.verify(); + return fw::JsonResponse(std::string("{\"intact\":") + (bad ? "false" : "true") + + ",\"records\":" + std::to_string(hub->chain.records().size()) + + ",\"first_broken\":" + (bad ? std::to_string(*bad) : "null") + "}"); + }); + router.post("/audit/tamper", "audit-tamper", [hub](Request req) -> Response { + std::string idx = get_or(input(req), "index"); + std::size_t i = 0; + if (idx.empty() || idx.size() > 6 || idx.find_first_not_of("0123456789") != std::string::npos) + return fw::problem_response(422, "Invalid index", "index must be a number", req.path); + i = std::stoul(idx); + if (!hub->chain.tamper(i)) + return fw::problem_response(404, "No such record", "index " + idx, req.path); + return fw::JsonResponse("{\"tampered\":" + idx + "}"); + }); + + router.get("/audit/mask", "audit-mask", [](Request req) { + std::string email = req.query_or("email", "ada.lovelace@example.com"); + std::string card = req.query_or("card", "4111 1111 1111 1111"); + std::string text = req.query_or("text", "mail ada@example.com card 4111 1111 1111 1111 ssn 123-45-6789 from 10.0.0.7"); + std::size_t hits = 0; + std::string red = fw::PiiMasker::redact(text, &hits); + return fw::JsonResponse("{\"email\":" + q(fw::PiiMasker::mask_email(email.substr(0, 120))) + + ",\"card\":" + q(fw::PiiMasker::mask_card(card.substr(0, 40))) + + ",\"redacted\":" + q(red) + ",\"redactions\":" + + std::to_string(hits) + "}"); + }); + + auto retention_json = [hub] { + std::string e, x; + for (const auto& r : hub->retention.entries()) + e += std::string(e.empty() ? "" : ",") + "{\"id\":" + q(r.id) + ",\"class\":" + q(r.klass) + + ",\"age_days\":" + std::to_string(r.age_days) + ",\"hold\":" + + (r.hold ? "true" : "false") + "}"; + for (const auto& id : hub->retention.expired()) x += (x.empty() ? "" : ",") + q(id); + return "{\"entries\":[" + e + "],\"expired\":[" + x + "]}"; + }; + router.get("/audit/retention", "audit-retention", [env, retention_json, hub](Request req) -> Response { + if (json_client(req)) return fw::JsonResponse(retention_json()); + std::vector> rows; + auto exp = hub->retention.expired(); + for (const auto& r : hub->retention.entries()) + rows.push_back({r.id, r.klass, std::to_string(r.age_days), + std::to_string(fw::RetentionStore::max_age(r.klass)), + r.hold ? "legal hold" : (std::find(exp.begin(), exp.end(), r.id) != exp.end() ? "expired" : "keep")}); + return page(env, "Retention", + table({"Id", "Class", "Age (d)", "Max (d)", "State"}, rows) + + form_html("/audit/retention/hold", "Legal hold", + {{"id", "Entry id"}, {"hold", "true or false"}}, "Set hold") + + form_html("/audit/retention/purge", "Purge expired (dry run unless confirm=yes)", + {{"confirm", "Type yes to run"}}, "Purge")); + }); + router.post("/audit/retention/hold", "audit-retention-hold", [hub](Request req) -> Response { + auto in = input(req); + std::string hold = get_or(in, "hold"); + if (hold != "true" && hold != "false") + return fw::problem_response(422, "Invalid hold", "hold must be true or false", req.path); + if (!hub->retention.set_hold(get_or(in, "id"), hold == "true")) + return fw::problem_response(404, "No such entry", get_or(in, "id"), req.path); + hub->chain.append("system", "legal-hold", get_or(in, "id"), "hold=" + hold); + return fw::JsonResponse("{\"ok\":true,\"hold\":" + hold + "}"); + }); + router.post("/audit/retention/purge", "audit-retention-purge", [hub](Request req) -> Response { + bool confirm = get_or(input(req), "confirm") == "yes"; + auto exp = hub->retention.expired(); + std::size_t n = confirm ? hub->retention.purge() : 0; + if (confirm) hub->chain.append("system", "purge", "retention", std::to_string(n) + " removed"); + return fw::JsonResponse(std::string("{\"dry_run\":") + (confirm ? "false" : "true") + + ",\"would_purge\":" + std::to_string(exp.size()) + + ",\"purged\":" + std::to_string(n) + "}"); + }); + + auto erase_view = [] { + FormView v; + v.title = "Data subject erasure"; + v.action = "/audit/subject/erase"; + v.submit = "Erase"; + v.fields = {{"subject", "Subject email", "email", "cascades across all tables"}, + {"confirm", "Confirm", "text", "type yes to erase"}}; + return v; + }; + router.get("/audit/subject", "audit-subject", [env, erase_view](Request) { + FormView v = erase_view(); + v.notice = "Export first: /audit/subject/export?subject=ada@example.com"; + return form_page(env, v); + }); + router.get("/audit/subject/export", "audit-subject-export", [hub](Request req) -> Response { + std::string s = req.query_or("subject", ""); + if (s.empty() || s.size() > 120) + return fw::problem_response(400, "Missing subject", "?subject= is required", req.path); + std::string out; + for (const auto& [t, rows] : hub->subjects.export_for(s)) { + std::string r; + for (const auto& x : rows) r += (r.empty() ? "" : ",") + q(x); + out += std::string(out.empty() ? "" : ",") + q(t) + ":[" + r + "]"; + } + return fw::JsonResponse("{\"subject\":" + q(s) + ",\"data\":{" + out + "}}"); + }); + router.post("/audit/subject/erase", "audit-subject-erase", + [hub, env, erase_view](Request req) -> Response { + FormView v = erase_view(); + auto in = input(req); + fw::Model m({{"subject", fw::FieldType::Email, true, 3, 120, 0, 0, false, ""}, + {"confirm", fw::FieldType::String, true, 1, 8, 0, 0, false, ""}}); + try { + auto c = m.validate(in); + if (c["confirm"] != "yes") throw fw::ValidationError({{"confirm", "type yes to confirm"}}); + std::size_t n = hub->subjects.erase(c["subject"]); + hub->chain.append("system", "erasure", fw::sha256_hex(c["subject"]).substr(0, 16), + std::to_string(n) + " rows removed"); + return form_ok(env, req, v, std::to_string(n) + " rows erased.", + "{\"ok\":true,\"erased\":" + std::to_string(n) + "}"); + } catch (const fw::ValidationError& e) { + return form_fail(env, req, v, e, in); + } + }); + + services.add_feature("Hash-chained audit log", "/audit/log"); + services.add_feature("Audit tamper detection", "/audit/verify"); + services.add_feature("PII masking and redaction", "/audit/mask"); + services.add_feature("Retention and legal hold", "/audit/retention"); + services.add_feature("Data subject export and erasure", "/audit/subject"); +} + +// ---- governance ------------------------------------------------------------ +void register_governance(fw::Router& router, Services& services, const HubPtr& hub, + fw::Environment* env) { + auto key_view = [] { + FormView v; + v.title = "Issue an API key"; + v.action = "/governance/keys"; + v.submit = "Issue"; + v.fields = {{"owner", "Owner", "text", ""}, + {"tier", "Tier", "text", "free or pro"}, + {"scopes", "Scopes", "text", "comma separated, e.g. read,write"}}; + return v; + }; + router.get("/governance/keys", "governance-keys-form", [env, key_view](Request) { + FormView v = key_view(); + return form_page(env, v); + }); + router.post("/governance/keys", "governance-keys", [hub, env, key_view](Request req) -> Response { + FormView v = key_view(); + auto in = input(req); + fw::Model m({{"owner", fw::FieldType::String, true, 1, 60, 0, 0, false, ""}, + {"tier", fw::FieldType::String, false, 0, 8, 0, 0, false, "free"}, + {"scopes", fw::FieldType::String, false, 0, 100, 0, 0, false, "read"}}); + try { + auto c = m.validate(in); + if (c["tier"] != "free" && c["tier"] != "pro") + throw fw::ValidationError({{"tier", "must be free or pro"}}); + std::set scopes; + std::stringstream ss(c["scopes"]); + for (std::string s; std::getline(ss, s, ',');) + if (!s.empty()) scopes.insert(s); + std::string id; + std::string key = hub->keys.issue(c["owner"], c["tier"], scopes, &id); + if (key.empty()) throw fw::ValidationError({{"owner", "key cap reached (64)"}}); + return form_ok(env, req, v, "Key " + id + ": " + key + " (shown once)", + "{\"id\":" + q(id) + ",\"key\":" + q(key) + "}", 201); + } catch (const fw::ValidationError& e) { + return form_fail(env, req, v, e, in); + } + }); + router.post("/governance/keys/{id}/revoke", "governance-keys-revoke", [hub](Request req) -> Response { + if (!hub->keys.revoke(req.param_or("id", ""))) + return fw::problem_response(404, "No such key", req.param_or("id", ""), req.path); + return fw::JsonResponse("{\"revoked\":true}"); + }); + + router.get("/governance/whoami", "governance-whoami", [hub](Request req) -> Response { + const fw::ApiKeyInfo* info = nullptr; + switch (hub->keys.authenticate(req.headers.get("X-API-Key"), "read", &info)) { + case fw::KeyResult::Ok: break; + case fw::KeyResult::Missing: + return fw::problem_response(401, "Missing key", "send X-API-Key", req.path); + case fw::KeyResult::Unknown: + return fw::problem_response(401, "Unknown key", "key not recognised", req.path); + case fw::KeyResult::Revoked: + return fw::problem_response(401, "Revoked key", "key was revoked", req.path); + case fw::KeyResult::Forbidden: + return fw::problem_response(403, "Missing scope", "scope read required", req.path); + } + fw::TierPolicy tier = fw::TierTable::find(info->tier); + bool ok = hub->ledger.charge(info->id, tier); + Response r = ok ? fw::JsonResponse("{\"id\":" + q(info->id) + ",\"owner\":" + q(info->owner) + + ",\"tier\":" + q(tier.name) + "}") + : fw::problem_response(429, "Rate limit exceeded", "tier " + tier.name, req.path); + hub->ledger.headers(info->id, tier, r.headers); + return r; + }); + router.get("/governance/tiers", "governance-tiers", [](Request) { + auto t = [](const char* n) { + auto p = fw::TierTable::find(n); + return "{\"tier\":" + q(p.name) + ",\"per_min\":" + std::to_string(p.per_min) + "}"; + }; + return fw::JsonResponse("{\"tiers\":[" + t("free") + "," + t("pro") + "]}"); + }); + + auto item = [hub](const char* ver) { + return [hub, ver](Request) { + Response r = fw::JsonResponse(std::string("{\"version\":\"") + ver + "\",\"items\":[1,2,3]}"); + hub->versions.annotate(ver, r.headers); + return r; + }; + }; + router.get("/v1/items", "v1-items", item("v1")); + router.get("/v2/items", "v2-items", item("v2")); + router.get("/governance/versions", "governance-versions", [hub](Request) { + std::string out; + for (const auto& v : hub->versions.all()) + out += std::string(out.empty() ? "" : ",") + "{\"version\":" + q(v.name) + + ",\"deprecated\":" + (v.deprecated ? "true" : "false") + + ",\"sunset\":" + q(v.sunset) + "}"; + return fw::JsonResponse("{\"versions\":[" + out + "]}"); + }); + + auto lint_view = [] { + FormView v; + v.title = "Route linter"; + v.action = "/governance/lint"; + v.submit = "Lint"; + v.fields = {{"paths", "Paths", "textarea", "one path per line, max 100"}}; + return v; + }; + router.get("/governance/lint", "governance-lint-form", [env, lint_view](Request) { + FormView v = lint_view(); + return form_page(env, v); + }); + router.post("/governance/lint", "governance-lint", [env, lint_view](Request req) -> Response { + FormView v = lint_view(); + auto in = input(req); + fw::Model m({{"paths", fw::FieldType::String, true, 1, 4000, 0, 0, false, ""}}); + try { + auto c = m.validate(in); + std::vector paths; + std::stringstream ss(c["paths"]); + for (std::string s; std::getline(ss, s);) { + while (!s.empty() && (s.back() == '\r' || s.back() == ' ')) s.pop_back(); + if (!s.empty()) paths.push_back(s); + } + auto findings = fw::RouteLinter::lint(paths); + std::string out, text; + for (const auto& f : findings) { + out += std::string(out.empty() ? "" : ",") + "{\"path\":" + q(f.path) + ",\"rule\":" + q(f.rule) + "}"; + text += f.path + " " + f.rule + "; "; + } + return form_ok(env, req, v, findings.empty() ? "No findings." : text, + "{\"findings\":[" + out + "]}"); + } catch (const fw::ValidationError& e) { + return form_fail(env, req, v, e, in); + } + }); + + auto diff_view = [] { + FormView v; + v.title = "Spec differ"; + v.action = "/governance/diff"; + v.submit = "Compare"; + v.fields = {{"before", "Before", "textarea", "METHOD /path required=a,b optional=c"}, + {"after", "After", "textarea", ""}}; + return v; + }; + router.get("/governance/diff", "governance-diff-form", [env, diff_view](Request) { + FormView v = diff_view(); + return form_page(env, v); + }); + router.post("/governance/diff", "governance-diff", [env, diff_view](Request req) -> Response { + FormView v = diff_view(); + auto in = input(req); + fw::Model m({{"before", fw::FieldType::String, true, 1, 8000, 0, 0, false, ""}, + {"after", fw::FieldType::String, true, 1, 8000, 0, 0, false, ""}}); + try { + auto c = m.validate(in); + auto findings = fw::SpecDiffer::diff(c["before"], c["after"]); + std::string out, text; + std::size_t breaking = 0; + for (const auto& f : findings) { + if (f.kind == "breaking") ++breaking; + out += std::string(out.empty() ? "" : ",") + "{\"kind\":" + q(f.kind) + + ",\"operation\":" + q(f.operation) + ",\"detail\":" + q(f.detail) + "}"; + text += f.kind + ": " + f.operation + " " + f.detail + "; "; + } + return form_ok(env, req, v, findings.empty() ? "No changes." : text, + "{\"breaking\":" + std::to_string(breaking) + ",\"findings\":[" + out + "]}"); + } catch (const fw::ValidationError& e) { + return form_fail(env, req, v, e, in); + } + }); + + router.get("/governance/problem", "governance-problem", [](Request) { + return fw::JsonResponse("{\"try\":\"/governance/problem/404\",\"format\":\"RFC 7807\"}"); + }); + router.get("/governance/problem/{status}", "governance-problem-status", [](Request req) -> Response { + std::string s = req.param_or("status", ""); + if (s.size() != 3 || s.find_first_not_of("0123456789") != std::string::npos || + std::stoi(s) < 400 || std::stoi(s) > 599) + return fw::problem_response(400, "Bad status", "status must be 400-599", req.path); + int st = std::stoi(s); + return fw::problem_response(st, fw::status_text(st), "Example problem for " + s, req.path); + }); + + services.add_feature("API keys with scopes", "/governance/keys"); + services.add_feature("Tiers and rate limit headers", "/governance/tiers"); + services.add_feature("API versions (Deprecation, Sunset)", "/v1/items"); + services.add_feature("API version list", "/governance/versions"); + services.add_feature("Route linter", "/governance/lint"); + services.add_feature("OpenAPI spec differ", "/governance/diff"); + services.add_feature("RFC 7807 problem+json", "/governance/problem"); +} + +} // namespace + +void register_enterprise_demo(fw::Router& router, Services& services) { + auto hub = std::make_shared(); + fw::Environment* env = services.templates.get(); + env->add_template("ent_page.html", + "{% extends \"base.html\" %}{% block content %}

{{ heading }}

" + "{{ body|safe }}{% endblock %}"); + register_tenancy(router, services, hub, env); + register_audit(router, services, hub, env); + register_governance(router, services, hub, env); +} + +} // namespace molo::example::app diff --git a/example/app/Index.cpp b/example/app/Index.cpp index 7d0b536..85cdd69 100644 --- a/example/app/Index.cpp +++ b/example/app/Index.cpp @@ -10,8 +10,8 @@ namespace { const char* kFeatures[][2] = { {"routing", "/routing/hello/world"}, - {"views", "/views/greet"}, - {"routers", "/routers/nested/deep"}, + {"views", "/routing/views/greet"}, + {"routers", "/routing/routers/nested/deep"}, {"chaining", "/app1/app1/app1/whoami"}, {"orm-sqlite", "/orm/users?backend=sqlite"}, {"orm-postgres", "/orm/users?backend=postgres"}, @@ -50,9 +50,10 @@ const char* kFeatures[][2] = { } // namespace -void register_index(fw::Router& router, Services& services) { - fw::Environment* env = services.templates.get(); - router.get("/", "index", [env](fw::Request req) { +void register_index(fw::Router& router, Services& services_ref) { + fw::Environment* env = services_ref.templates.get(); + Services* services = &services_ref; + router.get("/", "index", [env, services](fw::Request req) { fw::TemplateCtx ctx; ctx["title"] = std::string("MoloApp living example"); ctx["show_matrix"] = std::string("true"); @@ -60,6 +61,9 @@ void register_index(fw::Router& router, Services& services) { for (const auto& [feature, route] : kFeatures) { rows.push_back({{"feature", feature}, {"route", route}}); } + for (const auto& [feature, route] : services->features) { + rows.push_back({{"feature", feature}, {"route", route}}); + } ctx["matrix"] = rows; (void)req; return fw::Response(200, env->render("index.html", ctx), diff --git a/example/app/OpsDemo.cpp b/example/app/OpsDemo.cpp index cd38569..46b9751 100644 --- a/example/app/OpsDemo.cpp +++ b/example/app/OpsDemo.cpp @@ -186,6 +186,13 @@ void register_ops_demo(fw::Router& router, Services& services) { return fw::JsonResponse(body, 200); }); + router.get("/workflow/release", "workflow-usage", [](fw::Request req) { + (void)req; + return fw::JsonResponse( + "{\"usage\":\"POST commits=feat: add x|fix: y (Conventional " + "Commits separated by |)\",\"base_version\":\"0.1.0\"}", + 200); + }); router.post("/workflow/release", "workflow", [](fw::Request req) { auto input = fw::parse_json_object_flat(req.body); if (input.empty()) input = fw::parse_form(req.body); diff --git a/example/app/PlatformDemo.cpp b/example/app/PlatformDemo.cpp new file mode 100644 index 0000000..9424a3a --- /dev/null +++ b/example/app/PlatformDemo.cpp @@ -0,0 +1,586 @@ +#include +#include +#include +#include + +#include "App.hpp" +#include "FormUi.hpp" +#include "../fw/Cookies.hpp" +#include "../fw/Platform.hpp" +#include "../fw/Router.hpp" +#include "../fw/Templates.hpp" +#include "../fw/Validate.hpp" + +namespace molo::example::app { + +namespace { + +namespace pc = fw::platform_caps; + +std::string esc(const std::string& s) { return fw::html_escape(s); } + +bool parse_long_in(const std::string& s, long lo, long hi, long& out) { + if (s.empty() || s.size() > 12) return false; + std::size_t i = (s[0] == '-') ? 1 : 0; + if (i == s.size()) return false; + for (std::size_t k = i; k < s.size(); ++k) + if (s[k] < '0' || s[k] > '9') return false; + out = std::atol(s.c_str()); + return out >= lo && out <= hi; +} + +// All state of this feature group lives here, shared by the route lambdas. +class PlatformDemo { + public: + PlatformDemo(Services& services) + : env_(services.templates.get()), + prom_(services.metrics.get(), services.stats.get()) { + env_->add_template( + "platform_page.html", + "{% extends \"base.html\" %}{% block content %}

{{ title }}

" + "{{ body|safe }}{% endblock %}"); + seed_catalog(); + for (const char* t : {"templates", "embedded-assets", "seed-data"}) + health_.add_startup_task(t, true); + for (const char* c : {"database", "cache", "workers"}) + health_.add_check(c, true); + seed_di(); + logs_.log(fw::LogLevel::Info, "platform demo ready", "boot"); + doc_ = "Conditional GET demo document.\n0123456789abcdefghijklmnopqrstuvwxyz\n"; + } + + void mount(fw::Router& r); + + private: + fw::Response page(const std::string& title, const std::string& body, + int status = 200) const { + fw::TemplateCtx ctx; + ctx["title"] = title; + ctx["body"] = body; + return fw::Response(status, env_->render("platform_page.html", ctx), + "text/html; charset=utf-8"); + } + fw::Response form_page(const FormView& v, int status = 200) const { + return fw::Response(status, env_->render("form_page.html", v.ctx()), + "text/html; charset=utf-8"); + } + static fw::Response json(const std::string& body, int status = 200) { + fw::Response r(status, body, "application/json"); + r.headers.set("Cache-Control", "no-store"); + return r; + } + static fw::Response see_other(const std::string& to) { + fw::Response r(303, "", "text/plain; charset=utf-8"); + r.headers.set("Location", to); + return r; + } + fw::Response invalid(const FormView& base, const fw::Request& req, + const fw::ValidationError& e) const { + if (!wants_html(req)) return json(e.to_json(), 422); + FormView v = base; + v.values = fw::parse_form(req.body); + v.error = &e; + return form_page(v, 422); + } + static std::string corr(const fw::Request& req) { + std::string c = req.headers.get("X-Correlation-ID"); + return c.empty() ? req.correlation_id : c; + } + + void seed_catalog(); + void seed_di(); + void mount_i18n(fw::Router& r); + void mount_http(fw::Router& r); + void mount_ops(fw::Router& r); + void mount_observability(fw::Router& r); + void mount_app(fw::Router& r); + void mount_expose(fw::Router& r); + + fw::Environment* env_; + fw::MessageCatalog catalog_; + fw::HealthRegistry health_; + fw::PrometheusExporter prom_; + fw::LogRing logs_; + fw::TraceContextFactory trace_; + fw::ServiceRegistry di_; + fw::ExposeStats expose_; + std::mutex doc_mu_; + std::string doc_; + std::shared_ptr instance_counter_ = + std::make_shared(0); +}; + +void PlatformDemo::seed_catalog() { + catalog_.add("en", "welcome", {"Welcome"}); + catalog_.add("en", "items", {"%d item", "%d items"}); + catalog_.add("nl", "welcome", {"Welkom"}); + catalog_.add("nl", "items", {"%d item", "%d items"}); + catalog_.add("de", "welcome", {"Willkommen"}); + catalog_.add("de", "items", {"%d Artikel", "%d Artikel"}); + catalog_.add("fr", "welcome", {"Bienvenue"}); + catalog_.add("fr", "items", {"%d article", "%d articles"}); + catalog_.add("ru", "welcome", {"Добро пожаловать"}); + catalog_.add("ru", "items", {"%d предмет", "%d предмета", "%d предметов"}); + catalog_.add("ja", "welcome", {"ようこそ"}); + catalog_.add("ja", "items", {"%d 個"}); +} + +void PlatformDemo::seed_di() { + auto counter = instance_counter_; + auto make = [counter](std::string prefix) { + return [counter, prefix]() { return prefix + "#" + std::to_string(++*counter); }; + }; + di_.add("config", fw::Lifetime::Singleton, make("Config")); + di_.add("logger", fw::Lifetime::Singleton, make("Logger")); + di_.add("request-context", fw::Lifetime::Scoped, make("RequestContext")); + di_.add("mailer", fw::Lifetime::Transient, make("Mailer")); +} + +void PlatformDemo::mount_i18n(fw::Router& r) { + r.get("/i18n", "i18n-page", [this](fw::Request req) { + std::string lang_param = req.query_or("lang", ""); + std::string accept = req.headers.get("Accept-Language"); + std::string locale = fw::negotiate_locale(lang_param, accept, catalog_); + std::string source = catalog_.has_locale(fw::negotiate_locale(lang_param, "", catalog_)) && + !lang_param.empty() && + fw::negotiate_locale(lang_param, "", catalog_) == locale + ? "?lang parameter" + : (locale != catalog_.default_locale() ? "Accept-Language header" + : "default"); + std::string n_text = req.query_or("n", "3"); + long n = 0; + std::string n_error; + if (!parse_long_in(n_text, 0, pc::kMaxPluralN, n)) + n_error = "must be a whole number from 0 to " + std::to_string(pc::kMaxPluralN); + std::string body = "

Active locale: " + esc(locale) + + " (from " + source + ")

"; + body += "
" + "
"; + if (!n_error.empty()) + body += "

" + esc(n_error) + "

"; + body += "
"; + if (n_error.empty()) { + body += "

" + esc(catalog_.translate(locale, "welcome")) + "

"; + body += "

" + esc(catalog_.translate(locale, "items", n)) + "

"; + body += "

Plural form " + + std::to_string(fw::PluralRule::index(locale, n)) + " of " + + std::to_string(fw::PluralRule::forms(locale)) + ".

"; + } + body += "

Switch: "; + for (const auto& l : catalog_.locales()) + body += "" + l + " "; + body += "

catalog as .po

"; + fw::Response res = page("Internationalisation", body, n_error.empty() ? 200 : 422); + res.headers.set("Content-Language", locale); + res.headers.set("Vary", "Accept-Language"); + return res; + }); + + r.get("/i18n/po", "i18n-po", [this](fw::Request req) { + std::string locale = req.query_or("lang", "en"); + if (!catalog_.has_locale(locale)) + return fw::Response(404, "unknown locale\n", "text/plain; charset=utf-8"); + return fw::Response(200, catalog_.to_po(locale), "text/plain; charset=utf-8"); + }); +} + +void PlatformDemo::mount_http(fw::Router& r) { + r.get("/compress/demo", "compress-demo", [](fw::Request req) { + long size = 2000; + if (req.query.count("size") && + !parse_long_in(req.query_or("size", ""), 0, (long)pc::kMaxCompressBody, size)) + return fw::Response(422, "size must be 0 to " + std::to_string(pc::kMaxCompressBody) + "\n", + "text/plain; charset=utf-8"); + std::string body; + const std::string unit = "MoloApp compresses repetitive text well. "; + while ((long)body.size() < size) body += unit; + body.resize((std::size_t)size); + fw::Response res(200, body, "text/plain; charset=utf-8"); + res.headers.set("Vary", "Accept-Encoding"); + res.headers.set("X-Original-Length", std::to_string(body.size())); + if (body.size() >= pc::kCompressMin && + fw::accepts_encoding(req.headers.get("Accept-Encoding"), "gzip")) { + res.body = fw::gzip_stored(body); + res.headers.set("Content-Encoding", "gzip"); + } + return res; + }); + + r.get("/conditional/doc", "conditional-get", [this](fw::Request req) { + std::string doc; + { + std::lock_guard lock(doc_mu_); + doc = doc_; + } + std::string etag = fw::make_etag(doc); + fw::Response res(200, doc, "text/plain; charset=utf-8"); + res.headers.set("ETag", etag); + res.headers.set("Accept-Ranges", "bytes"); + res.headers.set("Cache-Control", "no-cache"); + if (fw::etag_matches(req.headers.get("If-None-Match"), etag)) { + res.status = 304; + res.body.clear(); + return res; + } + fw::ByteRange range = fw::parse_range(req.headers.get("Range"), doc.size()); + if (range.kind == fw::ByteRange::Kind::Unsatisfiable) { + res.status = 416; + res.body.clear(); + res.headers.set("Content-Range", "bytes */" + std::to_string(doc.size())); + } else if (range.kind == fw::ByteRange::Kind::Ok) { + res.status = 206; + res.body = doc.substr(range.first, range.last - range.first + 1); + res.headers.set("Content-Range", "bytes " + std::to_string(range.first) + "-" + + std::to_string(range.last) + "/" + + std::to_string(doc.size())); + } + return res; + }); + + r.post("/conditional/doc", "conditional-put", [this](fw::Request req) { + if (req.body.size() > pc::kMaxDocBytes) + return json("{\"error\":\"too_large\",\"max\":" + std::to_string(pc::kMaxDocBytes) + "}", 413); + if (req.body.empty()) + return json("{\"error\":\"validation\",\"message\":\"body is required\"}", 422); + std::lock_guard lock(doc_mu_); + std::string match = req.headers.get("If-Match"); + if (!match.empty() && !fw::etag_matches(match, fw::make_etag(doc_))) + return json("{\"error\":\"precondition_failed\",\"etag\":\"" + + fw::json_escape(fw::make_etag(doc_)) + "\"}", 412); + doc_ = req.body; + fw::Response res = json("{\"bytes\":" + std::to_string(doc_.size()) + "}"); + res.headers.set("ETag", fw::make_etag(doc_)); + return res; + }); +} + +void PlatformDemo::mount_ops(fw::Router& r) { + r.get("/healthz", "healthz", [this](fw::Request) { return json(health_.liveness_json()); }); + r.get("/readyz", "readyz", [this](fw::Request) { + return json(health_.readiness_json(), health_.ready() ? 200 : 503); + }); + r.get("/startupz", "startupz", [this](fw::Request) { + return json(health_.startup_json(), health_.startup_complete() ? 200 : 503); + }); + r.post("/readyz/checks/{name}", "readyz-toggle", [this](fw::Request req) { + std::string state = req.query_or("state", ""); + if (state != "up" && state != "down") + return json("{\"error\":\"validation\",\"message\":\"state must be up or down\"}", 422); + if (!health_.set_check(req.param_or("name", ""), state == "up")) + return json("{\"error\":\"not_found\",\"message\":\"unknown check\"}", 404); + return json(health_.readiness_json(), health_.ready() ? 200 : 503); + }); + r.post("/startupz/tasks/{name}", "startupz-toggle", [this](fw::Request req) { + std::string state = req.query_or("state", ""); + if (state != "done" && state != "pending") + return json("{\"error\":\"validation\",\"message\":\"state must be done or pending\"}", 422); + if (!health_.set_startup_task(req.param_or("name", ""), state == "done")) + return json("{\"error\":\"not_found\",\"message\":\"unknown task\"}", 404); + return json(health_.startup_json(), health_.startup_complete() ? 200 : 503); + }); + r.get("/metrics", "prometheus", [this](fw::Request) { + return fw::Response(200, prom_.render(), "text/plain; version=0.0.4; charset=utf-8"); + }); +} + +void PlatformDemo::mount_observability(fw::Router& r) { + auto emit_view = []() { + FormView v; + v.title = "Write a log record"; + v.action = "/logging/emit"; + v.submit = "Log"; + v.fields = {{"level", "Level", "text", "debug, info, warn or error"}, + {"message", "Message", "text", "secrets like password=x are redacted"}}; + return v; + }; + r.get("/logging/emit", "logging-form", [this, emit_view](fw::Request) { + return form_page(emit_view()); + }); + r.post("/logging/emit", "logging-emit", [this, emit_view](fw::Request req) { + auto input = fw::parse_form(req.body); + if (req.headers.get("Content-Type").find("application/json") != std::string::npos) + input = fw::parse_json_object_flat(req.body); + std::vector fails; + fw::LogLevel level = fw::LogLevel::Info; + if (input["level"].empty()) fails.push_back({"level", "is required"}); + else if (!fw::parse_level(input["level"], level)) + fails.push_back({"level", "must be debug, info, warn or error"}); + if (input["message"].empty()) fails.push_back({"message", "is required"}); + else if (input["message"].size() > 300) + fails.push_back({"message", "must be at most 300 characters"}); + if (!fails.empty()) return invalid(emit_view(), req, fw::ValidationError(fails)); + logs_.log(level, input["message"], corr(req)); + if (!wants_html(req)) return json("{\"ok\":true,\"size\":" + std::to_string(logs_.size()) + "}"); + return see_other("/logging/records"); + }); + r.get("/logging/records", "logging-records", [this](fw::Request req) { + long n = 20; + fw::LogLevel min = fw::LogLevel::Debug; + if (req.query.count("n") && !parse_long_in(req.query_or("n", ""), 1, 100, n)) + return page("Log records", "

n must be a whole number from 1 to 100

", 422); + if (req.query.count("level") && !fw::parse_level(req.query_or("level", ""), min)) + return page("Log records", "

level must be debug, info, warn or error

", 422); + auto recs = logs_.last((std::size_t)n, min); + if (req.query_or("format", "") == "json") { + std::string out; + for (const auto& rec : recs) out += fw::LogRing::to_json(rec) + "\n"; + return fw::Response(200, out, "application/x-ndjson"); + } + std::string body = "

Ring keeps the last " + std::to_string(pc::kLogRing) + + " records (now " + std::to_string(logs_.size()) + "). Showing " + + std::to_string(recs.size()) + ". Write one, " + "JSON lines.

" + "
" + ""; + for (const auto& rec : recs) + body += ""; + body += "
#LevelMessageCorrelation
" + std::to_string(rec.seq) + "" + fw::level_name(rec.level) + + "" + esc(rec.message) + "" + esc(rec.correlation_id) + "
"; + return page("Log records", body); + }); + + r.get("/context/trace", "context-trace", [this](fw::Request req) { + fw::TraceContext ctx = trace_.from_header(req.headers.get("traceparent")); + std::string cid = corr(req); + std::string out = "{\"correlation_id\":\"" + fw::json_escape(cid) + + "\",\"continued\":" + (ctx.parent_span_id.empty() ? "false" : "true") + + ",\"parent_valid\":" + (ctx.parent_valid ? "true" : "false") + + ",\"trace_id\":\"" + ctx.trace_id + "\",\"parent_span_id\":\"" + + ctx.parent_span_id + "\",\"span_id\":\"" + ctx.span_id + + "\",\"traceparent\":\"" + ctx.traceparent() + "\"}"; + fw::Response res = json(out); + res.headers.set("traceparent", ctx.traceparent()); + res.headers.set("X-Correlation-ID", cid); + return res; + }); +} + +void PlatformDemo::mount_app(fw::Router& r) { + r.get("/di/services", "di-services", [this](fw::Request) { + std::string body = "
" + ""; + for (const auto& e : di_.list()) + body += ""; + body += "
ServiceLifetimeCreatedResolved
" + esc(e.name) + "" + + fw::lifetime_name(e.lifetime) + "" + std::to_string(e.created) + + "" + std::to_string(e.resolved) + "

Singletons are built once, scoped once per " + "?scope= key, transients on every resolve.

"; + return page("Dependency injection", body); + }); + r.get("/di/resolve/{name}", "di-resolve", [this](fw::Request req) { + std::string name = req.param_or("name", ""); + std::string instance; + if (!di_.resolve(name, req.query_or("scope", "default"), instance)) + return json("{\"error\":\"not_found\",\"message\":\"unknown service\"}", 404); + return json("{\"name\":\"" + fw::json_escape(name) + "\",\"instance\":\"" + + fw::json_escape(instance) + "\"}"); + }); + + FormView flash_view; + flash_view.title = "Set a flash message"; + flash_view.action = "/flash/set"; + flash_view.submit = "Save and redirect"; + flash_view.fields = {{"message", "Message", "text", "shown once on the next page"}}; + r.get("/flash/set", "flash-form", [this, flash_view](fw::Request) { return form_page(flash_view); }); + r.post("/flash/set", "flash-set", [this, flash_view](fw::Request req) { + fw::Model model({{"message", fw::FieldType::String, true, 1, 120, 0, 0, false, ""}}); + std::map clean; + try { + clean = fw::Form(model).bind(req); + } catch (const fw::ValidationError& e) { + return invalid(flash_view, req, e); + } + std::string cookie = fw::make_set_cookie("flash", fw::FlashCodec::encode(clean["message"]), + "/", true, false, "Lax", 60); + fw::Response res = wants_html(req) ? see_other("/flash") : json("{\"ok\":true}"); + res.headers.set("Set-Cookie", cookie); + return res; + }); + r.get("/flash", "flash-show", [this](fw::Request req) { + std::string msg; + bool has = fw::FlashCodec::decode(req.cookie_or("flash", ""), msg); + std::string body; + if (has) body += "

" + esc(msg) + "

"; + else body += "

No flash message. It is shown once after a POST.

"; + body += "

Set a message

"; + fw::Response res = page("Flash messages", body); + if (has) res.headers.set("Set-Cookie", fw::make_set_cookie("flash", "", "/", true, false, "Lax", 0)); + return res; + }); + + r.any({fw::Method::Get, fw::Method::Post}, "/middleware/demo", "middleware-demo", + [this](fw::Request req) { + std::vector chain; + chain.push_back([](fw::Request q, fw::Next next) { // secure headers + fw::Response res = next(std::move(q)); + res.headers.set("X-Content-Type-Options", "nosniff"); + res.headers.set("X-Frame-Options", "DENY"); + res.headers.set("Referrer-Policy", "no-referrer"); + return res; + }); + chain.push_back([](fw::Request q, fw::Next next) { // body limit + if (q.body.size() > 64) + return fw::Response(413, "{\"error\":\"too_large\",\"max\":64}", "application/json"); + return next(std::move(q)); + }); + chain.push_back([](fw::Request q, fw::Next next) { // timing + auto t0 = std::chrono::steady_clock::now(); + fw::Response res = next(std::move(q)); + long us = (long)std::chrono::duration_cast( + std::chrono::steady_clock::now() - t0).count(); + res.headers.set("X-Response-Time-Us", std::to_string(us)); + return res; + }); + fw::Next end = [this](fw::Request q) { + return page("Middleware builtins", + "

This response passed through three builtin middlewares: secure headers, " + "body limit (64 bytes) and timing. Inspect the response headers.

" + "

Received " + std::to_string(q.body.size()) + " body bytes.

"); + }; + for (std::size_t i = chain.size(); i > 0; --i) { + fw::Middleware mw = chain[i - 1]; + fw::Next inner = end; + end = [mw, inner](fw::Request q) { return mw(std::move(q), inner); }; + } + return end(std::move(req)); + }); + + r.get("/cli/commands", "cli-commands", [this](fw::Request req) { + static const std::vector> cmds = { + {"new", "Scaffold a project (Makefile, AGENTS.md, .env.json, assets, templates, locales, git init)."}, + {"serve", "Dev server; --reload watches and rebuilds."}, + {"build", "Build, optionally embedding assets first."}, + {"embed", "Turn a static dir into constexpr C++ arrays, optionally gzip."}, + {"migrate", "Preview (--check) or apply (--apply) database migrations."}, + {"health", "Local liveness and readiness summary."}, + {"supervisor", "start, stop, restart, status, logs."}, + {"task", "enqueue, retry, cancel, inspect on the background queue."}, + {"ai", "new and continue: the AI agent."}, + {"expose", "serve, agent, list, stats, expose-key: reverse tunnels."}, + {"verify", "Offline artifact verification."}, + {"supply-check", "Pin and advisory gate."}, + {"release", "Ordered release gate."}, + {"rollout", "Staged deploys."}, + {"rollback", "Revert a rollout."}, + {"experiment", "Chaos and experiments."}, + {"debug", "Debug helpers."}}; + if (!wants_html(req)) { + std::string out = "{\"commands\":["; + for (std::size_t i = 0; i < cmds.size(); ++i) + out += std::string(i ? "," : "") + "{\"name\":\"" + cmds[i].first + "\",\"summary\":\"" + + fw::json_escape(cmds[i].second) + "\"}"; + return json(out + "]}"); + } + std::string body = "

Run molo help <command> for full help.

" + "
" + ""; + for (const auto& [n, s] : cmds) + body += ""; + body += "
CommandWhat it does
molo " + n + "" + esc(s) + "
"; + return page("molo commands", body); + }); +} + +void PlatformDemo::mount_expose(fw::Router& r) { + FormView view; + view.title = "Record a tunnel request"; + view.action = "/expose/record"; + view.submit = "Record"; + view.fields = {{"method", "Method", "text", "GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS"}, + {"bytes_up", "Bytes up", "number", "0 to 1000000000"}, + {"bytes_down", "Bytes down", "number", "0 to 1000000000"}, + {"micros", "Latency (microseconds)", "number", "0 to 60000000"}}; + r.get("/expose/record", "expose-form", [this, view](fw::Request) { return form_page(view); }); + r.post("/expose/record", "expose-record", [this, view](fw::Request req) { + fw::Model model( + {{"method", fw::FieldType::String, true, 3, 7, 0, 0, false, ""}, + {"bytes_up", fw::FieldType::Integer, true, 0, 0, 0, 1000000000, true, ""}, + {"bytes_down", fw::FieldType::Integer, true, 0, 0, 0, 1000000000, true, ""}, + {"micros", fw::FieldType::Integer, true, 0, 0, 0, 60000000, true, ""}}); + std::map c; + try { + c = fw::Form(model).bind(req); + if (!fw::ExposeStats::known_method(c["method"])) + throw fw::ValidationError({{"method", "must be an HTTP method in capitals"}}); + } catch (const fw::ValidationError& e) { + return invalid(view, req, e); + } + expose_.record(c["method"], std::stoul(c["bytes_up"]), std::stoul(c["bytes_down"]), + std::stoul(c["micros"])); + if (!wants_html(req)) return json("{\"ok\":true}"); + return see_other("/expose/stats"); + }); + r.get("/expose/stats", "expose-stats", [this](fw::Request req) { + auto snap = expose_.snapshot(); + auto kb = [](unsigned long b) { char x[32]; std::snprintf(x, sizeof x, "%.1f", b / 1024.0); return std::string(x); }; + auto f2 = [](double v) { char x[32]; std::snprintf(x, sizeof x, "%.2f", v); return std::string(x); }; + if (!wants_html(req)) { + std::string out = "{\"methods\":{"; + bool first = true; + for (const auto& [m, row] : snap) { + out += std::string(first ? "" : ",") + "\"" + m + "\":{\"count\":" + std::to_string(row.count) + + ",\"kb_up\":" + kb(row.bytes_up) + ",\"kb_down\":" + kb(row.bytes_down) + + ",\"kb_s_up\":" + f2(row.kb_s_up) + ",\"kb_s_down\":" + f2(row.kb_s_down) + + ",\"avg_ms\":" + f2(row.avg_ms()) + "}"; + first = false; + } + return json(out + "}}"); + } + std::string body = "

Mirror of molo expose stats. Rates are a " + "60 second window. Record a request.

" + "
" + "" + ""; + for (const auto& [m, row] : snap) + body += ""; + body += "
MethodCountkB upkB downkB/s upkB/s downAvg ms
" + m + "" + std::to_string(row.count) + "" + kb(row.bytes_up) + + "" + kb(row.bytes_down) + "" + f2(row.kb_s_up) + "" + + f2(row.kb_s_down) + "" + f2(row.avg_ms()) + "
"; + return page("Expose stats", body); + }); +} + +void PlatformDemo::mount(fw::Router& r) { + mount_i18n(r); + mount_http(r); + mount_ops(r); + mount_observability(r); + mount_app(r); + mount_expose(r); +} + +} // namespace + +void register_platform_demo(fw::Router& router, Services& services) { + auto demo = std::make_shared(services); + // Routes capture the raw pointer; the router owner keeps `demo` alive by + // holding it in a static-lifetime registry below (bounded: one per app). + static std::vector> keep; + if (keep.size() >= 64) throw std::runtime_error("platform demo instance cap"); + keep.push_back(demo); + demo->mount(router); + + services.add_feature("i18n", "/i18n"); + services.add_feature("gzip compression", "/compress/demo"); + services.add_feature("conditional GET and ranges", "/conditional/doc"); + services.add_feature("liveness probe", "/healthz"); + services.add_feature("readiness probe", "/readyz"); + services.add_feature("startup gate", "/startupz"); + services.add_feature("prometheus metrics", "/metrics"); + services.add_feature("structured logging", "/logging/records"); + services.add_feature("trace context", "/context/trace"); + services.add_feature("DI container", "/di/services"); + services.add_feature("flash messages", "/flash"); + services.add_feature("middleware builtins", "/middleware/demo"); + services.add_feature("expose stats", "/expose/stats"); + services.add_feature("CLI commands", "/cli/commands"); +} + +} // namespace molo::example::app diff --git a/example/app/RealtimeDemo.cpp b/example/app/RealtimeDemo.cpp index 1da5d93..70093c0 100644 --- a/example/app/RealtimeDemo.cpp +++ b/example/app/RealtimeDemo.cpp @@ -70,6 +70,11 @@ void register_realtime_demo(fw::Router& router, Services& services) { return fw::Response(207, webdav->propfind_xml("/files/"), "application/xml"); }); + router.get("/webdav/files/", "webdav-index", [webdav](fw::Request req) { + (void)req; + return fw::Response(200, webdav->propfind_xml("/files/"), + "application/xml"); + }); router.any({fw::Method::Get}, "/webdav/files/{name}", "webdav-get", [webdav](fw::Request req) -> fw::Response { std::string content; diff --git a/example/app/ResilienceDemo.cpp b/example/app/ResilienceDemo.cpp new file mode 100644 index 0000000..5f9e897 --- /dev/null +++ b/example/app/ResilienceDemo.cpp @@ -0,0 +1,621 @@ +#include "App.hpp" + +#include + +#include "../fw/Resilience.hpp" +#include "../fw/Router.hpp" +#include "../fw/Templates.hpp" +#include "../fw/Validate.hpp" +#include "FormUi.hpp" + +// Interactive lab pages for operability, supply chain, clients and data depth. +// Each page is a LabPage: a GET that renders the lab state plus one POST route +// per button. HTML clients get the page back, JSON clients get the lab state; +// validation failures render the shared inline-error form (422). + +namespace molo::example::app { + +namespace { + +using Params = std::map; + +const char kLabPage[] = R"LAB({% extends "base.html" %} +{% block content %} +

{{ title }}

+

{{ intro }}

+{% if notice %}

{{ notice }}

{% endif %} +
+ + +{% for r in rows %} + +{% endfor %} + +
StateValue
{{ r.k }}{{ r.v }}
+{% for a in actions %} +{{ a.html|safe }} +{% endfor %} +

Log

+
    +{% for l in log %} +
  • {{ l.line }}
  • +{% endfor %} +
+{% endblock %} +)LAB"; + +// Throws the standard field error when `value` is not one of `allowed`. +void require_in(const std::string& field, const std::string& value, + const std::vector& allowed) { + if (std::find(allowed.begin(), allowed.end(), value) != allowed.end()) return; + std::string list; + for (const auto& a : allowed) list += (list.empty() ? "" : ", ") + a; + throw fw::ValidationError({{field, "must be one of: " + list}}); +} + +// One button (with its inputs) on a lab page and the handler behind it. +class ActionSpec { + public: + ActionSpec(std::string sub, std::string label) + : sub_(std::move(sub)), label_(std::move(label)) {} + + ActionSpec& num(const std::string& name, const std::string& label, long lo, + long hi, const std::string& def, const std::string& hint = "") { + model_.push_back({name, fw::FieldType::Integer, false, 0, 1024, lo, hi, + true, def}); + form_.push_back({name, label, "number", + hint.empty() ? std::to_string(lo) + " to " + std::to_string(hi) + : hint}); + defaults_[name] = def; + return *this; + } + ActionSpec& text(const std::string& name, const std::string& label, + std::size_t min, std::size_t max, const std::string& def, + const std::string& hint = "", bool required = true) { + model_.push_back({name, fw::FieldType::String, required, min, max, 0, 0, + false, required ? "" : def}); + form_.push_back({name, label, "text", hint}); + defaults_[name] = def; + return *this; + } + ActionSpec& run(std::function fn) { + run_ = std::move(fn); + return *this; + } + ActionSpec& dynamic_defaults(std::function fn) { + dyn_ = std::move(fn); + return *this; + } + + const std::string& sub() const { return sub_; } + const std::string& label() const { return label_; } + const std::vector& form() const { return form_; } + fw::Model model() const { return fw::Model(model_); } + Params defaults() const { + Params d = defaults_; + if (dyn_) + for (const auto& [k, v] : dyn_()) d[k] = v; + return d; + } + std::string execute(const Params& p) const { return run_ ? run_(p) : ""; } + + private: + std::string sub_, label_; + std::vector model_; + std::vector form_; + Params defaults_; + std::function dyn_; + std::function run_; +}; + +class LabPage { + public: + LabPage(fw::Environment* env, std::string path, std::string title, + std::string intro, std::shared_ptr lab) + : env_(env), + path_(std::move(path)), + title_(std::move(title)), + intro_(std::move(intro)), + lab_(std::move(lab)) {} + + ActionSpec& action(std::string sub, std::string label) { + actions_.emplace_back(std::move(sub), std::move(label)); + return actions_.back(); + } + void extra_rows(std::function fn) { extra_ = std::move(fn); } + + fw::Response page(int status, const std::string& notice) const { + fw::TemplateCtx ctx; + ctx["title"] = title_; + ctx["intro"] = intro_; + ctx["notice"] = notice; + fw::TemplateList rows, acts, log; + fw::LabRows all = lab_->rows(); + if (extra_) + for (auto& r : extra_()) all.push_back(std::move(r)); + for (const auto& [k, v] : all) rows.push_back({{"k", k}, {"v", v}}); + for (const auto& a : actions_) acts.push_back({{"html", action_html(a)}}); + for (const auto& l : lab_->log()) log.push_back({{"line", l}}); + ctx["rows"] = rows; + ctx["actions"] = acts; + ctx["log"] = log; + return fw::Response(status, env_->render("lab_page.html", ctx), + "text/html; charset=utf-8"); + } + + fw::Response invalid(const ActionSpec& a, const fw::Request& req, + const fw::ValidationError& e) const { + if (!wants_html(req)) return fw::JsonResponse(e.to_json(), 422); + FormView v; + v.title = a.label(); + v.action = path_ + a.sub(); + v.submit = a.label(); + v.fields = a.form(); + v.values = a.defaults(); + for (const auto& [k, val] : fw::parse_form(req.body)) v.values[k] = val; + v.error = &e; + return fw::Response(422, env_->render("form_page.html", v.ctx()), + "text/html; charset=utf-8"); + } + + fw::Response handle(std::size_t i, const fw::Request& req) const { + const ActionSpec& a = actions_[i]; + std::string notice; + try { + notice = a.execute(fw::Form(a.model()).bind(req)); + } catch (const fw::ValidationError& e) { + return invalid(a, req, e); + } + if (!wants_html(req)) return fw::JsonResponse(lab_->json(), 200); + return page(200, notice); + } + + void mount(fw::Router& router, const std::shared_ptr& self, + const std::string& name) { + router.get(path_, name, [self](fw::Request) { return self->page(200, ""); }); + for (std::size_t i = 0; i < actions_.size(); ++i) + router.post(path_ + actions_[i].sub(), name + actions_[i].sub(), + [self, i](fw::Request req) { return self->handle(i, req); }); + } + + private: + std::string action_html(const ActionSpec& a) const { + std::string h = "
"; + Params d = a.defaults(); + for (const auto& f : a.form()) { + h += ""; + } + return h + "
"; + } + + fw::Environment* env_; + std::string path_, title_, intro_; + std::shared_ptr lab_; + std::deque actions_; // deque: stable references while adding + std::function extra_; +}; + +long to_long(const Params& p, const std::string& k) { + return std::stol(p.at(k)); +} + +// Builds, wires and registers one lab page in the index matrix. +class Mounter { + public: + Mounter(fw::Router& router, Services& services, + std::shared_ptr registry) + : router_(router), services_(services), registry_(std::move(registry)) {} + + std::shared_ptr page(const std::string& feature, + const std::string& path, + const std::string& title, + const std::string& intro, + std::shared_ptr lab) { + auto p = std::make_shared(services_.templates.get(), path, title, + intro, std::move(lab)); + pending_.push_back({p, feature, path}); + return p; + } + void finish() { + for (auto& e : pending_) { + e.page->mount(router_, e.page, e.path); + services_.add_feature(e.feature, e.path); + } + pending_.clear(); + } + + private: + struct Entry { + std::shared_ptr page; + std::string feature, path; + }; + fw::Router& router_; + Services& services_; + std::shared_ptr registry_; + std::vector pending_; +}; + +} // namespace + +void register_resilience_demo(fw::Router& router, Services& services) { + services.templates->add_template("lab_page.html", kLabPage); + auto registry = std::make_shared(); + Mounter m(router, services, registry); + + // ---- operability: resilience ------------------------------------------- + auto bclock = std::make_shared(); + auto breaker = std::make_shared(bclock); + registry->add(breaker); + auto p = m.page("Circuit breaker", "/operability/breaker", "Circuit breaker", + "Three failures open the breaker, calls are rejected while " + "open, 30 s of injected clock time later it goes half-open " + "and one good call closes it again.", + breaker); + p->action("/success", "Call succeeds").run([breaker](const Params&) { + return breaker->call(true) ? "call recorded" : "rejected: breaker open"; + }); + p->action("/failure", "Call fails").run([breaker](const Params&) { + return breaker->call(false) ? "failure recorded" : "rejected: breaker open"; + }); + p->action("/tick", "Advance 10 s").run([breaker](const Params&) { + breaker->tick(10000); + return "clock advanced 10 s"; + }); + + auto bulkhead = std::make_shared(); + registry->add(bulkhead); + p = m.page("Bulkhead", "/operability/bulkhead", "Bulkhead", + "Two concurrent slots and a queue of one; the fourth caller is " + "rejected instead of starving the rest.", + bulkhead); + p->action("/acquire", "Take a slot").run([bulkhead](const Params&) { + switch (bulkhead->acquire()) { + case fw::Bulkhead::Admission::Admitted: return "admitted"; + case fw::Bulkhead::Admission::Queued: return "queued"; + default: return "rejected: compartment full"; + } + }); + p->action("/release", "Release a slot").run([bulkhead](const Params&) { + return bulkhead->release() ? "released" : "nothing to release"; + }); + + auto shedder = std::make_shared(); + registry->add(shedder); + p = m.page("Load shedder", "/operability/shedder", "Load shedder", + "Limit 3 in flight. Low priority work is shed first (at 2), high " + "priority only at the limit.", + shedder); + p->action("/enter", "Start request") + .text("priority", "Priority", 1, 8, "low", "low or high", false) + .run([shedder](const Params& q) { + require_in("priority", q.at("priority"), {"low", "high"}); + return shedder->enter(q.at("priority") == "high") ? "accepted" : "shed"; + }); + p->action("/leave", "Finish request").run([shedder](const Params&) { + return shedder->leave() ? "finished" : "nothing in flight"; + }); + + auto deadline = std::make_shared(); + registry->add(deadline); + p = m.page("Deadline propagation", "/operability/deadline", + "Deadline propagation", + "One budget travels through every hop; each hop costs 30 ms and a " + "hop that cannot finish in time is skipped.", + deadline); + p->action("/run", "Run chain") + .num("budget_ms", "Budget ms", 0, 5000, "100") + .num("hops", "Hops", 1, 10, "3") + .run([deadline](const Params& q) { + int hop = deadline->run(to_long(q, "budget_ms"), (int)to_long(q, "hops")); + return hop ? "deadline exceeded at hop " + std::to_string(hop) + : std::string("all hops finished in budget"); + }); + + auto slo = std::make_shared(); + registry->add(slo); + p = m.page("SLO error budget", "/operability/slo", "SLO error budget", + "95.0% target over the last 100 events. The error budget is how " + "many bad events the window still allows.", + slo); + p->action("/good", "Good event").run([slo](const Params&) { + slo->record(true); + return "good event recorded"; + }); + p->action("/bad", "Bad event").run([slo](const Params&) { + slo->record(false); + return "bad event recorded"; + }); + p->action("/reset", "Reset window").run([slo](const Params&) { + slo->reset(); + return "window reset"; + }); + + // ---- operability: chaos, deploy, coordination --------------------------- + auto chaos = std::make_shared(); + registry->add(chaos); + p = m.page("Chaos experiment", "/operability/chaos", "Chaos experiment", + "Fault injection with a hard blast radius: at most 50 percent and " + "5 faults over 10 calls. Armed 0 is a dry run and injects nothing.", + chaos); + p->action("/run", "Run experiment") + .num("percent", "Percent", 0, fw::labcaps::kMaxPercent, "20") + .num("max_faults", "Max faults", 1, fw::labcaps::kMaxFaults, "2") + .num("armed", "Armed", 0, 1, "0", "0 dry run, 1 armed") + .run([chaos](const Params& q) { + auto r = chaos->run((int)to_long(q, "percent"), + (int)to_long(q, "max_faults"), to_long(q, "armed") == 1); + return std::string(r.armed ? "armed: " : "dry run: ") + + std::to_string(r.injected) + " injected, " + + std::to_string(r.planned) + " planned"; + }); + + auto deploy = std::make_shared(); + registry->add(deploy); + p = m.page("Deploy strategies", "/operability/deploy", "Deploy strategies", + "Rolling replaces one instance per step; blue-green prepares a " + "green fleet and switches in one step. Rollback restores the " + "previous fleet.", + deploy); + p->action("/start", "Start rollout") + .text("strategy", "Strategy", 1, 16, "rolling", "rolling or bluegreen", false) + .text("version", "Version", 1, 16, "v2", "", false) + .run([deploy](const Params& q) { + require_in("strategy", q.at("strategy"), {"rolling", "bluegreen"}); + return deploy->start(q.at("strategy"), q.at("version")) + ? "rollout started" + : "a rollout is already in progress"; + }); + p->action("/step", "Next step").run([deploy](const Params&) { + return deploy->step() ? "step done" : "no rollout in progress"; + }); + p->action("/rollback", "Roll back").run([deploy](const Params&) { + return deploy->rollback() ? "rolled back" : "nothing to roll back"; + }); + + auto cclock = std::make_shared(); + auto coord = std::make_shared(cclock); + registry->add(coord); + p = m.page("Lease, election, fenced lock", "/operability/coord", + "Coordination", + "Lease, leader election and distributed lock share one rule: a " + "new holder gets a higher fencing token, and writes with a stale " + "token are rejected. Leases last 10 s of injected time.", + coord); + p->action("/acquire", "Acquire") + .text("key", "Key", 1, 16, "lock", "lease, lock or leader", false) + .text("owner", "Owner", 1, 32, "a", "", true) + .run([coord](const Params& q) { + require_in("key", q.at("key"), {"lease", "lock", "leader"}); + auto g = coord->acquire(q.at("key"), q.at("owner")); + return g.ok ? "granted token " + std::to_string(g.token) + : "denied, held by " + g.holder; + }); + p->action("/release", "Release") + .text("key", "Key", 1, 16, "lock", "lease, lock or leader", false) + .text("owner", "Owner", 1, 32, "a", "", true) + .run([coord](const Params& q) { + require_in("key", q.at("key"), {"lease", "lock", "leader"}); + return coord->release(q.at("key"), q.at("owner")) ? "released" + : "not the holder"; + }); + p->action("/write", "Fenced write") + .text("key", "Key", 1, 16, "lock", "lease, lock or leader", false) + .num("token", "Token", 0, 1000000, "1") + .run([coord](const Params& q) { + require_in("key", q.at("key"), {"lease", "lock", "leader"}); + return coord->write(q.at("key"), to_long(q, "token")) + ? "write accepted" + : "write rejected: stale fencing token"; + }); + p->action("/tick", "Advance 6 s").run([coord](const Params&) { + coord->tick(6000); + return "clock advanced 6 s"; + }); + + // ---- supply chain ------------------------------------------------------- + auto supply = std::make_shared(); + registry->add(supply); + p = m.page("Supply chain gate", "/supplychain", "Supply chain gate", + "Illustrative SBOM of this example (CycloneDX shape), dependency " + "pins, an advisory scan and an attestation check. The gate passes " + "only when all three pass. The digest is FNV-1a, not a signature.", + supply); + p->action("/advisory", "Publish advisory for chartjs 4.4.1") + .run([supply](const Params&) { + return supply->add_advisory("chartjs", "4.4.1") ? "advisory published" + : "advisory db full"; + }); + p->action("/tamper", "Tamper with chartjs").run([supply](const Params&) { + supply->tamper(); + return "component changed after attestation"; + }); + p->action("/verify", "Verify attestation") + .text("digest", "Digest", 16, 16, "", "16 hex characters of sbom_digest") + .dynamic_defaults([supply] { return Params{{"digest", supply->attested()}}; }) + .run([supply](const Params& q) { + return supply->verify(q.at("digest")) ? "attestation verified" + : "attestation FAILED"; + }); + p->action("/reset", "Regenerate and attest").run([supply](const Params&) { + supply->reset(); + return "sbom regenerated"; + }); + router.get("/supplychain/sbom.json", "supplychain-sbom", + [supply](fw::Request) { + return fw::JsonResponse(supply->cyclonedx(), 200); + }); + + // ---- clients depth ------------------------------------------------------ + auto retry = std::make_shared(); + registry->add(retry); + p = m.page("Client retry and backoff", "/clients/retry", + "Client retry and backoff", + "In-process transport that fails the first N calls. 5xx is " + "retried with doubling backoff capped at 800 ms and at most 5 " + "attempts; 4xx is never retried. Waits are virtual.", + retry); + p->action("/run", "Send request") + .num("failures", "Failures", 0, 10, "2") + .num("max_attempts", "Max attempts", 1, fw::labcaps::kMaxAttempts, "3") + .num("status", "Failure status", 400, 599, "503") + .run([retry](const Params& q) { + auto o = retry->run((int)to_long(q, "failures"), + (int)to_long(q, "max_attempts"), + (int)to_long(q, "status")); + return "final status " + std::to_string(o.status) + " after " + + std::to_string(o.attempts) + " attempt(s)"; + }); + + auto session = std::make_shared(); + registry->add(session); + p = m.page("Client session: cookies, pool, proxy, auth", "/clients/session", + "Client session", + "Each request goes through the in-process transport: the server " + "sets cookies into the jar, connections are pooled and reused, a " + "proxy change opens a new connection, and the auth scheme sets the " + "Authorization header.", + session); + p->action("/request", "Send request") + .text("auth", "Auth", 1, 8, "none", "none, basic or bearer", false) + .text("proxy", "Proxy", 0, 80, "", "http://host:port or empty", false) + .run([session](const Params& q) { + require_in("auth", q.at("auth"), {"none", "basic", "bearer"}); + auto px = q.count("proxy") ? q.at("proxy") : std::string(); + if (!fw::ClientSessionLab::valid_proxy(px)) + throw fw::ValidationError( + {{"proxy", "must be empty or http(s)/socks5://host:port"}}); + auto h = session->request(q.at("auth"), px); + std::string out = "sent:"; + for (const auto& [k, v] : h) out += " " + k + "=" + v + ";"; + return out; + }); + + // ---- data depth --------------------------------------------------------- + auto mig = std::make_shared(); + registry->add(mig); + p = m.page("Migrations: check then apply", "/data/migrations", + "Migrations", + "Preview shows what apply would run without changing anything. " + "Apply runs pending steps in order. Drift drops a table out of " + "band and heal re-creates it from its migration.", + mig); + p->extra_rows([mig] { + fw::LabRows r; + for (const auto& line : mig->check()) r.emplace_back("preview", line); + return r; + }); + p->action("/apply", "Apply") + .num("limit", "Steps", 1, 3, "3") + .run([mig](const Params& q) { + int n = mig->apply((int)to_long(q, "limit")); + return std::to_string(n) + " migration(s) applied"; + }); + p->action("/drift", "Drop posts table").run([mig](const Params&) { + mig->drop_table("posts"); + return "posts dropped out of band"; + }); + p->action("/heal", "Heal drift").run([mig](const Params&) { + return std::to_string(mig->heal()) + " table(s) healed"; + }); + + auto tx = std::make_shared(); + registry->add(tx); + p = m.page("Transaction rollback", "/data/transaction", "Transaction rollback", + "Transfer between alice and bob inside a transaction. Overdrafts " + "and a failure after the debit roll back, so no money is lost.", + tx); + p->action("/transfer", "Transfer") + .text("from", "From", 1, 8, "alice", "alice or bob", false) + .text("to", "To", 1, 8, "bob", "alice or bob", false) + .num("amount", "Amount", 1, 1000, "30") + .num("fail_after_debit", "Fail after debit", 0, 1, "0", "0 or 1") + .run([tx](const Params& q) { + require_in("from", q.at("from"), {"alice", "bob"}); + require_in("to", q.at("to"), {"alice", "bob"}); + if (q.at("from") == q.at("to")) + throw fw::ValidationError({{"to", "must differ from the source"}}); + return tx->transfer(q.at("from"), q.at("to"), to_long(q, "amount"), + to_long(q, "fail_after_debit") == 1); + }); + + auto query = std::make_shared(); + registry->add(query); + p = m.page("Query builder and relations", "/data/query", + "Query builder and relations", + "Builds a joined SELECT over users has-many posts and runs it on " + "in-memory tables. User 0 means all users.", + query); + p->action("/run", "Run query") + .num("user_id", "User id", 0, 2, "0") + .text("order", "Order", 1, 8, "id", "id or title", false) + .num("limit", "Limit", 1, 10, "5") + .run([query](const Params& q) { + require_in("order", q.at("order"), {"id", "title"}); + auto r = query->run(to_long(q, "user_id"), q.at("order"), + (int)to_long(q, "limit")); + return std::to_string(r.rows.size()) + " row(s)"; + }); + + auto flight = std::make_shared(); + registry->add(flight); + p = m.page("Singleflight", "/data/singleflight", "Singleflight", + "Concurrent callers asking for the same key share one load. The " + "loader waits for the other callers to join, so the result is " + "deterministic. At most 16 threads.", + flight); + p->action("/run", "Run callers") + .num("callers", "Callers", 1, fw::labcaps::kMaxThreads, "8") + .run([flight](const Params& q) { + int callers = (int)to_long(q, "callers"); + int loads = flight->run(callers); + return std::to_string(callers) + " callers, " + std::to_string(loads) + + " load(s)"; + }); + + auto tclock = std::make_shared(); + auto ttl = std::make_shared(tclock); + registry->add(ttl); + p = m.page("TTL store, LRU, pubsub", "/data/ttl", "TTL store", + "Memory store with per-key TTL on injected time, LRU eviction at " + "5 entries, and a pubsub topic kv.events announcing every change.", + ttl); + p->extra_rows([ttl] { + fw::LabRows r; + for (const auto& e : ttl->events()) r.emplace_back("event", e); + return r; + }); + p->action("/set", "Set key") + .text("key", "Key", 1, 16, "", "", true) + .text("value", "Value", 1, 32, "", "", true) + .num("ttl_s", "TTL s", 1, 60, "10") + .run([ttl](const Params& q) { + ttl->set(q.at("key"), q.at("value"), to_long(q, "ttl_s")); + return "stored " + q.at("key"); + }); + p->action("/get", "Get key") + .text("key", "Key", 1, 16, "", "", true) + .run([ttl](const Params& q) { + std::string v; + return ttl->get(q.at("key"), v) ? "hit: " + v : std::string("miss"); + }); + p->action("/tick", "Advance 5 s").run([ttl](const Params&) { + ttl->tick(5000); + return "clock advanced 5 s"; + }); + + m.finish(); + + router.get("/operability/debug", "operability-debug", + [registry](fw::Request) { + return fw::JsonResponse(registry->snapshot(), 200); + }); + services.add_feature("Debug snapshot", "/operability/debug"); +} + +} // namespace molo::example::app diff --git a/example/app/SecurityDemo.cpp b/example/app/SecurityDemo.cpp index 865f834..fbc3350 100644 --- a/example/app/SecurityDemo.cpp +++ b/example/app/SecurityDemo.cpp @@ -69,6 +69,20 @@ void register_security_demo(fw::Router& router, Services& services) { return res; }); + router.get("/uploads/file", "upload-list", [uploads](fw::Request req) { + (void)req; + std::string body = "{\"files\":["; + bool first = true; + for (const auto& u : uploads->list()) { + body += std::string(first ? "" : ",") + "{\"file\":\"" + + fw::json_escape(u.filename) + "\",\"size\":" + + std::to_string(u.size) + "}"; + first = false; + } + return fw::JsonResponse( + body + "],\"usage\":\"POST body to /uploads/file?filename=\"}", + 200); + }); router.post("/uploads/file", "upload", [uploads](fw::Request req) { std::string filename = req.query_or("filename", "upload.txt"); std::string content_type = diff --git a/example/app/SecurityExtraDemo.cpp b/example/app/SecurityExtraDemo.cpp new file mode 100644 index 0000000..66225a7 --- /dev/null +++ b/example/app/SecurityExtraDemo.cpp @@ -0,0 +1,430 @@ +#include "App.hpp" + +#include "FormUi.hpp" +#include "../fw/Cookies.hpp" +#include "../fw/Router.hpp" +#include "../fw/SecurityExtra.hpp" +#include "../fw/Validate.hpp" + +namespace molo::example::app { + +namespace { + +constexpr const char* kHtml = "text/html; charset=utf-8"; + +// Shared state of this group, owned by the closures through shared_ptr. +struct SecState { + fw::DenialSink denials; + fw::CsrfGuard csrf; + fw::CorsPolicy cors; + fw::RateLimiter limiter; + fw::CspReportStore csp_reports; + fw::PermissionGuard perms; + fw::SecretsProvider secrets; + explicit SecState(const std::string& secret) + : csrf(secret), + cors({"https://app.example.com", "https://admin.example.com"}), + limiter(5, 60) {} +}; + +// One place that renders a FormView and maps failures to HTML or JSON. +class FormPage { + public: + FormPage(fw::Environment* env, SecState* st) : env_(env), st_(st) {} + + fw::Response show(const FormView& v, int status = 200) const { + return fw::Response(status, env_->render("form_page.html", v.ctx()), kHtml); + } + fw::Response fail(FormView v, const fw::Request& req, const std::string& guard, + const std::string& field, const std::string& message, + int status) const { + st_->denials.record(guard, message); + if (!wants_html(req)) + return fw::JsonResponse("{\"error\":\"" + fw::json_escape(guard) + + "\",\"field\":\"" + fw::json_escape(field) + + "\",\"message\":\"" + + fw::json_escape(message) + "\"}", + status); + fw::ValidationError err({{field, message}}); + v.values = fw::parse_form(req.body); + v.error = &err; + return show(v, status); + } + fw::Response ok(FormView v, const fw::Request& req, const std::string& notice, + const std::string& json, int status = 200) const { + if (!wants_html(req)) return fw::JsonResponse(json, status); + v.done = true; + v.notice = notice; + return show(v, status); + } + + private: + fw::Environment* env_; + SecState* st_; +}; + +std::map input_of(const fw::Request& req) { + if (req.headers.get("Content-Type").find("json") != std::string::npos) + return fw::parse_json_object_flat(req.body); + return fw::parse_form(req.body); +} + +std::string get(const std::map& m, const std::string& k) { + auto it = m.find(k); + return it == m.end() ? "" : it->second; +} + +FormView make_view(const std::string& title, const std::string& action, + const std::string& submit, std::vector fields) { + FormView v; + v.title = title; + v.action = action; + v.submit = submit; + v.fields = std::move(fields); + return v; +} + +std::string json_list(const std::vector& items) { + std::string out = "["; + for (std::size_t i = 0; i < items.size(); ++i) + out += (i ? "," : "") + std::string("\"") + fw::json_escape(items[i]) + "\""; + return out + "]"; +} + +} // namespace + +void register_security_extra_demo(fw::Router& router, Services& services) { + auto st = std::make_shared(services.cookie_secret + "-csrf"); + fw::Environment* env = services.templates.get(); + FormPage page(env, st.get()); + auto auth = services.auth; + auto sessions = services.sessions; + std::string secret = services.cookie_secret; + + // ---- CSRF -------------------------------------------------------------- + auto csrf_view = [](const std::string& token) { + FormView v = make_view("CSRF protected form", "/security/csrf", "Save", + {{"csrf_token", "CSRF token", "text", + "issued by the server; edit it to see a 403"}, + {"note", "Note", "text", "any text, 1 to 80 chars"}}); + v.values["csrf_token"] = token; + return v; + }; + router.get("/security/csrf", "csrf-form", [st, page, csrf_view](fw::Request) -> fw::Response { + return page.show(csrf_view(st->csrf.issue())); + }); + router.post("/security/csrf", "csrf-post", [st, page, csrf_view](fw::Request req) -> fw::Response { + auto in = input_of(req); + FormView v = csrf_view(""); + if (!st->csrf.verify(get(in, "csrf_token"))) + return page.fail(v, req, "CsrfGuard", "csrf_token", + "missing or invalid CSRF token, reload the form", 403); + std::string note = get(in, "note"); + if (note.empty() || note.size() > 80) + return page.fail(v, req, "Validation", "note", "must be 1 to 80 characters", 422); + return page.ok(v, req, "Saved: " + note, + "{\"ok\":true,\"note\":\"" + fw::json_escape(note) + "\"}"); + }); + + // ---- CORS -------------------------------------------------------------- + router.get("/security/cors", "cors-info", [st](fw::Request) -> fw::Response { + return fw::JsonResponse( + "{\"allowed_origins\":[\"https://app.example.com\",\"https://admin.example.com\"]," + "\"usage\":\"OPTIONS /security/cors with Origin and Access-Control-Request-Method\"}"); + }); + router.options("/security/cors", "cors-preflight", [st](fw::Request req) -> fw::Response { + fw::Response res(204, "", "text/plain"); + if (!st->cors.apply_preflight(req, res)) { + st->denials.record("CorsPolicy", "origin not allowed: " + req.headers.get("Origin")); + return fw::JsonResponse("{\"error\":\"origin not allowed\"}", 403); + } + return res; + }); + + // ---- Rate limiting ----------------------------------------------------- + router.get("/security/rate", "rate-limited", [st](fw::Request req) -> fw::Response { + std::string key = req.headers.get("X-Client-Id"); + if (key.empty() || key.size() > 64) key = "anon"; + int retry = 0, remaining = 0; + if (!st->limiter.hit(key, retry, remaining)) { + st->denials.record("RateLimiter", "limit hit for " + key); + fw::Response res = fw::JsonResponse( + "{\"error\":\"rate_limited\",\"retry_after\":" + std::to_string(retry) + "}", 429); + res.headers.set("Retry-After", std::to_string(retry)); + return res; + } + fw::Response res = fw::JsonResponse( + "{\"ok\":true,\"limit\":" + std::to_string(st->limiter.limit()) + + ",\"window_seconds\":" + std::to_string(st->limiter.window()) + + ",\"remaining\":" + std::to_string(remaining) + "}"); + res.headers.set("X-RateLimit-Remaining", std::to_string(remaining)); + return res; + }); + + // ---- Security headers -------------------------------------------------- + router.get("/security/headers", "security-headers", [](fw::Request) -> fw::Response { + std::string names = "["; + bool first = true; + for (const auto& [k, v] : fw::SecurityHeaders::defaults()) { + names += std::string(first ? "" : ",") + "{\"name\":\"" + k + + "\",\"value\":\"" + fw::json_escape(v) + "\"}"; + first = false; + } + fw::Response res = fw::JsonResponse("{\"applied\":" + names + "]}"); + fw::SecurityHeaders::apply(res); + return res; + }); + + // ---- CSP with nonce and report endpoint -------------------------------- + router.get("/csp/page", "csp-page", [](fw::Request) -> fw::Response { + std::string nonce = fw::SecureRandom::token_hex(16); + fw::CspBuilder csp; + csp.script_nonce(nonce).report_uri("/csp/report"); + std::string body = + "" + "" + "CSP nonce

CSP nonce demo

" + "

Script blocked.

Nonce: " + nonce + + "

Violation reports

" + "" + ""; + fw::Response res(200, body, kHtml); + res.headers.set(csp.header_name(), csp.build()); + return res; + }); + router.get("/csp/report", "csp-report-list", [st](fw::Request) -> fw::Response { + std::string out = "{\"total\":" + std::to_string(st->csp_reports.total()) + ",\"reports\":["; + bool first = true; + for (const auto& r : st->csp_reports.list()) { + out += std::string(first ? "" : ",") + "\"" + fw::json_escape(r) + "\""; + first = false; + } + return fw::JsonResponse(out + "]}"); + }); + router.post("/csp/report", "csp-report", [st](fw::Request req) -> fw::Response { + if (!st->csp_reports.add(req.body)) { + st->denials.record("CspReport", "empty or oversized report"); + return fw::JsonResponse("{\"error\":\"report empty or over 4096 bytes\"}", 400); + } + return fw::Response(204, "", "text/plain"); + }); + + // ---- Crypto: hash and HMAC -------------------------------------------- + auto hash_view = []() { + return make_view("SHA-256 and HMAC-SHA256", "/crypto/hash", "Hash", + {{"text", "Text", "text", "up to 4096 bytes"}, + {"key", "HMAC key", "text", "optional, default demo-key"}}); + }; + router.get("/crypto/hash", "crypto-hash-form", [page, hash_view](fw::Request) -> fw::Response { + return page.show(hash_view()); + }); + router.post("/crypto/hash", "crypto-hash", [page, hash_view](fw::Request req) -> fw::Response { + auto in = input_of(req); + std::string text = get(in, "text"); + std::string key = get(in, "key"); + if (key.empty()) key = "demo-key"; + if (text.empty() || text.size() > fw::secaps::kMaxHashInput) + return page.fail(hash_view(), req, "Validation", "text", + "must be 1 to 4096 bytes", 422); + std::string sha = fw::Sha256::hex(text); + std::string mac = fw::to_hex(fw::Hmac::sha256(key, text)); + return page.ok(hash_view(), req, "sha256=" + sha + " hmac=" + mac, + "{\"sha256\":\"" + sha + "\",\"hmac_sha256\":\"" + mac + "\"}"); + }); + + // ---- Crypto: secure random -------------------------------------------- + router.get("/crypto/random", "crypto-random", [](fw::Request req) -> fw::Response { + long n = 16; + try { n = std::stol(req.query_or("bytes", "16")); } catch (...) { n = -1; } + if (n < 1 || n > static_cast(fw::secaps::kMaxRandomBytes)) + return fw::JsonResponse("{\"error\":\"bytes must be 1 to 64\"}", 400); + return fw::JsonResponse("{\"bytes\":" + std::to_string(n) + ",\"token\":\"" + + fw::SecureRandom::token_hex(static_cast(n)) + "\"}"); + }); + + // ---- Crypto: password hashing ----------------------------------------- + auto pw_view = []() { + return make_view("Password hashing (DEMO)", "/crypto/password", "Hash", + {{"password", "Password", "password", + "8 to 128 chars; salted PBKDF2 at demo rounds, not production"}}); + }; + router.get("/crypto/password", "crypto-password-form", [page, pw_view](fw::Request) -> fw::Response { + return page.show(pw_view()); + }); + router.post("/crypto/password", "crypto-password", [page, pw_view](fw::Request req) -> fw::Response { + std::string pw = get(input_of(req), "password"); + if (pw.size() < 8 || pw.size() > 128) + return page.fail(pw_view(), req, "Validation", "password", + "must be 8 to 128 characters", 422); + std::string stored = fw::PasswordHasher::hash(pw); + bool good = fw::PasswordHasher::verify(pw, stored); + bool bad = fw::PasswordHasher::verify(pw + "x", stored); + return page.ok(pw_view(), req, + "Stored form: " + stored + " (verify ok: " + (good ? "yes" : "no") + + ", wrong password: " + (bad ? "accepted" : "rejected") + ")", + "{\"stored\":\"" + stored + "\",\"verify\":" + (good ? "true" : "false") + + ",\"verify_wrong\":" + (bad ? "true" : "false") + "}"); + }); + + // ---- Roles and permissions -------------------------------------------- + router.get("/security/roles", "roles-info", [st](fw::Request) -> fw::Response { + std::string out = "{\"tokens\":{\"viewer-token\":\"viewer\",\"editor-token\":\"editor\"," + "\"admin-token\":\"admin\"},\"roles\":{"; + bool first = true; + for (const char* r : {"viewer", "editor", "admin"}) { + out += std::string(first ? "" : ",") + "\"" + r + "\":" + json_list(st->perms.permissions(r)); + first = false; + } + return fw::JsonResponse(out + "},\"usage\":\"GET /security/admin with Authorization: Bearer \"}"); + }); + router.get("/security/admin", "admin-area", [st](fw::Request req) -> fw::Response { + std::string role; + auto verdict = st->perms.check(req.headers.get("Authorization"), "admin:manage", role); + if (verdict == fw::PermissionGuard::Verdict::Unauthenticated) { + st->denials.record("Guard", "unauthenticated"); + fw::Response res = fw::JsonResponse("{\"error\":\"authentication required\"}", 401); + res.headers.set("WWW-Authenticate", "Bearer"); + return res; + } + if (verdict == fw::PermissionGuard::Verdict::Forbidden) { + st->denials.record("Guard", "role " + role + " lacks admin:manage"); + return fw::JsonResponse("{\"error\":\"forbidden\",\"role\":\"" + role + "\"}", 403); + } + return fw::JsonResponse("{\"ok\":true,\"role\":\"" + role + "\"}"); + }); + + // ---- Secrets provider ------------------------------------------------- + router.get("/security/secrets", "secrets", [st](fw::Request req) -> fw::Response { + std::string name = req.query_or("name", ""); + if (!name.empty()) { + std::string m; + if (!st->secrets.masked(name, m)) + return fw::JsonResponse("{\"error\":\"unknown secret\"}", 404); + return fw::JsonResponse("{\"name\":\"" + fw::json_escape(name) + "\",\"value\":\"" + m + "\"}"); + } + return fw::JsonResponse("{\"names\":" + json_list(st->secrets.names()) + + ",\"note\":\"values are always masked\"}"); + }); + + // ---- Upload validation ------------------------------------------------ + auto up_view = []() { + return make_view("Upload validation", "/security/upload", "Check", + {{"filename", "File name", "text", "pdf, png or txt"}, + {"content_hex", "Content (hex bytes)", "text", + "pdf starts 255044462d, png starts 89504e47, max 1024 bytes"}}); + }; + router.get("/security/upload", "upload-check-form", [page, up_view](fw::Request) -> fw::Response { + return page.show(up_view()); + }); + router.post("/security/upload", "upload-check", [page, up_view](fw::Request req) -> fw::Response { + auto in = input_of(req); + std::string bytes; + std::string hex = get(in, "content_hex"); + if (hex.size() > fw::secaps::kMaxUploadHex || !fw::from_hex(hex, bytes)) + return page.fail(up_view(), req, "UploadValidator", "content_hex", + "must be an even number of hex digits, at most 4096", 422); + std::string why = fw::UploadValidator::check(get(in, "filename"), bytes); + if (!why.empty()) { + bool big = why.find("too large") != std::string::npos; + return page.fail(up_view(), req, "UploadValidator", + why.find("file name") != std::string::npos || + why.find("extension") != std::string::npos + ? "filename" + : "content_hex", + why, big ? 413 : 422); + } + return page.ok(up_view(), req, "Accepted " + get(in, "filename") + " (" + + std::to_string(bytes.size()) + " bytes)", + "{\"ok\":true,\"size\":" + std::to_string(bytes.size()) + "}"); + }); + + // ---- SQL safety and path guard ---------------------------------------- + auto guard_view = []() { + return make_view("SQL and path guard", "/security/guard", "Check", + {{"kind", "Kind", "text", "sql or path"}, + {"value", "Value", "text", "try ' OR 1=1 -- or ../etc/passwd"}}); + }; + router.get("/security/guard", "guard-form", [page, guard_view](fw::Request) -> fw::Response { + return page.show(guard_view()); + }); + router.post("/security/guard", "guard-check", [page, guard_view](fw::Request req) -> fw::Response { + auto in = input_of(req); + std::string kind = get(in, "kind"), value = get(in, "value"); + if (kind != "sql" && kind != "path") + return page.fail(guard_view(), req, "Validation", "kind", "must be sql or path", 422); + if (value.empty() || value.size() > fw::secaps::kMaxInputLen) + return page.fail(guard_view(), req, "Validation", "value", "must be 1 to 512 characters", 422); + std::string why = kind == "sql" ? fw::SqlSafety::check(value) : fw::PathGuard::check(value); + if (!why.empty()) + return page.fail(guard_view(), req, kind == "sql" ? "SqlSafety" : "PathGuard", "value", why, 422); + return page.ok(guard_view(), req, "Value is safe for " + kind, + "{\"ok\":true,\"kind\":\"" + kind + "\"}"); + }); + + // ---- Denial sink ------------------------------------------------------ + router.get("/security/denials", "denials", [st](fw::Request) -> fw::Response { + std::string out = "{\"total\":" + std::to_string(st->denials.total()) + ",\"recent\":["; + bool first = true; + for (const auto& d : st->denials.list()) { + out += std::string(first ? "" : ",") + "{\"guard\":\"" + fw::json_escape(d.guard) + + "\",\"reason\":\"" + fw::json_escape(d.reason) + "\"}"; + first = false; + } + return fw::JsonResponse(out + "]}"); + }); + + // ---- Login form with session cookie ----------------------------------- + auto login_view = []() { + return make_view("Sign in", "/security/login", "Sign in", + {{"username", "Username", "text", "demo: ada"}, + {"password", "Password", "password", "demo: correct-horse"}}); + }; + router.get("/security/login", "login-form", [page, login_view](fw::Request) -> fw::Response { + return page.show(login_view()); + }); + router.post("/security/login", "login-post", + [page, login_view, auth, sessions, secret](fw::Request req) -> fw::Response { + auto in = input_of(req); + std::string user = get(in, "username"); + if (user.empty() || user.size() > 64) + return page.fail(login_view(), req, "Validation", "username", "is required (max 64)", 422); + if (get(in, "password").empty()) + return page.fail(login_view(), req, "Validation", "password", "is required", 422); + if (!auth->check(user, get(in, "password"))) + return page.fail(login_view(), req, "Authenticator", "password", "wrong username or password", 401); + std::string id = sessions->create(); + sessions->put(id, "user", user); + fw::Response res = page.ok(login_view(), req, "Signed in as " + user, + "{\"user\":\"" + fw::json_escape(user) + "\"}"); + res.headers.set("Set-Cookie", fw::make_set_cookie("login_sid", fw::cookie_sign(id, secret), + "/", true, false, "Lax", 3600)); + return res; + }); + router.get("/security/whoami", "whoami", [sessions, secret](fw::Request req) -> fw::Response { + std::string id; + std::map data; + if (!fw::cookie_verify(req.cookie_or("login_sid", ""), secret, id) || + !sessions->get(id, data) || !data.count("user")) + return fw::JsonResponse("{\"error\":\"not signed in\"}", 401); + return fw::JsonResponse("{\"user\":\"" + fw::json_escape(data["user"]) + "\"}"); + }); + + services.add_feature("CSRF token form", "/security/csrf"); + services.add_feature("CORS preflight allowlist", "/security/cors"); + services.add_feature("Rate limiter (429 + Retry-After)", "/security/rate"); + services.add_feature("Security headers", "/security/headers"); + services.add_feature("CSP nonce page", "/csp/page"); + services.add_feature("CSP report endpoint", "/csp/report"); + services.add_feature("SHA-256 and HMAC", "/crypto/hash"); + services.add_feature("Secure random token", "/crypto/random"); + services.add_feature("Password hashing (demo)", "/crypto/password"); + services.add_feature("Roles and permissions", "/security/roles"); + services.add_feature("Secrets provider (masked)", "/security/secrets"); + services.add_feature("Upload validation", "/security/upload"); + services.add_feature("SQL and path guard", "/security/guard"); + services.add_feature("Denial sink", "/security/denials"); + services.add_feature("Login form with session cookie", "/security/login"); +} + +} // namespace molo::example::app diff --git a/example/example.mk b/example/example.mk index ed77f7e..ac348fe 100644 --- a/example/example.mk +++ b/example/example.mk @@ -25,6 +25,10 @@ EXAMPLE_SRCS = \ $(EXAMPLE_DIR)/fw/Tls.cpp \ $(EXAMPLE_DIR)/fw/Kv.cpp \ $(EXAMPLE_DIR)/fw/Stats.cpp \ + $(EXAMPLE_DIR)/fw/SecurityExtra.cpp \ + $(EXAMPLE_DIR)/fw/Platform.cpp \ + $(EXAMPLE_DIR)/fw/Enterprise.cpp \ + $(EXAMPLE_DIR)/fw/Resilience.cpp \ $(EXAMPLE_DIR)/fw/Observ.cpp \ $(EXAMPLE_DIR)/fw/Data.cpp \ $(EXAMPLE_DIR)/fw/Jobs.cpp \ @@ -43,7 +47,11 @@ EXAMPLE_SRCS = \ $(EXAMPLE_DIR)/app/FormatsDemo.cpp \ $(EXAMPLE_DIR)/app/RealtimeDemo.cpp \ $(EXAMPLE_DIR)/app/OpsDemo.cpp \ - $(EXAMPLE_DIR)/app/StatsDemo.cpp + $(EXAMPLE_DIR)/app/StatsDemo.cpp \ + $(EXAMPLE_DIR)/app/SecurityExtraDemo.cpp \ + $(EXAMPLE_DIR)/app/PlatformDemo.cpp \ + $(EXAMPLE_DIR)/app/EnterpriseDemo.cpp \ + $(EXAMPLE_DIR)/app/ResilienceDemo.cpp # Tests are discovered: every tests/example/**/*_test.cpp is one route test. EXAMPLE_TEST_JOBS ?= 8 diff --git a/example/fw/Enterprise.cpp b/example/fw/Enterprise.cpp new file mode 100644 index 0000000..2f5ca74 --- /dev/null +++ b/example/fw/Enterprise.cpp @@ -0,0 +1,544 @@ +#include "Enterprise.hpp" + +#include +#include +#include + +namespace molo::example::fw { + +namespace { + +std::uint32_t rotr(std::uint32_t x, int n) { return (x >> n) | (x << (32 - n)); } + +std::vector split(const std::string& s, char sep) { + std::vector out; + std::string cur; + for (char c : s) { + if (c == sep) { + out.push_back(cur); + cur.clear(); + } else { + cur += c; + } + } + out.push_back(cur); + return out; +} + +std::string trim(const std::string& s) { + std::size_t a = s.find_first_not_of(" \t\r\n"); + if (a == std::string::npos) return ""; + return s.substr(a, s.find_last_not_of(" \t\r\n") - a + 1); +} + +} // namespace + +std::string sha256_hex(const std::string& data) { + static const std::uint32_t k[64] = { + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, + 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, + 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, + 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, + 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, + 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, + 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, + 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, + 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; + std::uint32_t h[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, + 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19}; + std::string msg = data; + const std::uint64_t bits = static_cast(data.size()) * 8; + msg += static_cast(0x80); + while (msg.size() % 64 != 56) msg += '\0'; + for (int i = 7; i >= 0; --i) msg += static_cast((bits >> (i * 8)) & 0xff); + for (std::size_t off = 0; off < msg.size(); off += 64) { + std::uint32_t w[64]; + for (int i = 0; i < 16; ++i) + w[i] = (static_cast(static_cast(msg[off + i * 4])) << 24) | + (static_cast(static_cast(msg[off + i * 4 + 1])) << 16) | + (static_cast(static_cast(msg[off + i * 4 + 2])) << 8) | + static_cast(static_cast(msg[off + i * 4 + 3])); + for (int i = 16; i < 64; ++i) { + std::uint32_t s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >> 3); + std::uint32_t s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >> 10); + w[i] = w[i - 16] + s0 + w[i - 7] + s1; + } + std::uint32_t a = h[0], b = h[1], c = h[2], d = h[3], e = h[4], f = h[5], + g = h[6], hh = h[7]; + for (int i = 0; i < 64; ++i) { + std::uint32_t s1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25); + std::uint32_t ch = (e & f) ^ (~e & g); + std::uint32_t t1 = hh + s1 + ch + k[i] + w[i]; + std::uint32_t s0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22); + std::uint32_t mj = (a & b) ^ (a & c) ^ (b & c); + std::uint32_t t2 = s0 + mj; + hh = g; g = f; f = e; e = d + t1; d = c; c = b; b = a; a = t1 + t2; + } + h[0] += a; h[1] += b; h[2] += c; h[3] += d; + h[4] += e; h[5] += f; h[6] += g; h[7] += hh; + } + static const char* hex = "0123456789abcdef"; + std::string out; + for (std::uint32_t v : h) + for (int i = 28; i >= 0; i -= 4) out += hex[(v >> i) & 0xf]; + return out; +} + +Response problem_response(int status, const std::string& title, + const std::string& detail, + const std::string& instance) { + std::string body = "{\"type\":\"about:blank\",\"title\":\"" + json_escape(title) + + "\",\"status\":" + std::to_string(status) + + ",\"detail\":\"" + json_escape(detail) + "\""; + if (!instance.empty()) body += ",\"instance\":\"" + json_escape(instance) + "\""; + body += "}"; + return Response(status, body, "application/problem+json"); +} + +// ---- tenancy --------------------------------------------------------------- +bool TenantResolver::valid_id(const std::string& id) { + if (id.size() < 2 || id.size() > 32) return false; + for (char c : id) + if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-')) return false; + return id.front() != '-' && id.back() != '-'; +} + +TenantRef TenantResolver::resolve(const Request& req, + const std::string& path_tenant) const { + TenantRef ref; + std::string host = req.headers.get("Host"); + if (req.headers.has("X-Tenant-ID")) { + ref.id = req.headers.get("X-Tenant-ID"); + ref.source = "header"; + } else if (!path_tenant.empty()) { + ref.id = path_tenant; + ref.source = "path"; + } else if (std::count(host.begin(), host.end(), '.') >= 2) { + ref.id = host.substr(0, host.find('.')); + ref.source = "subdomain"; + } else { + ref.id = kDefaultTenant; + ref.source = "default"; + } + if (!valid_id(ref.id)) ref.error = "tenant id must be 2-32 chars of a-z, 0-9, '-'"; + return ref; +} + +TenantCatalog::TenantCatalog() { + tenants_["demo"] = {"demo", "Demo Inc", "free"}; + tenants_["acme"] = {"acme", "Acme Corp", "pro"}; + tenants_["globex"] = {"globex", "Globex", "free"}; +} + +const Tenant* TenantCatalog::find(const std::string& id) const { + auto it = tenants_.find(id); + return it == tenants_.end() ? nullptr : &it->second; +} + +std::string TenantCatalog::provision(const std::string& id, const std::string& name, + const std::string& plan) { + if (!TenantResolver::valid_id(id)) return "bad id"; + if (tenants_.count(id)) return "exists"; + if (tenants_.size() >= kMaxTenants) return "tenant cap reached"; + tenants_[id] = {id, name, plan}; + return ""; +} + +std::vector TenantCatalog::list() const { + std::vector out; + for (const auto& [k, v] : tenants_) out.push_back(v); + return out; +} + +bool TenantData::add(const std::string& tenant, const std::string& item) { + auto& rows = rows_[tenant]; + if (rows.size() >= kMaxPerTenant) return false; + rows.push_back(item); + return true; +} + +std::vector TenantData::list(const std::string& tenant) const { + auto it = rows_.find(tenant); + return it == rows_.end() ? std::vector{} : it->second; +} + +std::size_t TenantQuota::limit_for(const std::string& plan) { + return plan == "pro" ? 10 : 3; +} + +bool TenantQuota::consume(const std::string& tenant, const std::string& plan) { + auto& u = used_[tenant]; + if (u >= limit_for(plan)) return false; + ++u; + return true; +} + +std::size_t TenantQuota::used(const std::string& tenant) const { + auto it = used_.find(tenant); + return it == used_.end() ? 0 : it->second; +} + +FeatureFlags::FeatureFlags() { + defaults_ = {{"beta-ui", false}, {"export", true}, {"audit-log", true}}; +} + +bool FeatureFlags::known(const std::string& flag) const { + return defaults_.count(flag) > 0; +} + +bool FeatureFlags::enabled(const std::string& tenant, const std::string& flag) const { + auto o = overrides_.find(tenant + "/" + flag); + if (o != overrides_.end()) return o->second; + auto d = defaults_.find(flag); + return d != defaults_.end() && d->second; +} + +bool FeatureFlags::set(const std::string& tenant, const std::string& flag, bool on) { + const std::string key = tenant + "/" + flag; + if (!overrides_.count(key) && overrides_.size() >= kMaxOverrides) return false; + overrides_[key] = on; + return true; +} + +std::map FeatureFlags::effective(const std::string& tenant) const { + std::map out; + for (const auto& [f, v] : defaults_) out[f] = enabled(tenant, f); + return out; +} + +ConfigOverlay::ConfigOverlay() { + base_ = {{"theme", "light"}, {"page_size", "20"}, {"locale", "en"}}; +} + +bool ConfigOverlay::known(const std::string& key) const { return base_.count(key) > 0; } + +bool ConfigOverlay::set(const std::string& tenant, const std::string& key, + const std::string& value) { + std::size_t total = 0; + for (const auto& [t, m] : overlay_) total += m.size(); + if (total >= kMaxOverrides && !overlay_[tenant].count(key)) return false; + overlay_[tenant][key] = value; + return true; +} + +std::map ConfigOverlay::effective(const std::string& tenant) const { + auto out = base_; + auto it = overlay_.find(tenant); + if (it != overlay_.end()) + for (const auto& [k, v] : it->second) out[k] = v; + return out; +} + +void TenantMetrics::inc(const std::string& tenant, const std::string& name) { + ++counters_[tenant][name]; +} + +std::map> TenantMetrics::all() const { + return counters_; +} + +// ---- audit ----------------------------------------------------------------- +std::string AuditChain::hash_of(const AuditRecord& r) { + return sha256_hex(std::to_string(r.index) + "|" + r.actor + "|" + r.action + "|" + + r.subject + "|" + r.detail + "|" + r.prev); +} + +bool AuditChain::append(const std::string& actor, const std::string& action, + const std::string& subject, const std::string& detail) { + if (records_.size() >= kMaxRecords) return false; + AuditRecord r; + r.index = records_.size(); + r.actor = actor; + r.action = action; + r.subject = subject; + r.detail = detail; + r.prev = records_.empty() ? std::string(64, '0') : records_.back().hash; + r.hash = hash_of(r); + records_.push_back(r); + return true; +} + +std::optional AuditChain::verify() const { + std::string prev(64, '0'); + for (const auto& r : records_) { + if (r.prev != prev || hash_of(r) != r.hash) return r.index; + prev = r.hash; + } + return std::nullopt; +} + +bool AuditChain::tamper(std::size_t index) { + if (index >= records_.size()) return false; + records_[index].detail += " (edited)"; + return true; +} + +std::string PiiMasker::mask_email(const std::string& v) { + auto at = v.find('@'); + if (at == std::string::npos || at == 0) return std::string(v.size(), '*'); + return v.substr(0, 1) + std::string(at - 1, '*') + v.substr(at); +} + +std::string PiiMasker::mask_card(const std::string& v) { + std::string digits; + for (char c : v) + if (c >= '0' && c <= '9') digits += c; + if (digits.size() < 4) return std::string(digits.size(), '*'); + return std::string(digits.size() - 4, '*') + digits.substr(digits.size() - 4); +} + +std::string PiiMasker::redact(const std::string& text, std::size_t* hits) { + static const std::vector> patterns = { + {std::regex(R"([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,})"), "[EMAIL]"}, + {std::regex(R"(\b(?:\d[ -]?){13,16}\b)"), "[CARD]"}, + {std::regex(R"(\b\d{3}-\d{2}-\d{4}\b)"), "[SSN]"}, + {std::regex(R"(\b\d{1,3}(?:\.\d{1,3}){3}\b)"), "[IP]"}}; + std::string out = text.substr(0, 2000); + std::size_t n = 0; + for (const auto& [re, tag] : patterns) { + n += static_cast(std::distance( + std::sregex_iterator(out.begin(), out.end(), re), std::sregex_iterator())); + out = std::regex_replace(out, re, tag); + } + if (hits) *hits = n; + return out; +} + +RetentionStore::RetentionStore() { + entries_ = {{"r1", "session", 45, false}, {"r2", "session", 3, false}, + {"r3", "invoice", 400, false}, {"r4", "invoice", 3000, false}, + {"r5", "log", 120, false}}; +} + +int RetentionStore::max_age(const std::string& klass) { + if (klass == "session") return 30; + if (klass == "log") return 90; + return 2555; // invoice: 7 years +} + +bool RetentionStore::set_hold(const std::string& id, bool hold) { + for (auto& e : entries_) + if (e.id == id) { + e.hold = hold; + return true; + } + return false; +} + +std::vector RetentionStore::expired() const { + std::vector out; + for (const auto& e : entries_) + if (!e.hold && e.age_days > max_age(e.klass)) out.push_back(e.id); + return out; +} + +std::size_t RetentionStore::purge() { + auto ids = expired(); + entries_.erase(std::remove_if(entries_.begin(), entries_.end(), + [&](const RetentionEntry& e) { + return std::find(ids.begin(), ids.end(), e.id) != ids.end(); + }), + entries_.end()); + return ids.size(); +} + +SubjectStore::SubjectStore() { + tables_["profiles"] = {{"ada@example.com", "name=Ada"}, {"bob@example.com", "name=Bob"}}; + tables_["orders"] = {{"ada@example.com", "order=1001"}, {"ada@example.com", "order=1002"}, + {"bob@example.com", "order=2001"}}; + tables_["sessions"] = {{"ada@example.com", "ip=[IP]"}}; +} + +std::map> SubjectStore::export_for( + const std::string& subject) const { + std::map> out; + for (const auto& [t, rows] : tables_) + for (const auto& [s, d] : rows) + if (s == subject) out[t].push_back(d); + return out; +} + +std::size_t SubjectStore::erase(const std::string& subject) { + std::size_t n = 0; + for (auto& [t, rows] : tables_) { + auto before = rows.size(); + rows.erase(std::remove_if(rows.begin(), rows.end(), + [&](const auto& r) { return r.first == subject; }), + rows.end()); + n += before - rows.size(); + } + return n; +} + +// ---- governance ------------------------------------------------------------ +std::string ApiKeyStore::issue(const std::string& owner, const std::string& tier, + const std::set& scopes, + std::string* id_out) { + if (keys_.size() >= kMaxKeys) return ""; + ++counter_; + std::string secret = sha256_hex("molo-key|" + owner + "|" + std::to_string(counter_)); + std::string key = "mk_" + secret.substr(0, 32); + ApiKeyInfo info; + info.id = "key" + std::to_string(counter_); + info.owner = owner; + info.tier = tier; + info.scopes = scopes; + info.hash = sha256_hex(key); + keys_.push_back(info); + if (id_out) *id_out = info.id; + return key; +} + +bool ApiKeyStore::revoke(const std::string& id) { + for (auto& k : keys_) + if (k.id == id) { + k.revoked = true; + return true; + } + return false; +} + +KeyResult ApiKeyStore::authenticate(const std::string& key, const std::string& scope, + const ApiKeyInfo** info) const { + if (key.empty()) return KeyResult::Missing; + const std::string h = sha256_hex(key); + for (const auto& k : keys_) { + if (k.hash != h) continue; + if (info) *info = &k; + if (k.revoked) return KeyResult::Revoked; + if (!scope.empty() && !k.scopes.count(scope)) return KeyResult::Forbidden; + return KeyResult::Ok; + } + return KeyResult::Unknown; +} + +TierPolicy TierTable::find(const std::string& tier) { + if (tier == "pro") return {"pro", 10}; + return {"free", 3}; +} + +bool QuotaLedger::charge(const std::string& key_id, const TierPolicy& policy) { + auto& u = used_[key_id]; + if (u < policy.per_min * 100) ++u; // hard cap on the counter itself + return u <= policy.per_min; +} + +std::size_t QuotaLedger::used(const std::string& key_id) const { + auto it = used_.find(key_id); + return it == used_.end() ? 0 : it->second; +} + +void QuotaLedger::headers(const std::string& key_id, const TierPolicy& policy, + Headers& out) const { + std::size_t u = used(key_id); + std::size_t remaining = u >= policy.per_min ? 0 : policy.per_min - u; + out.set("X-Tier", policy.name); + out.set("X-RateLimit-Limit", std::to_string(policy.per_min)); + out.set("X-RateLimit-Remaining", std::to_string(remaining)); + out.set("X-RateLimit-Reset", "60"); + if (u > policy.per_min) out.set("Retry-After", "60"); +} + +VersionTable::VersionTable() { + versions_ = {{"v1", true, "Sat, 31 Dec 2033 23:59:59 GMT", "/v2"}, + {"v2", false, "", ""}}; +} + +const ApiVersion* VersionTable::find(const std::string& name) const { + for (const auto& v : versions_) + if (v.name == name) return &v; + return nullptr; +} + +void VersionTable::annotate(const std::string& name, Headers& out) const { + const ApiVersion* v = find(name); + if (!v) return; + out.set("X-API-Version", v->name); + if (!v->deprecated) return; + out.set("Deprecation", "true"); + if (!v->sunset.empty()) out.set("Sunset", v->sunset); + if (!v->successor.empty()) + out.set("Link", "<" + v->successor + "/items>; rel=\"successor-version\""); +} + +std::vector RouteLinter::lint(const std::vector& paths) { + static const std::vector verbs = {"get", "create", "delete", "update", + "fetch", "remove"}; + std::vector out; + std::size_t n = 0; + for (const auto& p : paths) { + if (++n > kMaxPaths) break; + if (p.size() > 1 && p.back() == '/') out.push_back({p, "no-trailing-slash"}); + for (char c : p) + if (c >= 'A' && c <= 'Z') { + out.push_back({p, "lowercase-only"}); + break; + } + if (p.find('_') != std::string::npos) out.push_back({p, "kebab-case"}); + for (const auto& seg : split(p, '/')) + if (std::find(verbs.begin(), verbs.end(), seg) != verbs.end()) { + out.push_back({p, "no-verbs-in-path"}); + break; + } + } + return out; +} + +namespace { + +struct Op { + std::set required, optional; +}; + +std::map parse_snapshot(const std::string& s) { + std::map ops; + std::size_t lines = 0; + for (const auto& raw : split(s, '\n')) { + std::string line = trim(raw); + if (line.empty()) continue; + if (++lines > SpecDiffer::kMaxLines) break; + std::istringstream in(line); + std::string method, path, tok; + in >> method >> path; + Op op; + while (in >> tok) { + auto eq = tok.find('='); + if (eq == std::string::npos) continue; + auto names = split(tok.substr(eq + 1), ','); + auto& dst = tok.substr(0, eq) == "required" ? op.required : op.optional; + for (auto& n : names) + if (!n.empty()) dst.insert(n); + } + ops[method + " " + path] = op; + } + return ops; +} + +} // namespace + +std::vector SpecDiffer::diff(const std::string& before, + const std::string& after) { + auto a = parse_snapshot(before); + auto b = parse_snapshot(after); + std::vector out; + for (const auto& [name, op] : a) { + auto it = b.find(name); + if (it == b.end()) { + out.push_back({"breaking", name, "operation removed"}); + continue; + } + for (const auto& r : it->second.required) + if (!op.required.count(r)) + out.push_back({"breaking", name, "new required parameter " + r}); + for (const auto& r : op.required) + if (!it->second.required.count(r) && !it->second.optional.count(r)) + out.push_back({"breaking", name, "parameter removed " + r}); + } + for (const auto& [name, op] : b) + if (!a.count(name)) out.push_back({"compatible", name, "operation added"}); + return out; +} + +} // namespace molo::example::fw diff --git a/example/fw/Enterprise.hpp b/example/fw/Enterprise.hpp new file mode 100644 index 0000000..0246de3 --- /dev/null +++ b/example/fw/Enterprise.hpp @@ -0,0 +1,269 @@ +#pragma once + +// Enterprise framework skeleton for the living example: multi-tenancy, audit +// and compliance, API governance. Every class is bounded by a hard cap. +// Gaps versus the real modules are listed in the report of the owning plan: +// src/tenancy/*, src/audit/*, src/governance/*. + +#include +#include +#include +#include +#include +#include + +#include "Http.hpp" + +namespace molo::example::fw { + +// ---- shared helpers ------------------------------------------------------- +std::string sha256_hex(const std::string& data); // self-contained SHA-256 + +// RFC 7807 problem+json (mirrors src/governance/problem.h). +Response problem_response(int status, const std::string& title, + const std::string& detail, + const std::string& instance = ""); + +// ---- multi-tenancy (src/tenancy) ----------------------------------------- +struct TenantRef { + std::string id; + std::string source; // header, path, subdomain, default + std::string error; // non-empty when the id is malformed +}; + +class TenantResolver { + public: + static constexpr const char* kDefaultTenant = "demo"; + static bool valid_id(const std::string& id); + // Precedence: header X-Tenant-ID, then path param, then Host subdomain. + TenantRef resolve(const Request& req, const std::string& path_tenant = "") const; +}; + +struct Tenant { + std::string id, name, plan; // plan: free or pro +}; + +class TenantCatalog { + public: + static constexpr std::size_t kMaxTenants = 64; + TenantCatalog(); + const Tenant* find(const std::string& id) const; + // Returns empty on success, else the failure reason. + std::string provision(const std::string& id, const std::string& name, + const std::string& plan); + std::vector list() const; + + private: + std::map tenants_; +}; + +// Data isolation: every access is keyed by tenant id. +class TenantData { + public: + static constexpr std::size_t kMaxPerTenant = 50; + bool add(const std::string& tenant, const std::string& item); + std::vector list(const std::string& tenant) const; + + private: + std::map> rows_; +}; + +class TenantQuota { + public: + static std::size_t limit_for(const std::string& plan); + bool consume(const std::string& tenant, const std::string& plan); + std::size_t used(const std::string& tenant) const; + + private: + std::map used_; +}; + +class FeatureFlags { + public: + static constexpr std::size_t kMaxOverrides = 128; + FeatureFlags(); + bool enabled(const std::string& tenant, const std::string& flag) const; + bool set(const std::string& tenant, const std::string& flag, bool on); + bool known(const std::string& flag) const; + std::map effective(const std::string& tenant) const; + + private: + std::map defaults_; + std::map overrides_; // "tenant/flag" +}; + +class ConfigOverlay { + public: + static constexpr std::size_t kMaxOverrides = 128; + ConfigOverlay(); + bool set(const std::string& tenant, const std::string& key, + const std::string& value); + std::map effective(const std::string& tenant) const; + bool known(const std::string& key) const; + + private: + std::map base_; + std::map> overlay_; +}; + +class TenantMetrics { + public: + void inc(const std::string& tenant, const std::string& name); + std::map> all() const; + + private: + std::map> counters_; +}; + +// ---- audit and compliance (src/audit) ------------------------------------ +struct AuditRecord { + std::size_t index = 0; + std::string actor, action, subject, detail, prev, hash; +}; + +class AuditChain { + public: + static constexpr std::size_t kMaxRecords = 500; + // False when the cap is reached (append-only: never evicts). + bool append(const std::string& actor, const std::string& action, + const std::string& subject, const std::string& detail); + // First index whose hash or link is wrong, or nullopt when intact. + std::optional verify() const; + bool tamper(std::size_t index); // test hook: flips one detail + const std::vector& records() const { return records_; } + + private: + static std::string hash_of(const AuditRecord& r); + std::vector records_; +}; + +class PiiMasker { + public: + static std::string mask_email(const std::string& v); + static std::string mask_card(const std::string& v); + // Redaction patterns over free text: email, card, ssn, ipv4. + static std::string redact(const std::string& text, std::size_t* hits = nullptr); +}; + +struct RetentionEntry { + std::string id, klass; + int age_days = 0; + bool hold = false; +}; + +class RetentionStore { + public: + RetentionStore(); + static int max_age(const std::string& klass); // policy per class + bool set_hold(const std::string& id, bool hold); + // Dry run reports; a real purge removes. Held entries are never touched. + std::vector expired() const; + std::size_t purge(); + const std::vector& entries() const { return entries_; } + + private: + std::vector entries_; +}; + +class SubjectStore { + public: + SubjectStore(); + // table -> list of rows for subject + std::map> export_for( + const std::string& subject) const; + // Cascade erase across all tables, returns rows removed. + std::size_t erase(const std::string& subject); + + private: + std::map>> tables_; +}; + +// ---- API governance (src/governance) ------------------------------------- +struct ApiKeyInfo { + std::string id, owner, tier, hash; + std::set scopes; + bool revoked = false; +}; + +enum class KeyResult { Ok, Missing, Unknown, Revoked, Forbidden }; + +class ApiKeyStore { + public: + static constexpr std::size_t kMaxKeys = 64; + // Returns the plaintext key once; only the hash is stored. Empty at cap. + std::string issue(const std::string& owner, const std::string& tier, + const std::set& scopes, std::string* id_out); + bool revoke(const std::string& id); + KeyResult authenticate(const std::string& key, const std::string& scope, + const ApiKeyInfo** info) const; + std::size_t size() const { return keys_.size(); } + + private: + std::vector keys_; + std::uint64_t counter_ = 0; +}; + +struct TierPolicy { + std::string name; + std::size_t per_min = 0; +}; + +class TierTable { + public: + static TierPolicy find(const std::string& tier); +}; + +class QuotaLedger { + public: + // True when the call is within the tier budget; always counts the call. + bool charge(const std::string& key_id, const TierPolicy& policy); + std::size_t used(const std::string& key_id) const; + void headers(const std::string& key_id, const TierPolicy& policy, + Headers& out) const; + + private: + std::map used_; +}; + +struct ApiVersion { + std::string name; + bool deprecated = false; + std::string sunset; // HTTP date + std::string successor; // path prefix +}; + +class VersionTable { + public: + VersionTable(); + const ApiVersion* find(const std::string& name) const; + void annotate(const std::string& name, Headers& out) const; + const std::vector& all() const { return versions_; } + + private: + std::vector versions_; +}; + +struct LintFinding { + std::string path, rule; +}; + +class RouteLinter { + public: + static constexpr std::size_t kMaxPaths = 100; + static std::vector lint(const std::vector& paths); +}; + +struct DiffFinding { + std::string kind, operation; // kind: breaking or compatible + std::string detail; +}; + +class SpecDiffer { + public: + static constexpr std::size_t kMaxLines = 200; + // Snapshot line: "METHOD /path required=a,b optional=c" + static std::vector diff(const std::string& before, + const std::string& after); +}; + +} // namespace molo::example::fw diff --git a/example/fw/Platform.cpp b/example/fw/Platform.cpp new file mode 100644 index 0000000..3680d61 --- /dev/null +++ b/example/fw/Platform.cpp @@ -0,0 +1,695 @@ +#include "Platform.hpp" + +#include +#include +#include +#include + +namespace molo::example::fw { + +namespace { + +std::string lower(std::string s) { + for (char& c : s) c = (char)std::tolower((unsigned char)c); + return s; +} + +std::string trim(const std::string& s) { + std::size_t a = s.find_first_not_of(" \t"); + if (a == std::string::npos) return ""; + return s.substr(a, s.find_last_not_of(" \t") - a + 1); +} + +std::vector split(const std::string& s, char sep) { + std::vector out; + std::size_t start = 0; + while (true) { + std::size_t at = s.find(sep, start); + out.push_back(trim(s.substr(start, at == std::string::npos ? at : at - start))); + if (at == std::string::npos) break; + start = at + 1; + } + return out; +} + +// "gzip;q=0.5" -> token "gzip", q 0.5. Missing q is 1. +void token_q(const std::string& part, std::string& token, double& q) { + std::size_t semi = part.find(';'); + token = lower(trim(part.substr(0, semi))); + q = 1.0; + if (semi == std::string::npos) return; + std::string rest = lower(trim(part.substr(semi + 1))); + if (rest.rfind("q=", 0) == 0) q = std::atof(rest.c_str() + 2); +} + +bool parse_size(const std::string& s, std::size_t& out) { + if (s.empty() || s.size() > 15) return false; + std::size_t v = 0; + for (char c : s) { + if (c < '0' || c > '9') return false; + v = v * 10 + (std::size_t)(c - '0'); + } + out = v; + return true; +} + +std::string hex_pad(unsigned long long v, std::size_t digits) { + char buf[40]; + std::snprintf(buf, sizeof(buf), "%0*llx", (int)digits, v); + std::string s = buf; + return s.substr(s.size() - digits); +} + + +} // namespace + +// ---- i18n ----------------------------------------------------------------- + +int PluralRule::forms(const std::string& locale) { + if (locale == "ru") return 3; + if (locale == "ja") return 1; + return 2; +} + +int PluralRule::index(const std::string& locale, long n) { + if (n < 0) n = -n; + if (locale == "ja") return 0; + if (locale == "fr") return n < 2 ? 0 : 1; + if (locale == "ru") { + if (n % 10 == 1 && n % 100 != 11) return 0; + if (n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 12 || n % 100 > 14)) return 1; + return 2; + } + return n == 1 ? 0 : 1; +} + +bool MessageCatalog::add(const std::string& locale, const std::string& msgid, + std::vector forms) { + if (!data_.count(locale) && data_.size() >= platform_caps::kMaxLocales) + return false; + auto& msgs = data_[locale]; + if (!msgs.count(msgid) && msgs.size() >= platform_caps::kMaxMessages) + return false; + msgs[msgid] = std::move(forms); + return true; +} + +std::string MessageCatalog::translate(const std::string& locale, + const std::string& msgid, long n) const { + for (const std::string& loc : {locale, default_}) { + auto l = data_.find(loc); + if (l == data_.end()) continue; + auto m = l->second.find(msgid); + if (m == l->second.end() || m->second.empty()) continue; + std::size_t idx = (std::size_t)PluralRule::index(loc, n); + const std::string& form = m->second[std::min(idx, m->second.size() - 1)]; + std::string out = form; + std::size_t at = out.find("%d"); + if (at != std::string::npos) out.replace(at, 2, std::to_string(n)); + return out; + } + return msgid; +} + +std::vector MessageCatalog::locales() const { + std::vector out; + for (const auto& [k, v] : data_) out.push_back(k); + return out; +} + +bool MessageCatalog::has_locale(const std::string& locale) const { + return data_.count(locale) > 0; +} + +std::string MessageCatalog::to_po(const std::string& locale) const { + auto l = data_.find(locale); + std::string out = "msgid \"\"\nmsgstr \"\"\n\"Language: " + locale + + "\\n\"\n\"Plural-Forms: nplurals=" + + std::to_string(PluralRule::forms(locale)) + ";\\n\"\n"; + if (l == data_.end()) return out; + for (const auto& [id, forms] : l->second) { + out += "\nmsgid \"" + id + "\"\n"; + if (forms.size() == 1) { + out += "msgstr \"" + forms[0] + "\"\n"; + } else { + out += "msgid_plural \"" + id + "\"\n"; + for (std::size_t i = 0; i < forms.size(); ++i) + out += "msgstr[" + std::to_string(i) + "] \"" + forms[i] + "\"\n"; + } + } + return out; +} + +void MessageCatalog::set_default(std::string locale) { + default_ = std::move(locale); +} +const std::string& MessageCatalog::default_locale() const { return default_; } + +std::string negotiate_locale(const std::string& lang_param, + const std::string& accept_language, + const MessageCatalog& catalog) { + std::string forced = lower(lang_param); + if (catalog.has_locale(forced)) return forced; + std::string best = catalog.default_locale(); + double best_q = 0; + for (const std::string& part : split(accept_language, ',')) { + if (part.empty()) continue; + std::string tag; + double q; + token_q(part, tag, q); + std::string primary = tag.substr(0, tag.find('-')); + std::string pick = catalog.has_locale(tag) ? tag + : catalog.has_locale(primary) ? primary + : ""; + if (!pick.empty() && q > best_q) { + best = pick; + best_q = q; + } + } + return best; +} + +// ---- compression ---------------------------------------------------------- + +std::uint32_t crc32(const std::string& data) { + std::uint32_t crc = 0xFFFFFFFFu; + for (unsigned char c : data) { + crc ^= c; + for (int k = 0; k < 8; ++k) crc = (crc >> 1) ^ (0xEDB88320u & (0u - (crc & 1u))); + } + return ~crc; +} + +namespace { +void put_le32(std::string& out, std::uint32_t v) { + for (int i = 0; i < 4; ++i) out.push_back((char)((v >> (8 * i)) & 0xFF)); +} +} // namespace + +std::string gzip_stored(const std::string& data) { + std::string out("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff", 10); + std::size_t pos = 0; + do { + std::size_t n = std::min(65535, data.size() - pos); + bool last = pos + n >= data.size(); + out.push_back(last ? 1 : 0); + out.push_back((char)(n & 0xFF)); + out.push_back((char)(n >> 8)); + out.push_back((char)(~n & 0xFF)); + out.push_back((char)((~n >> 8) & 0xFF)); + out.append(data, pos, n); + pos += n; + } while (pos < data.size()); + put_le32(out, crc32(data)); + put_le32(out, (std::uint32_t)data.size()); + return out; +} + +bool accepts_encoding(const std::string& accept_encoding, + const std::string& coding) { + bool star = false; + for (const std::string& part : split(accept_encoding, ',')) { + std::string tag; + double q; + token_q(part, tag, q); + if (tag == coding) return q > 0; + if (tag == "*" && q > 0) star = true; + } + return star; +} + +// ---- conditional GET and ranges ------------------------------------------- + +std::string make_etag(const std::string& body) { + std::uint64_t h = 1469598103934665603ull; + for (unsigned char c : body) { + h ^= c; + h *= 1099511628211ull; + } + return "\"" + hex_pad(h, 16) + "-" + std::to_string(body.size()) + "\""; +} + +bool etag_matches(const std::string& header, const std::string& etag) { + if (trim(header) == "*") return true; + for (std::string tag : split(header, ',')) { + if (tag.rfind("W/", 0) == 0) tag = tag.substr(2); + if (!tag.empty() && tag == etag) return true; + } + return false; +} + +ByteRange parse_range(const std::string& header, std::size_t size) { + ByteRange r; + if (header.rfind("bytes=", 0) != 0) return r; + std::string spec = trim(header.substr(6)); + if (spec.find(',') != std::string::npos) return r; + std::size_t dash = spec.find('-'); + if (dash == std::string::npos) return r; + std::string a = spec.substr(0, dash), b = spec.substr(dash + 1); + std::size_t first = 0, last = 0; + if (a.empty()) { + std::size_t n; + if (!parse_size(b, n)) return r; + r.kind = (n == 0 || size == 0) ? ByteRange::Kind::Unsatisfiable + : ByteRange::Kind::Ok; + if (r.kind == ByteRange::Kind::Ok) { + r.first = n >= size ? 0 : size - n; + r.last = size - 1; + } + return r; + } + if (!parse_size(a, first)) return r; + if (b.empty()) { + last = size ? size - 1 : 0; + } else if (!parse_size(b, last)) { + return r; + } + if (first >= size || first > last) { + r.kind = ByteRange::Kind::Unsatisfiable; + return r; + } + r.kind = ByteRange::Kind::Ok; + r.first = first; + r.last = std::min(last, size - 1); + return r; +} + +// ---- health probes -------------------------------------------------------- + +bool HealthRegistry::add_check(const std::string& name, bool up) { + std::lock_guard lock(mu_); + if (!checks_.count(name) && checks_.size() >= platform_caps::kMaxChecks) + return false; + checks_[name] = up; + return true; +} + +bool HealthRegistry::set_check(const std::string& name, bool up) { + std::lock_guard lock(mu_); + auto it = checks_.find(name); + if (it == checks_.end()) return false; + it->second = up; + return true; +} + +bool HealthRegistry::add_startup_task(const std::string& name, bool done) { + std::lock_guard lock(mu_); + if (!tasks_.count(name) && tasks_.size() >= platform_caps::kMaxChecks) + return false; + tasks_[name] = done; + return true; +} + +bool HealthRegistry::set_startup_task(const std::string& name, bool done) { + std::lock_guard lock(mu_); + auto it = tasks_.find(name); + if (it == tasks_.end()) return false; + it->second = done; + return true; +} + +bool HealthRegistry::startup_complete() const { + std::lock_guard lock(mu_); + for (const auto& [k, v] : tasks_) + if (!v) return false; + return true; +} + +bool HealthRegistry::ready() const { + if (!startup_complete()) return false; + std::lock_guard lock(mu_); + for (const auto& [k, v] : checks_) + if (!v) return false; + return true; +} + +namespace { +std::string bool_map_json(const std::map& m, + const char* up, const char* down) { + std::string out = "{"; + bool first = true; + for (const auto& [k, v] : m) { + out += std::string(first ? "" : ",") + "\"" + json_escape(k) + "\":\"" + + (v ? up : down) + "\""; + first = false; + } + return out + "}"; +} +} // namespace + +std::string HealthRegistry::liveness_json() const { + long up = (long)std::chrono::duration_cast( + std::chrono::steady_clock::now() - started_) + .count(); + return "{\"status\":\"alive\",\"uptime_seconds\":" + std::to_string(up) + "}"; +} + +std::string HealthRegistry::readiness_json() const { + bool ok = ready(); + std::lock_guard lock(mu_); + return std::string("{\"status\":\"") + (ok ? "ready" : "not_ready") + + "\",\"checks\":" + bool_map_json(checks_, "up", "down") + "}"; +} + +std::string HealthRegistry::startup_json() const { + bool ok = startup_complete(); + std::lock_guard lock(mu_); + return std::string("{\"status\":\"") + (ok ? "started" : "starting") + + "\",\"tasks\":" + bool_map_json(tasks_, "done", "pending") + "}"; +} + +// ---- Prometheus ----------------------------------------------------------- + +PrometheusExporter::PrometheusExporter(const Metrics* metrics, + const Stats* stats) + : metrics_(metrics), stats_(stats) {} + +std::string PrometheusExporter::sanitize(const std::string& name) { + std::string out; + for (char c : name) { + bool ok = std::isalnum((unsigned char)c) || c == '_' || c == ':'; + out.push_back(ok ? c : '_'); + } + if (out.empty() || std::isdigit((unsigned char)out[0])) out.insert(0, "_"); + return out; +} + +std::string PrometheusExporter::render() const { + std::string out; + auto sample = [&out](const std::string& name, const char* type, + const std::string& help, const std::string& value) { + out += "# HELP " + name + " " + help + "\n# TYPE " + name + " " + type + + "\n" + name + " " + value + "\n"; + }; + sample("molo_http_requests_total", "counter", "Requests served.", + std::to_string(stats_->total_requests())); + sample("molo_http_connections_total", "counter", "Connections accepted.", + std::to_string(stats_->total_connections())); + sample("molo_http_connections_active", "gauge", "Open connections.", + std::to_string(stats_->active_connections())); + for (const auto& [k, v] : metrics_->snapshot()) { + char buf[64]; + std::snprintf(buf, sizeof(buf), "%g", v); + sample("molo_example_" + sanitize(k), "gauge", + "Example metric " + k + ".", buf); + } + return out; +} + +// ---- structured logging --------------------------------------------------- + +bool parse_level(const std::string& name, LogLevel& out) { + std::string n = lower(name); + if (n == "debug") out = LogLevel::Debug; + else if (n == "info") out = LogLevel::Info; + else if (n == "warn") out = LogLevel::Warn; + else if (n == "error") out = LogLevel::Error; + else return false; + return true; +} + +std::string level_name(LogLevel level) { + switch (level) { + case LogLevel::Debug: return "debug"; + case LogLevel::Info: return "info"; + case LogLevel::Warn: return "warn"; + case LogLevel::Error: return "error"; + } + return "info"; +} + +std::string LogRing::redact(const std::string& message) { + static const char* keys[] = {"password", "passwd", "token", "secret", + "authorization", "api_key", "apikey"}; + std::string out = message; + std::string low = lower(out); + for (const char* key : keys) { + std::string k = key; + std::size_t from = 0; + while ((from = low.find(k, from)) != std::string::npos) { + std::size_t p = from + k.size(); + from = p; + if (p >= out.size() || (out[p] != '=' && out[p] != ':')) continue; + ++p; + while (p < out.size() && out[p] == ' ') ++p; + std::size_t e = p; + while (e < out.size() && !std::isspace((unsigned char)out[e]) && + out[e] != ',' && out[e] != ';' && out[e] != '&') + ++e; + out.replace(p, e - p, "***"); + low = lower(out); + from = p; + } + } + return out; +} + +void LogRing::log(LogLevel level, std::string message, + std::string correlation_id) { + std::lock_guard lock(mu_); + if (level < min_) { + ++filtered_; + return; + } + LogRecord r; + r.seq = next_seq_++; + r.ts = (long)std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) + .count(); + r.level = level; + if (message.size() > platform_caps::kLogMessage) + message.resize(platform_caps::kLogMessage); + r.message = redact(message); + r.correlation_id = std::move(correlation_id); + ring_.push_back(std::move(r)); + while (ring_.size() > platform_caps::kLogRing) ring_.pop_front(); +} + +std::vector LogRing::last(std::size_t n, LogLevel min) const { + std::lock_guard lock(mu_); + std::vector out; + for (auto it = ring_.rbegin(); it != ring_.rend() && out.size() < n; ++it) + if (it->level >= min) out.push_back(*it); + return out; +} + +void LogRing::set_min_level(LogLevel level) { + std::lock_guard lock(mu_); + min_ = level; +} +LogLevel LogRing::min_level() const { + std::lock_guard lock(mu_); + return min_; +} +std::size_t LogRing::size() const { + std::lock_guard lock(mu_); + return ring_.size(); +} +unsigned long LogRing::dropped_below_min() const { + std::lock_guard lock(mu_); + return filtered_; +} + +std::string LogRing::to_json(const LogRecord& r) { + return "{\"seq\":" + std::to_string(r.seq) + ",\"ts\":" + + std::to_string(r.ts) + ",\"level\":\"" + level_name(r.level) + + "\",\"msg\":\"" + json_escape(r.message) + "\",\"correlation_id\":\"" + + json_escape(r.correlation_id) + "\"}"; +} + +// ---- trace context -------------------------------------------------------- + +std::string TraceContext::traceparent() const { + return "00-" + trace_id + "-" + span_id + "-" + flags; +} + +std::string TraceContextFactory::next_hex(std::size_t digits) { + unsigned long long v = ++counter_; + v *= 0x9E3779B97F4A7C15ull; + std::string out = hex_pad(v, 16); + while (out.size() < digits) out += hex_pad(v * (out.size() + 7), 16); + return out.substr(0, digits); +} + +TraceContext TraceContextFactory::from_header(const std::string& header) { + std::lock_guard lock(mu_); + TraceContext ctx; + ctx.had_parent = !header.empty(); + auto parts = split(header, '-'); + auto is_hex = [](const std::string& s) { + return !s.empty() && std::all_of(s.begin(), s.end(), [](char c) { + return std::isxdigit((unsigned char)c) && !std::isupper((unsigned char)c); + }); + }; + bool ok = parts.size() == 4 && parts[0].size() == 2 && is_hex(parts[0]) && + parts[0] != "ff" && parts[1].size() == 32 && is_hex(parts[1]) && + parts[1] != std::string(32, '0') && parts[2].size() == 16 && + is_hex(parts[2]) && parts[2] != std::string(16, '0') && + parts[3].size() == 2 && is_hex(parts[3]); + if (ok) { + ctx.trace_id = parts[1]; + ctx.parent_span_id = parts[2]; + ctx.flags = parts[3]; + } else { + ctx.parent_valid = !ctx.had_parent; + ctx.trace_id = next_hex(32); + } + ctx.span_id = next_hex(16); + return ctx; +} + +// ---- DI registry ---------------------------------------------------------- + +std::string lifetime_name(Lifetime l) { + switch (l) { + case Lifetime::Singleton: return "singleton"; + case Lifetime::Scoped: return "scoped"; + case Lifetime::Transient: return "transient"; + } + return "singleton"; +} + +bool ServiceRegistry::add(const std::string& name, Lifetime lifetime, + std::function factory) { + std::lock_guard lock(mu_); + if (!slots_.count(name) && slots_.size() >= platform_caps::kMaxServices) + return false; + Slot s; + s.lifetime = lifetime; + s.factory = std::move(factory); + slots_[name] = std::move(s); + return true; +} + +bool ServiceRegistry::resolve(const std::string& name, const std::string& scope, + std::string& out) { + std::lock_guard lock(mu_); + auto it = slots_.find(name); + if (it == slots_.end()) return false; + Slot& s = it->second; + ++s.resolved; + if (s.lifetime == Lifetime::Singleton) { + if (!s.built) { + s.instance = s.factory(); + s.built = true; + ++s.created; + } + out = s.instance; + } else if (s.lifetime == Lifetime::Scoped) { + auto sc = s.scoped.find(scope); + if (sc == s.scoped.end()) { + if (s.scoped.size() >= 16) s.scoped.clear(); + sc = s.scoped.emplace(scope, s.factory()).first; + ++s.created; + } + out = sc->second; + } else { + out = s.factory(); + ++s.created; + } + return true; +} + +std::vector ServiceRegistry::list() const { + std::lock_guard lock(mu_); + std::vector out; + for (const auto& [k, s] : slots_) + out.push_back({k, s.lifetime, s.created, s.resolved}); + return out; +} + +// ---- flash messages ------------------------------------------------------- + +std::string FlashCodec::encode(const std::string& message) { + std::string m = message.substr(0, platform_caps::kFlashBytes); + std::string out; + for (unsigned char c : m) out += hex_pad(c, 2); + return out; +} + +bool FlashCodec::decode(const std::string& cookie, std::string& message) { + if (cookie.empty() || cookie.size() % 2 || cookie.size() > platform_caps::kFlashBytes * 2) + return false; + std::string out; + for (std::size_t i = 0; i < cookie.size(); i += 2) { + if (!std::isxdigit((unsigned char)cookie[i]) || + !std::isxdigit((unsigned char)cookie[i + 1])) + return false; + out.push_back((char)std::stoi(cookie.substr(i, 2), nullptr, 16)); + } + message = out; + return true; +} + +// ---- expose stats --------------------------------------------------------- + +namespace { +constexpr std::size_t kBuckets = + (std::size_t)(platform_caps::kExposeWindowSec / platform_caps::kExposeBucketSec); +} + +ExposeStats::ExposeStats(long (*now_sec)()) : clock_(now_sec) {} + +void ExposeStats::set_clock(long (*now_sec)()) { clock_ = now_sec; } + +long ExposeStats::now() const { + if (clock_) return clock_(); + return (long)std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) + .count(); +} + +bool ExposeStats::known_method(const std::string& m) { + static const char* names[] = {"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"}; + for (const char* n : names) + if (m == n) return true; + return false; +} + +bool ExposeStats::record(const std::string& method, unsigned long up, + unsigned long down, unsigned long micros) { + if (!known_method(method)) return false; + std::lock_guard lock(mu_); + ExposeRow& row = rows_[method]; + ++row.count; + row.bytes_up += up; + row.bytes_down += down; + row.micros += micros; + auto& win = windows_[method]; + if (win.empty()) win.resize(kBuckets); + long slot = now() / platform_caps::kExposeBucketSec; + Bucket& b = win[(std::size_t)slot % kBuckets]; + if (b.slot != slot) b = Bucket{slot, 0, 0}; + b.up += up; + b.down += down; + return true; +} + +std::map ExposeStats::snapshot() const { + std::lock_guard lock(mu_); + std::map out; + ExposeRow total; + long cur = now() / platform_caps::kExposeBucketSec; + for (const auto& [m, row] : rows_) { + ExposeRow r = row; + unsigned long up = 0, down = 0; + for (const Bucket& b : windows_.at(m)) + if (b.slot > cur - (long)kBuckets && b.slot <= cur) { + up += b.up; + down += b.down; + } + r.kb_s_up = up / 1024.0 / platform_caps::kExposeWindowSec; + r.kb_s_down = down / 1024.0 / platform_caps::kExposeWindowSec; + total.count += r.count; + total.bytes_up += r.bytes_up; + total.bytes_down += r.bytes_down; + total.micros += r.micros; + total.kb_s_up += r.kb_s_up; + total.kb_s_down += r.kb_s_down; + out[m] = r; + } + out["_total"] = total; + return out; +} + +} // namespace molo::example::fw diff --git a/example/fw/Platform.hpp b/example/fw/Platform.hpp new file mode 100644 index 0000000..84ca3d5 --- /dev/null +++ b/example/fw/Platform.hpp @@ -0,0 +1,272 @@ +#pragma once + +// Platform framework skeleton for the living example (namespace molo::example::fw): +// i18n, gzip negotiation, ETag and Range, health probes, Prometheus export, +// structured log ring, W3C trace context, DI registry, flash cookies, +// expose stats. Every store has a hard cap; past a cap input is rejected or +// the oldest entry is dropped, never grown. +// Gap: the real modules are src/cli/i18n, src/runtime/compress, +// src/runtime/static/etag.*, src/cli/ops, src/observability, src/app/di, +// src/app/views/flash.h and src/realtime/expose/stats.*. + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "App.hpp" +#include "Http.hpp" +#include "Observ.hpp" +#include "Stats.hpp" + +namespace molo::example::fw { + +namespace platform_caps { +inline constexpr std::size_t kMaxLocales = 8; +inline constexpr std::size_t kMaxMessages = 64; +inline constexpr long kMaxPluralN = 1000000; +inline constexpr std::size_t kMaxCompressBody = 65536; +inline constexpr std::size_t kCompressMin = 256; +inline constexpr std::size_t kMaxDocBytes = 4096; +inline constexpr std::size_t kMaxChecks = 32; +inline constexpr std::size_t kLogRing = 200; +inline constexpr std::size_t kLogMessage = 500; +inline constexpr std::size_t kMaxServices = 64; +inline constexpr std::size_t kFlashBytes = 200; +inline constexpr std::size_t kExposeMethods = 16; +inline constexpr long kExposeWindowSec = 60; +inline constexpr long kExposeBucketSec = 10; +} // namespace platform_caps + +// ---- i18n ----------------------------------------------------------------- + +class PluralRule { + public: + // Form index for count n in locale (en/nl/de: 2 forms, fr: 2, ru: 3, ja: 1). + static int index(const std::string& locale, long n); + static int forms(const std::string& locale); +}; + +class MessageCatalog { + public: + bool add(const std::string& locale, const std::string& msgid, + std::vector forms); + // Plural-aware lookup; falls back to the default locale, then the msgid. + std::string translate(const std::string& locale, const std::string& msgid, + long n = 1) const; + std::vector locales() const; + bool has_locale(const std::string& locale) const; + // gettext .po style dump of one locale. + std::string to_po(const std::string& locale) const; + void set_default(std::string locale); + const std::string& default_locale() const; + + private: + std::map>> data_; + std::string default_ = "en"; +}; + +// ?lang beats Accept-Language (q-weighted) beats the default. +std::string negotiate_locale(const std::string& lang_param, + const std::string& accept_language, + const MessageCatalog& catalog); + +// ---- compression ---------------------------------------------------------- + +std::uint32_t crc32(const std::string& data); +// Valid gzip member using stored deflate blocks (same approach as +// src/cli/embed/gzip_stored.cpp). Gap: no real LZ77 (src/runtime/compress/gzip.cpp). +std::string gzip_stored(const std::string& data); +bool accepts_encoding(const std::string& accept_encoding, + const std::string& coding); + +// ---- conditional GET and ranges ------------------------------------------- + +std::string make_etag(const std::string& body); +// True when If-None-Match (list, weak tags, or *) matches etag. +bool etag_matches(const std::string& header, const std::string& etag); + +struct ByteRange { + enum class Kind { None, Ok, Unsatisfiable } kind = Kind::None; + std::size_t first = 0; + std::size_t last = 0; // inclusive +}; +// Single range only: "a-b", "a-", "-n". Multi-range is ignored (Kind::None). +ByteRange parse_range(const std::string& header, std::size_t size); + +// ---- health probes -------------------------------------------------------- + +class HealthRegistry { + public: + bool add_check(const std::string& name, bool up); + bool set_check(const std::string& name, bool up); + bool add_startup_task(const std::string& name, bool done); + bool set_startup_task(const std::string& name, bool done); + bool startup_complete() const; + bool ready() const; + std::string liveness_json() const; + std::string readiness_json() const; + std::string startup_json() const; + + private: + mutable std::mutex mu_; + std::map checks_; + std::map tasks_; + std::chrono::steady_clock::time_point started_ = + std::chrono::steady_clock::now(); +}; + +// ---- Prometheus ----------------------------------------------------------- + +class PrometheusExporter { + public: + PrometheusExporter(const Metrics* metrics, const Stats* stats); + // text exposition format 0.0.4: HELP, TYPE, samples. + std::string render() const; + static std::string sanitize(const std::string& name); + + private: + const Metrics* metrics_; + const Stats* stats_; +}; + +// ---- structured logging --------------------------------------------------- + +enum class LogLevel { Debug = 0, Info = 1, Warn = 2, Error = 3 }; +bool parse_level(const std::string& name, LogLevel& out); +std::string level_name(LogLevel level); + +struct LogRecord { + unsigned long seq = 0; + long ts = 0; + LogLevel level = LogLevel::Info; + std::string message; + std::string correlation_id; +}; + +class LogRing { + public: + // Redacts secret=value style fields, caps the message, drops the oldest. + void log(LogLevel level, std::string message, std::string correlation_id); + std::vector last(std::size_t n, LogLevel min) const; + void set_min_level(LogLevel level); + LogLevel min_level() const; + std::size_t size() const; + unsigned long dropped_below_min() const; + static std::string redact(const std::string& message); + static std::string to_json(const LogRecord& record); + + private: + mutable std::mutex mu_; + std::deque ring_; + LogLevel min_ = LogLevel::Debug; + unsigned long next_seq_ = 1; + unsigned long filtered_ = 0; +}; + +// ---- trace context -------------------------------------------------------- + +struct TraceContext { + bool had_parent = false; + bool parent_valid = true; + std::string trace_id; + std::string parent_span_id; + std::string span_id; + std::string flags = "01"; + std::string traceparent() const; +}; + +class TraceContextFactory { + public: + // Continues a valid W3C traceparent, else starts a new trace. + TraceContext from_header(const std::string& traceparent); + + private: + std::string next_hex(std::size_t digits); + std::mutex mu_; + unsigned long long counter_ = 0x1000; +}; + +// ---- DI registry ---------------------------------------------------------- + +enum class Lifetime { Singleton, Scoped, Transient }; +std::string lifetime_name(Lifetime lifetime); + +class ServiceRegistry { + public: + bool add(const std::string& name, Lifetime lifetime, + std::function factory); + // Singleton built once, scoped once per scope key (max 16 kept), transient + // on every call. + bool resolve(const std::string& name, const std::string& scope, + std::string& out); + struct Entry { + std::string name; + Lifetime lifetime; + unsigned long created; + unsigned long resolved; + }; + std::vector list() const; + + private: + struct Slot { + Lifetime lifetime; + std::function factory; + std::string instance; + std::map scoped; + bool built = false; + unsigned long created = 0; + unsigned long resolved = 0; + }; + mutable std::mutex mu_; + std::map slots_; +}; + +// ---- flash messages ------------------------------------------------------- + +class FlashCodec { + public: + static std::string encode(const std::string& message); + static bool decode(const std::string& cookie, std::string& message); +}; + +// ---- expose stats --------------------------------------------------------- + +struct ExposeRow { + unsigned long count = 0; + unsigned long bytes_up = 0; + unsigned long bytes_down = 0; + unsigned long micros = 0; + double kb_s_up = 0; + double kb_s_down = 0; + double avg_ms() const { return count ? micros / 1000.0 / count : 0.0; } +}; + +class ExposeStats { + public: + explicit ExposeStats(long (*now_sec)() = nullptr); + bool record(const std::string& method, unsigned long up, + unsigned long down, unsigned long micros); + // Rows by method plus "_total"; windowed kB/s over the last 60 s. + std::map snapshot() const; + void set_clock(long (*now_sec)()); + static bool known_method(const std::string& method); + + private: + struct Bucket { + long slot = -1; + unsigned long up = 0; + unsigned long down = 0; + }; + long now() const; + mutable std::mutex mu_; + long (*clock_)() = nullptr; + std::map rows_; + std::map> windows_; +}; + +} // namespace molo::example::fw diff --git a/example/fw/Resilience.cpp b/example/fw/Resilience.cpp new file mode 100644 index 0000000..0d0d4ad --- /dev/null +++ b/example/fw/Resilience.cpp @@ -0,0 +1,1026 @@ +#include "Resilience.hpp" + +#include +#include +#include +#include + +#include "Http.hpp" + +namespace molo::example::fw { + +using Guard = std::lock_guard; + +void ManualClock::advance(long ms) { + if (ms < 0) ms = 0; + if (ms > labcaps::kMaxAdvanceMs) ms = labcaps::kMaxAdvanceMs; + now_ += ms; +} + +std::string fnv_hex(const std::string& data) { + unsigned long long h = 1469598103934665603ULL; + for (unsigned char c : data) { + h ^= c; + h *= 1099511628211ULL; + } + static const char* d = "0123456789abcdef"; + std::string out(16, '0'); + for (int i = 15; i >= 0; --i) { + out[(std::size_t)i] = d[h & 0xF]; + h >>= 4; + } + return out; +} + +std::string base64_encode(const std::string& in) { + static const char* t = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + std::string out; + std::size_t i = 0; + while (i < in.size()) { + unsigned v = (unsigned char)in[i] << 16; + std::size_t n = in.size() - i; + if (n > 1) v |= (unsigned char)in[i + 1] << 8; + if (n > 2) v |= (unsigned char)in[i + 2]; + out += t[(v >> 18) & 63]; + out += t[(v >> 12) & 63]; + out += n > 1 ? t[(v >> 6) & 63] : '='; + out += n > 2 ? t[v & 63] : '='; + i += 3; + } + return out; +} + +// ---- Lab ------------------------------------------------------------------- + +void Lab::note(std::string line) { + log_.push_back(std::move(line)); + while (log_.size() > labcaps::kLogLines) log_.pop_front(); +} + +std::vector Lab::log() const { + Guard g(mu_); + return {log_.begin(), log_.end()}; +} + +std::string Lab::json() const { + Guard g(mu_); + std::string out = "{\"name\":\"" + json_escape(name()) + "\",\"state\":{"; + bool first = true; + for (const auto& [k, v] : rows()) { + out += std::string(first ? "" : ",") + "\"" + json_escape(k) + "\":\"" + + json_escape(v) + "\""; + first = false; + } + out += "},\"log\":["; + first = true; + for (const auto& l : log_) { + out += std::string(first ? "" : ",") + "\"" + json_escape(l) + "\""; + first = false; + } + return out + "]}"; +} + +// ---- CircuitBreaker -------------------------------------------------------- + +std::string breaker_state_name(CircuitBreaker::State s) { + switch (s) { + case CircuitBreaker::State::Closed: return "closed"; + case CircuitBreaker::State::Open: return "open"; + case CircuitBreaker::State::HalfOpen: return "half-open"; + } + return "?"; +} + +CircuitBreaker::CircuitBreaker(std::shared_ptr clock, + int threshold, long open_ms) + : clock_(std::move(clock)), threshold_(threshold), open_ms_(open_ms) {} + +CircuitBreaker::State CircuitBreaker::effective() const { + if (state_ == State::Open && clock_->now() - opened_at_ >= open_ms_) + return State::HalfOpen; + return state_; +} + +CircuitBreaker::State CircuitBreaker::state() const { + Guard g(mu_); + return effective(); +} + +bool CircuitBreaker::call(bool success) { + Guard g(mu_); + State before = effective(); + if (before == State::Open) { + ++rejected_; + note("call rejected: breaker open"); + return false; + } + state_ = before; + if (success) { + failures_ = 0; + state_ = State::Closed; + } else if (before == State::HalfOpen || ++failures_ >= threshold_) { + state_ = State::Open; + opened_at_ = clock_->now(); + failures_ = 0; + } + note(std::string(success ? "success" : "failure") + ": " + + breaker_state_name(before) + " -> " + breaker_state_name(state_)); + return true; +} + +void CircuitBreaker::tick(long ms) { + Guard g(mu_); + State before = effective(); + clock_->advance(ms); + note("clock +" + std::to_string(ms) + "ms"); + if (before != effective()) + note(breaker_state_name(before) + " -> " + breaker_state_name(effective())); +} + +LabRows CircuitBreaker::rows() const { + Guard g(mu_); + return {{"state", breaker_state_name(effective())}, + {"failures", std::to_string(failures_) + "/" + + std::to_string(threshold_)}, + {"rejected", std::to_string(rejected_)}, + {"clock_ms", std::to_string(clock_->now())}, + {"reopens_after_ms", std::to_string(open_ms_)}}; +} + +// ---- Bulkhead -------------------------------------------------------------- + +Bulkhead::Bulkhead(int a, int q) : max_active_(a), max_queue_(q) {} + +Bulkhead::Admission Bulkhead::acquire() { + Guard g(mu_); + if (active_ < max_active_) { + ++active_; + note("admitted"); + return Admission::Admitted; + } + if (queued_ < max_queue_) { + ++queued_; + note("queued"); + return Admission::Queued; + } + ++rejected_; + note("rejected: compartment full"); + return Admission::Rejected; +} + +bool Bulkhead::release() { + Guard g(mu_); + if (active_ == 0) return false; + if (queued_ > 0) { + --queued_; + note("released, queued call promoted"); + } else { + --active_; + note("released"); + } + return true; +} + +LabRows Bulkhead::rows() const { + Guard g(mu_); + return {{"active", std::to_string(active_) + "/" + std::to_string(max_active_)}, + {"queued", std::to_string(queued_) + "/" + std::to_string(max_queue_)}, + {"rejected", std::to_string(rejected_)}}; +} + +// ---- Shedder --------------------------------------------------------------- + +bool Shedder::enter(bool high) { + Guard g(mu_); + int cap = high ? limit_ : limit_ * 2 / 3; + if (inflight_ >= cap) { + (high ? shed_high_ : shed_low_)++; + note(std::string("shed ") + (high ? "high" : "low") + " priority"); + return false; + } + ++inflight_; + note(std::string("accepted ") + (high ? "high" : "low") + " priority"); + return true; +} + +bool Shedder::leave() { + Guard g(mu_); + if (inflight_ == 0) return false; + --inflight_; + note("finished"); + return true; +} + +LabRows Shedder::rows() const { + Guard g(mu_); + return {{"inflight", std::to_string(inflight_) + "/" + std::to_string(limit_)}, + {"low_priority_cutoff", std::to_string(limit_ * 2 / 3)}, + {"shed_low", std::to_string(shed_low_)}, + {"shed_high", std::to_string(shed_high_)}}; +} + +// ---- Deadline -------------------------------------------------------------- + +int DeadlineLab::run(long budget_ms, int hops) { + Guard g(mu_); + budget_ms_ = budget_ms; + remaining_ms_ = budget_ms; + hops_ = hops; + expired_hop_ = 0; + ran_ = true; + for (int h = 1; h <= hops; ++h) { + if (remaining_ms_ < kHopCostMs) { + expired_hop_ = h; + note("hop " + std::to_string(h) + ": deadline exceeded, call skipped"); + break; + } + remaining_ms_ -= kHopCostMs; + note("hop " + std::to_string(h) + ": ok, " + + std::to_string(remaining_ms_) + "ms left"); + } + return expired_hop_; +} + +LabRows DeadlineLab::rows() const { + Guard g(mu_); + if (!ran_) return {{"status", "not run"}, {"hop_cost_ms", "30"}}; + return {{"status", expired_hop_ ? "expired" : "completed"}, + {"budget_ms", std::to_string(budget_ms_)}, + {"hops", std::to_string(hops_)}, + {"expired_at_hop", std::to_string(expired_hop_)}, + {"remaining_ms", std::to_string(remaining_ms_)}, + {"hop_cost_ms", "30"}}; +} + +// ---- SloBudget ------------------------------------------------------------- + +void SloBudget::record(bool good) { + Guard g(mu_); + window_.push_back(good); + if (window_.size() > kWindow) window_.pop_front(); + note(good ? "good event" : "bad event"); +} + +void SloBudget::reset() { + Guard g(mu_); + window_.clear(); + note("window reset"); +} + +long SloBudget::budget_remaining() const { + Guard g(mu_); + long bad = std::count(window_.begin(), window_.end(), false); + long allowed = (long)window_.size() * (1000 - target_) / 1000; + return allowed - bad; +} + +LabRows SloBudget::rows() const { + Guard g(mu_); + long bad = std::count(window_.begin(), window_.end(), false); + long left = budget_remaining(); + return {{"target", std::to_string(target_ / 10) + "." + + std::to_string(target_ % 10) + "%"}, + {"events", std::to_string(window_.size()) + "/" + + std::to_string(kWindow)}, + {"bad", std::to_string(bad)}, + {"budget_remaining", std::to_string(left)}, + {"state", bad == 0 ? "healthy" : left >= 0 ? "burning" : "exhausted"}}; +} + +// ---- ChaosLab -------------------------------------------------------------- + +ChaosLab::Result ChaosLab::run(int percent, int max_faults, bool armed) { + Guard g(mu_); + percent = std::clamp(percent, 0, labcaps::kMaxPercent); + max_faults = std::clamp(max_faults, 1, labcaps::kMaxFaults); + Result r; + r.percent = percent; + r.max_faults = max_faults; + r.armed = armed; + int acc = 0; + for (int i = 0; i < kCalls && r.planned < max_faults; ++i) { + acc += percent; + if (acc >= 100) { + acc -= 100; + ++r.planned; + } + } + r.injected = armed ? r.planned : 0; + last_ = r; + ++runs_; + total_injected_ += r.injected; + note(std::string(armed ? "ARMED" : "dry run") + ": " + + std::to_string(r.planned) + " fault(s) planned over " + + std::to_string(kCalls) + " calls, " + std::to_string(r.injected) + + " injected"); + return r; +} + +LabRows ChaosLab::rows() const { + Guard g(mu_); + return {{"mode", last_.armed ? "armed" : "dry run"}, + {"percent", std::to_string(last_.percent) + " (cap " + + std::to_string(labcaps::kMaxPercent) + ")"}, + {"max_faults", std::to_string(last_.max_faults) + " (cap " + + std::to_string(labcaps::kMaxFaults) + ")"}, + {"planned", std::to_string(last_.planned)}, + {"injected", std::to_string(last_.injected)}, + {"runs", std::to_string(runs_)}, + {"total_injected", std::to_string(total_injected_)}}; +} + +// ---- DeployLab ------------------------------------------------------------- + +std::string deploy_phase_name(DeployLab::Phase p) { + switch (p) { + case DeployLab::Phase::Idle: return "idle"; + case DeployLab::Phase::InProgress: return "in progress"; + case DeployLab::Phase::Done: return "done"; + case DeployLab::Phase::RolledBack: return "rolled back"; + } + return "?"; +} + +DeployLab::DeployLab() : fleet_(4, "v1") {} + +DeployLab::Phase DeployLab::phase() const { + Guard g(mu_); + return phase_; +} + +bool DeployLab::start(const std::string& strategy, + const std::string& version) { + Guard g(mu_); + if (phase_ == Phase::InProgress) return false; + previous_ = fleet_; + strategy_ = strategy; + target_ = version; + green_stage_ = 0; + phase_ = Phase::InProgress; + note("start " + strategy + " to " + version); + return true; +} + +bool DeployLab::step() { + Guard g(mu_); + if (phase_ != Phase::InProgress) return false; + if (strategy_ == "rolling") { + for (auto& v : fleet_) { + if (v != target_) { + v = target_; + note("replaced one instance with " + target_); + break; + } + } + if (std::all_of(fleet_.begin(), fleet_.end(), + [&](const std::string& v) { return v == target_; })) { + phase_ = Phase::Done; + note("rolling finished"); + } + } else if (green_stage_ == 0) { + green_stage_ = 1; + note("green fleet " + target_ + " prepared, blue still serves"); + } else { + std::fill(fleet_.begin(), fleet_.end(), target_); + phase_ = Phase::Done; + note("traffic switched to green"); + } + return true; +} + +bool DeployLab::rollback() { + Guard g(mu_); + if (phase_ == Phase::Idle || phase_ == Phase::RolledBack) return false; + fleet_ = previous_; + phase_ = Phase::RolledBack; + note("rolled back to previous fleet"); + return true; +} + +LabRows DeployLab::rows() const { + Guard g(mu_); + std::string fleet; + for (const auto& v : fleet_) fleet += (fleet.empty() ? "" : ",") + v; + return {{"strategy", strategy_}, + {"phase", deploy_phase_name(phase_)}, + {"target", target_.empty() ? "-" : target_}, + {"fleet", fleet}}; +} + +// ---- CoordLab -------------------------------------------------------------- + +CoordLab::CoordLab(std::shared_ptr clock, long ttl_ms) + : clock_(std::move(clock)), ttl_ms_(ttl_ms) {} + +bool CoordLab::known_key(const std::string& key) { + return key == "lease" || key == "lock" || key == "leader"; +} + +CoordLab::Grant CoordLab::acquire(const std::string& key, + const std::string& owner) { + Guard g(mu_); + Slot& s = slots_[key]; + bool live = !s.holder.empty() && s.expires > clock_->now(); + if (live && s.holder != owner) { + note(owner + " denied " + key + ": held by " + s.holder); + return {false, s.token, s.holder}; + } + if (!live) ++s.token; // new term, new fencing token + s.holder = owner; + s.expires = clock_->now() + ttl_ms_; + note(owner + (live ? " renewed " : " acquired ") + key + " token " + + std::to_string(s.token)); + return {true, s.token, owner}; +} + +bool CoordLab::release(const std::string& key, const std::string& owner) { + Guard g(mu_); + auto it = slots_.find(key); + if (it == slots_.end() || it->second.holder != owner) return false; + it->second.holder.clear(); + note(owner + " released " + key); + return true; +} + +bool CoordLab::write(const std::string& key, long token) { + Guard g(mu_); + Slot& s = slots_[key]; + if (token < s.fence) { + note("write with stale token " + std::to_string(token) + " rejected (fence " + + std::to_string(s.fence) + ")"); + return false; + } + s.fence = token; + note("write with token " + std::to_string(token) + " accepted"); + return true; +} + +void CoordLab::tick(long ms) { + Guard g(mu_); + clock_->advance(ms); + note("clock +" + std::to_string(ms) + "ms"); +} + +LabRows CoordLab::rows() const { + Guard g(mu_); + LabRows out{{"clock_ms", std::to_string(clock_->now())}, + {"ttl_ms", std::to_string(ttl_ms_)}}; + for (const char* key : {"lease", "lock", "leader"}) { + auto it = slots_.find(key); + if (it == slots_.end()) { + out.emplace_back(key, "free"); + continue; + } + const Slot& s = it->second; + bool live = !s.holder.empty() && s.expires > clock_->now(); + out.emplace_back(key, (live ? "held by " + s.holder : std::string("free")) + + ", token " + std::to_string(s.token) + + ", fence " + std::to_string(s.fence)); + } + return out; +} + +// ---- DebugRegistry --------------------------------------------------------- + +void DebugRegistry::add(std::shared_ptr lab) { + labs_.push_back(std::move(lab)); +} + +std::string DebugRegistry::snapshot() const { + std::string out = "{\"count\":" + std::to_string(labs_.size()) + ",\"labs\":{"; + bool first = true; + for (const auto& l : labs_) { + out += std::string(first ? "" : ",") + "\"" + json_escape(l->name()) + + "\":" + l->json(); + first = false; + } + return out + "}}"; +} + +// ---- SupplyChainLab -------------------------------------------------------- + +SupplyChainLab::SupplyChainLab() { + reset(); +} + +void SupplyChainLab::reset() { + Guard g(mu_); + components_ = {{"molo-example", "1.0.0", "proprietary"}, + {"chartjs", "4.4.1", "MIT"}, + {"wolfssl", "5.7.2", "GPL-2.0"}, + {"sqlite", "3.45.1", "blessing"}}; + pins_.clear(); + for (const auto& c : components_) pins_[c.name] = c.version; + advisories_.clear(); + attested_ = digest(); + verified_ = 0; + note("sbom regenerated and attested " + attested_); +} + +std::string SupplyChainLab::digest() const { + std::string canon; + for (const auto& c : components_) + canon += c.name + "@" + c.version + ";" + c.license + "\n"; + return fnv_hex(canon); +} + +bool SupplyChainLab::add_advisory(const std::string& component, + const std::string& version) { + Guard g(mu_); + if (advisories_.size() >= labcaps::kMaxAdvisories) return false; + advisories_.emplace_back(component, version); + note("advisory published for " + component + "@" + version); + return true; +} + +void SupplyChainLab::tamper() { + Guard g(mu_); + components_[1].version = "4.4.2"; + verified_ = 0; + note("chartjs changed to 4.4.2 after attestation"); +} + +bool SupplyChainLab::verify(const std::string& claimed) { + Guard g(mu_); + bool ok = claimed == attested_ && claimed == digest(); + verified_ = ok ? 1 : -1; + note(std::string("verify ") + (ok ? "ok" : "FAILED")); + return ok; +} + +bool SupplyChainLab::pins_ok() const { + for (const auto& c : components_) { + auto it = pins_.find(c.name); + if (it == pins_.end() || it->second != c.version) return false; + } + return true; +} + +bool SupplyChainLab::advisories_ok() const { + for (const auto& c : components_) + for (const auto& a : advisories_) + if (a.first == c.name && a.second == c.version) return false; + return true; +} + +bool SupplyChainLab::gate() const { + Guard g(mu_); + return pins_ok() && advisories_ok() && attested_ == digest(); +} + +std::string SupplyChainLab::cyclonedx() const { + Guard g(mu_); + std::string out = + "{\"bomFormat\":\"CycloneDX\",\"specVersion\":\"1.5\",\"components\":["; + bool first = true; + for (const auto& c : components_) { + out += std::string(first ? "" : ",") + "{\"name\":\"" + json_escape(c.name) + + "\",\"version\":\"" + json_escape(c.version) + + "\",\"license\":\"" + json_escape(c.license) + "\"}"; + first = false; + } + return out + "],\"digest\":\"" + digest() + "\"}"; +} + +LabRows SupplyChainLab::rows() const { + Guard g(mu_); + LabRows out; + for (const auto& c : components_) + out.emplace_back(c.name, c.version + " (" + c.license + ")"); + out.emplace_back("pins", pins_ok() ? "pass" : "FAIL"); + out.emplace_back("advisories", + advisories_ok() ? "pass (" + std::to_string(advisories_.size()) + + " known)" + : "FAIL"); + out.emplace_back("attestation", attested_ == digest() ? "pass" : "FAIL"); + out.emplace_back("last_verify", verified_ == 0 ? "none" : verified_ > 0 ? "ok" : "failed"); + out.emplace_back("sbom_digest", digest()); + out.emplace_back("gate", pins_ok() && advisories_ok() && attested_ == digest() + ? "PASS" + : "FAIL"); + return out; +} + +// ---- ClientRetryLab -------------------------------------------------------- + +ClientRetryLab::Outcome ClientRetryLab::run(int failures, int max_attempts, + int status) { + Guard g(mu_); + max_attempts = std::clamp(max_attempts, 1, labcaps::kMaxAttempts); + Outcome o; + for (int a = 1; a <= max_attempts; ++a) { + o.attempts = a; + o.status = a <= failures ? status : 200; + note("attempt " + std::to_string(a) + " -> " + std::to_string(o.status)); + if (o.status == 200) break; + if (o.status < 500) { + o.gave_up = true; + note("4xx is never retried"); + break; + } + if (a == max_attempts) { + o.gave_up = true; + note("attempts exhausted"); + break; + } + long wait = std::min(kCapMs, kBaseMs << (a - 1)); + o.waited_ms += wait; + note("backoff " + std::to_string(wait) + "ms (cap " + + std::to_string(kCapMs) + "ms)"); + } + last_ = o; + ++runs_; + return o; +} + +LabRows ClientRetryLab::rows() const { + Guard g(mu_); + return {{"runs", std::to_string(runs_)}, + {"attempts", std::to_string(last_.attempts)}, + {"final_status", std::to_string(last_.status)}, + {"virtual_wait_ms", std::to_string(last_.waited_ms)}, + {"gave_up", last_.gave_up ? "yes" : "no"}, + {"backoff", "100ms doubling, cap 800ms"}}; +} + +// ---- ClientSessionLab ------------------------------------------------------ + +ClientSessionLab::ClientSessionLab() = default; + +bool ClientSessionLab::valid_proxy(const std::string& p) { + if (p.empty() || p == "direct") return true; + for (const char* scheme : {"http://", "https://", "socks5://"}) { + std::string s = scheme; + if (p.compare(0, s.size(), s) != 0) continue; + std::string hp = p.substr(s.size()); + std::size_t colon = hp.rfind(':'); + if (colon == std::string::npos || colon == 0 || colon + 1 >= hp.size()) + return false; + for (std::size_t i = colon + 1; i < hp.size(); ++i) + if (hp[i] < '0' || hp[i] > '9') return false; + return hp.size() - colon - 1 <= 5; + } + return false; +} + +std::map ClientSessionLab::request( + const std::string& auth, const std::string& proxy) { + Guard g(mu_); + std::string px = proxy.empty() ? "direct" : proxy; + std::map h{{"Host", "example.internal"}}; + if (!jar_.empty()) { + std::string c; + for (const auto& [k, v] : jar_) c += (c.empty() ? "" : "; ") + k + "=" + v; + h["Cookie"] = c; + } + if (auth == "basic") + h["Authorization"] = "Basic " + base64_encode("demo:demo"); + else if (auth == "bearer") + h["Authorization"] = "Bearer demo-token"; + if (px != proxy_) idle_ = 0; // a new route needs a new connection + proxy_ = px; + auth_ = auth; + if (idle_ > 0) { + --idle_; + ++reused_; + note("connection reused"); + } else { + ++created_; + note("connection created via " + px); + } + ++idle_; + ++requests_; + jar_["sid"] = "s-1"; // Set-Cookie from the in-process server + jar_["visits"] = std::to_string(requests_); + note("request " + std::to_string(requests_) + " auth=" + auth); + return h; +} + +LabRows ClientSessionLab::rows() const { + Guard g(mu_); + std::string jar; + for (const auto& [k, v] : jar_) jar += (jar.empty() ? "" : "; ") + k + "=" + v; + return {{"requests", std::to_string(requests_)}, + {"cookie_jar", jar.empty() ? "empty" : jar}, + {"pool_created", std::to_string(created_)}, + {"pool_reused", std::to_string(reused_)}, + {"pool_idle", std::to_string(idle_)}, + {"proxy", proxy_}, + {"auth_scheme", auth_}}; +} + +// ---- MigrationLab ---------------------------------------------------------- + +MigrationLab::MigrationLab() { + steps_ = {{"001", "CREATE TABLE users (id, name)", "users"}, + {"002", "ALTER TABLE users ADD COLUMN email", "users"}, + {"003", "CREATE TABLE posts (id, user_id, title)", "posts"}}; +} + +std::vector MigrationLab::check() const { + Guard g(mu_); + std::vector out; + for (const auto& s : steps_) + if (std::find(applied_.begin(), applied_.end(), s.id) == applied_.end()) + out.push_back(s.id + " " + s.sql); + return out; +} + +int MigrationLab::apply(int limit) { + Guard g(mu_); + int n = 0; + for (const auto& s : steps_) { + if (n >= limit) break; + if (std::find(applied_.begin(), applied_.end(), s.id) != applied_.end()) + continue; + if (s.sql.compare(0, 6, "CREATE") == 0) + tables_[s.table] = s.sql.substr(s.sql.find('(') + 1); + else + tables_[s.table] += " +email"; + applied_.push_back(s.id); + note("applied " + s.id); + ++n; + } + return n; +} + +void MigrationLab::drop_table(const std::string& table) { + Guard g(mu_); + if (tables_.erase(table)) note("table " + table + " dropped out of band"); +} + +std::vector MigrationLab::drift() const { + Guard g(mu_); + std::vector out; + for (const auto& s : steps_) + if (s.sql.compare(0, 6, "CREATE") == 0 && !tables_.count(s.table) && + std::find(applied_.begin(), applied_.end(), s.id) != applied_.end()) + out.push_back(s.id + " " + s.table); + return out; +} + +int MigrationLab::heal() { + Guard g(mu_); + int n = 0; + for (const auto& s : steps_) { + if (s.sql.compare(0, 6, "CREATE") == 0 && !tables_.count(s.table) && + std::find(applied_.begin(), applied_.end(), s.id) != applied_.end()) { + tables_[s.table] = s.sql.substr(s.sql.find('(') + 1); + note("healed " + s.table); + ++n; + } + } + return n; +} + +LabRows MigrationLab::rows() const { + Guard g(mu_); + LabRows out; + std::string pending, applied; + for (const auto& s : steps_) { + bool done = std::find(applied_.begin(), applied_.end(), s.id) != applied_.end(); + (done ? applied : pending) += (done ? applied : pending).empty() ? s.id : "," + s.id; + } + out.emplace_back("applied", applied.empty() ? "none" : applied); + out.emplace_back("pending", pending.empty() ? "none" : pending); + for (const auto& [t, cols] : tables_) out.emplace_back("table " + t, cols); + auto d = drift(); + out.emplace_back("drift", d.empty() ? "none" : d.front()); + return out; +} + +// ---- TxLab ----------------------------------------------------------------- + +TxLab::TxLab() : accounts_{{"alice", 100}, {"bob", 50}} {} + +long TxLab::balance(const std::string& who) const { + Guard g(mu_); + auto it = accounts_.find(who); + return it == accounts_.end() ? -1 : it->second; +} + +std::string TxLab::transfer(const std::string& from, const std::string& to, + long amount, bool fail_after_debit) { + Guard g(mu_); + auto snapshot = accounts_; // BEGIN + accounts_[from] -= amount; + std::string reason; + if (accounts_[from] < 0) reason = "insufficient funds"; + else if (fail_after_debit) reason = "failure after debit"; + if (!reason.empty()) { + accounts_ = snapshot; // ROLLBACK + ++rollbacks_; + note("ROLLBACK: " + reason); + return "rolled back: " + reason; + } + accounts_[to] += amount; // COMMIT + ++commits_; + note("COMMIT: " + from + " -> " + to + " " + std::to_string(amount)); + return "committed"; +} + +LabRows TxLab::rows() const { + Guard g(mu_); + LabRows out; + for (const auto& [k, v] : accounts_) out.emplace_back(k, std::to_string(v)); + out.emplace_back("commits", std::to_string(commits_)); + out.emplace_back("rollbacks", std::to_string(rollbacks_)); + return out; +} + +// ---- QueryLab -------------------------------------------------------------- + +QueryLab::QueryLab() { + users_ = {{{"id", "1"}, {"name", "ada"}}, {{"id", "2"}, {"name", "grace"}}}; + posts_ = {{{"id", "1"}, {"user_id", "1"}, {"title", "Alpha"}}, + {{"id", "2"}, {"user_id", "1"}, {"title", "Gamma"}}, + {{"id", "3"}, {"user_id", "2"}, {"title", "Beta"}}, + {{"id", "4"}, {"user_id", "2"}, {"title", "Delta"}}, + {{"id", "5"}, {"user_id", "1"}, {"title", "Epsilon"}}}; +} + +QueryLab::Result QueryLab::run(long user_id, const std::string& order, + int limit) { + Guard g(mu_); + Result r; + r.sql = "SELECT posts.id, posts.title, users.name FROM posts JOIN users ON " + "users.id = posts.user_id"; + if (user_id > 0) r.sql += " WHERE posts.user_id = " + std::to_string(user_id); + std::string col = order == "title" ? "title" : "id"; + r.sql += " ORDER BY posts." + col + " LIMIT " + std::to_string(limit); + std::vector> picked; + for (const auto& p : posts_) { + if (user_id > 0 && p.at("user_id") != std::to_string(user_id)) continue; + auto row = p; + for (const auto& u : users_) + if (u.at("id") == p.at("user_id")) row["author"] = u.at("name"); + picked.push_back(row); + } + std::stable_sort(picked.begin(), picked.end(), [&](const auto& a, const auto& b) { + if (col == "id") return std::stol(a.at("id")) < std::stol(b.at("id")); + return a.at(col) < b.at(col); + }); + if ((int)picked.size() > limit) picked.resize((std::size_t)limit); + r.rows = std::move(picked); + last_ = r; + note(std::to_string(r.rows.size()) + " row(s)"); + return r; +} + +LabRows QueryLab::rows() const { + Guard g(mu_); + LabRows out{{"sql", last_.sql.empty() ? "not run" : last_.sql}}; + for (const auto& r : last_.rows) + out.emplace_back("post " + r.at("id"), r.at("title") + " by " + r.at("author")); + return out; +} + +// ---- SingleFlight ---------------------------------------------------------- + +std::string SingleFlight::run(const std::string& key, + const std::function& loader) { + std::unique_lock lk(mu_); + auto it = calls_.find(key); + if (it != calls_.end()) { + auto call = it->second; + ++call->waiters; + cv_.wait(lk, [&] { return call->done; }); + return call->value; + } + auto call = std::make_shared(); + calls_[key] = call; + lk.unlock(); + std::string value = loader(); + lk.lock(); + call->value = value; + call->done = true; + calls_.erase(key); + cv_.notify_all(); + return value; +} + +int SingleFlight::waiters(const std::string& key) const { + std::lock_guard lk(mu_); + auto it = calls_.find(key); + return it == calls_.end() ? 0 : it->second->waiters; +} + +SingleFlightLab::SingleFlightLab() : flight_(std::make_unique()) {} + +int SingleFlightLab::run(int callers) { + callers = std::clamp(callers, 1, labcaps::kMaxThreads); + std::atomic loads{0}; + SingleFlight* flight = flight_.get(); + auto loader = [&]() { + ++loads; + // Hold the load open until every other caller has joined (2 s cap), so + // the collapse is deterministic and never depends on scheduling luck. + for (int i = 0; i < 2000 && flight->waiters("report") < callers - 1; ++i) + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + return std::string("loaded"); + }; + std::vector pool; + for (int i = 0; i < callers; ++i) + pool.emplace_back([&] { flight->run("report", loader); }); + for (auto& t : pool) t.join(); + Guard g(mu_); + callers_ = callers; + loads_ = loads.load(); + ++runs_; + note(std::to_string(callers) + " concurrent callers -> " + + std::to_string(loads_) + " load(s)"); + return loads_; +} + +LabRows SingleFlightLab::rows() const { + Guard g(mu_); + return {{"runs", std::to_string(runs_)}, + {"callers", std::to_string(callers_)}, + {"loader_executions", std::to_string(loads_)}, + {"thread_cap", std::to_string(labcaps::kMaxThreads)}}; +} + +// ---- TtlLab ---------------------------------------------------------------- + +TtlLab::TtlLab(std::shared_ptr clock) : clock_(std::move(clock)) {} + +void TtlLab::publish(const std::string& event) { + events_.push_back(event); + while (events_.size() > labcaps::kLogLines) events_.pop_front(); + note("kv.events: " + event); +} + +void TtlLab::sweep() { + for (auto it = entries_.begin(); it != entries_.end();) { + if (it->second.expires <= clock_->now()) { + publish("expired " + it->first); + it = entries_.erase(it); + } else { + ++it; + } + } +} + +void TtlLab::set(const std::string& key, const std::string& value, long ttl_s) { + Guard g(mu_); + sweep(); + if (!entries_.count(key) && entries_.size() >= labcaps::kMaxEntries) { + auto lru = std::min_element( + entries_.begin(), entries_.end(), + [](const auto& a, const auto& b) { return a.second.touched < b.second.touched; }); + publish("evicted " + lru->first + " (lru)"); + entries_.erase(lru); + } + entries_[key] = {value, clock_->now() + ttl_s * 1000, seq_++}; + publish("set " + key); +} + +bool TtlLab::get(const std::string& key, std::string& value) { + Guard g(mu_); + sweep(); + auto it = entries_.find(key); + if (it == entries_.end()) { + ++misses_; + note("miss " + key); + return false; + } + it->second.touched = seq_++; + value = it->second.value; + ++hits_; + note("hit " + key); + return true; +} + +void TtlLab::tick(long ms) { + Guard g(mu_); + clock_->advance(ms); + note("clock +" + std::to_string(ms) + "ms"); + sweep(); +} + +std::size_t TtlLab::size() const { + Guard g(mu_); + return entries_.size(); +} + +std::vector TtlLab::events() const { + Guard g(mu_); + return {events_.begin(), events_.end()}; +} + +LabRows TtlLab::rows() const { + Guard g(mu_); + LabRows out{{"clock_ms", std::to_string(clock_->now())}, + {"entries", std::to_string(entries_.size()) + "/" + + std::to_string(labcaps::kMaxEntries)}, + {"hits", std::to_string(hits_)}, + {"misses", std::to_string(misses_)}}; + for (const auto& [k, e] : entries_) + out.emplace_back("key " + k, e.value + ", " + + std::to_string((e.expires - clock_->now()) / 1000) + + "s left"); + return out; +} + +} // namespace molo::example::fw diff --git a/example/fw/Resilience.hpp b/example/fw/Resilience.hpp new file mode 100644 index 0000000..2f136a9 --- /dev/null +++ b/example/fw/Resilience.hpp @@ -0,0 +1,408 @@ +#pragma once + +// Resilience, delivery, supply chain, clients and data-depth skeletons for the +// living example (namespace molo::example::fw). Every class is a Lab: a small +// self-contained state machine with injectable time, a bounded log, and a +// uniform rows() view that the demo pages and the debug snapshot render. +// Gaps are listed per class next to the real module in src/. + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace molo::example::fw { + +namespace labcaps { +constexpr std::size_t kLogLines = 20; // bounded log per lab +constexpr long kMaxAdvanceMs = 3600000; // one clock step, 1 hour +constexpr int kMaxThreads = 16; // singleflight callers +constexpr std::size_t kMaxEntries = 5; // ttl store capacity (LRU) +constexpr std::size_t kMaxAdvisories = 8; // supplychain advisory db +constexpr int kMaxAttempts = 5; // client retry ceiling +constexpr int kMaxFaults = 5; // chaos blast radius (faults) +constexpr int kMaxPercent = 50; // chaos blast radius (percent) +} // namespace labcaps + +using LabRows = std::vector>; + +// Injectable time so every state machine is deterministic in tests. +class ManualClock { + public: + long now() const { return now_; } + void advance(long ms); + + private: + long now_ = 0; +}; + +std::string fnv_hex(const std::string& data); // 64 bit FNV-1a, hex +std::string base64_encode(const std::string& data); + +class Lab { + public: + virtual ~Lab() = default; + virtual std::string name() const = 0; + virtual LabRows rows() const = 0; + std::vector log() const; + std::string json() const; // {"name":..,"state":{..},"log":[..]} + + protected: + void note(std::string line); // caller holds mu_ + mutable std::recursive_mutex mu_; + + private: + std::deque log_; +}; + +// src/operability/resilience/breaker.h +class CircuitBreaker : public Lab { + public: + enum class State { Closed, Open, HalfOpen }; + CircuitBreaker(std::shared_ptr clock, int threshold = 3, + long open_ms = 30000); + // Records one call. Returns false when the breaker rejected it (open). + bool call(bool success); + void tick(long ms); + State state() const; + std::string name() const override { return "breaker"; } + LabRows rows() const override; + + private: + State effective() const; + std::shared_ptr clock_; + int threshold_; + long open_ms_; + State state_ = State::Closed; + int failures_ = 0; + long opened_at_ = 0; + int rejected_ = 0; +}; +std::string breaker_state_name(CircuitBreaker::State s); + +// src/operability/resilience/bulkhead.h +class Bulkhead : public Lab { + public: + enum class Admission { Admitted, Queued, Rejected }; + Bulkhead(int max_active = 2, int max_queue = 1); + Admission acquire(); + bool release(); // false when nothing to release + std::string name() const override { return "bulkhead"; } + LabRows rows() const override; + + private: + int max_active_, max_queue_, active_ = 0, queued_ = 0, rejected_ = 0; +}; + +// src/operability/resilience/shedder.h +class Shedder : public Lab { + public: + explicit Shedder(int limit = 3) : limit_(limit) {} + // low priority is shed at 2/3 of the limit, high only at the limit. + bool enter(bool high); + bool leave(); + std::string name() const override { return "shedder"; } + LabRows rows() const override; + + private: + int limit_, inflight_ = 0, shed_low_ = 0, shed_high_ = 0; +}; + +// src/operability/resilience/deadline.h +class DeadlineLab : public Lab { + public: + static constexpr long kHopCostMs = 30; + // Walks `hops` downstream calls against one propagated budget. + // Returns the hop that expired (1 based) or 0 when all finished. + int run(long budget_ms, int hops); + std::string name() const override { return "deadline"; } + LabRows rows() const override; + + private: + long budget_ms_ = 0, remaining_ms_ = 0; + int hops_ = 0, expired_hop_ = 0; + bool ran_ = false; +}; + +// src/operability/resilience/slo.h +class SloBudget : public Lab { + public: + static constexpr std::size_t kWindow = 100; // ring of the last 100 events + SloBudget(int target_permille = 950) : target_(target_permille) {} + void record(bool good); + void reset(); + long budget_remaining() const; // allowed bad events minus actual bad + std::string name() const override { return "slo"; } + LabRows rows() const override; + + private: + int target_; + std::deque window_; +}; + +// src/operability/chaos/experiment.h, injector.h +class ChaosLab : public Lab { + public: + static constexpr int kCalls = 10; // simulated calls per experiment + struct Result { + int percent = 0, max_faults = 0, planned = 0, injected = 0; + bool armed = false; + }; + // Deterministic: an accumulator injects a fault every 100/percent calls. + // Dry run (armed=false) plans faults but never injects them. + Result run(int percent, int max_faults, bool armed); + std::string name() const override { return "chaos"; } + LabRows rows() const override; + + private: + Result last_; + int runs_ = 0, total_injected_ = 0; +}; + +// src/operability/deploy/rolling.h, blue_green.h, lifecycle.h +class DeployLab : public Lab { + public: + enum class Phase { Idle, InProgress, Done, RolledBack }; + DeployLab(); + bool start(const std::string& strategy, const std::string& version); + bool step(); + bool rollback(); + Phase phase() const; + std::string name() const override { return "deploy"; } + LabRows rows() const override; + + private: + std::vector fleet_, previous_; + std::string strategy_ = "none", target_; + int green_stage_ = 0; // blue-green: 0 prepare, 1 switched + Phase phase_ = Phase::Idle; +}; +std::string deploy_phase_name(DeployLab::Phase p); + +// src/operability/coord/lease.h, election.h, dist_lock.h, fence.h +class CoordLab : public Lab { + public: + struct Grant { + bool ok = false; + long token = 0; + std::string holder; + }; + explicit CoordLab(std::shared_ptr clock, long ttl_ms = 10000); + Grant acquire(const std::string& key, const std::string& owner); + bool release(const std::string& key, const std::string& owner); + // Fenced write: accepted only when token >= the highest token seen. + bool write(const std::string& key, long token); + void tick(long ms); + static bool known_key(const std::string& key); + std::string name() const override { return "coord"; } + LabRows rows() const override; + + private: + struct Slot { + std::string holder; + long expires = 0, token = 0, fence = 0; + }; + std::shared_ptr clock_; + long ttl_ms_; + std::map slots_; +}; + +// src/operability/debug/endpoint.h, snapshot.h +class DebugRegistry { + public: + void add(std::shared_ptr lab); + std::string snapshot() const; // {"labs":{name:{..}}} + std::size_t size() const { return labs_.size(); } + + private: + std::vector> labs_; +}; + +// src/supplychain/sbom, policy, attest +class SupplyChainLab : public Lab { + public: + struct Component { + std::string name, version, license; + }; + SupplyChainLab(); + bool add_advisory(const std::string& component, const std::string& version); + void tamper(); + void reset(); + // True when `digest` equals the attested and the current SBOM digest. + bool verify(const std::string& digest); + std::string digest() const; + std::string attested() const { return attested_; } + bool gate() const; + std::string cyclonedx() const; + std::string name() const override { return "supplychain"; } + LabRows rows() const override; + + private: + bool pins_ok() const; + bool advisories_ok() const; + std::vector components_; + std::map pins_; + std::vector> advisories_; + std::string attested_; + int verified_ = 0; // 0 none, 1 ok, -1 failed +}; + +// src/clients/retry.h +class ClientRetryLab : public Lab { + public: + struct Outcome { + int attempts = 0, status = 0; + long waited_ms = 0; + bool gave_up = false; + }; + // The in-process transport fails the first `failures` calls with `status`. + // 5xx is retried with capped exponential backoff; 4xx never is. + Outcome run(int failures, int max_attempts, int status); + std::string name() const override { return "client-retry"; } + LabRows rows() const override; + + private: + static constexpr long kBaseMs = 100, kCapMs = 800; + Outcome last_; + int runs_ = 0; +}; + +// src/clients/cookies.h, pool.h, proxy.h, auth.h +class ClientSessionLab : public Lab { + public: + ClientSessionLab(); + static bool valid_proxy(const std::string& proxy); + // One request through the in-process transport; returns the request headers. + std::map request(const std::string& auth, + const std::string& proxy); + std::string name() const override { return "client-session"; } + LabRows rows() const override; + + private: + std::map jar_; + int created_ = 0, reused_ = 0, idle_ = 0, requests_ = 0; + std::string proxy_ = "direct", auth_ = "none"; +}; + +// src/data/orm/migration.hpp, migrator.hpp, healer.hpp +class MigrationLab : public Lab { + public: + struct Step { + std::string id, sql, table; + }; + MigrationLab(); + // check: what apply would do, nothing changes. + std::vector check() const; + int apply(int limit); + void drop_table(const std::string& table); // simulated drift + int heal(); // re-apply drifted creates + std::vector drift() const; + std::string name() const override { return "migrations"; } + LabRows rows() const override; + + private: + std::vector steps_; + std::vector applied_; + std::map tables_; // table -> columns +}; + +// src/data/transaction.hpp +class TxLab : public Lab { + public: + TxLab(); + // Returns "committed", or the rollback reason. + std::string transfer(const std::string& from, const std::string& to, + long amount, bool fail_after_debit); + long balance(const std::string& who) const; + std::string name() const override { return "transaction"; } + LabRows rows() const override; + + private: + std::map accounts_; + int commits_ = 0, rollbacks_ = 0; +}; + +// src/data/orm/query.hpp, relations.hpp +class QueryLab : public Lab { + public: + struct Result { + std::string sql; + std::vector> rows; + }; + QueryLab(); + // Builds "SELECT ... FROM posts [WHERE user_id = ?] ORDER BY .. LIMIT n" + // and executes it against the in-memory users/posts tables (has-many). + Result run(long user_id, const std::string& order, int limit); + std::string name() const override { return "query"; } + LabRows rows() const override; + + private: + std::vector> users_, posts_; + Result last_; +}; + +// src/data/cache/singleflight.hpp +class SingleFlight { + public: + // Concurrent callers of one key share a single loader execution. + std::string run(const std::string& key, + const std::function& loader); + int waiters(const std::string& key) const; + + private: + struct Call { + bool done = false; + int waiters = 0; + std::string value; + }; + mutable std::mutex mu_; + std::condition_variable_any cv_; + std::map> calls_; +}; + +class SingleFlightLab : public Lab { + public: + SingleFlightLab(); + // Starts `callers` threads on one key; returns loader executions. + int run(int callers); + std::string name() const override { return "singleflight"; } + LabRows rows() const override; + + private: + std::unique_ptr flight_; + int callers_ = 0, loads_ = 0, runs_ = 0; +}; + +// src/data/kv/memory.hpp, src/data/cache/policy.hpp, src/data/pubsub/broker.hpp +class TtlLab : public Lab { + public: + explicit TtlLab(std::shared_ptr clock); + void set(const std::string& key, const std::string& value, long ttl_s); + // Returns true and fills value on a live hit. + bool get(const std::string& key, std::string& value); + void tick(long ms); + std::size_t size() const; + std::vector events() const; // pubsub topic "kv.events" + std::string name() const override { return "ttl"; } + LabRows rows() const override; + + private: + struct Entry { + std::string value; + long expires = 0, touched = 0; + }; + void sweep(); + void publish(const std::string& event); + std::shared_ptr clock_; + std::map entries_; + std::deque events_; + long seq_ = 0; + int hits_ = 0, misses_ = 0; +}; + +} // namespace molo::example::fw diff --git a/example/fw/SecurityExtra.cpp b/example/fw/SecurityExtra.cpp new file mode 100644 index 0000000..6e48844 --- /dev/null +++ b/example/fw/SecurityExtra.cpp @@ -0,0 +1,390 @@ +#include "SecurityExtra.hpp" + +#include +#include +#include +#include + +namespace molo::example::fw { + +namespace { +const char* kHex = "0123456789abcdef"; +inline std::uint32_t rotr(std::uint32_t x, int n) { + return (x >> n) | (x << (32 - n)); +} +const std::uint32_t kK[64] = { + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, + 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, + 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, + 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, + 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, + 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, + 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, + 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, + 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; +} // namespace + +std::string to_hex(const std::string& raw) { + std::string out; + out.reserve(raw.size() * 2); + for (unsigned char c : raw) { + out += kHex[c >> 4]; + out += kHex[c & 15]; + } + return out; +} + +bool from_hex(const std::string& hex, std::string& raw) { + raw.clear(); + if (hex.size() % 2 != 0) return false; + auto val = [](char c) -> int { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return c - 'a' + 10; + if (c >= 'A' && c <= 'F') return c - 'A' + 10; + return -1; + }; + for (std::size_t i = 0; i < hex.size(); i += 2) { + int a = val(hex[i]), b = val(hex[i + 1]); + if (a < 0 || b < 0) return false; + raw += static_cast(a * 16 + b); + } + return true; +} + +bool constant_time_equal(const std::string& a, const std::string& b) { + unsigned char diff = a.size() == b.size() ? 0 : 1; + std::size_t n = std::min(a.size(), b.size()); + for (std::size_t i = 0; i < n; ++i) + diff |= static_cast(a[i] ^ b[i]); + return diff == 0; +} + +std::string Sha256::digest(const std::string& data) { + std::uint32_t h[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, + 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19}; + std::string msg = data; + std::uint64_t bits = static_cast(data.size()) * 8; + msg += static_cast(0x80); + while (msg.size() % 64 != 56) msg += '\0'; + for (int i = 7; i >= 0; --i) msg += static_cast((bits >> (i * 8)) & 0xff); + for (std::size_t off = 0; off < msg.size(); off += 64) { + std::uint32_t w[64]; + for (int i = 0; i < 16; ++i) { + const unsigned char* p = + reinterpret_cast(msg.data()) + off + i * 4; + w[i] = (std::uint32_t(p[0]) << 24) | (std::uint32_t(p[1]) << 16) | + (std::uint32_t(p[2]) << 8) | p[3]; + } + for (int i = 16; i < 64; ++i) { + std::uint32_t s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >> 3); + std::uint32_t s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >> 10); + w[i] = w[i - 16] + s0 + w[i - 7] + s1; + } + std::uint32_t a = h[0], b = h[1], c = h[2], d = h[3], e = h[4], f = h[5], + g = h[6], hh = h[7]; + for (int i = 0; i < 64; ++i) { + std::uint32_t S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25); + std::uint32_t ch = (e & f) ^ (~e & g); + std::uint32_t t1 = hh + S1 + ch + kK[i] + w[i]; + std::uint32_t S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22); + std::uint32_t mj = (a & b) ^ (a & c) ^ (b & c); + std::uint32_t t2 = S0 + mj; + hh = g; g = f; f = e; e = d + t1; d = c; c = b; b = a; a = t1 + t2; + } + h[0] += a; h[1] += b; h[2] += c; h[3] += d; + h[4] += e; h[5] += f; h[6] += g; h[7] += hh; + } + std::string out; + for (int i = 0; i < 8; ++i) + for (int s = 24; s >= 0; s -= 8) out += static_cast((h[i] >> s) & 0xff); + return out; +} + +std::string Sha256::hex(const std::string& data) { return to_hex(digest(data)); } + +std::string Hmac::sha256(const std::string& key, const std::string& data) { + std::string k = key.size() > 64 ? Sha256::digest(key) : key; + k.resize(64, '\0'); + std::string ipad(64, '\0'), opad(64, '\0'); + for (int i = 0; i < 64; ++i) { + ipad[i] = static_cast(k[i] ^ 0x36); + opad[i] = static_cast(k[i] ^ 0x5c); + } + return Sha256::digest(opad + Sha256::digest(ipad + data)); +} + +std::string SecureRandom::bytes(std::size_t n) { + n = std::min(n, secaps::kMaxRandomBytes); + std::string out(n, '\0'); + std::FILE* f = std::fopen("/dev/urandom", "rb"); + if (!f) throw std::runtime_error("no secure random source"); + std::size_t got = std::fread(out.data(), 1, n, f); + std::fclose(f); + if (got != n) throw std::runtime_error("short read from urandom"); + return out; +} + +std::string SecureRandom::token_hex(std::size_t n) { return to_hex(bytes(n)); } + +namespace { +std::string pbkdf2(const std::string& pw, const std::string& salt, int rounds) { + std::string u = Hmac::sha256(pw, salt + std::string("\0\0\0\1", 4)); + std::string t = u; + for (int i = 1; i < rounds; ++i) { + u = Hmac::sha256(pw, u); + for (std::size_t j = 0; j < t.size(); ++j) t[j] = static_cast(t[j] ^ u[j]); + } + return t; +} +} // namespace + +std::string PasswordHasher::hash(const std::string& password) { + std::string salt = SecureRandom::bytes(16); + return "pbkdf2-demo$" + std::to_string(secaps::kDemoPbkdf2Rounds) + "$" + + to_hex(salt) + "$" + + to_hex(pbkdf2(password, salt, secaps::kDemoPbkdf2Rounds)); +} + +bool PasswordHasher::verify(const std::string& password, const std::string& stored) { + std::vector parts; + std::size_t pos = 0; + while (true) { + std::size_t d = stored.find('$', pos); + parts.push_back(stored.substr(pos, d == std::string::npos ? d : d - pos)); + if (d == std::string::npos) break; + pos = d + 1; + } + if (parts.size() != 4 || parts[0] != "pbkdf2-demo") return false; + int rounds = 0; + try { rounds = std::stoi(parts[1]); } catch (...) { return false; } + if (rounds < 1 || rounds > 100000) return false; + std::string salt; + if (!from_hex(parts[2], salt)) return false; + return constant_time_equal(to_hex(pbkdf2(password, salt, rounds)), parts[3]); +} + +void DenialSink::record(const std::string& guard, const std::string& reason) { + std::lock_guard lock(mu_); + ++total_; + if (ring_.size() >= secaps::kMaxDenials) ring_.erase(ring_.begin()); + ring_.push_back({guard, reason}); +} +std::vector DenialSink::list() const { + std::lock_guard lock(mu_); + return ring_; +} +std::size_t DenialSink::total() const { + std::lock_guard lock(mu_); + return total_; +} + +std::string CsrfGuard::issue() const { + std::string nonce = SecureRandom::token_hex(16); + return nonce + "." + to_hex(Hmac::sha256(secret_, nonce)); +} +bool CsrfGuard::verify(const std::string& token) const { + std::size_t dot = token.find('.'); + if (dot == std::string::npos || dot == 0 || token.size() > 200) return false; + std::string nonce = token.substr(0, dot); + return constant_time_equal(token.substr(dot + 1), + to_hex(Hmac::sha256(secret_, nonce))); +} + +bool CorsPolicy::allowed(const std::string& origin) const { + return !origin.empty() && origins_.count(origin) > 0; +} +bool CorsPolicy::apply_preflight(const Request& req, Response& res) const { + std::string origin = req.headers.get("Origin"); + if (!allowed(origin)) return false; + res.headers.set("Access-Control-Allow-Origin", origin); + res.headers.set("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); + res.headers.set("Access-Control-Allow-Headers", "Content-Type, Authorization"); + res.headers.set("Access-Control-Max-Age", "600"); + res.headers.set("Vary", "Origin"); + return true; +} + +bool RateLimiter::hit(const std::string& key, int& retry_after, int& remaining) { + std::lock_guard lock(mu_); + auto now = std::chrono::steady_clock::now(); + auto win = std::chrono::seconds(window_); + if (slots_.size() >= secaps::kMaxRateKeys && !slots_.count(key)) { + for (auto it = slots_.begin(); it != slots_.end();) + it = (now - it->second.start >= win) ? slots_.erase(it) : std::next(it); + if (slots_.size() >= secaps::kMaxRateKeys) { // floor: refuse new keys + retry_after = window_; + remaining = 0; + return false; + } + } + Slot& s = slots_[key]; + if (s.count == 0 || now - s.start >= win) { + s.start = now; + s.count = 0; + } + if (s.count >= limit_) { + auto left = std::chrono::duration_cast(win - (now - s.start)); + retry_after = std::max(1, static_cast(left.count()) + 1); + remaining = 0; + return false; + } + ++s.count; + retry_after = 0; + remaining = limit_ - s.count; + return true; +} + +std::vector> SecurityHeaders::defaults() { + return {{"X-Content-Type-Options", "nosniff"}, + {"X-Frame-Options", "DENY"}, + {"Referrer-Policy", "no-referrer"}, + {"Strict-Transport-Security", "max-age=31536000; includeSubDomains"}, + {"Cross-Origin-Opener-Policy", "same-origin"}, + {"Permissions-Policy", "camera=(), microphone=(), geolocation=()"}}; +} +void SecurityHeaders::apply(Response& res) { + for (const auto& [k, v] : defaults()) res.headers.set(k, v); +} + +CspBuilder::CspBuilder() { + directives_["default-src"] = "'none'"; + directives_["script-src"] = "'self'"; + directives_["style-src"] = "'self'"; + directives_["img-src"] = "'self'"; + directives_["frame-ancestors"] = "'none'"; +} +CspBuilder& CspBuilder::script_nonce(const std::string& nonce) { + directives_["script-src"] = "'self' 'nonce-" + nonce + "'"; + return *this; +} +CspBuilder& CspBuilder::report_uri(const std::string& uri) { + directives_["report-uri"] = uri; + return *this; +} +CspBuilder& CspBuilder::enforce() { + report_only_ = false; + return *this; +} +std::string CspBuilder::header_name() const { + return report_only_ ? "Content-Security-Policy-Report-Only" + : "Content-Security-Policy"; +} +std::string CspBuilder::build() const { + std::string out; + for (const auto& [k, v] : directives_) out += (out.empty() ? "" : "; ") + k + " " + v; + return out; +} + +bool CspReportStore::add(const std::string& body) { + if (body.empty() || body.size() > 4096) return false; + std::lock_guard lock(mu_); + ++total_; + if (reports_.size() >= secaps::kMaxCspReports) reports_.erase(reports_.begin()); + reports_.push_back(body); + return true; +} +std::vector CspReportStore::list() const { + std::lock_guard lock(mu_); + return reports_; +} +std::size_t CspReportStore::total() const { + std::lock_guard lock(mu_); + return total_; +} + +PermissionGuard::PermissionGuard() { + token_role_ = {{"viewer-token", "viewer"}, {"editor-token", "editor"}, + {"admin-token", "admin"}}; + role_perms_["viewer"] = {"doc:read"}; + role_perms_["editor"] = {"doc:read", "doc:write"}; + role_perms_["admin"] = {"doc:read", "doc:write", "admin:manage"}; +} +PermissionGuard::Verdict PermissionGuard::check(const std::string& authorization, + const std::string& permission, + std::string& role) const { + role.clear(); + const std::string prefix = "Bearer "; + if (authorization.compare(0, prefix.size(), prefix) != 0) + return Verdict::Unauthenticated; + auto it = token_role_.find(authorization.substr(prefix.size())); + if (it == token_role_.end()) return Verdict::Unauthenticated; + role = it->second; + return role_perms_.at(role).count(permission) ? Verdict::Allow + : Verdict::Forbidden; +} +std::vector PermissionGuard::permissions(const std::string& role) const { + auto it = role_perms_.find(role); + if (it == role_perms_.end()) return {}; + return {it->second.begin(), it->second.end()}; +} + +SecretsProvider::SecretsProvider() { + values_ = {{"DB_PASSWORD", "demo-db-password-1234"}, + {"API_KEY", "demo-api-key-abcdef"}, + {"SIGNING_KEY", "demo-signing-key-zz"}}; +} +std::vector SecretsProvider::names() const { + std::vector out; + for (const auto& [k, v] : values_) out.push_back(k); + return out; +} +bool SecretsProvider::masked(const std::string& name, std::string& out) const { + auto it = values_.find(name); + if (it == values_.end()) return false; + const std::string& v = it->second; + out = v.substr(0, 2) + std::string(v.size() - 4, '*') + v.substr(v.size() - 2); + return true; +} + +namespace { +std::string lower(std::string s) { + for (auto& c : s) c = static_cast(std::tolower(static_cast(c))); + return s; +} +} // namespace + +std::string SqlSafety::check(const std::string& value) { + std::string v = lower(value); + for (const char* bad : {"--", ";", "/*", "' or ", "\" or ", "union select", + "drop table", "xp_"}) + if (v.find(bad) != std::string::npos) + return std::string("SQL metacharacter or keyword rejected: ") + bad; + return ""; +} +std::string PathGuard::check(const std::string& value) { + std::string v = lower(value); + if (v.find('\0') != std::string::npos) return "NUL byte rejected"; + if (!v.empty() && v[0] == '/') return "absolute path rejected"; + for (const char* bad : {"..", "%2e", "\\"}) + if (v.find(bad) != std::string::npos) + return std::string("traversal sequence rejected: ") + bad; + return ""; +} + +std::string UploadValidator::check(const std::string& filename, + const std::string& bytes) { + if (filename.empty() || filename.size() > 255) return "file name length must be 1 to 255"; + if (filename.find('/') != std::string::npos || + filename.find('\\') != std::string::npos || filename.find("..") != std::string::npos) + return "file name must not contain path parts"; + std::size_t dot = filename.rfind('.'); + if (dot == std::string::npos) return "extension not allowed (use pdf, png, txt)"; + std::string ext = lower(filename.substr(dot + 1)); + if (ext != "pdf" && ext != "png" && ext != "txt") + return "extension not allowed (use pdf, png, txt)"; + if (bytes.size() > kMaxFileBytes) return "file too large (max 1024 bytes)"; + if (bytes.empty()) return "file is empty"; + if (ext == "pdf" && bytes.compare(0, 5, "%PDF-") != 0) + return "content does not match pdf magic bytes"; + if (ext == "png" && bytes.compare(0, 4, "\x89PNG") != 0) + return "content does not match png magic bytes"; + if (ext == "txt") + for (unsigned char c : bytes) + if (c < 9 || (c > 13 && c < 32) || c == 127) + return "txt content contains binary bytes"; + return ""; +} + +} // namespace molo::example::fw diff --git a/example/fw/SecurityExtra.hpp b/example/fw/SecurityExtra.hpp new file mode 100644 index 0000000..507228e --- /dev/null +++ b/example/fw/SecurityExtra.hpp @@ -0,0 +1,201 @@ +#pragma once + +// SecurityExtra framework skeleton for the living example (namespace +// molo::example::fw). Mirrors names and behavior of src/security/{guard, +// crypto,auth,secret,upload} and src/cli/csp in small, readable form. +// Every container has a hard cap. Crypto here is real SHA-256/HMAC but the +// password hasher uses DEMO iteration counts: not for production use. + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Http.hpp" + +namespace molo::example::fw { + +namespace secaps { +inline constexpr std::size_t kMaxDenials = 100; +inline constexpr std::size_t kMaxRateKeys = 1024; +inline constexpr std::size_t kMaxCspReports = 50; +inline constexpr std::size_t kMaxRandomBytes = 64; +inline constexpr std::size_t kMaxHashInput = 4096; +inline constexpr std::size_t kMaxUploadHex = 4096; +inline constexpr std::size_t kMaxInputLen = 512; +inline constexpr int kDemoPbkdf2Rounds = 2000; // real: 600000+ +} // namespace secaps + +std::string to_hex(const std::string& raw); +bool from_hex(const std::string& hex, std::string& raw); +bool constant_time_equal(const std::string& a, const std::string& b); + +// Mirrors src/security/crypto/Sha256.h and Hmac.h. +class Sha256 { + public: + static std::string digest(const std::string& data); // 32 raw bytes + static std::string hex(const std::string& data); +}; + +class Hmac { + public: + static std::string sha256(const std::string& key, const std::string& data); +}; + +// Mirrors SecureRandom.h: /dev/urandom, capped length. +class SecureRandom { + public: + static std::string bytes(std::size_t n); + static std::string token_hex(std::size_t n); +}; + +// Mirrors PasswordHasher.h with PBKDF2-HMAC-SHA256 at demo rounds. +class PasswordHasher { + public: + static std::string hash(const std::string& password); // "pbkdf2$r$salt$dk" + static bool verify(const std::string& password, const std::string& stored); +}; + +// Mirrors guard/DenialSink.h: capped ring of why requests were refused. +struct Denial { + std::string guard; + std::string reason; +}; +class DenialSink { + public: + void record(const std::string& guard, const std::string& reason); + std::vector list() const; + std::size_t total() const; + + private: + mutable std::mutex mu_; + std::vector ring_; + std::size_t total_ = 0; +}; + +// Mirrors guard/CsrfGuard.h: stateless signed token "nonce.mac". +class CsrfGuard { + public: + explicit CsrfGuard(std::string secret) : secret_(std::move(secret)) {} + std::string issue() const; + bool verify(const std::string& token) const; + + private: + std::string secret_; +}; + +// Mirrors guard/CorsPolicy.h: exact-origin allowlist, no wildcard with creds. +class CorsPolicy { + public: + explicit CorsPolicy(std::set origins) + : origins_(std::move(origins)) {} + bool allowed(const std::string& origin) const; + // Fills Access-Control-* headers when the origin is allowed. + bool apply_preflight(const Request& req, Response& res) const; + + private: + std::set origins_; +}; + +// Mirrors guard/RateLimiter.h: fixed window per key, capped key table. +class RateLimiter { + public: + RateLimiter(int limit, int window_seconds) + : limit_(limit), window_(window_seconds) {} + // Returns true when allowed; else sets retry_after (seconds, >= 1). + bool hit(const std::string& key, int& retry_after, int& remaining); + int limit() const { return limit_; } + int window() const { return window_; } + + private: + struct Slot { + std::chrono::steady_clock::time_point start; + int count = 0; + }; + std::mutex mu_; + std::map slots_; + int limit_; + int window_; +}; + +// Mirrors guard/SecurityHeaders.h. +class SecurityHeaders { + public: + static std::vector> defaults(); + static void apply(Response& res); +}; + +// Mirrors src/cli/csp: builder, per-request nonce, capped report store. +class CspBuilder { + public: + CspBuilder(); + CspBuilder& script_nonce(const std::string& nonce); + CspBuilder& report_uri(const std::string& uri); + CspBuilder& enforce(); + std::string header_name() const; + std::string build() const; + + private: + std::map directives_; + bool report_only_ = true; +}; +class CspReportStore { + public: + bool add(const std::string& body); // false when body is empty/too large + std::vector list() const; + std::size_t total() const; + + private: + mutable std::mutex mu_; + std::vector reports_; + std::size_t total_ = 0; +}; + +// Mirrors auth/Permission.h + Guard.h: token -> role -> permission set. +class PermissionGuard { + public: + PermissionGuard(); + enum class Verdict { Allow, Unauthenticated, Forbidden }; + Verdict check(const std::string& authorization, + const std::string& permission, std::string& role) const; + std::vector permissions(const std::string& role) const; + + private: + std::map token_role_; + std::map> role_perms_; +}; + +// Mirrors secret/SecretsProvider.h; values never leave except masked. +class SecretsProvider { + public: + SecretsProvider(); + std::vector names() const; + bool masked(const std::string& name, std::string& out) const; + + private: + std::map values_; +}; + +// Mirrors guard/SqlSafety.h and PathGuard.h: returns "" when safe, else reason. +class SqlSafety { + public: + static std::string check(const std::string& value); +}; +class PathGuard { + public: + static std::string check(const std::string& value); +}; + +// Mirrors upload/UploadValidator.h: ext allowlist, size cap, magic bytes. +class UploadValidator { + public: + static constexpr std::size_t kMaxFileBytes = 1024; + static std::string check(const std::string& filename, + const std::string& bytes); // "" when allowed +}; + +} // namespace molo::example::fw diff --git a/tests/example/audit/log_test.cpp b/tests/example/audit/log_test.cpp new file mode 100644 index 0000000..988501a --- /dev/null +++ b/tests/example/audit/log_test.cpp @@ -0,0 +1,21 @@ +// Route: GET/POST /audit/log (hash-chained log page and append form) +#include "support/Assert.hpp" + +using namespace molo::example::test; +using molo::example::fw::Method; + +int main() { + Fixture f; + auto page = f.client->get("/audit/log"); + check(page.status == 200 && contains(page.headers.get("Content-Type"), "text/html"), "html page"); + check(contains(page.body, "Chain intact"), "intact"); + auto js = f.client->request(Method::Get, "/audit/log", "", {{"Accept", "application/json"}}); + check(contains(js.body, "\"records\""), "json variant"); + auto bad = f.client->post("/audit/log", "actor=&action=x&subject=y", "application/x-www-form-urlencoded"); + check(bad.status == 422 && contains(bad.body, "field-error"), "inline validation"); + auto ok = f.client->post("/audit/log", "actor=ada&action=edit&subject=doc&detail=mail+me%40example.com", "application/x-www-form-urlencoded"); + check(ok.status == 201 && contains(ok.body, "appended"), "appended"); + auto after = f.client->request(Method::Get, "/audit/log", "", {{"Accept", "application/json"}}); + check(contains(after.body, "[EMAIL]") && !contains(after.body, "me@example.com"), "side effect: PII redacted in record"); + return finish("audit/log"); +} diff --git a/tests/example/audit/mask_test.cpp b/tests/example/audit/mask_test.cpp new file mode 100644 index 0000000..2812f44 --- /dev/null +++ b/tests/example/audit/mask_test.cpp @@ -0,0 +1,18 @@ +// Route: GET /audit/mask (masking and redaction patterns) +#include "support/Assert.hpp" + +using namespace molo::example::test; +using molo::example::fw::Method; + +int main() { + Fixture f; + auto d = f.client->get("/audit/mask"); + check(d.status == 200 && contains(d.body, "a***********@example.com"), "email masked"); + check(contains(d.body, "************1111"), "card keeps last 4"); + check(contains(d.body, "[EMAIL]") && contains(d.body, "[SSN]") && contains(d.body, "[IP]"), "redactions"); + check(contains(d.body, "\"redactions\":4"), "counted 4 hits"); + auto c = f.client->get("/audit/mask?text=nothing+here"); + check(contains(c.body, "\"redactions\":0"), "clean text unchanged"); + check(f.client->post("/audit/mask").status == 405, "405"); + return finish("audit/mask"); +} diff --git a/tests/example/audit/retention_test.cpp b/tests/example/audit/retention_test.cpp new file mode 100644 index 0000000..85036ed --- /dev/null +++ b/tests/example/audit/retention_test.cpp @@ -0,0 +1,23 @@ +// Route: GET /audit/retention + hold + purge (policy, legal hold, dry run) +#include "support/Assert.hpp" + +using namespace molo::example::test; +using molo::example::fw::Method; + +int main() { + Fixture f; + auto page = f.client->get("/audit/retention"); + check(page.status == 200 && contains(page.body, "expired"), "html page"); + auto js = f.client->request(Method::Get, "/audit/retention", "", {{"Accept", "application/json"}}); + check(contains(js.body, "\"r1\"") && contains(js.body, "\"r5\""), "expired listed"); + auto dry = f.client->post("/audit/retention/purge", ""); + check(contains(dry.body, "\"dry_run\":true") && contains(dry.body, "\"purged\":0"), "dry run by default"); + check(f.client->post("/audit/retention/hold", "id=r1&hold=true").status == 200, "hold"); + auto real = f.client->post("/audit/retention/purge", "confirm=yes"); + check(contains(real.body, "\"purged\":2"), "held entry skipped, two purged"); + auto after = f.client->request(Method::Get, "/audit/retention", "", {{"Accept", "application/json"}}); + check(contains(after.body, "\"r1\"") && !contains(after.body, "\"id\":\"r5\""), "side effect: r1 kept, r5 gone"); + check(f.client->post("/audit/retention/hold", "id=r1&hold=x").status == 422, "bad hold"); + check(f.client->post("/audit/retention/hold", "id=zz&hold=true").status == 404, "unknown id"); + return finish("audit/retention"); +} diff --git a/tests/example/audit/subject_test.cpp b/tests/example/audit/subject_test.cpp new file mode 100644 index 0000000..d9759a8 --- /dev/null +++ b/tests/example/audit/subject_test.cpp @@ -0,0 +1,21 @@ +// Route: GET /audit/subject, export, POST erase (data subject rights) +#include "support/Assert.hpp" + +using namespace molo::example::test; +using molo::example::fw::Method; + +int main() { + Fixture f; + auto page = f.client->get("/audit/subject"); + check(page.status == 200 && contains(page.headers.get("Content-Type"), "text/html"), "form page"); + auto ex = f.client->get("/audit/subject/export?subject=ada@example.com"); + check(ex.status == 200 && contains(ex.body, "order=1001") && contains(ex.body, "profiles"), "export across tables"); + check(f.client->get("/audit/subject/export").status == 400, "missing subject"); + auto bad = f.client->post("/audit/subject/erase", "subject=nope&confirm=no", "application/x-www-form-urlencoded"); + check(bad.status == 422 && contains(bad.body, "field-error"), "inline errors"); + auto ok = f.client->post("/audit/subject/erase", "subject=ada%40example.com&confirm=yes", "application/x-www-form-urlencoded"); + check(ok.status == 200 && contains(ok.body, "4 rows erased"), "cascade erased 4 rows"); + check(!contains(f.client->get("/audit/subject/export?subject=ada@example.com").body, "order="), "side effect: data gone"); + check(contains(f.client->get("/audit/verify").body, "\"intact\":true"), "erasure audited, chain intact"); + return finish("audit/subject"); +} diff --git a/tests/example/audit/verify_test.cpp b/tests/example/audit/verify_test.cpp new file mode 100644 index 0000000..0aa6c83 --- /dev/null +++ b/tests/example/audit/verify_test.cpp @@ -0,0 +1,18 @@ +// Route: GET /audit/verify + POST /audit/tamper (tamper detection) +#include "support/Assert.hpp" + +using namespace molo::example::test; +using molo::example::fw::Method; + +int main() { + Fixture f; + auto v = f.client->get("/audit/verify"); + check(v.status == 200 && contains(v.body, "\"intact\":true") && contains(v.body, "\"first_broken\":null"), "intact"); + check(f.client->post("/audit/tamper", "index=abc").status == 422, "bad index"); + check(f.client->post("/audit/tamper", "index=99").status == 404, "missing record"); + check(f.client->post("/audit/tamper", "index=1").status == 200, "tamper"); + auto b = f.client->get("/audit/verify"); + check(contains(b.body, "\"intact\":false") && contains(b.body, "\"first_broken\":1"), "first broken index reported"); + check(contains(f.client->get("/audit/log").body, "Chain broken at index 1"), "side effect: page shows break"); + return finish("audit/verify"); +} diff --git a/tests/example/cli/commands_test.cpp b/tests/example/cli/commands_test.cpp new file mode 100644 index 0000000..7f3e318 --- /dev/null +++ b/tests/example/cli/commands_test.cpp @@ -0,0 +1,20 @@ +// Route: GET /cli/commands (real molo command reference) +#include "support/Assert.hpp" + +using namespace molo::example::test; +using molo::example::fw::Method; + +int main() { + Fixture f; + auto res = f.client->get("/cli/commands"); + check(res.status == 200 && contains(res.headers.get("Content-Type"), "text/html"), "html page"); + for (const char* c : {"new", "serve", "build", "embed", "migrate", "health", "supervisor", "task", + "ai", "expose", "verify", "supply-check", "release", "rollout", "rollback", + "experiment", "debug"}) + check(contains(res.body, std::string("molo ") + c + ""), std::string("lists ") + c); + auto js = f.client->request(Method::Get, "/cli/commands", "", {{"Accept", "application/json"}}); + check(contains(js.body, "\"commands\""), "json for json clients"); + check(f.services.features.size() > 0, "side effect: feature rows registered"); + check(f.client->request(Method::Post, "/cli/commands").status == 405, "post is 405"); + return finish("clicmd/commands"); +} diff --git a/tests/example/clients/retry_test.cpp b/tests/example/clients/retry_test.cpp new file mode 100644 index 0000000..98dab34 --- /dev/null +++ b/tests/example/clients/retry_test.cpp @@ -0,0 +1,24 @@ +// Route: /clients/retry (capped backoff, 4xx never retried) +#include "support/Assert.hpp" + +using namespace molo::example::test; +using molo::example::fw::Method; + +static const char* kForm = "application/x-www-form-urlencoded"; + +int main() { + Fixture f; + check(f.client->get("/clients/retry").status == 200, "status 200"); + auto ok = f.client->post("/clients/retry/run", "failures=2&max_attempts=3&status=503", kForm); + check(contains(ok.body, "final status 200 after 3 attempt(s)"), "succeeds on third attempt"); + check(contains(ok.body, "backoff 100ms") && contains(ok.body, "backoff 200ms"), "doubling backoff"); + auto cap = f.client->post("/clients/retry/run", "failures=10&max_attempts=5&status=503", kForm); + check(contains(cap.body, "final status 503 after 5 attempt(s)"), "gives up at the attempt cap"); + check(contains(cap.body, "backoff 800ms"), "backoff capped at 800ms"); + auto c4 = f.client->post("/clients/retry/run", "failures=3&max_attempts=5&status=404", kForm); + check(contains(c4.body, "after 1 attempt(s)"), "404 is not retried"); + auto bad = f.client->post("/clients/retry/run", "failures=1&max_attempts=9&status=200", kForm); + check(bad.status == 422 && contains(bad.body, "must be between 1 and 5"), "422 attempts above hard cap"); + check(contains(f.client->get("/clients/retry").body, "runs"), "side effect: runs counted"); + return finish("clients/retry"); +} diff --git a/tests/example/clients/session_test.cpp b/tests/example/clients/session_test.cpp new file mode 100644 index 0000000..4c92556 --- /dev/null +++ b/tests/example/clients/session_test.cpp @@ -0,0 +1,27 @@ +// Route: /clients/session (cookie jar, pool, proxy, auth) +#include "support/Assert.hpp" + +using namespace molo::example::test; +using molo::example::fw::Method; + +static const char* kForm = "application/x-www-form-urlencoded"; + +int main() { + Fixture f; + check(f.client->get("/clients/session").status == 200, "status 200"); + auto r1 = f.client->post("/clients/session/request", "auth=basic", kForm); + check(contains(r1.body, "Authorization=Basic ZGVtbzpkZW1v"), "basic auth header"); + check(!contains(r1.body, "Cookie="), "first request has no cookie"); + auto r2 = f.client->post("/clients/session/request", "auth=bearer", kForm); + check(contains(r2.body, "Cookie=sid=s-1; visits=1"), "cookie jar replays server cookies"); + check(contains(r2.body, "Bearer demo-token"), "bearer header"); + check(contains(r2.body, "connection reused"), "pooled connection reused"); + auto r3 = f.client->post("/clients/session/request", "proxy=http://proxy.local:3128", kForm); + check(contains(r3.body, "connection created via http://proxy.local:3128"), "proxy change opens new connection"); + auto bad = f.client->post("/clients/session/request", "proxy=ftp://x", kForm); + check(bad.status == 422 && contains(bad.body, "socks5://host:port"), "422 bad proxy"); + auto badauth = f.client->post("/clients/session/request", "auth=digest", kForm); + check(badauth.status == 422, "422 unknown auth"); + check(contains(f.client->get("/clients/session").body, "pool_created"), "side effect: pool stats shown"); + return finish("clients/session"); +} diff --git a/tests/example/compress/demo_test.cpp b/tests/example/compress/demo_test.cpp new file mode 100644 index 0000000..cf6305f --- /dev/null +++ b/tests/example/compress/demo_test.cpp @@ -0,0 +1,25 @@ +// Route: GET /compress/demo (gzip negotiated by Accept-Encoding) +#include "support/Assert.hpp" + +using namespace molo::example::test; +using molo::example::fw::Method; + +int main() { + Fixture f; + auto plain = f.client->get("/compress/demo?size=1000"); + check(plain.status == 200 && plain.body.size() == 1000, "identity body"); + check(!plain.headers.has("Content-Encoding"), "no encoding without header"); + auto gz = f.client->request(Method::Get, "/compress/demo?size=1000", "", {{"Accept-Encoding", "br, gzip;q=0.8"}}); + check(gz.headers.get("Content-Encoding") == "gzip", "content-encoding gzip"); + check(gz.body.size() > 2 && (unsigned char)gz.body[0] == 0x1f && (unsigned char)gz.body[1] == 0x8b, "gzip magic"); + check(gz.headers.get("Vary") == "Accept-Encoding", "vary"); + check(gz.headers.get("X-Original-Length") == "1000", "original length header"); + auto refused = f.client->request(Method::Get, "/compress/demo?size=1000", "", {{"Accept-Encoding", "gzip;q=0"}}); + check(!refused.headers.has("Content-Encoding"), "q=0 refuses gzip"); + auto tiny = f.client->request(Method::Get, "/compress/demo?size=10", "", {{"Accept-Encoding", "gzip"}}); + check(!tiny.headers.has("Content-Encoding"), "tiny bodies stay identity"); + check(f.client->get("/compress/demo?size=x").status == 422, "bad size 422"); + check(f.client->get("/compress/demo?size=9999999").status == 422, "size cap 422"); + check(f.client->request(Method::Post, "/compress/demo").status == 405, "post is 405"); + return finish("compress/demo"); +} diff --git a/tests/example/conditional/doc_test.cpp b/tests/example/conditional/doc_test.cpp new file mode 100644 index 0000000..bd626ad --- /dev/null +++ b/tests/example/conditional/doc_test.cpp @@ -0,0 +1,34 @@ +// Route: GET/POST /conditional/doc (ETag, 304, Range, If-Match) +#include "support/Assert.hpp" + +using namespace molo::example::test; +using molo::example::fw::Method; + +int main() { + Fixture f; + auto full = f.client->get("/conditional/doc"); + std::string etag = full.headers.get("ETag"); + check(full.status == 200 && !etag.empty(), "200 with etag"); + check(full.headers.get("Accept-Ranges") == "bytes", "accept-ranges"); + auto nm = f.client->request(Method::Get, "/conditional/doc", "", {{"If-None-Match", etag}}); + check(nm.status == 304 && nm.body.empty(), "304 on matching etag"); + auto weak = f.client->request(Method::Get, "/conditional/doc", "", {{"If-None-Match", "W/" + etag}}); + check(weak.status == 304, "weak tag matches"); + auto part = f.client->request(Method::Get, "/conditional/doc", "", {{"Range", "bytes=0-9"}}); + check(part.status == 206 && part.body == full.body.substr(0, 10), "206 partial body"); + check(part.headers.get("Content-Range") == "bytes 0-9/" + std::to_string(full.body.size()), "content-range"); + auto suffix = f.client->request(Method::Get, "/conditional/doc", "", {{"Range", "bytes=-4"}}); + check(suffix.body == full.body.substr(full.body.size() - 4), "suffix range"); + auto bad = f.client->request(Method::Get, "/conditional/doc", "", {{"Range", "bytes=99999-"}}); + check(bad.status == 416, "416 unsatisfiable"); + auto stale = f.client->request(Method::Post, "/conditional/doc", "new text", {{"If-Match", "\"nope\""}}); + check(stale.status == 412, "412 on stale If-Match"); + auto ok = f.client->request(Method::Post, "/conditional/doc", "new text", {{"If-Match", etag}}); + check(ok.status == 200, "update with matching If-Match"); + auto after = f.client->get("/conditional/doc"); + check(after.body == "new text" && after.headers.get("ETag") != etag, "side effect: new body and etag"); + check(f.client->request(Method::Post, "/conditional/doc", "").status == 422, "empty body 422"); + check(f.client->request(Method::Post, "/conditional/doc", std::string(5000, 'x')).status == 413, "over cap 413"); + check(f.client->request(Method::Delete, "/conditional/doc").status == 405, "delete is 405"); + return finish("conditional/doc"); +} diff --git a/tests/example/context/trace_test.cpp b/tests/example/context/trace_test.cpp new file mode 100644 index 0000000..2cf73ab --- /dev/null +++ b/tests/example/context/trace_test.cpp @@ -0,0 +1,25 @@ +// Route: GET /context/trace (traceparent continuation, correlation id) +#include "support/Assert.hpp" + +using namespace molo::example::test; +using molo::example::fw::Method; + +int main() { + Fixture f; + std::string tid = "4bf92f3577b34da6a3ce929d0e0e4736"; + auto cont = f.client->request(Method::Get, "/context/trace", "", + {{"traceparent", "00-" + tid + "-00f067aa0ba902b7-01"}, {"X-Correlation-ID", "corr-9"}}); + check(cont.status == 200, "status 200"); + check(contains(cont.body, "\"trace_id\":\"" + tid), "trace id continued"); + check(contains(cont.body, "\"parent_span_id\":\"00f067aa0ba902b7\""), "parent span reported"); + check(cont.headers.get("traceparent").rfind("00-" + tid + "-", 0) == 0, "response traceparent same trace"); + check(!contains(cont.headers.get("traceparent"), "00f067aa0ba902b7"), "new span id"); + check(cont.headers.get("X-Correlation-ID") == "corr-9", "correlation propagated"); + auto fresh = f.client->get("/context/trace"); + check(contains(fresh.body, "\"continued\":false") && contains(fresh.body, "\"parent_valid\":true"), "new trace"); + check(fresh.headers.get("X-Correlation-ID").rfind("req-", 0) == 0, "falls back to request id"); + auto bad = f.client->request(Method::Get, "/context/trace", "", {{"traceparent", "garbage"}}); + check(contains(bad.body, "\"parent_valid\":false") && contains(bad.body, "\"continued\":false"), "invalid parent restarts trace"); + check(f.client->request(Method::Post, "/context/trace").status == 405, "post is 405"); + return finish("context/trace"); +} diff --git a/tests/example/crypto/hash_test.cpp b/tests/example/crypto/hash_test.cpp new file mode 100644 index 0000000..b128f23 --- /dev/null +++ b/tests/example/crypto/hash_test.cpp @@ -0,0 +1,19 @@ +// Route: /crypto/hash (real SHA-256 and HMAC-SHA256) +#include "support/Assert.hpp" +using namespace molo::example::test; +static const char* kForm = "application/x-www-form-urlencoded"; +int main() { + Fixture f; + check(contains(f.client->get("/crypto/hash").body, "post("/crypto/hash", "text=abc", kForm); + check(r.status == 200, "status 200"); + check(contains(r.body, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"), "sha256(abc) vector"); + auto m = f.client->post("/crypto/hash", "text=what+do+ya+want+for+nothing%3F&key=Jefe", kForm); + check(contains(m.body, "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843"), "RFC 4231 hmac vector"); + auto bad = f.client->post("/crypto/hash", "text=", kForm); + check(bad.status == 422 && contains(bad.body, "form-errors"), "empty text inline 422"); + auto big = f.client->post("/crypto/hash", "text=" + std::string(5000, 'a'), kForm); + check(big.status == 422, "over cap 422"); + check(f.client->request(molo::example::fw::Method::Put, "/crypto/hash").status == 405, "PUT is 405"); + return finish("crypto/hash"); +} diff --git a/tests/example/crypto/password_test.cpp b/tests/example/crypto/password_test.cpp new file mode 100644 index 0000000..0a154be --- /dev/null +++ b/tests/example/crypto/password_test.cpp @@ -0,0 +1,18 @@ +// Route: /crypto/password (salted PBKDF2, demo rounds) +#include "support/Assert.hpp" +using namespace molo::example::test; +static const char* kForm = "application/x-www-form-urlencoded"; +int main() { + Fixture f; + check(contains(f.client->get("/crypto/password").body, "DEMO"), "labelled demo"); + auto a = f.client->post("/crypto/password", "password=correct-horse", kForm); + auto b = f.client->post("/crypto/password", "password=correct-horse", kForm); + check(a.status == 200 && contains(a.body, "pbkdf2-demo$2000$"), "hash format"); + check(contains(a.body, "verify ok: yes") && contains(a.body, "wrong password: rejected"), "verify works"); + auto salt = [](const std::string& s) { std::size_t p = s.find("pbkdf2-demo$2000$"); return s.substr(p + 17, 32); }; + check(salt(a.body) != salt(b.body), "salt differs per hash"); + auto bad = f.client->post("/crypto/password", "password=short", kForm); + check(bad.status == 422 && contains(bad.body, "must be 8 to 128"), "short password inline 422"); + check(f.client->post("/crypto/password", "password=" + std::string(129, 'a'), kForm).status == 422, "over cap 422"); + return finish("crypto/password"); +} diff --git a/tests/example/crypto/random_test.cpp b/tests/example/crypto/random_test.cpp new file mode 100644 index 0000000..981db35 --- /dev/null +++ b/tests/example/crypto/random_test.cpp @@ -0,0 +1,15 @@ +// Route: GET /crypto/random (urandom token, 1 to 64 bytes) +#include "support/Assert.hpp" +using namespace molo::example::test; +int main() { + Fixture f; + auto a = f.client->get("/crypto/random?bytes=8"); + auto b = f.client->get("/crypto/random?bytes=8"); + check(a.status == 200 && contains(a.body, "\"bytes\":8"), "8 bytes ok"); + check(a.body != b.body, "tokens differ"); + check(f.client->get("/crypto/random").status == 200, "default ok"); + check(f.client->get("/crypto/random?bytes=65").status == 400, "over cap 400"); + check(f.client->get("/crypto/random?bytes=abc").status == 400, "non numeric 400"); + check(f.client->post("/crypto/random", "x").status == 405, "POST 405"); + return finish("crypto/random"); +} diff --git a/tests/example/csp/page_test.cpp b/tests/example/csp/page_test.cpp new file mode 100644 index 0000000..26567e0 --- /dev/null +++ b/tests/example/csp/page_test.cpp @@ -0,0 +1,24 @@ +// Route: GET /csp/page (per-request nonce in header and script tag) +#include "support/Assert.hpp" +using namespace molo::example::test; +static std::string nonce_of(const std::string& h) { + std::size_t a = h.find("'nonce-"); + if (a == std::string::npos) return ""; + a += 7; + return h.substr(a, h.find('\'', a) - a); +} +int main() { + Fixture f; + auto a = f.client->get("/csp/page"); + auto b = f.client->get("/csp/page"); + check(a.status == 200 && contains(a.content_type(), "text/html"), "html 200"); + std::string h = a.headers.get("Content-Security-Policy-Report-Only"); + check(contains(h, "default-src 'none'"), "deny-first default"); + check(contains(h, "report-uri /csp/report"), "report uri set"); + std::string n = nonce_of(h); + check(n.size() == 32, "nonce is 16 random bytes in hex"); + check(contains(a.body, "