OXYGENHUB
# 35f7a849bcc5 oxygenflow / crates / flow-core / src / pipeline.rs
53.8 KiB · Rust · last changed Allow-listed egress over vsock: network "egress" { allow "host"... } 20 days ago source blame history raw
1
//! `.flow/pipeline.kdl`: the M0 subset of spec §3 (docs/m0.md §1), parsed
2
//! from KDL 2.0 and validated before anything runs. Every problem is
3
//! reported with its line, and all of them at once.
4
5
use serde::{Deserialize, Serialize};
6
use std::collections::{BTreeMap, BTreeSet};
7
use std::fmt;
8
9
/// Where a pipeline lives in the state being built.
10
pub const PIPELINE_PATH: &str = ".flow/pipeline.kdl";
11
12
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
13
pub struct Pipeline {
14
    pub schema_version: u32,
15
    /// By name.
16
    pub sandboxes: BTreeMap<String, Sandbox>,
17
    /// By name.
18
    pub jobs: BTreeMap<String, Job>,
19
    /// When the pipeline's jobs run, unless a job says (oxygenflow#24);
20
    /// absent: on every push and from "Run pipeline".
21
    #[serde(default, skip_serializing_if = "Option::is_none")]
22
    pub on: Option<On>,
23
}
24
25
/// What starts a job (`on { push "main"; tag "v*"; manual; schedule "0 3 * * *" }`).
26
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
27
pub struct On {
28
    /// Bookmark globs a push runs it for; `Some(vec![])`: any bookmark.
29
    #[serde(default, skip_serializing_if = "Option::is_none")]
30
    pub push: Option<Vec<String>>,
31
    /// Tag globs a pushed tag runs it for; `Some(vec![])`: any tag.
32
    #[serde(default, skip_serializing_if = "Option::is_none")]
33
    pub tag: Option<Vec<String>>,
34
    /// From "Run pipeline" (and `oh run start`).
35
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
36
    pub manual: bool,
37
    /// Five-field cron lines, on the hub's clock, for the default bookmark.
38
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
39
    pub schedule: Vec<String>,
40
}
41
42
/// Why a run started, for [`On::matches`].
43
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
44
pub enum Event<'a> {
45
    /// A push moved this bookmark.
46
    Push(&'a str),
47
    /// This tag was pushed.
48
    Tag(&'a str),
49
    /// "Run pipeline", `oh run start`.
50
    Manual,
51
    /// A `schedule` line came due.
52
    Schedule,
53
}
54
55
impl On {
56
    /// What a job without any `on` runs for: every push, and by hand.
57
    pub fn default_on() -> On {
58
        On { push: Some(vec![]), tag: None, manual: true, schedule: vec![] }
59
    }
60
61
    pub fn matches(&self, event: Event<'_>) -> bool {
62
        let any = |globs: &Option<Vec<String>>, name: &str| match globs {
63
            None => false,
64
            Some(g) if g.is_empty() => true,
65
            Some(g) => g.iter().any(|p| globset::Glob::new(p).is_ok_and(|p| p.compile_matcher().is_match(name))),
66
        };
67
        match event {
68
            Event::Push(b) => any(&self.push, b),
69
            Event::Tag(t) => any(&self.tag, t),
70
            Event::Manual => self.manual,
71
            Event::Schedule => !self.schedule.is_empty(),
72
        }
73
    }
74
}
75
76
impl Pipeline {
77
    /// When `job` runs: its own `on`, else the pipeline's, else the default.
78
    pub fn on_of(&self, job: &Job) -> On {
79
        job.on.clone().or_else(|| self.on.clone()).unwrap_or_else(On::default_on)
80
    }
81
}
82
83
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
84
#[serde(rename_all = "lowercase")]
85
pub enum Runtime {
86
    /// Tier 0 (spec §4.1); refused in M0.
87
    Wasm,
88
    /// Tier 1 (spec §4.2): namespaces, Landlock, seccomp, cgroups.
89
    Syscall,
90
    /// Tier 2 (spec §4.3), the default.
91
    Microvm,
92
}
93
94
impl Runtime {
95
    pub fn as_str(self) -> &'static str {
96
        match self {
97
            Runtime::Wasm => "wasm",
98
            Runtime::Syscall => "syscall",
99
            Runtime::Microvm => "microvm",
100
        }
101
    }
102
}
103
104
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
105
#[serde(rename_all = "lowercase")]
106
pub enum Network {
107
    Isolated,
108
    Loopback,
109
    Egress,
110
}
111
112
impl Network {
113
    pub fn as_str(self) -> &'static str {
114
        match self {
115
            Network::Isolated => "isolated",
116
            Network::Loopback => "loopback",
117
            Network::Egress => "egress",
118
        }
119
    }
120
}
121
122
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
123
pub struct Sandbox {
124
    pub name: String,
125
    pub runtime: Runtime,
126
    /// BLAKE3 of the rootfs image, hex.
127
    pub rootfs: String,
128
    pub memory_mib: u32,
129
    pub cpus: u32,
130
    pub network: Network,
131
    /// Environment every job in this sandbox starts from; a job's own `env`
132
    /// overrides it.
133
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
134
    pub env: BTreeMap<String, String>,
135
    /// Labels a runner must have to run jobs here (`runs-on`), sorted.
136
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
137
    pub runs_on: Vec<String>,
138
    /// The hosts a `network "egress"` sandbox may reach (oxygenflow#55):
139
    /// names, or `*.` and a domain for any name under it; sorted. Its jobs
140
    /// reach them through the runner's proxy, and nothing else.
141
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
142
    pub egress_allow: Vec<String>,
143
    /// How long each job here may run, in seconds (`timeout "2h"`), when
144
    /// the job does not say; the hub caps it at the site's maximum.
145
    #[serde(default, skip_serializing_if = "Option::is_none")]
146
    pub timeout_s: Option<u64>,
147
    /// The architecture the jobs need (`arch "riscv64"`, spec §11.3);
148
    /// without it, the rootfs image's.
149
    #[serde(default, skip_serializing_if = "Option::is_none")]
150
    pub arch: Option<String>,
151
}
152
153
/// An architecture name as runners report it: `x86_64`, `aarch64`, `riscv64`.
154
pub fn arch_ok(a: &str) -> bool {
155
    (2..=16).contains(&a.len()) && a.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'_')
