#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