OXYGENHUB
# cf1b0412f142 oxygen / crates / oxy-core / src / repo / hosted.rs
20.6 KiB · Rust · last changed Status snapshots skip files unchanged since the manifest 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
        let built = self.build_from_entries(&base, &base_snapshot, HashMap::new(), &entries)?;
101
        if let Some(s) = built
102
            .statuses
103
            .iter()
104
            .find(|s| s.status == ChangeStatus::ConflictEdited)
105
        {
106
            return fail(format!("{} still contains conflict markers", s.path));
107
        }
108
        if built.tree_ops.is_empty() && built.edits.is_empty() {
109
            return fail("the edits change nothing");
110
        }
111
        let mut change = Change {
112
            change_id: ChangeId::random(),
113
            deps: vec![],
114
            author,
115
            hlc: Hlc::now(),
116
            message: message.trim_end().to_string(),
117
            tree_ops: built.tree_ops,
118
            edits: built.edits,
119
            kind: if built.resolves {
120
                ChangeKind::Resolution
121
            } else {
122
                ChangeKind::Normal
123
            },
124
        };
125
        let mut deps: BTreeSet<ChangeHash> = referenced_changes(&change, &base);
126
        deps.extend(built.contributors.iter().copied());
127
        change.deps = deps.into_iter().collect();
128
        change.normalize();
129
        Ok(change)
130
    }
131
132
    /// A reference update of `bookmark` to `heads`, attested by `signer`
133
    /// when given (the attestation a push would carry, spec §14.4).
134
    pub fn host_ref_update(
135
        &self,
136
        bookmark: &str,
137
        heads: &[ChangeHash],
138
        signer: Option<(&KeyId, &SecretKey)>,
139
    ) -> Result<RefUpdate> {
140
        let view = self.view()?;
141
        let empty = self.store.put_state(&StateObject::new(vec![]))?;
142
        let old_state = view.bookmarks.get(bookmark).copied().unwrap_or(empty);
143
        let new_state = self.store.put_state(&StateObject::new(heads.to_vec()))?;
144
        let attestation = match signer {
145
            Some((key, sk)) => {
146
                let serial = self
147
                    .latest_ref_attest(bookmark)?
148
                    .and_then(|(_, a)| match a.attest.body {
149
                        AttestBody::Ref { serial, .. } => Some(serial),
150
                        _ => None,
151
                    })
152
                    .map(|s| s + 1)
153
                    .unwrap_or(1);
154
                let attest = Attest {
155
                    repo: self.id()?,
156
                    hlc: Hlc::now(),
157
                    body: AttestBody::Ref {
158
                        name: bookmark.to_string(),
159
                        old_state,
160
                        new_state,
161
                        serial,
162
                    },
163
                };
164
                let r = attest.hash();
165
                let sig = sk.sign(Domain::Attest, key, &r);
166
                Some(SignedAttest {
167
                    attest,
168
                    sigs: vec![sig],
169
                })
170
            }
171
            None => None,
172
        };
173
        Ok(RefUpdate {
174
            name: bookmark.to_string(),
175
            old_state,
176
            new_state,
177
            force: false,
178
            attestation,
179
        })
180
    }
181
182
    /// Apply a host-made update as if it had been pushed: the new change
183
    /// (with its signatures) if any, the identities it needs, and the
184
    /// reference update, all through `receive_push_report`, so the same
185
    /// authentication, compare-and-swap and policy checks apply.
186
    pub fn apply_hosted(
187
        &self,
188
        change: Option<(Change, Vec<Signature>)>,
189
        identities: &[Identity],
190
        update: RefUpdate,
191
    ) -> Result<PushReport> {
192
        let mut bundle = Bundle::default();
193
        for id in identities {
194
            bundle.objects.push((ObjectType::Identity, id.encode()));
195
        }
196
        if let Ok(st) = self.store.get_state(&update.new_state) {
197
            bundle.objects.push((ObjectType::State, st.encode()));
198
        }
199
        if let Some((c, sigs)) = change {
200
            bundle.changes.push((c, sigs));
201
        }
202
        self.receive_push_report(bundle, vec![update], None, vec![])
203
    }