156
}
157
158
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
159
pub struct Job {
160
    pub name: String,
161
    pub sandbox: String,
162
    /// Glob patterns over repository paths (spec §6.2).
163
    pub scope: Vec<String>,
164
    /// Sorted.
165
    pub depends_on: Vec<String>,
166
    pub env: BTreeMap<String, String>,
167
    /// Executed as is: no shell, no expansion (spec §2.2 I2).
168
    pub exec: Vec<String>,
169
    /// Ephemeral scratch mounts, sorted by target (spec §3.1 `mount`).
170
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
171
    pub mounts: Vec<Mount>,
172
    /// Files the job produces, sorted by name (spec §5.2 `output`).
173
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
174
    pub outputs: Vec<Output>,
175
    /// Secrets the job mounts, sorted by name (spec §9 `secret`): the hub
176
    /// supplies each by name, the runner mounts it as a file.
177
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
178
    pub secrets: Vec<Secret>,
179
    /// Identities the job asks the hub for (oxygenhub#103): an OIDC token
180
    /// for an audience, mounted as a file like a secret.
181
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
182
    pub identities: Vec<Identity>,
183
    /// Labels a runner must have to take this job (`runs-on`), sorted;
184
    /// with its sandbox's, what the hub matches against a runner's.
185
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
186
    pub runs_on: Vec<String>,
187
    /// How long the job may run, in seconds (`timeout "90m"`); its
188
    /// sandbox's when absent, else the site's default (oxygenflow#23).
189
    #[serde(default, skip_serializing_if = "Option::is_none")]
190
    pub timeout_s: Option<u64>,
191
    /// When it runs, over the pipeline's `on` (oxygenflow#24).
192
    #[serde(default, skip_serializing_if = "Option::is_none")]
193
    pub on: Option<On>,
194
}
195
196
impl Job {
197
    /// The job's environment on top of its sandbox's: what it runs with.
198
    pub fn in_sandbox(&self, sandbox: &Sandbox) -> Job {
199
        let mut job = self.clone();
200
        for (k, v) in &sandbox.env {
201
            job.env.entry(k.clone()).or_insert_with(|| v.clone());
202
        }
203
        job.runs_on.extend(sandbox.runs_on.iter().cloned());
204
        job.runs_on.sort();
205
        job.runs_on.dedup();
206
        job.timeout_s = job.timeout_s.or(sandbox.timeout_s);
207
        job
208
    }
209
210
    /// The job with variables that tell it what it builds (`FLOW_STATE`,
211
    /// `FLOW_BOOKMARK` …) added to its environment; a variable the pipeline
212
    /// sets itself keeps the pipeline's value.
213
    pub fn with_flow_env(&self, vars: &[(String, String)]) -> Job {
214
        let mut job = self.clone();
215
        for (k, v) in vars {
216
            job.env.entry(k.clone()).or_insert_with(|| v.clone());
217
        }
218
        job
219
    }
220
}
221
222
/// An ephemeral scratch mount: empty at the start of every job and gone
223
/// after it, so it cannot carry anything between jobs (spec §2.2 I4) --
224
/// unless it names a `cache` (spec §5.4, #28): then a disk kept from an
225
/// earlier passing job may start it, and the receipt says which.
226
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
227
pub struct Mount {
228
    pub source: MountSource,
229
    /// An absolute path under `/src` or `/tmp`.
230
    pub target: String,
231
    pub size_mib: u32,
232
    /// `cache "name"`: keep this disk after a passing job and start later
233
    /// jobs from it (disk mounts only).
234
    #[serde(default, skip_serializing_if = "Option::is_none")]
235
    pub cache: Option<String>,
236
    /// `key "Cargo.lock" ...`: files whose content picks the cache entry.
237
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
238
    pub key: Vec<String>,
239
}
240
241
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
242
#[serde(rename_all = "lowercase")]
243
pub enum MountSource {
244
    /// `ephemeral://tmpfs`: memory in the sandbox, counted against its memory-limit.
245
    Tmpfs,
246
    /// `ephemeral://disk`: a fresh sparse disk on the runner, for large build trees.
247
    Disk,
248
}
249
250
impl MountSource {
251
    pub fn as_str(self) -> &'static str {
252
        match self {
253
            MountSource::Tmpfs => "ephemeral://tmpfs",
254
            MountSource::Disk => "ephemeral://disk",
255
        }
256
    }
257
}
258
259
/// A file a job produces (spec §5.2): read from the job's disks when it
260
/// ends, kept in the repository as a blob, and named by its receipt.
261
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
262
pub struct Output {
263
    /// The artifact's name, unique in the job.
264
    pub name: String,
265
    /// A clean absolute path under `/src`, not on a tmpfs mount.
266
    pub path: String,
267
    /// `when "always"`: kept from a failing job too, such as a test report
268
    /// (oxygenflow#37). From a failing job it is never in the receipt.
269
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
270
    pub always: bool,
271
    /// An artifact is one file; a report is a directory with a
272
    /// `report.toml`, kept file by file with a manifest (oxygenhub#127).
273
    #[serde(default, skip_serializing_if = "OutputKind::is_artifact")]
274
    pub kind: OutputKind,
275
}
276
277
/// What an [`Output`] is.
278
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
279
#[serde(rename_all = "lowercase")]
280
pub enum OutputKind {
281
    #[default]
282
    Artifact,
283
    Report,
284
}
285
286
impl OutputKind {
287
    pub fn is_artifact(&self) -> bool {
288
        *self == OutputKind::Artifact
289
    }
290
291
    pub fn word(&self) -> &'static str {
292
        match self {
293
            OutputKind::Artifact => "artifact",
294
            OutputKind::Report => "report",
295
        }
296
    }
297
}
298
299
/// A secret the job reads as a file (spec §9): never in the environment.
300
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
301
pub struct Secret {
302
    /// Which secret, as the hub names it; unique in the job.
303
    pub name: String,
304
    /// A clean absolute path under `/run/secrets/`, unique in the job;
305
    /// the file is 0400, the job's user's, on an in-memory mount.
306
    pub mount_path: String,
307
}
308
309
/// An OIDC token the hub mints for the job (oxygenhub#103), naming the
310
/// repository, bookmark, run and job, for `audience`; read as a file.
311
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
312
pub struct Identity {
313
    /// Who the token is for (`sts.amazonaws.com`, a Vault role's audience, …).
314
    pub audience: String,
315
    /// A clean absolute path under `/run/secrets/`, unique among the
316
    /// job's secrets and identities.
317
    pub mount_path: String,
318
}
319
320
/// A secret's mount path: under `/run/secrets/`, no `.` or `..`.
321
pub fn secret_path_ok(p: &str) -> bool {
322
    p.strip_prefix("/run/secrets/").is_some_and(|rest| !rest.is_empty() && rest.split('/').all(|c| !c.is_empty() && c != "." && c != ".."))
323
}
324
325
/// Artifacts a job may declare.
326
pub const MAX_OUTPUTS: usize = 16;
327
328
/// Whether `p` is a clean absolute path strictly under `/src`, with nothing
329
/// a debugfs command line would have to quote.
330
fn output_path_ok(p: &str) -> bool {
331
    p.strip_prefix("/src/").is_some_and(|rest| {
332
        !rest.is_empty()
333
            && rest.split('/').all(|c| !c.is_empty() && c != "." && c != "..")
334
            && p.bytes().all(|b| b.is_ascii_graphic() && b != b'"' && b != b'\\')
335
    })
336
}
337
338
/// Mounts a job may declare.
339
pub const MAX_MOUNTS: usize = 4;
340
/// The most secrets a job may mount.
341
pub const MAX_SECRETS: usize = 8;
342
/// The most identities a job may ask for.
343
pub const MAX_IDENTITIES: usize = 4;
344
/// Sizes a mount may have: 16 MiB to 64 GiB.
345
pub const MIN_MOUNT_MIB: u64 = 16;
346
pub const MAX_MOUNT_MIB: u64 = 64 * 1024;
347
348
/// Whether `t` is a clean absolute path strictly under `/src` or `/tmp`.
349
fn mount_target_ok(t: &str) -> bool {
350
    let under = t.strip_prefix("/src/").or_else(|| t.strip_prefix("/tmp/"));
351
    under.is_some_and(|rest| !rest.is_empty() && rest.split('/').all(|c| !c.is_empty() && c != "." && c != ".."))
352
}
353
354
/// One problem in a pipeline, at a 1-based line.
355
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
356
pub struct Problem {
357
    pub line: usize,
358
    pub message: String,
359
}
360
361
impl fmt::Display for Problem {
362
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
363
        write!(f, "line {}: {}", self.line, self.message)
364
    }
365
}
366
367
/// Every problem found, in line order.
368
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, thiserror::Error)]
369
pub struct Problems(pub Vec<Problem>);
370
371
impl fmt::Display for Problems {
372
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
373
        let lines: Vec<String> = self.0.iter().map(|p| p.to_string()).collect();
374
        f.write_str(&lines.join("\n"))
375
    }
