OXYGENHUB
# 1197a869a047 oxygen / crates / oxy-core / src / protocol.rs
33.9 KiB · Rust · last changed Unbound hosted repositories adopt the first pusher's id 1 month ago source blame history raw
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
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`.
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;
18
19
pub const CONTENT_TYPE: &str = "application/oxy-cbor";
20
pub const PROTOCOL_VERSION: u64 = 1;
21
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
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
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> {
42
        wire::policy_from(
43
            &crate::cbor::decode(b).map_err(|e| RepoError::Object(ObjectError::from(e)))?,
44
        )
45
    }
46
}
47
48
/// What `refs` returns: every bookmark, every tag, and the current policy.
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
    /// 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>,
57
}
58
59
/// A tag as a remote reports it (spec §10.1).
60
#[derive(Clone, Debug, PartialEq, Eq)]
61
pub struct TagRef {
62
    pub name: String,
63
    pub state: ObjectRef,
64
    pub heads: Vec<ChangeHash>,
65
    pub attestation: Option<SignedAttest>,
66
}
67
68
/// One reference as a remote reports it.
69
#[derive(Clone, Debug)]
70
pub struct RemoteRef {
71
    pub name: String,
72
    pub state: ObjectRef,
73
    pub heads: Vec<ChangeHash>,
74
    pub attestation: Option<SignedAttest>,
75
}
76
77
/// Everything needed to add a set of changes to a repository.
78
#[derive(Clone, Debug, Default)]
79
pub struct Bundle {
80
    /// In dependency order, each with its individual signatures.
81
    pub changes: Vec<(Change, Vec<Signature>)>,
82
    /// Seals covering the changes.
83
    pub attests: Vec<SignedAttest>,
84
    /// Chunks, blobs, identities, and state objects referenced by the changes.
85
    pub objects: Vec<(ObjectType, Vec<u8>)>,
86
    /// Policies between the receiver's and the one sent with the push,
87
    /// ascending by serial (spec §14.5).
88
    pub policies: Vec<SignedPolicy>,
89
}
90
91
impl Bundle {
92
    pub fn is_empty(&self) -> bool {
93
        self.changes.is_empty() && self.attests.is_empty() && self.objects.is_empty()
94
    }
95
}
96
97
/// A requested bookmark move (spec §10.2, §16.3).
98
#[derive(Clone, Debug)]
99
pub struct RefUpdate {
100
    pub name: String,
101
    pub old_state: ObjectRef,
102
    pub new_state: ObjectRef,
103
    pub force: bool,
104
    pub attestation: Option<SignedAttest>,
105
}
106
107
#[derive(Clone, Debug, PartialEq, Eq)]
108
pub struct RefResult {
109
    pub name: String,
110
    pub ok: bool,
111
    pub reason: String,
112
}
113
114
pub trait Remote {
115
    fn repo_id(&self) -> Result<[u8; 16]>;
116
    fn refs(&self) -> Result<RefsResponse>;
117
    /// Changes in the closure of `want` but not of `have`, in dependency order.
118
    fn negotiate(&self, have: &[ChangeHash], want: &[ChangeHash]) -> Result<Vec<ChangeHash>>;
119
    /// The changes with their signatures, seals, identities, and blobs; blobs
120
    /// larger than `max_blob` bytes are left out for lazy fetching (spec §13.5).
121
    fn fetch(&self, changes: &[ChangeHash], max_blob: Option<u64>) -> Result<Bundle>;
122
    /// Individual objects by reference (spec §16.1 `objects`); a blob comes
123
    /// with its chunks.
124
    fn objects(&self, refs: &[ObjectRef]) -> Result<Vec<(ObjectType, Vec<u8>)>>;
125
    /// A checkpoint of exactly `heads` (spec §16.1 `checkpoint`): the
126
    /// checkpoint object, its payload blob, and the blob's chunks.
127
    fn checkpoint(&self, heads: &[ChangeHash]) -> Result<Bundle>;
128
    /// The changes within `depth` dependency steps of `heads` (spec §17).
129
    fn shallow(&self, heads: &[ChangeHash], depth: u32) -> Result<Vec<ChangeHash>>;
130
    /// Push changes, bookmark updates, new tags, and optionally a newer
131
    /// policy. The policy is applied first; a result named `policy` reports
132
    /// it, and each tag reports as `tags/<name>`.
133
    fn push(
134
        &self,
135
        bundle: Bundle,
136
        updates: Vec<RefUpdate>,
137
        policy: Option<SignedPolicy>,
138
        tags: Vec<TagRef>,
139
    ) -> Result<Vec<RefResult>>;
140
}
141
142
// ---------------------------------------------------------------- local
143
144
/// Another repository on this machine, served in-process.
145
pub struct LocalRemote {
146
    pub repo: Repo,
147
}
148
149
impl LocalRemote {
150
    pub fn open(path: &Path) -> Result<LocalRemote> {
151
        Ok(LocalRemote {
152
            repo: Repo::open(path)?,
153
        })
154
    }
155
}
156
157
impl Remote for LocalRemote {
158
    fn repo_id(&self) -> Result<[u8; 16]> {
159
        self.repo.id()
160
    }
161
    fn refs(&self) -> Result<RefsResponse> {
162
        self.repo.remote_refs()
163
    }
164
    fn negotiate(&self, have: &[ChangeHash], want: &[ChangeHash]) -> Result<Vec<ChangeHash>> {
165
        self.repo.negotiate(have, want)
166
    }
167
    fn fetch(&self, changes: &[ChangeHash], max_blob: Option<u64>) -> Result<Bundle> {
168
        self.repo.bundle_for_filtered(changes, max_blob)
169
    }
170
    fn objects(&self, refs: &[ObjectRef]) -> Result<Vec<(ObjectType, Vec<u8>)>> {
171
        self.repo.objects_for(refs)
172
    }
173
    fn checkpoint(&self, heads: &[ChangeHash]) -> Result<Bundle> {
174
        self.repo.checkpoint_bundle_for(heads)
175
    }
176
    fn shallow(&self, heads: &[ChangeHash], depth: u32) -> Result<Vec<ChangeHash>> {
177
        self.repo.shallow_changes(heads, depth)
178
    }
179
    fn push(
180
        &self,
181
        bundle: Bundle,
182
        updates: Vec<RefUpdate>,
183
        policy: Option<SignedPolicy>,
184
        tags: Vec<TagRef>,
185
    ) -> Result<Vec<RefResult>> {
186
        self.repo.receive_push(bundle, updates, policy, tags)
187
    }
188
}
189
190
// ---------------------------------------------------------------- http
191
192
/// How to reach a remote: credentials and trust.
193
#[derive(Clone, Debug, Default)]
194
pub struct RemoteOptions {
195
    /// Bearer token for pushes.
196
    pub token: Option<String>,
197
    /// PEM file with extra root certificates to trust for `https://` remotes
