use std::collections::BTreeSet;
use anyhow::{Context as _, Result};
use openmoat_audit::{ChainReport, EventId, Store};
use openmoat_core::attribution::{
AuditHead, Discrepancy, EventHash, FileRecord, NOTE_FORMAT, Note, NoteFormat, compare,
};
use serde::Serialize;
use crate::integrity::sha256_hex;
pub const MAX_NOTE_BYTES: usize = 4 * 1024 * 1024;
pub fn build(store: &Store, commit: &str, files: Vec<FileRecord>) -> Result<Note> {
let head = store
.head()?
.context("the audit log has no chained events yet; there is nothing to cite")?;
let cited: BTreeSet<i64> = files.iter().map(|f| f.event).collect();
let mut events = Vec::new();
for event in cited {
let hash = store.hash_of(EventId(event))?.unwrap_or_default();
events.push(EventHash { event, hash });
}
let mut note = Note {
format: NoteFormat::V1,
commit: commit.to_owned(),
head: AuditHead {
event: head.id.0,
hash: head.hash,
},
events,
digest: String::new(),
files,
};
note.digest = digest(¬e);
Ok(note)
}
pub fn digest(note: &Note) -> String {
sha256_hex(¬e.digest_input())
}
pub fn encode(note: &Note) -> Result<String> {
Ok(serde_json::to_string(note)?)
}
pub fn decode(text: &str) -> Result<Note> {
serde_json::from_str(text.trim()).with_context(|| format!("not an {NOTE_FORMAT} note"))
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Checked {
pub commit: String,
pub noted: bool,
pub problems: Vec<String>,
}
pub fn verify(
store: &Store,
chain: &ChainReport,
commit: &str,
note: &Note,
found: &[FileRecord],
) -> Result<Vec<String>> {
let mut problems = Vec::new();
if note.commit != commit {
problems.push(format!("the note names another commit ({})", note.commit));
}
if note.digest != digest(note) {
problems.push("the note was edited after it was written: its digest does not match".into());
}
if let Some(broken) = chain.broken.as_ref().filter(|b| b.id.0 <= note.head.event) {
problems.push(format!(
"the audit log's hash chain breaks at event {}, before the head the note cites",
broken.id
));
}
if store.hash_of(EventId(note.head.event))?.as_deref() != Some(note.head.hash.as_str()) {
problems.push(format!(
"the log no longer holds the head the note cites (event {:x}): events were \
rewritten or deleted, or the note comes from another machine's log",
note.head.event
));
}
for row in ¬e.events {
if store.hash_of(EventId(row.event))?.as_deref() != Some(row.hash.as_str()) {
problems.push(format!(
"event {:x} the note cites is not in this log with that hash",
row.event
));
}
}
for file in ¬e.files {
problems.extend(cited_write(store, note, file)?);
}
for discrepancy in compare(¬e.files, found) {
problems.push(describe(&discrepancy));
}
Ok(problems)
}
fn cited_write(store: &Store, note: &Note, file: &FileRecord) -> Result<Option<String>> {
if !note.events.iter().any(|e| e.event == file.event) {
return Ok(Some(format!(
"{} cites event {:x} without its hash",
file.path, file.event
)));
}
let backs = store.get(EventId(file.event))?.is_some_and(|event| {
event.host == file.host && event.session_id == file.session && event.ts_ms <= file.ts_ms
});
Ok((!backs).then(|| {
format!(
"{}: event {:x} is not a call of {} session {}",
file.path, file.event, file.host, file.session
)
}))
}
fn describe(discrepancy: &Discrepancy) -> String {
match discrepancy {
Discrepancy::Omitted {
path,
host,
session,
} => format!("the note leaves out that {host} session {session} wrote {path}"),
Discrepancy::Changed {
path,
host,
session,
what,
} => format!("the note changes {what} for {path} ({host} session {session})"),
}
}