yah-qed 0.8.21

CI scheduler: pipelines, step DAGs, triggers, and pass/fail gating over task execution
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! Native-tarball packaging (R407-T2, W154).
//!
//! Emits a `.tar.gz` containing a static musl Rust binary plus a workload-spec
//! manifest. Kamaji consumes the tarball at deploy time and directly
//! fork+exec+cgroup+pidfd-supervises the binary — no systemd Portable Service,
//! no per-workload `.service` unit. The tarball doubles as the deploy artifact
//! and the manifest-of-record describing how to launch the workload.
//!
//! ## Layout inside the tarball
//!
//! ```text
//! bin/<basename>          ← the static musl binary, 0o755
//! manifest.toml           ← [`NativeTarballManifest`] serialized
//! ```
//!
//! Pure filesystem work; the runner-side dispatch (catalog lookup, validation
//! that the catalog entry actually declares `produces = ["native-tarball"]`)
//! lives in [`crate::runner::PipelineRunner::execute_step_package_native_tarball`].

use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};

use async_trait::async_trait;
use flate2::write::GzEncoder;
use flate2::Compression;
use serde::{Deserialize, Serialize};

/// The `manifest.toml` written into every native-tarball.
///
/// Forward-compatible — Kamaji readers should accept additive fields. Today
/// this carries the bare minimum needed to launch a workload: the binary's
/// in-tarball path, the target triple it was built for, and the env vars the
/// catalog entry declared. Capabilities, drain hooks, and probe shape land
/// alongside the Kamaji workload-spec proper.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct NativeTarballManifest {
    /// Catalog entry name (matches `[image].name` in the source TOML).
    pub name: String,
    /// Release version. Resolved at packaging time from
    /// `YAH_RELEASE_VERSION` env or the qed crate's compiled version
    /// (see [`crate::publish::resolve_release_version`]).
    pub version: String,
    /// Target-triple shorthand the binary was compiled for, e.g.
    /// `x86_64-unknown-linux-musl`.
    pub triple: String,
    /// Path to the executable *inside the tarball* (e.g. `bin/yubaba`).
    pub binary: String,
    /// Short human description (mirrors the catalog entry's `description`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Env vars the catalog entry declared. Kamaji applies these to the
    /// child before exec.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub env: BTreeMap<String, String>,
}

/// Write `<binary>` and `manifest.toml` into a `.tar.gz` at `output_path`.
///
/// The output directory is created if missing. The binary is stored at
/// `bin/<filename>` inside the tarball with mode `0o755`; the manifest is
/// stored at the top level as `manifest.toml`. Existing `output_path` is
/// truncated.
pub fn pack_native_tarball(
    binary_path: &Path,
    manifest: &NativeTarballManifest,
    output_path: &Path,
) -> std::io::Result<()> {
    if let Some(parent) = output_path.parent() {
        fs::create_dir_all(parent)?;
    }
    let manifest_toml = toml::to_string_pretty(manifest)
        .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;

    let file = fs::File::create(output_path)?;
    let gz = GzEncoder::new(file, Compression::default());
    let mut builder = tar::Builder::new(gz);

    let bin_basename = binary_path.file_name().ok_or_else(|| {
        std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            format!("binary path has no filename: {}", binary_path.display()),
        )
    })?;
    let in_tar_path = format!("bin/{}", bin_basename.to_string_lossy());

    let mut bin = fs::File::open(binary_path)?;
    let bin_meta = bin.metadata()?;
    let mut bin_header = tar::Header::new_gnu();
    bin_header.set_size(bin_meta.len());
    bin_header.set_mode(0o755);
    bin_header.set_mtime(0);
    bin_header.set_cksum();
    builder.append_data(&mut bin_header, &in_tar_path, &mut bin)?;

    let manifest_bytes = manifest_toml.as_bytes();
    let mut manifest_header = tar::Header::new_gnu();
    manifest_header.set_size(manifest_bytes.len() as u64);
    manifest_header.set_mode(0o644);
    manifest_header.set_mtime(0);
    manifest_header.set_cksum();
    builder.append_data(&mut manifest_header, "manifest.toml", manifest_bytes)?;

    let gz = builder.into_inner()?;
    gz.finish()?;
    Ok(())
}

/// Filesystem-safe tarball stem for a catalog image + triple pair. The runner
/// uses this for the on-disk filename so packaging and signing both resolve
/// the same path without re-deriving it (R407-T2 / R407-T5).
pub fn tarball_stem(image_name: &str, triple: &str) -> String {
    let raw = format!("{image_name}-{triple}");
    raw.chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || matches!(c, '_' | '.' | '-') {
                c
            } else {
                '_'
            }
        })
        .collect()
}

