Files
moloapp/example/fw/Enterprise.hpp
T
retoorandClaude Sonnet 5.5 62fdd08da8 feat(workflow): example covers security, platform, enterprise and resilience features
105 route tests, matrix crawl, spec and README updated.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-24 02:19:52 +02:00

270 lines
8.0 KiB
C++

#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 <cstdint>
#include <map>
#include <optional>
#include <set>
#include <string>
#include <vector>
#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<Tenant> list() const;
private:
std::map<std::string, Tenant> 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<std::string> list(const std::string& tenant) const;
private:
std::map<std::string, std::vector<std::string>> 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<std::string, std::size_t> 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<std::string, bool> effective(const std::string& tenant) const;
private:
std::map<std::string, bool> defaults_;
std::map<std::string, bool> 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<std::string, std::string> effective(const std::string& tenant) const;
bool known(const std::string& key) const;
private:
std::map<std::string, std::string> base_;
std::map<std::string, std::map<std::string, std::string>> overlay_;
};
class TenantMetrics {
public:
void inc(const std::string& tenant, const std::string& name);
std::map<std::string, std::map<std::string, std::uint64_t>> all() const;
private:
std::map<std::string, std::map<std::string, std::uint64_t>> 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<std::size_t> verify() const;
bool tamper(std::size_t index); // test hook: flips one detail
const std::vector<AuditRecord>& records() const { return records_; }
private:
static std::string hash_of(const AuditRecord& r);
std::vector<AuditRecord> 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<std::string> expired() const;
std::size_t purge();
const std::vector<RetentionEntry>& entries() const { return entries_; }
private:
std::vector<RetentionEntry> entries_;
};
class SubjectStore {
public:
SubjectStore();
// table -> list of rows for subject
std::map<std::string, std::vector<std::string>> 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<std::string, std::vector<std::pair<std::string, std::string>>> tables_;
};
// ---- API governance (src/governance) -------------------------------------
struct ApiKeyInfo {
std::string id, owner, tier, hash;
std::set<std::string> 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<std::string>& 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<ApiKeyInfo> 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<std::string, std::size_t> 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<ApiVersion>& all() const { return versions_; }
private:
std::vector<ApiVersion> versions_;
};
struct LintFinding {
std::string path, rule;
};
class RouteLinter {
public:
static constexpr std::size_t kMaxPaths = 100;
static std::vector<LintFinding> lint(const std::vector<std::string>& 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<DiffFinding> diff(const std::string& before,
const std::string& after);
};
} // namespace molo::example::fw