376
}
377
378
const MIN_MEMORY_MIB: u64 = 64;
379
const MAX_MEMORY_MIB: u64 = 16 * 1024;
380
const MAX_CPUS: i128 = 16;
381
382
/// Nodes of the full spec that M0 recognises and refuses.
383
const LATER_TOP: &[&str] = &["publish", "secret"];
384
const LATER_JOB: &[&str] = &["attest"];
385
386
struct Ctx<'a> {
387
    src: &'a str,
388
    problems: Vec<Problem>,
389
}
390
391
impl Ctx<'_> {
392
    fn line(&self, span: SourceSpan) -> usize {
393
        let at = span.offset().min(self.src.len());
394
        self.src[..at].bytes().filter(|b| *b == b'\n').count() + 1
395
    }
396
397
    fn err(&mut self, span: SourceSpan, message: impl Into<String>) {
398
        let line = self.line(span);
399
        self.problems.push(Problem { line, message: message.into() });
400
    }
401
402
    /// The node's arguments as strings; properties and other types are errors.
403
    fn strings(&mut self, node: &KdlNode) -> Vec<String> {
404
        let mut out = Vec::new();
405
        for e in node.entries() {
406
            if let Some(k) = e.name() {
407
                self.err(e.span(), format!("`{}` takes no property `{}`", node.name().value(), k.value()));
408
                continue;
409
            }
410
            match e.value() {
411
                KdlValue::String(s) => out.push(s.clone()),
412
                other => self.err(e.span(), format!("`{}` takes strings, not {}", node.name().value(), kind_of(other))),
413
            }
414
        }
415
        out
416
    }
417
418
    /// Exactly one string argument.
419
    fn one_string(&mut self, node: &KdlNode) -> Option<String> {
420
        let v = self.strings(node);
421
        if v.len() != 1 {
422
            self.err(node.span(), format!("`{}` takes one string", node.name().value()));
423
            return None;
424
        }
425
        v.into_iter().next()
426
    }
427
428
    fn one_integer(&mut self, node: &KdlNode) -> Option<i128> {
429
        match node.entries() {
430
            [e] if e.name().is_none() => match e.value() {
431
                KdlValue::Integer(n) => Some(*n),
432
                other => {
433
                    self.err(e.span(), format!("`{}` takes an integer, not {}", node.name().value(), kind_of(other)));
434
                    None
435
                }
436
            },
437
            _ => {
438
                self.err(node.span(), format!("`{}` takes one integer", node.name().value()));
439
                None
440
            }
441
        }
442
    }
443
444
    fn no_children(&mut self, node: &KdlNode) {
445
        if node.children().is_some() {
446
            self.err(node.span(), format!("`{}` takes no block", node.name().value()));
447
        }
448
    }
449
450
    fn name_ok(&mut self, span: SourceSpan, what: &str, name: &str) -> bool {
451
        let ok = !name.is_empty() && name.len() <= 64 && name.bytes().all(|b| b.is_ascii_alphanumeric() || b"._-".contains(&b));
452
        if !ok {
453
            self.err(span, format!("{what} name {name:?} must be 1 to 64 of A-Z a-z 0-9 . _ -"));
454
        }
455
        ok
456
    }
457
}
458
459
use kdl::{KdlDocument, KdlNode, KdlValue};
460
use miette::SourceSpan;
461
462
fn kind_of(v: &KdlValue) -> &'static str {
463
    match v {
464
        KdlValue::String(_) => "a string",
465
        KdlValue::Integer(_) => "an integer",
466
        KdlValue::Float(_) => "a number",
467
        KdlValue::Bool(_) => "a boolean",
468
        KdlValue::Null => "null",
469
    }
