feat: implement MoloApp plans 01-15 with integration
Full C++23 framework tree: runtime, observability, security, data, app, realtime, supervisor, clients, CLI, example plus enterprise tenancy, audit, operability, governance, supplychain. Postgres via docker compose for tests.
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
// chain_tamper: 10k chained records verify clean; a 1-byte tamper names the
|
||||
// exact seq; a dropped record reports a gap; seals round-trip under HMAC.
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "audit/log/appender.h"
|
||||
#include "audit/log/chain.h"
|
||||
#include "audit/log/store.h"
|
||||
|
||||
#define CHECK(c, msg) \
|
||||
if (!(c)) { \
|
||||
std::cerr << "FAIL chain_tamper: " << msg << "\n"; \
|
||||
return 1; \
|
||||
}
|
||||
|
||||
int main() {
|
||||
using namespace molo::audit::log;
|
||||
MemoryStore store;
|
||||
Appender app(store);
|
||||
for (int i = 0; i < 10000; ++i) {
|
||||
Appender::Write w;
|
||||
w.actor = "tester";
|
||||
w.action = "orm.update";
|
||||
w.table = "users";
|
||||
w.pk = std::to_string(i);
|
||||
w.op = "update";
|
||||
w.after_json = "{\"i\":" + std::to_string(i) + "}";
|
||||
w.tenant = "t1";
|
||||
app.Append(std::move(w));
|
||||
}
|
||||
CHECK(store.LatestSeq() == 10000, "10k appended");
|
||||
CHECK(app.appended() == 10000, "appended counter");
|
||||
CHECK(store.Seals().size() == 10, "seal every 1000 records");
|
||||
|
||||
std::vector<AuditRecord> all;
|
||||
for (std::uint64_t s = 1; s <= 10000;) {
|
||||
auto page = store.ReadRange(s, 1000);
|
||||
CHECK(!page.empty(), "page nonempty");
|
||||
for (auto& r : page) all.push_back(r);
|
||||
s = all.back().seq + 1;
|
||||
}
|
||||
VerifyResult ok = Chain::Verify(all);
|
||||
CHECK(ok.ok, "clean chain verifies");
|
||||
|
||||
std::vector<AuditRecord> tampered = all;
|
||||
tampered[4999].after_json[5] ^= 0x01; // 1-byte flip at seq 5000
|
||||
VerifyResult bad = Chain::Verify(tampered);
|
||||
CHECK(!bad.ok && bad.bad_seq == 5000, "tamper names seq 5000");
|
||||
|
||||
std::vector<AuditRecord> gapped = all;
|
||||
gapped.erase(gapped.begin() + 1234); // drop seq 1235
|
||||
VerifyResult gap = Chain::Verify(gapped);
|
||||
CHECK(!gap.ok && gap.reason == "gap" && gap.bad_seq == 1235, "gap names seq 1235");
|
||||
|
||||
Seal s = Chain::MakeSeal(1, store.Tip(), 1700000000, "audit-key");
|
||||
CHECK(Chain::CheckSeal(s, "audit-key"), "seal verifies");
|
||||
CHECK(!Chain::CheckSeal(s, "wrong-key"), "seal rejects wrong key");
|
||||
std::cout << "PASS chain_tamper\n";
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
// erasure_cascade: all seven stores run, held subjects erase nothing,
|
||||
// exempted subjects report Exempted, audit index keeps erased:<req> marker,
|
||||
// and the audit chain stays valid throughout.
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include "audit/log/appender.h"
|
||||
#include "audit/log/chain.h"
|
||||
#include "audit/log/store.h"
|
||||
#include "audit/rights/eraser.h"
|
||||
|
||||
#define CHECK(c, msg) \
|
||||
if (!(c)) { \
|
||||
std::cerr << "FAIL erasure_cascade: " << msg << "\n"; \
|
||||
return 1; \
|
||||
}
|
||||
|
||||
int main() {
|
||||
using namespace molo::audit::rights;
|
||||
using molo::audit::log::Appender;
|
||||
using molo::audit::log::Chain;
|
||||
using molo::audit::log::MemoryStore;
|
||||
using molo::audit::retention::Hold;
|
||||
|
||||
MemoryStore store;
|
||||
Appender app(store);
|
||||
RightsRequest req;
|
||||
req.id = "dsar-9";
|
||||
req.kind = RequestKind::kErasure;
|
||||
req.subject = Subject::User("t1", "42");
|
||||
|
||||
std::map<std::string, int> stock = {{"sessions", 2}, {"cache_tags", 3}, {"kv_prefix", 5},
|
||||
{"orm_rows", 4}, {"uploads", 1}, {"tasks", 2}};
|
||||
std::string audit_index = "user:42";
|
||||
CascadeDeleters d;
|
||||
auto mk = [&](const std::string& name) {
|
||||
return [&, name](const Subject&) -> std::uint64_t {
|
||||
std::uint64_t n = stock[name];
|
||||
stock[name] = 0;
|
||||
return n;
|
||||
};
|
||||
};
|
||||
d.sessions = mk("sessions");
|
||||
d.cache_tags = mk("cache_tags");
|
||||
d.kv_prefix = mk("kv_prefix");
|
||||
d.orm_rows = mk("orm_rows");
|
||||
d.uploads = mk("uploads");
|
||||
d.tasks = mk("tasks");
|
||||
d.audit_index = [&](const Subject&, const std::string& id) { audit_index = "erased:" + id; };
|
||||
|
||||
Hold hold;
|
||||
EraseReport r = Eraser::Erase(req, hold, d);
|
||||
CHECK(r.outcome == EraseOutcome::kDone, "outcome done");
|
||||
CHECK(r.erased_total == 17, "all stores erased");
|
||||
CHECK(r.per_store.size() == 6, "six store counts");
|
||||
CHECK(audit_index == "erased:dsar-9", "audit index marker");
|
||||
for (auto& [_, left] : stock) CHECK(left == 0, "rescan zero left");
|
||||
|
||||
Appender::Write w;
|
||||
w.actor = "dsar";
|
||||
w.action = "rights.erasure";
|
||||
w.table = "subjects";
|
||||
w.pk = "user:42";
|
||||
w.op = "erase";
|
||||
w.tenant = "t1";
|
||||
app.Append(std::move(w));
|
||||
CHECK(Chain::Verify(store.ReadRange(1, 100)).ok, "chain valid after erasure");
|
||||
|
||||
stock["orm_rows"] = 4;
|
||||
Hold hold2;
|
||||
hold2.Add(Hold::SubjectKey("t1", "user:42"), "court order");
|
||||
EraseReport h = Eraser::Erase(req, hold2, d);
|
||||
CHECK(h.outcome == EraseOutcome::kHeld && h.erased_total == 0, "held erases nothing");
|
||||
CHECK(stock["orm_rows"] == 4, "held rows untouched");
|
||||
|
||||
d.has_exemption = [](const Subject&) { return true; };
|
||||
EraseReport x = Eraser::Erase(req, Hold{}, d);
|
||||
CHECK(x.outcome == EraseOutcome::kExempted, "exemption reported");
|
||||
std::cout << "PASS erasure_cascade\n";
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// export_bundle: manifest schema, per-chunk checksums, byte determinism
|
||||
// regardless of input row order.
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "audit/rights/exporter.h"
|
||||
|
||||
#define CHECK(c, msg) \
|
||||
if (!(c)) { \
|
||||
std::cerr << "FAIL export_bundle: " << msg << "\n"; \
|
||||
return 1; \
|
||||
}
|
||||
|
||||
static molo::audit::rights::ExportBundle Run(const std::vector<molo::audit::rights::ExportRow>& in,
|
||||
const molo::audit::rights::RightsRequest& req) {
|
||||
using namespace molo::audit::rights;
|
||||
std::size_t i = 0;
|
||||
return Exporter::Build(req, [&](ExportRow& out) -> bool {
|
||||
if (i >= in.size()) return false;
|
||||
out = in[i++];
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
static std::string Sha(const std::string& s) {
|
||||
return molo::security::crypto::Sha256::hex(molo::security::crypto::Sha256::hash(s));
|
||||
}
|
||||
|
||||
int main() {
|
||||
using namespace molo::audit::rights;
|
||||
RightsRequest req;
|
||||
req.id = "dsar-1";
|
||||
req.subject = Subject::User("t1", "42");
|
||||
|
||||
std::vector<ExportRow> rows = {{"users", "42", "{\"name\":\"ada\"}"},
|
||||
{"orders", "7", "{\"total\":9}"},
|
||||
{"orders", "3", "{\"total\":1}"}};
|
||||
ExportBundle b = Run(rows, req);
|
||||
CHECK(b.rows == 3, "row count");
|
||||
CHECK(b.manifest_json.find("\"request\":\"dsar-1\"") != std::string::npos, "manifest id");
|
||||
CHECK(b.manifest_json.find("\"rows\":3") != std::string::npos, "manifest rows");
|
||||
CHECK(b.manifest_json.find("\"sha256\":\"" + b.bundle_sha256 + "\"") != std::string::npos,
|
||||
"manifest checksum");
|
||||
std::string hashes;
|
||||
for (auto& c : b.chunks) {
|
||||
CHECK(Sha(c.bytes) == c.sha256, "chunk checksum verifies");
|
||||
hashes += c.sha256;
|
||||
}
|
||||
CHECK(Sha(hashes) == b.bundle_sha256, "bundle checksum verifies");
|
||||
|
||||
std::vector<ExportRow> shuffled = {rows[2], rows[0], rows[1]};
|
||||
ExportBundle b2 = Run(shuffled, req);
|
||||
CHECK(b2.bundle_sha256 == b.bundle_sha256, "deterministic across input order");
|
||||
CHECK(b2.chunks.size() == b.chunks.size() && b2.chunks[0].bytes == b.chunks[0].bytes,
|
||||
"deterministic bytes");
|
||||
std::cout << "PASS export_bundle\n";
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
// mask_fixture: multi-chunk scrub (PII straddling the 1MB boundary) leaves
|
||||
// zero residuals, masks are deterministic per seed, join keys stay stable.
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "audit/mask/fixture.h"
|
||||
#include "audit/mask/masker.h"
|
||||
|
||||
#define CHECK(c, msg) \
|
||||
if (!(c)) { \
|
||||
std::cerr << "FAIL mask_fixture: " << msg << "\n"; \
|
||||
return 1; \
|
||||
}
|
||||
|
||||
int main() {
|
||||
using namespace molo::audit::mask;
|
||||
Masker m("seed-1");
|
||||
CHECK(m.Mask("ada@example.com", molo::audit::pii::PiiClass::kEmail) ==
|
||||
m.Mask("ada@example.com", molo::audit::pii::PiiClass::kEmail),
|
||||
"mask deterministic");
|
||||
CHECK(m.Mask("a", molo::audit::pii::PiiClass::kName) !=
|
||||
m.Mask("b", molo::audit::pii::PiiClass::kName),
|
||||
"mask differs per value");
|
||||
CHECK(m.Mask("x", molo::audit::pii::PiiClass::kName) !=
|
||||
Masker("seed-2").Mask("x", molo::audit::pii::PiiClass::kName),
|
||||
"mask differs per seed");
|
||||
CHECK(m.JoinKey("user:42") == m.JoinKey("user:42"), "join key stable");
|
||||
|
||||
// 2.5MB blob with PII placed to straddle the 1MB chunk boundary.
|
||||
std::string blob;
|
||||
blob.reserve(3 * 1024 * 1024);
|
||||
std::string pad(molo::audit::kFixtureChunkBytes - 10, '~');
|
||||
blob += pad + "ada@example.com" + pad + "card 4111 1111 1111 1111" + pad;
|
||||
std::string scrubbed;
|
||||
ScrubStats st = Fixture::Scrub(blob, m, [&](std::string_view piece) { scrubbed += piece; });
|
||||
CHECK(st.chunks == 4, "four chunks streamed");
|
||||
CHECK(st.masked >= 2, "both PII hits masked");
|
||||
CHECK(scrubbed.find("ada@example.com") == std::string::npos, "no raw email left");
|
||||
CHECK(scrubbed.find("4111") == std::string::npos, "no raw card left");
|
||||
CHECK(Fixture::Rescan(scrubbed) == 0, "rescan zero");
|
||||
|
||||
std::string again;
|
||||
Fixture::Scrub(blob, Masker("seed-1"), [&](std::string_view p) { again += p; });
|
||||
CHECK(again == scrubbed, "scrub deterministic");
|
||||
std::cout << "PASS mask_fixture\n";
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
// orm_hook: Model save()/remove() reach the audit Appender through the
|
||||
// PATCHES section 2 hook. Without an installed Appender the hook no-ops;
|
||||
// with one installed, insert/update/delete append self-describing records.
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "audit/log/appender.h"
|
||||
#include "audit/log/store.h"
|
||||
#include "data/orm/model.hpp"
|
||||
|
||||
#define CHECK(c, msg) \
|
||||
if (!(c)) { \
|
||||
std::cerr << "FAIL orm_hook: " << msg << "\n"; \
|
||||
return 1; \
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
using molo::data::orm::FieldDef;
|
||||
using molo::data::orm::Model;
|
||||
|
||||
struct Item : public Model<Item> {
|
||||
static std::string table_name() { return "items"; }
|
||||
static std::string primary_key() { return "id"; }
|
||||
static std::vector<FieldDef> fields() {
|
||||
return {FieldDef::primary_key("id"), FieldDef::text("name")};
|
||||
}
|
||||
};
|
||||
|
||||
class FakeConn : public molo::data::Connection {
|
||||
public:
|
||||
molo::data::Backend backend() const override { return molo::data::Backend::Sqlite; }
|
||||
std::string backend_id() const override { return "fake"; }
|
||||
molo::data::Result exec(const molo::data::Context&,
|
||||
const molo::data::BoundStatement&) override {
|
||||
return molo::data::Result::command(0);
|
||||
}
|
||||
molo::data::Prepared prepare(const molo::data::Context&,
|
||||
const molo::data::BoundStatement&) override {
|
||||
return {};
|
||||
}
|
||||
molo::data::Result exec_prepared(const molo::data::Context&, const molo::data::Prepared&,
|
||||
const std::vector<molo::data::Value>&) override {
|
||||
return molo::data::Result::command(0);
|
||||
}
|
||||
void ping(const molo::data::Context&) override {}
|
||||
void close() override {}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
using namespace molo::audit::log;
|
||||
FakeConn conn;
|
||||
molo::data::Context ctx("req-1", "t1", 30);
|
||||
|
||||
// Without an installed Appender the hook no-ops (no crash, no store).
|
||||
{
|
||||
Item it;
|
||||
it.set("id", molo::data::make_int(7));
|
||||
it.set("name", molo::data::make_text("n1"));
|
||||
it.save(conn, ctx);
|
||||
it.set("name", molo::data::make_text("n2"));
|
||||
it.save(conn, ctx);
|
||||
it.remove(conn, ctx);
|
||||
}
|
||||
|
||||
MemoryStore store;
|
||||
Appender app(store);
|
||||
Appender::Install(&app);
|
||||
{
|
||||
Item it;
|
||||
it.set("id", molo::data::make_int(7));
|
||||
it.set("name", molo::data::make_text("n1"));
|
||||
it.save(conn, ctx);
|
||||
it.set("name", molo::data::make_text("n2"));
|
||||
it.save(conn, ctx);
|
||||
it.remove(conn, ctx);
|
||||
}
|
||||
Appender::Install(nullptr);
|
||||
|
||||
auto rows = store.ReadRange(1, 10);
|
||||
CHECK(rows.size() == 3, "three hook records");
|
||||
CHECK(rows[0].op == "insert" && rows[0].table == "items" && rows[0].pk == "7",
|
||||
"insert record shape");
|
||||
CHECK(rows[0].tenant == "t1" && rows[0].request_id == "req-1", "context carried");
|
||||
CHECK(rows[0].after_json.find("n1") != std::string::npos, "insert after image");
|
||||
CHECK(rows[1].op == "update", "update op");
|
||||
CHECK(rows[2].op == "delete" && rows[2].after_json.empty(), "delete op");
|
||||
CHECK(rows[2].before_json.find("n2") != std::string::npos, "delete before image");
|
||||
std::cout << "PASS orm_hook\n";
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// pii_boot: registry builds when every field is marked, boot-fails naming
|
||||
// the unmarked fields otherwise.
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "audit/pii/registry.h"
|
||||
|
||||
#define CHECK(c, msg) \
|
||||
if (!(c)) { \
|
||||
std::cerr << "FAIL pii_boot: " << msg << "\n"; \
|
||||
return 1; \
|
||||
}
|
||||
|
||||
int main() {
|
||||
using namespace molo::audit::pii;
|
||||
std::map<std::string, std::vector<std::string>> schema = {
|
||||
{"users", {"id", "email", "name"}}, {"orders", {"id", "user_id", "total"}}};
|
||||
std::map<std::string, FieldMark> marks = {
|
||||
{"users.id", {PiiClass::kNone, true, ErasureRule::kKeep, "pk"}},
|
||||
{"users.email", {PiiClass::kEmail, true, ErasureRule::kDelete, "consent"}},
|
||||
{"users.name", {PiiClass::kName, true, ErasureRule::kDelete, "consent"}},
|
||||
{"orders.id", {PiiClass::kNone, true, ErasureRule::kKeep, "pk"}},
|
||||
{"orders.user_id", {PiiClass::kNationalId, false, ErasureRule::kMask, "contract"}},
|
||||
{"orders.total", {PiiClass::kFinancial, true, ErasureRule::kDelete, "contract"}},
|
||||
};
|
||||
Registry r = Registry::Build(schema, marks);
|
||||
CHECK(r.size() == 6, "registry size");
|
||||
CHECK(r.Lookup("users", "email").cls == PiiClass::kEmail, "lookup class");
|
||||
CHECK(!r.Lookup("orders", "user_id").exportable, "lookup exportable");
|
||||
CHECK(r.Lookup("nope", "nope").cls == PiiClass::kNone, "lookup default none");
|
||||
|
||||
marks.erase("orders.total");
|
||||
bool threw = false;
|
||||
try {
|
||||
Registry::Build(schema, marks);
|
||||
} catch (const std::runtime_error& e) {
|
||||
threw = true;
|
||||
CHECK(std::string(e.what()).find("orders.total") != std::string::npos,
|
||||
"names missing field");
|
||||
}
|
||||
CHECK(threw, "unmarked field must boot-fail");
|
||||
std::cout << "PASS pii_boot\n";
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
// query_guard: queries without audit.read get 403 and are self-audited;
|
||||
// granted queries page rows and are self-audited too.
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "audit/log/appender.h"
|
||||
#include "audit/log/query.h"
|
||||
#include "audit/log/store.h"
|
||||
|
||||
#define CHECK(c, msg) \
|
||||
if (!(c)) { \
|
||||
std::cerr << "FAIL query_guard: " << msg << "\n"; \
|
||||
return 1; \
|
||||
}
|
||||
|
||||
int main() {
|
||||
using namespace molo::audit::log;
|
||||
using namespace molo::security::auth;
|
||||
MemoryStore store;
|
||||
Appender app(store);
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
Appender::Write w;
|
||||
w.actor = "writer";
|
||||
w.action = "orm.insert";
|
||||
w.table = "orders";
|
||||
w.pk = std::to_string(i);
|
||||
w.op = "insert";
|
||||
w.tenant = "t1";
|
||||
app.Append(std::move(w));
|
||||
}
|
||||
Query q(store, &app);
|
||||
|
||||
// INTEGRATION: real Session (security/session) plus explicit roles.
|
||||
molo::security::session::Session anon;
|
||||
anon.user_id = "anon";
|
||||
Filter f;
|
||||
f.tenant = "t1";
|
||||
QueryPage denied = q.Run(f, anon, {});
|
||||
CHECK(denied.http_status == 403 && denied.code == "forbidden", "anon gets 403");
|
||||
CHECK(denied.rows.empty(), "denied returns no rows");
|
||||
auto trail = store.ReadRange(6, 10);
|
||||
CHECK(trail.size() == 1 && trail[0].action == "query.denied", "denial self-audited");
|
||||
|
||||
molo::security::session::Session reader;
|
||||
reader.user_id = "reader";
|
||||
reader.privilege = molo::security::session::Session::Privilege::User;
|
||||
std::vector<Role> roles = {Role{"auditor", {Permission{"audit.read"}}}};
|
||||
QueryPage allowed = q.Run(f, reader, roles);
|
||||
CHECK(allowed.http_status == 200, "reader gets 200");
|
||||
CHECK(allowed.rows.size() == 6, "reader sees 5 rows plus denial self-audit");
|
||||
CHECK(allowed.next_cursor == "6", "cursor at last row");
|
||||
auto trail2 = store.ReadRange(7, 10);
|
||||
CHECK(trail2.size() == 1 && trail2[0].action == "query.allowed", "grant self-audited");
|
||||
|
||||
f.limit = 2;
|
||||
QueryPage p1 = q.Run(f, reader, roles);
|
||||
CHECK(p1.rows.size() == 2 && p1.next_cursor == "2", "page 1");
|
||||
f.cursor = p1.next_cursor;
|
||||
QueryPage p2 = q.Run(f, reader, roles);
|
||||
CHECK(p2.rows.size() == 2 && p2.rows[0].seq == 3, "page 2 resumes");
|
||||
std::cout << "PASS query_guard\n";
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// redactor: PII shapes replaced with [redacted:<class>], sensitive log keys
|
||||
// replaced wholesale, oversize input capped.
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
#include "audit/redact/redactor.h"
|
||||
|
||||
#define CHECK(c, msg) \
|
||||
if (!(c)) { \
|
||||
std::cerr << "FAIL redactor: " << msg << "\n"; \
|
||||
return 1; \
|
||||
}
|
||||
|
||||
int main() {
|
||||
using molo::audit::redact::Redactor;
|
||||
std::string mail = Redactor::Redact("contact ada@example.com please");
|
||||
CHECK(mail.find("ada@example.com") == std::string::npos, "email removed");
|
||||
CHECK(mail.find("[redacted:email]") != std::string::npos, "email tag");
|
||||
|
||||
std::string cred = Redactor::Redact("login password: s3cret hunter2");
|
||||
CHECK(cred.find("s3cret") == std::string::npos, "credential removed");
|
||||
CHECK(cred.find("[redacted:credential]") != std::string::npos, "credential tag");
|
||||
|
||||
std::string ssn = Redactor::Redact("ssn 123-45-6789 on file");
|
||||
CHECK(ssn.find("123-45-6789") == std::string::npos, "national id removed");
|
||||
CHECK(ssn.find("[redacted:national_id]") != std::string::npos, "national id tag");
|
||||
|
||||
std::string clean = Redactor::Redact("nothing sensitive here");
|
||||
CHECK(clean == "nothing sensitive here", "clean text untouched");
|
||||
|
||||
molo::log::Record rec;
|
||||
rec.message = "user ada@example.com logged in";
|
||||
// INTEGRATION: real FieldValue is a variant; spell strings explicitly
|
||||
// (bare literals would bind the bool alternative).
|
||||
rec.fields.push_back({"api_key", std::string("abcdef")});
|
||||
rec.fields.push_back({"route", std::string("/v1/items")});
|
||||
Redactor::RedactRecord(rec);
|
||||
CHECK(rec.message.find("[redacted:email]") != std::string::npos, "record message");
|
||||
CHECK(std::get<std::string>(rec.fields[0].value) == "[redacted:credential]",
|
||||
"sensitive key wholesale");
|
||||
CHECK(std::get<std::string>(rec.fields[1].value) == "/v1/items", "plain field kept");
|
||||
|
||||
std::string big(2 * 1024 * 1024, 'x');
|
||||
big += "ada@example.com";
|
||||
std::string out = Redactor::Redact(big);
|
||||
CHECK(out.size() <= molo::audit::kRedactMaxBytesPerCall + 64, "oversize capped");
|
||||
std::cout << "PASS redactor\n";
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// retention_hold: TTL boundary (89 kept, 91 deleted), grace days, legal holds
|
||||
// block deletion, apply honors the pinned policy version only.
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "audit/retention/enforcer.h"
|
||||
#include "audit/retention/hold.h"
|
||||
#include "audit/retention/policy.h"
|
||||
|
||||
#define CHECK(c, msg) \
|
||||
if (!(c)) { \
|
||||
std::cerr << "FAIL retention_hold: " << msg << "\n"; \
|
||||
return 1; \
|
||||
}
|
||||
|
||||
int main() {
|
||||
using namespace molo::audit::retention;
|
||||
Policy policy(7, {{"users", TableRule{90, 0}}, {"logs", TableRule{90, 5}}});
|
||||
std::vector<ScannedRow> rows = {
|
||||
{"t1", "users", "a", 89}, {"t1", "users", "b", 91}, {"t1", "logs", "c", 93}};
|
||||
Hold hold;
|
||||
|
||||
auto scan = [&](ScannedRow& out) -> bool {
|
||||
static std::size_t i = 0;
|
||||
if (i >= rows.size()) {
|
||||
i = 0;
|
||||
return false;
|
||||
}
|
||||
out = rows[i++];
|
||||
return true;
|
||||
};
|
||||
DryRunReport dry = Enforcer::DryRun(policy, hold, scan);
|
||||
CHECK(dry.scanned == 3 && dry.kept == 2 && dry.would_delete == 1 && dry.held == 0,
|
||||
"89 kept, 91 deleted, 93 in grace");
|
||||
CHECK(dry.deletions.size() == 1 && dry.deletions[0].pk == "b", "deletion names pk b");
|
||||
|
||||
hold.Add(Hold::RowKey("t1", "users", "b"), "litigation");
|
||||
DryRunReport dry2 = Enforcer::DryRun(policy, hold, scan);
|
||||
CHECK(dry2.held == 1 && dry2.would_delete == 0, "hold blocks deletion");
|
||||
|
||||
hold.Remove(Hold::RowKey("t1", "users", "b"));
|
||||
std::vector<std::string> deleted;
|
||||
ApplyResult applied =
|
||||
Enforcer::Apply(policy, hold, dry, [&](const ScannedRow& r) { deleted.push_back(r.pk); });
|
||||
CHECK(applied.deleted == 1 && deleted[0] == "b", "apply deletes pinned rows");
|
||||
|
||||
hold.Add(Hold::RowKey("t1", "users", "b"), "late hold");
|
||||
ApplyResult held =
|
||||
Enforcer::Apply(policy, hold, dry, [&](const ScannedRow&) { deleted.push_back("x"); });
|
||||
CHECK(held.deleted == 0 && held.skipped_hold == 1, "late hold skips apply");
|
||||
|
||||
Policy newer(8, {{"users", TableRule{90, 0}}});
|
||||
ApplyResult stale =
|
||||
Enforcer::Apply(newer, Hold{}, dry, [&](const ScannedRow&) { deleted.push_back("x"); });
|
||||
CHECK(stale.deleted == 0 && stale.skipped_policy == 1, "version change skips apply");
|
||||
std::cout << "PASS retention_hold\n";
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
// sql_store: SqlStore round-trips records and seals through the SqlExec seam
|
||||
// under both placeholder styles (Postgres $n and SQLite ?), and the stored
|
||||
// chain verifies.
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "audit/log/chain.h"
|
||||
#include "audit/log/sql_store.h"
|
||||
|
||||
#define CHECK(c, msg) \
|
||||
if (!(c)) { \
|
||||
std::cerr << "FAIL sql_store: " << msg << "\n"; \
|
||||
return 1; \
|
||||
}
|
||||
|
||||
// In-memory SqlExec honoring the exact statements SqlStore emits.
|
||||
class FakeExec : public molo::audit::log::SqlExec {
|
||||
public:
|
||||
explicit FakeExec(bool pg) : pg_(pg) {}
|
||||
void Exec(const std::string& sql, const std::vector<std::string>& p) override {
|
||||
if (sql.rfind("CREATE TABLE", 0) == 0) return;
|
||||
if (sql.rfind("INSERT INTO audit_records", 0) == 0) {
|
||||
if (p.size() != 14) throw std::runtime_error("records INSERT arity");
|
||||
records_.push_back(p);
|
||||
return;
|
||||
}
|
||||
if (sql.rfind("INSERT INTO audit_seals", 0) == 0) {
|
||||
if (p.size() != 4) throw std::runtime_error("seals INSERT arity");
|
||||
seals_.push_back(p);
|
||||
return;
|
||||
}
|
||||
throw std::runtime_error("unexpected Exec: " + sql);
|
||||
}
|
||||
std::vector<std::vector<std::string>> Query(const std::string& sql,
|
||||
const std::vector<std::string>& p,
|
||||
std::size_t max_rows) override {
|
||||
if (sql == "SELECT seq,hash FROM audit_records ORDER BY seq DESC LIMIT 1") {
|
||||
if (records_.empty()) return {};
|
||||
auto& r = records_.back();
|
||||
return {{r[0], r[13]}};
|
||||
}
|
||||
if (sql == "SELECT seq FROM audit_records ORDER BY seq DESC LIMIT 1") {
|
||||
if (records_.empty()) return {};
|
||||
return {{records_.back()[0]}};
|
||||
}
|
||||
if (sql == "SELECT hash FROM audit_records ORDER BY seq DESC LIMIT 1") {
|
||||
if (records_.empty()) return {};
|
||||
return {{records_.back()[13]}};
|
||||
}
|
||||
if (sql.rfind("SELECT seq,ts,tenant,", 0) == 0) {
|
||||
std::uint64_t from = std::stoull(p.at(0));
|
||||
std::vector<std::vector<std::string>> out;
|
||||
for (auto& r : records_) {
|
||||
if (out.size() >= max_rows) break;
|
||||
if (std::stoull(r[0]) >= from) out.push_back(r);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
if (sql.rfind("SELECT seal_seq,root,ts,mac", 0) == 0) return seals_;
|
||||
throw std::runtime_error("unexpected Query: " + sql);
|
||||
}
|
||||
bool IsPostgres() const override { return pg_; }
|
||||
|
||||
private:
|
||||
bool pg_;
|
||||
std::vector<std::vector<std::string>> records_, seals_;
|
||||
};
|
||||
|
||||
static int RunBoth(bool pg) {
|
||||
using namespace molo::audit::log;
|
||||
FakeExec db(pg);
|
||||
SqlStore store(db);
|
||||
CHECK(store.LatestSeq() == 0, "empty latest 0");
|
||||
CHECK(store.Tip() == GenesisHash(), "empty tip genesis");
|
||||
for (int i = 0; i < 50; ++i) {
|
||||
AuditRecord r;
|
||||
r.ts = 1700000000 + i;
|
||||
r.tenant = "t1";
|
||||
r.actor = "w";
|
||||
r.action = "orm.insert";
|
||||
r.table = "users";
|
||||
r.pk = std::to_string(i);
|
||||
r.op = "insert";
|
||||
r.after_json = "{\"i\":" + std::to_string(i) + "}";
|
||||
CHECK(store.Append(r) == (std::uint64_t)(i + 1), "seq assigned");
|
||||
}
|
||||
CHECK(store.LatestSeq() == 50, "latest 50");
|
||||
auto page = store.ReadRange(11, 10);
|
||||
CHECK(page.size() == 10 && page.front().seq == 11 && page.back().seq == 20, "range page");
|
||||
CHECK(page[0].after_json == "{\"i\":10}", "payload round-trip");
|
||||
auto all = store.ReadRange(1, 1000);
|
||||
CHECK(Chain::Verify(all).ok, "stored chain verifies");
|
||||
Seal s = Chain::MakeSeal(1, store.Tip(), 1700000600, "k");
|
||||
store.SaveSeal(s);
|
||||
auto seals = store.Seals();
|
||||
CHECK(seals.size() == 1 && seals[0].mac == s.mac, "seal round-trip");
|
||||
CHECK(Chain::CheckSeal(seals[0], "k"), "seal mac verifies");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
if (RunBoth(false) != 0) return 1;
|
||||
if (RunBoth(true) != 0) return 1;
|
||||
std::cout << "PASS sql_store\n";
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user