| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | | use serde::{Deserialize, Serialize}; |
| 6 | | use std::collections::{BTreeMap, BTreeSet}; |
| 7 | | use std::fmt; |
| 8 | |
|
| 9 | |
|
| 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 | | |
| 16 | | pub sandboxes: BTreeMap<String, Sandbox>, |
| 17 | | |
| 18 | | pub jobs: BTreeMap<String, Job>, |
| 19 | | |
| 20 | | |
| 21 | | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 22 | | pub on: Option<On>, |
| 23 | | } |
| 24 | |
|
| 25 | |
|
| 26 | | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 27 | | pub struct On { |
| 28 | | |
| 29 | | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 30 | | pub push: Option<Vec<String>>, |
| 31 | | |
| 32 | | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 33 | | pub tag: Option<Vec<String>>, |
| 34 | | |
| 35 | | #[serde(default, skip_serializing_if = "std::ops::Not::not")] |
| 36 | | pub manual: bool, |
| 37 | | |
| 38 | | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 39 | | pub schedule: Vec<String>, |
| 40 | | } |
| 41 | |
|
| 42 | |
|
| 43 | | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 44 | | pub enum Event<'a> { |
| 45 | | |
| 46 | | Push(&'a str), |
| 47 | | |
| 48 | | Tag(&'a str), |
| 49 | | |
| 50 | | Manual, |
| 51 | | |
| 52 | | Schedule, |
| 53 | | |
| 54 | | ScheduleAt(When), |
| 55 | | } |
| 56 | |
|
| 57 | |
|
| 58 | | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 59 | | pub struct When { |
| 60 | | pub minute: u32, |
| 61 | | pub hour: u32, |
| 62 | | pub day: u32, |
| 63 | | pub month: u32, |
| 64 | | |
| 65 | | pub weekday: u32, |
| 66 | | } |
| 67 | |
|
| 68 | | impl On { |
| 69 | | |
| 70 | 2× | pub fn default_on() -> On { |
| 71 | 2× | On { push: Some(vec![]), tag: None, manual: true, schedule: vec![] } |
| 72 | 2× | } |
| 73 | |
|
| 74 | 16× | pub fn matches(&self, event: Event<'_>) -> bool { |
| 75 | 16× | let any = |globs: &Option<Vec<String>>, name: &str| match globs { |
| 76 | 2× | None => false, |
| 77 | 6× | Some(g) if g.is_empty() => true, |
| 78 | 7× | Some(g) => g.iter().any(|p| globset::Glob::new(p).is_ok_and(|p| p.compile_matcher().is_match(name))), |
| 79 | 8× | }; |
| 80 | 16× | match event { |
| 81 | 5× | Event::Push(b) => any(&self.push, b), |
| 82 | 3× | Event::Tag(t) => any(&self.tag, t), |
| 83 | 4× | Event::Manual => self.manual, |
| 84 | 1× | Event::Schedule => !self.schedule.is_empty(), |
| 85 | 3× | Event::ScheduleAt(w) => self |
| 86 | 3× | .schedule |
| 87 | 3× | .iter() |
| 88 | 3× | .filter_map(|l| crate::cron::Cron::parse(l)) |
| 89 | 3× | .any(|c| c.matches(w.minute, w.hour, w.day, w.month, w.weekday)), |
| 90 | | } |
| 91 | 16× | } |
| 92 | | } |
| 93 | |
|
| 94 | | impl Pipeline { |
| 95 | | |
| 96 | 16× | pub fn on_of(&self, job: &Job) -> On { |
| 97 | 16× | job.on.clone().or_else(|| self.on.clone()).unwrap_or_else(On::default_on) |
| 98 | 16× | } |
| 99 | | } |
| 100 | |
|
| 101 | | #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] |
| 102 | | #[serde(rename_all = "lowercase")] |
| 103 | | pub enum Runtime { |
| 104 | | |
| 105 | | Wasm, |
| 106 | | |
| 107 | | Syscall, |
| 108 | | |
| 109 | | Microvm, |
| 110 | | } |
| 111 | |
|
| 112 | | impl Runtime { |
| 113 | 42× | pub fn as_str(self) -> &'static str { |
| 114 | 42× | match self { |
| 115 | 0× | Runtime::Wasm => "wasm", |
| 116 | 0× | Runtime::Syscall => "syscall", |
| 117 | 42× | Runtime::Microvm => "microvm", |
| 118 | | } |
| 119 | 42× | } |
| 120 | | } |
| 121 | |
|
| 122 | | #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 123 | | #[serde(rename_all = "lowercase")] |
| 124 | | pub enum Network { |
| 125 | | Isolated, |
| 126 | | Loopback, |
| 127 | | Egress, |
| 128 | | } |
| 129 | |
|
| 130 | | impl Network { |
| 131 | 42× | pub fn as_str(self) -> &'static str { |
| 132 | 42× | match self { |
| 133 | 39× | Network::Isolated => "isolated", |
| 134 | 1× | Network::Loopback => "loopback", |
| 135 | 2× | Network::Egress => "egress", |
| 136 | | } |
| 137 | 42× | } |
| 138 | | } |
| 139 | |
|
| 140 | | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 141 | | pub struct Sandbox { |
| 142 | | pub name: String, |
| 143 | | pub runtime: Runtime, |
| 144 | | |
| 145 | | pub rootfs: String, |
| 146 | | pub memory_mib: u32, |
| 147 | | pub cpus: u32, |
| 148 | | pub network: Network, |
| 149 | | |
| 150 | | |
| 151 | | #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] |
| 152 | | pub env: BTreeMap<String, String>, |
| 153 | | |
| 154 | | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 155 | | pub runs_on: Vec<String>, |
| 156 | | |
| 157 | | |
| 158 | | |
| 159 | | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 160 | | pub egress_allow: Vec<String>, |
| 161 | | |
| 162 | | |
| 163 | | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 164 | | pub timeout_s: Option<u64>, |
| 165 | | |
| 166 | | |
| 167 | | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 168 | | pub arch: Option<String>, |
| 169 | | } |
| 170 | |
|
| 171 | |
|
| 172 | 6× | pub fn arch_ok(a: &str) -> bool { |
| 173 | 22× | (2..=16).contains(&a.len()) && a.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'_') |
| 174 | 6× | } |
| 175 | |
|
| 176 | | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 177 | | pub struct Job { |
| 178 | | pub name: String, |
| 179 | | pub sandbox: String, |
| 180 | | |
| 181 | | pub scope: Vec<String>, |
| 182 | | |
| 183 | | pub depends_on: Vec<String>, |
| 184 | | pub env: BTreeMap<String, String>, |
| 185 | | |
| 186 | | pub exec: Vec<String>, |
| 187 | | |
| 188 | | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 189 | | pub mounts: Vec<Mount>, |
| 190 | | |
| 191 | | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 192 | | pub outputs: Vec<Output>, |
| 193 | | |
| 194 | | |
| 195 | | |
| 196 | | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 197 | | pub inputs: Vec<Input>, |
| 198 | | |
| 199 | | |
| 200 | | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 201 | | pub approval: Option<Approval>, |
| 202 | | |
| 203 | | |
| 204 | | |
| 205 | | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 206 | | pub environment: Option<String>, |
| 207 | | |
| 208 | | |
| 209 | | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 210 | | pub secrets: Vec<Secret>, |
| 211 | | |
| 212 | | |
| 213 | | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 214 | | pub identities: Vec<Identity>, |
| 215 | | |
| 216 | | |
| 217 | | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 218 | | pub runs_on: Vec<String>, |
| 219 | | |
| 220 | | |
| 221 | | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 222 | | pub timeout_s: Option<u64>, |
| 223 | | |
| 224 | | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 225 | | pub on: Option<On>, |
| 226 | | |
| 227 | | |
| 228 | | |
| 229 | | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 230 | | pub priority: Option<String>, |
| 231 | | } |
| 232 | |
|
| 233 | | impl Job { |
| 234 | | |
| 235 | 16× | pub fn in_sandbox(&self, sandbox: &Sandbox) -> Job { |
| 236 | 16× | let mut job = self.clone(); |
| 237 | 16× | for (k, v) in &sandbox.env { |
| 238 | 2× | job.env.entry(k.clone()).or_insert_with(|| v.clone()); |
| 239 | | } |
| 240 | 16× | job.runs_on.extend(sandbox.runs_on.iter().cloned()); |
| 241 | 16× | job.runs_on.sort(); |
| 242 | 16× | job.runs_on.dedup(); |
| 243 | 16× | job.timeout_s = job.timeout_s.or(sandbox.timeout_s); |
| 244 | 16× | job |
| 245 | 16× | } |
| 246 | |
|
| 247 | | |
| 248 | | |
| 249 | | |
| 250 | 25× | pub fn with_flow_env(&self, vars: &[(String, String)]) -> Job { |
| 251 | 25× | let mut job = self.clone(); |
| 252 | 31× | for (k, v) in vars { |
| 253 | 31× | job.env.entry(k.clone()).or_insert_with(|| v.clone()); |
| 254 | | } |
| 255 | 25× | job |
| 256 | 25× | } |
| 257 | | } |
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 264 | | pub struct Mount { |
| 265 | | pub source: MountSource, |
| 266 | | |
| 267 | | pub target: String, |
| 268 | | pub size_mib: u32, |
| 269 | | |
| 270 | | |
| 271 | | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 272 | | pub cache: Option<String>, |
| 273 | | |
| 274 | | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 275 | | pub key: Vec<String>, |
| 276 | | } |
| 277 | |
|
| 278 | | #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 279 | | #[serde(rename_all = "lowercase")] |
| 280 | | pub enum MountSource { |
| 281 | | |
| 282 | | Tmpfs, |
| 283 | | |
| 284 | | Disk, |
| 285 | | } |
| 286 | |
|
| 287 | | impl MountSource { |
| 288 | 14× | pub fn as_str(self) -> &'static str { |
| 289 | 14× | match self { |
| 290 | 5× | MountSource::Tmpfs => "ephemeral://tmpfs", |
| 291 | 9× | MountSource::Disk => "ephemeral://disk", |
| 292 | | } |
| 293 | 14× | } |
| 294 | | } |
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 299 | | pub struct Output { |
| 300 | | |
| 301 | | pub name: String, |
| 302 | | |
| 303 | | pub path: String, |
| 304 | | |
| 305 | | |
| 306 | | #[serde(default, skip_serializing_if = "std::ops::Not::not")] |
| 307 | | pub always: bool, |
| 308 | | |
| 309 | | |
| 310 | | #[serde(default, skip_serializing_if = "OutputKind::is_artifact")] |
| 311 | | pub kind: OutputKind, |
| 312 | | } |
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 317 | | pub struct Input { |
| 318 | | |
| 319 | | pub artifact: String, |
| 320 | | |
| 321 | | pub from: String, |
| 322 | | |
| 323 | | pub path: String, |
| 324 | | } |
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 329 | | pub struct Approval { |
| 330 | | |
| 331 | | pub by: String, |
| 332 | | |
| 333 | | #[serde(default, skip_serializing_if = "std::ops::Not::not")] |
| 334 | | pub others_only: bool, |
| 335 | | |
| 336 | | pub expires_s: u64, |
| 337 | | |
| 338 | | |
| 339 | | |
| 340 | | #[serde(default, skip_serializing_if = "std::ops::Not::not")] |
| 341 | | pub sealed: bool, |
| 342 | | } |
| 343 | |
|
| 344 | |
|
| 345 | | const MAX_APPROVAL_S: u64 = 30 * 24 * 3600; |
| 346 | |
|
| 347 | |
|
| 348 | | const MAX_INPUTS: usize = 8; |
| 349 | |
|
| 350 | |
|
| 351 | | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 352 | | #[serde(rename_all = "lowercase")] |
| 353 | | pub enum OutputKind { |
| 354 | | #[default] |
| 355 | | Artifact, |
| 356 | | Report, |
| 357 | | } |
| 358 | |
|
| 359 | | impl OutputKind { |
| 360 | 16× | pub fn is_artifact(&self) -> bool { |
| 361 | 16× | *self == OutputKind::Artifact |
| 362 | 16× | } |
| 363 | |
|
| 364 | 40× | pub fn word(&self) -> &'static str { |
| 365 | 40× | match self { |
| 366 | 24× | OutputKind::Artifact => "artifact", |
| 367 | 16× | OutputKind::Report => "report", |
| 368 | | } |
| 369 | 40× | } |
| 370 | | } |
| 371 | |
|
| 372 | |
|
| 373 | | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 374 | | pub struct Secret { |
| 375 | | |
| 376 | | pub name: String, |
| 377 | | |
| 378 | | |
| 379 | | pub mount_path: String, |
| 380 | | } |
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 385 | | pub struct Identity { |
| 386 | | |
| 387 | | pub audience: String, |
| 388 | | |
| 389 | | |
| 390 | | pub mount_path: String, |
| 391 | | } |
| 392 | |
|
| 393 | |
|
| 394 | 17× | pub fn secret_path_ok(p: &str) -> bool { |
| 395 | 17× | p.strip_prefix("/run/secrets/").is_some_and(|rest| !rest.is_empty() && rest.split('/').all(|c| !c.is_empty() && c != "." && c != "..")) |
| 396 | 17× | } |
| 397 | |
|
| 398 | |
|
| 399 | | pub const MAX_OUTPUTS: usize = 16; |
| 400 | |
|
| 401 | |
|
| 402 | |
|
| 403 | 44× | fn output_path_ok(p: &str) -> bool { |
| 404 | 44× | p.strip_prefix("/src/").is_some_and(|rest| { |
| 405 | 42× | !rest.is_empty() |
| 406 | 70× | && rest.split('/').all(|c| !c.is_empty() && c != "." && c != "..") |
| 407 | 467× | && p.bytes().all(|b| b.is_ascii_graphic() && b != b'"' && b != b'\\') |
| 408 | 42× | }) |
| 409 | 44× | } |
| 410 | |
|
| 411 | |
|
| 412 | | pub const MAX_MOUNTS: usize = 4; |
| 413 | |
|
| 414 | | pub const MAX_SECRETS: usize = 8; |
| 415 | |
|
| 416 | | pub const MAX_IDENTITIES: usize = 4; |
| 417 | |
|
| 418 | | pub const MIN_MOUNT_MIB: u64 = 16; |
| 419 | | pub const MAX_MOUNT_MIB: u64 = 64 * 1024; |
| 420 | |
|
| 421 | |
|
| 422 | 59× | fn mount_target_ok(t: &str) -> bool { |
| 423 | 59× | let under = t.strip_prefix("/src/").or_else(|| t.strip_prefix("/tmp/")); |
| 424 | 59× | under.is_some_and(|rest| !rest.is_empty() && rest.split('/').all(|c| !c.is_empty() && c != "." && c != "..")) |
| 425 | 59× | } |
| 426 | |
|
| 427 | |
|
| 428 | | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 429 | | pub struct Problem { |
| 430 | | pub line: usize, |
| 431 | | pub message: String, |
| 432 | | } |
| 433 | |
|
| 434 | | impl fmt::Display for Problem { |
| 435 | 22× | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 436 | 22× | write!(f, "line {}: {}", self.line, self.message) |
| 437 | 22× | } |
| 438 | | } |
| 439 | |
|
| 440 | |
|
| 441 | | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, thiserror::Error)] |
| 442 | | pub struct Problems(pub Vec<Problem>); |
| 443 | |
|
| 444 | | impl fmt::Display for Problems { |
| 445 | 17× | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 446 | 22× | let lines: Vec<String> = self.0.iter().map(|p| p.to_string()).collect(); |
| 447 | 17× | f.write_str(&lines.join("\n")) |
| 448 | 17× | } |
| 449 | | } |
| 450 | |
|
| 451 | | const MIN_MEMORY_MIB: u64 = 64; |
| 452 | | const MAX_MEMORY_MIB: u64 = 16 * 1024; |
| 453 | | const MAX_CPUS: i128 = 16; |
| 454 | |
|
| 455 | |
|
| 456 | | const LATER_TOP: &[&str] = &["publish", "secret"]; |
| 457 | | const LATER_JOB: &[&str] = &["attest"]; |
| 458 | |
|
| 459 | | struct Ctx<'a> { |
| 460 | | src: &'a str, |
| 461 | | problems: Vec<Problem>, |
| 462 | | } |
| 463 | |
|
| 464 | | impl Ctx<'_> { |
| 465 | 108× | fn line(&self, span: SourceSpan) -> usize { |
| 466 | 108× | let at = span.offset().min(self.src.len()); |
| 467 | 28700× | self.src[..at].bytes().filter(|b| *b == b'\n').count() + 1 |
| 468 | 108× | } |
| 469 | |
|
| 470 | 108× | fn err(&mut self, span: SourceSpan, message: impl Into<String>) { |
| 471 | 108× | let line = self.line(span); |
| 472 | 108× | self.problems.push(Problem { line, message: message.into() }); |
| 473 | 108× | } |
| 474 | |
|
| 475 | | |
| 476 | 1516× | fn strings(&mut self, node: &KdlNode) -> Vec<String> { |
| 477 | 1516× | let mut out = Vec::new(); |
| 478 | 1558× | for e in node.entries() { |
| 479 | 1558× | if let Some(k) = e.name() { |
| 480 | 0× | self.err(e.span(), format!("`{}` takes no property `{}`", node.name().value(), k.value())); |
| 481 | 0× | continue; |
| 482 | 1558× | } |
| 483 | 1558× | match e.value() { |
| 484 | 1557× | KdlValue::String(s) => out.push(s.clone()), |
| 485 | 1× | other => self.err(e.span(), format!("`{}` takes strings, not {}", node.name().value(), kind_of(other))), |
| 486 | | } |
| 487 | | } |
| 488 | 1516× | out |
| 489 | 1516× | } |
| 490 | |
|
| 491 | | |
| 492 | 1289× | fn one_string(&mut self, node: &KdlNode) -> Option<String> { |
| 493 | 1289× | let v = self.strings(node); |
| 494 | 1289× | if v.len() != 1 { |
| 495 | 0× | self.err(node.span(), format!("`{}` takes one string", node.name().value())); |
| 496 | 0× | return None; |
| 497 | 1289× | } |
| 498 | 1289× | v.into_iter().next() |
| 499 | 1289× | } |
| 500 | |
|
| 501 | 241× | fn one_integer(&mut self, node: &KdlNode) -> Option<i128> { |
| 502 | 241× | match node.entries() { |
| 503 | 241× | [e] if e.name().is_none() => match e.value() { |
| 504 | 240× | KdlValue::Integer(n) => Some(*n), |
| 505 | 1× | other => { |
| 506 | 1× | self.err(e.span(), format!("`{}` takes an integer, not {}", node.name().value(), kind_of(other))); |
| 507 | 1× | None |
| 508 | | } |
| 509 | | }, |
| 510 | | _ => { |
| 511 | 0× | self.err(node.span(), format!("`{}` takes one integer", node.name().value())); |
| 512 | 0× | None |
| 513 | | } |
| 514 | | } |
| 515 | 241× | } |
| 516 | |
|
| 517 | 1404× | fn no_children(&mut self, node: &KdlNode) { |
| 518 | 1404× | if node.children().is_some() { |
| 519 | 1× | self.err(node.span(), format!("`{}` takes no block", node.name().value())); |
| 520 | 1403× | } |
| 521 | 1404× | } |
| 522 | |
|
| 523 | 371× | fn name_ok(&mut self, span: SourceSpan, what: &str, name: &str) -> bool { |
| 524 | 1373× | let ok = !name.is_empty() && name.len() <= 64 && name.bytes().all(|b| b.is_ascii_alphanumeric() || b"._-".contains(&b)); |
| 525 | 371× | if !ok { |
| 526 | 0× | self.err(span, format!("{what} name {name:?} must be 1 to 64 of A-Z a-z 0-9 . _ -")); |
| 527 | 371× | } |
| 528 | 371× | ok |
| 529 | 371× | } |
| 530 | | } |
| 531 | |
|
| 532 | | use kdl::{KdlDocument, KdlNode, KdlValue}; |
| 533 | | use miette::SourceSpan; |
| 534 | |
|
| 535 | 2× | fn kind_of(v: &KdlValue) -> &'static str { |
| 536 | 2× | match v { |
| 537 | 1× | KdlValue::String(_) => "a string", |
| 538 | 1× | KdlValue::Integer(_) => "an integer", |
| 539 | 0× | KdlValue::Float(_) => "a number", |
| 540 | 0× | KdlValue::Bool(_) => "a boolean", |
| 541 | 0× | KdlValue::Null => "null", |
| 542 | | } |
| 543 | 2× | } |
| 544 | |
|
| 545 | |
|
| 546 | 171× | fn parse_memory(s: &str) -> Option<u64> { |
| 547 | 570× | let (num, unit) = s.split_at(s.find(|c: char| !c.is_ascii_digit())?); |
| 548 | 171× | let n: u64 = num.parse().ok()?; |
| 549 | 170× | let mib = match unit { |
| 550 | 170× | "KiB" => n.is_multiple_of(1024).then_some(n / 1024)?, |
| 551 | 170× | "MiB" => n, |
| 552 | 45× | "GiB" => n.checked_mul(1024)?, |
| 553 | 1× | _ => return None, |
| 554 | | }; |
| 555 | 169× | Some(mib) |
| 556 | 171× | } |
| 557 | |
|
| 558 | |
|
| 559 | |
|
| 560 | 2× | fn foreign_escape_line(src: &str) -> Option<usize> { |
| 561 | 2× | src.lines() |
| 562 | 17× | .position(|l| { |
| 563 | 17× | let b = l.as_bytes(); |
| 564 | 287× | b.windows(2).enumerate().any(|(i, w)| { |
| 565 | 287× | w[0] == b'\\' |
| 566 | 1× | && (i == 0 || b[i - 1] != b'\\') |
| 567 | 1× | && (w[1] == b'e' || w[1] == b'x' || (w[1] == b'0' && b.get(i + 2).is_some_and(u8::is_ascii_digit))) |
| 568 | 287× | }) |
| 569 | 17× | }) |
| 570 | 2× | .map(|i| i + 1) |
| 571 | 2× | } |
| 572 | |
|
| 573 | | impl Pipeline { |
| 574 | | |
| 575 | 134× | pub fn parse(src: &str) -> Result<Pipeline, Problems> { |
| 576 | 134× | let doc = match KdlDocument::parse_v2(src) { |
| 577 | 132× | Ok(d) => d, |
| 578 | 2× | Err(e) => { |
| 579 | 2× | let mut ctx = Ctx { src, problems: Vec::new() }; |
| 580 | 2× | for d in &e.diagnostics { |
| 581 | 2× | let msg = d.message.clone().or_else(|| d.label.clone()).unwrap_or_else(|| "not valid KDL 2.0".into()); |
| 582 | 2× | ctx.err(d.span, msg); |
| 583 | | } |
| 584 | 2× | if ctx.problems.is_empty() { |
| 585 | 0× | ctx.problems.push(Problem { line: 1, message: "not valid KDL 2.0".into() }); |
| 586 | 2× | } |
| 587 | 2× | if let Some(line) = foreign_escape_line(src) { |
| 588 | 1× | ctx.problems.push(Problem { |
| 589 | 1× | line, |
| 590 | 1× | message: "KDL strings have no \\033, \\e or \\x escapes: write ESC (for ANSI colours) as \\u{1b}".into(), |
| 591 | 1× | }); |
| 592 | 1× | } |
| 593 | 2× | return Err(Problems(ctx.problems)); |
| 594 | | } |
| 595 | | }; |
| 596 | 132× | let mut ctx = Ctx { src, problems: Vec::new() }; |
| 597 | 132× | let mut schema = None; |
| 598 | 132× | let mut sandboxes = BTreeMap::new(); |
| 599 | 132× | let mut jobs: BTreeMap<String, (Job, SourceSpan)> = BTreeMap::new(); |
| 600 | 132× | let mut on = None; |
| 601 | 132× | let mut priority = None; |
| 602 | 442× | for node in doc.nodes() { |
| 603 | 442× | match node.name().value() { |
| 604 | 442× | "priority" => { |
| 605 | 1× | if priority.is_some() { |
| 606 | 0× | ctx.err(node.span(), "`priority` is given twice"); |
| 607 | 1× | } |
| 608 | 1× | priority = parse_priority(&mut ctx, node); |
| 609 | | } |
| 610 | 441× | "on" => { |
| 611 | 4× | if on.is_some() { |
| 612 | 0× | ctx.err(node.span(), "`on` is given twice"); |
| 613 | 4× | } |
| 614 | 4× | on = Some(parse_on(&mut ctx, node)); |
| 615 | | } |
| 616 | 437× | "schema-version" => { |
| 617 | 132× | ctx.no_children(node); |
| 618 | 132× | match ctx.one_integer(node) { |
| 619 | 131× | Some(1) => schema = Some(1), |
| 620 | 1× | Some(n) => ctx.err(node.span(), format!("schema-version {n} is not supported; this runner reads 1")), |
| 621 | 0× | None => {} |
| 622 | | } |
| 623 | | } |
| 624 | 305× | "sandbox" => { |
| 625 | 131× | if let Some(sb) = parse_sandbox(&mut ctx, node) { |
| 626 | 129× | if sandboxes.contains_key(&sb.name) { |
| 627 | 0× | ctx.err(node.span(), format!("sandbox {:?} is declared twice", sb.name)); |
| 628 | 129× | } else { |
| 629 | 129× | sandboxes.insert(sb.name.clone(), sb); |
| 630 | 129× | } |
| 631 | 2× | } |
| 632 | | } |
| 633 | 174× | "job" => { |
| 634 | 173× | if let Some(job) = parse_job(&mut ctx, node) { |
| 635 | 173× | if jobs.contains_key(&job.name) { |
| 636 | 0× | ctx.err(node.span(), format!("job {:?} is declared twice", job.name)); |
| 637 | 173× | } else { |
| 638 | 173× | jobs.insert(job.name.clone(), (job, node.span())); |
| 639 | 173× | } |
| 640 | 0× | } |
| 641 | | } |
| 642 | 1× | n if LATER_TOP.contains(&n) => ctx.err(node.span(), format!("`{n}` is not in M0 yet")), |
| 643 | 1× | n => ctx.err(node.span(), format!("unknown node `{n}`; expected schema-version, on, priority, sandbox or job")), |
| 644 | | } |
| 645 | | } |
| 646 | 132× | if schema.is_none() && !ctx.problems.iter().any(|p| p.message.contains("schema-version")) { |
| 647 | 0× | ctx.problems.push(Problem { line: 1, message: "`schema-version 1` is required".into() }); |
| 648 | 132× | } |
| 649 | 132× | if jobs.is_empty() && ctx.problems.is_empty() { |
| 650 | 0× | ctx.problems.push(Problem { line: 1, message: "a pipeline needs at least one job".into() }); |
| 651 | 132× | } |
| 652 | | |
| 653 | 173× | for (job, span) in jobs.values() { |
| 654 | 173× | if !job.sandbox.is_empty() && !sandboxes.contains_key(&job.sandbox) { |
| 655 | 3× | ctx.err(*span, format!("job {:?} uses sandbox {:?}, which is not declared", job.name, job.sandbox)); |
| 656 | 170× | } |
| 657 | 173× | for d in &job.depends_on { |
| 658 | 17× | if !jobs.contains_key(d) { |
| 659 | 1× | ctx.err(*span, format!("job {:?} depends on {:?}, which is not declared", job.name, d)); |
| 660 | 16× | } else if d == &job.name { |
| 661 | 1× | ctx.err(*span, format!("job {:?} depends on itself", job.name)); |
| 662 | 15× | } |
| 663 | | } |
| 664 | | |
| 665 | | |
| 666 | 173× | for i in &job.inputs { |
| 667 | 6× | let Some((producer, _)) = jobs.get(&i.from) else { |
| 668 | 1× | ctx.err(*span, format!("job {:?} takes an input from {:?}, which is not declared", job.name, i.from)); |
| 669 | 1× | continue; |
| 670 | | }; |
| 671 | 5× | if !job.depends_on.contains(&i.from) { |
| 672 | 1× | ctx.err(*span, format!("job {:?} takes {:?} from {:?}: add depends-on \"{}\"", job.name, i.artifact, i.from, i.from)); |
| 673 | 4× | } |
| 674 | 9× | match producer.outputs.iter().find(|o| o.name == i.artifact) { |
| 675 | 4× | Some(o) if o.kind == OutputKind::Artifact => {} |
| 676 | 1× | Some(_) => ctx.err(*span, format!("{:?} of job {:?} is a report; inputs are artifacts", i.artifact, i.from)), |
| 677 | 1× | None => ctx.err(*span, format!("job {:?} declares no artifact {:?}", i.from, i.artifact)), |
| 678 | | } |
| 679 | | } |
| 680 | | } |
| 681 | 132× | let graph: BTreeMap<&str, Vec<&str>> = |
| 682 | 173× | jobs.values().map(|(j, _)| (j.name.as_str(), j.depends_on.iter().map(String::as_str).collect())).collect(); |
| 683 | 132× | if let Some(cycle) = find_cycle(&graph) { |
| 684 | 1× | let span = jobs[cycle[0]].1; |
| 685 | 1× | ctx.err(span, format!("jobs depend on each other in a cycle: {}", cycle.join(" → "))); |
| 686 | 131× | } |
| 687 | 132× | if !ctx.problems.is_empty() { |
| 688 | 79× | ctx.problems.sort_by_key(|p| p.line); |
| 689 | 79× | return Err(Problems(ctx.problems)); |
| 690 | 53× | } |
| 691 | | |
| 692 | 53× | let jobs = jobs |
| 693 | 53× | .into_iter() |
| 694 | 75× | .map(|(k, (mut j, _))| { |
| 695 | 75× | if j.priority.is_none() { |
| 696 | 73× | j.priority.clone_from(&priority); |
| 697 | 73× | } |
| 698 | 75× | (k, j) |
| 699 | 75× | }) |
| 700 | 53× | .collect(); |
| 701 | 53× | Ok(Pipeline { schema_version: 1, sandboxes, jobs, on }) |
| 702 | 134× | } |
| 703 | |
|
| 704 | | |
| 705 | 4× | pub fn order(&self) -> Vec<&Job> { |
| 706 | 4× | let mut done: BTreeSet<&str> = BTreeSet::new(); |
| 707 | 4× | let mut out = Vec::with_capacity(self.jobs.len()); |
| 708 | 10× | while out.len() < self.jobs.len() { |
| 709 | 6× | let before = out.len(); |
| 710 | 20× | for j in self.jobs.values() { |
| 711 | 20× | if !done.contains(j.name.as_str()) && j.depends_on.iter().all(|d| done.contains(d.as_str())) { |
| 712 | 16× | out.push(j); |
| 713 | 16× | } |
| 714 | | } |
| 715 | 16× | for j in &out[before..] { |
| 716 | 16× | done.insert(j.name.as_str()); |
| 717 | 16× | } |
| 718 | 6× | if out.len() == before { |
| 719 | 0× | break; |
| 720 | 6× | } |
| 721 | | } |
| 722 | 4× | out |
| 723 | 4× | } |
| 724 | |
|
| 725 | | |
| 726 | 1× | pub fn depths(&self) -> BTreeMap<&str, usize> { |
| 727 | 1× | let mut depth: BTreeMap<&str, usize> = BTreeMap::new(); |
| 728 | 2× | for j in self.order() { |
| 729 | 2× | let d = j.depends_on.iter().map(|x| depth.get(x.as_str()).copied().unwrap_or(0) + 1).max().unwrap_or(0); |
| 730 | 2× | depth.insert(j.name.as_str(), d); |
| 731 | | } |
| 732 | 1× | depth |
| 733 | 1× | } |
| 734 | | } |
| 735 | |
|
| 736 | 132× | fn find_cycle<'a>(graph: &BTreeMap<&'a str, Vec<&'a str>>) -> Option<Vec<&'a str>> { |
| 737 | | #[derive(Clone, Copy, PartialEq)] |
| 738 | | enum Mark { |
| 739 | | Visiting, |
| 740 | | Done, |
| 741 | | } |
| 742 | 185× | fn visit<'a>( |
| 743 | 185× | n: &'a str, |
| 744 | 185× | graph: &BTreeMap<&'a str, Vec<&'a str>>, |
| 745 | 185× | marks: &mut BTreeMap<&'a str, Mark>, |
| 746 | 185× | stack: &mut Vec<&'a str>, |
| 747 | 185× | ) -> Option<Vec<&'a str>> { |
| 748 | 185× | match marks.get(n) { |
| 749 | 12× | Some(Mark::Done) => return None, |
| 750 | | Some(Mark::Visiting) => { |
| 751 | 1× | let at = stack.iter().position(|s| *s == n).unwrap_or(0); |
| 752 | 1× | let mut c = stack[at..].to_vec(); |
| 753 | 1× | c.push(n); |
| 754 | 1× | return Some(c); |
| 755 | | } |
| 756 | 172× | None => {} |
| 757 | | } |
| 758 | 172× | marks.insert(n, Mark::Visiting); |
| 759 | 172× | stack.push(n); |
| 760 | 172× | for d in graph.get(n).into_iter().flatten() { |
| 761 | 16× | if graph.contains_key(d) && *d != n { |
| 762 | 15× | if let Some(c) = visit(d, graph, marks, stack) { |
| 763 | 3× | return Some(c); |
| 764 | 12× | } |
| 765 | 1× | } |
| 766 | | } |
| 767 | 169× | stack.pop(); |
| 768 | 169× | marks.insert(n, Mark::Done); |
| 769 | 169× | None |
| 770 | 185× | } |
| 771 | 132× | let mut marks = BTreeMap::new(); |
| 772 | 170× | for n in graph.keys() { |
| 773 | 170× | let mut stack = Vec::new(); |
| 774 | 170× | if let Some(c) = visit(n, graph, &mut marks, &mut stack) { |
| 775 | 1× | return Some(c); |
| 776 | 169× | } |
| 777 | | } |
| 778 | 131× | None |
| 779 | 132× | } |
| 780 | |
|
| 781 | 483× | fn block<'a>(ctx: &mut Ctx, node: &'a KdlNode) -> &'a [KdlNode] { |
| 782 | 483× | match node.children() { |
| 783 | 482× | Some(d) => d.nodes(), |
| 784 | | None => { |
| 785 | 1× | ctx.err(node.span(), format!("`{}` needs a block {{ … }}", node.name().value())); |
| 786 | 1× | &[] |
| 787 | | } |
| 788 | | } |
| 789 | 483× | } |
| 790 | |
|
| 791 | 131× | fn parse_sandbox(ctx: &mut Ctx, node: &KdlNode) -> Option<Sandbox> { |
| 792 | 131× | let name = ctx.one_string(node)?; |
| 793 | 131× | let named = ctx.name_ok(node.span(), "sandbox", &name); |
| 794 | 131× | let (mut runtime, mut rootfs, mut memory, mut cpus, mut network) = |
| 795 | 131× | (Some(Runtime::Microvm), None, Some(1024u32), Some(1u32), Some(Network::Isolated)); |
| 796 | 131× | let mut seen = BTreeSet::new(); |
| 797 | 131× | let mut env = BTreeMap::new(); |
| 798 | 131× | let mut runs_on = Vec::new(); |
| 799 | 131× | let mut timeout_s = None; |
| 800 | 131× | let mut arch: Option<String> = None; |
| 801 | 131× | let mut egress_allow = Vec::new(); |
| 802 | 593× | for c in block(ctx, node) { |
| 803 | 593× | let key = c.name().value(); |
| 804 | 593× | if key != "runs-on" && !seen.insert(key.to_string()) { |
| 805 | 0× | ctx.err(c.span(), format!("`{key}` is given twice")); |
| 806 | 593× | } |
| 807 | 593× | if key == "env" { |
| 808 | 2× | parse_env(ctx, c, &mut env); |
| 809 | 2× | continue; |
| 810 | 591× | } |
| 811 | 591× | if key == "runs-on" { |
| 812 | 1× | ctx.no_children(c); |
| 813 | 1× | parse_labels(ctx, c, &mut runs_on); |
| 814 | 1× | continue; |
| 815 | 590× | } |
| 816 | 590× | if key != "network" { |
| 817 | 468× | ctx.no_children(c); |
| 818 | 468× | } |
| 819 | 590× | match key { |
| 820 | 590× | "runtime" => { |
| 821 | 114× | runtime = match ctx.one_string(c).as_deref() { |
| 822 | 114× | Some("microvm") => Some(Runtime::Microvm), |
| 823 | 6× | Some("syscall") | Some("gvisor") => Some(Runtime::Syscall), |
| 824 | | |
| 825 | 5× | Some("wasm") => Some(Runtime::Wasm), |
| 826 | 0× | Some(other) => { |
| 827 | 0× | ctx.err(c.span(), format!("runtime {other:?}: expected \"microvm\", \"syscall\" or \"wasm\"")); |
| 828 | 0× | None |
| 829 | | } |
| 830 | 0× | None => None, |
| 831 | | } |
| 832 | | } |
| 833 | 476× | "rootfs" => { |
| 834 | 127× | rootfs = match ctx.one_string(c) { |
| 835 | 127× | Some(r) => match r.strip_prefix("cas://blob/") { |
| 836 | 8064× | Some(h) if h.len() == 64 && h.bytes().all(|b| b.is_ascii_hexdigit() && !b.is_ascii_uppercase()) => { |
| 837 | 126× | Some(h.to_string()) |
| 838 | | } |
| 839 | | _ => { |
| 840 | 1× | ctx.err(c.span(), format!("rootfs {r:?}: expected \"cas://blob/\" and 64 lower-case hex digits")); |
| 841 | 1× | None |
| 842 | | } |
| 843 | | }, |
| 844 | 0× | None => None, |
| 845 | | } |
| 846 | | } |
| 847 | 349× | "arch" => { |
| 848 | 2× | arch = match ctx.one_string(c) { |
| 849 | 2× | Some(a) if arch_ok(&a) => Some(a), |
| 850 | 1× | Some(a) => { |
| 851 | 1× | ctx.err(c.span(), format!("arch {a:?} is not an architecture name such as x86_64, aarch64 or riscv64")); |
| 852 | 1× | None |
| 853 | | } |
| 854 | 0× | None => None, |
| 855 | | } |
| 856 | | } |
| 857 | 347× | "memory-limit" => { |
| 858 | 113× | memory = match ctx.one_string(c) { |
| 859 | 113× | Some(m) => match parse_memory(&m) { |
| 860 | 112× | Some(mib) if (MIN_MEMORY_MIB..=MAX_MEMORY_MIB).contains(&mib) => Some(mib as u32), |
| 861 | | Some(_) => { |
| 862 | 0× | ctx.err(c.span(), format!("memory-limit {m:?} must be from 64MiB to 16GiB")); |
| 863 | 0× | None |
| 864 | | } |
| 865 | | None => { |
| 866 | 1× | ctx.err(c.span(), format!("memory-limit {m:?}: expected a whole number of KiB, MiB or GiB, as \"4GiB\"")); |
| 867 | 1× | None |
| 868 | | } |
| 869 | | }, |
| 870 | 0× | None => None, |
| 871 | | } |
| 872 | | } |
| 873 | 234× | "cpu-limit" => { |
| 874 | 109× | cpus = match ctx.one_integer(c) { |
| 875 | 108× | Some(n) if (1..=MAX_CPUS).contains(&n) => Some(n as u32), |
| 876 | 0× | Some(n) => { |
| 877 | 0× | ctx.err(c.span(), format!("cpu-limit {n} must be from 1 to {MAX_CPUS}")); |
| 878 | 0× | None |
| 879 | | } |
| 880 | 1× | None => None, |
| 881 | | } |
| 882 | | } |
| 883 | 125× | "network" => { |
| 884 | 122× | let kind = ctx.one_string(c); |
| 885 | 122× | if kind.as_deref() != Some("egress") { |
| 886 | 113× | ctx.no_children(c); |
| 887 | 113× | } |
| 888 | 122× | network = match kind.as_deref() { |
| 889 | 122× | Some("isolated") => Some(Network::Isolated), |
| 890 | | |
| 891 | | |
| 892 | 13× | Some("loopback") => Some(Network::Loopback), |
| 893 | | |
| 894 | | |
| 895 | | |
| 896 | 10× | Some("egress") => { |
| 897 | 9× | egress_allow = parse_egress(ctx, c); |
| 898 | 9× | Some(Network::Egress) |
| 899 | | } |
| 900 | 1× | Some(other) => { |
| 901 | 1× | ctx.err(c.span(), format!("network {other:?}: expected \"isolated\", \"loopback\" or \"egress\"")); |
| 902 | 1× | None |
| 903 | | } |
| 904 | 0× | None => None, |
| 905 | | } |
| 906 | | } |
| 907 | 3× | "timeout" => timeout_s = parse_timeout(ctx, c), |
| 908 | 0× | other => ctx.err(c.span(), format!("unknown sandbox setting `{other}`")), |
| 909 | | } |
| 910 | | } |
| 911 | | |
| 912 | | |
| 913 | 131× | let wasm = runtime == Some(Runtime::Wasm); |
| 914 | 131× | if wasm { |
| 915 | 5× | if seen.contains("rootfs") { |
| 916 | 1× | ctx.err(node.span(), format!("sandbox {name:?}: runtime \"wasm\" takes no rootfs; its module is a file the job has")); |
| 917 | 4× | } |
| 918 | 5× | if network.is_some_and(|n| n != Network::Isolated) { |
| 919 | 1× | ctx.err(node.span(), format!("sandbox {name:?}: runtime \"wasm\" has no network; leave out `network` or say \"isolated\"")); |
| 920 | 4× | } |
| 921 | 5× | rootfs = Some(String::new()); |
| 922 | 5× | network = Some(Network::Isolated); |
| 923 | 126× | } else if rootfs.is_none() && !seen.contains("rootfs") { |
| 924 | 0× | ctx.err(node.span(), format!("sandbox {name:?} needs a rootfs")); |
| 925 | 126× | } |
| 926 | 131× | if network == Some(Network::Egress) { |
| 927 | 9× | if runtime == Some(Runtime::Syscall) { |
| 928 | 1× | ctx.err(node.span(), format!("sandbox {name:?}: network \"egress\" needs runtime \"microvm\"")); |
| 929 | 8× | } |
| 930 | | |
| 931 | 9× | if !runs_on.iter().any(|l| l == EGRESS_LABEL) { |
| 932 | 9× | runs_on.push(EGRESS_LABEL.to_string()); |
| 933 | 9× | runs_on.sort(); |
| 934 | 9× | } |
| 935 | 122× | } |
| 936 | 131× | if !named { |
| 937 | 0× | return None; |
| 938 | 131× | } |
| 939 | | Some(Sandbox { |
| 940 | 131× | name, |
| 941 | 131× | runtime: runtime?, |
| 942 | 131× | rootfs: rootfs?, |
| 943 | 130× | memory_mib: memory?, |
| 944 | 129× | cpus: cpus?, |
| 945 | 129× | network: network?, |
| 946 | 129× | env, |
| 947 | 129× | runs_on, |
| 948 | 129× | egress_allow, |
| 949 | 129× | timeout_s, |
| 950 | 129× | arch, |
| 951 | | }) |
| 952 | 131× | } |
| 953 | |
|
| 954 | |
|
| 955 | |
|
| 956 | | pub const EGRESS_LABEL: &str = "egress"; |
| 957 | |
|
| 958 | |
|
| 959 | | const MAX_EGRESS_HOSTS: usize = 32; |
| 960 | |
|
| 961 | |
|
| 962 | |
|
| 963 | 8× | pub fn egress_pattern_ok(pattern: &str) -> bool { |
| 964 | 8× | let name = pattern.strip_prefix("*.").unwrap_or(pattern); |
| 965 | 8× | let labels: Vec<&str> = name.split('.').collect(); |
| 966 | 8× | name.len() <= 253 |
| 967 | 8× | && labels.len() >= 2 |
| 968 | 15× | && labels.iter().all(|l| { |
| 969 | 15× | !l.is_empty() |
| 970 | 15× | && l.len() <= 63 |
| 971 | 15× | && !l.starts_with('-') |
| 972 | 15× | && !l.ends_with('-') |
| 973 | 86× | && l.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-') |
| 974 | 15× | }) |
| 975 | 8× | } |
| 976 | |
|
| 977 | |
|
| 978 | |
|
| 979 | 14× | pub fn egress_allows(patterns: &[String], host: &str) -> bool { |
| 980 | 14× | let host = host.trim_end_matches('.').to_ascii_lowercase(); |
| 981 | 25× | patterns.iter().any(|p| match p.strip_prefix("*.") { |
| 982 | 14× | Some(domain) => host.len() > domain.len() + 1 && host.ends_with(domain) && host[..host.len() - domain.len()].ends_with('.'), |
| 983 | 11× | None => host == *p, |
| 984 | 25× | }) |
| 985 | 14× | } |
| 986 | |
|
| 987 | |
|
| 988 | 9× | fn parse_egress(ctx: &mut Ctx, node: &KdlNode) -> Vec<String> { |
| 989 | 9× | let mut out = Vec::new(); |
| 990 | 9× | for c in block(ctx, node) { |
| 991 | 8× | ctx.no_children(c); |
| 992 | 8× | if c.name().value() != "allow" { |
| 993 | 1× | ctx.err(c.span(), format!("unknown egress setting `{}`; egress takes `allow \"host\"...`", c.name().value())); |
| 994 | 1× | continue; |
| 995 | 7× | } |
| 996 | 8× | for h in ctx.strings(c) { |
| 997 | 8× | if egress_pattern_ok(&h) { |
| 998 | 5× | out.push(h); |
| 999 | 5× | } else { |
| 1000 | 3× | ctx.err( |
| 1001 | 3× | c.span(), |
| 1002 | 3× | format!("allow {h:?}: expected a lower-case host name such as \"registry.opentofu.org\", or \"*.\" and a domain"), |
| 1003 | 3× | ); |
| 1004 | 3× | } |
| 1005 | | } |
| 1006 | | } |
| 1007 | 9× | out.sort(); |
| 1008 | 9× | out.dedup(); |
| 1009 | 9× | if out.is_empty() { |
| 1010 | 6× | ctx.err(node.span(), "network \"egress\" needs at least one `allow \"host\"`"); |
| 1011 | 6× | } else if out.len() > MAX_EGRESS_HOSTS { |
| 1012 | 0× | ctx.err(node.span(), format!("network \"egress\" may allow at most {MAX_EGRESS_HOSTS} hosts")); |
| 1013 | 3× | } |
| 1014 | 9× | out |
| 1015 | 9× | } |
| 1016 | |
|
| 1017 | |
|
| 1018 | |
|
| 1019 | 14× | fn parse_on(ctx: &mut Ctx, node: &KdlNode) -> On { |
| 1020 | 14× | let mut on = On::default(); |
| 1021 | 14× | if !node.entries().is_empty() { |
| 1022 | 0× | ctx.err(node.span(), "`on` takes a block: on { push \"main\"; tag \"v*\"; manual }"); |
| 1023 | 14× | } |
| 1024 | 14× | let globs = |ctx: &mut Ctx, c: &KdlNode| -> Vec<String> { |
| 1025 | 6× | let g = ctx.strings(c); |
| 1026 | 7× | for p in &g { |
| 1027 | 7× | if let Err(e) = globset::Glob::new(p) { |
| 1028 | 0× | ctx.err(c.span(), format!("{p:?}: {e}")); |
| 1029 | 7× | } |
| 1030 | | } |
| 1031 | 6× | g |
| 1032 | 6× | }; |
| 1033 | 19× | for c in block(ctx, node) { |
| 1034 | 19× | ctx.no_children(c); |
| 1035 | 19× | match c.name().value() { |
| 1036 | 19× | "push" => on.push.get_or_insert_with(Vec::new).extend(globs(ctx, c)), |
| 1037 | 18× | "tag" => on.tag.get_or_insert_with(Vec::new).extend(globs(ctx, c)), |
| 1038 | 13× | "manual" => on.manual = true, |
| 1039 | 7× | "schedule" => { |
| 1040 | 6× | for line in ctx.strings(c) { |
| 1041 | 6× | if crate::cron::Cron::parse(&line).is_some() { |
| 1042 | 5× | on.schedule.push(line); |
| 1043 | 5× | } else { |
| 1044 | 1× | ctx.err( |
| 1045 | 1× | c.span(), |
| 1046 | 1× | format!("schedule {line:?}: expected five cron fields (minute hour day month weekday), as \"0 3 * * *\""), |
| 1047 | 1× | ); |
| 1048 | 1× | } |
| 1049 | | } |
| 1050 | | } |
| 1051 | 1× | other => ctx.err(c.span(), format!("unknown `on` event `{other}`; expected push, tag, manual or schedule")), |
| 1052 | | } |
| 1053 | | } |
| 1054 | 14× | if on.push.is_none() && on.tag.is_none() && !on.manual && on.schedule.is_empty() { |
| 1055 | 3× | ctx.err(node.span(), "`on` names no event, so nothing would run it"); |
| 1056 | 11× | } |
| 1057 | 14× | on |
| 1058 | 14× | } |
| 1059 | |
|
| 1060 | |
|
| 1061 | | pub const MAX_TIMEOUT_S: u64 = 24 * 3600; |
| 1062 | |
|
| 1063 | |
|
| 1064 | |
|
| 1065 | 19× | pub fn parse_duration(s: &str) -> Option<u64> { |
| 1066 | 19× | let (mut total, mut num, mut last) = (0u64, String::new(), 0u8); |
| 1067 | 50× | for ch in s.chars() { |
| 1068 | 50× | if ch.is_ascii_digit() { |
| 1069 | 29× | num.push(ch); |
| 1070 | 29× | continue; |
| 1071 | 21× | } |
| 1072 | 21× | let (unit, mult) = match ch { |
| 1073 | 12× | 'h' => (1, 3600), |
| 1074 | 5× | 'm' => (2, 60), |
| 1075 | 1× | 's' => (3, 1), |
| 1076 | 3× | _ => return None, |
| 1077 | | }; |
| 1078 | | |
| 1079 | 18× | if num.is_empty() || unit <= last { |
| 1080 | 3× | return None; |
| 1081 | 15× | } |
| 1082 | 15× | last = unit; |
| 1083 | 15× | total = total.checked_add(num.parse::<u64>().ok()?.checked_mul(mult)?)?; |
| 1084 | 15× | num.clear(); |
| 1085 | | } |
| 1086 | 13× | (num.is_empty() && total > 0).then_some(total) |
| 1087 | 19× | } |
| 1088 | |
|
| 1089 | |
|
| 1090 | |
|
| 1091 | |
|
| 1092 | 6× | fn parse_priority(ctx: &mut Ctx, c: &KdlNode) -> Option<String> { |
| 1093 | 6× | ctx.no_children(c); |
| 1094 | 6× | let p = ctx.one_string(c)?; |
| 1095 | 6× | match p.as_str() { |
| 1096 | 6× | "low" | "normal" => Some(p), |
| 1097 | 3× | "high" | "urgent" => { |
| 1098 | 2× | ctx.err( |
| 1099 | 2× | c.span(), |
| 1100 | 2× | format!("priority {p:?}: a pipeline may lower its jobs (`low`, `normal`), not raise them; higher levels are set on the hub, and a person may expedite a run"), |
| 1101 | | ); |
| 1102 | 2× | None |
| 1103 | | } |
| 1104 | | _ => { |
| 1105 | 1× | ctx.err(c.span(), format!("priority {p:?}: expected `low` or `normal`")); |
| 1106 | 1× | None |
| 1107 | | } |
| 1108 | | } |
| 1109 | 6× | } |
| 1110 | |
|
| 1111 | 6× | fn parse_timeout(ctx: &mut Ctx, c: &KdlNode) -> Option<u64> { |
| 1112 | 6× | let t = ctx.one_string(c)?; |
| 1113 | 6× | match parse_duration(&t) { |
| 1114 | 5× | Some(secs) if (60..=MAX_TIMEOUT_S).contains(&secs) => Some(secs), |
| 1115 | | Some(_) => { |
| 1116 | 1× | ctx.err(c.span(), format!("timeout {t:?} must be from 1m to 24h")); |
| 1117 | 1× | None |
| 1118 | | } |
| 1119 | | None => { |
| 1120 | 1× | ctx.err(c.span(), format!("timeout {t:?}: expected hours, minutes and seconds, as \"90m\" or \"1h30m\"")); |
| 1121 | 1× | None |
| 1122 | | } |
| 1123 | | } |
| 1124 | 6× | } |
| 1125 | |
|
| 1126 | |
|
| 1127 | |
|
| 1128 | 4× | fn parse_labels(ctx: &mut Ctx, c: &KdlNode, into: &mut Vec<String>) { |
| 1129 | 4× | let labels = ctx.strings(c); |
| 1130 | 4× | if labels.is_empty() { |
| 1131 | 1× | ctx.err(c.span(), "`runs-on` needs one or more labels, as runs-on \"riscv64\""); |
| 1132 | 3× | } |
| 1133 | 4× | for l in labels { |
| 1134 | 4× | let ok = !l.is_empty() |
| 1135 | 4× | && l.len() <= 64 |
| 1136 | 19× | && l.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || matches!(b, b'-' | b'_' | b'.')); |
| 1137 | 4× | if ok { |
| 1138 | 3× | into.push(l); |
| 1139 | 3× | } else { |
| 1140 | 1× | ctx.err(c.span(), format!("runs-on {l:?}: a label is lower-case letters, digits, -, _ and ., at most 64")); |
| 1141 | 1× | } |
| 1142 | | } |
| 1143 | 4× | into.sort(); |
| 1144 | 4× | into.dedup(); |
| 1145 | 4× | } |
| 1146 | |
|
| 1147 | |
|
| 1148 | 7× | fn parse_env(ctx: &mut Ctx, c: &KdlNode, env: &mut BTreeMap<String, String>) { |
| 1149 | 7× | if !c.entries().is_empty() { |
| 1150 | 0× | ctx.err(c.span(), "`env` takes a block of NAME \"value\" lines"); |
| 1151 | 7× | } |
| 1152 | 8× | for e in block(ctx, c) { |
| 1153 | 8× | let k = e.name().value().to_string(); |
| 1154 | 8× | let valid = |
| 1155 | 49× | !k.is_empty() && !k.starts_with(|c: char| c.is_ascii_digit()) && k.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_'); |
| 1156 | 8× | if !valid { |
| 1157 | 1× | ctx.err(e.span(), format!("environment name {k:?} must be letters, digits and _")); |
| 1158 | 1× | continue; |
| 1159 | 7× | } |
| 1160 | 7× | ctx.no_children(e); |
| 1161 | 7× | if let Some(v) = ctx.one_string(e) { |
| 1162 | 7× | if env.insert(k.clone(), v).is_some() { |
| 1163 | 0× | ctx.err(e.span(), format!("environment {k} is set twice")); |
| 1164 | 7× | } |
| 1165 | 0× | } |
| 1166 | | } |
| 1167 | 7× | } |
| 1168 | |
|
| 1169 | |
|
| 1170 | 60× | fn parse_mount(ctx: &mut Ctx, node: &KdlNode) -> Option<Mount> { |
| 1171 | 60× | let (mut source, mut target, mut size) = (None, None, None); |
| 1172 | 60× | let (mut cache, mut keys) = (None, Vec::new()); |
| 1173 | 60× | let mut seen = BTreeSet::new(); |
| 1174 | 187× | for c in block(ctx, node) { |
| 1175 | 187× | let key = c.name().value(); |
| 1176 | 187× | if !seen.insert(key.to_string()) { |
| 1177 | 1× | ctx.err(c.span(), format!("`{key}` is given twice")); |
| 1178 | 1× | continue; |
| 1179 | 186× | } |
| 1180 | 186× | ctx.no_children(c); |
| 1181 | 186× | match key { |
| 1182 | 186× | "source" => { |
| 1183 | 60× | source = match ctx.one_string(c).as_deref() { |
| 1184 | 60× | Some("ephemeral://tmpfs") => Some(MountSource::Tmpfs), |
| 1185 | 39× | Some("ephemeral://disk") => Some(MountSource::Disk), |
| 1186 | 2× | Some(other) => { |
| 1187 | 2× | ctx.err( |
| 1188 | 2× | c.span(), |
| 1189 | 2× | format!("mount source {other:?} is not in M0 yet; M0 mounts \"ephemeral://tmpfs\" or \"ephemeral://disk\""), |
| 1190 | | ); |
| 1191 | 2× | None |
| 1192 | | } |
| 1193 | 0× | None => None, |
| 1194 | | } |
| 1195 | | } |
| 1196 | 126× | "target" => { |
| 1197 | 59× | target = match ctx.one_string(c) { |
| 1198 | 59× | Some(t) if mount_target_ok(&t) => Some(t), |
| 1199 | 4× | Some(t) => { |
| 1200 | 4× | ctx.err( |
| 1201 | 4× | c.span(), |
| 1202 | 4× | format!("mount target {t:?} must be an absolute path under /src or /tmp, without . or .. or a trailing /"), |
| 1203 | | ); |
| 1204 | 4× | None |
| 1205 | | } |
| 1206 | 0× | None => None, |
| 1207 | | } |
| 1208 | | } |
| 1209 | 67× | "size" => { |
| 1210 | 58× | size = match ctx.one_string(c) { |
| 1211 | 58× | Some(m) => match parse_memory(&m) { |
| 1212 | 57× | Some(mib) if (MIN_MOUNT_MIB..=MAX_MOUNT_MIB).contains(&mib) => Some(mib as u32), |
| 1213 | | Some(_) => { |
| 1214 | 2× | ctx.err(c.span(), format!("mount size {m:?} must be from 16MiB to 64GiB")); |
| 1215 | 2× | None |
| 1216 | | } |
| 1217 | | None => { |
| 1218 | 1× | ctx.err(c.span(), format!("mount size {m:?}: expected a whole number of KiB, MiB or GiB, as \"8GiB\"")); |
| 1219 | 1× | None |
| 1220 | | } |
| 1221 | | }, |
| 1222 | 0× | None => None, |
| 1223 | | } |
| 1224 | | } |
| 1225 | 9× | "cache" => { |
| 1226 | 5× | cache = match ctx.one_string(c) { |
| 1227 | 17× | Some(n) if !n.is_empty() && n.len() <= 64 && n.bytes().all(|b| b.is_ascii_alphanumeric() || b"._-".contains(&b)) => { |
| 1228 | 4× | Some(n) |
| 1229 | | } |
| 1230 | 1× | Some(n) => { |
| 1231 | 1× | ctx.err(c.span(), format!("cache name {n:?}: letters, digits, '.', '_' and '-', at most 64")); |
| 1232 | 1× | None |
| 1233 | | } |
| 1234 | 0× | None => None, |
| 1235 | | } |
| 1236 | | } |
| 1237 | 4× | "key" => { |
| 1238 | 3× | keys = ctx.strings(c); |
| 1239 | 3× | if keys.is_empty() { |
| 1240 | 0× | ctx.err(c.span(), "`key` names one or more files, as \"Cargo.lock\""); |
| 1241 | 3× | } |
| 1242 | 4× | for k in &keys { |
| 1243 | 4× | if k.starts_with('/') || k.split('/').any(|p| p.is_empty() || p == "." || p == "..") { |
| 1244 | 1× | ctx.err(c.span(), format!("key file {k:?} must be a path in the repository, without . or ..")); |
| 1245 | 3× | } |
| 1246 | | } |
| 1247 | | } |
| 1248 | 1× | "readonly" | "include" => ctx.err(c.span(), format!("mount `{key}` is not in M0 yet")), |
| 1249 | 0× | other => ctx.err(c.span(), format!("unknown mount setting `{other}`")), |
| 1250 | | } |
| 1251 | | } |
| 1252 | 180× | for (name, missing) in [("source", source.is_none()), ("target", target.is_none()), ("size", size.is_none())] { |
| 1253 | 180× | if missing && !seen.contains(name) { |
| 1254 | 3× | ctx.err(node.span(), format!("a mount needs a {name}")); |
| 1255 | 177× | } |
| 1256 | | } |
| 1257 | 60× | if source == Some(MountSource::Tmpfs) && cache.is_some() { |
| 1258 | 1× | ctx.err(node.span(), "only an ephemeral://disk mount can be a cache"); |
| 1259 | 59× | } |
| 1260 | 60× | if !keys.is_empty() && cache.is_none() { |
| 1261 | 1× | ctx.err(node.span(), "`key` goes with `cache`"); |
| 1262 | 59× | } |
| 1263 | 60× | Some(Mount { source: source?, target: target?, size_mib: size?, cache, key: keys }) |
| 1264 | 60× | } |
| 1265 | |
|
| 1266 | |
|
| 1267 | |
|
| 1268 | |
|
| 1269 | |
|
| 1270 | 12× | fn parse_secret(ctx: &mut Ctx, node: &KdlNode, job: &mut Job) { |
| 1271 | 12× | let Some(name) = ctx.one_string(node) else { |
| 1272 | 0× | ctx.err(node.span(), "`secret` takes a name and a block with mount-path"); |
| 1273 | 0× | return; |
| 1274 | | }; |
| 1275 | 12× | if !ctx.name_ok(node.span(), "secret", &name) { |
| 1276 | 0× | return; |
| 1277 | 12× | } |
| 1278 | 12× | let mut path = None; |
| 1279 | 12× | for c in block(ctx, node) { |
| 1280 | 12× | ctx.no_children(c); |
| 1281 | 12× | match c.name().value() { |
| 1282 | 12× | "mount-path" => match ctx.one_string(c) { |
| 1283 | 11× | Some(p) if secret_path_ok(&p) => path = Some(p), |
| 1284 | 2× | Some(p) => { |
| 1285 | 2× | ctx.err(c.span(), format!("secret mount-path {p:?} must be an absolute path under /run/secrets/, without . or ..")) |
| 1286 | | } |
| 1287 | 0× | None => {} |
| 1288 | | }, |
| 1289 | 1× | "vault-ref" => ctx.err(c.span(), "`vault-ref` is not in M0; the hub supplies the secret of that name"), |
| 1290 | 0× | other => ctx.err(c.span(), format!("unknown secret setting `{other}`")), |
| 1291 | | } |
| 1292 | | } |
| 1293 | 12× | let Some(mount_path) = path else { |
| 1294 | 3× | ctx.err(node.span(), format!("secret {name:?} needs a mount-path")); |
| 1295 | 3× | return; |
| 1296 | | }; |
| 1297 | 9× | if job.secrets.iter().any(|s| s.name == name) { |
| 1298 | 1× | ctx.err(node.span(), format!("secret {name:?} is given twice")); |
| 1299 | 8× | } else if job.secrets.iter().any(|s| s.mount_path == mount_path) || job.identities.iter().any(|i| i.mount_path == mount_path) { |
| 1300 | 2× | ctx.err(node.span(), format!("two files mount at {mount_path:?}")); |
| 1301 | 6× | } else if job.secrets.len() == MAX_SECRETS { |
| 1302 | 0× | ctx.err(node.span(), format!("a job has at most {MAX_SECRETS} secrets")); |
| 1303 | 6× | } else { |
| 1304 | 6× | job.secrets.push(Secret { name, mount_path }); |
| 1305 | 6× | } |
| 1306 | 12× | } |
| 1307 | |
|
| 1308 | |
|
| 1309 | |
|
| 1310 | |
|
| 1311 | 8× | fn parse_identity(ctx: &mut Ctx, node: &KdlNode, job: &mut Job) { |
| 1312 | 8× | let Some(audience) = ctx.one_string(node) else { |
| 1313 | 0× | ctx.err(node.span(), "`identity` takes an audience and a block with mount-path"); |
| 1314 | 0× | return; |
| 1315 | | }; |
| 1316 | 45× | let aud_ok = !audience.is_empty() && audience.len() <= 255 && !audience.chars().any(|c| c.is_whitespace() || c.is_control()); |
| 1317 | 8× | if !aud_ok { |
| 1318 | 1× | ctx.err(node.span(), format!("identity audience {audience:?} must be 1 to 255 characters without spaces")); |
| 1319 | 1× | return; |
| 1320 | 7× | } |
| 1321 | 7× | let mut path = None; |
| 1322 | 7× | for c in block(ctx, node) { |
| 1323 | 6× | ctx.no_children(c); |
| 1324 | 6× | match c.name().value() { |
| 1325 | 6× | "mount-path" => match ctx.one_string(c) { |
| 1326 | 6× | Some(p) if secret_path_ok(&p) => path = Some(p), |
| 1327 | 1× | Some(p) => { |
| 1328 | 1× | ctx.err(c.span(), format!("identity mount-path {p:?} must be an absolute path under /run/secrets/, without . or ..")) |
| 1329 | | } |
| 1330 | 0× | None => {} |
| 1331 | | }, |
| 1332 | 0× | other => ctx.err(c.span(), format!("unknown identity setting `{other}`")), |
| 1333 | | } |
| 1334 | | } |
| 1335 | 7× | let Some(mount_path) = path else { |
| 1336 | 2× | ctx.err(node.span(), format!("identity {audience:?} needs a mount-path")); |
| 1337 | 2× | return; |
| 1338 | | }; |
| 1339 | 5× | if job.identities.iter().any(|i| i.audience == audience) { |
| 1340 | 1× | ctx.err(node.span(), format!("identity {audience:?} is given twice")); |
| 1341 | 4× | } else if job.identities.iter().any(|i| i.mount_path == mount_path) || job.secrets.iter().any(|s| s.mount_path == mount_path) { |
| 1342 | 0× | ctx.err(node.span(), format!("two files mount at {mount_path:?}")); |
| 1343 | 4× | } else if job.identities.len() == MAX_IDENTITIES { |
| 1344 | 0× | ctx.err(node.span(), format!("a job has at most {MAX_IDENTITIES} identities")); |
| 1345 | 4× | } else { |
| 1346 | 4× | job.identities.push(Identity { audience, mount_path }); |
| 1347 | 4× | } |
| 1348 | 8× | } |
| 1349 | |
|
| 1350 | 23× | fn parse_output(ctx: &mut Ctx, node: &KdlNode, job: &mut Job) { |
| 1351 | 23× | if !node.entries().is_empty() { |
| 1352 | 0× | ctx.err(node.span(), "`output` takes a block of artifacts and reports"); |
| 1353 | 23× | } |
| 1354 | 37× | for a in block(ctx, node) { |
| 1355 | 37× | let kind = match a.name().value() { |
| 1356 | 37× | "artifact" => OutputKind::Artifact, |
| 1357 | 13× | "report" => OutputKind::Report, |
| 1358 | 0× | other => { |
| 1359 | 0× | ctx.err( |
| 1360 | 0× | a.span(), |
| 1361 | 0× | format!("unknown output `{other}`; outputs are `artifact \"name\" {{ path \"/src/file\" }}` and `report \"name\" {{ path \"/src/dir\" }}`"), |
| 1362 | | ); |
| 1363 | 0× | continue; |
| 1364 | | } |
| 1365 | | }; |
| 1366 | 37× | let word = kind.word(); |
| 1367 | 37× | let Some(name) = ctx.one_string(a) else { continue }; |
| 1368 | 37× | if !ctx.name_ok(a.span(), word, &name) { |
| 1369 | 0× | continue; |
| 1370 | 37× | } |
| 1371 | 37× | let mut path = None; |
| 1372 | 37× | let mut always = false; |
| 1373 | 42× | for c in block(ctx, a) { |
| 1374 | 42× | ctx.no_children(c); |
| 1375 | 42× | match c.name().value() { |
| 1376 | 42× | "path" => match ctx.one_string(c) { |
| 1377 | 35× | Some(p) if output_path_ok(&p) => path = Some(p), |
| 1378 | 2× | Some(p) => ctx |
| 1379 | 2× | .err(c.span(), format!("{word} path {p:?} must be an absolute path under /src, without . or .., quotes or spaces")), |
| 1380 | 0× | None => {} |
| 1381 | | }, |
| 1382 | 7× | "format" if kind == OutputKind::Artifact => { |
| 1383 | 2× | if ctx.one_string(c).as_deref() != Some("blob") { |
| 1384 | 1× | ctx.err(c.span(), "artifact format is \"blob\""); |
| 1385 | 1× | } |
| 1386 | | } |
| 1387 | 5× | "when" => match ctx.one_string(c).as_deref() { |
| 1388 | 5× | Some("passed") => always = false, |
| 1389 | 4× | Some("always") => always = true, |
| 1390 | 1× | Some(_) => ctx.err(c.span(), format!("{word} `when` is \"passed\" (the default) or \"always\"")), |
| 1391 | 0× | None => {} |
| 1392 | | }, |
| 1393 | 0× | other => ctx.err(c.span(), format!("unknown {word} setting `{other}`")), |
| 1394 | | } |
| 1395 | | } |
| 1396 | 37× | let Some(path) = path else { |
| 1397 | 4× | ctx.err(a.span(), format!("{word} {name:?} needs a path")); |
| 1398 | 4× | continue; |
| 1399 | | }; |
| 1400 | 33× | if job.outputs.iter().any(|o| o.name == name) { |
| 1401 | 2× | ctx.err(a.span(), format!("output {name:?} is given twice (artifacts and reports share names)")); |
| 1402 | 31× | } else if job.outputs.len() == MAX_OUTPUTS { |
| 1403 | 0× | ctx.err(a.span(), format!("a job has at most {MAX_OUTPUTS} artifacts and reports")); |
| 1404 | 31× | } else { |
| 1405 | 31× | job.outputs.push(Output { name, path, always, kind }); |
| 1406 | 31× | } |
| 1407 | | } |
| 1408 | 23× | } |
| 1409 | |
|
| 1410 | |
|
| 1411 | |
|
| 1412 | |
|
| 1413 | |
|
| 1414 | 11× | fn parse_approval(ctx: &mut Ctx, node: &KdlNode) -> Option<(Approval, bool)> { |
| 1415 | 11× | if !node.entries().is_empty() { |
| 1416 | 0× | ctx.err(node.span(), "`approval` takes a block: approval { by \"maintain\"; others-only #true; expires \"24h\" }"); |
| 1417 | 11× | } |
| 1418 | 11× | let mut a = Approval { by: "maintain".into(), others_only: false, expires_s: 24 * 3600, sealed: false }; |
| 1419 | 11× | let mut sealed_said = false; |
| 1420 | 11× | let Some(children) = node.children() else { return Some((a, false)) }; |
| 1421 | 11× | for c in children.nodes() { |
| 1422 | 11× | ctx.no_children(c); |
| 1423 | 11× | match c.name().value() { |
| 1424 | 11× | "by" => match ctx.one_string(c).as_deref() { |
| 1425 | 2× | Some(r @ ("write" | "maintain" | "admin")) => a.by = r.to_string(), |
| 1426 | 1× | Some(r) => ctx.err(c.span(), format!("approval by {r:?}: expected \"write\", \"maintain\" or \"admin\"")), |
| 1427 | 0× | None => {} |
| 1428 | | }, |
| 1429 | 9× | "others-only" => match c.entries() { |
| 1430 | 2× | [e] if e.name().is_none() => match e.value() { |
| 1431 | 1× | KdlValue::Bool(b) => a.others_only = *b, |
| 1432 | 1× | _ => ctx.err(c.span(), "`others-only` takes #true or #false"), |
| 1433 | | }, |
| 1434 | 0× | _ => ctx.err(c.span(), "`others-only` takes #true or #false"), |
| 1435 | | }, |
| 1436 | 7× | "sealed" => match c.entries() { |
| 1437 | 4× | [e] if e.name().is_none() => match e.value() { |
| 1438 | 3× | KdlValue::Bool(b) => { |
| 1439 | 3× | a.sealed = *b; |
| 1440 | 3× | sealed_said = true; |
| 1441 | 3× | } |
| 1442 | 1× | _ => ctx.err(c.span(), "`sealed` takes #true or #false"), |
| 1443 | | }, |
| 1444 | 0× | _ => ctx.err(c.span(), "`sealed` takes #true or #false"), |
| 1445 | | }, |
| 1446 | 3× | "expires" => { |
| 1447 | 2× | let Some(t) = ctx.one_string(c) else { continue }; |
| 1448 | 2× | match parse_duration(&t) { |
| 1449 | 2× | Some(secs) if (60..=MAX_APPROVAL_S).contains(&secs) => a.expires_s = secs, |
| 1450 | 1× | Some(_) => ctx.err(c.span(), format!("expires {t:?} must be from 1m to 720h")), |
| 1451 | 0× | None => ctx.err(c.span(), format!("expires {t:?}: expected hours, minutes and seconds, as \"24h\"")), |
| 1452 | | } |
| 1453 | | } |
| 1454 | 1× | other => { |
| 1455 | 1× | ctx.err(c.span(), format!("unknown approval setting `{other}`; approval takes `by`, `others-only`, `expires` and `sealed`")) |
| 1456 | | } |
| 1457 | | } |
| 1458 | | } |
| 1459 | 9× | Some((a, sealed_said)) |
| 1460 | 11× | } |
| 1461 | |
|
| 1462 | |
|
| 1463 | 10× | fn parse_input(ctx: &mut Ctx, node: &KdlNode, job: &mut Job) { |
| 1464 | 10× | if !node.entries().is_empty() { |
| 1465 | 0× | ctx.err(node.span(), "`input` takes a block: input { artifact \"tfplan\" from \"plan\"; path \"/src/tfplan\" }"); |
| 1466 | 10× | } |
| 1467 | 10× | let (mut artifact, mut from, mut path) = (None, None, None); |
| 1468 | 19× | for c in block(ctx, node) { |
| 1469 | 19× | ctx.no_children(c); |
| 1470 | 19× | match c.name().value() { |
| 1471 | 19× | "artifact" => match ctx.strings(c).as_slice() { |
| 1472 | 9× | [name, word, producer] if word == "from" => { |
| 1473 | 9× | if ctx.name_ok(c.span(), "artifact", name) && ctx.name_ok(c.span(), "job", producer) { |
| 1474 | 9× | artifact = Some(name.clone()); |
| 1475 | 9× | from = Some(producer.clone()); |
| 1476 | 9× | } |
| 1477 | | } |
| 1478 | 1× | _ => ctx.err(c.span(), "`artifact` names the artifact and its job: artifact \"tfplan\" from \"plan\""), |
| 1479 | | }, |
| 1480 | 9× | "path" => match ctx.one_string(c) { |
| 1481 | 9× | Some(p) if output_path_ok(&p) => path = Some(p), |
| 1482 | 1× | Some(p) => { |
| 1483 | 1× | ctx.err(c.span(), format!("input path {p:?} must be an absolute path under /src, without . or .., quotes or spaces")) |
| 1484 | | } |
| 1485 | 0× | None => {} |
| 1486 | | }, |
| 1487 | 0× | other => ctx.err(c.span(), format!("unknown input setting `{other}`; an input has `artifact` and `path`")), |
| 1488 | | } |
| 1489 | | } |
| 1490 | 10× | match (artifact, from, path) { |
| 1491 | 7× | (Some(artifact), Some(from), Some(path)) => { |
| 1492 | 7× | if job.inputs.iter().any(|i| i.path == path) { |
| 1493 | 1× | ctx.err(node.span(), format!("two inputs are put at {path:?}")); |
| 1494 | 6× | } else if job.inputs.len() == MAX_INPUTS { |
| 1495 | 0× | ctx.err(node.span(), format!("a job takes at most {MAX_INPUTS} inputs")); |
| 1496 | 6× | } else { |
| 1497 | 6× | job.inputs.push(Input { artifact, from, path }); |
| 1498 | 6× | } |
| 1499 | | } |
| 1500 | 3× | (a, _, p) => { |
| 1501 | 3× | if a.is_some() && p.is_none() { |
| 1502 | 2× | ctx.err(node.span(), "an input needs a `path`"); |
| 1503 | 2× | } else if a.is_none() && p.is_some() { |
| 1504 | 1× | ctx.err(node.span(), "an input needs `artifact \"NAME\" from \"JOB\"`"); |
| 1505 | 1× | } |
| 1506 | | } |
| 1507 | | } |
| 1508 | 10× | } |
| 1509 | |
|
| 1510 | 173× | fn parse_job(ctx: &mut Ctx, node: &KdlNode) -> Option<Job> { |
| 1511 | 173× | let name = ctx.one_string(node)?; |
| 1512 | 173× | let named = ctx.name_ok(node.span(), "job", &name); |
| 1513 | 173× | let mut approval: Option<(Approval, bool)> = None; |
| 1514 | 173× | let mut job = Job { |
| 1515 | 173× | name: name.clone(), |
| 1516 | 173× | sandbox: String::new(), |
| 1517 | 173× | scope: vec![], |
| 1518 | 173× | depends_on: vec![], |
| 1519 | 173× | env: BTreeMap::new(), |
| 1520 | 173× | exec: vec![], |
| 1521 | 173× | mounts: vec![], |
| 1522 | 173× | secrets: vec![], |
| 1523 | 173× | identities: vec![], |
| 1524 | 173× | outputs: vec![], |
| 1525 | 173× | inputs: vec![], |
| 1526 | 173× | approval: None, |
| 1527 | 173× | environment: None, |
| 1528 | 173× | runs_on: vec![], |
| 1529 | 173× | timeout_s: None, |
| 1530 | 173× | on: None, |
| 1531 | 173× | priority: None, |
| 1532 | 173× | }; |
| 1533 | 173× | let mut seen = BTreeSet::new(); |
| 1534 | 173× | let mut outputs_at = None; |
| 1535 | 518× | for c in block(ctx, node) { |
| 1536 | 518× | let key = c.name().value(); |
| 1537 | 518× | if key != "scope" |
| 1538 | 517× | && key != "depends-on" |
| 1539 | 500× | && key != "mount" |
| 1540 | 440× | && key != "secret" |
| 1541 | 428× | && key != "identity" |
| 1542 | 420× | && key != "runs-on" |
| 1543 | 417× | && !seen.insert(key.to_string()) |
| 1544 | 1× | { |
| 1545 | 1× | ctx.err(c.span(), format!("`{key}` is given twice")); |
| 1546 | 517× | } |
| 1547 | 0× | match key { |
| 1548 | 518× | "sandbox" => { |
| 1549 | 173× | ctx.no_children(c); |
| 1550 | 173× | job.sandbox = ctx.one_string(c).unwrap_or_default(); |
| 1551 | 173× | } |
| 1552 | 345× | "scope" => { |
| 1553 | 1× | ctx.no_children(c); |
| 1554 | 2× | for g in ctx.strings(c) { |
| 1555 | 2× | match globset::Glob::new(&g) { |
| 1556 | 2× | Ok(_) => job.scope.push(g), |
| 1557 | 0× | Err(e) => ctx.err(c.span(), format!("scope {g:?}: {e}")), |
| 1558 | | } |
| 1559 | | } |
| 1560 | | } |
| 1561 | 344× | "depends-on" => { |
| 1562 | 17× | ctx.no_children(c); |
| 1563 | 17× | job.depends_on.extend(ctx.strings(c)); |
| 1564 | 17× | } |
| 1565 | 327× | "runs-on" => { |
| 1566 | 3× | ctx.no_children(c); |
| 1567 | 3× | parse_labels(ctx, c, &mut job.runs_on); |
| 1568 | 3× | } |
| 1569 | 324× | "timeout" => { |
| 1570 | 3× | ctx.no_children(c); |
| 1571 | 3× | job.timeout_s = parse_timeout(ctx, c); |
| 1572 | 3× | } |
| 1573 | 321× | "priority" => job.priority = parse_priority(ctx, c), |
| 1574 | 316× | "on" => job.on = Some(parse_on(ctx, c)), |
| 1575 | 306× | "env" => parse_env(ctx, c, &mut job.env), |
| 1576 | 301× | "exec" => { |
| 1577 | 173× | ctx.no_children(c); |
| 1578 | 173× | job.exec = ctx.strings(c); |
| 1579 | 173× | if job.exec.is_empty() { |
| 1580 | 1× | ctx.err(c.span(), "`exec` needs a program and its arguments"); |
| 1581 | 191× | } else if job.exec.iter().any(|a| a.contains('\0')) { |
| 1582 | 0× | ctx.err(c.span(), "`exec` arguments cannot contain NUL"); |
| 1583 | 172× | } |
| 1584 | | } |
| 1585 | 128× | "mount" => { |
| 1586 | 60× | if !c.entries().is_empty() { |
| 1587 | 0× | ctx.err(c.span(), "`mount` takes a block of source, target and size"); |
| 1588 | 60× | } |
| 1589 | 60× | if let Some(m) = parse_mount(ctx, c) { |
| 1590 | 50× | if job.mounts.len() == MAX_MOUNTS { |
| 1591 | 1× | ctx.err(c.span(), format!("a job has at most {MAX_MOUNTS} mounts")); |
| 1592 | 49× | } else if let Some(other) = job.mounts.iter().find(|o| { |
| 1593 | 24× | o.target == m.target |
| 1594 | 23× | || m.target.starts_with(&format!("{}/", o.target)) |
| 1595 | 22× | || o.target.starts_with(&format!("{}/", m.target)) |
| 1596 | 24× | }) { |
| 1597 | 2× | ctx.err(c.span(), format!("mount {:?} overlaps mount {:?}", m.target, other.target)); |
| 1598 | 47× | } else { |
| 1599 | 47× | job.mounts.push(m); |
| 1600 | 47× | } |
| 1601 | 10× | } |
| 1602 | | } |
| 1603 | 68× | "output" => { |
| 1604 | 23× | outputs_at = Some(c.span()); |
| 1605 | 23× | parse_output(ctx, c, &mut job); |
| 1606 | 23× | } |
| 1607 | 45× | "input" => parse_input(ctx, c, &mut job), |
| 1608 | 35× | "approval" => approval = parse_approval(ctx, c), |
| 1609 | 24× | "environment" => { |
| 1610 | 4× | ctx.no_children(c); |
| 1611 | 4× | if let Some(e) = ctx.one_string(c) { |
| 1612 | 13× | let ok = !e.is_empty() && e.len() <= 32 && e.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-'); |
| 1613 | 4× | if ok { |
| 1614 | 3× | job.environment = Some(e); |
| 1615 | 3× | } else { |
| 1616 | 1× | ctx.err(c.span(), format!("environment {e:?}: 1 to 32 of a-z 0-9 -, as \"prod\"")); |
| 1617 | 1× | } |
| 1618 | 0× | } |
| 1619 | | } |
| 1620 | 20× | "secret" => parse_secret(ctx, c, &mut job), |
| 1621 | 8× | "identity" => parse_identity(ctx, c, &mut job), |
| 1622 | 0× | n if LATER_JOB.contains(&n) => ctx.err(c.span(), format!("`{n}` is not in M0 yet")), |
| 1623 | 0× | other => ctx.err(c.span(), format!("unknown job setting `{other}`")), |
| 1624 | | } |
| 1625 | | } |
| 1626 | | |
| 1627 | 173× | job.approval = approval.map(|(mut a, said)| { |
| 1628 | 11× | if !said { |
| 1629 | 8× | a.sealed = job.environment.is_some(); |
| 1630 | 8× | } |
| 1631 | 11× | a |
| 1632 | 11× | }); |
| 1633 | 173× | job.mounts.sort_by(|a, b| a.target.cmp(&b.target)); |
| 1634 | 173× | job.outputs.sort_by(|a, b| a.name.cmp(&b.name)); |
| 1635 | 173× | job.inputs.sort_by(|a, b| a.path.cmp(&b.path)); |
| 1636 | 173× | job.secrets.sort_by(|a, b| a.name.cmp(&b.name)); |
| 1637 | 173× | job.identities.sort_by(|a, b| a.audience.cmp(&b.audience)); |
| 1638 | | |
| 1639 | 173× | for o in &job.outputs { |
| 1640 | 31× | if let Some(m) = job.mounts.iter().find(|m| m.source == MountSource::Tmpfs && o.path.starts_with(&format!("{}/", m.target))) { |
| 1641 | 1× | ctx.err( |
| 1642 | 1× | outputs_at.unwrap_or(node.span()), |
| 1643 | 1× | format!( |
| 1644 | 1× | "artifact {:?} is on the tmpfs mount {:?}, which is gone when the job ends; use ephemeral://disk", |
| 1645 | 1× | o.name, m.target |
| 1646 | 1× | ), |
| 1647 | 1× | ); |
| 1648 | 30× | } |
| 1649 | | } |
| 1650 | 173× | if job.sandbox.is_empty() && !seen.contains("sandbox") { |
| 1651 | 0× | ctx.err(node.span(), format!("job {name:?} needs a sandbox")); |
| 1652 | 173× | } |
| 1653 | 173× | if job.exec.is_empty() && !seen.contains("exec") { |
| 1654 | 0× | ctx.err(node.span(), format!("job {name:?} needs an exec")); |
| 1655 | 173× | } |
| 1656 | 173× | job.depends_on.sort(); |
| 1657 | 173× | job.depends_on.dedup(); |
| 1658 | | |
| 1659 | | |
| 1660 | 173× | named.then_some(job) |
| 1661 | 173× | } |