470
}
471
472
/// `"512MiB"`, `"4GiB"`, `"65536KiB"` to MiB.
473
fn parse_memory(s: &str) -> Option<u64> {
474
    let (num, unit) = s.split_at(s.find(|c: char| !c.is_ascii_digit())?);
475
    let n: u64 = num.parse().ok()?;
476
    let mib = match unit {
477
        "KiB" => n.is_multiple_of(1024).then_some(n / 1024)?,
478
        "MiB" => n,
479
        "GiB" => n.checked_mul(1024)?,
480
        _ => return None,
481
    };
482
    Some(mib)
483
}
484
485
/// The line of the first escape KDL 2.0 does not have but shells and C
486
/// do (`\0NN`, `\e`, `\xNN`), as someone colouring output writes them.
487
fn foreign_escape_line(src: &str) -> Option<usize> {
488
    src.lines()
489
        .position(|l| {
490
            let b = l.as_bytes();
491
            b.windows(2).enumerate().any(|(i, w)| {
492
                w[0] == b'\\'
493
                    && (i == 0 || b[i - 1] != b'\\')
494
                    && (w[1] == b'e' || w[1] == b'x' || (w[1] == b'0' && b.get(i + 2).is_some_and(u8::is_ascii_digit)))
495
            })
496
        })
497
        .map(|i| i + 1)
498
}
499
500
impl Pipeline {
501
    /// Parse and validate a pipeline document.
502
    pub fn parse(src: &str) -> Result<Pipeline, Problems> {
503
        let doc = match KdlDocument::parse_v2(src) {
504
            Ok(d) => d,
505
            Err(e) => {
506
                let mut ctx = Ctx { src, problems: Vec::new() };
507
                for d in &e.diagnostics {
508
                    let msg = d.message.clone().or_else(|| d.label.clone()).unwrap_or_else(|| "not valid KDL 2.0".into());
509
                    ctx.err(d.span, msg);
510
                }
511
                if ctx.problems.is_empty() {
512
                    ctx.problems.push(Problem { line: 1, message: "not valid KDL 2.0".into() });
513
                }
514
                if let Some(line) = foreign_escape_line(src) {
515
                    ctx.problems.push(Problem {
516
                        line,
517
                        message: "KDL strings have no \\033, \\e or \\x escapes: write ESC (for ANSI colours) as \\u{1b}".into(),
518
                    });
519
                }
520
                return Err(Problems(ctx.problems));
521
            }
522
        };
523
        let mut ctx = Ctx { src, problems: Vec::new() };
524
        let mut schema = None;
525
        let mut sandboxes = BTreeMap::new();
526
        let mut jobs: BTreeMap<String, (Job, SourceSpan)> = BTreeMap::new();
527
        let mut on = None;
528
        for node in doc.nodes() {
529
            match node.name().value() {
530
                "on" => {
531
                    if on.is_some() {
532
                        ctx.err(node.span(), "`on` is given twice");
533
                    }
534
                    on = Some(parse_on(&mut ctx, node));
535
                }
536
                "schema-version" => {
537
                    ctx.no_children(node);
538
                    match ctx.one_integer(node) {
539
                        Some(1) => schema = Some(1),
540
                        Some(n) => ctx.err(node.span(), format!("schema-version {n} is not supported; this runner reads 1")),
541
                        None => {}
542
                    }
543
                }
544
                "sandbox" => {
545
                    if let Some(sb) = parse_sandbox(&mut ctx, node) {
546
                        if sandboxes.contains_key(&sb.name) {
547
                            ctx.err(node.span(), format!("sandbox {:?} is declared twice", sb.name));
548
                        } else {
549
                            sandboxes.insert(sb.name.clone(), sb);
550
                        }
551
                    }
552
                }
553
                "job" => {
554
                    if let Some(job) = parse_job(&mut ctx, node) {
555
                        if jobs.contains_key(&job.name) {
556
                            ctx.err(node.span(), format!("job {:?} is declared twice", job.name));
557
                        } else {
558
                            jobs.insert(job.name.clone(), (job, node.span()));
559
                        }
560
                    }
561
                }
562
                n if LATER_TOP.contains(&n) => ctx.err(node.span(), format!("`{n}` is not in M0 yet")),
563
                n => ctx.err(node.span(), format!("unknown node `{n}`; expected schema-version, on, sandbox or job")),
564
            }
565
        }
566
        if schema.is_none() && !ctx.problems.iter().any(|p| p.message.contains("schema-version")) {
567
            ctx.problems.push(Problem { line: 1, message: "`schema-version 1` is required".into() });
568
        }
569
        if jobs.is_empty() && ctx.problems.is_empty() {
570
            ctx.problems.push(Problem { line: 1, message: "a pipeline needs at least one job".into() });
571
        }
572
        // References and cycles.
573
        for (job, span) in jobs.values() {
574
            if !job.sandbox.is_empty() && !sandboxes.contains_key(&job.sandbox) {
575
                ctx.err(*span, format!("job {:?} uses sandbox {:?}, which is not declared", job.name, job.sandbox));
576
            }
577
            for d in &job.depends_on {
578
                if !jobs.contains_key(d) {
579
                    ctx.err(*span, format!("job {:?} depends on {:?}, which is not declared", job.name, d));
580
                } else if d == &job.name {
581
                    ctx.err(*span, format!("job {:?} depends on itself", job.name));
582
                }
583
            }
584
        }
585
        let graph: BTreeMap<&str, Vec<&str>> =
586
            jobs.values().map(|(j, _)| (j.name.as_str(), j.depends_on.iter().map(String::as_str).collect())).collect();
587
        if let Some(cycle) = find_cycle(&graph) {
588
            let span = jobs[cycle[0]].1;
589
            ctx.err(span, format!("jobs depend on each other in a cycle: {}", cycle.join(" → ")));
590
        }
591
        if !ctx.problems.is_empty() {
592
            ctx.problems.sort_by_key(|p| p.line);
593
            return Err(Problems(ctx.problems));
594
        }
595
        Ok(Pipeline { schema_version: 1, sandboxes, jobs: jobs.into_iter().map(|(k, (j, _))| (k, j)).collect(), on })
596
    }
597
598
    /// Jobs in an order where each comes after what it depends on (ties by name).
599
    pub fn order(&self) -> Vec<&Job> {
600
        let mut done: BTreeSet<&str> = BTreeSet::new();
601
        let mut out = Vec::with_capacity(self.jobs.len());
602
        while out.len() < self.jobs.len() {
603
            let before = out.len();
604
            for j in self.jobs.values() {
605
                if !done.contains(j.name.as_str()) && j.depends_on.iter().all(|d| done.contains(d.as_str())) {
606
                    out.push(j);
607
                }
608
            }
609
            for j in &out[before..] {
610
                done.insert(j.name.as_str());
611
            }
612
            if out.len() == before {
613
                break; // validated pipelines have no cycles
614
            }
615
        }
616
        out
617
    }
618
619
    /// Each job's depth: 0 without dependencies, else one more than its deepest dependency.
620
    pub fn depths(&self) -> BTreeMap<&str, usize> {
621
        let mut depth: BTreeMap<&str, usize> = BTreeMap::new();
622
        for j in self.order() {
623
            let d = j.depends_on.iter().map(|x| depth.get(x.as_str()).copied().unwrap_or(0) + 1).max().unwrap_or(0);
624
            depth.insert(j.name.as_str(), d);
625
        }
626
        depth
627
    }
628
}
629
630
fn find_cycle<'a>(graph: &BTreeMap<&'a str, Vec<&'a str>>) -> Option<Vec<&'a str>> {
631
    #[derive(Clone, Copy, PartialEq)]
632
    enum Mark {
633
        Visiting,
634
        Done,
635
    }
636
    fn visit<'a>(
637
        n: &'a str,
638
        graph: &BTreeMap<&'a str, Vec<&'a str>>,
639
        marks: &mut BTreeMap<&'a str, Mark>,
640
        stack: &mut Vec<&'a str>,
641
    ) -> Option<Vec<&'a str>> {
642
        match marks.get(n) {
643
            Some(Mark::Done) => return None,
644
            Some(Mark::Visiting) => {
645
                let at = stack.iter().position(|s| *s == n).unwrap_or(0);
646
                let mut c = stack[at..].to_vec();
647
                c.push(n);
648
                return Some(c);
649
            }
650
            None => {}
651
        }
652
        marks.insert(n, Mark::Visiting);
653
        stack.push(n);
654
        for d in graph.get(n).into_iter().flatten() {
655
            if graph.contains_key(d) && *d != n {
656
                if let Some(c) = visit(d, graph, marks, stack) {
657
                    return Some(c);
658
                }
659
            }
660
        }
661
        stack.pop();
662
        marks.insert(n, Mark::Done);
663
        None
664
    }
665
    let mut marks = BTreeMap::new();
666
    for n in graph.keys() {
667
        let mut stack = Vec::new();
668
        if let Some(c) = visit(n, graph, &mut marks, &mut stack) {
669
            return Some(c);
670
        }
671
    }
672
    None
673
}
674
675
fn block<'a>(ctx: &mut Ctx, node: &'a KdlNode) -> &'a [KdlNode] {
676
    match node.children() {
677
        Some(d) => d.nodes(),
678
        None => {
679
            ctx.err(node.span(), format!("`{}` needs a block {{ … }}", node.name().value()));
680
            &[]
681
        }
682
    }
