cargo-rail 0.28.1

Cargo-native Rust workspace engine for affected CI, Rust source-surface analysis, compiler reuse, dependency coherence, exact-SHA releases, and split/sync
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! Exact GitHub workflow attempts that authorize release effects.

use super::{process, remote::RemoteRepository};
use crate::error::{RailError, RailResult};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::{BTreeMap, BTreeSet};
use std::path::Path;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct WorkflowRun {
    pub workflow: String,
    pub workflow_id: u64,
    pub run_id: u64,
    pub attempt: u64,
    pub jobs: Vec<WorkflowJob>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct WorkflowJob {
    pub name: String,
    pub id: u64,
}

pub(crate) enum Observation {
    Verified(Vec<WorkflowRun>),
    Waiting(String),
    Failed(String),
}

pub(crate) fn observe(
    root: &Path,
    repository: &RemoteRepository,
    source: &str,
    required: &BTreeMap<String, Vec<String>>,
    retained: &[WorkflowRun],
    dispatched: &BTreeMap<String, u64>,
) -> RailResult<Observation> {
    if required.is_empty() {
        return Err(RailError::with_help(
            "GitHub release validation has no required workflows",
            "configure release.validation with workflow paths and required job names before preparing the release",
        ));
    }
    let host = repository
        .host()
        .ok_or_else(|| RailError::message("GitHub validation requires a hosted repository"))?;
    let repository = repository.path();
    let current_run = std::env::var("GITHUB_RUN_ID")
        .ok()
        .and_then(|id| id.parse::<u64>().ok());
    let mut verified = Vec::new();
    for (workflow, required_jobs) in required {
        let workflow_id = workflow_id(root, host, repository, workflow)?;
        let run = if let Some(previous) = retained.iter().find(|run| run.workflow == *workflow) {
            api(
                root,
                host,
                &format!("repos/{repository}/actions/runs/{}", previous.run_id),
            )?
        } else if let Some(dispatched) = dispatched.get(workflow) {
            api(root, host, &format!("repos/{repository}/actions/runs/{dispatched}"))?
        } else {
            let Some(selected) = select_run(root, host, repository, workflow_id, source)? else {
                return Ok(Observation::Waiting(format!(
                    "{workflow} has no validation run for {source}"
                )));
            };
            selected
        };
        let run_id = positive(&run, "id")?;
        let attempt = positive(&run, "run_attempt")?;
        if dispatched.get(workflow).is_some_and(|dispatch| *dispatch != run_id) {
            return Err(RailError::message("dispatched validation run changed"));
        }
        if Some(run_id) == current_run {
            return Err(RailError::message(
                "a release executor cannot authorize itself as validation",
            ));
        }
        if retained
            .iter()
            .find(|run| run.workflow == *workflow)
            .is_some_and(|previous| {
                previous.run_id != run_id || previous.attempt != attempt || previous.workflow_id != workflow_id
            })
        {
            return Err(RailError::message(
                "retained validation attempt changed; the release evidence cannot be replaced",
            ));
        }
        let exact = api(
            root,
            host,
            &format!("repos/{repository}/actions/runs/{run_id}/attempts/{attempt}"),
        )?;
        match inspect_run(&exact, repository, source, workflow, workflow_id, run_id, attempt)? {
            RunStatus::Waiting => {
                return Ok(Observation::Waiting(format!(
                    "{workflow} run {run_id}, attempt {attempt} is incomplete"
                )));
            }
            RunStatus::Failed => {
                return Ok(Observation::Failed(format!(
                    "{workflow} run {run_id}, attempt {attempt} did not succeed"
                )));
            }
            RunStatus::Succeeded => {}
        }
        let jobs = api(
            root,
            host,
            &format!("repos/{repository}/actions/runs/{run_id}/attempts/{attempt}/jobs?per_page=100"),
        )?;
        let jobs = inspect_jobs(&jobs, required_jobs, run_id, attempt, source)?;
        verified.push(WorkflowRun {
            workflow: workflow.clone(),
            workflow_id,
            run_id,
            attempt,
            jobs,
        });
    }
    if !retained.is_empty() && retained != verified {
        return Err(RailError::message(
            "validation results differ from retained release evidence",
        ));
    }
    Ok(Observation::Verified(verified))
}

pub(crate) fn validate_records(required: &BTreeMap<String, Vec<String>>, runs: &[WorkflowRun]) -> RailResult<()> {
    if runs.is_empty() {
        return Ok(());
    }
    if runs.len() != required.len() {
        return Err(RailError::message(
            "release validation evidence does not cover the required workflows",
        ));
    }
    let mut ids = BTreeSet::new();
    for ((workflow, jobs), run) in required.iter().zip(runs) {
        if &run.workflow != workflow
            || run.workflow_id == 0
            || run.run_id == 0
            || run.attempt == 0
            || !ids.insert(run.run_id)
            || run.jobs.len() != jobs.len()
            || run
                .jobs
                .iter()
                .zip(jobs)
                .any(|(actual, expected)| &actual.name != expected || actual.id == 0)
            || run.jobs.iter().map(|job| job.id).collect::<BTreeSet<_>>().len() != jobs.len()
        {
            return Err(RailError::message(
                "release validation evidence has inconsistent workflow or job identities",
            ));
        }
    }
    Ok(())
}

enum RunStatus {
    Waiting,
    Failed,
    Succeeded,
}

fn inspect_run(
    run: &Value,
    repository: &str,
    source: &str,
    workflow: &str,
    workflow_id: u64,
    run_id: u64,
    attempt: u64,
) -> RailResult<RunStatus> {
    if run["id"] != run_id
        || run["run_attempt"] != attempt
        || run["workflow_id"] != workflow_id
        || run["head_sha"] != source
        || run["path"].as_str().is_none_or(|path| {
            path != workflow
                && path
                    .strip_prefix(workflow)
                    .is_none_or(|suffix| !suffix.starts_with('@') || suffix.len() == 1)
        })
        || run["head_repository"]["full_name"]
            .as_str()
            .is_none_or(|name| !name.eq_ignore_ascii_case(repository))
        || run["repository"]["full_name"]
            .as_str()
            .is_none_or(|name| !name.eq_ignore_ascii_case(repository))
        || !matches!(
            run["event"].as_str(),
            Some("push" | "workflow_dispatch" | "workflow_call")
        )
    {
        return Err(RailError::message(
            "validation run does not match its required workflow, attempt, repository, and release commit",
        ));
    }
    match run["status"].as_str() {
        Some("completed") if run["conclusion"] == "success" => Ok(RunStatus::Succeeded),
        Some("completed") => Ok(RunStatus::Failed),
        Some("queued" | "requested" | "waiting" | "pending" | "in_progress") => Ok(RunStatus::Waiting),
        _ => Err(RailError::message("validation run has an unavailable status")),
    }
}

fn inspect_jobs(
    value: &Value,
    required: &[String],
    run_id: u64,
    attempt: u64,
    source: &str,
) -> RailResult<Vec<WorkflowJob>> {
    let rows = bounded_rows(value, "jobs")?;
    let mut jobs = Vec::new();
    for name in required {
        let mut matching = rows.iter().filter(|job| job["name"] == *name);
        let job = matching
            .next()
            .ok_or_else(|| RailError::message(format!("required validation job '{name}' is missing")))?;
        if matching.next().is_some()
            || job["run_id"] != run_id
            || job.get("run_attempt").is_some_and(|value| value != attempt)
            || job["head_sha"] != source
            || job["status"] != "completed"
            || job["conclusion"] != "success"
        {
            return Err(RailError::message(format!(
                "required validation job '{name}' is ambiguous, skipped, incomplete, failed, or incorrectly bound"
            )));
        }
        jobs.push(WorkflowJob {
            name: name.clone(),
            id: positive(job, "id")?,
        });
    }
    Ok(jobs)
}

pub(super) fn bounded_rows<'a>(value: &'a Value, field: &str) -> RailResult<&'a [Value]> {
    let rows = value[field]
        .as_array()
        .ok_or_else(|| RailError::message("GitHub validation response has no result array"))?;
    if rows.len() > 100 || value["total_count"].as_u64() != Some(rows.len() as u64) {
        return Err(RailError::message(
            "GitHub validation response is incomplete or exceeds its 100-result bound",
        ));
    }
    Ok(rows)
}