198
    /// (self-hosted servers); the system web roots are always trusted.
199
    pub ca: Option<PathBuf>,
200
    /// The local repository's id, sent as `Oxy-Repo-Id` so an unbound
201
    /// hosted repository can adopt it on the first push (spec §16.1).
202
    pub repo_id: Option<[u8; 16]>,
203
}
204
205
/// The request header carrying the client's repository id.
206
pub const REPO_ID_HEADER: &str = "Oxy-Repo-Id";
207
208
/// The id of a hosted repository that has not adopted one yet.
209
pub const UNBOUND_ID: [u8; 16] = [0; 16];
210
211
/// A remote served by `oxy-server` at `<base>/oxy/v1/...`.
212
pub struct HttpRemote {
213
    base: String,
214
    token: Option<String>,
215
    repo_id: Option<[u8; 16]>,
216
    agent: ureq::Agent,
217
}
218
219
/// A TLS client configuration trusting the web roots plus `ca`, if given.
220
fn tls_client_config(ca: Option<&Path>) -> Result<Arc<rustls::ClientConfig>> {
221
    let mut roots = rustls::RootCertStore::empty();
222
    roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
223
    if let Some(path) = ca {
224
        let pem = std::fs::read(path)
225
            .map_err(|e| RepoError::Msg(format!("reading CA file {}: {e}", path.display())))?;
226
        let certs = rustls_pemfile::certs(&mut pem.as_slice())
227
            .collect::<std::result::Result<Vec<_>, _>>()
228
            .map_err(|e| RepoError::Msg(format!("parsing CA file {}: {e}", path.display())))?;
229
        if certs.is_empty() {
230
            return Err(RepoError::Msg(format!(
231
                "CA file {} holds no certificates",
232
                path.display()
233
            )));
234
        }
235
        for c in certs {
236
            roots
237
                .add(c)
238
                .map_err(|e| RepoError::Msg(format!("CA file {}: {e}", path.display())))?;
239
        }
240
    }
241
    let cfg = rustls::ClientConfig::builder_with_provider(Arc::new(
242
        rustls::crypto::ring::default_provider(),
243
    ))
244
    .with_safe_default_protocol_versions()
245
    .map_err(|e| RepoError::Msg(e.to_string()))?
246
    .with_root_certificates(roots)
247
    .with_no_client_auth();
248
    Ok(Arc::new(cfg))
249
}
250
251
impl HttpRemote {
252
    pub fn new(base: &str, options: &RemoteOptions) -> Result<HttpRemote> {
253
        let agent = ureq::AgentBuilder::new()
254
            .tls_config(tls_client_config(options.ca.as_deref())?)
255
            .build();
256
        Ok(HttpRemote {
257
            base: base.trim_end_matches('/').to_string(),
258
            token: options.token.clone(),
259
            repo_id: options.repo_id,
260
            agent,
261
        })
262
    }
263
264
    fn call(&self, post: bool, endpoint: &str, body: Option<&[u8]>) -> Result<Vec<u8>> {
265
        let url = format!("{}/oxy/v1/{endpoint}", self.base);
266
        let mut req = if post {
267
            self.agent.post(&url)
268
        } else {
269
            self.agent.get(&url)
270
        };
271
        req = req.set("Accept", CONTENT_TYPE);
272
        if let Some(t) = &self.token {
273
            req = req.set("Authorization", &format!("Bearer {t}"));
274
        }
275
        if let Some(id) = &self.repo_id {
276
            req = req.set(REPO_ID_HEADER, &hex::encode(id));
277
        }
278
        let resp = match body {
279
            Some(b) => req.set("Content-Type", CONTENT_TYPE).send_bytes(b),
280
            None => req.call(),
281
        };
282
        match resp {
283
            Ok(r) => {
284
                let mut out = Vec::new();
285
                r.into_reader()
286
                    .read_to_end(&mut out)
287
                    .map_err(|e| RepoError::Msg(format!("{url}: {e}")))?;
288
                Ok(out)
289
            }
290
            Err(ureq::Error::Status(code, r)) => {
291
                let text = r.into_string().unwrap_or_default();
292
                Err(RepoError::Msg(format!(
293
                    "{url}: HTTP {code}: {}",
294
                    text.trim()
295
                )))
296
            }
297
            Err(e) => Err(RepoError::Msg(format!("{url}: {e}"))),
298
        }
299
    }
300
}
301
302
impl Remote for HttpRemote {
303
    fn repo_id(&self) -> Result<[u8; 16]> {
304
        wire::decode_capabilities(&self.call(false, "capabilities", None)?)
305
    }
306
    fn refs(&self) -> Result<RefsResponse> {
307
        Ok(wire::decode_refs(&self.call(false, "refs", None)?)?.1)
308
    }
309
    fn negotiate(&self, have: &[ChangeHash], want: &[ChangeHash]) -> Result<Vec<ChangeHash>> {
310
        wire::decode_hashes(&self.call(
311
            true,
312
            "negotiate",
313
            Some(&wire::encode_negotiate(have, want)),
314
        )?)
315
    }
316
    fn fetch(&self, changes: &[ChangeHash], max_blob: Option<u64>) -> Result<Bundle> {
317
        wire::decode_bundle(&self.call(
318
            true,
319
            "fetch",
320
            Some(&wire::encode_fetch(changes, max_blob)),
321
        )?)
322
    }
323
    fn objects(&self, refs: &[ObjectRef]) -> Result<Vec<(ObjectType, Vec<u8>)>> {
324
        Ok(
325
            wire::decode_bundle(&self.call(true, "objects", Some(&wire::encode_hashes(refs)))?)?
326
                .objects,
327
        )
328
    }
329
    fn checkpoint(&self, heads: &[ChangeHash]) -> Result<Bundle> {
330
        wire::decode_bundle(&self.call(true, "checkpoint", Some(&wire::encode_hashes(heads)))?)
331
    }
332
    fn shallow(&self, heads: &[ChangeHash], depth: u32) -> Result<Vec<ChangeHash>> {
333
        wire::decode_hashes(&self.call(
334
            true,
335
            "shallow",
336
            Some(&wire::encode_shallow(heads, depth)),
337
        )?)
338
    }
339
    fn push(
340
        &self,
341
        bundle: Bundle,
342
        updates: Vec<RefUpdate>,
343
        policy: Option<SignedPolicy>,
344
        tags: Vec<TagRef>,
345
    ) -> Result<Vec<RefResult>> {
346
        wire::decode_push_response(&self.call(
347
            true,
348
            "push",
349
            Some(&wire::encode_push(
350
                &bundle,
351
                &updates,
352
                policy.as_ref(),
353
                &tags,
354
            )),
355
        )?)
356
    }
357
}
358
359
/// Open a remote by URL: a local path (optionally `file://`), or `http(s)://`.
360
pub fn open_remote(url: &str, options: &RemoteOptions) -> Result<Box<dyn Remote>> {
361
    if url.starts_with("http://") || url.starts_with("https://") {
362
        return Ok(Box::new(HttpRemote::new(url, options)?));
363
    }
364
    let path = url.strip_prefix("file://").unwrap_or(url);
365
    if path.contains("://") {
366
        return Err(RepoError::Msg(format!("unsupported remote URL {url:?}")));
367
    }
368
    Ok(Box::new(LocalRemote::open(Path::new(path))?))
369
}
370
371
// ---------------------------------------------------------------- wire
372
373
/// CBOR encodings of the protocol messages (spec §16.1).
374
pub mod wire {
375
    use super::*;
376
377
    fn schema(msg: &str) -> RepoError {
378
        RepoError::Object(ObjectError::Schema(msg.into()))
379
    }
380
    fn sigs_value(sigs: &[Signature]) -> Value {
381
        Value::Array(sigs.iter().map(|s| s.to_value()).collect())
382
    }
383
    fn sigs_from(v: &Value) -> Result<Vec<Signature>> {
384
        Ok(o::array(v, "signatures")?
385
            .iter()
386
            .map(Signature::from_value)
387
            .collect::<std::result::Result<Vec<_>, _>>()?)
388
    }
389
    fn hashes_value(hs: &[ObjectRef]) -> Value {
390
        Value::Array(hs.iter().map(|h| Value::Bytes(h.0.to_vec())).collect())
391
    }
392
    fn hashes_from(v: &Value) -> Result<Vec<ObjectRef>> {
393
        Ok(o::array(v, "hashes")?
394
            .iter()
395
            .map(|h| o::oref(h, "hash"))
396
            .collect::<std::result::Result<Vec<_>, _>>()?)
397
    }
398
    fn attest_value(sa: &SignedAttest) -> Value {
399
        Value::Array(vec![Value::Bytes(sa.attest.encode()), sigs_value(&sa.sigs)])
400
    }
401
    fn attest_from(v: &Value) -> Result<SignedAttest> {
402
        let a = o::array(v, "signed attest")?;
403
        if a.len() != 2 {
404
            return Err(schema("signed attest: expected [attest, sigs]"));
405
        }
406
        Ok(SignedAttest {
407
            attest: Attest::decode(o::bytes(&a[0], "attest")?)?,
408
            sigs: sigs_from(&a[1])?,
409
        })
410
    }
411
    fn opt_attest_value(sa: &Option<SignedAttest>) -> Value {
412
        sa.as_ref().map(attest_value).unwrap_or(Value::Null)
413
    }
414
    fn opt_attest_from(v: &Value) -> Result<Option<SignedAttest>> {
415
        if v.is_null() {
416
            Ok(None)
417
        } else {
418
            attest_from(v).map(Some)
419
        }
420
    }
421
    /// `[policy bytes, [signature]]`: also the format of a policy proposal
422
    /// file exchanged between root holders (spec §14.5).
423
    pub fn policy_value(sp: &SignedPolicy) -> Value {
424
        Value::Array(vec![Value::Bytes(sp.policy.encode()), sigs_value(&sp.sigs)])
425
    }
426
    pub fn policy_from(v: &Value) -> Result<SignedPolicy> {
427
        let a = o::array(v, "signed policy")?;
428
        if a.len() != 2 {
429
            return Err(schema("signed policy: expected [policy, sigs]"));
430
        }
431
        Ok(SignedPolicy {
432
            policy: Policy::decode(o::bytes(&a[0], "policy")?)?,
433
            sigs: sigs_from(&a[1])?,
434
        })
435
    }
436
    fn opt_policy_value(sp: Option<&SignedPolicy>) -> Value {
437
        sp.map(policy_value).unwrap_or(Value::Null)
438
    }
439
    fn opt_policy_from(v: &Value) -> Result<Option<SignedPolicy>> {
440
        if v.is_null() {
441
            Ok(None)
442
        } else {
443
            policy_from(v).map(Some)
444
        }
445
    }
446
    fn map<'a>(v: &'a Value, what: &str) -> Result<&'a o::Map> {
