| 1 |
|
| 2 |
|
| 3 |
use crate::{BundleDigest, ContractError, GateRecord, Manifest, Provenance, Scope}; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
pub const CONTRACT_VERSION: u32 = 1; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] |
| 19 |
pub struct ArtifactRecord { |
| 20 |
pub contract: u32, |
| 21 |
|
| 22 |
pub producer: String, |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
pub digest: BundleDigest, |
| 27 |
pub manifest: Manifest, |
| 28 |
pub provenance: Provenance, |
| 29 |
|
| 30 |
pub gates: Vec<GateRecord>, |
| 31 |
} |
| 32 |
|
| 33 |
impl ArtifactRecord { |
| 34 |
pub fn new( |
| 35 |
producer: impl Into<String>, |
| 36 |
manifest: Manifest, |
| 37 |
provenance: Provenance, |
| 38 |
gates: Vec<GateRecord>, |
| 39 |
) -> Result<Self, ContractError> { |
| 40 |
let record = Self { |
| 41 |
contract: CONTRACT_VERSION, |
| 42 |
producer: producer.into(), |
| 43 |
digest: manifest.digest(), |
| 44 |
manifest, |
| 45 |
provenance, |
| 46 |
gates, |
| 47 |
}; |
| 48 |
record.validate()?; |
| 49 |
Ok(record) |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
pub fn validate(&self) -> Result<(), ContractError> { |
| 58 |
if self.contract != CONTRACT_VERSION { |
| 59 |
return Err(ContractError::UnknownContractVersion(self.contract)); |
| 60 |
} |
| 61 |
if self.producer.trim().is_empty() { |
| 62 |
return Err(ContractError::MissingProvenance("producer")); |
| 63 |
} |
| 64 |
if self.digest != self.manifest.digest() { |
| 65 |
return Err(ContractError::DigestMismatch { |
| 66 |
claimed: self.digest.to_string(), |
| 67 |
computed: self.manifest.digest().to_string(), |
| 68 |
}); |
| 69 |
} |
| 70 |
self.provenance.validate()?; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
for g in &self.gates { |
| 76 |
if let Scope::Environment { env } = &g.scope { |
| 77 |
return Err(ContractError::EnvironmentEvidenceFromBuilder { |
| 78 |
gate: g.gate.clone(), |
| 79 |
env: env.clone(), |
| 80 |
}); |
| 81 |
} |
| 82 |
} |
| 83 |
Ok(()) |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
pub fn all_gates_passed(&self) -> bool { |
| 90 |
self.gates.iter().all(|g| g.verdict.is_passed()) |
| 91 |
} |
| 92 |
|
| 93 |
pub fn to_json(&self) -> String { |
| 94 |
|
| 95 |
|
| 96 |
let mut s = serde_json::to_string_pretty(self).unwrap_or_default(); |
| 97 |
s.push('\n'); |
| 98 |
s |
| 99 |
} |
| 100 |
|
| 101 |
pub fn parse(json: &str) -> Result<Self, ContractError> { |
| 102 |
let record: Self = serde_json::from_str(json).map_err(ContractError::Malformed)?; |
| 103 |
record.validate()?; |
| 104 |
Ok(record) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] |
| 116 |
pub struct EvidenceRecord { |
| 117 |
pub contract: u32, |
| 118 |
|
| 119 |
pub producer: String, |
| 120 |
|
| 121 |
pub subject: BundleDigest, |
| 122 |
pub gates: Vec<GateRecord>, |
| 123 |
} |
| 124 |
|
| 125 |
impl EvidenceRecord { |
| 126 |
pub fn new( |
| 127 |
producer: impl Into<String>, |
| 128 |
subject: BundleDigest, |
| 129 |
gates: Vec<GateRecord>, |
| 130 |
) -> Result<Self, ContractError> { |
| 131 |
let record = Self { |
| 132 |
contract: CONTRACT_VERSION, |
| 133 |
producer: producer.into(), |
| 134 |
subject, |
| 135 |
gates, |
| 136 |
}; |
| 137 |
record.validate()?; |
| 138 |
Ok(record) |
| 139 |
} |
| 140 |
|
| 141 |
pub fn validate(&self) -> Result<(), ContractError> { |
| 142 |
if self.contract != CONTRACT_VERSION { |
| 143 |
return Err(ContractError::UnknownContractVersion(self.contract)); |
| 144 |
} |
| 145 |
if self.producer.trim().is_empty() { |
| 146 |
return Err(ContractError::MissingProvenance("producer")); |
| 147 |
} |
| 148 |
Ok(()) |
| 149 |
} |
| 150 |
|
| 151 |
pub fn to_json(&self) -> String { |
| 152 |
let mut s = serde_json::to_string_pretty(self).unwrap_or_default(); |
| 153 |
s.push('\n'); |
| 154 |
s |
| 155 |
} |
| 156 |
|
| 157 |
pub fn parse(json: &str) -> Result<Self, ContractError> { |
| 158 |
let record: Self = serde_json::from_str(json).map_err(ContractError::Malformed)?; |
| 159 |
record.validate()?; |
| 160 |
Ok(record) |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
#[cfg(test)] |
| 165 |
mod tests { |
| 166 |
use super::*; |
| 167 |
use crate::Verdict; |
| 168 |
use chrono::{DateTime, Utc}; |
| 169 |
|
| 170 |
fn at() -> DateTime<Utc> { |
| 171 |
DateTime::<Utc>::from_timestamp(1_754_000_000, 0).unwrap() |
| 172 |
} |
| 173 |
|
| 174 |
fn manifest() -> Manifest { |
| 175 |
Manifest::new([ |
| 176 |
("GoingsOn.AppImage", "1".repeat(64)), |
| 177 |
("GoingsOn.AppImage.minisig", "2".repeat(64)), |
| 178 |
]) |
| 179 |
.unwrap() |
| 180 |
} |
| 181 |
|
| 182 |
fn provenance() -> Provenance { |
| 183 |
Provenance { |
| 184 |
app: "goingson".into(), |
| 185 |
version: "0.4.1".into(), |
| 186 |
tag: "v0.4.1".into(), |
| 187 |
git_sha: "a".repeat(40), |
| 188 |
target: "linux/x86_64".into(), |
| 189 |
build_host: "fw13".into(), |
| 190 |
toolchain: "rustc 1.97.0".into(), |
| 191 |
built_at: at(), |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
fn gate(name: &str, verdict: Verdict) -> GateRecord { |
| 196 |
GateRecord::new(name, Scope::Artifact, verdict, "ok", at()) |
| 197 |
} |
| 198 |
|
| 199 |
fn record() -> ArtifactRecord { |
| 200 |
ArtifactRecord::new( |
| 201 |
"bento", |
| 202 |
manifest(), |
| 203 |
provenance(), |
| 204 |
vec![ |
| 205 |
gate("clippy", Verdict::Passed), |
| 206 |
gate("cargo_test", Verdict::Passed), |
| 207 |
], |
| 208 |
) |
| 209 |
.unwrap() |
| 210 |
} |
| 211 |
|
| 212 |
#[test] |
| 213 |
fn the_digest_is_the_manifests_and_the_record_round_trips() { |
| 214 |
let r = record(); |
| 215 |
assert_eq!(r.digest, manifest().digest()); |
| 216 |
assert!(r.all_gates_passed()); |
| 217 |
let back = ArtifactRecord::parse(&r.to_json()).unwrap(); |
| 218 |
assert_eq!(r, back); |
| 219 |
} |
| 220 |
|
| 221 |
#[test] |
| 222 |
fn a_record_whose_digest_does_not_match_its_manifest_is_refused() { |
| 223 |
|
| 224 |
|
| 225 |
let mut r = record(); |
| 226 |
r.digest = BundleDigest::parse(&"f".repeat(64)).unwrap(); |
| 227 |
assert!(matches!( |
| 228 |
r.validate().unwrap_err(), |
| 229 |
ContractError::DigestMismatch { .. } |
| 230 |
)); |
| 231 |
assert!(ArtifactRecord::parse(&serde_json::to_string(&r).unwrap()).is_err()); |
| 232 |
} |
| 233 |
|
| 234 |
#[test] |
| 235 |
fn a_builder_cannot_hand_over_environment_evidence() { |
| 236 |
let g = GateRecord::new( |
| 237 |
"boot_smoke", |
| 238 |
Scope::Environment { |
| 239 |
env: "tier:a".into(), |
| 240 |
}, |
| 241 |
Verdict::Passed, |
| 242 |
"served /health", |
| 243 |
at(), |
| 244 |
); |
| 245 |
let err = ArtifactRecord::new("bento", manifest(), provenance(), vec![g]).unwrap_err(); |
| 246 |
assert!( |
| 247 |
matches!(err, ContractError::EnvironmentEvidenceFromBuilder { gate, env } |
| 248 |
if gate == "boot_smoke" && env == "tier:a") |
| 249 |
); |
| 250 |
} |
| 251 |
|
| 252 |
#[test] |
| 253 |
fn a_failing_gate_does_not_invalidate_the_record() { |
| 254 |
|
| 255 |
|
| 256 |
let r = ArtifactRecord::new( |
| 257 |
"bento", |
| 258 |
manifest(), |
| 259 |
provenance(), |
| 260 |
vec![gate("cargo_test", Verdict::Failed)], |
| 261 |
) |
| 262 |
.unwrap(); |
| 263 |
assert!(!r.all_gates_passed()); |
| 264 |
} |
| 265 |
|
| 266 |
#[test] |
| 267 |
fn a_future_contract_version_is_refused_rather_than_guessed_at() { |
| 268 |
let mut r = record(); |
| 269 |
r.contract = CONTRACT_VERSION + 1; |
| 270 |
assert!(matches!( |
| 271 |
r.validate().unwrap_err(), |
| 272 |
ContractError::UnknownContractVersion(_) |
| 273 |
)); |
| 274 |
} |
| 275 |
|
| 276 |
#[test] |
| 277 |
fn evidence_names_the_bundle_it_is_about_and_may_be_environment_scoped() { |
| 278 |
let e = EvidenceRecord::new( |
| 279 |
"sando", |
| 280 |
manifest().digest(), |
| 281 |
vec![GateRecord::new( |
| 282 |
"burn_in", |
| 283 |
Scope::Environment { |
| 284 |
env: "tier:a".into(), |
| 285 |
}, |
| 286 |
Verdict::Blocked, |
| 287 |
"12 hours remaining of 48", |
| 288 |
at(), |
| 289 |
)], |
| 290 |
) |
| 291 |
.unwrap(); |
| 292 |
let back = EvidenceRecord::parse(&e.to_json()).unwrap(); |
| 293 |
assert_eq!(back.subject, manifest().digest()); |
| 294 |
assert_eq!(back, e); |
| 295 |
} |
| 296 |
|
| 297 |
#[test] |
| 298 |
fn appending_evidence_does_not_touch_the_handover() { |
| 299 |
|
| 300 |
|
| 301 |
let handover = record(); |
| 302 |
let _ = EvidenceRecord::new("sando", handover.digest.clone(), vec![]).unwrap(); |
| 303 |
assert_eq!(handover, record()); |
| 304 |
assert_eq!(handover.digest, manifest().digest()); |
| 305 |
} |
| 306 |
} |
| 307 |
|