openmoat_core/attribution/
note.rs1use serde::{Deserialize, Serialize};
12
13use super::FileRecord;
14
15pub const NOTE_FORMAT: &str = "openmoat-provenance-v1";
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
20pub enum NoteFormat {
21 #[serde(rename = "openmoat-provenance-v1")]
23 V1,
24}
25
26#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
28#[serde(deny_unknown_fields)]
29pub struct AuditHead {
30 pub event: i64,
32 pub hash: String,
34}
35
36#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
38#[serde(deny_unknown_fields)]
39pub struct EventHash {
40 pub event: i64,
42 pub hash: String,
44}
45
46#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
48#[serde(deny_unknown_fields)]
49pub struct Note {
50 pub format: NoteFormat,
52 pub commit: String,
54 pub head: AuditHead,
56 pub events: Vec<EventHash>,
58 pub digest: String,
60 pub files: Vec<FileRecord>,
62}
63
64impl Note {
65 #[must_use]
69 pub fn digest_input(&self) -> Vec<u8> {
70 let mut out = Vec::new();
71 let mut field = |text: &str| {
72 out.extend_from_slice(text.len().to_string().as_bytes());
73 out.push(b':');
74 out.extend_from_slice(text.as_bytes());
75 out.push(b'\n');
76 };
77 field(NOTE_FORMAT);
78 field(&self.commit);
79 field(&self.head.event.to_string());
80 field(&self.head.hash);
81 for row in &self.events {
82 field(&row.event.to_string());
83 field(&row.hash);
84 }
85 for file in &self.files {
86 field(&file.path);
87 field(&file.host);
88 field(&file.session);
89 field(&file.event.to_string());
90 field(&file.ts_ms.to_string());
91 field(file.evidence.as_str());
92 let lines: Vec<String> = file
93 .lines
94 .iter()
95 .map(|r| format!("{}-{}", r.start, r.end))
96 .collect();
97 field(&lines.join(","));
98 match &file.untrusted {
99 Some(u) => {
100 field(u.kind.as_str());
101 field(&u.source);
102 }
103 None => field("-"),
104 }
105 }
106 out
107 }
108}
109
110#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
112#[serde(tag = "kind", rename_all = "kebab-case")]
113pub enum Discrepancy {
114 Omitted {
116 path: String,
118 host: String,
120 session: String,
122 },
123 Changed {
125 path: String,
127 host: String,
129 session: String,
131 what: &'static str,
133 },
134}
135
136#[must_use]
141pub fn compare(claimed: &[FileRecord], found: &[FileRecord]) -> Vec<Discrepancy> {
142 let mut out = Vec::new();
143 let same = |a: &FileRecord, b: &FileRecord| {
144 a.path == b.path && a.host == b.host && a.session == b.session
145 };
146 for now in found {
147 let Some(note) = claimed.iter().find(|c| same(c, now)) else {
148 out.push(Discrepancy::Omitted {
149 path: now.path.clone(),
150 host: now.host.clone(),
151 session: now.session.clone(),
152 });
153 continue;
154 };
155 let what = if note.event != now.event || note.ts_ms != now.ts_ms {
156 Some("the write it cites")
157 } else if note.evidence != now.evidence || note.lines != now.lines {
158 Some("how the write is tied to the file")
159 } else if note.untrusted.as_ref().map(|u| u.kind) != now.untrusted.as_ref().map(|u| u.kind)
160 {
161 Some("whether the session had read untrusted content")
162 } else {
163 None
164 };
165 if let Some(what) = what {
166 out.push(Discrepancy::Changed {
167 path: now.path.clone(),
168 host: now.host.clone(),
169 session: now.session.clone(),
170 what,
171 });
172 }
173 }
174 out
175}