Skip to main content

mkit_cli/commands/
attest.rs

1//! `mkit attest` — produce a signed DSSE attestation for a commit.
2//!
3//! ```text
4//! mkit attest [--commit <hash>] [--algorithm ed25519|secp256k1|p256]
5//!             [--signer repo-key|external|keystore]
6//!             [--predicate-type <URI>] [--predicate-file <path>]
7//!             [--external-signer-arg <V>]...
8//!             [--additional-signer "<spec>"]...
9//! ```
10//!
11//! `--external-signer-arg` is repeatable; each instance adds one
12//! argv token to the external signer subprocess. If any are passed,
13//! they REPLACE (not append to) `attest.external_signer_args` from
14//! `.mkit/config` — per-invocation override for "sign with tag X
15//! this one time" that avoids shell-quoting hell.
16//!
17//! Defaults:
18//! * `--commit` — HEAD.
19//! * `--algorithm` — `attest.default_algorithm` in config, else `ed25519`.
20//! * `--signer` — `attest.signer` in config, else `repo-key`.
21//! * `--predicate-type` —
22//!   `https://github.com/officialunofficial/mkit/spec/predicate/empty/v1`.
23//! * `--predicate-file` — omitted ⇒ `{}`.
24//!
25//! Multi-signature envelopes are produced by passing one or more
26//! `--additional-signer` flags after the primary signer. Each spec is
27//! a comma-separated `key=value` list:
28//!
29//! ```text
30//! --additional-signer "algorithm=<algo>,signer=<kind>[,path=<file-or-binary>][,args=<a>|<b>|<c>]"
31//! ```
32//!
33//! The optional `args=` clause is pipe-separated (commas would clash
34//! with the outer key=value separator) and applies only when
35//! `signer=external`. Each pipe-separated token becomes one argv
36//! entry for the child process, same as `--external-signer-arg` on
37//! the primary signer.
38//!
39//! Signers are invoked in order (primary first, then each
40//! `--additional-signer` as they appear on the command line) and the
41//! resulting `{keyid, sig}` tuples are written into one envelope in
42//! that same order. Any signer failure aborts the attest — no
43//! partial envelopes are written to disk.
44//!
45//! On success, prints the att-id (64 hex chars) and exits 0.
46
47use std::io::Write;
48use std::path::Path;
49
50use clap::Parser;
51use mkit_attest::{Algorithm, Envelope, PAYLOAD_TYPE_IN_TOTO, Sig, Signer, statement, store};
52use mkit_core::hash::Hash;
53use mkit_core::{hash as hash_mod, refs};
54
55use crate::clap_shim;
56use crate::commands::attest_factory::{self, FactoryError};
57use crate::config::Config;
58use crate::exit;
59
60/// Default predicate type URI — placeholder; real callers pass their own.
61///
62/// Uses the GitHub-anchored URI scheme defined in
63/// `docs/SPEC-ATTESTATIONS.md` §6.4
64/// (`https://github.com/officialunofficial/mkit/spec/predicate/<name>/v<n>`)
65/// so the only predicate URI mkit ships out-of-the-box points at a
66/// location the project actually controls.
67const DEFAULT_PREDICATE_TYPE: &str =
68    "https://github.com/officialunofficial/mkit/spec/predicate/empty/v1";
69
70/// Hard cap on a `--predicate-file` body (#223). A DSSE predicate is a
71/// small JSON object; refusing anything past 1 MiB stops a runaway or
72/// hostile file from being slurped whole into memory before the JSON
73/// parse even runs.
74const MAX_PREDICATE_BYTES: u64 = 1024 * 1024;
75
76#[derive(Debug, Parser)]
77#[command(
78    name = "mkit attest",
79    about = "Produce a signed DSSE attestation for a commit."
80)]
81#[allow(clippy::struct_field_names)]
82struct Args {
83    /// Commit hash to attest. Defaults to HEAD.
84    #[arg(long, value_name = "HASH")]
85    commit: Option<String>,
86    /// Algorithm: `ed25519`, `secp256k1`, or `p256`.
87    #[arg(long, value_name = "ALG")]
88    algorithm: Option<String>,
89    /// Signer kind: `repo-key` (default) or `external`.
90    #[arg(long, value_name = "KIND")]
91    signer: Option<String>,
92    /// Predicate type URI.
93    #[arg(long = "predicate-type", value_name = "URI")]
94    predicate_type: Option<String>,
95    /// Path to a JSON predicate body.
96    #[arg(long = "predicate-file", value_name = "PATH")]
97    predicate_file: Option<String>,
98    /// Repeatable `--additional-signer "<spec>"`. Each spec is a
99    /// comma-separated `key=value` list parsed downstream — see the
100    /// module docstring.
101    #[arg(long = "additional-signer", value_name = "SPEC")]
102    additional_signers: Vec<String>,
103    /// Repeatable `--external-signer-arg <V>`. If any instance is
104    /// supplied, the full list REPLACES `attest.external_signer_args`
105    /// from config (not appended). Empty list ⇒ flag was not passed.
106    ///
107    /// `allow_hyphen_values` is set so users can pass values that
108    /// start with `-` / `--` (e.g. `--external-signer-arg --tag`)
109    /// without quoting — the hand-rolled parser this replaces
110    /// accepted those literally and we preserve that.
111    #[arg(
112        long = "external-signer-arg",
113        value_name = "ARG",
114        allow_hyphen_values = true
115    )]
116    external_signer_args_vec: Vec<String>,
117}
118
119impl Args {
120    /// Convert the raw `Vec<String>` form into `Option<Vec<…>>`:
121    /// `None` means "flag was not passed; fall back to config";
122    /// `Some(_)` means "flag was passed with these values".
123    fn external_signer_args(&self) -> Option<Vec<String>> {
124        if self.external_signer_args_vec.is_empty() {
125            None
126        } else {
127            Some(self.external_signer_args_vec.clone())
128        }
129    }
130}
131
132/// Parsed `--additional-signer` spec. The `path` field overrides the
133/// per-algorithm key path (for `repo-key`) or the external-signer
134/// binary path (for `external`); if unset we fall back to the
135/// `[attest]` config section just like the primary signer does.
136#[derive(Debug, PartialEq, Eq)]
137struct SignerSpec {
138    algorithm: Algorithm,
139    signer_kind: String,
140    path: Option<String>,
141    /// Parsed `args=a|b|c` clause. `None` ⇒ fall through to
142    /// `attest.external_signer_args`; `Some(vec)` ⇒ override for this
143    /// signer only. Pipe-separated on the wire because comma is the
144    /// spec's key=value separator.
145    args: Option<Vec<String>>,
146}
147
148fn parse_signer_spec(s: &str) -> Result<SignerSpec, String> {
149    let mut algorithm: Option<Algorithm> = None;
150    let mut signer_kind: Option<String> = None;
151    let mut path: Option<String> = None;
152    let mut args: Option<Vec<String>> = None;
153    for part in s.split(',') {
154        let part = part.trim();
155        if part.is_empty() {
156            continue;
157        }
158        let Some((k, v)) = part.split_once('=') else {
159            return Err(format!(
160                "--additional-signer spec part '{part}' is not key=value"
161            ));
162        };
163        match k.trim() {
164            "algorithm" => {
165                let v = v.trim();
166                let alg = attest_factory::parse_algorithm(v).map_err(|_| {
167                    format!("--additional-signer: unknown algorithm '{v}' — expected one of: ed25519, secp256k1, p256")
168                })?;
169                algorithm = Some(alg);
170            }
171            "signer" => {
172                let v = v.trim();
173                if !matches!(v, "repo-key" | "external") {
174                    return Err(format!(
175                        "--additional-signer: unknown signer '{v}' — expected one of: repo-key, external"
176                    ));
177                }
178                signer_kind = Some(v.to_owned());
179            }
180            "path" => {
181                path = Some(v.trim().to_owned());
182            }
183            "args" => {
184                // Pipe-separator is a deliberate divergence from the
185                // `,`-separator used between spec keys: `,` is already
186                // taken, and `|` is the shortest ASCII separator that
187                // doesn't need shell-quoting. An empty value means
188                // "zero argv" and is valid (overrides a non-empty
189                // config explicitly).
190                args = Some(crate::config::parse_pipe_list(v.trim()));
191            }
192            other => {
193                return Err(format!("--additional-signer: unknown spec key '{other}'"));
194            }
195        }
196    }
197    let algorithm =
198        algorithm.ok_or_else(|| "--additional-signer: missing algorithm=...".to_owned())?;
199    let signer_kind =
200        signer_kind.ok_or_else(|| "--additional-signer: missing signer=...".to_owned())?;
201    Ok(SignerSpec {
202        algorithm,
203        signer_kind,
204        path,
205        args,
206    })
207}
208
209#[must_use]
210#[allow(clippy::too_many_lines)]
211pub fn run(args: &[String]) -> u8 {
212    let parsed = match clap_shim::parse::<Args>("mkit attest", args) {
213        Ok(o) => o,
214        Err(code) => return code,
215    };
216
217    let cwd = match std::env::current_dir() {
218        Ok(p) => p,
219        Err(e) => return emit_err(&format!("cwd: {e}"), exit::NOINPUT),
220    };
221    let mkit_dir = cwd.join(mkit_core::MKIT_DIR);
222    if !mkit_dir.is_dir() {
223        return emit_err("not a mkit repo", exit::GENERAL_ERROR);
224    }
225
226    let mut cfg = match crate::config::read_or_default(&cwd) {
227        Ok(c) => c,
228        Err(e) => return emit_err(&format!("config: {e}"), exit::CONFIG_ERROR),
229    };
230
231    // `--external-signer-arg` REPLACES `attest.external_signer_args`
232    // when present. Per-invocation override, intentionally not additive
233    // so a user can cleanly reproduce `mkit-sign-se sign --tag demo`
234    // without having to remember (or clobber) whatever's in config.
235    if let Some(argv) = parsed.external_signer_args() {
236        cfg.attest.external_signer_args = argv;
237    }
238
239    // --- Resolve commit. --------------------------------------------
240    let commit_hash = match resolve_commit(&mkit_dir, parsed.commit.as_deref()) {
241        Ok(h) => h,
242        Err((msg, code)) => return emit_err(&msg, code),
243    };
244
245    // --- Resolve primary algorithm + signer. ------------------------
246    let alg_str = parsed
247        .algorithm
248        .clone()
249        .unwrap_or_else(|| cfg.attest.default_algorithm_or_fallback().to_owned());
250    let algorithm = match attest_factory::parse_algorithm(&alg_str) {
251        Ok(a) => a,
252        Err(FactoryError::UnknownAlgorithm(s)) => {
253            return emit_err(
254                &format!("unknown algorithm '{s}' — expected one of: ed25519, secp256k1, p256"),
255                exit::USAGE,
256            );
257        }
258        Err(e) => return emit_err(&format!("{e}"), exit::USAGE),
259    };
260    let signer_kind = parsed
261        .signer
262        .clone()
263        .unwrap_or_else(|| cfg.attest.signer_or_fallback().to_owned());
264
265    let primary_signer = match attest_factory::build_signer(&cwd, algorithm, &signer_kind, &cfg) {
266        Ok(s) => s,
267        Err(e) => return emit_err(&format!("{e}"), factory_error_code(&e)),
268    };
269
270    // --- Resolve additional signers. --------------------------------
271    // Parse ALL specs before building ANY signer so a malformed spec
272    // surfaces as a USAGE error without any crypto happening.
273    let mut additional_specs: Vec<SignerSpec> = Vec::with_capacity(parsed.additional_signers.len());
274    for spec_str in &parsed.additional_signers {
275        match parse_signer_spec(spec_str) {
276            Ok(s) => additional_specs.push(s),
277            Err(e) => return emit_err(&e, exit::USAGE),
278        }
279    }
280
281    let mut signers: Vec<Box<dyn Signer>> = Vec::with_capacity(1 + additional_specs.len());
282    signers.push(primary_signer);
283    for spec in &additional_specs {
284        let signer = match build_additional_signer(&cwd, spec, &cfg) {
285            Ok(s) => s,
286            Err(e) => return emit_err(&format!("{e}"), factory_error_code(&e)),
287        };
288        signers.push(signer);
289    }
290
291    // --- Build predicate bytes. ------------------------------------
292    let predicate_bytes: Vec<u8> = match parsed.predicate_file.as_deref() {
293        Some(p) => match read_predicate_file(p) {
294            Ok(b) => b,
295            Err((msg, code)) => return emit_err(&msg, code),
296        },
297        None => b"{}".to_vec(),
298    };
299    let predicate_type = parsed
300        .predicate_type
301        .unwrap_or_else(|| DEFAULT_PREDICATE_TYPE.to_owned());
302
303    // --- Build Statement. ------------------------------------------
304    let stmt_bytes = match statement::for_commit(&commit_hash, &predicate_type, &predicate_bytes) {
305        Ok(s) => s.into_bytes(),
306        Err(
307            mkit_attest::Error::PredicateMustBeJsonObject
308            | mkit_attest::Error::PredicateNotJsonObject
309            | mkit_attest::Error::PredicateNotUtf8,
310        ) => {
311            return emit_err(
312                "--predicate-file must contain a JCS-canonical JSON object",
313                exit::DATAERR,
314            );
315        }
316        Err(e) => return emit_err(&format!("statement: {e}"), exit::DATAERR),
317    };
318
319    // --- Sign with every signer, aborting on the first failure. -----
320    let pae = mkit_attest::pae_of(PAYLOAD_TYPE_IN_TOTO, &stmt_bytes);
321    let mut signatures: Vec<Sig> = Vec::with_capacity(signers.len());
322    for (idx, signer) in signers.iter_mut().enumerate() {
323        let sig_bytes = match signer.sign(&pae) {
324            Ok(b) => b,
325            Err(e) => {
326                return emit_err(
327                    &format!("sign (signer #{}): {e}", idx + 1),
328                    exit::GENERAL_ERROR,
329                );
330            }
331        };
332        let keyid = match signer.keyid() {
333            Ok(k) => k,
334            Err(e) => {
335                return emit_err(
336                    &format!("keyid (signer #{}): {e}", idx + 1),
337                    exit::GENERAL_ERROR,
338                );
339            }
340        };
341        signatures.push(Sig {
342            keyid,
343            sig: sig_bytes,
344        });
345    }
346
347    let envelope = Envelope {
348        payload_type: PAYLOAD_TYPE_IN_TOTO.to_owned(),
349        payload: stmt_bytes,
350        signatures,
351    };
352    let encoded = match envelope.encode() {
353        Ok(s) => s,
354        Err(e) => return emit_err(&format!("encode envelope: {e}"), exit::DATAERR),
355    };
356
357    // --- Save. ----------------------------------------------------
358    // Hold the repo lock across the envelope write so a concurrent
359    // `gc --grace-secs 0` can't compute its live set (which treats
360    // attestation subjects as roots) before this attestation lands and then
361    // prune the just-attested commit (#267). The repo was validated above
362    // (`mkit_dir.is_dir()`), so a non-repo reported cleanly. Held tightly,
363    // after signing (which may shell out to an external signer), around the
364    // write only.
365    let _lock = match super::acquire_worktree_lock(&cwd) {
366        Ok(l) => l,
367        Err(code) => return code,
368    };
369    // The commit was resolved before the lock; re-verify it still exists in
370    // the object store now that gc can't run, so we never write an
371    // attestation whose subject a concurrent `gc --grace-secs 0` pruned
372    // between resolution and this save (#267). (`obj_store` avoids shadowing
373    // the `mkit_attest::store` module used for `store::save`.)
374    let obj_store = match mkit_core::store::ObjectStore::open(&cwd) {
375        Ok(s) => s,
376        Err(e) => return emit_err(&format!("not a mkit repo: {e}"), exit::GENERAL_ERROR),
377    };
378    if !obj_store.contains(&commit_hash) {
379        return emit_err(
380            &format!(
381                "attested commit {} no longer exists (pruned concurrently?); aborting",
382                hash_mod::to_hex(&commit_hash)
383            ),
384            exit::CANTCREAT,
385        );
386    }
387    let (att_id, path) = match store::save(&mkit_dir, &commit_hash, encoded.as_bytes()) {
388        Ok(p) => p,
389        Err(e) => return emit_err(&format!("store: {e}"), exit::CANTCREAT),
390    };
391    let mut stderr = std::io::stderr().lock();
392    let _ = writeln!(
393        stderr,
394        "attested {} → {} ({} signature(s))",
395        hash_mod::to_hex(&att_id),
396        path.display(),
397        envelope.signatures.len()
398    );
399    exit::OK
400}
401
402/// Build an additional signer from a parsed spec.
403///
404/// `path` overrides the per-algorithm key path (repo-key) or the
405/// external-signer binary path (external); if unset we fall through to
406/// the same `[attest]` config the primary signer uses.
407fn build_additional_signer(
408    root: &Path,
409    spec: &SignerSpec,
410    base: &Config,
411) -> Result<Box<dyn Signer>, FactoryError> {
412    match spec.signer_kind.as_str() {
413        "repo-key" => {
414            // A per-spec path overrides the config-level key path; we
415            // synthesise a one-off Config to feed the factory so it
416            // still does the load-and-validate dance we want.
417            let mut cfg = base.clone();
418            if let Some(p) = spec.path.as_deref() {
419                // Path-traversal guard at the spec layer, mirroring the
420                // user-config validator. A per-spec `path=...` value
421                // can come from `--additional-signer` argv or, in the
422                // multi-sig flow, from a CI-controlled string. Either
423                // way `..` traversal is rejected.
424                if let Err(e) = crate::config::validate_key_path(p) {
425                    return Err(FactoryError::InvalidKeyFile {
426                        path: p.to_owned(),
427                        reason: e.to_string(),
428                    });
429                }
430                match spec.algorithm {
431                    Algorithm::Ed25519 => p.clone_into(&mut cfg.signing_key),
432                    Algorithm::Secp256k1 => p.clone_into(&mut cfg.attest.secp256k1_key_path),
433                    Algorithm::P256 => p.clone_into(&mut cfg.attest.p256_key_path),
434                    #[cfg(feature = "bls-threshold")]
435                    Algorithm::Bls12381Threshold => {
436                        return Err(FactoryError::UnknownAlgorithm(
437                            "bls12381-thr key path not configurable in Phase 1".to_owned(),
438                        ));
439                    }
440                }
441            }
442            attest_factory::build_signer(root, spec.algorithm, "repo-key", &cfg)
443        }
444        "external" => {
445            let mut cfg = base.clone();
446            if let Some(p) = spec.path.as_deref() {
447                p.clone_into(&mut cfg.attest.external_signer_path);
448            }
449            // Per-spec `args=...` REPLACES the config-level argv so
450            // multi-sig specs can independently drive different
451            // external binaries. Absent `args=` ⇒ inherit the primary
452            // signer's config value, matching how `path=` falls through.
453            if let Some(argv) = spec.args.as_ref() {
454                cfg.attest.external_signer_args.clone_from(argv);
455            }
456            attest_factory::build_signer(root, spec.algorithm, "external", &cfg)
457        }
458        other => Err(FactoryError::UnknownSignerKind(other.to_owned())),
459    }
460}
461
462pub(crate) fn factory_error_code(e: &FactoryError) -> u8 {
463    match e {
464        FactoryError::UnknownSignerKind(_) | FactoryError::UnknownAlgorithm(_) => exit::USAGE,
465        FactoryError::MissingKeyFile { .. } | FactoryError::MissingKeystoreKey { .. } => {
466            exit::NOINPUT
467        }
468        _ => exit::CONFIG_ERROR,
469    }
470}
471
472/// Read a `--predicate-file` with a size cap (#223). Stats the file
473/// first so an oversized predicate is rejected before any large read,
474/// then reads with a bounded `take` as defence-in-depth against a file
475/// that grows between the stat and the read.
476fn read_predicate_file(path: &str) -> Result<Vec<u8>, (String, u8)> {
477    use std::io::Read;
478    let meta = std::fs::metadata(path)
479        .map_err(|e| (format!("predicate file '{path}': {e}"), exit::NOINPUT))?;
480    if meta.len() > MAX_PREDICATE_BYTES {
481        return Err((
482            format!("predicate file '{path}' exceeds {MAX_PREDICATE_BYTES}-byte cap"),
483            exit::DATAERR,
484        ));
485    }
486    let file = std::fs::File::open(path)
487        .map_err(|e| (format!("predicate file '{path}': {e}"), exit::NOINPUT))?;
488    let mut data = Vec::new();
489    file.take(MAX_PREDICATE_BYTES + 1)
490        .read_to_end(&mut data)
491        .map_err(|e| (format!("predicate file '{path}': {e}"), exit::NOINPUT))?;
492    if data.len() as u64 > MAX_PREDICATE_BYTES {
493        return Err((
494            format!("predicate file '{path}' exceeds {MAX_PREDICATE_BYTES}-byte cap"),
495            exit::DATAERR,
496        ));
497    }
498    Ok(data)
499}
500
501/// Parse `--commit` value or fall back to HEAD.
502fn resolve_commit(mkit_dir: &Path, flag: Option<&str>) -> Result<Hash, (String, u8)> {
503    if let Some(hex) = flag {
504        return hash_mod::from_hex(hex)
505            .map_err(|e| (format!("bad --commit hash: {e}"), exit::DATAERR));
506    }
507    match refs::resolve_head(mkit_dir) {
508        Ok(Some(h)) => Ok(h),
509        Ok(None) => Err(("HEAD has no commit yet".to_owned(), exit::GENERAL_ERROR)),
510        Err(e) => Err((format!("read HEAD: {e}"), exit::GENERAL_ERROR)),
511    }
512}
513
514fn emit_err(msg: &str, code: u8) -> u8 {
515    let mut stderr = std::io::stderr().lock();
516    let _ = writeln!(stderr, "error: {msg}");
517    code
518}
519
520#[cfg(test)]
521mod tests {
522    use super::*;
523    use clap::Parser;
524
525    /// Test-only adapter: drive the clap-derive parser with just the
526    /// trailing args (no `mkit attest` argv[0]).
527    fn parse_args(args: &[String]) -> Result<Args, clap::Error> {
528        let mut full: Vec<String> = vec!["mkit attest".into()];
529        full.extend_from_slice(args);
530        Args::try_parse_from(full)
531    }
532
533    #[test]
534    fn parse_args_accepts_all_flags() {
535        let args = vec![
536            "--commit".into(),
537            "abc".into(),
538            "--algorithm".into(),
539            "p256".into(),
540            "--signer".into(),
541            "external".into(),
542            "--predicate-type".into(),
543            "https://example.com/p".into(),
544            "--predicate-file".into(),
545            "/tmp/x.json".into(),
546        ];
547        let p = parse_args(&args).unwrap();
548        assert_eq!(p.commit.as_deref(), Some("abc"));
549        assert_eq!(p.algorithm.as_deref(), Some("p256"));
550        assert_eq!(p.signer.as_deref(), Some("external"));
551        assert_eq!(p.predicate_type.as_deref(), Some("https://example.com/p"));
552        assert_eq!(p.predicate_file.as_deref(), Some("/tmp/x.json"));
553        assert!(p.additional_signers.is_empty());
554    }
555
556    #[test]
557    fn parse_args_collects_repeatable_external_signer_args() {
558        let args = vec![
559            "--external-signer-arg".into(),
560            "sign".into(),
561            "--external-signer-arg".into(),
562            "--tag".into(),
563            "--external-signer-arg".into(),
564            "demo".into(),
565        ];
566        let p = parse_args(&args).unwrap();
567        let expected = vec!["sign".to_owned(), "--tag".to_owned(), "demo".to_owned()];
568        assert_eq!(p.external_signer_args(), Some(expected));
569    }
570
571    #[test]
572    fn parse_args_external_signer_arg_none_when_absent() {
573        // Distinguishes "flag not passed" (None → fall through to config)
574        // from "flag passed with no values" (impossible: the flag needs
575        // a value).
576        let p = parse_args(&[]).unwrap();
577        assert!(p.external_signer_args().is_none());
578    }
579
580    #[test]
581    fn parse_args_collects_multiple_additional_signers() {
582        let args = vec![
583            "--additional-signer".into(),
584            "algorithm=ed25519,signer=repo-key".into(),
585            "--additional-signer".into(),
586            "algorithm=p256,signer=external,path=/x".into(),
587        ];
588        let p = parse_args(&args).unwrap();
589        assert_eq!(p.additional_signers.len(), 2);
590        assert_eq!(p.additional_signers[0], "algorithm=ed25519,signer=repo-key");
591    }
592
593    #[test]
594    fn parse_args_rejects_unknown() {
595        let args = vec!["--bogus".into(), "x".into()];
596        assert!(parse_args(&args).is_err());
597    }
598
599    #[test]
600    fn parse_signer_spec_ok() {
601        let s = parse_signer_spec("algorithm=secp256k1,signer=repo-key,path=k.key").unwrap();
602        assert_eq!(s.algorithm, Algorithm::Secp256k1);
603        assert_eq!(s.signer_kind, "repo-key");
604        assert_eq!(s.path.as_deref(), Some("k.key"));
605    }
606
607    #[test]
608    fn parse_signer_spec_with_args() {
609        let s = parse_signer_spec(
610            "algorithm=p256,signer=external,path=/usr/bin/signer,args=sign|--tag|demo",
611        )
612        .unwrap();
613        assert_eq!(s.algorithm, Algorithm::P256);
614        assert_eq!(s.signer_kind, "external");
615        assert_eq!(s.path.as_deref(), Some("/usr/bin/signer"));
616        assert_eq!(
617            s.args.as_deref(),
618            Some(["sign".to_owned(), "--tag".to_owned(), "demo".to_owned()].as_slice())
619        );
620    }
621
622    #[test]
623    fn parse_signer_spec_args_empty_means_zero_argv() {
624        // `args=` with no value is a valid override that means "this
625        // signer gets zero argv, regardless of config." Distinct from
626        // omitting `args=` entirely (which inherits from config).
627        let s = parse_signer_spec("algorithm=ed25519,signer=external,args=").unwrap();
628        assert_eq!(s.args.as_deref(), Some([].as_slice()));
629    }
630
631    #[test]
632    fn parse_signer_spec_without_path() {
633        let s = parse_signer_spec("algorithm=p256,signer=external").unwrap();
634        assert_eq!(s.algorithm, Algorithm::P256);
635        assert_eq!(s.signer_kind, "external");
636        assert!(s.path.is_none());
637    }
638
639    #[test]
640    fn parse_signer_spec_missing_algorithm() {
641        let e = parse_signer_spec("signer=repo-key").unwrap_err();
642        assert!(e.contains("algorithm"), "{e}");
643    }
644
645    #[test]
646    fn parse_signer_spec_missing_signer() {
647        let e = parse_signer_spec("algorithm=ed25519").unwrap_err();
648        assert!(e.contains("signer"), "{e}");
649    }
650
651    #[test]
652    fn parse_signer_spec_unknown_algorithm() {
653        let e = parse_signer_spec("algorithm=rsa,signer=repo-key").unwrap_err();
654        assert!(e.contains("rsa"), "{e}");
655    }
656
657    #[test]
658    fn parse_signer_spec_unknown_signer_kind() {
659        let e = parse_signer_spec("algorithm=ed25519,signer=sigstore").unwrap_err();
660        assert!(e.contains("sigstore"), "{e}");
661    }
662
663    #[test]
664    fn parse_signer_spec_not_key_value() {
665        // Missing comma between key=value pairs — `split_once('=')` swallows
666        // the rest into the value of the first key. This surfaces as an
667        // "unknown algorithm" error for the bogus algorithm value, which is
668        // clear enough for the user to fix.
669        let e = parse_signer_spec("algorithm=ed25519 signer=repo-key").unwrap_err();
670        assert!(e.contains("algorithm") || e.contains("key=value"), "{e}");
671    }
672
673    #[test]
674    fn parse_args_all_defaults_when_empty() {
675        let p = parse_args(&[]).unwrap();
676        assert!(p.commit.is_none());
677        assert!(p.algorithm.is_none());
678        assert!(p.signer.is_none());
679        assert!(p.predicate_type.is_none());
680        assert!(p.predicate_file.is_none());
681        assert!(p.additional_signers.is_empty());
682    }
683}