/// Path the packaging step writes (and the signing step reads) under
/// `<camp_root>/.yah/cache/native/<safe-stem>.tar.gz`. Single source of truth
/// for the on-disk convention so signing never drifts from packaging.
pub fn native_tarball_output_path(camp_root: &Path, image_name: &str, triple: &str) -> PathBuf {
    camp_root
        .join(".yah/cache/native")
        .join(format!("{}.tar.gz", tarball_stem(image_name, triple)))
}

// ── Sigstore signing seam (R407-T5, W154) ──────────────────────────────────
//
// W154: "Sigstore signing extends to native tarballs (same trust model,
// different artifact shape)." For OCI images, cosign signs the registry
// digest (`cosign sign --yes <ref>@<digest>`). For tarballs, the equivalent
// is `cosign sign-blob --yes`, which produces a detached signature and an
// associated certificate / Rekor bundle. The trust model is the same:
// keyless OIDC via the GHA token, identity matched at verification time by
// regex against the workflow identity, transparency log entry in Rekor.
//
// This module owns the abstraction; the runner attaches a concrete signer
// via [`crate::runner::PipelineRunner::with_signer`]. The default in every
// constructor is [`LoggingSigner`] — local `yah qed run` flows write
// placeholder bytes and log a warning rather than fail when cosign isn't on
// PATH. A real release pipeline (GHA or yubaba-run) wires [`CosignSigner`]
// explicitly so an unsigned tarball never silently ships.

/// On-disk paths emitted by a successful [`SigstoreSigner::sign_blob`] call.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SignedBlob {
    /// Detached signature, conventionally `<blob>.sig`.
    pub signature_path: PathBuf,
    /// Signing certificate (the leaf cert with the OIDC identity), `<blob>.crt`.
    pub certificate_path: PathBuf,
    /// Cosign bundle (signature + cert + Rekor inclusion proof), `<blob>.bundle`.
    /// `None` when the signer doesn't emit a bundle.
    pub bundle_path: Option<PathBuf>,
}

/// Sign a single blob (a native tarball, conventionally) with the same
/// Sigstore keyless OIDC trust model that signs the OCI images today. The
/// signer writes the resulting `.sig` / `.crt` / `.bundle` files next to the
/// blob and reports their paths back so the caller can publish them.
#[async_trait]
pub trait SigstoreSigner: Send + Sync {
    async fn sign_blob(&self, blob_path: &Path) -> std::io::Result<SignedBlob>;
}

/// Append `suffix` (e.g. `.sig`) to the blob's full filename — extends, does
/// not replace. `Path::with_extension` would turn `foo.tar.gz` into
/// `foo.tar.sig`; we want `foo.tar.gz.sig` so the channel layout shows the
/// signature next to the artifact it covers.
fn append_suffix(blob: &Path, suffix: &str) -> PathBuf {
    let mut s = blob.as_os_str().to_owned();
    s.push(suffix);
    PathBuf::from(s)
}

/// Default production signer — shells out to `cosign sign-blob --yes`.
/// Set `cosign_bin` to `"cosign"` (PATH lookup) or an absolute path; a
/// missing binary surfaces as a `NotFound` IO error so the runner reports a
/// clean step-failure message at the call site.
pub struct CosignSigner {
    pub cosign_bin: PathBuf,
}

impl Default for CosignSigner {
    fn default() -> Self {
        Self {
            cosign_bin: PathBuf::from("cosign"),
        }
    }
}

#[async_trait]
impl SigstoreSigner for CosignSigner {
    async fn sign_blob(&self, blob_path: &Path) -> std::io::Result<SignedBlob> {
        let sig = append_suffix(blob_path, ".sig");
        let crt = append_suffix(blob_path, ".crt");
        let bundle = append_suffix(blob_path, ".bundle");

        let status = tokio::process::Command::new(&self.cosign_bin)
            .arg("sign-blob")
            .arg("--yes")
            .arg("--output-signature")
            .arg(&sig)
            .arg("--output-certificate")
            .arg(&crt)
            .arg("--bundle")
            .arg(&bundle)
            .arg(blob_path)
            .status()
            .await?;
        if !status.success() {
            return Err(std::io::Error::new(
                std::io::ErrorKind::Other,
                format!(
                    "cosign sign-blob exited with status {} (blob: {})",
                    status,
                    blob_path.display(),
                ),
            ));
        }
        Ok(SignedBlob {
            signature_path: sig,
            certificate_path: crt,
            bundle_path: Some(bundle),
        })
    }
}

