agentmux_srv_core/backend/audit_query/
peers.rs1use crate::backend::rpc_types::{AuditEntry, AuditLinks, AuditPeerRows, AuditQueryReq, AuditQueryResult};
11
12pub const QUERY_OP: &str = "audit.query";
14
15pub const SETTING_AUDIT_SHARE: &str = "audit:sharewithpaired";
17
18pub const PEER_LIMIT: u32 = 100;
20
21pub fn request_for_peer(req: &AuditQueryReq) -> AuditQueryReq {
24 AuditQueryReq { cursor: None, totals: None, scope: Some("machine".to_string()), limit: Some(req.limit.unwrap_or(50).min(PEER_LIMIT)), ..req.clone() }
25}
26
27pub fn without_local_links(entries: &mut [AuditEntry]) {
31 for e in entries {
32 e.prompt = None;
33 e.answer = None;
34 e.links = AuditLinks::default();
35 for end in [e.from.as_mut(), e.to.as_mut()].into_iter().flatten() {
36 end.links = AuditLinks::default();
37 }
38 }
39}
40
41pub fn rows_of(host: &str, result: AuditQueryResult) -> AuditPeerRows {
43 let more = result.next_cursor.is_some() || result.other_channels.iter().flatten().any(|c| c.more);
44 let mut entries = result.entries;
45 for channel in result.other_channels.unwrap_or_default() {
46 entries.extend(channel.entries);
47 }
48 entries.sort_by(|a, b| (b.ts_ms, &b.id).cmp(&(a.ts_ms, &a.id)));
49 without_local_links(&mut entries);
50 for e in &mut entries {
51 e.id = format!("{host}/{}", e.id);
52 let provenance = e.provenance.get_or_insert_with(Default::default);
53 provenance.host = Some(host.to_string());
55 if provenance.channel.as_deref().is_none_or(str::is_empty) {
56 provenance.channel = result.channel.clone();
57 }
58 }
59 AuditPeerRows { host: host.to_string(), entries, more, note: None }
60}
61
62pub fn no_rows(host: &str, note: &str) -> AuditPeerRows {
64 AuditPeerRows { host: host.to_string(), entries: Vec::new(), more: false, note: Some(note.to_string()) }
65}
66
67pub fn note_for(refused_reason: Option<&str>, error: Option<&str>) -> String {
69 match refused_reason {
70 Some("not_shared") => "its owner has not turned on sharing its Audit with paired computers".to_string(),
71 Some("needs_certificate") | Some("install_not_trusted") => "this computer is not yet approved there".to_string(),
72 Some("remote_ops_off") => "it does not take requests from other computers".to_string(),
73 Some("peer_cannot_do_remote_ops") | Some("unknown_op") => "it runs a version that cannot share its Audit".to_string(),
74 Some(other) => format!("refused: {other}"),
75 None => error.map_or_else(|| "it did not answer".to_string(), |e| format!("failed: {e}")),
76 }
77}
78
79#[cfg(test)]
80#[path = "peers_tests.rs"]
81mod tests;