447
        Ok(o::map(v, what)?)
448
    }
449
    fn field<'a>(m: &'a o::Map, k: u64, what: &str) -> Result<&'a Value> {
450
        Ok(o::field(m, k, what)?)
451
    }
452
453
    pub fn bundle_value(b: &Bundle) -> Value {
454
        Value::map([
455
            (
456
                0,
457
                Value::Array(
458
                    b.changes
459
                        .iter()
460
                        .map(|(c, s)| Value::Array(vec![Value::Bytes(c.encode()), sigs_value(s)]))
461
                        .collect(),
462
                ),
463
            ),
464
            (
465
                1,
466
                Value::Array(b.attests.iter().map(attest_value).collect()),
467
            ),
468
            (
469
                2,
470
                Value::Array(
471
                    b.objects
472
                        .iter()
473
                        .map(|(t, body)| {
474
                            Value::Array(vec![
475
                                Value::Text(t.name().into()),
476
                                Value::Bytes(body.clone()),
477
                            ])
478
                        })
479
                        .collect(),
480
                ),
481
            ),
482
            (
483
                3,
484
                Value::Array(b.policies.iter().map(policy_value).collect()),
485
            ),
486
        ])
487
    }
488
489
    pub fn bundle_from_value(v: &Value) -> Result<Bundle> {
490
        let m = map(v, "bundle")?;
491
        let mut b = Bundle::default();
492
        for c in o::array(field(m, 0, "changes")?, "changes")? {
493
            let pair = o::array(c, "change entry")?;
494
            if pair.len() != 2 {
495
                return Err(schema("change entry: expected [change, sigs]"));
496
            }
497
            b.changes.push((
498
                Change::decode(o::bytes(&pair[0], "change")?)?,
499
                sigs_from(&pair[1])?,
500
            ));
501
        }
502
        for a in o::array(field(m, 1, "attests")?, "attests")? {
503
            b.attests.push(attest_from(a)?);
504
        }
505
        for ob in o::array(field(m, 2, "objects")?, "objects")? {
506
            let pair = o::array(ob, "object")?;
507
            if pair.len() != 2 {
508
                return Err(schema("object: expected [type, body]"));
509
            }
510
            let ty = ObjectType::from_name(o::text(&pair[0], "object type")?)
511
                .ok_or_else(|| schema("unknown object type"))?;
512
            b.objects
513
                .push((ty, o::bytes(&pair[1], "object body")?.to_vec()));
514
        }
515
        if let Some(ps) = m.get(&3) {
516
            for p in o::array(ps, "policies")? {
517
                b.policies.push(policy_from(p)?);
518
            }
519
        }
520
        Ok(b)
521
    }
