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.
125 lines
4.2 KiB
C++
125 lines
4.2 KiB
C++
// Appender: the write path for audit records. ORM save()/remove() reach it
|
|
// through notify_write (see PATCHES.md entry 2); the AUDIT macro fills
|
|
// actor, subject, request and trace ids from the ambient call context.
|
|
// Oversize before/after images are truncated to their caps; oversize records
|
|
// are rejected loudly, never stored half.
|
|
#pragma once
|
|
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
|
|
#include "../adapt.h"
|
|
#include "../caps.h"
|
|
#include "record.h"
|
|
#include "store.h"
|
|
|
|
namespace molo::audit::log {
|
|
|
|
class Appender {
|
|
public:
|
|
// INTEGRATION: real null_sink() returns NullSink&, not shared_ptr, and
|
|
// real counter() takes (name, int64, labels-string).
|
|
explicit Appender(Store& store, ::molo::data::obs::MetricSink& metrics =
|
|
::molo::data::obs::null_sink())
|
|
: store_(store), metrics_(metrics) {}
|
|
|
|
struct Write {
|
|
std::string actor;
|
|
std::string action;
|
|
std::string table;
|
|
std::string pk;
|
|
std::string op;
|
|
std::string before_json;
|
|
std::string after_json;
|
|
std::string tenant;
|
|
std::string request_id;
|
|
std::string trace_id;
|
|
};
|
|
|
|
// Appends one record, returns its seq. Throws on oversize record.
|
|
std::uint64_t Append(Write w) {
|
|
if (w.before_json.size() > kMaxBeforeBytes) w.before_json.resize(kMaxBeforeBytes);
|
|
if (w.after_json.size() > kMaxAfterBytes) w.after_json.resize(kMaxAfterBytes);
|
|
AuditRecord rec;
|
|
rec.ts = Now();
|
|
rec.tenant = std::move(w.tenant);
|
|
rec.actor = std::move(w.actor);
|
|
rec.action = std::move(w.action);
|
|
rec.table = std::move(w.table);
|
|
rec.pk = std::move(w.pk);
|
|
rec.op = std::move(w.op);
|
|
rec.before_json = std::move(w.before_json);
|
|
rec.after_json = std::move(w.after_json);
|
|
rec.request_id = std::move(w.request_id);
|
|
rec.trace_id = std::move(w.trace_id);
|
|
if (rec.Canonical().size() > kMaxRecordBytes)
|
|
throw std::runtime_error("audit record exceeds kMaxRecordBytes");
|
|
std::uint64_t seq = store_.Append(rec);
|
|
appended_.fetch_add(1);
|
|
metrics_.counter("audit_records_appended_total", 1, "table=" + rec.table);
|
|
MaybeSeal(seq);
|
|
return seq;
|
|
}
|
|
|
|
// Process-wide writer installed once at deploy composition. Null until set.
|
|
static void Install(Appender* a) { Global() = a; }
|
|
static Appender* GlobalInstance() { return Global(); }
|
|
std::uint64_t appended() const { return appended_.load(); }
|
|
|
|
private:
|
|
static Appender*& Global() {
|
|
static Appender* g = nullptr;
|
|
return g;
|
|
}
|
|
|
|
static std::int64_t Now() {
|
|
return std::chrono::duration_cast<std::chrono::seconds>(
|
|
std::chrono::system_clock::now().time_since_epoch())
|
|
.count();
|
|
}
|
|
|
|
void MaybeSeal(std::uint64_t seq) {
|
|
if (seq % kSealEveryRecords != 0) return;
|
|
Seal s = Chain::MakeSeal(seq / kSealEveryRecords, store_.Tip(), Now(), seal_key_);
|
|
store_.SaveSeal(s);
|
|
metrics_.counter("audit_seals_total", 1);
|
|
}
|
|
|
|
Store& store_;
|
|
::molo::data::obs::MetricSink& metrics_;
|
|
std::string seal_key_; // empty until deploy composition provisions the key
|
|
std::atomic<std::uint64_t> appended_{0};
|
|
};
|
|
|
|
// ORM hook called from Model save()/remove() after bus->publish. No-op until
|
|
// Appender::Install wires the store, so ORM boots without audit configured.
|
|
inline void notify_write(const std::string& table, const std::string& pk, const std::string& op,
|
|
const std::string& before_json, const std::string& after_json,
|
|
const ::molo::data::Context& ctx, const std::string& actor = "") {
|
|
Appender* a = Appender::GlobalInstance();
|
|
if (!a) return;
|
|
Appender::Write w;
|
|
w.actor = actor;
|
|
w.action = "orm." + op;
|
|
w.table = table;
|
|
w.pk = pk;
|
|
w.op = op;
|
|
w.before_json = before_json;
|
|
w.after_json = after_json;
|
|
w.tenant = ctx.tenant();
|
|
w.request_id = ctx.correlation_id();
|
|
a->Append(std::move(w));
|
|
}
|
|
|
|
} // namespace molo::audit::log
|
|
|
|
// AUDIT(store, table, pk, op, before, after, ctx, actor): one-line ORM hook
|
|
// filling tenant + request id from the data Context. Availability of the
|
|
// global Appender decides whether this writes or no-ops.
|
|
#define AUDIT(store, table, pk, op, before, after, ctx, actor) \
|
|
::molo::audit::log::notify_write((table), (pk), (op), (before), (after), (ctx), (actor))
|