| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
use crate::Shared; |
| 11 |
pub use oxy_core::dsse::pae; |
| 12 |
|
| 13 |
|
| 14 |
pub const PAYLOAD_TYPE: &str = oxy_core::dsse::IN_TOTO; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
pub const BUILD_TYPE: &str = "https://oxygenhub.io/oxygenflow/job/v1"; |
| 19 |
|
| 20 |
|
| 21 |
pub struct Subject<'a> { |
| 22 |
|
| 23 |
pub name: &'a str, |
| 24 |
pub bytes: &'a [u8], |
| 25 |
|
| 26 |
pub repo: &'a str, |
| 27 |
pub run_url: &'a str, |
| 28 |
pub bookmark: &'a str, |
| 29 |
|
| 30 |
pub state: &'a str, |
| 31 |
pub heads: &'a str, |
| 32 |
pub pipeline_hash: &'a str, |
| 33 |
pub job: &'a str, |
| 34 |
|
| 35 |
pub runner: &'a str, |
| 36 |
pub receipt_signer: &'a str, |
| 37 |
pub receipt_attest: &'a str, |
| 38 |
pub started_ms: Option<i64>, |
| 39 |
pub finished_ms: Option<i64>, |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
pub fn statement(base_url: &str, s: &Subject) -> serde_json::Value { |
| 44 |
use sha2::Digest; |
| 45 |
let sha256 = hex::encode(sha2::Sha256::digest(s.bytes)); |
| 46 |
let when = |ms: Option<i64>| { |
| 47 |
ms.and_then(|t| time::OffsetDateTime::from_unix_timestamp_nanos(t as i128 * 1_000_000).ok()) |
| 48 |
.and_then(|t| t.format(&time::format_description::well_known::Rfc3339).ok()) |
| 49 |
}; |
| 50 |
let mut metadata = serde_json::json!({ "invocationId": s.run_url }); |
| 51 |
if let Some(t) = when(s.started_ms) { |
| 52 |
metadata["startedOn"] = t.into(); |
| 53 |
} |
| 54 |
if let Some(t) = when(s.finished_ms) { |
| 55 |
metadata["finishedOn"] = t.into(); |
| 56 |
} |
| 57 |
serde_json::json!({ |
| 58 |
"_type": "https://in-toto.io/Statement/v1", |
| 59 |
"subject": [{ "name": s.name, "digest": { "sha256": sha256, "blake3": blake3::hash(s.bytes).to_hex().to_string() } }], |
| 60 |
"predicateType": "https://slsa.dev/provenance/v1", |
| 61 |
"predicate": { |
| 62 |
"buildDefinition": { |
| 63 |
"buildType": BUILD_TYPE, |
| 64 |
"externalParameters": { |
| 65 |
"repository": format!("{base_url}/{}", s.repo), |
| 66 |
"bookmark": s.bookmark, |
| 67 |
"job": s.job, |
| 68 |
"pipeline": s.pipeline_hash, |
| 69 |
}, |
| 70 |
"resolvedDependencies": [{ |
| 71 |
"uri": format!("{base_url}/{}", s.repo), |
| 72 |
"digest": { "oxygen-state": s.state }, |
| 73 |
"annotations": { "heads": s.heads }, |
| 74 |
}], |
| 75 |
}, |
| 76 |
"runDetails": { |
| 77 |
"builder": { |
| 78 |
"id": format!("{base_url}/oxygenflow/runner/{}", s.runner), |
| 79 |
}, |
| 80 |
"metadata": metadata, |
| 81 |
"byproducts": [{ |
| 82 |
"name": "oxygen-receipt", |
| 83 |
"digest": { "oxygen-attestation": s.receipt_attest }, |
| 84 |
"annotations": { "signedBy": s.receipt_signer }, |
| 85 |
}], |
| 86 |
}, |
| 87 |
}, |
| 88 |
}) |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
pub fn envelope(app: &Shared, statement: &serde_json::Value) -> anyhow::Result<serde_json::Value> { |
| 93 |
let payload = serde_json::to_vec(statement)?; |
| 94 |
let (key, secret) = crate::keys::load_secret(&app.config.keys_dir())?; |
| 95 |
Ok(serde_json::from_str(&oxy_core::dsse::envelope(PAYLOAD_TYPE, &payload, &key.to_hex(), &secret))?) |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
pub fn public_pem(app: &Shared) -> String { |
| 100 |
oxy_core::dsse::public_pem(&app.hub_key.identity) |
| 101 |
} |