522
523
    pub fn encode_bundle(b: &Bundle) -> Vec<u8> {
524
        cbor::encode(&bundle_value(b))
525
    }
526
    pub fn decode_bundle(bytes: &[u8]) -> Result<Bundle> {
527
        bundle_from_value(&cbor::decode(bytes).map_err(ObjectError::from)?)
528
    }
529
530
    pub fn encode_capabilities(id: [u8; 16]) -> Vec<u8> {
531
        cbor::encode(&Value::map([
532
            (0, Value::Array(vec![Value::Uint(PROTOCOL_VERSION)])),
533
            (1, Value::Bytes(id.to_vec())),
534
            (2, Value::Array(vec![Value::Text("seal".into())])),
535
        ]))
536
    }
537
    pub fn decode_capabilities(bytes: &[u8]) -> Result<[u8; 16]> {
538
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
539
        let m = map(&v, "capabilities")?;
540
        let versions = o::array(field(m, 0, "versions")?, "versions")?;
541
        if !versions
542
            .iter()
543
            .any(|x| x.as_uint() == Some(PROTOCOL_VERSION))
544
        {
545
            return Err(RepoError::Msg(
546
                "remote does not speak protocol version 1".into(),
547
            ));
548
        }
549
        o::bytes(field(m, 1, "repo id")?, "repo id")?
550
            .try_into()
551
            .map_err(|_| schema("repo id: expected 16 bytes"))
552
    }
