| 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 | 70× | fn refs(v: &[ObjectRef]) -> Value { |
| 31 | 70× | Value::Array(v.iter().map(|h| Value::Bytes(h.0.to_vec())).collect()) |
| 32 | 70× | } |
| 33 | |
|
| 34 | 26× | fn refs_from(v: &Value, what: &str) -> Result<Vec<ObjectRef>, ObjectError> { |
| 35 | 26× | array(v, what)?.iter().map(|h| oref(h, what)).collect() |
| 36 | 26× | } |
| 37 | |
|
| 38 | | impl Receipt { |
| 39 | 2× | pub fn passed(&self) -> bool { |
| 40 | 2× | self.exit_code == 0 |
| 41 | 2× | } |
| 42 | |
|
| 43 | 35× | pub fn to_value(&self) -> Value { |
| 44 | 35× | Value::map([ |
| 45 | 35× | (0, Value::Uint(1)), |
| 46 | 35× | (1, Value::Text(self.job_name.clone())), |
| 47 | 35× | (2, Value::Bytes(self.pipeline_hash.0.to_vec())), |
| 48 | 35× | (3, Value::Bytes(self.scope_tree_hash.0.to_vec())), |
| 49 | 35× | (4, refs(&self.inputs)), |
| 50 | 35× | (5, Value::Bytes(self.environment.0.to_vec())), |
| 51 | 35× | (6, refs(&self.outputs)), |
| 52 | 35× | (7, Value::Uint(self.exit_code as u64)), |
| 53 | 35× | (8, Value::Uint(self.duration_ms)), |
| 54 | 35× | ]) |
| 55 | 35× | } |
| 56 | |
|
| 57 | 35× | pub fn encode(&self) -> Vec<u8> { |
| 58 | 35× | cbor::encode(&self.to_value()) |
| 59 | 35× | } |
| 60 | |
|
| 61 | 25× | pub fn hash(&self) -> ObjectRef { |
| 62 | 25× | hash_object(ObjectType::Receipt, &self.encode()) |
| 63 | 25× | } |
| 64 | |
|
| 65 | 13× | pub fn decode(b: &[u8]) -> Result<Receipt, ObjectError> { |
| 66 | 13× | let v = cbor::decode(b)?; |
| 67 | 13× | let m = map(&v, "receipt")?; |
| 68 | 13× | check_version(m)?; |
| 69 | 13× | let exit_code = uint(field(m, 7, "exit_code")?, "exit_code")?; |
| 70 | | Ok(Receipt { |
| 71 | 13× | job_name: text(field(m, 1, "job_name")?, "job_name")?.to_owned(), |
| 72 | 13× | pipeline_hash: oref(field(m, 2, "pipeline_hash")?, "pipeline_hash")?, |
| 73 | 13× | scope_tree_hash: oref(field(m, 3, "scope_tree_hash")?, "scope_tree_hash")?, |
| 74 | 13× | inputs: refs_from(field(m, 4, "inputs")?, "inputs")?, |
| 75 | 13× | environment: oref(field(m, 5, "environment")?, "environment")?, |
| 76 | 13× | outputs: refs_from(field(m, 6, "outputs")?, "outputs")?, |
| 77 | 13× | exit_code: u32::try_from(exit_code) |
| 78 | 13× | .map_err(|_| ObjectError::Schema("exit_code: expected u32".into()))?, |
| 79 | 13× | duration_ms: uint(field(m, 8, "duration_ms")?, "duration_ms")?, |
| 80 | | }) |
| 81 | 13× | } |
| 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("{0}")] |
| 95 | | Schema(String), |
| 96 | | } |
| 97 | |
|
| 98 | | impl From<ObjectError> for ReceiptError { |
| 99 | 0× | fn from(e: ObjectError) -> Self { |
| 100 | 0× | ReceiptError::Schema(e.to_string()) |
| 101 | 0× | } |
| 102 | | } |
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 108 | | pub struct SignedReceipt { |
| 109 | | pub receipt: Receipt, |
| 110 | | pub attest: Attest, |
| 111 | | pub sigs: Vec<Signature>, |
| 112 | | pub runner: Identity, |
| 113 | | } |
| 114 | |
|
| 115 | | impl SignedReceipt { |
| 116 | | |
| 117 | | |
| 118 | 4× | pub fn sign( |
| 119 | 4× | receipt: Receipt, |
| 120 | 4× | repo: [u8; 16], |
| 121 | 4× | state: ObjectRef, |
| 122 | 4× | run: &str, |
| 123 | 4× | runner: &Identity, |
| 124 | 4× | sk: &SecretKey, |
| 125 | 4× | ) -> SignedReceipt { |
| 126 | 4× | let attest = Attest { |
| 127 | 4× | repo, |
| 128 | 4× | hlc: Hlc::now(), |
| 129 | 4× | body: AttestBody::Receipt { |
| 130 | 4× | receipt: receipt.hash(), |
| 131 | 4× | state, |
| 132 | 4× | run: run.into(), |
| 133 | 4× | }, |
| 134 | 4× | }; |
| 135 | 4× | let sig = sk.sign(Domain::Attest, &runner.hash(), &attest.hash()); |
| 136 | 4× | SignedReceipt { |
| 137 | 4× | receipt, |
| 138 | 4× | attest, |
| 139 | 4× | sigs: vec![sig], |
| 140 | 4× | runner: runner.clone(), |
| 141 | 4× | } |
| 142 | 4× | } |
| 143 | |
|
| 144 | | |
| 145 | 7× | pub fn state(&self) -> ObjectRef { |
| 146 | 7× | match &self.attest.body { |
| 147 | 7× | AttestBody::Receipt { state, .. } => *state, |
| 148 | 0× | _ => ObjectRef([0; 32]), |
| 149 | | } |
| 150 | 7× | } |
| 151 | |
|
| 152 | 6× | pub fn run(&self) -> &str { |
| 153 | 6× | match &self.attest.body { |
| 154 | 6× | AttestBody::Receipt { run, .. } => run, |
| 155 | 0× | _ => "", |
| 156 | | } |
| 157 | 6× | } |
| 158 | |
|
| 159 | | |
| 160 | | |
| 161 | 16× | pub fn verify(&self) -> Result<(), ReceiptError> { |
| 162 | 16× | let AttestBody::Receipt { receipt, .. } = &self.attest.body else { |
| 163 | 0× | return Err(ReceiptError::NotReceipt); |
| 164 | | }; |
| 165 | 16× | if *receipt != self.receipt.hash() { |
| 166 | 4× | return Err(ReceiptError::WrongReceipt); |
| 167 | 12× | } |
| 168 | 12× | let key = self.runner.hash(); |
| 169 | 12× | let target = self.attest.hash(); |
| 170 | 12× | let sig = self |
| 171 | 12× | .sigs |
| 172 | 12× | .iter() |
| 173 | 12× | .find(|s| s.key == key) |
| 174 | 12× | .ok_or_else(|| ReceiptError::Unsigned(key.short(12)))?; |
| 175 | 11× | verify(&self.runner, sig, Domain::Attest, &target) |
| 176 | 11× | .map_err(|e| ReceiptError::BadSignature(e.to_string())) |
| 177 | 16× | } |
| 178 | |
|
| 179 | | |
| 180 | 5× | pub fn encode(&self) -> Vec<u8> { |
| 181 | 5× | cbor::encode(&Value::Array(vec![ |
| 182 | 5× | Value::Bytes(self.receipt.encode()), |
| 183 | 5× | Value::Bytes(self.attest.encode()), |
| 184 | 5× | Value::Array(self.sigs.iter().map(|s| s.to_value()).collect()), |
| 185 | 5× | Value::Bytes(self.runner.encode()), |
| 186 | | ])) |
| 187 | 5× | } |
| 188 | |
|
| 189 | 5× | pub fn decode(b: &[u8]) -> Result<SignedReceipt, ReceiptError> { |
| 190 | 5× | let v = cbor::decode(b).map_err(ObjectError::from)?; |
| 191 | 5× | let a = array(&v, "signed receipt")?; |
| 192 | 5× | if a.len() != 4 { |
| 193 | 0× | return Err(ReceiptError::Schema( |
| 194 | 0× | "signed receipt: expected [receipt, attest, sigs, identity]".into(), |
| 195 | 0× | )); |
| 196 | 5× | } |
| 197 | | Ok(SignedReceipt { |
| 198 | 5× | receipt: Receipt::decode(bytes(&a[0], "receipt")?)?, |
| 199 | 5× | attest: Attest::decode(bytes(&a[1], "attest")?)?, |
| 200 | 5× | sigs: array(&a[2], "sigs")? |
| 201 | 5× | .iter() |
| 202 | 5× | .map(Signature::from_value) |
| 203 | 5× | .collect::<Result<_, _>>()?, |
| 204 | 5× | runner: Identity::decode(bytes(&a[3], "identity")?)?, |
| 205 | | }) |
| 206 | 5× | } |
| 207 | | } |
| 208 | |
|
| 209 | | #[cfg(test)] |
| 210 | | mod tests { |
| 211 | | use super::*; |
| 212 | |
|
| 213 | 2× | pub(crate) fn sample() -> Receipt { |
| 214 | 2× | Receipt { |
| 215 | 2× | job_name: "test".into(), |
| 216 | 2× | pipeline_hash: ObjectRef([1; 32]), |
| 217 | 2× | scope_tree_hash: ObjectRef([2; 32]), |
| 218 | 2× | inputs: vec![], |
| 219 | 2× | environment: ObjectRef([3; 32]), |
| 220 | 2× | outputs: vec![ObjectRef([4; 32])], |
| 221 | 2× | exit_code: 0, |
| 222 | 2× | duration_ms: 1500, |
| 223 | 2× | } |
| 224 | 2× | } |
| 225 | |
|
| 226 | | #[test] |
| 227 | 1× | fn receipts_round_trip_and_hash_as_receipt_objects() { |
| 228 | 1× | let r = sample(); |
| 229 | 1× | assert_eq!(Receipt::decode(&r.encode()).unwrap(), r); |
| 230 | 1× | let mut h = blake3::Hasher::new(); |
| 231 | 1× | h.update(format!("oxy1 receipt {}\0", r.encode().len()).as_bytes()); |
| 232 | 1× | h.update(&r.encode()); |
| 233 | 1× | assert_eq!( |
| 234 | 1× | r.hash().0, |
| 235 | 1× | *h.finalize().as_bytes(), |
| 236 | | "OxygenFlow's receipt hash" |
| 237 | | ); |
| 238 | 1× | assert!(r.passed()); |
| 239 | 1× | } |
| 240 | |
|
| 241 | | #[test] |
| 242 | 1× | fn a_signed_receipt_verifies_and_tampering_does_not() { |
| 243 | 1× | let sk = SecretKey::generate(); |
| 244 | 1× | let runner = sk.identity("earth-1", "runner@earth", 1); |
| 245 | 1× | let sr = SignedReceipt::sign( |
| 246 | 1× | sample(), |
| 247 | 1× | [7; 16], |
| 248 | 1× | ObjectRef([9; 32]), |
| 249 | 1× | "andy/demo#3", |
| 250 | 1× | &runner, |
| 251 | 1× | &sk, |
| 252 | | ); |
| 253 | 1× | sr.verify().unwrap(); |
| 254 | 1× | let back = SignedReceipt::decode(&sr.encode()).unwrap(); |
| 255 | 1× | assert_eq!(back, sr); |
| 256 | 1× | assert_eq!(back.state(), ObjectRef([9; 32])); |
| 257 | 1× | assert_eq!(back.run(), "andy/demo#3"); |
| 258 | |
|
| 259 | 1× | let mut forged = sr.clone(); |
| 260 | 1× | forged.receipt.exit_code = 0; |
| 261 | 1× | forged.receipt.duration_ms = 1; |
| 262 | 1× | assert_eq!(forged.verify(), Err(ReceiptError::WrongReceipt)); |
| 263 | |
|
| 264 | 1× | let mut moved = sr.clone(); |
| 265 | 1× | if let AttestBody::Receipt { state, .. } = &mut moved.attest.body { |
| 266 | 1× | *state = ObjectRef([8; 32]); |
| 267 | 1× | } |
| 268 | 1× | assert!(matches!(moved.verify(), Err(ReceiptError::BadSignature(_)))); |
| 269 | |
|
| 270 | 1× | let other = SecretKey::generate().identity("mallory", "m@x", 1); |
| 271 | 1× | let mut swapped = sr.clone(); |
| 272 | 1× | swapped.runner = other; |
| 273 | 1× | assert!(matches!(swapped.verify(), Err(ReceiptError::Unsigned(_)))); |
| 274 | 1× | } |
| 275 | | } |