// query_guard: queries without audit.read get 403 and are self-audited; // granted queries page rows and are self-audited too. #include #include #include #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 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; }