204
}
205
206
#[cfg(test)]
207
mod tests {
208
    use super::*;
209
    use crate::identity::SecretKey;
210
211
    fn hermetic() {
212
        static ONCE: std::sync::Once = std::sync::Once::new();
213
        static DIR: std::sync::OnceLock<tempfile::TempDir> = std::sync::OnceLock::new();
214
        ONCE.call_once(|| {
215
            let d = tempfile::tempdir().unwrap();
216
            std::env::set_var("OXY_HOME", d.path());
217
            DIR.set(d).ok();
218
        });
219
    }
220
221
    fn write(root: &Path, rel: &str, content: &[u8]) {
222
        let p = root.join(rel);
223
        fs::create_dir_all(p.parent().unwrap()).unwrap();
224
        fs::write(p, content).unwrap();
225
    }
226
227
    /// A user repository pushed to a bare host, and a hub key the user
228
    /// has delegated to. Returns (host, user, hub key, hub secret).
229
    fn setup(
230
        root: &Path,
231
        with_policy: bool,
232
        delegate: bool,
233
    ) -> (Repo, Repo, KeyId, SecretKey, Identity) {
234
        hermetic();
235
        let host_dir = root.join("host");
236
        Repo::init_bare_unbound(&host_dir).unwrap();
237
        let work = root.join("work");
238
        let mut user = Repo::init(&work).unwrap();
239
        user.create_identity("Andy", "[email protected]", IdentityScope::Repo)
240
            .unwrap();
241
        user.config.set("core.sign", "change").unwrap();
242
        user.config
243
            .set("remote.origin.url", host_dir.to_str().unwrap())
244
            .unwrap();
245
        user.save_config().unwrap();
246
        let hub_sk = SecretKey::generate();
247
        let hub_id = hub_sk.identity("Hub", "hub@example", 1);
248
        let hub_key = hub_id.hash();
249
        if with_policy {
250
            user.policy_init().unwrap();
251
        }
252
        if delegate {
253
            user.delegate_add(hub_id.clone()).unwrap();
254
        }
255
        write(&work, "a.txt", b"one\ntwo\n");
256
        user.snapshot("t").unwrap();
257
        user.commit("first", &[]).unwrap();
258
        let r = user.push("origin", false).unwrap();
259
        assert!(r.iter().all(|x| x.ok), "{r:?}");
260
        (
261
            Repo::open(&host_dir).unwrap(),
262
            user,
263
            hub_key,
264
            hub_sk,
265
            hub_id,
266
        )
267
    }
268
269
    #[test]
270
    fn a_host_edit_signed_by_a_delegate_lands() {
271
        let dir = tempfile::tempdir().unwrap();
272
        let (host, user, hub_key, hub_sk, hub_id) = setup(dir.path(), false, true);
273
        let author = user.config.author_key.unwrap();
274
        let heads = host.bookmark_heads().unwrap();
275
        let change = host
276
            .build_change(
277
                &heads,
278
                &[
279
                    FileEdit {
280
                        path: "a.txt".into(),
281
                        content: Some(b"one\ntwo\nthree\n".to_vec()),
282
                    },
283
                    FileEdit {
284
                        path: "docs/new.md".into(),
285
                        content: Some(b"# New\n".to_vec()),
286
                    },
287
                ],
288
                "Edit on the web",
289
                author,
290
            )
291
            .unwrap();
292
        assert_eq!(change.author, author);
293
        assert_eq!(change.kind, ChangeKind::Normal);
294
        let hash = change.hash();
295
        let sig = hub_sk.sign(Domain::Change, &hub_key, &hash);
296
        let mut new_heads = heads.clone();
297
        new_heads.push(hash);
298
        let update = host
299
            .host_ref_update("main", &[hash], Some((&hub_key, &hub_sk)))
300
            .unwrap();
301
        let ids = vec![hub_id, user.identity(&author).unwrap()];
302
        let report = host
303
            .apply_hosted(Some((change, vec![sig])), &ids, update)
304
            .unwrap();
305
        assert!(report.results.iter().all(|r| r.ok), "{:?}", report.results);
306
        assert_eq!(host.bookmark_heads().unwrap(), vec![hash]);
307
        assert_eq!(
308
            host.file_at("a.txt", Some(&[hash])).unwrap(),
309
            b"one\ntwo\nthree\n"
310
        );
311
        assert_eq!(
312
            host.file_at("docs/new.md", Some(&[hash])).unwrap(),
313
            b"# New\n"
314
        );
315
        // Authored by the user, authenticated by the hub key as delegate.
316
        assert!(
317
            matches!(host.authenticate_change(&hash).unwrap(), SignatureStatus::Valid { key, name, .. } if key == hub_key && name == "Hub")
318
        );
319
        let _ = new_heads;
320
        // Bad paths and empty edits are refused.
321
        assert!(host
322
            .build_change(
323
                &[hash],
324
                &[FileEdit {
325
                    path: "../x".into(),
326
                    content: Some(vec![])
327
                }],
328
                "m",
329
                author
330
            )
331
            .is_err());
332
        assert!(
333
            host.build_change(
334
                &[hash],
335
                &[FileEdit {
336
                    path: "a.txt".into(),
337
                    content: Some(b"one\ntwo\nthree\n".to_vec())
338
                }],
339
                "m",
340
                author
341
            )
342
            .is_err(),
343
            "no-op edits are refused"
344
        );
345
        assert!(
346
            host.build_change(
347
                &[hash],
348
                &[FileEdit {
349
                    path: "a.txt/b".into(),
350
                    content: Some(vec![])
351
                }],
352
                "m",
353
                author
354
            )
355
            .is_err(),
356
            "a file inside a file"
357
        );
358
        let rm = host
359
            .build_change(
360
                &[hash],
361
                &[FileEdit {
362
                    path: "docs".into(),
363
                    content: None,
364
                }],
365
                "Remove docs",
366
                author,
367
            )
368
            .unwrap();
369
        assert!(!rm.tree_ops.is_empty());
370
    }
371
372
    #[test]
373
    fn a_host_edit_by_a_key_that_is_not_a_delegate_is_refused() {
374
        let dir = tempfile::tempdir().unwrap();
375
        let (host, user, hub_key, hub_sk, hub_id) = setup(dir.path(), false, false);
376
        let author = user.config.author_key.unwrap();
377
        let heads = host.bookmark_heads().unwrap();
378
        let change = host
379
            .build_change(
380
                &heads,
381
                &[FileEdit {
382
                    path: "a.txt".into(),
383
                    content: Some(b"x\n".to_vec()),
384
                }],
385
                "Edit",
386
                author,
387
            )
388
            .unwrap();
389
        let hash = change.hash();
390
        let sig = hub_sk.sign(Domain::Change, &hub_key, &hash);
391
        let update = host
392
            .host_ref_update("main", &[hash], Some((&hub_key, &hub_sk)))
393
            .unwrap();
394
        let report = host
395
            .apply_hosted(
396
                Some((change, vec![sig])),
397
                &[hub_id, user.identity(&author).unwrap()],
398
                update,
399
            )
400
            .unwrap();
401
        // Without a policy the host accepts unauthenticated changes, but the
402
        // signature does not verify as the author's: it stays unauthenticated.
403
        assert!(report.results.iter().all(|r| r.ok));
404
        assert!(
405
            matches!(host.authenticate_change(&hash).unwrap(), SignatureStatus::Invalid(why) if why.contains("neither the author"))
406
        );
407
    }
408
409
    #[test]
410
    fn under_a_policy_a_delegate_may_update_the_reference_and_a_stranger_may_not() {
411
        let dir = tempfile::tempdir().unwrap();
412
        let (host, user, hub_key, hub_sk, hub_id) = setup(dir.path(), true, true);
413
        assert!(host.current_policy().unwrap().is_some());
414
        let author = user.config.author_key.unwrap();
415
        let heads = host.bookmark_heads().unwrap();
416
        let change = host
417
            .build_change(
418
                &heads,
419
                &[FileEdit {
420
                    path: "a.txt".into(),
421
                    content: Some(b"one\n".to_vec()),
422
                }],
423
                "Trim",
424
                author,
425
            )
426
            .unwrap();
427
        let hash = change.hash();
428
        let sig = hub_sk.sign(Domain::Change, &hub_key, &hash);
429
        let ids = vec![hub_id.clone(), user.identity(&author).unwrap()];
430
        let update = host
431
            .host_ref_update("main", &[hash], Some((&hub_key, &hub_sk)))
432
            .unwrap();
433
        let report = host
434
            .apply_hosted(Some((change, vec![sig])), &ids, update)
435
            .unwrap();
436
        assert!(report.results.iter().all(|r| r.ok), "{:?}", report.results);
437
        assert_eq!(host.bookmark_heads().unwrap(), vec![hash]);
438
439
        // A stranger's key cannot move the reference, even with the user as author.
440
        let stranger = SecretKey::generate();
441
        let sid = stranger.identity("Mallory", "m@example", 1);
442
        let skey = sid.hash();
443
        let change2 = host
444
            .build_change(
445
                &[hash],
446
                &[FileEdit {
447
                    path: "a.txt".into(),
448
                    content: Some(b"evil\n".to_vec()),
449
                }],
450
                "Evil",
451
                author,
452
            )
453
            .unwrap();
454
        let h2 = change2.hash();
455
        let sig2 = stranger.sign(Domain::Change, &skey, &h2);
456
        let update = host
457
            .host_ref_update("main", &[h2], Some((&skey, &stranger)))
458
            .unwrap();
459
        let report = host
460
            .apply_hosted(Some((change2, vec![sig2])), &[sid], update)
461
            .unwrap();
462
        assert!(report.results.iter().all(|r| !r.ok), "{:?}", report.results);
463
        assert_eq!(
464
            host.bookmark_heads().unwrap(),
465
            vec![hash],
466
            "the reference did not move"
467
        );
468
        // An unsigned update is refused under a policy too.
469
        let update = host.host_ref_update("main", &[hash], None).unwrap();
470
        let report = host.apply_hosted(None, &[], update).unwrap();
471
        assert!(report.results.iter().all(|r| !r.ok));
472
    }
473
474
    #[test]
475
    fn a_host_edit_that_removes_markers_resolves_the_conflict() {
476
        let dir = tempfile::tempdir().unwrap();
477
        let (host, mut user, hub_key, hub_sk, hub_id) = setup(dir.path(), false, true);
478
        let author = user.config.author_key.unwrap();
479
        let base = host.bookmark_heads().unwrap();
480
        // Two independent edits of the same line: a conflict in the union.
481
        let a = host
482
            .build_change(
483
                &base,
484
                &[FileEdit {
485
                    path: "a.txt".into(),
486
                    content: Some(b"ONE\ntwo\n".to_vec()),
487
                }],
488
                "A",
489
                author,
490
            )
491
            .unwrap();
492
        let b = host
493
            .build_change(
494
                &base,
495
                &[FileEdit {
496
                    path: "a.txt".into(),
497
                    content: Some(b"uno\ntwo\n".to_vec()),
498
                }],
499
                "B",
500
                author,
501
            )
502
            .unwrap();
503
        let (ha, hb) = (a.hash(), b.hash());
504
        let ids = vec![hub_id.clone(), user.identity(&author).unwrap()];
505
        for (c, h) in [(a, ha), (b, hb)] {
506
            let sig = hub_sk.sign(Domain::Change, &hub_key, &h);
507
            let mut heads = host.bookmark_heads().unwrap();
508
            heads.retain(|x| !base.contains(x));
509
            heads.push(h);
510
            let update = host
511
                .host_ref_update("main", &heads, Some((&hub_key, &hub_sk)))
512
                .unwrap();
513
            let r = host
514
                .apply_hosted(Some((c, vec![sig])), &ids, update)
515
                .unwrap();
516
            assert!(r.results.iter().all(|x| x.ok), "{:?}", r.results);
517
        }
518
        let heads = host.bookmark_heads().unwrap();
519
        assert_eq!(heads.len(), 2);
520
        let marked = String::from_utf8(host.file_at("a.txt", Some(&heads)).unwrap()).unwrap();
521
        assert!(marked.contains("<<<<<<< oxy conflict"), "{marked}");
522
        // Saving with markers left in is refused; saving without them resolves.
523
        assert!(host
524
            .build_change(
525
                &heads,
526
                &[FileEdit {
527
                    path: "a.txt".into(),
528
                    content: Some(format!("{marked}x\n").into_bytes())
529
                }],
530
                "half",
531
                author
532
            )
533
            .is_err());
534
        let res = host
535
            .build_change(
536
                &heads,
537
                &[FileEdit {
538
                    path: "a.txt".into(),
539
                    content: Some(b"One\ntwo\n".to_vec()),
540
                }],
541
                "Resolve",
542
                author,
543
            )
544
            .unwrap();
545
        assert_eq!(res.kind, ChangeKind::Resolution);
546
        assert!(
547
            res.deps.contains(&ha) && res.deps.contains(&hb),
548
            "a resolution depends on both sides"
549
        );
550
        let hr = res.hash();
551
        let sig = hub_sk.sign(Domain::Change, &hub_key, &hr);
552
        let update = host
553
            .host_ref_update("main", &[hr], Some((&hub_key, &hub_sk)))
554
            .unwrap();
555
        let r = host
556
            .apply_hosted(Some((res, vec![sig])), &ids, update)
557
            .unwrap();
558
        assert!(r.results.iter().all(|x| x.ok), "{:?}", r.results);
559
        assert_eq!(host.file_at("a.txt", Some(&[hr])).unwrap(), b"One\ntwo\n");
560
        assert!(!host
561
            .load_state(&[hr])
562
            .unwrap()
563
            .materialize()
564
            .is_conflicted());
565
        let _ = &mut user;
566
    }
567
}