// SPDX-License-Identifier: CPAL-1.0 // Copyright (c) 2026 Marek Novák. Exhibit A notice: LICENSE at the repository root. //! Nothing unsigned runs (mandatory, tests first): a tampered artifact, a wrong key, a missing or //! garbled signature, a release of something else: refused and reported, and the target is never //! called. Also roles, replays and what the target's outcome becomes in the report. #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] mod common; use std::cell::RefCell; use common::*; use legatus::pipeline::{Context, handle}; use legatus_linux::Outcome; use legatus_protocol::{JobGuard, Report, Roles, Status, Step}; const ART: &[u8] = b"pretend this is shop-1.4.0.tar.gz"; struct Run { _dir: tempfile::TempDir, ctx: Context, guard: JobGuard, key: ReleaseKey, target: FakeTarget, } fn setup() -> Run { let dir = tempfile::tempdir().unwrap(); let key = ReleaseKey::new(); let ctx = Context { team: "acme".into(), roles: Roles { build: false, deploy: true, }, release_keys: vec![key.public()], downloads: dir.path().join("downloads"), }; Run { _dir: dir, ctx, guard: JobGuard::new(), key, target: FakeTarget::new(), } } impl Run { fn go(&mut self, job: &[u8], served: &[u8]) -> (Vec, u32) { let fetch = FakeFetch { bytes: served.to_vec(), fetched: RefCell::new(0), }; let mut reports = Vec::new(); handle( job, now(), &mut self.ctx, &mut self.guard, &fetch, &self.target, &mut |r| reports.push(r), ); let n = *fetch.fetched.borrow(); (reports, n) } fn signed_job(&self, n: u8) -> Vec { let m = manifest("acme", "shop", "1.4.0", "shop.tar.gz", ART); deploy_job(&job_id(n), "1.4.0", &m, &self.key.sign(m.as_bytes())) } fn untouched(&self) -> bool { self.target.calls.borrow().is_empty() } fn no_leftovers(&self) -> bool { std::fs::read_dir(&self.ctx.downloads) .map(|d| d.count() == 0) .unwrap_or(true) } } fn last(r: &[Report]) -> &Report { r.last().unwrap() } #[test] fn a_signed_release_reaches_the_target_with_its_checked_bytes() { let mut run = setup(); let (r, fetched) = run.go(&run.signed_job(1), ART); assert_eq!(fetched, 1); assert_eq!(r[0].status, Status::Accepted); assert_eq!(last(&r).status, Status::Succeeded, "{r:?}"); assert_eq!(last(&r).deployed.as_ref().unwrap().version, "1.4.0"); let calls = run.target.calls.borrow(); assert_eq!(calls.len(), 1); assert_eq!(calls[0].2, sha256_hex(ART)); assert_eq!(calls[0].3, ART); drop(calls); assert!(run.no_leftovers(), "the download is deleted after the job"); } #[test] fn a_tampered_artifact_is_refused() { let mut run = setup(); let mut evil = ART.to_vec(); evil[5] ^= 1; let (r, _) = run.go(&run.signed_job(1), &evil); assert_eq!(last(&r).status, Status::Refused); assert_eq!(last(&r).step, Some(Step::Download)); assert!( last(&r).message.starts_with("tampered: "), "{}", last(&r).message ); assert!(run.untouched()); assert!(run.no_leftovers()); // Longer than signed: refused too, and never more than one byte past the signed size is read. let mut long = ART.to_vec(); long.extend_from_slice(&[0u8; 10_000]); let (r, _) = run.go(&run.signed_job(2), &long); assert!( last(&r).message.starts_with("tampered: "), "{}", last(&r).message ); assert!(run.untouched()); } #[test] fn a_wrong_key_is_refused_before_anything_is_downloaded() { let mut run = setup(); let stranger = ReleaseKey::new(); let m = manifest("acme", "shop", "1.4.0", "shop.tar.gz", ART); let job = deploy_job(&job_id(1), "1.4.0", &m, &stranger.sign(m.as_bytes())); let (r, fetched) = run.go(&job, ART); assert_eq!(fetched, 0); assert_eq!(r.len(), 1); assert_eq!(r[0].status, Status::Refused); assert_eq!(r[0].step, Some(Step::Verify)); assert!( r[0].message.starts_with("unknown_key: "), "{}", r[0].message ); assert!(run.untouched()); } #[test] fn a_missing_or_garbled_signature_is_refused() { let mut run = setup(); let m = manifest("acme", "shop", "1.4.0", "shop.tar.gz", ART); let (r, fetched) = run.go(&deploy_job(&job_id(1), "1.4.0", &m, ""), ART); assert!(r[0].message.starts_with("unsigned: "), "{}", r[0].message); assert_eq!(fetched, 0); let (r, fetched) = run.go( &deploy_job(&job_id(2), "1.4.0", &m, "untrusted comment: x\nAAAA\n"), ART, ); assert!( r[0].message.starts_with("bad_signature: "), "{}", r[0].message ); assert_eq!(fetched, 0); // A good signature over another manifest. let other = manifest("acme", "shop", "1.4.0", "shop.tar.gz", b"other bytes"); let (r, _) = run.go( &deploy_job(&job_id(3), "1.4.0", &m, &run.key.sign(other.as_bytes())), ART, ); assert!( r[0].message.starts_with("bad_signature: "), "{}", r[0].message ); assert!(run.untouched()); } #[test] fn a_signed_release_for_another_team_or_version_is_refused() { let mut run = setup(); let m = manifest("globex", "shop", "1.4.0", "shop.tar.gz", ART); let (r, _) = run.go( &deploy_job(&job_id(1), "1.4.0", &m, &run.key.sign(m.as_bytes())), ART, ); assert!( r[0].message.starts_with("wrong_release: "), "{}", r[0].message ); // The job asks for 1.5.0 but carries 1.4.0's signed manifest (a downgrade in disguise). let m = manifest("acme", "shop", "1.4.0", "shop.tar.gz", ART); let (r, _) = run.go( &deploy_job(&job_id(2), "1.5.0", &m, &run.key.sign(m.as_bytes())), ART, ); assert!( r[0].message.starts_with("wrong_release: "), "{}", r[0].message ); assert!(run.untouched()); } #[test] fn deploy_here_off_refuses_and_a_config_job_turns_it_on() { let mut run = setup(); run.ctx.roles = Roles::default(); let (r, fetched) = run.go(&run.signed_job(1), ART); assert!(r[0].message.starts_with("role_off: "), "{}", r[0].message); assert_eq!(r[0].step, Some(Step::Role)); assert_eq!(fetched, 0); let cfg = legatus_protocol::msg::job_json( &job_id(2), now(), now() + 60, &legatus_protocol::JobBody::Config(legatus_protocol::msg::ConfigJob { roles: Roles { build: false, deploy: true, }, }), ); let (r, _) = run.go(&cfg, b""); assert_eq!(r[0].status, Status::Succeeded); assert!(run.ctx.roles.deploy); let (r, _) = run.go(&run.signed_job(3), ART); assert_eq!(last(&r).status, Status::Succeeded); } #[test] fn a_replayed_job_is_refused() { let mut run = setup(); let job = run.signed_job(1); run.go(&job, ART); let (r, fetched) = run.go(&job, ART); assert_eq!(r.len(), 1); assert!(r[0].message.starts_with("replayed: "), "{}", r[0].message); assert_eq!(fetched, 0); assert_eq!( run.target.calls.borrow().len(), 1, "deployed once, not twice" ); } #[test] fn unknown_and_invalid_jobs_are_reported() { let mut run = setup(); let unknown = format!( r#"{{"v":1,"id":"{}","issued_at":{},"expires_at":{},"kind":"build","spec":{{}}}}"#, job_id(1), now(), now() + 60 ); let (r, _) = run.go(unknown.as_bytes(), b""); assert!( r[0].message.starts_with("unsupported: "), "{}", r[0].message ); let bad = String::from_utf8(run.signed_job(2)) .unwrap() .replace(r#""unit":"shop.service""#, r#""unit":"../x""#); let (r, _) = run.go(bad.as_bytes(), ART); assert!( r[0].message.starts_with("bad_request: "), "{}", r[0].message ); // Garbage: no id, nothing to report to. let (r, _) = run.go(b"{not json", ART); assert!(r.is_empty()); assert!(run.untouched()); } #[test] fn the_targets_outcome_becomes_the_report() { let mut run = setup(); *run.target.outcome.borrow_mut() = Ok(Outcome::RolledBack { to: "1.3.0".into(), healthy: true, reason: "the health check did not pass within 5 s".into(), }); let (r, _) = run.go(&run.signed_job(1), ART); let l = last(&r); assert_eq!(l.status, Status::Failed); assert_eq!(l.rolled_back, Some(true)); assert_eq!(l.deployed.as_ref().unwrap().version, "1.3.0"); assert!(l.message.contains("rolled back to 1.3.0"), "{}", l.message); *run.target.outcome.borrow_mut() = Err("archive".into()); let (r, _) = run.go(&run.signed_job(2), ART); assert_eq!(last(&r).status, Status::Refused); assert!( last(&r).message.starts_with("bad_archive: "), "{}", last(&r).message ); }