lonca-audit 0.2.1

Independent auditor of a Lonca repository: replays every decision of the signed operation log and recomputes its facts and merges
Documentation
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! The independent auditor ("the gatekeeper's gatekeeper").
//!
//! Reads a repository (the daemon's store, a pushed remote or a CI checkout)
//! without writing to it and checks that:
//! 1. the operation log is an unbroken, correctly signed hash chain whose
//!    time never goes backwards;
//! 2. every decision is reproduced by the decision function from the state
//!    before it and the policy in force at that point;
//! 3. every recorded fact set equals the facts recomputed from the Git
//!    objects, and every merged commit equals the recomputed merge, both
//!    with the symbol engine version recorded in the entry;
//! 4. every daemon key switch (`daemon.rotate`, `daemon.recover`) was
//!    authorised: its decision replays as allowed, so a forged human
//!    signature can not move the log to another key;
//! 5. trunk and policy refs equal the replayed state;
//! 6. (`compare`) a pushed log is a prefix of the local one, so history that
//!    was published can not be rewritten unnoticed.
//!
//! It contains no AI and calls no network.

#![forbid(unsafe_code)]
// The trusted core: a panic in the single writer stops every repository,
// so bad input must become an error or a denial (clippy.toml lets tests panic).
#![warn(clippy::unwrap_used, clippy::expect_used, clippy::indexing_slicing)]

use std::collections::BTreeMap;
use std::sync::Arc;

use lonca_core::oplog::{OpLog, SignedOp, verify_chain};
use lonca_core::ops::Op;
use lonca_core::{ChangeId, GitId, Result, Store};
use lonca_policy::config::{CONFIG_FILE, Config, POLICY_FILE, PolicyBundle};
use lonca_policy::decide::{Ctx, decide};
use lonca_policy::state::State;
use lonca_symbols::engine;
use lonca_symbols::integrate::{facts_of, integrate_joining};
use serde::Serialize;

pub const TRUNK_REF: &str = "refs/heads/main";
pub const POLICY_REF: &str = "refs/lonca/policy";

#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct Finding {
    pub seq: Option<u64>,
    pub kind: String,
    pub detail: String,
}

#[derive(Clone, Debug, Default, Serialize)]
pub struct Report {
    pub ops: u64,
    pub head_hash: String,
    pub findings: Vec<Finding>,
    /// Rechecks skipped because objects were missing (e.g. a shallow clone).
    pub skipped: Vec<Finding>,
    /// Every daemon key switch along the log, for the reader to see.
    pub key_switches: Vec<KeySwitch>,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct KeySwitch {
    pub seq: u64,
    /// `daemon.rotate` or `daemon.recover`.
    pub kind: String,
    /// Who requested it (`human:<name>`).
    pub actor: String,
    pub key: String,
}

impl Report {
    pub fn ok(&self) -> bool {
        self.findings.is_empty()
    }

    fn find(&mut self, seq: Option<u64>, kind: &str, detail: impl Into<String>) {
        self.findings.push(Finding {
            seq,
            kind: kind.into(),
            detail: detail.into(),
        });
    }