553
554
    fn tags_value(tags: &[TagRef]) -> Value {
555
        Value::Array(
556
            tags.iter()
557
                .map(|t| {
558
                    Value::map([
559
                        (0, Value::Text(t.name.clone())),
560
                        (1, Value::Bytes(t.state.0.to_vec())),
561
                        (2, hashes_value(&t.heads)),
562
                        (3, opt_attest_value(&t.attestation)),
563
                    ])
564
                })
565
                .collect(),
566
        )
567
    }
568
    fn tags_from(v: &Value) -> Result<Vec<TagRef>> {
569
        let mut out = Vec::new();
570
        for t in o::array(v, "tags")? {
571
            let tm = map(t, "tag")?;
572
            out.push(TagRef {
573
                name: o::text(field(tm, 0, "name")?, "name")?.to_owned(),
574
                state: o::oref(field(tm, 1, "state")?, "state")?,
575
                heads: hashes_from(field(tm, 2, "heads")?)?,
576
                attestation: opt_attest_from(field(tm, 3, "attestation")?)?,
577
            });
578
        }
579
        Ok(out)
580
    }
581
582
    pub fn encode_refs(id: [u8; 16], resp: &RefsResponse) -> Vec<u8> {
583
        cbor::encode(&Value::map([
584
            (
585
                0,
586
                Value::Array(
587
                    resp.refs
588
                        .iter()
589
                        .map(|r| {
590
                            Value::map([
591
                                (0, Value::Text(r.name.clone())),
592
                                (1, Value::Bytes(r.state.0.to_vec())),
593
                                (2, hashes_value(&r.heads)),
594
                                (3, opt_attest_value(&r.attestation)),
595
                                (4, r.last_serial.map(Value::Uint).unwrap_or(Value::Null)),
596
                            ])
597
                        })
598
                        .collect(),
599
                ),
600
            ),
601
            (1, Value::Bytes(id.to_vec())),
602
            (2, opt_policy_value(resp.policy.as_ref())),
603
            (3, tags_value(&resp.tags)),
604
            (
605
                4,
606
                Value::Array(resp.history.iter().map(policy_value).collect()),
607
            ),
608
        ]))
609
    }