/// Test / local-dev fake — writes deterministic placeholder bytes so a
/// pipeline's `sign-native-tarball` step succeeds without a real cosign
/// install. NOT suitable for releases; release CI must wire [`CosignSigner`]
/// explicitly.
pub struct LoggingSigner;

#[async_trait]
impl SigstoreSigner for LoggingSigner {
    async fn sign_blob(&self, blob_path: &Path) -> std::io::Result<SignedBlob> {
        if !blob_path.is_file() {
            return Err(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                format!("blob to sign not found: {}", blob_path.display()),
            ));
        }
        let sig = append_suffix(blob_path, ".sig");
        let crt = append_suffix(blob_path, ".crt");
        let bundle = append_suffix(blob_path, ".bundle");
        fs::write(
            &sig,
            b"# yah logging-signer: placeholder signature (NOT a real cosign signature)\n",
        )?;
        fs::write(
            &crt,
            b"# yah logging-signer: placeholder certificate (NOT a real cosign cert)\n",
        )?;
        fs::write(
            &bundle,
            b"{\"_comment\":\"yah logging-signer placeholder bundle\"}\n",
        )?;
        tracing::warn!(
            blob = %blob_path.display(),
            "qed sign-native-tarball: LoggingSigner emitted placeholder \
             .sig/.crt/.bundle (cosign not wired)"
        );
        Ok(SignedBlob {
            signature_path: sig,
            certificate_path: crt,
            bundle_path: Some(bundle),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use flate2::read::GzDecoder;
    use std::io::{Read, Write};
    use tempfile::TempDir;

    fn write_dummy_binary(dir: &Path, name: &str, body: &[u8]) -> std::path::PathBuf {
        let path = dir.join(name);
        let mut f = fs::File::create(&path).unwrap();
        f.write_all(body).unwrap();
        path
    }

    fn sample_manifest() -> NativeTarballManifest {
        NativeTarballManifest {
            name: "yah-yubaba".into(),
            version: "0.8.6".into(),
            triple: "x86_64-unknown-linux-musl".into(),
            binary: "bin/yubaba".into(),
            description: Some("Native musl-static yubaba".into()),
            env: BTreeMap::from([("RUST_LOG".into(), "info".into())]),
        }
    }

    fn list_tar_entries(path: &Path) -> Vec<(String, Vec<u8>, u32)> {
        let f = fs::File::open(path).unwrap();
        let gz = GzDecoder::new(f);
        let mut archive = tar::Archive::new(gz);
        let mut out = Vec::new();
        for entry in archive.entries().unwrap() {
            let mut entry = entry.unwrap();
            let header_path = entry.path().unwrap().to_string_lossy().into_owned();
            let mode = entry.header().mode().unwrap();
            let mut buf = Vec::new();
            entry.read_to_end(&mut buf).unwrap();
            out.push((header_path, buf, mode));
        }
        out.sort_by(|a, b| a.0.cmp(&b.0));
        out
    }

    #[test]
    fn pack_writes_binary_and_manifest_with_expected_modes() {
        let dir = TempDir::new().unwrap();
        let bin = write_dummy_binary(dir.path(), "yubaba", b"\x7fELF-fake-musl-binary");
        let out = dir
            .path()
            .join("out/yah-yubaba-x86_64-unknown-linux-musl.tar.gz");

        let manifest = sample_manifest();
        pack_native_tarball(&bin, &manifest, &out).unwrap();

        assert!(out.is_file(), "tarball materialised at {}", out.display());
        let entries = list_tar_entries(&out);
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0].0, "bin/yubaba");
        assert_eq!(entries[0].1, b"\x7fELF-fake-musl-binary");
        assert_eq!(entries[0].2, 0o755);
        assert_eq!(entries[1].0, "manifest.toml");
        assert_eq!(entries[1].2, 0o644);
    }

    #[test]
    fn pack_manifest_roundtrips_through_toml() {
        let dir = TempDir::new().unwrap();
        let bin = write_dummy_binary(dir.path(), "yubaba", b"x");
        let out = dir.path().join("yubaba.tar.gz");
        let manifest = sample_manifest();
        pack_native_tarball(&bin, &manifest, &out).unwrap();

        let entries = list_tar_entries(&out);
        let manifest_entry = entries
            .iter()
            .find(|(p, _, _)| p == "manifest.toml")
            .expect("manifest.toml present");
        let text = std::str::from_utf8(&manifest_entry.1).unwrap();
        let parsed: NativeTarballManifest = toml::from_str(text).expect("manifest.toml parses");
        assert_eq!(parsed, manifest);
    }

