1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//! Neo4j implementation of [`AdminAuditStore`] (B.10b PHASE 2).
//!
//! Cypher over the HTTP transactional API, mirroring the Postgres impl
//! (`postgres_admin_audit.rs`) SEMANTICS exactly so the cross-backend
//! conformance contract passes byte-for-byte. The tamper-evident hash chain is
//! computed in Rust via the SHARED [`compute_admin_audit_hash`] /
//! [`verify_admin_audit_chain_step`] helpers, so PG / MySQL / SQLite / MSSQL /
//! MongoDB / Cassandra / Neo4j chains are byte-identical.
//!
//! Every row is a `(:UdbAuditLog)` node carrying `run_tag` (test isolation on
//! Community single-DB) + `audit_id` as a property; every query is scoped by
//! `{run_tag:$tag}`.
//!
//! ## Atomicity — chain-serialised append in ONE HTTP transaction
//!
//! PG uses `pg_advisory_xact_lock` to serialise the chain; Neo4j has no
//! advisory lock. Instead `append_admin_audit` runs read-head-then-create as a
//! LIST of statements through [`cypher_tx_rows`], which the HTTP
//! `/db/<db>/tx/commit` endpoint runs as ONE ACID transaction. The first
//! statement MATCHes the latest-hash node, taking a read lock that the
//! transaction holds to commit; we then compute the next `current_hash` in Rust
//! and the second statement CREATEs the audit node carrying that hash. Two
//! concurrent appends serialise on the contended nodes, so the chain never
//! forks with two rows sharing one `previous_hash`.
//!
//! ## Modeling choices (mirror the logical PG `udb_admin_audit_log`)
//!
//! - `request_json` JSON stored as a Cypher string property (serialised/parsed
//! in Rust — Neo4j node properties cannot hold nested maps).
//! - `created_at` stored as a client-computed epoch-millis integer so the
//! verify scan can `ORDER BY created_at, audit_id` exactly like PG.
use async_trait::async_trait;
use serde_json::{Value as Json, json};
use uuid::Uuid;
use super::neo4j::{
LABEL_AUDIT_LOG, Neo4jCanonicalStore, neo_err, now_unix_ms, prop_dt, prop_json, prop_str,
prop_uuid,
};
use super::system_store::{
AdminAuditChainReport, AdminAuditInsert, AdminAuditListFilter, AdminAuditRow, AdminAuditStore,
SystemStoreError, SystemStoreResult, compute_admin_audit_hash, verify_admin_audit_chain_step,
};
/// Build an `AdminAuditRow` from a `node{.*}` map projection.
fn node_to_audit(node: &Json) -> SystemStoreResult<AdminAuditRow> {
let audit_id = prop_uuid(node, "audit_id")?;
Ok(AdminAuditRow {
audit_id,
actor: prop_str(node, "actor"),
operation: prop_str(node, "operation"),
target: prop_str(node, "target"),
request_json: prop_json(node, "request_json", Json::Null),
result: prop_str(node, "result"),
tenant_id: prop_str(node, "tenant_id"),
project_id: prop_str(node, "project_id"),
correlation_id: prop_str(node, "correlation_id"),
previous_hash: prop_str(node, "previous_hash"),
current_hash: prop_str(node, "current_hash"),
signer_key_id: prop_str(node, "signer_key_id"),
external_anchor: prop_str(node, "external_anchor"),
created_at: prop_dt(node, "created_at"),
})
}
impl Neo4jCanonicalStore {
/// The run-tag bound param value (mirrors phase-1's `tag()`).
fn audit_tag(&self) -> Json {
json!(self.run_tag())
}
}
#[async_trait]
impl AdminAuditStore for Neo4jCanonicalStore {
fn backend_label(&self) -> &'static str {
"neo4j"
}
async fn ensure_admin_audit_tables(&self) -> SystemStoreResult<()> {
// Composite RANGE indexes (Community-compatible — see phase-1
// `ensure_system_tables`). `created_at` is in a composite index so the
// verify scan's `ORDER BY created_at, audit_id` is cheap.
let indexes = [
format!(
"CREATE INDEX udb_audit_id_idx IF NOT EXISTS \
FOR (a:{LABEL_AUDIT_LOG}) ON (a.run_tag, a.audit_id)"
),
format!(
"CREATE INDEX udb_audit_created_idx IF NOT EXISTS \
FOR (a:{LABEL_AUDIT_LOG}) ON (a.run_tag, a.created_at)"
),
];
for ddl in indexes {
self.executor()
.cypher_rows(&ddl, json!({}))
.await
.map_err(|e| neo_err("ensure_admin_audit_tables", e))?;
}
Ok(())
}
async fn latest_admin_audit_hash(&self) -> SystemStoreResult<String> {
// Newest non-empty current_hash by (created_at DESC, audit_id DESC) —
// identical ordering to PG so a tie on created_at breaks the same way.
let cypher = format!(
"MATCH (a:{LABEL_AUDIT_LOG} {{run_tag:$tag}}) WHERE a.current_hash <> '' \
RETURN a.current_hash AS h ORDER BY a.created_at DESC, a.audit_id DESC LIMIT 1"
);
let rows = self
.executor()
.cypher_rows(&cypher, json!({ "tag": self.audit_tag() }))
.await
.map_err(|e| neo_err("latest_admin_audit_hash", e))?;
Ok(rows
.first()
.and_then(|r| r.get("h"))
.and_then(Json::as_str)
.map(|s| s.to_string())
.unwrap_or_default())
}
async fn append_admin_audit(&self, entry: &AdminAuditInsert) -> SystemStoreResult<Uuid> {
// ── Chain-serialised append via cypher_tx_rows (read head, then create) ─
// We read the latest-hash node, compute the next `current_hash` in Rust
// (via the SHARED hasher so the chain is byte-identical to every other
// backend's), then run read-head + guarded-create as ONE HTTP
// transaction through `cypher_tx_rows` (the /tx/commit endpoint commits
// the statement list atomically). The create re-reads the head INSIDE
// that tx and gates `WHERE observed = $previous_hash`, making the link a
// compare-and-set: if a concurrent append advanced the head between our
// first read and this tx, the guard fails, CREATE does not run, and the
// create statement RETURNs `created = 0` — so the chain never forks and
// a lost CAS surfaces (as a retryable Io error) rather than silently
// double-linking.
let read_head = format!(
"MATCH (a:{LABEL_AUDIT_LOG} {{run_tag:$tag}}) WHERE a.current_hash <> '' \
RETURN a.current_hash AS h ORDER BY a.created_at DESC, a.audit_id DESC LIMIT 1"
);
let head_params = json!({ "tag": self.audit_tag() });
let head_rows = self
.executor()
.cypher_rows(&read_head, head_params.clone())
.await
.map_err(|e| neo_err("append_admin_audit read head", e))?;
let previous_hash = head_rows
.first()
.and_then(|r| r.get("h"))
.and_then(Json::as_str)
.map(|s| s.to_string())
.unwrap_or_default();
let current_hash = compute_admin_audit_hash(
&previous_hash,
&entry.actor,
&entry.operation,
&entry.target,
&entry.request_json,
&entry.result,
&entry.tenant_id,
&entry.project_id,
&entry.correlation_id,
&entry.signer_key_id,
&entry.external_anchor,
);
let audit_id = Uuid::new_v4();
let now = now_unix_ms();
// Re-read the head inside the tx as `observed` (folding the empty chain
// to '' via collect()), gate `WHERE observed = $previous_hash`, CREATE,
// and RETURN the created-node count so the CAS outcome is observable.
let create = format!(
"OPTIONAL MATCH (a:{LABEL_AUDIT_LOG} {{run_tag:$tag}}) WHERE a.current_hash <> '' \
WITH a.current_hash AS observed, a.created_at AS ca, a.audit_id AS ai \
ORDER BY ca DESC, ai DESC \
WITH collect(observed) AS observed_list \
WITH CASE WHEN size(observed_list) = 0 THEN '' ELSE observed_list[0] END AS observed \
WHERE observed = $previous_hash \
CREATE (:{LABEL_AUDIT_LOG} {{run_tag:$tag, audit_id:$audit_id, actor:$actor, \
operation:$operation, target:$target, request_json:$request_json, result:$result, \
tenant_id:$tenant_id, project_id:$project_id, correlation_id:$correlation_id, \
previous_hash:$previous_hash, current_hash:$current_hash, \
signer_key_id:$signer_key_id, external_anchor:$external_anchor, created_at:$now}}) \
RETURN count(*) AS created"
);
let create_params = json!({
"tag": self.audit_tag(),
"audit_id": audit_id.to_string(),
"actor": entry.actor,
"operation": entry.operation,
"target": entry.target,
"request_json": entry.request_json.to_string(),
"result": entry.result,
"tenant_id": entry.tenant_id,
"project_id": entry.project_id,
"correlation_id": entry.correlation_id,
"previous_hash": previous_hash,
"current_hash": current_hash,
"signer_key_id": entry.signer_key_id,
"external_anchor": entry.external_anchor,
"now": now,
});
let statements: &[(&str, Json)] = &[
(read_head.as_str(), head_params),
(create.as_str(), create_params),
];
let results = self
.executor()
.cypher_tx_rows(statements)
.await
.map_err(|e| neo_err("append_admin_audit tx", e))?;
// `cypher_tx_rows` returns the rows of EACH statement in order; the
// create is statement index 1, RETURNing `created` (1 on a winning CAS,
// 0 on a lost one).
let created = results
.get(1)
.and_then(|rows| rows.first())
.and_then(|r| r.get("created"))
.and_then(Json::as_i64)
.unwrap_or(0);
if created == 0 {
// Guard missed (a concurrent append linked first). Surface as Io so
// the caller can retry the append against the new head.
return Err(neo_err(
"append_admin_audit",
"chain head advanced concurrently; retry append",
));
}
Ok(audit_id)
}
async fn list_admin_audit(
&self,
filter: &AdminAuditListFilter,
) -> SystemStoreResult<Vec<AdminAuditRow>> {
// MATCH + optional equality filters + ORDER BY created_at DESC +
// SKIP/LIMIT (PG's WHERE … ORDER BY created_at DESC LIMIT/OFFSET).
let mut filters = String::new();
if filter.operation.is_some() {
filters.push_str(" AND a.operation = $operation");
}
if filter.actor.is_some() {
filters.push_str(" AND a.actor = $actor");
}
if filter.tenant_id.is_some() {
filters.push_str(" AND a.tenant_id = $tenant_id");
}
if filter.project_id.is_some() {
filters.push_str(" AND a.project_id = $project_id");
}
let limit = if filter.limit <= 0 { 100 } else { filter.limit };
let offset = filter.offset.max(0);
let cypher = format!(
"MATCH (a:{LABEL_AUDIT_LOG} {{run_tag:$tag}}) \
WHERE true{filters} \
WITH a ORDER BY a.created_at DESC SKIP $offset LIMIT $limit \
RETURN a{{.*}} AS a"
);
let mut params = json!({ "tag": self.audit_tag(), "offset": offset, "limit": limit });
let obj = params.as_object_mut().expect("params is an object");
if let Some(op) = &filter.operation {
obj.insert("operation".to_string(), json!(op));
}
if let Some(actor) = &filter.actor {
obj.insert("actor".to_string(), json!(actor));
}
if let Some(t) = &filter.tenant_id {
obj.insert("tenant_id".to_string(), json!(t));
}
if let Some(p) = &filter.project_id {
obj.insert("project_id".to_string(), json!(p));
}
let rows = self
.executor()
.cypher_rows(&cypher, params)
.await
.map_err(|e| neo_err("list_admin_audit", e))?;
let mut out = Vec::with_capacity(rows.len());
for row in &rows {
let node = row
.get("a")
.ok_or_else(|| neo_err("list_admin_audit", "row missing 'a' projection"))?;
let mut audit = node_to_audit(node)?;
if filter.redact_request_json {
audit.request_json = json!({"redacted": true});
}
out.push(audit);
}
Ok(out)
}
async fn verify_admin_audit_chain(
&self,
limit: Option<i64>,
) -> SystemStoreResult<AdminAuditChainReport> {
// Read the chain oldest-to-newest (created_at ASC, audit_id ASC — same
// total order PG verifies under) and feed rows one at a time to the
// SHARED verify step, stopping at the first break or once `limit` rows
// are checked.
let max = match limit {
Some(n) if n > 0 => Some(n),
_ => None,
};
let mut cypher = format!(
"MATCH (a:{LABEL_AUDIT_LOG} {{run_tag:$tag}}) \
WITH a ORDER BY a.created_at ASC, a.audit_id ASC"
);
if let Some(n) = max {
cypher.push_str(&format!(" LIMIT {n}"));
}
cypher.push_str(" RETURN a{.*} AS a");
let rows = self
.executor()
.cypher_rows(&cypher, json!({ "tag": self.audit_tag() }))
.await
.map_err(|e| neo_err("verify_admin_audit_chain", e))?;
let mut previous_hash = String::new();
let mut checked: i64 = 0;
for row in &rows {
if let Some(n) = max {
if checked >= n {
break;
}
}
let node = row
.get("a")
.ok_or_else(|| neo_err("verify_admin_audit_chain", "row missing 'a' projection"))?;
let audit = node_to_audit(node)?;
match verify_admin_audit_chain_step(&audit, &previous_hash, checked) {
Ok(next) => {
previous_hash = next;
checked += 1;
}
Err(report) => return Ok(report),
}
}
Ok(AdminAuditChainReport::Passed {
checked_count: checked,
last_hash: previous_hash,
})
}
}