610
    pub fn decode_refs(bytes: &[u8]) -> Result<([u8; 16], RefsResponse)> {
611
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
612
        let m = map(&v, "refs")?;
613
        let id: [u8; 16] = o::bytes(field(m, 1, "repo id")?, "repo id")?
614
            .try_into()
615
            .map_err(|_| schema("repo id: expected 16 bytes"))?;
616
        let mut refs = Vec::new();
617
        for r in o::array(field(m, 0, "refs")?, "refs")? {
618
            let rm = map(r, "ref")?;
619
            let ls = field(rm, 4, "last serial")?;
620
            refs.push(RemoteRef {
621
                name: o::text(field(rm, 0, "name")?, "name")?.to_owned(),
622
                state: o::oref(field(rm, 1, "state")?, "state")?,
623
                heads: hashes_from(field(rm, 2, "heads")?)?,
624
                attestation: opt_attest_from(field(rm, 3, "attestation")?)?,
625
                last_serial: if ls.is_null() {
626
                    None
627
                } else {
628
                    Some(o::uint(ls, "last serial")?)
629
                },
630
            });
631
        }
632
        let policy = match m.get(&2) {
633
            Some(p) => opt_policy_from(p)?,
634
            None => None,
635
        };
636
        let tags = match m.get(&3) {
637
            Some(t) => tags_from(t)?,
638
            None => Vec::new(),
639
        };
640
        let mut history = Vec::new();
641
        if let Some(h) = m.get(&4) {
642
            for p in o::array(h, "history")? {
643
                history.push(policy_from(p)?);
644
            }
645
        }
646
        Ok((
647
            id,
648
            RefsResponse {
649
                refs,
650
                policy,
651
                tags,
652
                history,
653
            },
654
        ))
655
    }
656
657
    pub fn encode_negotiate(have: &[ChangeHash], want: &[ChangeHash]) -> Vec<u8> {
658
        cbor::encode(&Value::map([
659
            (0, hashes_value(have)),
660
            (1, hashes_value(want)),
661
        ]))
662
    }
663
    pub fn decode_negotiate(bytes: &[u8]) -> Result<(Vec<ChangeHash>, Vec<ChangeHash>)> {
664
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
665
        let m = map(&v, "negotiate")?;
666
        Ok((
667
            hashes_from(field(m, 0, "have")?)?,
668
            hashes_from(field(m, 1, "want")?)?,
669
        ))
670
    }
671
672
    /// `fetch` request: `{0: [changes], 1: max blob size or null}`.
673
    pub fn encode_fetch(hs: &[ChangeHash], max_blob: Option<u64>) -> Vec<u8> {
674
        cbor::encode(&Value::map([
675
            (0, hashes_value(hs)),
676
            (1, max_blob.map(Value::Uint).unwrap_or(Value::Null)),
677
        ]))
678
    }
679
    pub fn decode_fetch(bytes: &[u8]) -> Result<(Vec<ChangeHash>, Option<u64>)> {
680
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
681
        let m = map(&v, "fetch")?;
682
        let max = match m.get(&1) {
683
            Some(x) if !x.is_null() => Some(o::uint(x, "max blob")?),
684
            _ => None,
685
        };
686
        Ok((hashes_from(field(m, 0, "changes")?)?, max))
687
    }
688
689
    /// `shallow` request: `{0: [heads], 1: depth}`.
690
    pub fn encode_shallow(hs: &[ChangeHash], depth: u32) -> Vec<u8> {
691
        cbor::encode(&Value::map([
692
            (0, hashes_value(hs)),
693
            (1, Value::Uint(depth as u64)),
694
        ]))
695
    }
696
    pub fn decode_shallow(bytes: &[u8]) -> Result<(Vec<ChangeHash>, u32)> {
697
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
698
        let m = map(&v, "shallow")?;
699
        Ok((
700
            hashes_from(field(m, 0, "heads")?)?,
701
            o::u32_(field(m, 1, "depth")?, "depth")?,
702
        ))
703
    }
704
705
    pub fn encode_hashes(hs: &[ChangeHash]) -> Vec<u8> {
706
        cbor::encode(&Value::map([(0, hashes_value(hs))]))
707
    }
708
    pub fn decode_hashes(bytes: &[u8]) -> Result<Vec<ChangeHash>> {
709
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
710
        hashes_from(field(map(&v, "hashes")?, 0, "hashes")?)
711
    }
712
713
    pub fn encode_push(
714
        bundle: &Bundle,
715
        updates: &[RefUpdate],
716
        policy: Option<&SignedPolicy>,
717
        tags: &[TagRef],
718
    ) -> Vec<u8> {
719
        cbor::encode(&Value::map([
720
            (0, bundle_value(bundle)),
721
            (2, opt_policy_value(policy)),
722
            (3, tags_value(tags)),
723
            (
724
                1,
725
                Value::Array(
726
                    updates
727
                        .iter()
728
                        .map(|u| {
729
                            Value::map([
730
                                (0, Value::Text(u.name.clone())),
731
                                (1, Value::Bytes(u.old_state.0.to_vec())),
732
                                (2, Value::Bytes(u.new_state.0.to_vec())),
733
                                (3, Value::Bool(u.force)),
734
                                (4, opt_attest_value(&u.attestation)),
735
                            ])
736
                        })
737
                        .collect(),
738
                ),
739
            ),
740
        ]))
741
    }