    fn skip(&mut self, seq: u64, kind: &str, detail: impl Into<String>) {
        self.skipped.push(Finding {
            seq: Some(seq),
            kind: kind.into(),
            detail: detail.into(),
        });
    }
}

#[derive(Clone, Copy, Debug)]
pub struct Options {
    /// Recompute facts and merges (slower, needs every object).
    pub deep: bool,
    /// Compare the refs with the replayed state: trunk, policy, every
    /// proposal's ref, and no name under `refs/lonca/` the daemon never
    /// writes.
    pub check_refs: bool,
    /// Hold every ref to that, not only Lonca's: for the daemon's own store,
    /// or a remote nothing but Lonca pushes to. A clone has its remotes'
    /// branches and tags as well, which are not the log's.
    pub all_refs: bool,
}

impl Default for Options {
    fn default() -> Self {
        Self {
            deep: true,
            check_refs: true,
            all_refs: false,
        }
    }
}

fn load_policy(store: &Store, commit: &GitId) -> std::result::Result<PolicyBundle, String> {
    let read = |f: &str| -> std::result::Result<String, String> {
        let bytes = store
            .read_file(commit, f)
            .map_err(|e| e.to_string())?
            .ok_or_else(|| format!("missing {f}"))?;
        String::from_utf8(bytes).map_err(|_| format!("{f} is not UTF-8"))
    };
    PolicyBundle::parse(&read(CONFIG_FILE)?, &read(POLICY_FILE)?)
}

pub fn audit(store: &Store, opts: Options) -> Result<Report> {
    let ops = OpLog::new(store).read_all()?;
    Ok(audit_ops(store, &ops, opts))
}

pub fn audit_ops(store: &Store, ops: &[SignedOp], opts: Options) -> Report {
    let mut rep = Report {
        ops: ops.len() as u64,
        ..Default::default()
    };
    match verify_chain(ops) {
        Ok(h) => rep.head_hash = h,
        Err(e) => {
            rep.find(None, "chain", e.to_string());
            return rep;
        }
    }
    let default_policy = Arc::new(PolicyBundle::default_bundle());
    let mut policies: BTreeMap<GitId, Arc<PolicyBundle>> = BTreeMap::new();
    let mut state = State::new();
    for op in ops {
        let e = &op.entry;
        let policy = match &state.policy {
            None => default_policy.clone(),
            Some(id) => match policies.get(id) {
                Some(p) => p.clone(),
                None => match load_policy(store, id) {
                    Ok(p) => {
                        let p = Arc::new(p);
                        policies.insert(id.clone(), p.clone());
                        p
                    }
                    Err(err) => {
                        rep.find(
                            Some(e.seq),
                            "policy",
                            format!("policy {} unreadable: {err}", id.short()),
                        );
                        return rep;
                    }
                },
            },
        };
        let engine_known = match e.engine().map(engine::check) {
            Some(Err(err)) => {
                rep.find(Some(e.seq), "engine", err.to_string());
                false
            }
            _ => true,
        };
        if opts.deep && engine_known {
            recheck_objects(store, &state, &policy.config, op, &mut rep);
        }
        let d = decide(
            &state,
            &e.actor,
            &e.op,
            e.human.as_ref(),
            &Ctx {
                format: e.format,
                time_ms: e.time_ms,
                policy: &policy,
                facts: e.facts.as_ref(),
                evidence: &e.evidence,
                symbols_engine: e.symbols_engine,
            },
        );
        if let Some(key) = e.switches_daemon_key() {
            rep.key_switches.push(KeySwitch {
                seq: e.seq,
                kind: e.op.kind().as_str().into(),
                actor: e.actor.label(),
                key: key.into(),
            });
        }
        if d != e.decision {
            let kind = if e.op.daemon_key_switch().is_some() {
                "daemon_key"
            } else {
                "decision"
            };
            rep.find(
                Some(e.seq),
                kind,
                format!(
                    "{}: recorded {:?} {:?}, replay gives {:?} {:?}",
                    e.op.kind().as_str(),
                    e.decision.outcome,
                    e.decision.reasons,
                    d.outcome,
                    d.reasons
                ),
            );
        }
        state.apply(op);
    }
    if opts.check_refs {
        for (name, want) in [(TRUNK_REF, &state.trunk), (POLICY_REF, &state.policy)] {
            match store.ref_get(name) {
                Ok(found) if found.as_ref() == want.as_ref() => {}
                Ok(found) => rep.find(
                    None,
                    "ref",
                    format!("{name} is {found:?}, the log says {want:?}"),
                ),
                Err(err) => rep.find(None, "ref", format!("{name}: {err}")),
            }
        }
        check_other_refs(store, &state, opts.all_refs, &mut rep);
    }
    rep
}

/// The refs besides trunk and policy, as the daemon's `reconcile()` holds
/// them (`lonca_policy::refs`). A proposal's ref past the end of the log is
/// reported too: the log was cut short, or the daemon stopped between
/// writing the ref and logging the proposal (it removes such a ref when it
/// starts).
fn check_other_refs(store: &Store, state: &State, all: bool, rep: &mut Report) {
    use lonca_policy::refs::{RefCheck, check, missing_changes};
    let refs = match store.refs_with_prefix(if all { "refs/" } else { "refs/lonca/" }) {
        Ok(r) => r,
        Err(err) => return rep.find(None, "ref", format!("listing refs: {err}")),
    };
    let present = refs.iter().map(|(n, _)| n.clone()).collect();
    for (name, found) in &refs {
        if name == TRUNK_REF || name == POLICY_REF {
            continue;
        }
        let said = match check(state, name, found) {
            RefCheck::Fine => continue,
            RefCheck::Differs(want) => format!("{name} is {found}, the log says {want:?}"),
            RefCheck::NotLogged => {
                format!("{name} names a proposal past the end of the log")
            }
            RefCheck::Unknown => format!("{name} ({found}) is no ref the log has"),
        };
        rep.find(None, "ref", said);
    }
    for (name, commit) in missing_changes(state, &present) {
        rep.find(
            None,
            "ref",
            format!("{name} is missing; the log says {commit}"),
        );
    }
}

/// Recomputes facts and merges. `state` is the one before `op`: what a merge
/// may join comes from it, as it did in the daemon.
fn recheck_objects(store: &Store, state: &State, config: &Config, op: &SignedOp, rep: &mut Report) {
    let e = &op.entry;
    let engine = e.engine().unwrap_or(engine::ENGINE_CURRENT);
    match &e.op {
        Op::ChangePropose { base, commit, .. } => {
            if !store.has_object(commit) || !store.has_object(base) {
                rep.skip(e.seq, "facts", "proposal objects not present");
                return;
            }
            match facts_of(store, engine, base, commit) {
                Ok(a) if Some(&a.facts) == e.facts.as_ref() => {}
                Ok(a) => rep.find(
                    Some(e.seq),
                    "facts",
                    format!("recorded facts differ from recomputed {:?}", a.facts),
                ),
                Err(err) => rep.find(Some(e.seq), "facts", err.to_string()),
            }
        }
        Op::ChangeIntegrate {
            change,
            trunk_before,
            merged,
            conflicts,
        } => {
            let Some(rec) = state.changes.get(change) else {
                return;
            };
            if !store.has_object(&rec.commit) || !store.has_object(trunk_before) {
                rep.skip(e.seq, "merge", "integration objects not present");
                return;
            }
            let joins = state.joins(*change, &[], config);
            let may_join = |s: &str| joins.allows(s);
            match integrate_joining(
                store,
                engine,
                &rec.base,
                trunk_before,
                &rec.commit,
                &may_join,
            ) {
                Ok(i) => {
                    if &i.merged != merged || &i.conflicts != conflicts {
                        rep.find(
                            Some(e.seq),
                            "merge",
                            format!(
                                "recorded {merged:?} {conflicts:?}, recomputed {:?} {:?}",
                                i.merged, i.conflicts
                            ),
                        );
                    }
                }
                Err(err) => rep.find(Some(e.seq), "merge", err.to_string()),
            }
        }
        Op::ChangeIntegrateBatch {
            trunk_before,
            merged,
        } => {
            let mut trunk = trunk_before.clone();
            let members: Vec<ChangeId> = merged.iter().map(|(c, _)| *c).collect();
            for (k, (change, commit)) in merged.iter().enumerate() {
                let Some(rec) = state.changes.get(change) else {
                    return;
                };
                if !store.has_object(&rec.commit) || !store.has_object(&trunk) {
                    rep.skip(e.seq, "merge", "integration objects not present");
                    return;
                }
                let joins = state.joins(*change, members.get(..k).unwrap_or_default(), config);
                let may_join = |s: &str| joins.allows(s);
                match integrate_joining(store, engine, &rec.base, &trunk, &rec.commit, &may_join) {
                    Ok(i) if i.merged.as_ref() == Some(commit) => {}
                    Ok(i) => {
                        let found = format!("recorded {commit:?}, recomputed {:?}", i.merged);
                        rep.find(Some(e.seq), "merge", found);
                        return;
                    }
                    Err(err) => {
                        rep.find(Some(e.seq), "merge", err.to_string());
                        return;
                    }
                }
                trunk = commit.clone();
            }
            // What was verified is the tip, which is what trunk became.
            match store.read_commit(&trunk) {
                Ok(tip)
                    if !e.evidence.is_empty()
                        && e.evidence.iter().all(|a| a.subject_tree == tip.tree) => {}
                Ok(_) => rep.find(Some(e.seq), "merge", "evidence of another tree".to_string()),
                Err(err) => rep.find(Some(e.seq), "merge", err.to_string()),
            }
        }
        _ => {}
    }
}

/// Checks that `published` is a prefix of `local`: everything that was
/// pushed is still in the local log, unchanged.
pub fn compare(local: &[SignedOp], published: &[SignedOp]) -> Vec<Finding> {
    let mut out = Vec::new();
    if published.len() > local.len() {
        out.push(Finding {
            seq: Some(local.len() as u64),
            kind: "anchor".into(),
            detail: format!(
                "published log has {} entries, local only {}",
                published.len(),
                local.len()
            ),
        });
    }
    for (l, p) in local.iter().zip(published) {
        if l.hash != p.hash {
            out.push(Finding {
                seq: Some(p.entry.seq),
                kind: "anchor".into(),
                detail: "local history differs from what was published".into(),
            });
            break;
        }
    }
    out
}