| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | | use crate::attest::Attest; |
| 10 | | use crate::cbor::{self, Value}; |
| 11 | | use crate::hash::{ChangeHash, ObjectRef, ObjectType}; |
| 12 | | use crate::identity::Signature; |
| 13 | | use crate::objects::{self as o, Change, ObjectError}; |
| 14 | | use crate::policy::Policy; |
| 15 | | use crate::repo::{Repo, RepoError, Result}; |
| 16 | | use std::path::{Path, PathBuf}; |
| 17 | | use std::sync::{Arc, Mutex}; |
| 18 | |
|
| 19 | | pub const CONTENT_TYPE: &str = "application/oxy-cbor"; |
| 20 | | pub const PROTOCOL_VERSION: u64 = 1; |
| 21 | |
|
| 22 | |
|
| 23 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 24 | | pub struct SignedAttest { |
| 25 | | pub attest: Attest, |
| 26 | | pub sigs: Vec<Signature>, |
| 27 | | } |
| 28 | |
|
| 29 | |
|
| 30 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 31 | | pub struct SignedPolicy { |
| 32 | | pub policy: Policy, |
| 33 | | pub sigs: Vec<Signature>, |
| 34 | | } |
| 35 | |
|
| 36 | | impl SignedPolicy { |
| 37 | | |
| 38 | 1× | pub fn encode(&self) -> Vec<u8> { |
| 39 | 1× | crate::cbor::encode(&wire::policy_value(self)) |
| 40 | 1× | } |
| 41 | 1× | pub fn decode(b: &[u8]) -> Result<SignedPolicy> { |
| 42 | 1× | wire::policy_from( |
| 43 | 1× | &crate::cbor::decode(b).map_err(|e| RepoError::Object(ObjectError::from(e)))?, |
| 44 | | ) |
| 45 | 1× | } |
| 46 | | } |
| 47 | |
|
| 48 | |
|
| 49 | | #[derive(Clone, Debug, Default)] |
| 50 | | pub struct RefsResponse { |
| 51 | | pub refs: Vec<RemoteRef>, |
| 52 | | pub policy: Option<SignedPolicy>, |
| 53 | | pub tags: Vec<TagRef>, |
| 54 | | |
| 55 | | |
| 56 | | pub history: Vec<SignedPolicy>, |
| 57 | | |
| 58 | | pub default_bookmark: Option<String>, |
| 59 | | } |
| 60 | |
|
| 61 | |
|
| 62 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 63 | | pub struct TagRef { |
| 64 | | pub name: String, |
| 65 | | pub state: ObjectRef, |
| 66 | | pub heads: Vec<ChangeHash>, |
| 67 | | pub attestation: Option<SignedAttest>, |
| 68 | | } |
| 69 | |
|
| 70 | |
|
| 71 | | #[derive(Clone, Debug)] |
| 72 | | pub struct RemoteRef { |
| 73 | | pub name: String, |
| 74 | | pub state: ObjectRef, |
| 75 | | pub heads: Vec<ChangeHash>, |
| 76 | | |
| 77 | | |
| 78 | | pub attestation: Option<SignedAttest>, |
| 79 | | |
| 80 | | |
| 81 | | pub last_serial: Option<u64>, |
| 82 | | |
| 83 | | |
| 84 | | pub accepted: Option<SignedAttest>, |
| 85 | | } |
| 86 | |
|
| 87 | |
|
| 88 | | #[derive(Clone, Debug, Default)] |
| 89 | | pub struct Bundle { |
| 90 | | |
| 91 | | pub changes: Vec<(Change, Vec<Signature>)>, |
| 92 | | |
| 93 | | pub attests: Vec<SignedAttest>, |
| 94 | | |
| 95 | | pub objects: Vec<(ObjectType, Vec<u8>)>, |
| 96 | | |
| 97 | | |
| 98 | | pub policies: Vec<SignedPolicy>, |
| 99 | | |
| 100 | | |
| 101 | | |
| 102 | | pub hashes: Vec<ChangeHash>, |
| 103 | | |
| 104 | | |
| 105 | | |
| 106 | | pub raw_changes: Vec<(Vec<u8>, Vec<Signature>)>, |
| 107 | | |
| 108 | | |
| 109 | | pub certificates: Vec<crate::cert::SignedCertificate>, |
| 110 | | } |
| 111 | |
|
| 112 | | impl Bundle { |
| 113 | 27× | pub fn is_empty(&self) -> bool { |
| 114 | 27× | self.changes.is_empty() && self.attests.is_empty() && self.objects.is_empty() |
| 115 | 27× | } |
| 116 | | } |
| 117 | |
|
| 118 | |
|
| 119 | | #[derive(Clone, Debug)] |
| 120 | | pub struct RefUpdate { |
| 121 | | pub name: String, |
| 122 | | pub old_state: ObjectRef, |
| 123 | | pub new_state: ObjectRef, |
| 124 | | pub force: bool, |
| 125 | | pub attestation: Option<SignedAttest>, |
| 126 | | } |
| 127 | |
|
| 128 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 129 | | pub struct RefResult { |
| 130 | | pub name: String, |
| 131 | | pub ok: bool, |
| 132 | | pub reason: String, |
| 133 | | } |
| 134 | |
|
| 135 | | pub trait Remote { |
| 136 | | fn repo_id(&self) -> Result<[u8; 16]>; |
| 137 | | fn refs(&self) -> Result<RefsResponse>; |
| 138 | | |
| 139 | | fn negotiate(&self, have: &[ChangeHash], want: &[ChangeHash]) -> Result<Vec<ChangeHash>>; |
| 140 | | |
| 141 | | |
| 142 | | fn fetch(&self, changes: &[ChangeHash], max_blob: Option<u64>) -> Result<Bundle>; |
| 143 | | |
| 144 | | |
| 145 | | fn objects(&self, refs: &[ObjectRef]) -> Result<Vec<(ObjectType, Vec<u8>)>>; |
| 146 | | |
| 147 | | |
| 148 | | fn checkpoint(&self, heads: &[ChangeHash]) -> Result<Bundle>; |
| 149 | | |
| 150 | | fn shallow(&self, heads: &[ChangeHash], depth: u32) -> Result<Vec<ChangeHash>>; |
| 151 | | |
| 152 | | |
| 153 | | |
| 154 | | fn push( |
| 155 | | &self, |
| 156 | | bundle: Bundle, |
| 157 | | updates: Vec<RefUpdate>, |
| 158 | | policy: Option<SignedPolicy>, |
| 159 | | tags: Vec<TagRef>, |
| 160 | | ) -> Result<Vec<RefResult>>; |
| 161 | | } |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | | pub struct LocalRemote { |
| 167 | | pub repo: Repo, |
| 168 | | |
| 169 | | |
| 170 | | pub claimed_id: Option<[u8; 16]>, |
| 171 | | } |
| 172 | |
|
| 173 | | impl LocalRemote { |
| 174 | 1063× | pub fn open(path: &Path) -> Result<LocalRemote> { |
| 175 | | Ok(LocalRemote { |
| 176 | 1063× | repo: Repo::open(path)?, |
| 177 | 1063× | claimed_id: None, |
| 178 | | }) |
| 179 | 1063× | } |
| 180 | | } |
| 181 | |
|
| 182 | | impl Remote for LocalRemote { |
| 183 | 836× | fn repo_id(&self) -> Result<[u8; 16]> { |
| 184 | 836× | self.repo.id() |
| 185 | 836× | } |
| 186 | 835× | fn refs(&self) -> Result<RefsResponse> { |
| 187 | 835× | self.repo.remote_refs() |
| 188 | 835× | } |
| 189 | 313× | fn negotiate(&self, have: &[ChangeHash], want: &[ChangeHash]) -> Result<Vec<ChangeHash>> { |
| 190 | 313× | self.repo.negotiate(have, want) |
| 191 | 313× | } |
| 192 | 316× | fn fetch(&self, changes: &[ChangeHash], max_blob: Option<u64>) -> Result<Bundle> { |
| 193 | 316× | self.repo.bundle_raw_for(changes, max_blob) |
| 194 | 316× | } |
| 195 | 3× | fn objects(&self, refs: &[ObjectRef]) -> Result<Vec<(ObjectType, Vec<u8>)>> { |
| 196 | 3× | self.repo.objects_for(refs) |
| 197 | 3× | } |
| 198 | 1× | fn checkpoint(&self, heads: &[ChangeHash]) -> Result<Bundle> { |
| 199 | 1× | self.repo.checkpoint_bundle_for(heads) |
| 200 | 1× | } |
| 201 | 3× | fn shallow(&self, heads: &[ChangeHash], depth: u32) -> Result<Vec<ChangeHash>> { |
| 202 | 3× | self.repo.shallow_changes(heads, depth) |
| 203 | 3× | } |
| 204 | 246× | fn push( |
| 205 | 246× | &self, |
| 206 | 246× | bundle: Bundle, |
| 207 | 246× | updates: Vec<RefUpdate>, |
| 208 | 246× | policy: Option<SignedPolicy>, |
| 209 | 246× | tags: Vec<TagRef>, |
| 210 | 246× | ) -> Result<Vec<RefResult>> { |
| 211 | 246× | if self.repo.is_unbound() { |
| 212 | 11× | match self.claimed_id { |
| 213 | 11× | Some(id) => self.repo.bind_id(id)?, |
| 214 | | None => { |
| 215 | 0× | return Err(RepoError::Msg( |
| 216 | 0× | "the remote is unbound and this client did not say which repository it is" |
| 217 | 0× | .into(), |
| 218 | 0× | )) |
| 219 | | } |
| 220 | | } |
| 221 | 235× | } |
| 222 | 246× | self.repo.receive_push(bundle, updates, policy, tags) |
| 223 | 246× | } |
| 224 | | } |
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | | #[derive(Clone, Debug, Default)] |
| 230 | | pub struct RemoteOptions { |
| 231 | | |
| 232 | | pub token: Option<String>, |
| 233 | | |
| 234 | | |
| 235 | | pub ca: Option<PathBuf>, |
| 236 | | |
| 237 | | |
| 238 | | pub repo_id: Option<[u8; 16]>, |
| 239 | | |
| 240 | | |
| 241 | | |
| 242 | | |
| 243 | | pub cache_dir: Option<PathBuf>, |
| 244 | | |
| 245 | | |
| 246 | | |
| 247 | | |
| 248 | | pub fallback_tokens: Vec<String>, |
| 249 | | } |
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | | pub type StoreOpener = |
| 255 | | fn(&str, &RemoteOptions) -> Option<crate::strata::Result<Box<dyn crate::strata::Store>>>; |
| 256 | |
|
| 257 | | static STORE_OPENERS: Mutex<Vec<StoreOpener>> = Mutex::new(Vec::new()); |
| 258 | |
|
| 259 | 154× | pub fn register_store_opener(opener: StoreOpener) { |
| 260 | 154× | STORE_OPENERS |
| 261 | 154× | .lock() |
| 262 | 154× | .unwrap_or_else(|p| p.into_inner()) |
| 263 | 154× | .push(opener); |
| 264 | 154× | } |
| 265 | |
|
| 266 | |
|
| 267 | 1123× | pub fn open_store( |
| 268 | 1123× | url: &str, |
| 269 | 1123× | options: &RemoteOptions, |
| 270 | 1123× | ) -> Option<crate::strata::Result<Box<dyn crate::strata::Store>>> { |
| 271 | 1123× | if let Some(path) = url.strip_prefix("dir://") { |
| 272 | | return Some( |
| 273 | 59× | crate::strata::LocalStore::open(path) |
| 274 | 59× | .map(|s| Box::new(s) as Box<dyn crate::strata::Store>), |
| 275 | | ); |
| 276 | 1064× | } |
| 277 | 1064× | let openers = STORE_OPENERS.lock().unwrap_or_else(|p| p.into_inner()); |
| 278 | 1064× | openers.iter().find_map(|open| open(url, options)) |
| 279 | 1123× | } |
| 280 | |
|
| 281 | |
|
| 282 | | pub const REPO_ID_HEADER: &str = "Oxy-Repo-Id"; |
| 283 | |
|
| 284 | |
|
| 285 | | pub const UNBOUND_ID: [u8; 16] = [0; 16]; |
| 286 | |
|
| 287 | |
|
| 288 | | pub struct HttpRemote { |
| 289 | | base: String, |
| 290 | | |
| 291 | | token: Mutex<Option<String>>, |
| 292 | | fallbacks: Mutex<Vec<String>>, |
| 293 | | repo_id: Option<[u8; 16]>, |
| 294 | | agent: ureq::Agent, |
| 295 | | } |
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | 153× | pub fn tls_client_config(ca: Option<&Path>) -> Result<Arc<rustls::ClientConfig>> { |
| 300 | 153× | let mut roots = rustls::RootCertStore::empty(); |
| 301 | 153× | roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned()); |
| 302 | 153× | if let Some(path) = ca { |
| 303 | 65× | let pem = std::fs::read(path) |
| 304 | 65× | .map_err(|e| RepoError::Msg(format!("reading CA file {}: {e}", path.display())))?; |
| 305 | 65× | let certs = rustls_pemfile::certs(&mut pem.as_slice()) |
| 306 | 65× | .collect::<std::result::Result<Vec<_>, _>>() |
| 307 | 65× | .map_err(|e| RepoError::Msg(format!("parsing CA file {}: {e}", path.display())))?; |
| 308 | 65× | if certs.is_empty() { |
| 309 | 0× | return Err(RepoError::Msg(format!( |
| 310 | 0× | "CA file {} holds no certificates", |
| 311 | 0× | path.display() |
| 312 | 0× | ))); |
| 313 | 65× | } |
| 314 | 65× | for c in certs { |
| 315 | 65× | roots |
| 316 | 65× | .add(c) |
| 317 | 65× | .map_err(|e| RepoError::Msg(format!("CA file {}: {e}", path.display())))?; |
| 318 | | } |
| 319 | 88× | } |
| 320 | 153× | let cfg = rustls::ClientConfig::builder_with_provider(Arc::new( |
| 321 | 153× | rustls::crypto::ring::default_provider(), |
| 322 | | )) |
| 323 | 153× | .with_safe_default_protocol_versions() |
| 324 | 153× | .map_err(|e| RepoError::Msg(e.to_string()))? |
| 325 | 153× | .with_root_certificates(roots) |
| 326 | 153× | .with_no_client_auth(); |
| 327 | 153× | Ok(Arc::new(cfg)) |
| 328 | 153× | } |
| 329 | |
|
| 330 | | impl HttpRemote { |
| 331 | 149× | pub fn new(base: &str, options: &RemoteOptions) -> Result<HttpRemote> { |
| 332 | 149× | refuse_plain_token(base, options)?; |
| 333 | 149× | let agent = ureq::AgentBuilder::new() |
| 334 | 149× | .tls_config(tls_client_config(options.ca.as_deref())?) |
| 335 | 149× | .build(); |
| 336 | 149× | Ok(HttpRemote { |
| 337 | 149× | base: base.trim_end_matches('/').to_string(), |
| 338 | 149× | token: Mutex::new(options.token.clone()), |
| 339 | 149× | fallbacks: Mutex::new(options.fallback_tokens.iter().rev().cloned().collect()), |
| 340 | 149× | repo_id: options.repo_id, |
| 341 | 149× | agent, |
| 342 | 149× | }) |
| 343 | 149× | } |
| 344 | |
|
| 345 | | |
| 346 | | |
| 347 | 380× | fn call(&self, post: bool, endpoint: &str, body: Option<&[u8]>) -> Result<Vec<u8>> { |
| 348 | | loop { |
| 349 | 381× | let token = self.token.lock().unwrap_or_else(|p| p.into_inner()).clone(); |
| 350 | 381× | match self.call_once(post, endpoint, body, token.as_deref()) { |
| 351 | 11× | Err(RepoError::Msg(m)) if token.is_some() && refused(&m) => { |
| 352 | 3× | let next = self |
| 353 | 3× | .fallbacks |
| 354 | 3× | .lock() |
| 355 | 3× | .unwrap_or_else(|p| p.into_inner()) |
| 356 | 3× | .pop(); |
| 357 | 3× | let Some(next) = next else { |
| 358 | 2× | return Err(RepoError::Msg(m)); |
| 359 | | }; |
| 360 | 1× | crate::credentials::warn_once(&format!( |
| 361 | 1× | "{}: the stored token was refused; using the one an enclosing checkout has for this hub", |
| 362 | 1× | self.base |
| 363 | 1× | )); |
| 364 | 1× | *self.token.lock().unwrap_or_else(|p| p.into_inner()) = Some(next); |
| 365 | | } |
| 366 | 378× | other => return other, |
| 367 | | } |
| 368 | | } |
| 369 | 380× | } |
| 370 | |
|
| 371 | 381× | fn call_once( |
| 372 | 381× | &self, |
| 373 | 381× | post: bool, |
| 374 | 381× | endpoint: &str, |
| 375 | 381× | body: Option<&[u8]>, |
| 376 | 381× | token: Option<&str>, |
| 377 | 381× | ) -> Result<Vec<u8>> { |
| 378 | 381× | let url = format!("{}/oxy/v1/{endpoint}", self.base); |
| 379 | 381× | let mut req = if post { |
| 380 | 134× | self.agent.post(&url) |
| 381 | | } else { |
| 382 | 247× | self.agent.get(&url) |
| 383 | | }; |
| 384 | 381× | req = req.set("Accept", CONTENT_TYPE); |
| 385 | 381× | if let Some(t) = token { |
| 386 | 22× | req = req.set("Authorization", &format!("Bearer {t}")); |
| 387 | 359× | } |
| 388 | 381× | if let Some(id) = &self.repo_id { |
| 389 | 356× | req = req.set(REPO_ID_HEADER, &hex::encode(id)); |
| 390 | 356× | } |
| 391 | 381× | let resp = match body { |
| 392 | 134× | Some(b) => req.set("Content-Type", CONTENT_TYPE).send_bytes(b), |
| 393 | 247× | None => req.call(), |
| 394 | | }; |
| 395 | 11× | match resp { |
| 396 | 370× | Ok(r) => { |
| 397 | 370× | let mut out = Vec::new(); |
| 398 | 370× | r.into_reader() |
| 399 | 370× | .read_to_end(&mut out) |
| 400 | 370× | .map_err(|e| RepoError::Msg(format!("{url}: {e}")))?; |
| 401 | 370× | Ok(out) |
| 402 | | } |
| 403 | 8× | Err(ureq::Error::Status(code, r)) => { |
| 404 | 8× | let text = r.into_string().unwrap_or_default(); |
| 405 | 8× | let msg = format!("{url}: HTTP {code}: {}", text.trim()); |
| 406 | 8× | Err(RepoError::Msg(with_hint(msg))) |
| 407 | | } |
| 408 | | |
| 409 | 3× | Err(e) => Err(RepoError::Msg(with_hint(e.to_string()))), |
| 410 | | } |
| 411 | 381× | } |
| 412 | | } |
| 413 | |
|
| 414 | |
|
| 415 | |
|
| 416 | |
|
| 417 | 4× | fn refused(message: &str) -> bool { |
| 418 | 4× | [": HTTP 401", ": HTTP 403", ": HTTP 404"] |
| 419 | 4× | .iter() |
| 420 | 6× | .any(|c| message.contains(c)) |
| 421 | 4× | } |
| 422 | |
|
| 423 | |
|
| 424 | |
|
| 425 | 149× | fn refuse_plain_token(url: &str, options: &RemoteOptions) -> Result<()> { |
| 426 | 149× | if options.token.is_some() && !crate::credentials::token_allowed(url) { |
| 427 | 0× | return Err(RepoError::Msg(format!( |
| 428 | 0× | "{url}: refusing to send a token over plain http; use https" |
| 429 | 0× | ))); |
| 430 | 149× | } |
| 431 | 149× | Ok(()) |
| 432 | 149× | } |
| 433 | |
|
| 434 | |
|
| 435 | 19× | pub fn hint_for(message: &str) -> Option<&'static str> { |
| 436 | 19× | let m = message.to_ascii_lowercase(); |
| 437 | 19× | let hint = if m.contains("http 401") { |
| 438 | 6× | "the hub needs you signed in, or did not accept the token: run `oxy auth login HUB` (pushing needs repo:write)" |
| 439 | 13× | } else if m.contains("http 403") { |
| 440 | 1× | "the token or account lacks permission: pushing needs a token with repo:write and write access to the repository" |
| 441 | 12× | } else if m.contains("http 404") { |
| 442 | 4× | "nothing at this URL that you can see: check the owner and repository name; a private repository needs you signed in (`oxy auth login HUB`)" |
| 443 | 8× | } else if m.contains("unknownissuer") |
| 444 | 6× | || m.contains("unknown issuer") |
| 445 | 6× | || m.contains("certificate") |
| 446 | 5× | || m.contains("invalidcertificate") |
| 447 | | { |
| 448 | 3× | "the server's certificate is not trusted: for a hub with a self-signed certificate, download it from /.well-known/oxyhub-ca.pem on the hub and run `oxy config remote.<remote>.ca FILE`" |
| 449 | 5× | } else if m.contains("connection refused") { |
| 450 | 3× | "nothing is listening there: check the URL and port, and that the server is running" |
| 451 | 2× | } else if m.contains("dns") |
| 452 | 1× | || m.contains("failed to lookup") |
| 453 | 1× | || m.contains("name or service not known") |
| 454 | | { |
| 455 | 1× | "the host name does not resolve: check the URL" |
| 456 | 1× | } else if m.contains("timed out") || m.contains("timeout") { |
| 457 | 0× | "the server did not answer in time: check the network, or try again" |
| 458 | | } else { |
| 459 | 1× | return None; |
| 460 | | }; |
| 461 | 18× | Some(hint) |
| 462 | 19× | } |
| 463 | |
|
| 464 | |
|
| 465 | 11× | pub fn with_hint(message: String) -> String { |
| 466 | 11× | match hint_for(&message) { |
| 467 | 11× | Some(h) => format!("{message}\n hint: {h}"), |
| 468 | 0× | None => message, |
| 469 | | } |
| 470 | 11× | } |
| 471 | |
|
| 472 | |
|
| 473 | |
|
| 474 | |
|
| 475 | 0× | pub fn http_json( |
| 476 | 0× | method: &str, |
| 477 | 0× | url: &str, |
| 478 | 0× | body: Option<&str>, |
| 479 | 0× | options: &RemoteOptions, |
| 480 | 0× | ) -> Result<(u16, String)> { |
| 481 | 0× | refuse_plain_token(url, options)?; |
| 482 | 0× | let agent = ureq::AgentBuilder::new() |
| 483 | 0× | .tls_config(tls_client_config(options.ca.as_deref())?) |
| 484 | 0× | .timeout(std::time::Duration::from_secs(60)) |
| 485 | 0× | .build(); |
| 486 | 0× | let mut req = agent.request(method, url).set("Accept", "application/json"); |
| 487 | 0× | if let Some(t) = &options.token { |
| 488 | 0× | req = req.set("Authorization", &format!("Bearer {t}")); |
| 489 | 0× | } |
| 490 | 0× | let resp = match body { |
| 491 | 0× | Some(b) => req.set("Content-Type", "application/json").send_string(b), |
| 492 | 0× | None => req.call(), |
| 493 | | }; |
| 494 | 0× | match resp { |
| 495 | 0× | Ok(r) => { |
| 496 | 0× | let code = r.status(); |
| 497 | 0× | Ok((code, r.into_string().unwrap_or_default())) |
| 498 | | } |
| 499 | 0× | Err(ureq::Error::Status(code, r)) => Ok((code, r.into_string().unwrap_or_default())), |
| 500 | 0× | Err(e) => Err(RepoError::Msg(with_hint(e.to_string()))), |
| 501 | | } |
| 502 | 0× | } |
| 503 | |
|
| 504 | |
|
| 505 | |
|
| 506 | 0× | pub fn http_get(url: &str, options: &RemoteOptions) -> Result<Vec<u8>> { |
| 507 | 0× | refuse_plain_token(url, options)?; |
| 508 | 0× | let agent = ureq::AgentBuilder::new() |
| 509 | 0× | .tls_config(tls_client_config(options.ca.as_deref())?) |
| 510 | 0× | .build(); |
| 511 | 0× | let mut req = agent.get(url).set("Accept", "application/json"); |
| 512 | 0× | if let Some(t) = &options.token { |
| 513 | 0× | req = req.set("Authorization", &format!("Bearer {t}")); |
| 514 | 0× | } |
| 515 | 0× | match req.call() { |
| 516 | 0× | Ok(r) => { |
| 517 | 0× | let mut out = Vec::new(); |
| 518 | 0× | r.into_reader() |
| 519 | 0× | .read_to_end(&mut out) |
| 520 | 0× | .map_err(|e| RepoError::Msg(format!("{url}: {e}")))?; |
| 521 | 0× | Ok(out) |
| 522 | | } |
| 523 | 0× | Err(ureq::Error::Status(code, r)) => { |
| 524 | 0× | let text = r.into_string().unwrap_or_default(); |
| 525 | 0× | Err(RepoError::Msg(with_hint(format!( |
| 526 | 0× | "{url}: HTTP {code}: {}", |
| 527 | 0× | text.trim() |
| 528 | 0× | )))) |
| 529 | | } |
| 530 | | |
| 531 | 0× | Err(e) => Err(RepoError::Msg(with_hint(e.to_string()))), |
| 532 | | } |
| 533 | 0× | } |
| 534 | |
|
| 535 | | impl Remote for HttpRemote { |
| 536 | 128× | fn repo_id(&self) -> Result<[u8; 16]> { |
| 537 | 128× | wire::decode_capabilities(&self.call(false, "capabilities", None)?) |
| 538 | 128× | } |
| 539 | 119× | fn refs(&self) -> Result<RefsResponse> { |
| 540 | 119× | Ok(wire::decode_refs(&self.call(false, "refs", None)?)?.1) |
| 541 | 119× | } |
| 542 | 47× | fn negotiate(&self, have: &[ChangeHash], want: &[ChangeHash]) -> Result<Vec<ChangeHash>> { |
| 543 | 47× | wire::decode_hashes(&self.call( |
| 544 | | true, |
| 545 | 47× | "negotiate", |
| 546 | 47× | Some(&wire::encode_negotiate(have, want)), |
| 547 | 0× | )?) |
| 548 | 47× | } |
| 549 | 47× | fn fetch(&self, changes: &[ChangeHash], max_blob: Option<u64>) -> Result<Bundle> { |
| 550 | 47× | wire::decode_bundle(&self.call( |
| 551 | | true, |
| 552 | 47× | "fetch", |
| 553 | 47× | Some(&wire::encode_fetch(changes, max_blob)), |
| 554 | 0× | )?) |
| 555 | 47× | } |
| 556 | 1× | fn objects(&self, refs: &[ObjectRef]) -> Result<Vec<(ObjectType, Vec<u8>)>> { |
| 557 | | Ok( |
| 558 | 1× | wire::decode_bundle(&self.call(true, "objects", Some(&wire::encode_hashes(refs)))?)? |
| 559 | | .objects, |
| 560 | | ) |
| 561 | 1× | } |
| 562 | 0× | fn checkpoint(&self, heads: &[ChangeHash]) -> Result<Bundle> { |
| 563 | 0× | wire::decode_bundle(&self.call(true, "checkpoint", Some(&wire::encode_hashes(heads)))?) |
| 564 | 0× | } |
| 565 | 0× | fn shallow(&self, heads: &[ChangeHash], depth: u32) -> Result<Vec<ChangeHash>> { |
| 566 | 0× | wire::decode_hashes(&self.call( |
| 567 | | true, |
| 568 | 0× | "shallow", |
| 569 | 0× | Some(&wire::encode_shallow(heads, depth)), |
| 570 | 0× | )?) |
| 571 | 0× | } |
| 572 | 38× | fn push( |
| 573 | 38× | &self, |
| 574 | 38× | bundle: Bundle, |
| 575 | 38× | updates: Vec<RefUpdate>, |
| 576 | 38× | policy: Option<SignedPolicy>, |
| 577 | 38× | tags: Vec<TagRef>, |
| 578 | 38× | ) -> Result<Vec<RefResult>> { |
| 579 | 38× | wire::decode_push_response(&self.call( |
| 580 | | true, |
| 581 | 38× | "push", |
| 582 | 38× | Some(&wire::encode_push( |
| 583 | 38× | &bundle, |
| 584 | 38× | &updates, |
| 585 | 38× | policy.as_ref(), |
| 586 | 38× | &tags, |
| 587 | 38× | )), |
| 588 | 4× | )?) |
| 589 | 38× | } |
| 590 | | } |
| 591 | |
|
| 592 | |
|
| 593 | |
|
| 594 | |
|
| 595 | 258× | pub fn stored_url(url: &str) -> String { |
| 596 | | |
| 597 | | |
| 598 | 258× | let (prefix, path) = match (url.strip_prefix("file://"), url.strip_prefix("dir://")) { |
| 599 | 1× | (Some(p), _) => ("file://", p), |
| 600 | 11× | (_, Some(p)) => ("dir://", p), |
| 601 | 246× | _ if url.contains("://") => return url.to_string(), |
| 602 | 223× | _ => ("", url), |
| 603 | | }; |
| 604 | 235× | match std::path::absolute(path) { |
| 605 | 235× | Ok(abs) => format!("{prefix}{}", abs.display()), |
| 606 | 0× | Err(_) => url.to_string(), |
| 607 | | } |
| 608 | 258× | } |
| 609 | |
|
| 610 | |
|
| 611 | |
|
| 612 | 1260× | pub fn open_remote(url: &str, options: &RemoteOptions) -> Result<Box<dyn Remote>> { |
| 613 | 1260× | if url.starts_with("http://") || url.starts_with("https://") { |
| 614 | 149× | return Ok(Box::new(HttpRemote::new(url, options)?)); |
| 615 | 1111× | } |
| 616 | 1111× | if let Some(store) = open_store(url, options) { |
| 617 | 48× | return crate::repo::strata::open_store_remote(store?, options); |
| 618 | 1063× | } |
| 619 | 1063× | let path = url.strip_prefix("file://").unwrap_or(url); |
| 620 | 1063× | if path.contains("://") { |
| 621 | 0× | return Err(RepoError::Msg(format!("unsupported remote URL {url:?}"))); |
| 622 | 1063× | } |
| 623 | 1063× | let mut local = LocalRemote::open(Path::new(path))?; |
| 624 | 1063× | local.claimed_id = options.repo_id; |
| 625 | 1063× | Ok(Box::new(local)) |
| 626 | 1260× | } |
| 627 | |
|
| 628 | |
|
| 629 | |
|
| 630 | |
|
| 631 | | pub mod wire { |
| 632 | | use super::*; |
| 633 | |
|
| 634 | 0× | fn schema(msg: &str) -> RepoError { |
| 635 | 0× | RepoError::Object(ObjectError::Schema(msg.into())) |
| 636 | 0× | } |
| 637 | 151× | fn sigs_value(sigs: &[Signature]) -> Value { |
| 638 | 151× | Value::Array(sigs.iter().map(|s| s.to_value()).collect()) |
| 639 | 151× | } |
| 640 | 146× | fn sigs_from(v: &Value) -> Result<Vec<Signature>> { |
| 641 | 146× | Ok(o::array(v, "signatures")? |
| 642 | 146× | .iter() |
| 643 | 146× | .map(Signature::from_value) |
| 644 | 146× | .collect::<std::result::Result<Vec<_>, _>>()?) |
| 645 | 146× | } |
| 646 | 354× | fn hashes_value(hs: &[ObjectRef]) -> Value { |
| 647 | 497× | Value::Array(hs.iter().map(|h| Value::Bytes(h.0.to_vec())).collect()) |
| 648 | 354× | } |
| 649 | 413× | fn hashes_from(v: &Value) -> Result<Vec<ObjectRef>> { |
| 650 | 413× | Ok(o::array(v, "hashes")? |
| 651 | 413× | .iter() |
| 652 | 557× | .map(|h| o::oref(h, "hash")) |
| 653 | 413× | .collect::<std::result::Result<Vec<_>, _>>()?) |
| 654 | 413× | } |
| 655 | 24× | fn attest_value(sa: &SignedAttest) -> Value { |
| 656 | 24× | Value::Array(vec![Value::Bytes(sa.attest.encode()), sigs_value(&sa.sigs)]) |
| 657 | 24× | } |
| 658 | 38× | fn attest_from(v: &Value) -> Result<SignedAttest> { |
| 659 | 38× | let a = o::array(v, "signed attest")?; |
| 660 | 38× | if a.len() != 2 { |
| 661 | 0× | return Err(schema("signed attest: expected [attest, sigs]")); |
| 662 | 38× | } |
| 663 | | Ok(SignedAttest { |
| 664 | 38× | attest: Attest::decode(o::bytes(&a[0], "attest")?)?, |
| 665 | 38× | sigs: sigs_from(&a[1])?, |
| 666 | | }) |
| 667 | 38× | } |
| 668 | 197× | fn opt_attest_value(sa: &Option<SignedAttest>) -> Value { |
| 669 | 197× | sa.as_ref().map(attest_value).unwrap_or(Value::Null) |
| 670 | 197× | } |
| 671 | 252× | fn opt_attest_from(v: &Value) -> Result<Option<SignedAttest>> { |
| 672 | 252× | if v.is_null() { |
| 673 | 225× | Ok(None) |
| 674 | | } else { |
| 675 | 27× | attest_from(v).map(Some) |
| 676 | | } |
| 677 | 252× | } |
| 678 | | |
| 679 | | |
| 680 | 10× | pub fn policy_value(sp: &SignedPolicy) -> Value { |
| 681 | 10× | Value::Array(vec![Value::Bytes(sp.policy.encode()), sigs_value(&sp.sigs)]) |
| 682 | 10× | } |
| 683 | 8× | pub fn policy_from(v: &Value) -> Result<SignedPolicy> { |
| 684 | 8× | let a = o::array(v, "signed policy")?; |
| 685 | 8× | if a.len() != 2 { |
| 686 | 0× | return Err(schema("signed policy: expected [policy, sigs]")); |
| 687 | 8× | } |
| 688 | | Ok(SignedPolicy { |
| 689 | 8× | policy: Policy::decode(o::bytes(&a[0], "policy")?)?, |
| 690 | 8× | sigs: sigs_from(&a[1])?, |
| 691 | | }) |
| 692 | 8× | } |
| 693 | 186× | fn opt_policy_value(sp: Option<&SignedPolicy>) -> Value { |
| 694 | 186× | sp.map(policy_value).unwrap_or(Value::Null) |
| 695 | 186× | } |
| 696 | 225× | fn opt_policy_from(v: &Value) -> Result<Option<SignedPolicy>> { |
| 697 | 225× | if v.is_null() { |
| 698 | 222× | Ok(None) |
| 699 | | } else { |
| 700 | 3× | policy_from(v).map(Some) |
| 701 | | } |
| 702 | 225× | } |
| 703 | 931× | fn map<'a>(v: &'a Value, what: &str) -> Result<&'a o::Map> { |
| 704 | 931× | Ok(o::map(v, what)?) |
| 705 | 931× | } |
| 706 | 2609× | fn field<'a>(m: &'a o::Map, k: u64, what: &str) -> Result<&'a Value> { |
| 707 | 2609× | Ok(o::field(m, k, what)?) |
| 708 | 2609× | } |
| 709 | |
|
| 710 | | |
| 711 | | |
| 712 | | |
| 713 | 122× | pub fn bundle_value(b: &Bundle) -> Value { |
| 714 | 122× | let mut changes = Vec::with_capacity(b.raw_changes.len() + b.changes.len()); |
| 715 | | |
| 716 | | |
| 717 | 122× | for (bytes, s) in &b.raw_changes { |
| 718 | 0× | changes.push(Value::Array(vec![ |
| 719 | 0× | Value::Bytes(bytes.clone()), |
| 720 | 0× | sigs_value(s), |
| 721 | 0× | ])); |
| 722 | 0× | } |
| 723 | 122× | let mut texts: Vec<(ObjectType, Vec<u8>)> = Vec::new(); |
| 724 | 122× | let mut listed: Option<std::collections::HashSet<ObjectRef>> = None; |
| 725 | 122× | for (c, s) in &b.changes { |
| 726 | 117× | let (bytes, stored) = c.encode_parts(); |
| 727 | 117× | for t in stored { |
| 728 | 0× | let listed = listed.get_or_insert_with(|| { |
| 729 | 0× | b.objects |
| 730 | 0× | .iter() |
| 731 | 0× | .map(|(ty, body)| crate::hash::hash_object(*ty, body)) |
| 732 | 0× | .collect() |
| 733 | 0× | }); |
| 734 | 0× | if !listed.insert(t.blob.hash()) { |
| 735 | 0× | continue; |
| 736 | 0× | } |
| 737 | 0× | for chunk in crate::chunk::chunks(&t.content) { |
| 738 | 0× | if listed.insert(crate::hash::hash_object(ObjectType::Chunk, chunk)) { |
| 739 | 0× | texts.push((ObjectType::Chunk, chunk.to_vec())); |
| 740 | 0× | } |
| 741 | | } |
| 742 | 0× | texts.push((ObjectType::Blob, t.blob.encode())); |
| 743 | | } |
| 744 | 117× | changes.push(Value::Array(vec![Value::Bytes(bytes), sigs_value(s)])); |
| 745 | | } |
| 746 | 122× | let mut v = Value::map([ |
| 747 | 122× | (0, Value::Array(changes)), |
| 748 | 122× | ( |
| 749 | 122× | 1, |
| 750 | 122× | Value::Array(b.attests.iter().map(attest_value).collect()), |
| 751 | 122× | ), |
| 752 | | ( |
| 753 | | 2, |
| 754 | | Value::Array( |
| 755 | 122× | b.objects |
| 756 | 122× | .iter() |
| 757 | 122× | .chain(&texts) |
| 758 | 122× | .map(|(t, body)| { |
| 759 | 92× | Value::Array(vec![ |
| 760 | 92× | Value::Text(t.name().into()), |
| 761 | 92× | Value::Bytes(body.clone()), |
| 762 | 92× | ]) |
| 763 | 92× | }) |
| 764 | 122× | .collect(), |
| 765 | | ), |
| 766 | | ), |
| 767 | 122× | ( |
| 768 | 122× | 3, |
| 769 | 122× | Value::Array(b.policies.iter().map(policy_value).collect()), |
| 770 | 122× | ), |
| 771 | | ]); |
| 772 | | |
| 773 | 122× | if !b.certificates.is_empty() { |
| 774 | 1× | if let Value::Map(m) = &mut v { |
| 775 | 1× | m.insert( |
| 776 | | 4, |
| 777 | | Value::Array( |
| 778 | 1× | b.certificates |
| 779 | 1× | .iter() |
| 780 | 1× | .map(|c| Value::Bytes(c.encode())) |
| 781 | 1× | .collect(), |
| 782 | | ), |
| 783 | | ); |
| 784 | 0× | } |
| 785 | 121× | } |
| 786 | 122× | v |
| 787 | 122× | } |
| 788 | |
|
| 789 | 112× | pub fn bundle_from_value(v: &Value) -> Result<Bundle> { |
| 790 | 112× | let m = map(v, "bundle")?; |
| 791 | 112× | let mut b = Bundle::default(); |
| 792 | 112× | for ob in o::array(field(m, 2, "objects")?, "objects")? { |
| 793 | 81× | let pair = o::array(ob, "object")?; |
| 794 | 81× | if pair.len() != 2 { |
| 795 | 0× | return Err(schema("object: expected [type, body]")); |
| 796 | 81× | } |
| 797 | 81× | let ty = ObjectType::from_name(o::text(&pair[0], "object type")?) |
| 798 | 81× | .ok_or_else(|| schema("unknown object type"))?; |
| 799 | 81× | b.objects |
| 800 | 81× | .push((ty, o::bytes(&pair[1], "object body")?.to_vec())); |
| 801 | | } |
| 802 | | |
| 803 | 112× | let mut by_ref: Option<std::collections::HashMap<ObjectRef, usize>> = None; |
| 804 | 112× | for c in o::array(field(m, 0, "changes")?, "changes")? { |
| 805 | 100× | let pair = o::array(c, "change entry")?; |
| 806 | 100× | if pair.len() != 2 { |
| 807 | 0× | return Err(schema("change entry: expected [change, sigs]")); |
| 808 | 100× | } |
| 809 | 100× | let encoded = o::bytes(&pair[0], "change")?; |
| 810 | 100× | b.hashes |
| 811 | 100× | .push(crate::hash::hash_object(ObjectType::Change, encoded)); |
| 812 | 100× | let change = Change::decode_with(encoded, &mut |text, count, form| { |
| 813 | 0× | let by_ref = by_ref.get_or_insert_with(|| { |
| 814 | 0× | b.objects |
| 815 | 0× | .iter() |
| 816 | 0× | .enumerate() |
| 817 | 0× | .map(|(i, (ty, body))| (crate::hash::hash_object(*ty, body), i)) |
| 818 | 0× | .collect() |
| 819 | 0× | }); |
| 820 | 0× | let get = |
| 821 | 0× | |r: &ObjectRef, want: ObjectType| match by_ref.get(r).map(|i| &b.objects[*i]) { |
| 822 | 0× | Some((ty, body)) if *ty == want => Ok(body), |
| 823 | 0× | _ => Err(ObjectError::Schema(format!( |
| 824 | 0× | "the bundle lacks {} {}", |
| 825 | 0× | want.name(), |
| 826 | 0× | r.short(12) |
| 827 | 0× | ))), |
| 828 | 0× | }; |
| 829 | 0× | let blob = crate::objects::Blob::decode(get(text, ObjectType::Blob)?)?; |
| 830 | 0× | let mut content = Vec::with_capacity(blob.length as usize); |
| 831 | 0× | for chunk in &blob.chunks { |
| 832 | 0× | content.extend_from_slice(get(chunk, ObjectType::Chunk)?); |
| 833 | | } |
| 834 | 0× | crate::objects::text_lines(&content, count, form) |
| 835 | 0× | })?; |
| 836 | 100× | b.changes.push((change, sigs_from(&pair[1])?)); |
| 837 | | } |
| 838 | 112× | for a in o::array(field(m, 1, "attests")?, "attests")? { |
| 839 | 11× | b.attests.push(attest_from(a)?); |
| 840 | | } |
| 841 | 112× | if let Some(ps) = m.get(&3) { |
| 842 | 112× | for p in o::array(ps, "policies")? { |
| 843 | 3× | b.policies.push(policy_from(p)?); |
| 844 | | } |
| 845 | 0× | } |
| 846 | 112× | if let Some(cs) = m.get(&4) { |
| 847 | 1× | for c in o::array(cs, "certificates")? { |
| 848 | 1× | b.certificates.push( |
| 849 | 1× | crate::cert::SignedCertificate::decode(o::bytes(c, "certificate")?) |
| 850 | 1× | .map_err(|e| schema(&format!("certificate: {e}")))?, |
| 851 | | ); |
| 852 | | } |
| 853 | 111× | } |
| 854 | 112× | Ok(b) |
| 855 | 112× | } |
| 856 | |
|
| 857 | 83× | pub fn encode_bundle(b: &Bundle) -> Vec<u8> { |
| 858 | 83× | cbor::encode(&bundle_value(b)) |
| 859 | 83× | } |
| 860 | 77× | pub fn decode_bundle(bytes: &[u8]) -> Result<Bundle> { |
| 861 | 77× | bundle_from_value(&cbor::decode(bytes).map_err(ObjectError::from)?) |
| 862 | 77× | } |
| 863 | |
|
| 864 | 123× | pub fn encode_capabilities(id: [u8; 16]) -> Vec<u8> { |
| 865 | 123× | cbor::encode(&Value::map([ |
| 866 | 123× | (0, Value::Array(vec![Value::Uint(PROTOCOL_VERSION)])), |
| 867 | 123× | (1, Value::Bytes(id.to_vec())), |
| 868 | 123× | (2, Value::Array(vec![Value::Text("seal".into())])), |
| 869 | 123× | ])) |
| 870 | 123× | } |
| 871 | 123× | pub fn decode_capabilities(bytes: &[u8]) -> Result<[u8; 16]> { |
| 872 | 123× | let v = cbor::decode(bytes).map_err(ObjectError::from)?; |
| 873 | 123× | let m = map(&v, "capabilities")?; |
| 874 | 123× | let versions = o::array(field(m, 0, "versions")?, "versions")?; |
| 875 | 123× | if !versions |
| 876 | 123× | .iter() |
| 877 | 123× | .any(|x| x.as_uint() == Some(PROTOCOL_VERSION)) |
| 878 | | { |
| 879 | 0× | return Err(RepoError::Msg( |
| 880 | 0× | "remote does not speak protocol version 1".into(), |
| 881 | 0× | )); |
| 882 | 123× | } |
| 883 | 123× | o::bytes(field(m, 1, "repo id")?, "repo id")? |
| 884 | 123× | .try_into() |
| 885 | 123× | .map_err(|_| schema("repo id: expected 16 bytes")) |
| 886 | 123× | } |
| 887 | |
|
| 888 | 186× | fn tags_value(tags: &[TagRef]) -> Value { |
| 889 | | Value::Array( |
| 890 | 186× | tags.iter() |
| 891 | 186× | .map(|t| { |
| 892 | 7× | Value::map([ |
| 893 | 7× | (0, Value::Text(t.name.clone())), |
| 894 | 7× | (1, Value::Bytes(t.state.0.to_vec())), |
| 895 | 7× | (2, hashes_value(&t.heads)), |
| 896 | 7× | (3, opt_attest_value(&t.attestation)), |
| 897 | 7× | ]) |
| 898 | 7× | }) |
| 899 | 186× | .collect(), |
| 900 | | ) |
| 901 | 186× | } |
| 902 | 225× | fn tags_from(v: &Value) -> Result<Vec<TagRef>> { |
| 903 | 225× | let mut out = Vec::new(); |
| 904 | 225× | for t in o::array(v, "tags")? { |
| 905 | 23× | let tm = map(t, "tag")?; |
| 906 | 23× | out.push(TagRef { |
| 907 | 23× | name: o::text(field(tm, 0, "name")?, "name")?.to_owned(), |
| 908 | 23× | state: o::oref(field(tm, 1, "state")?, "state")?, |
| 909 | 23× | heads: hashes_from(field(tm, 2, "heads")?)?, |
| 910 | 23× | attestation: opt_attest_from(field(tm, 3, "attestation")?)?, |
| 911 | | }); |
| 912 | | } |
| 913 | 225× | Ok(out) |
| 914 | 225× | } |
| 915 | |
|
| 916 | 147× | pub fn encode_refs(id: [u8; 16], resp: &RefsResponse) -> Vec<u8> { |
| 917 | 147× | cbor::encode(&Value::map([ |
| 918 | | ( |
| 919 | | 0, |
| 920 | | Value::Array( |
| 921 | 147× | resp.refs |
| 922 | 147× | .iter() |
| 923 | 151× | .map(|r| { |
| 924 | 151× | let mut v = Value::map([ |
| 925 | 151× | (0, Value::Text(r.name.clone())), |
| 926 | 151× | (1, Value::Bytes(r.state.0.to_vec())), |
| 927 | 151× | (2, hashes_value(&r.heads)), |
| 928 | 151× | (3, opt_attest_value(&r.attestation)), |
| 929 | 151× | (4, r.last_serial.map(Value::Uint).unwrap_or(Value::Null)), |
| 930 | 151× | ]); |
| 931 | | |
| 932 | 151× | if r.accepted.is_some() { |
| 933 | 0× | if let Value::Map(m) = &mut v { |
| 934 | 0× | m.insert(5, opt_attest_value(&r.accepted)); |
| 935 | 0× | } |
| 936 | 151× | } |
| 937 | 151× | v |
| 938 | 151× | }) |
| 939 | 147× | .collect(), |
| 940 | | ), |
| 941 | | ), |
| 942 | 147× | (1, Value::Bytes(id.to_vec())), |
| 943 | 147× | (2, opt_policy_value(resp.policy.as_ref())), |
| 944 | 147× | (3, tags_value(&resp.tags)), |
| 945 | 147× | ( |
| 946 | 147× | 4, |
| 947 | 147× | Value::Array(resp.history.iter().map(policy_value).collect()), |
| 948 | 147× | ), |
| 949 | 147× | ( |
| 950 | 147× | 5, |
| 951 | 147× | resp.default_bookmark |
| 952 | 147× | .clone() |
| 953 | 147× | .map(Value::Text) |
| 954 | 147× | .unwrap_or(Value::Null), |
| 955 | 147× | ), |
| 956 | | ])) |
| 957 | 147× | } |
| 958 | 190× | pub fn decode_refs(bytes: &[u8]) -> Result<([u8; 16], RefsResponse)> { |
| 959 | 190× | let v = cbor::decode(bytes).map_err(ObjectError::from)?; |
| 960 | 190× | let m = map(&v, "refs")?; |
| 961 | 190× | let id: [u8; 16] = o::bytes(field(m, 1, "repo id")?, "repo id")? |
| 962 | 190× | .try_into() |
| 963 | 190× | .map_err(|_| schema("repo id: expected 16 bytes"))?; |
| 964 | 190× | let mut refs = Vec::new(); |
| 965 | 194× | for r in o::array(field(m, 0, "refs")?, "refs")? { |
| 966 | 194× | let rm = map(r, "ref")?; |
| 967 | 194× | let ls = field(rm, 4, "last serial")?; |
| 968 | 194× | refs.push(RemoteRef { |
| 969 | 194× | name: o::text(field(rm, 0, "name")?, "name")?.to_owned(), |
| 970 | 194× | state: o::oref(field(rm, 1, "state")?, "state")?, |
| 971 | 194× | heads: hashes_from(field(rm, 2, "heads")?)?, |
| 972 | 194× | attestation: opt_attest_from(field(rm, 3, "attestation")?)?, |
| 973 | 194× | last_serial: if ls.is_null() { |
| 974 | 178× | None |
| 975 | | } else { |
| 976 | 16× | Some(o::uint(ls, "last serial")?) |
| 977 | | }, |
| 978 | 194× | accepted: match rm.get(&5) { |
| 979 | 0× | Some(a) => opt_attest_from(a)?, |
| 980 | 194× | None => None, |
| 981 | | }, |
| 982 | | }); |
| 983 | | } |
| 984 | 190× | let policy = match m.get(&2) { |
| 985 | 190× | Some(p) => opt_policy_from(p)?, |
| 986 | 0× | None => None, |
| 987 | | }; |
| 988 | 190× | let tags = match m.get(&3) { |
| 989 | 190× | Some(t) => tags_from(t)?, |
| 990 | 0× | None => Vec::new(), |
| 991 | | }; |
| 992 | 190× | let mut history = Vec::new(); |
| 993 | 190× | if let Some(h) = m.get(&4) { |
| 994 | 190× | for p in o::array(h, "history")? { |
| 995 | 1× | history.push(policy_from(p)?); |
| 996 | | } |
| 997 | 0× | } |
| 998 | 190× | let default_bookmark = match m.get(&5) { |
| 999 | 121× | Some(Value::Text(t)) => Some(t.clone()), |
| 1000 | 69× | _ => None, |
| 1001 | | }; |
| 1002 | 190× | Ok(( |
| 1003 | 190× | id, |
| 1004 | 190× | RefsResponse { |
| 1005 | 190× | refs, |
| 1006 | 190× | policy, |
| 1007 | 190× | tags, |
| 1008 | 190× | history, |
| 1009 | 190× | default_bookmark, |
| 1010 | 190× | }, |
| 1011 | 190× | )) |
| 1012 | 190× | } |
| 1013 | |
|
| 1014 | 48× | pub fn encode_negotiate(have: &[ChangeHash], want: &[ChangeHash]) -> Vec<u8> { |
| 1015 | 48× | cbor::encode(&Value::map([ |
| 1016 | 48× | (0, hashes_value(have)), |
| 1017 | 48× | (1, hashes_value(want)), |
| 1018 | 48× | ])) |
| 1019 | 48× | } |
| 1020 | 48× | pub fn decode_negotiate(bytes: &[u8]) -> Result<(Vec<ChangeHash>, Vec<ChangeHash>)> { |
| 1021 | 48× | let v = cbor::decode(bytes).map_err(ObjectError::from)?; |
| 1022 | 48× | let m = map(&v, "negotiate")?; |
| 1023 | | Ok(( |
| 1024 | 48× | hashes_from(field(m, 0, "have")?)?, |
| 1025 | 48× | hashes_from(field(m, 1, "want")?)?, |
| 1026 | | )) |
| 1027 | 48× | } |
| 1028 | |
|
| 1029 | | |
| 1030 | 49× | pub fn encode_fetch(hs: &[ChangeHash], max_blob: Option<u64>) -> Vec<u8> { |
| 1031 | 49× | cbor::encode(&Value::map([ |
| 1032 | 49× | (0, hashes_value(hs)), |
| 1033 | 49× | (1, max_blob.map(Value::Uint).unwrap_or(Value::Null)), |
| 1034 | 49× | ])) |
| 1035 | 49× | } |
| 1036 | 50× | pub fn decode_fetch(bytes: &[u8]) -> Result<(Vec<ChangeHash>, Option<u64>)> { |
| 1037 | 50× | let v = cbor::decode(bytes).map_err(ObjectError::from)?; |
| 1038 | 50× | let m = map(&v, "fetch")?; |
| 1039 | 50× | let max = match m.get(&1) { |
| 1040 | 49× | Some(x) if !x.is_null() => Some(o::uint(x, "max blob")?), |
| 1041 | 48× | _ => None, |
| 1042 | | }; |
| 1043 | 50× | Ok((hashes_from(field(m, 0, "changes")?)?, max)) |
| 1044 | 50× | } |
| 1045 | |
|
| 1046 | | |
| 1047 | 1× | pub fn encode_shallow(hs: &[ChangeHash], depth: u32) -> Vec<u8> { |
| 1048 | 1× | cbor::encode(&Value::map([ |
| 1049 | 1× | (0, hashes_value(hs)), |
| 1050 | 1× | (1, Value::Uint(depth as u64)), |
| 1051 | 1× | ])) |
| 1052 | 1× | } |
| 1053 | 1× | pub fn decode_shallow(bytes: &[u8]) -> Result<(Vec<ChangeHash>, u32)> { |
| 1054 | 1× | let v = cbor::decode(bytes).map_err(ObjectError::from)?; |
| 1055 | 1× | let m = map(&v, "shallow")?; |
| 1056 | | Ok(( |
| 1057 | 1× | hashes_from(field(m, 0, "heads")?)?, |
| 1058 | 1× | o::u32_(field(m, 1, "depth")?, "depth")?, |
| 1059 | | )) |
| 1060 | 1× | } |
| 1061 | |
|
| 1062 | 50× | pub fn encode_hashes(hs: &[ChangeHash]) -> Vec<u8> { |
| 1063 | 50× | cbor::encode(&Value::map([(0, hashes_value(hs))])) |
| 1064 | 50× | } |
| 1065 | 49× | pub fn decode_hashes(bytes: &[u8]) -> Result<Vec<ChangeHash>> { |
| 1066 | 49× | let v = cbor::decode(bytes).map_err(ObjectError::from)?; |
| 1067 | 49× | hashes_from(field(map(&v, "hashes")?, 0, "hashes")?) |
| 1068 | 49× | } |
| 1069 | |
|
| 1070 | 39× | pub fn encode_push( |
| 1071 | 39× | bundle: &Bundle, |
| 1072 | 39× | updates: &[RefUpdate], |
| 1073 | 39× | policy: Option<&SignedPolicy>, |
| 1074 | 39× | tags: &[TagRef], |
| 1075 | 39× | ) -> Vec<u8> { |
| 1076 | 39× | cbor::encode(&Value::map([ |
| 1077 | 39× | (0, bundle_value(bundle)), |
| 1078 | 39× | (2, opt_policy_value(policy)), |
| 1079 | 39× | (3, tags_value(tags)), |
| 1080 | | ( |
| 1081 | | 1, |
| 1082 | | Value::Array( |
| 1083 | 39× | updates |
| 1084 | 39× | .iter() |
| 1085 | 39× | .map(|u| { |
| 1086 | 39× | Value::map([ |
| 1087 | 39× | (0, Value::Text(u.name.clone())), |
| 1088 | 39× | (1, Value::Bytes(u.old_state.0.to_vec())), |
| 1089 | 39× | (2, Value::Bytes(u.new_state.0.to_vec())), |
| 1090 | 39× | (3, Value::Bool(u.force)), |
| 1091 | 39× | (4, opt_attest_value(&u.attestation)), |
| 1092 | 39× | ]) |
| 1093 | 39× | }) |
| 1094 | 39× | .collect(), |
| 1095 | | ), |
| 1096 | | ), |
| 1097 | | ])) |
| 1098 | 39× | } |
| 1099 | | |
| 1100 | | pub type PushParts = (Bundle, Vec<RefUpdate>, Option<SignedPolicy>, Vec<TagRef>); |
| 1101 | |
|
| 1102 | 35× | pub fn decode_push(bytes: &[u8]) -> Result<PushParts> { |
| 1103 | 35× | let v = cbor::decode(bytes).map_err(ObjectError::from)?; |
| 1104 | 35× | let m = map(&v, "push")?; |
| 1105 | 35× | let bundle = bundle_from_value(field(m, 0, "bundle")?)?; |
| 1106 | 35× | let mut updates = Vec::new(); |
| 1107 | 35× | for u in o::array(field(m, 1, "updates")?, "updates")? { |
| 1108 | 35× | let um = map(u, "update")?; |
| 1109 | 35× | updates.push(RefUpdate { |
| 1110 | 35× | name: o::text(field(um, 0, "name")?, "name")?.to_owned(), |
| 1111 | 35× | old_state: o::oref(field(um, 1, "old_state")?, "old_state")?, |
| 1112 | 35× | new_state: o::oref(field(um, 2, "new_state")?, "new_state")?, |
| 1113 | 35× | force: field(um, 3, "force")? |
| 1114 | 35× | .as_bool() |
| 1115 | 35× | .ok_or_else(|| schema("force: expected bool"))?, |
| 1116 | 35× | attestation: opt_attest_from(field(um, 4, "attestation")?)?, |
| 1117 | | }); |
| 1118 | | } |
| 1119 | 35× | let policy = match m.get(&2) { |
| 1120 | 35× | Some(p) => opt_policy_from(p)?, |
| 1121 | 0× | None => None, |
| 1122 | | }; |
| 1123 | 35× | let tags = match m.get(&3) { |
| 1124 | 35× | Some(t) => tags_from(t)?, |
| 1125 | 0× | None => Vec::new(), |
| 1126 | | }; |
| 1127 | 35× | Ok((bundle, updates, policy, tags)) |
| 1128 | 35× | } |
| 1129 | |
|
| 1130 | 35× | pub fn encode_push_response(results: &[RefResult]) -> Vec<u8> { |
| 1131 | 35× | cbor::encode(&Value::map([( |
| 1132 | | 0, |
| 1133 | | Value::Array( |
| 1134 | 35× | results |
| 1135 | 35× | .iter() |
| 1136 | 36× | .map(|r| { |
| 1137 | 36× | Value::map([ |
| 1138 | 36× | (0, Value::Text(r.name.clone())), |
| 1139 | 36× | (1, Value::Bool(r.ok)), |
| 1140 | 36× | (2, Value::Text(r.reason.clone())), |
| 1141 | 36× | ]) |
| 1142 | 36× | }) |
| 1143 | 35× | .collect(), |
| 1144 | | ), |
| 1145 | | )])) |
| 1146 | 35× | } |
| 1147 | 35× | pub fn decode_push_response(bytes: &[u8]) -> Result<Vec<RefResult>> { |
| 1148 | 35× | let v = cbor::decode(bytes).map_err(ObjectError::from)?; |
| 1149 | 35× | let m = map(&v, "push response")?; |
| 1150 | 35× | let mut out = Vec::new(); |
| 1151 | 36× | for r in o::array(field(m, 0, "results")?, "results")? { |
| 1152 | 36× | let rm = map(r, "result")?; |
| 1153 | 36× | out.push(RefResult { |
| 1154 | 36× | name: o::text(field(rm, 0, "name")?, "name")?.to_owned(), |
| 1155 | 36× | ok: field(rm, 1, "ok")? |
| 1156 | 36× | .as_bool() |
| 1157 | 36× | .ok_or_else(|| schema("ok: expected bool"))?, |
| 1158 | 36× | reason: o::text(field(rm, 2, "reason")?, "reason")?.to_owned(), |
| 1159 | | }); |
| 1160 | | } |
| 1161 | 35× | Ok(out) |
| 1162 | 35× | } |
| 1163 | | } |
| 1164 | |
|
| 1165 | | #[cfg(test)] |
| 1166 | | mod tests { |
| 1167 | | use super::*; |
| 1168 | | use crate::attest::AttestBody; |
| 1169 | | use crate::objects::Hlc; |
| 1170 | |
|
| 1171 | | #[test] |
| 1172 | 1× | fn wire_round_trips() { |
| 1173 | 1× | let c = crate::objects::tests::sample_change(); |
| 1174 | 1× | let sk = crate::identity::SecretKey { |
| 1175 | 1× | ed_seed: [1; 32], |
| 1176 | 1× | ml_seed: [2; 32], |
| 1177 | 1× | }; |
| 1178 | 1× | let id = sk.identity("a", "b", 0); |
| 1179 | 1× | let key = id.hash(); |
| 1180 | 1× | let sig = sk.sign(crate::identity::Domain::Change, &key, &c.hash()); |
| 1181 | 1× | let seal = Attest::seal([3; 16], vec![c.hash()]); |
| 1182 | 1× | let seal_sig = sk.sign(crate::identity::Domain::Attest, &key, &seal.hash()); |
| 1183 | 1× | let policy = Policy::single_root([3; 16], id.clone(), 99); |
| 1184 | 1× | let psig = sk.sign(crate::identity::Domain::Policy, &key, &policy.hash()); |
| 1185 | 1× | let bundle = Bundle { |
| 1186 | 1× | changes: vec![(c.clone(), vec![sig.clone()])], |
| 1187 | 1× | attests: vec![SignedAttest { |
| 1188 | 1× | attest: seal.clone(), |
| 1189 | 1× | sigs: vec![seal_sig], |
| 1190 | 1× | }], |
| 1191 | 1× | objects: vec![ |
| 1192 | 1× | (ObjectType::Identity, id.encode()), |
| 1193 | 1× | (ObjectType::Chunk, b"xyz".to_vec()), |
| 1194 | 1× | ], |
| 1195 | 1× | policies: vec![SignedPolicy { |
| 1196 | 1× | policy: policy.clone(), |
| 1197 | 1× | sigs: vec![psig.clone()], |
| 1198 | 1× | }], |
| 1199 | 1× | hashes: vec![], |
| 1200 | 1× | raw_changes: vec![], |
| 1201 | 1× | certificates: vec![], |
| 1202 | 1× | }; |
| 1203 | 1× | let d = wire::decode_bundle(&wire::encode_bundle(&bundle)).unwrap(); |
| 1204 | 1× | assert_eq!( |
| 1205 | | d.hashes, |
| 1206 | 1× | vec![c.hash()], |
| 1207 | | "the receiver hashes what it was sent" |
| 1208 | | ); |
| 1209 | 1× | assert_eq!(d.changes, bundle.changes); |
| 1210 | 1× | assert_eq!(d.attests, bundle.attests); |
| 1211 | 1× | assert_eq!(d.objects, bundle.objects); |
| 1212 | 1× | assert_eq!(d.policies, bundle.policies); |
| 1213 | 1× | assert!(d.certificates.is_empty()); |
| 1214 | | |
| 1215 | | |
| 1216 | 1× | let plain = wire::encode_bundle(&bundle); |
| 1217 | 1× | let mut with = bundle.clone(); |
| 1218 | 1× | let issuer_sk = crate::identity::SecretKey::generate(); |
| 1219 | 1× | let issuer = issuer_sk.identity("hub", "h@x", 1); |
| 1220 | 1× | with.certificates.push(crate::cert::SignedCertificate::sign( |
| 1221 | 1× | crate::cert::Certificate { |
| 1222 | 1× | key: id.clone(), |
| 1223 | 1× | issuer: issuer.hash(), |
| 1224 | 1× | subject: "agent:a@b".into(), |
| 1225 | 1× | claims: vec![], |
| 1226 | 1× | scope: crate::identity::Scope::CHANGES, |
| 1227 | 1× | not_before: 1, |
| 1228 | 1× | expires: 2, |
| 1229 | 1× | grant: None, |
| 1230 | 1× | }, |
| 1231 | 1× | &issuer, |
| 1232 | 1× | &issuer_sk, |
| 1233 | | )); |
| 1234 | 1× | let encoded = wire::encode_bundle(&with); |
| 1235 | 1× | assert_ne!(encoded, plain); |
| 1236 | 1× | assert_eq!( |
| 1237 | 1× | wire::decode_bundle(&encoded).unwrap().certificates, |
| 1238 | | with.certificates |
| 1239 | | ); |
| 1240 | 1× | with.certificates.clear(); |
| 1241 | 1× | assert_eq!(wire::encode_bundle(&with), plain); |
| 1242 | |
|
| 1243 | 1× | let att = Attest { |
| 1244 | 1× | repo: [3; 16], |
| 1245 | 1× | hlc: Hlc::default(), |
| 1246 | 1× | body: AttestBody::Ref { |
| 1247 | 1× | name: "main".into(), |
| 1248 | 1× | old_state: ObjectRef([1; 32]), |
| 1249 | 1× | new_state: ObjectRef([2; 32]), |
| 1250 | 1× | serial: 4, |
| 1251 | 1× | }, |
| 1252 | 1× | }; |
| 1253 | 1× | let refs = vec![ |
| 1254 | 1× | RemoteRef { |
| 1255 | 1× | name: "main".into(), |
| 1256 | 1× | state: ObjectRef([2; 32]), |
| 1257 | 1× | heads: vec![c.hash()], |
| 1258 | 1× | attestation: Some(SignedAttest { |
| 1259 | 1× | attest: att.clone(), |
| 1260 | 1× | sigs: vec![sig.clone()], |
| 1261 | 1× | }), |
| 1262 | 1× | last_serial: Some(4), |
| 1263 | 1× | accepted: None, |
| 1264 | 1× | }, |
| 1265 | 1× | RemoteRef { |
| 1266 | 1× | name: "dev".into(), |
| 1267 | 1× | state: ObjectRef([5; 32]), |
| 1268 | 1× | heads: vec![], |
| 1269 | 1× | attestation: None, |
| 1270 | 1× | last_serial: None, |
| 1271 | 1× | accepted: None, |
| 1272 | 1× | }, |
| 1273 | | ]; |
| 1274 | 1× | let tag = TagRef { |
| 1275 | 1× | name: "v1".into(), |
| 1276 | 1× | state: ObjectRef([2; 32]), |
| 1277 | 1× | heads: vec![c.hash()], |
| 1278 | 1× | attestation: None, |
| 1279 | 1× | }; |
| 1280 | 1× | let resp = RefsResponse { |
| 1281 | 1× | refs, |
| 1282 | 1× | policy: Some(SignedPolicy { |
| 1283 | 1× | policy: policy.clone(), |
| 1284 | 1× | sigs: vec![psig.clone()], |
| 1285 | 1× | }), |
| 1286 | 1× | tags: vec![tag.clone()], |
| 1287 | 1× | history: vec![SignedPolicy { |
| 1288 | 1× | policy: policy.clone(), |
| 1289 | 1× | sigs: vec![], |
| 1290 | 1× | }], |
| 1291 | 1× | default_bookmark: Some("develop".into()), |
| 1292 | 1× | }; |
| 1293 | 1× | let (id_back, back) = wire::decode_refs(&wire::encode_refs([3; 16], &resp)).unwrap(); |
| 1294 | 1× | assert_eq!(id_back, [3; 16]); |
| 1295 | 1× | assert_eq!(back.refs.len(), 2); |
| 1296 | 1× | assert_eq!(back.refs[0].attestation.as_ref().unwrap().attest, att); |
| 1297 | 1× | assert_eq!(back.refs[1].last_serial, None); |
| 1298 | 1× | assert_eq!(back.policy.as_ref().unwrap().policy, policy); |
| 1299 | 1× | assert_eq!(back.history, resp.history); |
| 1300 | 1× | assert_eq!(back.tags, vec![tag.clone()]); |
| 1301 | |
|
| 1302 | 1× | let (h, w) = wire::decode_negotiate(&wire::encode_negotiate( |
| 1303 | 1× | &[ObjectRef([1; 32])], |
| 1304 | 1× | &[ObjectRef([2; 32])], |
| 1305 | 1× | )) |
| 1306 | 1× | .unwrap(); |
| 1307 | 1× | assert_eq!((h, w), (vec![ObjectRef([1; 32])], vec![ObjectRef([2; 32])])); |
| 1308 | 1× | assert_eq!( |
| 1309 | 1× | wire::decode_hashes(&wire::encode_hashes(&[c.hash()])).unwrap(), |
| 1310 | 1× | vec![c.hash()] |
| 1311 | | ); |
| 1312 | 1× | assert_eq!( |
| 1313 | 1× | wire::decode_fetch(&wire::encode_fetch(&[c.hash()], Some(4096))).unwrap(), |
| 1314 | 1× | (vec![c.hash()], Some(4096)) |
| 1315 | | ); |
| 1316 | 1× | assert_eq!( |
| 1317 | 1× | wire::decode_fetch(&wire::encode_fetch(&[], None)).unwrap(), |
| 1318 | 1× | (vec![], None) |
| 1319 | | ); |
| 1320 | 1× | assert_eq!( |
| 1321 | 1× | wire::decode_shallow(&wire::encode_shallow(&[c.hash()], 3)).unwrap(), |
| 1322 | 1× | (vec![c.hash()], 3) |
| 1323 | | ); |
| 1324 | | |
| 1325 | 1× | assert_eq!( |
| 1326 | 1× | wire::decode_fetch(&wire::encode_hashes(&[c.hash()])).unwrap(), |
| 1327 | 1× | (vec![c.hash()], None) |
| 1328 | | ); |
| 1329 | |
|
| 1330 | 1× | let up = RefUpdate { |
| 1331 | 1× | name: "main".into(), |
| 1332 | 1× | old_state: ObjectRef([1; 32]), |
| 1333 | 1× | new_state: ObjectRef([2; 32]), |
| 1334 | 1× | force: true, |
| 1335 | 1× | attestation: Some(SignedAttest { |
| 1336 | 1× | attest: att, |
| 1337 | 1× | sigs: vec![sig], |
| 1338 | 1× | }), |
| 1339 | 1× | }; |
| 1340 | 1× | let sp = SignedPolicy { |
| 1341 | 1× | policy, |
| 1342 | 1× | sigs: vec![psig], |
| 1343 | 1× | }; |
| 1344 | 1× | let (b2, ups, pol, tags) = wire::decode_push(&wire::encode_push( |
| 1345 | 1× | &bundle, |
| 1346 | 1× | std::slice::from_ref(&up), |
| 1347 | 1× | Some(&sp), |
| 1348 | 1× | std::slice::from_ref(&tag), |
| 1349 | 1× | )) |
| 1350 | 1× | .unwrap(); |
| 1351 | 1× | assert_eq!(pol, Some(sp)); |
| 1352 | 1× | assert_eq!(tags, vec![tag]); |
| 1353 | 1× | assert_eq!(b2.changes.len(), 1); |
| 1354 | 1× | assert_eq!(ups[0].name, "main"); |
| 1355 | 1× | assert!(ups[0].force); |
| 1356 | 1× | assert_eq!(ups[0].attestation, up.attestation); |
| 1357 | 1× | let res = vec![RefResult { |
| 1358 | 1× | name: "main".into(), |
| 1359 | 1× | ok: false, |
| 1360 | 1× | reason: "stale".into(), |
| 1361 | 1× | }]; |
| 1362 | 1× | assert_eq!( |
| 1363 | 1× | wire::decode_push_response(&wire::encode_push_response(&res)).unwrap(), |
| 1364 | | res |
| 1365 | | ); |
| 1366 | 1× | assert_eq!( |
| 1367 | 1× | wire::decode_capabilities(&wire::encode_capabilities([7; 16])).unwrap(), |
| 1368 | | [7; 16] |
| 1369 | | ); |
| 1370 | 1× | } |
| 1371 | | } |