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