OXYGENHUB

Coverage / src/cli/tlog.rs 0.0%

0 of 94 lines run, 0 of 20 functions · run #322 on main · run missed

1
//! `oxy tlog verify` (spec §14.8, oxygenhub#238): audit a hub's whole
2
//! transparency log and list the entries that concern you.
3
4
use super::json;
5
use super::open;
6
use super::ui;
7
use anyhow::{anyhow, Result};
8
use oxy_core::hash::KeyId;
9
use std::path::Path;
10
110×
pub fn verify(hub: Option<&str>, key: Option<&str>, all: bool, as_json: bool) -> Result<i32> {
120×
    let repo = open()?;
130×
    let remote = repo.default_remote();
140×
    let url = match hub {
150×
        Some(h) if h.contains("://") => h.to_string(),
160×
        Some(h) => format!("https://{h}"),
170×
        None => repo.config.remotes.get(&remote).cloned().ok_or_else(|| {
180×
            anyhow!("name the hub to check (this checkout has no remote {remote})")
190×
        })?,
20
    };
210×
    let ca = repo
220×
        .config
230×
        .remote_cas
240×
        .get(&remote)
250×
        .map(Path::new)
260×
        .filter(|_| hub.is_none());
270×
    let audit = repo.audit_log(&url, ca).map_err(|e| anyhow!("{e}"))?;
280×
    let key = match key {
290×
        Some(k) => Some(
300×
            KeyId::from_hex(k)
310×
                .ok_or_else(|| anyhow!("--key takes a full KeyId (64 hex digits)"))?,
32
        ),
330×
        None => None,
34
    };
350×
    let rid = repo.id().ok();
360×
    let listed: Vec<(usize, &oxy_core::tlog::Entry)> = audit
370×
        .entries
380×
        .iter()
390×
        .enumerate()
400×
        .filter(|(_, e)| match (&key, all) {
410×
            (_, true) => true,
420×
            (Some(k), _) => e.signers.contains(k),
430×
            (None, _) => Some(e.repo) == rid,
440×
        })
450×
        .collect();
460×
    let what = match (&key, all) {
470×
        (_, true) => "every entry".to_string(),
480×
        (Some(k), _) => format!("entries signed by {}", k.short(12)),
490×
        (None, _) => "this repository's entries".to_string(),
50
    };
510×
    if as_json {
520×
        println!(
53
            "{}",
540×
            json::obj([
550×
                ("hub", json::s(&audit.hub)),
560×
                ("origin", json::s(&audit.checkpoint.origin)),
570×
                ("size", json::n(audit.checkpoint.size)),
580×
                (
590×
                    "root",
600×
                    json::s(&oxy_core::hash::ObjectRef(audit.checkpoint.root).to_hex())
610×
                ),
62
                (
630×
                    "entries",
640×
                    json::arr(listed.iter().map(|(i, e)| {
650×
                        json::obj([
660×
                            ("index", json::n(*i)),
670×
                            ("kind", json::s(&e.kind)),
68
                            (
690×
                                "repo",
700×
                                json::s(
710×
                                    &e.repo
720×
                                        .iter()
730×
                                        .map(|b| format!("{b:02x}"))
740×
                                        .collect::<String>(),
75
                                ),
76
                            ),
770×
                            ("object", json::s(&e.object.to_hex())),
78
                            (
790×
                                "signers",
800×
                                json::arr(e.signers.iter().map(|k| json::s(&k.to_hex()))),
81
                            ),
820×
                            ("received_ms", json::n(e.received)),
83
                        ])
840×
                    })),
85
                ),
86
            ])
87
        );
880×
        return Ok(0);
890×
    }
900×
    println!(
91
        "{} {}: {} entries make the root its log key signed, and extend what you saw before",
920×
        ui::done("Verified"),
930×
        ui::path(&audit.checkpoint.origin),
94
        audit.checkpoint.size
95
    );
960×
    if listed.is_empty() {
970×
        println!("  {} {}", ui::note(), ui::dim(format!("no {what} in it")));
980×
        return Ok(0);
990×
    }
1000×
    println!(
101
        "  {} {}",
1020×
        ui::note(),
1030×
        ui::dim(format!("{} of {what}:", listed.len()))
104
    );
1050×
    for (i, e) in listed {
1060×
        let signers: Vec<String> = e.signers.iter().map(|k| k.short(12)).collect();
1070×
        println!(
108
            "  #{i:<6} {:<11} {} {} {}",
109
            e.kind,
1100×
            ui::hash(e.object.short(12)),
1110×
            ui::dim(if signers.is_empty() {
1120×
                "unsigned".to_string()
113
            } else {
1140×
                format!("by {}", signers.join(", "))
115
            }),
1160×
            ui::dim(ui::ago(e.received))
117
        );
118
    }
1190×
    Ok(0)
1200×
}