OXYGENHUB
# d4dbab97e948 oxygen / crates / oxy-core / src / repo / hosted.rs
21.0 KiB · Rust · last changed Store large inserts as text blobs (change format 2) 1 month ago source blame history raw
1
//! What a host (a hub) does to a repository without a workspace (hub spec
2
//! §6.9, §7.6): build a change from an edit set on any state, sign a
3
//! reference update with a key it holds, and apply both through the same
4
//! validation a received push gets. Nothing here bypasses policy.
5
6
use super::*;
7
use crate::identity::SecretKey;
8
9
/// One file of an edit set: new content, or `None` to remove the path.
10
#[derive(Clone, Debug, PartialEq, Eq)]
11
pub struct FileEdit {
12
    pub path: String,
13
    pub content: Option<Vec<u8>>,
14
}
15
16
impl Repo {
17
    /// Build (but do not store or sign) a change that turns the state with
18
    /// `heads` into that state with `edits` applied, authored by `author`.
19
    /// Files the edits do not name keep their content, conflict markers
20
    /// included, so an edit that removes a file's markers resolves that
21
    /// file's conflict (a resolution change, spec §9.6).
22
    pub fn build_change(
23
        &self,
24
        heads: &[ChangeHash],
25
        edits: &[FileEdit],
26
        message: &str,
27
        author: KeyId,
28
    ) -> Result<Change> {
29
        if message.trim().is_empty() {
30
            return fail("a change message is required");
31
        }
32
        if edits.is_empty() {
33
            return fail("nothing to change");
34
        }
35
        let base = self.load_state(heads)?;
36
        let base_snapshot = base.materialize();
37
        let mut entries: BTreeMap<String, FsEntry> = BTreeMap::new();
38
        for (path, f) in &base_snapshot.files {
39
            let entry = match (&f.kind, &f.content) {
40
                (NodeKind::Dir, _) | (_, FileContent::Dir) => continue,
41
                (kind, FileContent::Text { bytes, .. }) => {
42
                    FsEntry::new(*kind, f.mode.exec, bytes.clone())
43
                }
44
                (kind, FileContent::Opaque { blobs }) => FsEntry {
45
                    blob: blobs.first().copied(),
46
                    ..FsEntry::new(*kind, f.mode.exec, vec![])
47
                },
48
            };
49
            entries.insert(path.clone(), entry);
50
        }
51
        for e in edits {
52
            let path = e.path.trim_matches('/');
53
            if path.is_empty()
54
                || path
55
                    .split('/')
56
                    .any(|c| c.is_empty() || c == "." || c == ".." || c == OXY_DIR)
57
            {
58
                return fail(format!("{:?} is not a valid path", e.path));
59
            }
60
            match &e.content {
61
                Some(bytes) => {
62
                    let exec = entries.get(path).map(|x| x.exec).unwrap_or(false);
63
                    // A file cannot sit where a directory is, or inside a file.
64
                    if base_snapshot
65
                        .files
66
                        .get(path)
67
                        .is_some_and(|f| f.kind == NodeKind::Dir)
68
                    {
69
                        return fail(format!("{path} is a directory"));
70
                    }
71
                    let mut parent = path;
72
                    while let Some((p, _)) = parent.rsplit_once('/') {
73
                        if base_snapshot
74
                            .files
75
                            .get(p)
76
                            .is_some_and(|f| f.kind != NodeKind::Dir)
77
                        {
78
                            return fail(format!("{p} is a file, not a directory"));
79
                        }
80
                        parent = p;
81
                    }
82
                    entries.insert(
83
                        path.to_string(),
84
                        FsEntry::new(NodeKind::File, exec, bytes.clone()),
85
                    );
86
                }
87
                None => {
88
                    if entries.remove(path).is_none() {
89
                        // Removing a directory removes everything below it.
90
                        let prefix = format!("{path}/");
91
                        let before = entries.len();
92
                        entries.retain(|p, _| !p.starts_with(&prefix));
93
                        if entries.len() == before {
94
                            return fail(format!("{path} does not exist"));
95
                        }
96
                    }
97
                }
98
            }
99
        }
100
        // A path the user writes settles its replace conflict, if any: they
101
        // chose that content (spec §9.3).
102
        let written: std::collections::HashSet<String> = edits
103
            .iter()
104
            .filter(|e| e.content.is_some())
105
            .map(|e| e.path.trim_matches('/').to_string())
106
            .collect();
107
        let built = self.build_from_entries_resolving(
108
            &base,
109
            &base_snapshot,
110
            HashMap::new(),
111
            &entries,
112
            &written,
113
        )?;
114
        if let Some(s) = built
115
            .statuses
116
            .iter()
117
            .find(|s| s.status == ChangeStatus::ConflictEdited)
118
        {
119
            return fail(format!("{} still contains conflict markers", s.path));
120
        }
121
        if built.tree_ops.is_empty() && built.edits.is_empty() {
122
            return fail("the edits change nothing");
123
        }
124
        let mut change = Change {
125
            version: crate::objects::CHANGE_VERSION,
126
            change_id: ChangeId::random(),
127
            deps: vec![],
128
            author,
129
            hlc: Hlc::now(),
130
            message: message.trim_end().to_string(),
131
            tree_ops: built.tree_ops,
132
            edits: built.edits,
133
            kind: if built.resolves {
134
                ChangeKind::Resolution
135
            } else {
136
                ChangeKind::Normal
137
            },
138
        };
139
        let mut deps: BTreeSet<ChangeHash> = referenced_changes(&change, &base);
140
        deps.extend(built.contributors.iter().copied());
141
        change.deps = deps.into_iter().collect();
142
        change.normalize();
143
        Ok(change)
144
    }
145
146
    /// A reference update of `bookmark` to `heads`, attested by `signer`
147
    /// when given (the attestation a push would carry, spec §14.4).
148
    pub fn host_ref_update(
149
        &self,
150
        bookmark: &str,
151
        heads: &[ChangeHash],
152
        signer: Option<(&KeyId, &SecretKey)>,
153
    ) -> Result<RefUpdate> {
154
        let view = self.view()?;
155
        let empty = self.store.put_state(&StateObject::new(vec![]))?;
156
        let old_state = view.bookmarks.get(bookmark).copied().unwrap_or(empty);
157
        let new_state = self.store.put_state(&StateObject::new(heads.to_vec()))?;
158
        let attestation = match signer {
159
            Some((key, sk)) => {
160
                let serial = self
161
                    .latest_ref_attest(bookmark)?
162
                    .and_then(|(_, a)| match a.attest.body {
163
                        AttestBody::Ref { serial, .. } => Some(serial),
164
                        _ => None,
165
                    })
166
                    .map(|s| s + 1)
167
                    .unwrap_or(1);
168
                let attest = Attest {
169
                    repo: self.id()?,
170
                    hlc: Hlc::now(),
171
                    body: AttestBody::Ref {
172
                        name: bookmark.to_string(),
173
                        old_state,
174
                        new_state,
175
                        serial,
176
                    },
177
                };
178
                let r = attest.hash();
179
                let sig = sk.sign(Domain::Attest, key, &r);
180
                Some(SignedAttest {
181
                    attest,
182
                    sigs: vec![sig],
183
                })
184
            }
185
            None => None,
186
        };
187
        Ok(RefUpdate {
188
            name: bookmark.to_string(),
189
            old_state,
190
            new_state,
191
            force: false,
192
            attestation,
193
        })
194
    }
195
196
    /// Apply a host-made update as if it had been pushed: the new change
197
    /// (with its signatures) if any, the identities it needs, and the
198
    /// reference update, all through `receive_push_report`, so the same
199
    /// authentication, compare-and-swap and policy checks apply.
200
    pub fn apply_hosted(
201
        &self,
202
        change: Option<(Change, Vec<Signature>)>,
203
        identities: &[Identity],
204
        update: RefUpdate,
205
    ) -> Result<PushReport> {
206
        let mut bundle = Bundle::default();
207
        for id in identities {
208
            bundle.objects.push((ObjectType::Identity, id.encode()));
209
        }
210
        if let Ok(st) = self.store.get_state(&update.new_state) {
211
            bundle.objects.push((ObjectType::State, st.encode()));
212
        }
213
        if let Some((c, sigs)) = change {
214
            bundle.changes.push((c, sigs));
215
        }
216
        self.receive_push_report(bundle, vec![update], None, vec![])
217
    }
218
}
219
220
#[cfg(test)]
221
mod tests {
222
    use super::*;
223
    use crate::identity::SecretKey;
224
225
    fn hermetic() {
226
        static ONCE: std::sync::Once = std::sync::Once::new();
227
        static DIR: std::sync::OnceLock<tempfile::TempDir> = std::sync::OnceLock::new();
228
        ONCE.call_once(|| {
229
            let d = tempfile::tempdir().unwrap();
230
            std::env::set_var("OXY_HOME", d.path());
231
            DIR.set(d).ok();
232
        });
233
    }
234
235
    fn write(root: &Path, rel: &str, content: &[u8]) {
236
        let p = root.join(rel);
237
        fs::create_dir_all(p.parent().unwrap()).unwrap();
238
        fs::write(p, content).unwrap();
239
    }
240
241
    /// A user repository pushed to a bare host, and a hub key the user
242
    /// has delegated to. Returns (host, user, hub key, hub secret).
243
    fn setup(
244
        root: &Path,
245
        with_policy: bool,
246
        delegate: bool,
247
    ) -> (Repo, Repo, KeyId, SecretKey, Identity) {
248
        hermetic();
249
        let host_dir = root.join("host");
250
        Repo::init_bare_unbound(&host_dir).unwrap();
251
        let work = root.join("work");
252
        let mut user = Repo::init(&work).unwrap();
253
        user.create_identity("Andy", "[email protected]", IdentityScope::Repo)
254
            .unwrap();
255
        user.config.set("core.sign", "change").unwrap();
256
        user.config
257
            .set("remote.origin.url", host_dir.to_str().unwrap())
258
            .unwrap();
259
        user.save_config().unwrap();
260
        let hub_sk = SecretKey::generate();
261
        let hub_id = hub_sk.identity("Hub", "hub@example", 1);
262
        let hub_key = hub_id.hash();
263
        if with_policy {
264
            user.policy_init().unwrap();
265
        }
266
        if delegate {
267
            user.delegate_add(hub_id.clone()).unwrap();
268
        }
269
        write(&work, "a.txt", b"one\ntwo\n");
270
        user.snapshot("t").unwrap();
271
        user.commit("first", &[]).unwrap();
272
        let r = user.push("origin", false).unwrap();
273
        assert!(r.iter().all(|x| x.ok), "{r:?}");
274
        (
275
            Repo::open(&host_dir).unwrap(),
276
            user,
277
            hub_key,
278
            hub_sk,
279
            hub_id,
280
        )
281
    }
282
283
    #[test]
284
    fn a_host_edit_signed_by_a_delegate_lands() {
285
        let dir = tempfile::tempdir().unwrap();
286
        let (host, user, hub_key, hub_sk, hub_id) = setup(dir.path(), false, true);
287
        let author = user.config.author_key.unwrap();
288
        let heads = host.bookmark_heads().unwrap();
289
        let change = host
290
            .build_change(
291
                &heads,
292
                &[
293
                    FileEdit {
294
                        path: "a.txt".into(),
295
                        content: Some(b"one\ntwo\nthree\n".to_vec()),
296
                    },
297
                    FileEdit {
298
                        path: "docs/new.md".into(),
299
                        content: Some(b"# New\n".to_vec()),
300
                    },
301
                ],
302
                "Edit on the web",
303
                author,
304
            )
305
            .unwrap();
306
        assert_eq!(change.author, author);
307
        assert_eq!(change.kind, ChangeKind::Normal);
308
        let hash = change.hash();
309
        let sig = hub_sk.sign(Domain::Change, &hub_key, &hash);
310
        let mut new_heads = heads.clone();
311
        new_heads.push(hash);
312
        let update = host
313
            .host_ref_update("main", &[hash], Some((&hub_key, &hub_sk)))
314
            .unwrap();
315
        let ids = vec![hub_id, user.identity(&author).unwrap()];
316
        let report = host
317
            .apply_hosted(Some((change, vec![sig])), &ids, update)
318
            .unwrap();
319
        assert!(report.results.iter().all(|r| r.ok), "{:?}", report.results);
320
        assert_eq!(host.bookmark_heads().unwrap(), vec![hash]);
321
        assert_eq!(
322
            host.file_at("a.txt", Some(&[hash])).unwrap(),
323
            b"one\ntwo\nthree\n"
324
        );
325
        assert_eq!(
326
            host.file_at("docs/new.md", Some(&[hash])).unwrap(),
327
            b"# New\n"
328
        );
329
        // Authored by the user, authenticated by the hub key as delegate.
330
        assert!(
331
            matches!(host.authenticate_change(&hash).unwrap(), SignatureStatus::Valid { key, name, .. } if key == hub_key && name == "Hub")
332
        );
333
        let _ = new_heads;
334
        // Bad paths and empty edits are refused.
335
        assert!(host
336
            .build_change(
337
                &[hash],
338
                &[FileEdit {
339
                    path: "../x".into(),
340
                    content: Some(vec![])
341
                }],
342
                "m",
343
                author
344
            )
345
            .is_err());
346
        assert!(
347
            host.build_change(
348
                &[hash],
349
                &[FileEdit {
350
                    path: "a.txt".into(),
351
                    content: Some(b"one\ntwo\nthree\n".to_vec())
352
                }],
353
                "m",
354
                author
355
            )
356
            .is_err(),
357
            "no-op edits are refused"
358
        );
359
        assert!(
360
            host.build_change(
361
                &[hash],
362
                &[FileEdit {
363
                    path: "a.txt/b".into(),
364
                    content: Some(vec![])
365
                }],
366
                "m",
367
                author
368
            )
369
            .is_err(),
370
            "a file inside a file"
371
        );
372
        let rm = host
373
            .build_change(
374
                &[hash],
375
                &[FileEdit {
376
                    path: "docs".into(),
377
                    content: None,
378
                }],
379
                "Remove docs",
380
                author,
381
            )
382
            .unwrap();
383
        assert!(!rm.tree_ops.is_empty());
384
    }
385
386
    #[test]
387
    fn a_host_edit_by_a_key_that_is_not_a_delegate_is_refused() {
388
        let dir = tempfile::tempdir().unwrap();
389
        let (host, user, hub_key, hub_sk, hub_id) = setup(dir.path(), false, false);
390
        let author = user.config.author_key.unwrap();
391
        let heads = host.bookmark_heads().unwrap();
392
        let change = host
393
            .build_change(
394
                &heads,
395
                &[FileEdit {
396
                    path: "a.txt".into(),
397
                    content: Some(b"x\n".to_vec()),
398
                }],
399
                "Edit",
400
                author,
401
            )
402
            .unwrap();
403
        let hash = change.hash();
404
        let sig = hub_sk.sign(Domain::Change, &hub_key, &hash);
405
        let update = host
406
            .host_ref_update("main", &[hash], Some((&hub_key, &hub_sk)))
407
            .unwrap();
408
        let report = host
409
            .apply_hosted(
410
                Some((change, vec![sig])),
411
                &[hub_id, user.identity(&author).unwrap()],
412
                update,
413
            )
414
            .unwrap();
415
        // Without a policy the host accepts unauthenticated changes, but the
416
        // signature does not verify as the author's: it stays unauthenticated.
417
        assert!(report.results.iter().all(|r| r.ok));
418
        assert!(
419
            matches!(host.authenticate_change(&hash).unwrap(), SignatureStatus::Invalid(why) if why.contains("neither the author"))
420
        );
421
    }
422
423
    #[test]
424
    fn under_a_policy_a_delegate_may_update_the_reference_and_a_stranger_may_not() {
425
        let dir = tempfile::tempdir().unwrap();
426
        let (host, user, hub_key, hub_sk, hub_id) = setup(dir.path(), true, true);
427
        assert!(host.current_policy().unwrap().is_some());
428
        let author = user.config.author_key.unwrap();
429
        let heads = host.bookmark_heads().unwrap();
430
        let change = host
431
            .build_change(
432
                &heads,
433
                &[FileEdit {
434
                    path: "a.txt".into(),
435
                    content: Some(b"one\n".to_vec()),
436
                }],
437
                "Trim",
438
                author,
439
            )
440
            .unwrap();
441
        let hash = change.hash();
442
        let sig = hub_sk.sign(Domain::Change, &hub_key, &hash);
443
        let ids = vec![hub_id.clone(), user.identity(&author).unwrap()];
444
        let update = host
445
            .host_ref_update("main", &[hash], Some((&hub_key, &hub_sk)))
446
            .unwrap();
447
        let report = host
448
            .apply_hosted(Some((change, vec![sig])), &ids, update)
449
            .unwrap();
450
        assert!(report.results.iter().all(|r| r.ok), "{:?}", report.results);
451
        assert_eq!(host.bookmark_heads().unwrap(), vec![hash]);
452
453
        // A stranger's key cannot move the reference, even with the user as author.
454
        let stranger = SecretKey::generate();
455
        let sid = stranger.identity("Mallory", "m@example", 1);
456
        let skey = sid.hash();
457
        let change2 = host
458
            .build_change(
459
                &[hash],
460
                &[FileEdit {
461
                    path: "a.txt".into(),
462
                    content: Some(b"evil\n".to_vec()),
463
                }],
464
                "Evil",
465
                author,
466
            )
467
            .unwrap();
468
        let h2 = change2.hash();
469
        let sig2 = stranger.sign(Domain::Change, &skey, &h2);
470
        let update = host
471
            .host_ref_update("main", &[h2], Some((&skey, &stranger)))
472
            .unwrap();
473
        let report = host
474
            .apply_hosted(Some((change2, vec![sig2])), &[sid], update)
475
            .unwrap();
476
        assert!(report.results.iter().all(|r| !r.ok), "{:?}", report.results);
477
        assert_eq!(
478
            host.bookmark_heads().unwrap(),
479
            vec![hash],
480
            "the reference did not move"
481
        );
482
        // An unsigned update is refused under a policy too.
483
        let update = host.host_ref_update("main", &[hash], None).unwrap();
484
        let report = host.apply_hosted(None, &[], update).unwrap();
485
        assert!(report.results.iter().all(|r| !r.ok));
486
    }
487
488
    #[test]
489
    fn a_host_edit_that_removes_markers_resolves_the_conflict() {
490
        let dir = tempfile::tempdir().unwrap();
491
        let (host, mut user, hub_key, hub_sk, hub_id) = setup(dir.path(), false, true);
492
        let author = user.config.author_key.unwrap();
493
        let base = host.bookmark_heads().unwrap();
494
        // Two independent edits of the same line: a conflict in the union.
495
        let a = host
496
            .build_change(
497
                &base,
498
                &[FileEdit {
499
                    path: "a.txt".into(),
500
                    content: Some(b"ONE\ntwo\n".to_vec()),
501
                }],
502
                "A",
503
                author,
504
            )
505
            .unwrap();
506
        let b = host
507
            .build_change(
508
                &base,
509
                &[FileEdit {
510
                    path: "a.txt".into(),
511
                    content: Some(b"uno\ntwo\n".to_vec()),
512
                }],
513
                "B",
514
                author,
515
            )
516
            .unwrap();
517
        let (ha, hb) = (a.hash(), b.hash());
518
        let ids = vec![hub_id.clone(), user.identity(&author).unwrap()];
519
        for (c, h) in [(a, ha), (b, hb)] {
520
            let sig = hub_sk.sign(Domain::Change, &hub_key, &h);
521
            let mut heads = host.bookmark_heads().unwrap();
522
            heads.retain(|x| !base.contains(x));
523
            heads.push(h);
524
            let update = host
525
                .host_ref_update("main", &heads, Some((&hub_key, &hub_sk)))
526
                .unwrap();
527
            let r = host
528
                .apply_hosted(Some((c, vec![sig])), &ids, update)
529
                .unwrap();
530
            assert!(r.results.iter().all(|x| x.ok), "{:?}", r.results);
531
        }
532
        let heads = host.bookmark_heads().unwrap();
533
        assert_eq!(heads.len(), 2);
534
        let marked = String::from_utf8(host.file_at("a.txt", Some(&heads)).unwrap()).unwrap();
535
        assert!(marked.contains("<<<<<<< oxy conflict"), "{marked}");
536
        // Saving with markers left in is refused; saving without them resolves.
537
        assert!(host
538
            .build_change(
539
                &heads,
540
                &[FileEdit {
541
                    path: "a.txt".into(),
542
                    content: Some(format!("{marked}x\n").into_bytes())
543
                }],
544
                "half",
545
                author
546
            )
547
            .is_err());
548
        let res = host
549
            .build_change(
550
                &heads,
551
                &[FileEdit {
552
                    path: "a.txt".into(),
553
                    content: Some(b"One\ntwo\n".to_vec()),
554
                }],
555
                "Resolve",
556
                author,
557
            )
558
            .unwrap();
559
        assert_eq!(res.kind, ChangeKind::Resolution);
560
        assert!(
561
            res.deps.contains(&ha) && res.deps.contains(&hb),
562
            "a resolution depends on both sides"
563
        );
564
        let hr = res.hash();
565
        let sig = hub_sk.sign(Domain::Change, &hub_key, &hr);
566
        let update = host
567
            .host_ref_update("main", &[hr], Some((&hub_key, &hub_sk)))
568
            .unwrap();
569
        let r = host
570
            .apply_hosted(Some((res, vec![sig])), &ids, update)
571
            .unwrap();
572
        assert!(r.results.iter().all(|x| x.ok), "{:?}", r.results);
573
        assert_eq!(host.file_at("a.txt", Some(&[hr])).unwrap(), b"One\ntwo\n");
574
        assert!(!host
575
            .load_state(&[hr])
576
            .unwrap()
577
            .materialize()
578
            .is_conflicted());
579
        let _ = &mut user;
580
    }
581
}