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