| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | | use crate::pipeline::{Job, Pipeline, Sandbox}; |
| 7 | | use oxy_core::cbor::{encode, Value}; |
| 8 | |
|
| 9 | |
|
| 10 | | pub type Hash = [u8; 32]; |
| 11 | |
|
| 12 | |
|
| 13 | 47× | pub fn object_hash(ty: &str, body: &[u8]) -> Hash { |
| 14 | 47× | let mut h = blake3::Hasher::new(); |
| 15 | 47× | h.update(format!("oxy1 {ty} {}\0", body.len()).as_bytes()); |
| 16 | 47× | h.update(body); |
| 17 | 47× | *h.finalize().as_bytes() |
| 18 | 47× | } |
| 19 | |
|
| 20 | 386× | fn text(s: &str) -> Value { |
| 21 | 386× | Value::Text(s.to_string()) |
| 22 | 386× | } |
| 23 | |
|
| 24 | 155× | fn texts(v: &[String]) -> Value { |
| 25 | 155× | Value::Array(v.iter().map(|s| text(s)).collect()) |
| 26 | 155× | } |
| 27 | |
|
| 28 | 42× | fn hex32(s: &str) -> Value { |
| 29 | 42× | Value::Bytes(hex::decode(s).unwrap_or_default()) |
| 30 | 42× | } |
| 31 | |
|
| 32 | 42× | pub fn sandbox_value(s: &Sandbox) -> Value { |
| 33 | 42× | let mut fields = vec![ |
| 34 | 42× | (0, text(&s.name)), |
| 35 | 42× | (1, text(s.runtime.as_str())), |
| 36 | 42× | (2, hex32(&s.rootfs)), |
| 37 | 42× | (3, Value::Uint(s.memory_mib as u64)), |
| 38 | 42× | (4, Value::Uint(s.cpus as u64)), |
| 39 | 42× | (5, text(s.network.as_str())), |
| 40 | | ]; |
| 41 | | |
| 42 | | |
| 43 | 42× | if !s.env.is_empty() { |
| 44 | 2× | fields.push((6, Value::Array(s.env.iter().map(|(k, v)| Value::Array(vec![text(k), text(v)])).collect()))); |
| 45 | 41× | } |
| 46 | | |
| 47 | 42× | if let Some(a) = &s.arch { |
| 48 | 1× | fields.push((7, text(a))); |
| 49 | 41× | } |
| 50 | | |
| 51 | 42× | if !s.egress_allow.is_empty() { |
| 52 | 2× | fields.push((8, texts(&s.egress_allow))); |
| 53 | 40× | } |
| 54 | 42× | Value::map(fields) |
| 55 | 42× | } |
| 56 | |
|
| 57 | 51× | pub fn job_value(j: &Job) -> Value { |
| 58 | 51× | let mut fields = vec![ |
| 59 | 51× | (0, text(&j.name)), |
| 60 | 51× | (1, text(&j.sandbox)), |
| 61 | 51× | (2, texts(&j.scope)), |
| 62 | 51× | (3, texts(&j.depends_on)), |
| 63 | | |
| 64 | 51× | (4, Value::Array(j.env.iter().map(|(k, v)| Value::Array(vec![text(k), text(v)])).collect())), |
| 65 | 51× | (5, texts(&j.exec)), |
| 66 | | ]; |
| 67 | | |
| 68 | | |
| 69 | 51× | if !j.mounts.is_empty() { |
| 70 | 9× | fields.push(( |
| 71 | | 6, |
| 72 | | Value::Array( |
| 73 | 9× | j.mounts |
| 74 | 9× | .iter() |
| 75 | 14× | .map(|m| { |
| 76 | | |
| 77 | | |
| 78 | 14× | let mut v = vec![text(m.source.as_str()), text(&m.target), Value::Uint(m.size_mib as u64)]; |
| 79 | 14× | if let Some(c) = &m.cache { |
| 80 | 3× | v.push(text(c)); |
| 81 | 4× | v.push(Value::Array(m.key.iter().map(|k| text(k)).collect())); |
| 82 | 11× | } |
| 83 | 14× | Value::Array(v) |
| 84 | 14× | }) |
| 85 | 9× | .collect(), |
| 86 | | ), |
| 87 | | )); |
| 88 | 42× | } |
| 89 | | |
| 90 | | |
| 91 | 51× | if !j.outputs.is_empty() { |
| 92 | 8× | let output = |o: &crate::pipeline::Output| { |
| 93 | 8× | let mut v = vec![text(&o.name), text(&o.path)]; |
| 94 | 8× | if !o.kind.is_artifact() { |
| 95 | 3× | v.push(text(o.kind.word())); |
| 96 | 5× | } |
| 97 | 8× | Value::Array(v) |
| 98 | 8× | }; |
| 99 | 5× | fields.push((7, Value::Array(j.outputs.iter().map(output).collect()))); |
| 100 | 46× | } |
| 101 | | |
| 102 | 51× | if !j.secrets.is_empty() { |
| 103 | 3× | fields.push((8, Value::Array(j.secrets.iter().map(|s| Value::Array(vec![text(&s.name), text(&s.mount_path)])).collect()))); |
| 104 | 49× | } |
| 105 | | |
| 106 | 51× | if !j.identities.is_empty() { |
| 107 | 2× | fields.push((9, Value::Array(j.identities.iter().map(|i| Value::Array(vec![text(&i.audience), text(&i.mount_path)])).collect()))); |
| 108 | 50× | } |
| 109 | | |
| 110 | | |
| 111 | 51× | if let Some(a) = &j.approval { |
| 112 | 4× | fields.push((11, Value::Array(vec![text(&a.by), Value::Bool(a.others_only), Value::Uint(a.expires_s), Value::Bool(a.sealed)]))); |
| 113 | 47× | } |
| 114 | | |
| 115 | 51× | if let Some(e) = &j.environment { |
| 116 | 1× | fields.push((12, text(e))); |
| 117 | 50× | } |
| 118 | 51× | if !j.inputs.is_empty() { |
| 119 | 1× | fields.push(( |
| 120 | | 10, |
| 121 | 1× | Value::Array(j.inputs.iter().map(|i| Value::Array(vec![text(&i.artifact), text(&i.from), text(&i.path)])).collect()), |
| 122 | | )); |
| 123 | 50× | } |
| 124 | 51× | Value::map(fields) |
| 125 | 51× | } |
| 126 | |
|
| 127 | | impl Pipeline { |
| 128 | | |
| 129 | 34× | pub fn canonical(&self) -> Vec<u8> { |
| 130 | 34× | encode(&Value::map([ |
| 131 | 34× | (0, Value::Uint(self.schema_version as u64)), |
| 132 | 34× | (1, Value::Array(self.sandboxes.values().map(sandbox_value).collect())), |
| 133 | 34× | (2, Value::Array(self.jobs.values().map(job_value).collect())), |
| 134 | 34× | ])) |
| 135 | 34× | } |
| 136 | |
|
| 137 | | |
| 138 | 30× | pub fn hash(&self) -> Hash { |
| 139 | 30× | object_hash("pipeline", &self.canonical()) |
| 140 | 30× | } |
| 141 | | } |
| 142 | |
|
| 143 | | impl Job { |
| 144 | | |
| 145 | | |
| 146 | 8× | pub fn hash(&self, sandbox: &Sandbox) -> Hash { |
| 147 | 8× | object_hash("job", &encode(&Value::Array(vec![job_value(self), sandbox_value(sandbox)]))) |
| 148 | 8× | } |
| 149 | | } |
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | 5× | pub fn scope_tree_hash<'a>(scope: &[String], files: impl IntoIterator<Item = (&'a str, Hash)>) -> Hash { |
| 154 | 5× | let set = { |
| 155 | 5× | let mut b = globset::GlobSetBuilder::new(); |
| 156 | 5× | for g in scope { |
| 157 | 3× | if let Ok(g) = globset::Glob::new(g) { |
| 158 | 3× | b.add(g); |
| 159 | 3× | } |
| 160 | | } |
| 161 | 5× | b.build().unwrap_or_else(|_| globset::GlobSet::empty()) |
| 162 | | }; |
| 163 | 8× | let mut picked: Vec<(&str, Hash)> = files.into_iter().filter(|(p, _)| scope.is_empty() || set.is_match(p)).collect(); |
| 164 | 5× | picked.sort(); |
| 165 | 5× | let body = encode(&Value::Array(picked.iter().map(|(p, h)| Value::Array(vec![text(p), Value::Bytes(h.to_vec())])).collect())); |
| 166 | 5× | object_hash("scope", &body) |
| 167 | 5× | } |