742
    /// A decoded push: bundle, bookmark updates, policy, tags.
743
    pub type PushParts = (Bundle, Vec<RefUpdate>, Option<SignedPolicy>, Vec<TagRef>);
744
745
    pub fn decode_push(bytes: &[u8]) -> Result<PushParts> {
746
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
747
        let m = map(&v, "push")?;
748
        let bundle = bundle_from_value(field(m, 0, "bundle")?)?;
749
        let mut updates = Vec::new();
750
        for u in o::array(field(m, 1, "updates")?, "updates")? {
751
            let um = map(u, "update")?;
752
            updates.push(RefUpdate {
753
                name: o::text(field(um, 0, "name")?, "name")?.to_owned(),
754
                old_state: o::oref(field(um, 1, "old_state")?, "old_state")?,
755
                new_state: o::oref(field(um, 2, "new_state")?, "new_state")?,
756
                force: field(um, 3, "force")?
757
                    .as_bool()
758
                    .ok_or_else(|| schema("force: expected bool"))?,
759
                attestation: opt_attest_from(field(um, 4, "attestation")?)?,
760
            });
761
        }
762
        let policy = match m.get(&2) {
763
            Some(p) => opt_policy_from(p)?,
764
            None => None,
765
        };
766
        let tags = match m.get(&3) {
767
            Some(t) => tags_from(t)?,
768
            None => Vec::new(),
769
        };
770
        Ok((bundle, updates, policy, tags))
771
    }
772
773
    pub fn encode_push_response(results: &[RefResult]) -> Vec<u8> {
774
        cbor::encode(&Value::map([(
775
            0,
776
            Value::Array(
777
                results
778
                    .iter()
779
                    .map(|r| {
780
                        Value::map([
781
                            (0, Value::Text(r.name.clone())),
782
                            (1, Value::Bool(r.ok)),
783
                            (2, Value::Text(r.reason.clone())),
784
                        ])
785
                    })
786
                    .collect(),
787
            ),
788
        )]))
789
    }
790
    pub fn decode_push_response(bytes: &[u8]) -> Result<Vec<RefResult>> {
791
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
792
        let m = map(&v, "push response")?;
793
        let mut out = Vec::new();
794
        for r in o::array(field(m, 0, "results")?, "results")? {
795
            let rm = map(r, "result")?;
796
            out.push(RefResult {
797
                name: o::text(field(rm, 0, "name")?, "name")?.to_owned(),
798
                ok: field(rm, 1, "ok")?
799
                    .as_bool()
800
                    .ok_or_else(|| schema("ok: expected bool"))?,
801
                reason: o::text(field(rm, 2, "reason")?, "reason")?.to_owned(),
802
            });
803
        }
804
        Ok(out)
805
    }