683
}
684
685
fn parse_sandbox(ctx: &mut Ctx, node: &KdlNode) -> Option<Sandbox> {
686
    let name = ctx.one_string(node)?;
687
    let named = ctx.name_ok(node.span(), "sandbox", &name);
688
    let (mut runtime, mut rootfs, mut memory, mut cpus, mut network) =
689
        (Some(Runtime::Microvm), None, Some(1024u32), Some(1u32), Some(Network::Isolated));
690
    let mut seen = BTreeSet::new();
691
    let mut env = BTreeMap::new();
692
    let mut runs_on = Vec::new();
693
    let mut timeout_s = None;
694
    let mut arch: Option<String> = None;
695
    let mut egress_allow = Vec::new();
696
    for c in block(ctx, node) {
697
        let key = c.name().value();
698
        if key != "runs-on" && !seen.insert(key.to_string()) {
699
            ctx.err(c.span(), format!("`{key}` is given twice"));
700
        }
701
        if key == "env" {
702
            parse_env(ctx, c, &mut env);
703
            continue;
704
        }
705
        if key == "runs-on" {
706
            ctx.no_children(c);
707
            parse_labels(ctx, c, &mut runs_on);
708
            continue;
709
        }
710
        if key != "network" {
711
            ctx.no_children(c);
712
        }
713
        match key {
714
            "runtime" => {
715
                runtime = match ctx.one_string(c).as_deref() {
716
                    Some("microvm") => Some(Runtime::Microvm),
717
                    Some("syscall") | Some("gvisor") => Some(Runtime::Syscall),
718
                    Some("wasm") => {
719
                        ctx.err(c.span(), "runtime \"wasm\" is not in M0 yet; use \"microvm\" or \"syscall\"");
720
                        None
721
                    }
722
                    Some(other) => {
723
                        ctx.err(c.span(), format!("runtime {other:?}: expected \"microvm\", \"syscall\" or \"wasm\""));
724
                        None
725
                    }
726
                    None => None,
727
                }
728
            }
729
            "rootfs" => {
730
                rootfs = match ctx.one_string(c) {
731
                    Some(r) => match r.strip_prefix("cas://blob/") {
732
                        Some(h) if h.len() == 64 && h.bytes().all(|b| b.is_ascii_hexdigit() && !b.is_ascii_uppercase()) => {
733
                            Some(h.to_string())
734
                        }
735
                        _ => {
736
                            ctx.err(c.span(), format!("rootfs {r:?}: expected \"cas://blob/\" and 64 lower-case hex digits"));
737
                            None
738
                        }
739
                    },
740
                    None => None,
741
                }
742
            }
743
            "arch" => {
744
                arch = match ctx.one_string(c) {
745
                    Some(a) if arch_ok(&a) => Some(a),
746
                    Some(a) => {
747
                        ctx.err(c.span(), format!("arch {a:?} is not an architecture name such as x86_64, aarch64 or riscv64"));
748
                        None
749
                    }
750
                    None => None,
751
                }
752
            }
753
            "memory-limit" => {
754
                memory = match ctx.one_string(c) {
755
                    Some(m) => match parse_memory(&m) {
756
                        Some(mib) if (MIN_MEMORY_MIB..=MAX_MEMORY_MIB).contains(&mib) => Some(mib as u32),
757
                        Some(_) => {
758
                            ctx.err(c.span(), format!("memory-limit {m:?} must be from 64MiB to 16GiB"));
759
                            None
760
                        }
761
                        None => {
762
                            ctx.err(c.span(), format!("memory-limit {m:?}: expected a whole number of KiB, MiB or GiB, as \"4GiB\""));
763
                            None
764
                        }
765
                    },
766
                    None => None,
767
                }
768
            }
769
            "cpu-limit" => {
770
                cpus = match ctx.one_integer(c) {
771
                    Some(n) if (1..=MAX_CPUS).contains(&n) => Some(n as u32),
772
                    Some(n) => {
773
                        ctx.err(c.span(), format!("cpu-limit {n} must be from 1 to {MAX_CPUS}"));
774
                        None
775
                    }
776
                    None => None,
777
                }
778
            }
779
            "network" => {
780
                let kind = ctx.one_string(c);
781
                if kind.as_deref() != Some("egress") {
782
                    ctx.no_children(c);
783
                }
784
                network = match kind.as_deref() {
785
                    Some("isolated") => Some(Network::Isolated),
786
                    // Loopback only: the guest's `lo` comes up, and it still has
787
                    // no device that reaches anything outside it.
788
                    Some("loopback") => Some(Network::Loopback),
789
                    // Allow-listed egress (oxygenflow#55): still no network
790
                    // device; a proxy in the guest relays to the runner, which
791
                    // lets through only the hosts named here.
792
                    Some("egress") => {
793
                        egress_allow = parse_egress(ctx, c);
794
                        Some(Network::Egress)
795
                    }
796
                    Some(other) => {
797
                        ctx.err(c.span(), format!("network {other:?}: expected \"isolated\", \"loopback\" or \"egress\""));
798
                        None
799
                    }
800
                    None => None,
801
                }
802
            }
803
            "timeout" => timeout_s = parse_timeout(ctx, c),
804
            other => ctx.err(c.span(), format!("unknown sandbox setting `{other}`")),
805
        }
806
    }
807
    if rootfs.is_none() && !seen.contains("rootfs") {
808
        ctx.err(node.span(), format!("sandbox {name:?} needs a rootfs"));
809
    }
810
    if network == Some(Network::Egress) {
811
        if runtime == Some(Runtime::Syscall) {
812
            ctx.err(node.span(), format!("sandbox {name:?}: network \"egress\" needs runtime \"microvm\""));
813
        }
814
        // Only a runner that allows egress offers this label.
815
        if !runs_on.iter().any(|l| l == EGRESS_LABEL) {
816
            runs_on.push(EGRESS_LABEL.to_string());
817
            runs_on.sort();
818
        }
819
    }
820
    if !named {
821
        return None;
822
    }
823
    Some(Sandbox {
824
        name,
825
        runtime: runtime?,
826
        rootfs: rootfs?,
827
        memory_mib: memory?,
828
        cpus: cpus?,
829
        network: network?,
830
        env,
831
        runs_on,
832
        egress_allow,
833
        timeout_s,
834
        arch,
835
    })
836
}
837
838
/// The label a runner offers when it allows egress, and an egress sandbox
839
/// needs (oxygenflow#55).
840
pub const EGRESS_LABEL: &str = "egress";
841
842
/// The most hosts one sandbox may name.
843
const MAX_EGRESS_HOSTS: usize = 32;
844
845
/// Whether `pattern` is a host name, or `*.` and a domain: lower-case
846
/// letters, digits, `-` and dots, at least two labels.
847
pub fn egress_pattern_ok(pattern: &str) -> bool {
848
    let name = pattern.strip_prefix("*.").unwrap_or(pattern);
849
    let labels: Vec<&str> = name.split('.').collect();
850
    name.len() <= 253
851
        && labels.len() >= 2
852
        && labels.iter().all(|l| {
853
            !l.is_empty()
854
                && l.len() <= 63
855
                && !l.starts_with('-')
856
                && !l.ends_with('-')
857
                && l.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-')
858
        })
859
}
860
861
/// Whether `host` is one `patterns` allow: a name exactly, or a name under
862
/// a `*.` domain (not the domain itself). Case and a final dot are ignored.
863
pub fn egress_allows(patterns: &[String], host: &str) -> bool {
864
    let host = host.trim_end_matches('.').to_ascii_lowercase();
865
    patterns.iter().any(|p| match p.strip_prefix("*.") {
866
        Some(domain) => host.len() > domain.len() + 1 && host.ends_with(domain) && host[..host.len() - domain.len()].ends_with('.'),
867
        None => host == *p,
868
    })
869
}
870
871
/// `network "egress" { allow "*.googleapis.com" "registry.opentofu.org" }`.
872
fn parse_egress(ctx: &mut Ctx, node: &KdlNode) -> Vec<String> {
873
    let mut out = Vec::new();
874
    for c in block(ctx, node) {
875
        ctx.no_children(c);
876
        if c.name().value() != "allow" {
877
            ctx.err(c.span(), format!("unknown egress setting `{}`; egress takes `allow \"host\"...`", c.name().value()));
878
            continue;
879
        }
880
        for h in ctx.strings(c) {
881
            if egress_pattern_ok(&h) {
882
                out.push(h);
883
            } else {
884
                ctx.err(
885
                    c.span(),
886
                    format!("allow {h:?}: expected a lower-case host name such as \"registry.opentofu.org\", or \"*.\" and a domain"),
887
                );
888
            }
889
        }
890
    }
891
    out.sort();
892
    out.dedup();
893
    if out.is_empty() {
894
        ctx.err(node.span(), "network \"egress\" needs at least one `allow \"host\"`");
895
    } else if out.len() > MAX_EGRESS_HOSTS {
896
        ctx.err(node.span(), format!("network \"egress\" may allow at most {MAX_EGRESS_HOSTS} hosts"));
897
    }
898
    out
899
}
900
901
/// An `on` block: `push` and `tag` with globs (none: any), `manual`,
902
/// `schedule` with five-field cron lines.
903
fn parse_on(ctx: &mut Ctx, node: &KdlNode) -> On {
904
    let mut on = On::default();
905
    if !node.entries().is_empty() {
906
        ctx.err(node.span(), "`on` takes a block: on { push \"main\"; tag \"v*\"; manual }");
907
    }
908
    let globs = |ctx: &mut Ctx, c: &KdlNode| -> Vec<String> {
909
        let g = ctx.strings(c);
910
        for p in &g {
911
            if let Err(e) = globset::Glob::new(p) {
912
                ctx.err(c.span(), format!("{p:?}: {e}"));
913
            }
914
        }
915
        g
916
    };
917
    for c in block(ctx, node) {
918
        ctx.no_children(c);
919
        match c.name().value() {
920
            "push" => on.push.get_or_insert_with(Vec::new).extend(globs(ctx, c)),
921
            "tag" => on.tag.get_or_insert_with(Vec::new).extend(globs(ctx, c)),
922
            "manual" => on.manual = true,
923
            "schedule" => {
924
                for line in ctx.strings(c) {
925
                    if crate::cron::Cron::parse(&line).is_some() {
926
                        on.schedule.push(line);
927
                    } else {
928
                        ctx.err(
929
                            c.span(),
930
                            format!("schedule {line:?}: expected five cron fields (minute hour day month weekday), as \"0 3 * * *\""),
931
                        );
932
                    }
933
                }
934
            }
935
            other => ctx.err(c.span(), format!("unknown `on` event `{other}`; expected push, tag, manual or schedule")),
936
        }
937
    }
938
    if on.push.is_none() && on.tag.is_none() && !on.manual && on.schedule.is_empty() {
939
        ctx.err(node.span(), "`on` names no event, so nothing would run it");
940
    }
941
    on
942
}
943
944
/// The longest a pipeline may ask a job to run.
945
pub const MAX_TIMEOUT_S: u64 = 24 * 3600;
946
947
/// A duration: whole hours, minutes and seconds in that order, each
948
/// optional, as "90m", "2h", "1h30m" or "45s".
949
pub fn parse_duration(s: &str) -> Option<u64> {
950
    let (mut total, mut num, mut last) = (0u64, String::new(), 0u8);
951
    for ch in s.chars() {
952
        if ch.is_ascii_digit() {
953
            num.push(ch);
954
            continue;
955
        }
956
        let (unit, mult) = match ch {
957
            'h' => (1, 3600),
958
            'm' => (2, 60),
959
            's' => (3, 1),
960
            _ => return None,
961
        };
962
        // Each unit once, largest first, each with its number.
963
        if num.is_empty() || unit <= last {
964
            return None;
965
        }
966
        last = unit;
967
        total = total.checked_add(num.parse::<u64>().ok()?.checked_mul(mult)?)?;
968
        num.clear();
969
    }
970
    (num.is_empty() && total > 0).then_some(total)
971
}
972
973
/// A `timeout` line: one duration, from a minute to a day.
974
fn parse_timeout(ctx: &mut Ctx, c: &KdlNode) -> Option<u64> {
975
    let t = ctx.one_string(c)?;
976
    match parse_duration(&t) {
977
        Some(secs) if (60..=MAX_TIMEOUT_S).contains(&secs) => Some(secs),
978
        Some(_) => {
979
            ctx.err(c.span(), format!("timeout {t:?} must be from 1m to 24h"));
980
            None
981
        }
982
        None => {
983
            ctx.err(c.span(), format!("timeout {t:?}: expected hours, minutes and seconds, as \"90m\" or \"1h30m\""));
984
            None
985
        }
986
    }
987
}
988
989
/// The labels of a `runs-on` line: lower-case letters, digits, `-`, `_`
990
/// and `.`, at most 64 bytes each; into `into`, sorted, once each.
991
fn parse_labels(ctx: &mut Ctx, c: &KdlNode, into: &mut Vec<String>) {
992
    let labels = ctx.strings(c);
993
    if labels.is_empty() {
994
        ctx.err(c.span(), "`runs-on` needs one or more labels, as runs-on \"riscv64\"");
995
    }
996
    for l in labels {
997
        let ok = !l.is_empty()
998
            && l.len() <= 64
999
            && l.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || matches!(b, b'-' | b'_' | b'.'));
1000
        if ok {
1001
            into.push(l);
1002
        } else {
1003
            ctx.err(c.span(), format!("runs-on {l:?}: a label is lower-case letters, digits, -, _ and ., at most 64"));
1004
        }
1005
    }