pub(super) fn positive(value: &Value, field: &str) -> RailResult<u64> {
    value[field]
        .as_u64()
        .filter(|id| *id != 0)
        .ok_or_else(|| RailError::message(format!("GitHub validation has no positive {field}")))
}

pub(super) fn api(root: &Path, host: &str, path: &str) -> RailResult<Value> {
    let output = process::run("gh", &["api", "--hostname", host, path], Some(root))?;
    if !output.status.success() {
        return Err(RailError::message(format!(
            "GitHub validation observation failed: {}",
            String::from_utf8_lossy(&output.stderr)
        )));
    }
    super::contract::decode(&output.stdout)
}

pub(super) fn workflow_id(root: &Path, host: &str, repository: &str, workflow: &str) -> RailResult<u64> {
    let filename = workflow
        .rsplit('/')
        .next()
        .ok_or_else(|| RailError::message("invalid validation workflow"))?;
    let descriptor = api(root, host, &format!("repos/{repository}/actions/workflows/{filename}"))?;
    if descriptor["path"] != workflow || descriptor["state"] != "active" {
        return Err(RailError::message(
            "required workflow is unavailable or has another path",
        ));
    }
    positive(&descriptor, "id")
}

pub(super) fn select_run(
    root: &Path,
    host: &str,
    repository: &str,
    workflow_id: u64,
    source: &str,
) -> RailResult<Option<Value>> {
    let runs = api(
        root,
        host,
        &format!("repos/{repository}/actions/workflows/{workflow_id}/runs?head_sha={source}&per_page=100"),
    )?;
    let rows = bounded_rows(&runs, "workflow_runs")?;
    Ok(rows
        .iter()
        .filter(|run| {
            run["head_sha"] == source
                && matches!(
                    run["event"].as_str(),
                    Some("push" | "workflow_dispatch" | "workflow_call")
                )
        })
        .max_by_key(|run| run["id"].as_u64())
        .cloned())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn run() -> Value {
        serde_json::json!({"id": 42, "run_attempt": 3, "workflow_id": 7, "head_sha": "a".repeat(40),
            "path": ".github/workflows/ci.yml", "repository":{"full_name":"example/repository"},
            "head_repository":{"full_name":"example/repository"}, "event":"workflow_dispatch", "status":"completed", "conclusion":"success"})
    }

    #[test]
    fn workflow_authorization_rejects_wrong_source_workflow_attempt_and_repository() {
        let valid = run();
        assert!(matches!(
            inspect_run(
                &valid,
                "example/repository",
                &"a".repeat(40),
                ".github/workflows/ci.yml",
                7,
                42,
                3
            )
            .unwrap(),
            RunStatus::Succeeded
        ));
        for (field, replacement) in [
            ("id", serde_json::json!(43)),
            ("run_attempt", serde_json::json!(2)),
            ("workflow_id", serde_json::json!(8)),
            ("head_sha", serde_json::json!("b".repeat(40))),
            ("path", serde_json::json!(".github/workflows/release.yml")),
            (
                "head_repository",
                serde_json::json!({"full_name":"attacker/repository"}),
            ),
            ("event", serde_json::json!("pull_request")),
        ] {
            let mut invalid = valid.clone();
            invalid[field] = replacement;
            let error = inspect_run(
                &invalid,
                "example/repository",
                &"a".repeat(40),
                ".github/workflows/ci.yml",
                7,
                42,
                3,
            )
            .err()
            .expect("wrong binding must fail");
            assert!(error.to_string().contains("does not match"), "{field}: {error}");
        }
    }

    #[test]
    fn required_jobs_reject_skipped_missing_duplicate_and_wrong_attempt() {
        let valid = serde_json::json!({"total_count":1,"jobs":[{"id":17,"name":"tests","run_id":42,"run_attempt":3,"head_sha":"a".repeat(40),"status":"completed","conclusion":"success"}]});
        assert_eq!(
            inspect_jobs(&valid, &["tests".to_owned()], 42, 3, &"a".repeat(40)).unwrap(),
            vec![WorkflowJob {
                name: "tests".to_owned(),
                id: 17
            }]
        );
        let mut documented_response = valid.clone();
        documented_response["jobs"][0]
            .as_object_mut()
            .unwrap()
            .remove("run_attempt");
        assert_eq!(
            inspect_jobs(&documented_response, &["tests".to_owned()], 42, 3, &"a".repeat(40)).unwrap(),
            vec![WorkflowJob {
                name: "tests".to_owned(),
                id: 17
            }],
        );
        for conclusion in ["skipped", "neutral", "failure", "cancelled"] {
            let mut invalid = valid.clone();
            invalid["jobs"][0]["conclusion"] = serde_json::json!(conclusion);
            assert!(
                inspect_jobs(&invalid, &["tests".to_owned()], 42, 3, &"a".repeat(40))
                    .unwrap_err()
                    .to_string()
                    .contains("required validation job")
            );
        }
        let mut invalid = valid.clone();
        invalid["jobs"].as_array_mut().unwrap().push(valid["jobs"][0].clone());
        invalid["total_count"] = serde_json::json!(2);
        assert!(
            inspect_jobs(&invalid, &["tests".to_owned()], 42, 3, &"a".repeat(40))
                .unwrap_err()
                .to_string()
                .contains("ambiguous")
        );
        assert!(
            inspect_jobs(&valid, &["absent".to_owned()], 42, 3, &"a".repeat(40))
                .unwrap_err()
                .to_string()
                .contains("missing")
        );
        assert!(
            inspect_jobs(&valid, &["tests".to_owned()], 42, 4, &"a".repeat(40))
                .unwrap_err()
                .to_string()
                .contains("incorrectly bound")
        );
    }
}