806
}
807
808
#[cfg(test)]
809
mod tests {
810
    use super::*;
811
    use crate::attest::AttestBody;
812
    use crate::objects::Hlc;
813
814
    #[test]
815
    fn wire_round_trips() {
816
        let c = crate::objects::tests::sample_change();
817
        let sk = crate::identity::SecretKey {
818
            ed_seed: [1; 32],
819
            ml_seed: [2; 32],
820
        };
821
        let id = sk.identity("a", "b", 0);
822
        let key = id.hash();
823
        let sig = sk.sign(crate::identity::Domain::Change, &key, &c.hash());
824
        let seal = Attest::seal([3; 16], vec![c.hash()]);
825
        let seal_sig = sk.sign(crate::identity::Domain::Attest, &key, &seal.hash());
826
        let policy = Policy::single_root([3; 16], id.clone(), 99);
827
        let psig = sk.sign(crate::identity::Domain::Policy, &key, &policy.hash());
828
        let bundle = Bundle {
829
            changes: vec![(c.clone(), vec![sig.clone()])],
830
            attests: vec![SignedAttest {
831
                attest: seal.clone(),
832
                sigs: vec![seal_sig],
833
            }],
834
            objects: vec![
835
                (ObjectType::Identity, id.encode()),
836
                (ObjectType::Chunk, b"xyz".to_vec()),
837
            ],
838
            policies: vec![SignedPolicy {
839
                policy: policy.clone(),
840
                sigs: vec![psig.clone()],
841
            }],
842
        };
843
        let d = wire::decode_bundle(&wire::encode_bundle(&bundle)).unwrap();
844
        assert_eq!(d.changes, bundle.changes);
845
        assert_eq!(d.attests, bundle.attests);
846
        assert_eq!(d.objects, bundle.objects);
847
        assert_eq!(d.policies, bundle.policies);
848
849
        let att = Attest {
850
            repo: [3; 16],
851
            hlc: Hlc::default(),
852
            body: AttestBody::Ref {
853
                name: "main".into(),
854
                old_state: ObjectRef([1; 32]),
855
                new_state: ObjectRef([2; 32]),
856
                serial: 4,
857
            },
858
        };
859
        let refs = vec![
860
            RemoteRef {
861
                name: "main".into(),
862
                state: ObjectRef([2; 32]),
863
                heads: vec![c.hash()],
864
                attestation: Some(SignedAttest {
865
                    attest: att.clone(),
866
                    sigs: vec![sig.clone()],
867
                }),
868
                last_serial: Some(4),
869
            },
870
            RemoteRef {
871
                name: "dev".into(),
872
                state: ObjectRef([5; 32]),
873
                heads: vec![],
874
                attestation: None,
875
                last_serial: None,
876
            },
877
        ];
878
        let tag = TagRef {
879
            name: "v1".into(),
880
            state: ObjectRef([2; 32]),
881
            heads: vec![c.hash()],
882
            attestation: None,
883
        };
884
        let resp = RefsResponse {
885
            refs,
886
            policy: Some(SignedPolicy {
887
                policy: policy.clone(),
888
                sigs: vec![psig.clone()],
889
            }),
890
            tags: vec![tag.clone()],
891
            history: vec![SignedPolicy {
892
                policy: policy.clone(),
893
                sigs: vec![],
894
            }],
895
        };
896
        let (id_back, back) = wire::decode_refs(&wire::encode_refs([3; 16], &resp)).unwrap();
897
        assert_eq!(id_back, [3; 16]);
898
        assert_eq!(back.refs.len(), 2);
899
        assert_eq!(back.refs[0].attestation.as_ref().unwrap().attest, att);
900
        assert_eq!(back.refs[1].last_serial, None);
901
        assert_eq!(back.policy.as_ref().unwrap().policy, policy);
902
        assert_eq!(back.history, resp.history);
903
        assert_eq!(back.tags, vec![tag.clone()]);
904
905
        let (h, w) = wire::decode_negotiate(&wire::encode_negotiate(
906
            &[ObjectRef([1; 32])],
907
            &[ObjectRef([2; 32])],
908
        ))
909
        .unwrap();
910
        assert_eq!((h, w), (vec![ObjectRef([1; 32])], vec![ObjectRef([2; 32])]));
911
        assert_eq!(
912
            wire::decode_hashes(&wire::encode_hashes(&[c.hash()])).unwrap(),
913
            vec![c.hash()]
914
        );
915
        assert_eq!(
916
            wire::decode_fetch(&wire::encode_fetch(&[c.hash()], Some(4096))).unwrap(),
917
            (vec![c.hash()], Some(4096))
918
        );
919
        assert_eq!(
920
            wire::decode_fetch(&wire::encode_fetch(&[], None)).unwrap(),
921
            (vec![], None)
922
        );
923
        assert_eq!(
924
            wire::decode_shallow(&wire::encode_shallow(&[c.hash()], 3)).unwrap(),
925
            (vec![c.hash()], 3)
926
        );
927
        // An old-style fetch request without the filter field still decodes.
928
        assert_eq!(
929
            wire::decode_fetch(&wire::encode_hashes(&[c.hash()])).unwrap(),
930
            (vec![c.hash()], None)
931
        );
932
933
        let up = RefUpdate {
934
            name: "main".into(),
935
            old_state: ObjectRef([1; 32]),
936
            new_state: ObjectRef([2; 32]),
937
            force: true,
938
            attestation: Some(SignedAttest {
939
                attest: att,
940
                sigs: vec![sig],
941
            }),
942
        };
943
        let sp = SignedPolicy {
944
            policy,
945
            sigs: vec![psig],
946
        };
947
        let (b2, ups, pol, tags) = wire::decode_push(&wire::encode_push(
948
            &bundle,
949
            std::slice::from_ref(&up),
950
            Some(&sp),
951
            std::slice::from_ref(&tag),
952
        ))
953
        .unwrap();
954
        assert_eq!(pol, Some(sp));
955
        assert_eq!(tags, vec![tag]);
956
        assert_eq!(b2.changes.len(), 1);
957
        assert_eq!(ups[0].name, "main");
958
        assert!(ups[0].force);
959
        assert_eq!(ups[0].attestation, up.attestation);
960
        let res = vec![RefResult {
961
            name: "main".into(),
962
            ok: false,
963
            reason: "stale".into(),
964
        }];
965
        assert_eq!(
966
            wire::decode_push_response(&wire::encode_push_response(&res)).unwrap(),
967
            res
968
        );
969
        assert_eq!(
970
            wire::decode_capabilities(&wire::encode_capabilities([7; 16])).unwrap(),
971
            [7; 16]
972
        );
973
    }
974
}