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>
This commit is contained in:
@@ -0,0 +1,544 @@
|
||||
#include "Enterprise.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
|
||||
namespace molo::example::fw {
|
||||
|
||||
namespace {
|
||||
|
||||
std::uint32_t rotr(std::uint32_t x, int n) { return (x >> n) | (x << (32 - n)); }
|
||||
|
||||
std::vector<std::string> split(const std::string& s, char sep) {
|
||||
std::vector<std::string> 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<std::uint64_t>(data.size()) * 8;
|
||||
msg += static_cast<char>(0x80);
|
||||
while (msg.size() % 64 != 56) msg += '\0';
|
||||
for (int i = 7; i >= 0; --i) msg += static_cast<char>((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<std::uint32_t>(static_cast<unsigned char>(msg[off + i * 4])) << 24) |
|
||||
(static_cast<std::uint32_t>(static_cast<unsigned char>(msg[off + i * 4 + 1])) << 16) |
|
||||
(static_cast<std::uint32_t>(static_cast<unsigned char>(msg[off + i * 4 + 2])) << 8) |
|
||||
static_cast<std::uint32_t>(static_cast<unsigned char>(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<Tenant> TenantCatalog::list() const {
|
||||
std::vector<Tenant> 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<std::string> TenantData::list(const std::string& tenant) const {
|
||||
auto it = rows_.find(tenant);
|
||||
return it == rows_.end() ? std::vector<std::string>{} : 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<std::string, bool> FeatureFlags::effective(const std::string& tenant) const {
|
||||
std::map<std::string, bool> 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<std::string, std::string> 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<std::string, std::map<std::string, std::uint64_t>> 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<std::size_t> 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<std::pair<std::regex, std::string>> 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::size_t>(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<std::string> RetentionStore::expired() const {
|
||||
std::vector<std::string> 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<std::string, std::vector<std::string>> SubjectStore::export_for(
|
||||
const std::string& subject) const {
|
||||
std::map<std::string, std::vector<std::string>> 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<std::string>& 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<LintFinding> RouteLinter::lint(const std::vector<std::string>& paths) {
|
||||
static const std::vector<std::string> verbs = {"get", "create", "delete", "update",
|
||||
"fetch", "remove"};
|
||||
std::vector<LintFinding> 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<std::string> required, optional;
|
||||
};
|
||||
|
||||
std::map<std::string, Op> parse_snapshot(const std::string& s) {
|
||||
std::map<std::string, Op> 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<DiffFinding> SpecDiffer::diff(const std::string& before,
|
||||
const std::string& after) {
|
||||
auto a = parse_snapshot(before);
|
||||
auto b = parse_snapshot(after);
|
||||
std::vector<DiffFinding> 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
|
||||
Reference in New Issue
Block a user