    #[test]
    fn pack_creates_missing_parent_dirs() {
        let dir = TempDir::new().unwrap();
        let bin = write_dummy_binary(dir.path(), "yubaba", b"x");
        let out = dir.path().join("deeply/nested/path/yubaba.tar.gz");
        pack_native_tarball(&bin, &sample_manifest(), &out).unwrap();
        assert!(out.is_file());
    }

    #[test]
    fn pack_missing_binary_is_io_error() {
        let dir = TempDir::new().unwrap();
        let bogus = dir.path().join("does-not-exist");
        let out = dir.path().join("out.tar.gz");
        let err = pack_native_tarball(&bogus, &sample_manifest(), &out).unwrap_err();
        assert_eq!(err.kind(), std::io::ErrorKind::NotFound);
    }

    // ── R407-T5 path helper + signer ──────────────────────────────────────

    #[test]
    fn tarball_stem_replaces_unsafe_chars() {
        assert_eq!(
            tarball_stem("yah-yubaba", "x86_64-unknown-linux-musl"),
            "yah-yubaba-x86_64-unknown-linux-musl",
        );
        // `/` and `:` are not in the [A-Za-z0-9_.-] allowlist — both rewrite.
        assert_eq!(
            tarball_stem("ghcr.io/yah-ai/yah-yubaba", "linux:musl"),
            "ghcr.io_yah-ai_yah-yubaba-linux_musl",
        );
    }

    #[test]
    fn native_tarball_output_path_matches_runner_convention() {
        // The runner writes/reads `<camp>/.yah/cache/native/<stem>.tar.gz` —
        // this helper is the single source of truth, so packaging (T2) and
        // signing (T5) never drift.
        let camp = Path::new("/camp");
        let out = native_tarball_output_path(camp, "yah-yubaba", "x86_64-unknown-linux-musl");
        assert_eq!(
            out,
            Path::new("/camp/.yah/cache/native/yah-yubaba-x86_64-unknown-linux-musl.tar.gz"),
        );
    }

    #[tokio::test]
    async fn logging_signer_writes_placeholder_sig_crt_bundle_next_to_blob() {
        let dir = TempDir::new().unwrap();
        let blob = dir
            .path()
            .join("yah-yubaba-x86_64-unknown-linux-musl.tar.gz");
        fs::write(&blob, b"<fake tarball bytes>").unwrap();

        let signer = LoggingSigner;
        let signed = signer.sign_blob(&blob).await.unwrap();

        // Suffixes are appended, not substituted — keep the `.tar.gz` so the
        // signature reads next to its artifact in the channel layout.
        assert_eq!(
            signed.signature_path,
            blob.with_file_name(format!(
                "{}.sig",
                blob.file_name().unwrap().to_string_lossy()
            )),
        );
        assert_eq!(
            signed.certificate_path,
            blob.with_file_name(format!(
                "{}.crt",
                blob.file_name().unwrap().to_string_lossy()
            )),
        );
        let bundle = signed.bundle_path.expect("LoggingSigner emits a bundle");
        assert_eq!(
            bundle,
            blob.with_file_name(format!(
                "{}.bundle",
                blob.file_name().unwrap().to_string_lossy()
            )),
        );

        // The placeholder files are non-empty so downstream tooling that
        // counts bytes / hashes contents doesn't get an empty-file footgun.
        assert!(fs::read(&signed.signature_path).unwrap().len() > 10);
        assert!(fs::read(&signed.certificate_path).unwrap().len() > 10);
        assert!(fs::read(&bundle).unwrap().len() > 10);
    }

    #[tokio::test]
    async fn logging_signer_missing_blob_is_not_found_error() {
        let dir = TempDir::new().unwrap();
        let bogus = dir.path().join("does-not-exist.tar.gz");
        let err = LoggingSigner.sign_blob(&bogus).await.unwrap_err();
        assert_eq!(err.kind(), std::io::ErrorKind::NotFound);
    }

    #[tokio::test]
    async fn cosign_signer_missing_binary_surfaces_not_found() {
        // No cosign on PATH in the test sandbox — point at an absolute path
        // we know doesn't exist. Exercises the std::io::ErrorKind::NotFound
        // branch the runner converts into a `StepFailed` with a clean
        // operator-facing message.
        let dir = TempDir::new().unwrap();
        let blob = dir.path().join("artifact.tar.gz");
        fs::write(&blob, b"x").unwrap();
        let signer = CosignSigner {
            cosign_bin: PathBuf::from("/definitely/not/a/real/cosign-binary"),
        };
        let err = signer.sign_blob(&blob).await.unwrap_err();
        assert_eq!(err.kind(), std::io::ErrorKind::NotFound);
    }
}