diff options
| -rw-r--r-- | src/pipeline/steps.rs | 16 | ||||
| -rw-r--r-- | src/watch/digests.rs | 4 | ||||
| -rw-r--r-- | src/watch/mod.rs | 3 | ||||
| -rw-r--r-- | src/watch/sources.rs | 44 | ||||
| -rw-r--r-- | tests/architecture-budget.json | 6 |
5 files changed, 54 insertions, 19 deletions
diff --git a/src/pipeline/steps.rs b/src/pipeline/steps.rs index 3a6cff0..ba3a08e 100644 --- a/src/pipeline/steps.rs +++ b/src/pipeline/steps.rs @@ -5,7 +5,7 @@ use crate::error::{Error, Result}; use crate::git; use crate::pipeline::{Collect, CollectedPkg, Ctx, StepResult}; use crate::sandbox::Cmd; -use crate::watch::digests::cached_path; +use crate::watch::sources; pub fn run_step(ctx: &Ctx, step: &str) -> Result<StepResult> { match step { @@ -42,8 +42,7 @@ fn materialize(ctx: &Ctx) -> Result<StepResult> { } let _ = std::fs::remove_dir_all(&ctx.srcdir); git::materialize(&mirror, &ctx.src.aur_commit, &ctx.srcdir)?; - let tree = crate::watch::mirror::read_tree(&mirror, &ctx.src.aur_commit)?; - let computed = tree.recipe_hash(); + let computed = sources::recipe_hash(&mirror, &ctx.src.aur_commit)?; if !ctx.src.recipe_hash.is_empty() && computed != ctx.src.recipe_hash { return Err(Error::fail_closed(format!( "recipe_hash mismatch: approved {}, materialized {}", @@ -212,16 +211,7 @@ fn sources(ctx: &Ctx) -> Result<StepResult> { "local" => verified += 1, "vcs" | "git" | "svn" | "hg" | "bzr" => verified += 1, _ => { - let (digest, _size) = match cached_path(&ctx.db, &d.url, &d.digest) { - Some(p) => crate::hash::digest_file(&p)?, - None => crate::watch::digests::download_and_cache(&ctx.cfg, &ctx.db, &d.url)?, - }; - if digest != d.digest { - return Err(Error::fail_closed(format!( - "source {} hashed to {digest} but the candidate recorded {}", - d.url, d.digest - ))); - } + sources::verify_cached(&ctx.cfg, &ctx.db, &d.url, &d.digest)?; verified += 1; } } diff --git a/src/watch/digests.rs b/src/watch/digests.rs index b95bd6c..534b5bd 100644 --- a/src/watch/digests.rs +++ b/src/watch/digests.rs @@ -92,7 +92,7 @@ pub fn resolve_sources( Ok(out) } -pub fn download_and_cache(cfg: &Config, db: &Db, url: &str) -> Result<(String, u64)> { +pub(crate) fn download_and_cache(cfg: &Config, db: &Db, url: &str) -> Result<(String, u64)> { let tmp = cfg.source_cache_dir().join(format!("tmp-{}", crate::hash::short(url.as_bytes(), 16))); let (digest, size) = crate::fetch::fetch_to(url, &tmp)?; let dest = cfg.source_cache_path(&digest); @@ -109,7 +109,7 @@ pub fn download_and_cache(cfg: &Config, db: &Db, url: &str) -> Result<(String, u Ok((digest, size)) } -pub fn cached_path(db: &Db, url: &str, digest: &str) -> Option<std::path::PathBuf> { +pub(crate) fn cached_path(db: &Db, url: &str, digest: &str) -> Option<std::path::PathBuf> { db::source_cache_path(db, url, digest) .ok() .flatten() diff --git a/src/watch/mod.rs b/src/watch/mod.rs index 8c7604f..b3c0533 100644 --- a/src/watch/mod.rs +++ b/src/watch/mod.rs @@ -2,10 +2,11 @@ pub mod detect; pub mod digests; pub mod mirror; pub mod scheduler; +pub mod sources; pub mod staleness; pub mod upstream; pub use detect::{check_pkgbase, CheckReport, Outcome}; -pub use digests::{cached_path, download_and_cache, resolve_sources}; +pub use digests::resolve_sources; pub use mirror::{read_tree, sync_mirror, Tree}; pub use scheduler::spawn_scheduler; diff --git a/src/watch/sources.rs b/src/watch/sources.rs new file mode 100644 index 0000000..7b56774 --- /dev/null +++ b/src/watch/sources.rs @@ -0,0 +1,44 @@ +//! The source interface the build pipeline asks watch for. +//! +//! The step machine used to reach into two of watch's internals and reassemble +//! the rules itself: `mirror::read_tree` plus `Tree::recipe_hash` to re-derive +//! the hash a reviewer approved, and `digests::cached_path` plus +//! `digests::download_and_cache` plus its own comparison to check a payload. +//! That put the fail-closed test — the one thing that must never be skipped — +//! in whichever step happened to want a file, and left the pipeline knowing how +//! watch caches. +//! +//! Two calls cover what it actually needs: what a tree hashes to, and a +//! guarantee that a cached source is the bytes its name claims. + +use std::path::Path; + +use crate::config::Config; +use crate::db::Db; +use crate::error::{Error, Result}; +use crate::watch::digests::{cached_path, download_and_cache}; +use crate::watch::mirror::read_tree; + +/// The recipe hash of a mirror at a commit — the same value the reviewer saw, +/// so a build can refuse a tree that moved underneath the approval. +pub fn recipe_hash(mirror: &Path, commit: &str) -> Result<String> { + Ok(read_tree(mirror, commit)?.recipe_hash()) +} + +/// Ensure `url` is cached and that what is cached hashes to `digest`. +/// +/// Fails closed on a mismatch rather than handing back bytes that are not what +/// was approved: a source that quietly changed is exactly the case the whole +/// approval chain exists to catch. +pub fn verify_cached(cfg: &Config, db: &Db, url: &str, digest: &str) -> Result<()> { + let found = match cached_path(db, url, digest) { + Some(p) => crate::hash::digest_file(&p)?.0, + None => download_and_cache(cfg, db, url)?.0, + }; + if found != digest { + return Err(Error::fail_closed(format!( + "source {url} hashed to {found} but the candidate recorded {digest}" + ))); + } + Ok(()) +} diff --git a/tests/architecture-budget.json b/tests/architecture-budget.json index ce8faef..5559831 100644 --- a/tests/architecture-budget.json +++ b/tests/architecture-budget.json @@ -287,15 +287,15 @@ "test": 0 }, "watch": { - "dead": 11, + "dead": 12, "deadonly": 7, "glob": 0, "largest": 317, "largest_file": "src/watch/detect.rs", "lines": 969, "priv": 0, - "pub": 31, - "test": 4 + "pub": 30, + "test": 5 } }, "totals": { |
