| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | | use crate::attest::{Attest, AttestBody}; |
| 7 | | use crate::cbor::{self, Value}; |
| 8 | | use crate::hash::{hash_object, ObjectRef, ObjectType}; |
| 9 | | use crate::identity::{verify, Domain, Identity, SecretKey, Signature}; |
| 10 | | use crate::objects::{array, bytes, check_version, field, map, oref, text, uint, Hlc, ObjectError}; |
| 11 | |
|
| 12 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 13 | | pub struct Receipt { |
| 14 | | pub job_name: String, |
| 15 | | |
| 16 | | pub pipeline_hash: ObjectRef, |
| 17 | | |
| 18 | | |
| 19 | | pub scope_tree_hash: ObjectRef, |
| 20 | | |
| 21 | | pub inputs: Vec<ObjectRef>, |
| 22 | | |
| 23 | | pub environment: ObjectRef, |
| 24 | | |
| 25 | | pub outputs: Vec<ObjectRef>, |
| 26 | | pub exit_code: u32, |
| 27 | | pub duration_ms: u64, |
| 28 | | } |
| 29 | |
|
| 30 | 106× | fn refs(v: &[ObjectRef]) -> Value { |
| 31 | 106× | Value::Array(v.iter().map(|h| Value::Bytes(h.0.to_vec())).collect()) |
| 32 | 106× | } |
| 33 | |
|
| 34 | 44× | fn refs_from(v: &Value, what: &str) -> Result<Vec<ObjectRef>, ObjectError> { |
| 35 | 44× | array(v, what)?.iter().map(|h| oref(h, what)).collect() |
| 36 | 44× | } |
| 37 | |
|
| 38 | | impl Receipt { |
| 39 | 2× | pub fn passed(&self) -> bool { |
| 40 | 2× | self.exit_code == 0 |
| 41 | 2× | } |
| 42 | |
|
| 43 | 53× | pub fn to_value(&self) -> Value { |
| 44 | 53× | Value::map([ |
| 45 | 53× | (0, Value::Uint(1)), |
| 46 | 53× | (1, Value::Text(self.job_name.clone())), |
| 47 | 53× | (2, Value::Bytes(self.pipeline_hash.0.to_vec())), |
| 48 | 53× | (3, Value::Bytes(self.scope_tree_hash.0.to_vec())), |
| 49 | 53× | (4, refs(&self.inputs)), |
| 50 | 53× | (5, Value::Bytes(self.environment.0.to_vec())), |
| 51 | 53× | (6, refs(&self.outputs)), |
| 52 | 53× | (7, Value::Uint(self.exit_code as u64)), |
| 53 | 53× | (8, Value::Uint(self.duration_ms)), |
| 54 | 53× | ]) |
| 55 | 53× | } |
| 56 | |
|
| 57 | 53× | pub fn encode(&self) -> Vec<u8> { |
| 58 | 53× | cbor::encode(&self.to_value()) |
| 59 | 53× | } |
| 60 | |
|
| 61 | 38× | pub fn hash(&self) -> ObjectRef { |
| 62 | 38× | hash_object(ObjectType::Receipt, &self.encode()) |
| 63 | 38× | } |
| 64 | |
|
| 65 | 22× | pub fn decode(b: &[u8]) -> Result<Receipt, ObjectError> { |
| 66 | 22× | let v = cbor::decode(b)?; |
| 67 | 22× | let m = map(&v, "receipt")?; |
| 68 | 22× | check_version(m)?; |
| 69 | 22× | let exit_code = uint(field(m, 7, "exit_code")?, "exit_code")?; |
| 70 | | Ok(Receipt { |
| 71 | 22× | job_name: text(field(m, 1, "job_name")?, "job_name")?.to_owned(), |
| 72 | 22× | pipeline_hash: oref(field(m, 2, "pipeline_hash")?, "pipeline_hash")?, |
| 73 | 22× | scope_tree_hash: oref(field(m, 3, "scope_tree_hash")?, "scope_tree_hash")?, |
| 74 | 22× | inputs: refs_from(field(m, 4, "inputs")?, "inputs")?, |
| 75 | 22× | environment: oref(field(m, 5, "environment")?, "environment")?, |
| 76 | 22× | outputs: refs_from(field(m, 6, "outputs")?, "outputs")?, |
| 77 | 22× | exit_code: u32::try_from(exit_code) |
| 78 | 22× | .map_err(|_| ObjectError::Schema("exit_code: expected u32".into()))?, |
| 79 | 22× | duration_ms: uint(field(m, 8, "duration_ms")?, "duration_ms")?, |
| 80 | | }) |
| 81 | 22× | } |
| 82 | | } |
| 83 | |
|
| 84 | | #[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] |
| 85 | | pub enum ReceiptError { |
| 86 | | #[error("the attestation is not of kind receipt")] |
| 87 | | NotReceipt, |
| 88 | | #[error("the attestation names a different receipt")] |
| 89 | | WrongReceipt, |
| 90 | | #[error("no signature by the runner {0}")] |
| 91 | | Unsigned(String), |
| 92 | | #[error("the runner's signature does not verify: {0}")] |
| 93 | | BadSignature(String), |
| 94 | | #[error("its certificate does not hold: {0}")] |
| 95 | | Certificate(String), |
| 96 | | #[error("{0}")] |
| 97 | | Schema(String), |
| 98 | | } |
| 99 | |
|
| 100 | | impl From<ObjectError> for ReceiptError { |
| 101 | 0× | fn from(e: ObjectError) -> Self { |
| 102 | 0× | ReceiptError::Schema(e.to_string()) |
| 103 | 0× | } |
| 104 | | } |
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 112 | | pub struct SignedReceipt { |
| 113 | | pub receipt: Receipt, |
| 114 | | pub attest: Attest, |
| 115 | | pub sigs: Vec<Signature>, |
| 116 | | pub runner: Identity, |
| 117 | | pub certificate: Option<CertifiedBy>, |
| 118 | | } |
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 123 | | pub struct CertifiedBy { |
| 124 | | pub cert: crate::cert::SignedCertificate, |
| 125 | | pub issuer: Identity, |
| 126 | | } |
| 127 | |
|
| 128 | | impl SignedReceipt { |
| 129 | | |
| 130 | | |
| 131 | 6× | pub fn sign( |
| 132 | 6× | receipt: Receipt, |
| 133 | 6× | repo: [u8; 16], |
| 134 | 6× | state: ObjectRef, |
| 135 | 6× | run: &str, |
| 136 | 6× | runner: &Identity, |
| 137 | 6× | sk: &SecretKey, |
| 138 | 6× | ) -> SignedReceipt { |
| 139 | 6× | let attest = Attest { |
| 140 | 6× | repo, |
| 141 | 6× | hlc: Hlc::now(), |
| 142 | 6× | body: AttestBody::Receipt { |
| 143 | 6× | receipt: receipt.hash(), |
| 144 | 6× | state, |
| 145 | 6× | run: run.into(), |
| 146 | 6× | }, |
| 147 | 6× | }; |
| 148 | 6× | let sig = sk.sign(Domain::Attest, &runner.hash(), &attest.hash()); |
| 149 | 6× | SignedReceipt { |
| 150 | 6× | receipt, |
| 151 | 6× | attest, |
| 152 | 6× | sigs: vec![sig], |
| 153 | 6× | runner: runner.clone(), |
| 154 | 6× | certificate: None, |
| 155 | 6× | } |
| 156 | 6× | } |
| 157 | |
|
| 158 | | |
| 159 | | |
| 160 | 6× | pub fn with_certificate( |
| 161 | 6× | mut self, |
| 162 | 6× | cert: crate::cert::SignedCertificate, |
| 163 | 6× | issuer: Identity, |
| 164 | 6× | ) -> SignedReceipt { |
| 165 | 6× | self.certificate = Some(CertifiedBy { cert, issuer }); |
| 166 | 6× | self |
| 167 | 6× | } |
| 168 | |
|
| 169 | | |
| 170 | 10× | pub fn state(&self) -> ObjectRef { |
| 171 | 10× | match &self.attest.body { |
| 172 | 10× | AttestBody::Receipt { state, .. } => *state, |
| 173 | 0× | _ => ObjectRef([0; 32]), |
| 174 | | } |
| 175 | 10× | } |
| 176 | |
|
| 177 | 7× | pub fn run(&self) -> &str { |
| 178 | 7× | match &self.attest.body { |
| 179 | 7× | AttestBody::Receipt { run, .. } => run, |
| 180 | 0× | _ => "", |
| 181 | | } |
| 182 | 7× | } |
| 183 | |
|
| 184 | | |
| 185 | | |
| 186 | 27× | pub fn verify(&self) -> Result<(), ReceiptError> { |
| 187 | 27× | let AttestBody::Receipt { receipt, .. } = &self.attest.body else { |
| 188 | 0× | return Err(ReceiptError::NotReceipt); |
| 189 | | }; |
| 190 | 27× | if *receipt != self.receipt.hash() { |
| 191 | 4× | return Err(ReceiptError::WrongReceipt); |
| 192 | 23× | } |
| 193 | 23× | let key = self.runner.hash(); |
| 194 | 23× | let target = self.attest.hash(); |
| 195 | 23× | let sig = self |
| 196 | 23× | .sigs |
| 197 | 23× | .iter() |
| 198 | 23× | .find(|s| s.key == key) |
| 199 | 23× | .ok_or_else(|| ReceiptError::Unsigned(key.short(12)))?; |
| 200 | 22× | verify(&self.runner, sig, Domain::Attest, &target) |
| 201 | 22× | .map_err(|e| ReceiptError::BadSignature(e.to_string()))?; |
| 202 | 21× | if let Some(c) = &self.certificate { |
| 203 | 8× | c.cert |
| 204 | 8× | .verify(&c.issuer) |
| 205 | 8× | .map_err(|e| ReceiptError::Certificate(e.to_string()))?; |
| 206 | 7× | if c.cert.cert.key_id() != key { |
| 207 | 1× | return Err(ReceiptError::Certificate( |
| 208 | 1× | "it certifies another key than the one that signed".into(), |
| 209 | 1× | )); |
| 210 | 6× | } |
| 211 | | |
| 212 | | |
| 213 | 6× | if !c |
| 214 | 6× | .cert |
| 215 | 6× | .cert |
| 216 | 6× | .covers(crate::identity::Scope::SEAL, self.attest.hlc.wall_ms) |
| 217 | | { |
| 218 | 2× | return Err(ReceiptError::Certificate( |
| 219 | 2× | "the receipt was signed outside the certificate's window, or it gives no seal scope".into(), |
| 220 | 2× | )); |
| 221 | 4× | } |
| 222 | 13× | } |
| 223 | 17× | Ok(()) |
| 224 | 27× | } |
| 225 | |
|
| 226 | | |
| 227 | | |
| 228 | | |
| 229 | | |
| 230 | 9× | pub fn encode(&self) -> Vec<u8> { |
| 231 | 9× | let mut parts = vec![ |
| 232 | 9× | Value::Bytes(self.receipt.encode()), |
| 233 | 9× | Value::Bytes(self.attest.encode()), |
| 234 | 9× | Value::Array(self.sigs.iter().map(|s| s.to_value()).collect()), |
| 235 | 9× | Value::Bytes(self.runner.encode()), |
| 236 | | ]; |
| 237 | 9× | if let Some(c) = &self.certificate { |
| 238 | 1× | parts.push(Value::Array(vec![ |
| 239 | 1× | Value::Bytes(c.cert.encode()), |
| 240 | 1× | Value::Bytes(c.issuer.encode()), |
| 241 | 1× | ])); |
| 242 | 8× | } |
| 243 | 9× | cbor::encode(&Value::Array(parts)) |
| 244 | 9× | } |
| 245 | |
|
| 246 | 7× | pub fn decode(b: &[u8]) -> Result<SignedReceipt, ReceiptError> { |
| 247 | 7× | let v = cbor::decode(b).map_err(ObjectError::from)?; |
| 248 | 7× | let a = array(&v, "signed receipt")?; |
| 249 | 7× | if a.len() != 4 && a.len() != 5 { |
| 250 | 0× | return Err(ReceiptError::Schema( |
| 251 | 0× | "signed receipt: expected [receipt, attest, sigs, identity, (certificate, issuer)]" |
| 252 | 0× | .into(), |
| 253 | 0× | )); |
| 254 | 7× | } |
| 255 | 7× | let certificate = match a.get(4) { |
| 256 | 6× | None => None, |
| 257 | 1× | Some(c) => { |
| 258 | 1× | let c = array(c, "certificate")?; |
| 259 | 1× | if c.len() != 2 { |
| 260 | 0× | return Err(ReceiptError::Schema( |
| 261 | 0× | "signed receipt: expected [certificate, issuer identity]".into(), |
| 262 | 0× | )); |
| 263 | 1× | } |
| 264 | | Some(CertifiedBy { |
| 265 | 1× | cert: crate::cert::SignedCertificate::decode(bytes(&c[0], "certificate")?) |
| 266 | 1× | .map_err(|e| ReceiptError::Certificate(e.to_string()))?, |
| 267 | 1× | issuer: Identity::decode(bytes(&c[1], "issuer")?)?, |
| 268 | | }) |
| 269 | | } |
| 270 | | }; |
| 271 | | Ok(SignedReceipt { |
| 272 | 7× | receipt: Receipt::decode(bytes(&a[0], "receipt")?)?, |
| 273 | 7× | attest: Attest::decode(bytes(&a[1], "attest")?)?, |
| 274 | 7× | sigs: array(&a[2], "sigs")? |
| 275 | 7× | .iter() |
| 276 | 7× | .map(Signature::from_value) |
| 277 | 7× | .collect::<Result<_, _>>()?, |
| 278 | 7× | runner: Identity::decode(bytes(&a[3], "identity")?)?, |
| 279 | 7× | certificate, |
| 280 | | }) |
| 281 | 7× | } |
| 282 | | } |
| 283 | |
|
| 284 | | #[cfg(test)] |
| 285 | | mod tests { |
| 286 | | use super::*; |
| 287 | |
|
| 288 | 3× | pub(crate) fn sample() -> Receipt { |
| 289 | 3× | Receipt { |
| 290 | 3× | job_name: "test".into(), |
| 291 | 3× | pipeline_hash: ObjectRef([1; 32]), |
| 292 | 3× | scope_tree_hash: ObjectRef([2; 32]), |
| 293 | 3× | inputs: vec![], |
| 294 | 3× | environment: ObjectRef([3; 32]), |
| 295 | 3× | outputs: vec![ObjectRef([4; 32])], |
| 296 | 3× | exit_code: 0, |
| 297 | 3× | duration_ms: 1500, |
| 298 | 3× | } |
| 299 | 3× | } |
| 300 | |
|
| 301 | | #[test] |
| 302 | 1× | fn receipts_round_trip_and_hash_as_receipt_objects() { |
| 303 | 1× | let r = sample(); |
| 304 | 1× | assert_eq!(Receipt::decode(&r.encode()).unwrap(), r); |
| 305 | 1× | let mut h = blake3::Hasher::new(); |
| 306 | 1× | h.update(format!("oxy1 receipt {}\0", r.encode().len()).as_bytes()); |
| 307 | 1× | h.update(&r.encode()); |
| 308 | 1× | assert_eq!( |
| 309 | 1× | r.hash().0, |
| 310 | 1× | *h.finalize().as_bytes(), |
| 311 | | "OxygenFlow's receipt hash" |
| 312 | | ); |
| 313 | 1× | assert!(r.passed()); |
| 314 | 1× | } |
| 315 | |
|
| 316 | | #[test] |
| 317 | 1× | fn a_signed_receipt_verifies_and_tampering_does_not() { |
| 318 | 1× | let sk = SecretKey::generate(); |
| 319 | 1× | let runner = sk.identity("earth-1", "runner@earth", 1); |
| 320 | 1× | let sr = SignedReceipt::sign( |
| 321 | 1× | sample(), |
| 322 | 1× | [7; 16], |
| 323 | 1× | ObjectRef([9; 32]), |
| 324 | 1× | "andy/demo#3", |
| 325 | 1× | &runner, |
| 326 | 1× | &sk, |
| 327 | | ); |
| 328 | 1× | sr.verify().unwrap(); |
| 329 | 1× | let back = SignedReceipt::decode(&sr.encode()).unwrap(); |
| 330 | 1× | assert_eq!(back, sr); |
| 331 | 1× | assert_eq!(back.state(), ObjectRef([9; 32])); |
| 332 | 1× | assert_eq!(back.run(), "andy/demo#3"); |
| 333 | |
|
| 334 | 1× | let mut forged = sr.clone(); |
| 335 | 1× | forged.receipt.exit_code = 0; |
| 336 | 1× | forged.receipt.duration_ms = 1; |
| 337 | 1× | assert_eq!(forged.verify(), Err(ReceiptError::WrongReceipt)); |
| 338 | |
|
| 339 | 1× | let mut moved = sr.clone(); |
| 340 | 1× | if let AttestBody::Receipt { state, .. } = &mut moved.attest.body { |
| 341 | 1× | *state = ObjectRef([8; 32]); |
| 342 | 1× | } |
| 343 | 1× | assert!(matches!(moved.verify(), Err(ReceiptError::BadSignature(_)))); |
| 344 | |
|
| 345 | 1× | let other = SecretKey::generate().identity("mallory", "m@x", 1); |
| 346 | 1× | let mut swapped = sr.clone(); |
| 347 | 1× | swapped.runner = other; |
| 348 | 1× | assert!(matches!(swapped.verify(), Err(ReceiptError::Unsigned(_)))); |
| 349 | 1× | } |
| 350 | |
|
| 351 | | |
| 352 | | |
| 353 | | |
| 354 | | |
| 355 | | #[test] |
| 356 | 1× | fn a_receipt_by_a_certified_key_carries_and_checks_its_certificate() { |
| 357 | | use crate::cert::{Certificate, SignedCertificate}; |
| 358 | | use crate::identity::Scope; |
| 359 | 1× | let hub_sk = SecretKey::generate(); |
| 360 | 1× | let hub = hub_sk.identity("oxygenhub.io", "[email protected]", 1); |
| 361 | 1× | let job_sk = SecretKey::generate(); |
| 362 | 1× | let job = job_sk.identity("job 7 test", "[email protected]", 1); |
| 363 | 5× | let certify = |key: &Identity, scope: Scope, from: u64, to: u64| { |
| 364 | 5× | SignedCertificate::sign( |
| 365 | 5× | Certificate { |
| 366 | 5× | key: key.clone(), |
| 367 | 5× | issuer: hub.hash(), |
| 368 | 5× | subject: "repo:andy/demo:bookmark:main".into(), |
| 369 | 5× | claims: vec![("job".into(), "test".into())], |
| 370 | 5× | scope, |
| 371 | 5× | not_before: from, |
| 372 | 5× | expires: to, |
| 373 | 5× | grant: None, |
| 374 | 5× | }, |
| 375 | 5× | &hub, |
| 376 | 5× | &hub_sk, |
| 377 | | ) |
| 378 | 5× | }; |
| 379 | 1× | let sr = SignedReceipt::sign( |
| 380 | 1× | sample(), |
| 381 | 1× | [7; 16], |
| 382 | 1× | ObjectRef([9; 32]), |
| 383 | 1× | "andy/demo#7", |
| 384 | 1× | &job, |
| 385 | 1× | &job_sk, |
| 386 | | ); |
| 387 | 1× | let now = sr.attest.hlc.wall_ms; |
| 388 | 1× | let good = sr.clone().with_certificate( |
| 389 | 1× | certify(&job, Scope::SEAL, now - 1000, now + 600_000), |
| 390 | 1× | hub.clone(), |
| 391 | | ); |
| 392 | 1× | good.verify().unwrap(); |
| 393 | 1× | let back = SignedReceipt::decode(&good.encode()).unwrap(); |
| 394 | 1× | assert_eq!(back, good); |
| 395 | 1× | assert_eq!( |
| 396 | 1× | SignedReceipt::decode(&sr.encode()).unwrap().certificate, |
| 397 | | None, |
| 398 | | "a plain receipt reads as before" |
| 399 | | ); |
| 400 | |
|
| 401 | 1× | let other = SecretKey::generate().identity("other", "o@x", 1); |
| 402 | 1× | let wrong_key = sr.clone().with_certificate( |
| 403 | 1× | certify(&other, Scope::SEAL, now - 1000, now + 600_000), |
| 404 | 1× | hub.clone(), |
| 405 | | ); |
| 406 | 1× | assert!( |
| 407 | 1× | matches!(wrong_key.verify(), Err(ReceiptError::Certificate(m)) if m.contains("another key")) |
| 408 | | ); |
| 409 | 1× | let no_seal = sr.clone().with_certificate( |
| 410 | 1× | certify(&job, Scope::CHANGES, now - 1000, now + 600_000), |
| 411 | 1× | hub.clone(), |
| 412 | | ); |
| 413 | 1× | assert!(matches!( |
| 414 | 1× | no_seal.verify(), |
| 415 | | Err(ReceiptError::Certificate(_)) |
| 416 | | )); |
| 417 | 1× | let too_late = sr.clone().with_certificate( |
| 418 | 1× | certify(&job, Scope::SEAL, now - 700_000, now - 100_000), |
| 419 | 1× | hub.clone(), |
| 420 | | ); |
| 421 | 1× | assert!( |
| 422 | 1× | matches!(too_late.verify(), Err(ReceiptError::Certificate(m)) if m.contains("window")) |
| 423 | | ); |
| 424 | 1× | let wrong_issuer = sr |
| 425 | 1× | .clone() |
| 426 | 1× | .with_certificate(certify(&job, Scope::SEAL, now - 1000, now + 600_000), other); |
| 427 | 1× | assert!(matches!( |
| 428 | 1× | wrong_issuer.verify(), |
| 429 | | Err(ReceiptError::Certificate(_)) |
| 430 | | )); |
| 431 | 1× | } |
| 432 | | } |