1006
    into.sort();
1007
    into.dedup();
1008
}
1009
1010
/// An `env` block of `NAME "value"` lines, into `env`.
1011
fn parse_env(ctx: &mut Ctx, c: &KdlNode, env: &mut BTreeMap<String, String>) {
1012
    if !c.entries().is_empty() {
1013
        ctx.err(c.span(), "`env` takes a block of NAME \"value\" lines");
1014
    }
1015
    for e in block(ctx, c) {
1016
        let k = e.name().value().to_string();
1017
        let valid =
1018
            !k.is_empty() && !k.starts_with(|c: char| c.is_ascii_digit()) && k.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_');
1019
        if !valid {
1020
            ctx.err(e.span(), format!("environment name {k:?} must be letters, digits and _"));
1021
            continue;
1022
        }
1023
        ctx.no_children(e);
1024
        if let Some(v) = ctx.one_string(e) {
1025
            if env.insert(k.clone(), v).is_some() {
1026
                ctx.err(e.span(), format!("environment {k} is set twice"));
1027
            }
1028
        }
1029
    }
1030
}
1031
1032
/// `mount { source "ephemeral://disk"; target "/src/target"; size "8GiB" }`.
1033
fn parse_mount(ctx: &mut Ctx, node: &KdlNode) -> Option<Mount> {
1034
    let (mut source, mut target, mut size) = (None, None, None);
1035
    let (mut cache, mut keys) = (None, Vec::new());
1036
    let mut seen = BTreeSet::new();
1037
    for c in block(ctx, node) {
1038
        let key = c.name().value();
1039
        if !seen.insert(key.to_string()) {
1040
            ctx.err(c.span(), format!("`{key}` is given twice"));
1041
            continue;
1042
        }
1043
        ctx.no_children(c);
1044
        match key {
1045
            "source" => {
1046
                source = match ctx.one_string(c).as_deref() {
1047
                    Some("ephemeral://tmpfs") => Some(MountSource::Tmpfs),
1048
                    Some("ephemeral://disk") => Some(MountSource::Disk),
1049
                    Some(other) => {
1050
                        ctx.err(
1051
                            c.span(),
1052
                            format!("mount source {other:?} is not in M0 yet; M0 mounts \"ephemeral://tmpfs\" or \"ephemeral://disk\""),
1053
                        );
1054
                        None
1055
                    }
1056
                    None => None,
1057
                }
1058
            }
1059
            "target" => {
1060
                target = match ctx.one_string(c) {
1061
                    Some(t) if mount_target_ok(&t) => Some(t),
1062
                    Some(t) => {
1063
                        ctx.err(
1064
                            c.span(),
1065
                            format!("mount target {t:?} must be an absolute path under /src or /tmp, without . or .. or a trailing /"),
1066
                        );
1067
                        None
1068
                    }
1069
                    None => None,
1070
                }
1071
            }
1072
            "size" => {
1073
                size = match ctx.one_string(c) {
1074
                    Some(m) => match parse_memory(&m) {
1075
                        Some(mib) if (MIN_MOUNT_MIB..=MAX_MOUNT_MIB).contains(&mib) => Some(mib as u32),
1076
                        Some(_) => {
1077
                            ctx.err(c.span(), format!("mount size {m:?} must be from 16MiB to 64GiB"));
1078
                            None
1079
                        }
1080
                        None => {
1081
                            ctx.err(c.span(), format!("mount size {m:?}: expected a whole number of KiB, MiB or GiB, as \"8GiB\""));
1082
                            None
1083
                        }
1084
                    },
1085
                    None => None,
1086
                }
1087
            }
1088
            "cache" => {
1089
                cache = match ctx.one_string(c) {
1090
                    Some(n) if !n.is_empty() && n.len() <= 64 && n.bytes().all(|b| b.is_ascii_alphanumeric() || b"._-".contains(&b)) => {
1091
                        Some(n)
1092
                    }
1093
                    Some(n) => {
1094
                        ctx.err(c.span(), format!("cache name {n:?}: letters, digits, '.', '_' and '-', at most 64"));
1095
                        None
1096
                    }
1097
                    None => None,
1098
                }
1099
            }
1100
            "key" => {
1101
                keys = ctx.strings(c);
1102
                if keys.is_empty() {
1103
                    ctx.err(c.span(), "`key` names one or more files, as \"Cargo.lock\"");
1104
                }
1105
                for k in &keys {
1106
                    if k.starts_with('/') || k.split('/').any(|p| p.is_empty() || p == "." || p == "..") {
1107
                        ctx.err(c.span(), format!("key file {k:?} must be a path in the repository, without . or .."));
1108
                    }
1109
                }
1110
            }
1111
            "readonly" | "include" => ctx.err(c.span(), format!("mount `{key}` is not in M0 yet")),
1112
            other => ctx.err(c.span(), format!("unknown mount setting `{other}`")),
1113
        }
1114
    }
1115
    for (name, missing) in [("source", source.is_none()), ("target", target.is_none()), ("size", size.is_none())] {
1116
        if missing && !seen.contains(name) {
1117
            ctx.err(node.span(), format!("a mount needs a {name}"));
1118
        }
1119
    }
1120
    if source == Some(MountSource::Tmpfs) && cache.is_some() {
1121
        ctx.err(node.span(), "only an ephemeral://disk mount can be a cache");
1122
    }
1123
    if !keys.is_empty() && cache.is_none() {
1124
        ctx.err(node.span(), "`key` goes with `cache`");
1125
    }
1126
    Some(Mount { source: source?, target: target?, size_mib: size?, cache, key: keys })
1127
}
1128
1129
/// `output { artifact "name" { path "/src/target/release/tool" } }`.
1130
/// `secret "deploy-token" { mount-path "/run/secrets/token" }` (spec §9.1).
1131
/// The hub is the vault: the secret of that name set for the repository,
1132
/// its organisation or the site. `vault-ref` is not in this slice.
1133
fn parse_secret(ctx: &mut Ctx, node: &KdlNode, job: &mut Job) {
1134
    let Some(name) = ctx.one_string(node) else {
1135
        ctx.err(node.span(), "`secret` takes a name and a block with mount-path");
1136
        return;
1137
    };
1138
    if !ctx.name_ok(node.span(), "secret", &name) {
1139
        return;
1140
    }
1141
    let mut path = None;
1142
    for c in block(ctx, node) {
1143
        ctx.no_children(c);
1144
        match c.name().value() {
1145
            "mount-path" => match ctx.one_string(c) {
1146
                Some(p) if secret_path_ok(&p) => path = Some(p),
1147
                Some(p) => {
1148
                    ctx.err(c.span(), format!("secret mount-path {p:?} must be an absolute path under /run/secrets/, without . or .."))
1149
                }
1150
                None => {}
1151
            },
1152
            "vault-ref" => ctx.err(c.span(), "`vault-ref` is not in M0; the hub supplies the secret of that name"),
1153
            other => ctx.err(c.span(), format!("unknown secret setting `{other}`")),
1154
        }
1155
    }
1156
    let Some(mount_path) = path else {
1157
        ctx.err(node.span(), format!("secret {name:?} needs a mount-path"));
1158
        return;
1159
    };
1160
    if job.secrets.iter().any(|s| s.name == name) {
1161
        ctx.err(node.span(), format!("secret {name:?} is given twice"));
1162
    } else if job.secrets.iter().any(|s| s.mount_path == mount_path) || job.identities.iter().any(|i| i.mount_path == mount_path) {
1163
        ctx.err(node.span(), format!("two files mount at {mount_path:?}"));
1164
    } else if job.secrets.len() == MAX_SECRETS {
1165
        ctx.err(node.span(), format!("a job has at most {MAX_SECRETS} secrets"));
1166
    } else {
1167
        job.secrets.push(Secret { name, mount_path });
1168
    }
1169
}
1170
1171
/// `identity "sts.amazonaws.com" { mount-path "/run/secrets/aws-token" }`:
1172
/// the hub mints an OIDC token for that audience and the runner mounts it
1173
/// as a file (oxygenhub#103).
1174
fn parse_identity(ctx: &mut Ctx, node: &KdlNode, job: &mut Job) {
1175
    let Some(audience) = ctx.one_string(node) else {
1176
        ctx.err(node.span(), "`identity` takes an audience and a block with mount-path");
1177
        return;
1178
    };
1179
    let aud_ok = !audience.is_empty() && audience.len() <= 255 && !audience.chars().any(|c| c.is_whitespace() || c.is_control());
1180
    if !aud_ok {
1181
        ctx.err(node.span(), format!("identity audience {audience:?} must be 1 to 255 characters without spaces"));
1182
        return;
1183
    }
1184
    let mut path = None;
1185
    for c in block(ctx, node) {
1186
        ctx.no_children(c);
1187
        match c.name().value() {
1188
            "mount-path" => match ctx.one_string(c) {
1189
                Some(p) if secret_path_ok(&p) => path = Some(p),
1190
                Some(p) => {
1191
                    ctx.err(c.span(), format!("identity mount-path {p:?} must be an absolute path under /run/secrets/, without . or .."))
1192
                }
1193
                None => {}
1194
            },
1195
            other => ctx.err(c.span(), format!("unknown identity setting `{other}`")),
1196
        }
1197
    }
1198
    let Some(mount_path) = path else {
1199
        ctx.err(node.span(), format!("identity {audience:?} needs a mount-path"));
1200
        return;
1201
    };
1202
    if job.identities.iter().any(|i| i.audience == audience) {
1203
        ctx.err(node.span(), format!("identity {audience:?} is given twice"));
1204
    } else if job.identities.iter().any(|i| i.mount_path == mount_path) || job.secrets.iter().any(|s| s.mount_path == mount_path) {
1205
        ctx.err(node.span(), format!("two files mount at {mount_path:?}"));
1206
    } else if job.identities.len() == MAX_IDENTITIES {
1207
        ctx.err(node.span(), format!("a job has at most {MAX_IDENTITIES} identities"));
1208
    } else {
1209
        job.identities.push(Identity { audience, mount_path });
1210
    }
1211
}
1212
1213
fn parse_output(ctx: &mut Ctx, node: &KdlNode, job: &mut Job) {
1214
    if !node.entries().is_empty() {
1215
        ctx.err(node.span(), "`output` takes a block of artifacts and reports");
1216
    }
1217
    for a in block(ctx, node) {
1218
        let kind = match a.name().value() {
1219
            "artifact" => OutputKind::Artifact,
1220
            "report" => OutputKind::Report,
1221
            other => {
1222
                ctx.err(
1223
                    a.span(),
1224
                    format!("unknown output `{other}`; outputs are `artifact \"name\" {{ path \"/src/file\" }}` and `report \"name\" {{ path \"/src/dir\" }}`"),
1225
                );
1226
                continue;
1227
            }
1228
        };
1229
        let word = kind.word();
1230
        let Some(name) = ctx.one_string(a) else { continue };
1231
        if !ctx.name_ok(a.span(), word, &name) {
1232
            continue;
1233
        }
1234
        let mut path = None;
1235
        let mut always = false;
1236
        for c in block(ctx, a) {
1237
            ctx.no_children(c);
1238
            match c.name().value() {
1239
                "path" => match ctx.one_string(c) {
1240
                    Some(p) if output_path_ok(&p) => path = Some(p),
1241
                    Some(p) => ctx
1242
                        .err(c.span(), format!("{word} path {p:?} must be an absolute path under /src, without . or .., quotes or spaces")),
1243
                    None => {}
1244
                },
1245
                "format" if kind == OutputKind::Artifact => {
1246
                    if ctx.one_string(c).as_deref() != Some("blob") {
1247
                        ctx.err(c.span(), "artifact format is \"blob\"");
1248
                    }
1249
                }
1250
                "when" => match ctx.one_string(c).as_deref() {
1251
                    Some("passed") => always = false,
1252
                    Some("always") => always = true,
1253
                    Some(_) => ctx.err(c.span(), format!("{word} `when` is \"passed\" (the default) or \"always\"")),
1254
                    None => {}
1255
                },
1256
                other => ctx.err(c.span(), format!("unknown {word} setting `{other}`")),
1257
            }
1258
        }
1259
        let Some(path) = path else {
1260
            ctx.err(a.span(), format!("{word} {name:?} needs a path"));
1261
            continue;
1262
        };
1263
        if job.outputs.iter().any(|o| o.name == name) {
1264
            ctx.err(a.span(), format!("output {name:?} is given twice (artifacts and reports share names)"));
1265
        } else if job.outputs.len() == MAX_OUTPUTS {
1266
            ctx.err(a.span(), format!("a job has at most {MAX_OUTPUTS} artifacts and reports"));
1267
        } else {
1268
            job.outputs.push(Output { name, path, always, kind });
1269
        }
1270
    }
1271
}
1272
1273
fn parse_job(ctx: &mut Ctx, node: &KdlNode) -> Option<Job> {
1274
    let name = ctx.one_string(node)?;
1275
    let named = ctx.name_ok(node.span(), "job", &name);
1276
    let mut job = Job {
1277
        name: name.clone(),
1278
        sandbox: String::new(),
1279
        scope: vec![],
1280
        depends_on: vec![],
1281
        env: BTreeMap::new(),
1282
        exec: vec![],
1283
        mounts: vec![],
1284
        secrets: vec![],
1285
        identities: vec![],
1286
        outputs: vec![],
1287
        runs_on: vec![],
1288
        timeout_s: None,
1289
        on: None,
1290
    };
1291
    let mut seen = BTreeSet::new();
1292
    let mut outputs_at = None;
1293
    for c in block(ctx, node) {
1294
        let key = c.name().value();
1295
        if key != "scope"
1296
            && key != "depends-on"
1297
            && key != "mount"
1298
            && key != "secret"
1299
            && key != "identity"
1300
            && key != "runs-on"
1301
            && !seen.insert(key.to_string())
1302
        {
1303
            ctx.err(c.span(), format!("`{key}` is given twice"));
1304
        }
1305
        match key {
1306
            "sandbox" => {
1307
                ctx.no_children(c);
1308
                job.sandbox = ctx.one_string(c).unwrap_or_default();
1309
            }
1310
            "scope" => {
1311
                ctx.no_children(c);
1312
                for g in ctx.strings(c) {
1313
                    match globset::Glob::new(&g) {
1314
                        Ok(_) => job.scope.push(g),
1315
                        Err(e) => ctx.err(c.span(), format!("scope {g:?}: {e}")),
1316
                    }
1317
                }
1318
            }
1319
            "depends-on" => {
1320
                ctx.no_children(c);
1321
                job.depends_on.extend(ctx.strings(c));
1322
            }
1323
            "runs-on" => {
1324
                ctx.no_children(c);
1325
                parse_labels(ctx, c, &mut job.runs_on);
1326
            }
1327
            "timeout" => {
1328
                ctx.no_children(c);
1329
                job.timeout_s = parse_timeout(ctx, c);
1330
            }
1331
            "on" => job.on = Some(parse_on(ctx, c)),
1332
            "env" => parse_env(ctx, c, &mut job.env),
1333
            "exec" => {
1334
                ctx.no_children(c);
1335
                job.exec = ctx.strings(c);
1336
                if job.exec.is_empty() {
1337
                    ctx.err(c.span(), "`exec` needs a program and its arguments");
1338
                } else if job.exec.iter().any(|a| a.contains('\0')) {
1339
                    ctx.err(c.span(), "`exec` arguments cannot contain NUL");
1340
                }
1341
            }
1342
            "mount" => {
1343
                if !c.entries().is_empty() {
1344
                    ctx.err(c.span(), "`mount` takes a block of source, target and size");
1345
                }
1346
                if let Some(m) = parse_mount(ctx, c) {
1347
                    if job.mounts.len() == MAX_MOUNTS {
1348
                        ctx.err(c.span(), format!("a job has at most {MAX_MOUNTS} mounts"));
1349
                    } else if let Some(other) = job.mounts.iter().find(|o| {
1350
                        o.target == m.target
1351
                            || m.target.starts_with(&format!("{}/", o.target))
1352
                            || o.target.starts_with(&format!("{}/", m.target))
1353
                    }) {
1354
                        ctx.err(c.span(), format!("mount {:?} overlaps mount {:?}", m.target, other.target));
1355
                    } else {
1356
                        job.mounts.push(m);
1357
                    }
1358
                }
1359
            }
1360
            "output" => {
1361
                outputs_at = Some(c.span());
1362
                parse_output(ctx, c, &mut job);
1363
            }
1364
            "secret" => parse_secret(ctx, c, &mut job),
1365
            "identity" => parse_identity(ctx, c, &mut job),
1366
            n if LATER_JOB.contains(&n) => ctx.err(c.span(), format!("`{n}` is not in M0 yet")),
1367
            other => ctx.err(c.span(), format!("unknown job setting `{other}`")),
1368
        }
1369
    }
1370
    job.mounts.sort_by(|a, b| a.target.cmp(&b.target));
1371
    job.outputs.sort_by(|a, b| a.name.cmp(&b.name));
1372
    job.secrets.sort_by(|a, b| a.name.cmp(&b.name));
1373
    job.identities.sort_by(|a, b| a.audience.cmp(&b.audience));
1374
    // A tmpfs is gone when the job ends; nothing on it can be kept.
1375
    for o in &job.outputs {
1376
        if let Some(m) = job.mounts.iter().find(|m| m.source == MountSource::Tmpfs && o.path.starts_with(&format!("{}/", m.target))) {
1377
            ctx.err(
1378
                outputs_at.unwrap_or(node.span()),
1379
                format!(
1380
                    "artifact {:?} is on the tmpfs mount {:?}, which is gone when the job ends; use ephemeral://disk",
1381
                    o.name, m.target
1382
                ),
1383
            );
1384
        }
1385
    }
1386
    if job.sandbox.is_empty() && !seen.contains("sandbox") {
1387
        ctx.err(node.span(), format!("job {name:?} needs a sandbox"));
1388
    }
1389
    if job.exec.is_empty() && !seen.contains("exec") {
1390
        ctx.err(node.span(), format!("job {name:?} needs an exec"));
1391
    }
1392
    job.depends_on.sort();
1393
    job.depends_on.dedup();
1394
    // Kept even when incomplete, so its references are checked too; the
1395
    // problems above already make the pipeline invalid.
1396
    named.then_some(job)
1397
}