radicle 0.23.0

Radicle standard library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
pub mod error;

#[cfg(test)]
mod test;

use std::path::Path;

use crypto::signature::{self, Signer};
use crypto::PublicKey;
use radicle_core::{NodeId, RepoId};
use radicle_git_metadata::author::Author;
use radicle_git_metadata::commit::{headers::Headers, trailers::OwnedTrailer, CommitData};
use radicle_oid::Oid;

use crate::git;
use crate::storage::refs::sigrefs::git::{object, reference, Committer};
use crate::storage::refs::sigrefs::read::CommitReader;
use crate::storage::refs::sigrefs::{read, VerifiedCommit};
use crate::storage::refs::SignedRefs;
use crate::storage::refs::{
    FeatureLevel, Refs, IDENTITY_ROOT, REFS_BLOB_PATH, SIGNATURE_BLOB_PATH, SIGREFS_BRANCH,
    SIGREFS_PARENT,
};

/// The result of attempting to write signed references using
/// [`SignedRefsWriter`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Update {
    /// The new signed references commit was written to the Git repository.
    Changed {
        entry: Box<Commit>,
        level: FeatureLevel,
    },
    /// The provided [`Refs`] were equal to the current [`Refs`], so the process
    /// exited early.
    Unchanged { verified: VerifiedCommit },
}

impl Update {
    fn changed(commit: Commit, level: FeatureLevel) -> Self {
        Self::Changed {
            entry: Box::new(commit),
            level,
        }
    }

    fn unchanged(verified: VerifiedCommit) -> Self {
        Self::Unchanged { verified }
    }
}

/// A [`SignedRefsWriter`] write a commit to the `rad/sigrefs` reference of a
/// namespace.
///
/// To create a new reader, use [`SignedRefsWriter::new`].
///
/// The construction expects:
/// - A [`Refs`] to write to the commit.
/// - A [`NodeId`] which identifies the namespace for which `rad/sigrefs`
///   reference should be read and written to.
/// - A `repository` which is the Git repository being used for reading and
///   writing.
/// - A `signer` which is the entity that produces the cryptographic signature
///   over the [`Refs`].
pub struct SignedRefsWriter<'a, R, S> {
    refs: Refs,
    rid: RepoId,
    namespace: NodeId,
    repository: &'a R,
    signer: &'a S,
}

impl<'a, R, S> SignedRefsWriter<'a, R, S>
where
    R: object::Writer + object::Reader + reference::Writer + reference::Reader,
    S: Signer<crypto::Signature>,
    S: signature::Verifier<crypto::Signature>,
{
    /// Construct a new [`SignedRefsWriter`].
    ///
    /// The construction removes the ref [`SIGREFS_BRANCH`] from [`Refs`]
    /// (if present).
    ///
    /// When calling writing signed references, if the process is successful,
    /// the given [`Refs`] will be written to the provided `namespace`.
    pub fn new(
        mut refs: Refs,
        rid: RepoId,
        namespace: NodeId,
        repository: &'a R,
        signer: &'a S,
    ) -> Self {
        debug_assert!(refs.get(&IDENTITY_ROOT).is_some());
        debug_assert!(refs.get(&SIGREFS_PARENT).is_none());
        refs.remove_sigrefs();
        Self {
            refs,
            rid,
            namespace,
            repository,
            signer,
        }
    }

    /// Write a commit using the [`SignedRefsWriter`].
    ///
    /// The commit written will be composed of:
    /// - The parent commit of the previous entry, unless it is the root commit.
    /// - The [`Refs`] under the `/refs` blob. The [`Refs`] must include:
    ///   - The [`SIGREFS_PARENT`] entry.
    ///   - The [`IDENTITY_ROOT`] entry.
    /// - The [`crypto::Signature`] of the [`Refs`] bytes, under the
    ///   `/signature` blob.
    ///
    /// Note that the [`SIGREFS_PARENT`] is not never included in the [`Refs`]
    /// outside of this process.
    ///
    /// This commit is then written to the reference:
    /// ```text,no_run
    /// refs/namespaces/<namespace>/refs/rad/sigrefs
    /// ```
    pub(in super::super::super::refs) fn write(
        self,
        committer: Committer,
        message: String,
        reflog: String,
    ) -> Result<Update, error::Write> {
        self.write_with(committer, message, reflog, false)
    }

    /// Write a commit even if the current sigrefs head contains identical refs.
    ///
    /// Most users will prefer [`Self::write`].
    pub(in super::super::super::refs) fn force_write(
        self,
        committer: Committer,
        message: String,
        reflog: String,
    ) -> Result<Update, error::Write> {
        self.write_with(committer, message, reflog, true)
    }

    fn write_with(
        self,
        committer: Committer,
        message: String,
        reflog: String,
        force: bool,
    ) -> Result<Update, error::Write> {
        let author = committer.into_inner();
        let Self {
            refs,
            rid,
            namespace,
            repository,
            signer,
        } = self;
        let reference = SIGREFS_BRANCH.with_namespace(git::fmt::Component::from(&namespace));

        let head = HeadReader::new(&reference, repository, rid, self.signer).read();

        let commit_writer = match head {
            Ok(Some(head)) if !force && head.is_unchanged(&refs) => {
                return Ok(Update::unchanged(head.verified))
            }
            Ok(Some(head)) => CommitWriter::with_parent(
                refs,
                *head.verified.commit().oid(),
                author,
                message,
                repository,
                signer,
            ),
            Ok(None) => CommitWriter::root(refs, author, message, repository, signer),
            Err(error::Head::Verify { commit, source }) => {
                log::warn!("Verification of head of signed references failed: {source}");
                CommitWriter::with_parent(refs, commit, author, message, repository, signer)
            }
            Err(err) => return Err(error::Write::Head(err)),
        };
        let commit = commit_writer.write().map_err(error::Write::Commit)?;
        repository
            .write_reference(&reference, commit.oid, commit.parent, reflog)
            .map_err(error::Write::Reference)?;
        Ok(Update::changed(commit, FeatureLevel::Parent))
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Commit {
    /// The [`Oid`] of the parent commit.
    parent: Option<Oid>,
    /// The [`Oid`] of this commit.
    oid: Oid,
    /// The [`Refs`] that were committed.
    refs: Refs,
    /// The [`Signature`] of the [`Refs`] and the [`CommitData`].
    signature: crypto::Signature,
}

impl Commit {
    #[cfg(test)]
    pub(super) fn into_refs(self) -> Refs {
        self.refs
    }

    pub(crate) fn into_sigrefs_at(self, id: PublicKey, level: FeatureLevel) -> SignedRefs {
        SignedRefs {
            at: self.oid,
            id,
            signature: self.signature,
            refs: self.refs,
            level,
            parent: self.parent,
        }
    }
}

struct CommitWriter<'a, R, S> {
    refs: Refs,
    parent: Option<Oid>,
    author: Author,
    message: String,
    repository: &'a R,
    signer: &'a S,
}

impl<'a, R, S> CommitWriter<'a, R, S>
where
    R: object::Writer,
    S: Signer<crypto::Signature>,
{
    fn root(refs: Refs, author: Author, message: String, repository: &'a R, signer: &'a S) -> Self {
        Self {
            refs,
            parent: None,
            author,
            message,
            repository,
            signer,
        }
    }

    fn with_parent(
        refs: Refs,
        parent: Oid,
        author: Author,
        message: String,
        repository: &'a R,
        signer: &'a S,
    ) -> Self {
        Self {
            refs,
            parent: Some(parent),
            author,
            message,
            repository,
            signer,
        }
    }

    fn write(mut self) -> Result<Commit, error::Commit> {
        if let Some(parent) = self.parent {
            let prev = self.refs.add_parent(parent);
            debug_assert!(prev.is_none());
        }

        let mut tree = TreeWriter::new(self.refs, self.repository, self.signer)
            .write()
            .map_err(error::Commit::Tree)?;

        let commit = CommitData::new::<_, _, OwnedTrailer>(
            tree.oid,
            self.parent,
            self.author.clone(),
            self.author,
            Headers::new(),
            self.message,
            vec![],
        );

        let oid = self
            .repository
            .write_commit(commit.to_string().as_bytes())
            .map_err(error::Commit::Write)?;

        tree.refs.remove_parent();

        Ok(Commit {
            parent: self.parent,
            oid,
            refs: tree.refs,
            signature: tree.signature,
        })
    }
}

#[derive(Debug, PartialEq, Eq)]
struct Tree {
    oid: Oid,
    refs: Refs,
    signature: crypto::Signature,
}

struct TreeWriter<'a, R, S> {
    refs: Refs,
    repository: &'a R,
    signer: &'a S,
}

impl<'a, R, S> TreeWriter<'a, R, S>
where
    R: object::Writer,
    S: Signer<crypto::Signature>,
{
    fn new(refs: Refs, repository: &'a R, signer: &'a S) -> Self {
        Self {
            refs,
            repository,
            signer,
        }
    }

    fn write(self) -> Result<Tree, error::Tree> {
        let canonical = self.refs.canonical();
        let signature = self
            .signer
            .try_sign(&canonical)
            .map_err(error::Tree::Sign)?;
        let refs = object::RefsEntry {
            path: Path::new(REFS_BLOB_PATH).to_path_buf(),
            content: canonical,
        };
        let sig = object::SignatureEntry {
            path: Path::new(SIGNATURE_BLOB_PATH).to_path_buf(),
            content: signature.to_vec(),
        };
        let oid = self
            .repository
            .write_tree(refs, sig)
            .map_err(error::Tree::Write)?;
        Ok(Tree {
            oid,
            refs: self.refs,
            signature,
        })
    }
}

/// The current head commit of the reference that points to the signed
/// references payload.
#[derive(Clone, Debug, PartialEq, Eq)]
struct Head {
    /// The verified commit at the head of the reference.
    verified: VerifiedCommit,
}

impl Head {
    /// Returns `true` if the `proposed` [`Refs`] are equal to the [`Refs`]
    /// of the [`Head`].
    fn is_unchanged(&self, proposed: &Refs) -> bool {
        self.verified.commit().refs() == proposed
    }
}

struct HeadReader<'a, 'b, 'c, R, V> {
    rid: RepoId,
    reference: &'a git::fmt::Namespaced<'a>,
    repository: &'b R,
    verifier: &'c V,
}

impl<'a, 'b, 'c, R, V> HeadReader<'a, 'b, 'c, R, V>
where
    R: object::Reader + reference::Reader,
    V: signature::Verifier<crypto::Signature>,
{
    /// Construct a [`HeadReader`] with the `reference` that is being read from
    /// the `repository.`
    fn new(
        reference: &'a git::fmt::Namespaced<'a>,
        repository: &'b R,
        rid: RepoId,
        verifier: &'c V,
    ) -> Self {
        Self {
            rid,
            reference,
            repository,
            verifier,
        }
    }

    /// Read the [`Head`] that is found in the repository under the given
    /// reference.
    ///
    /// Returns `None` if no such reference exists.
    ///
    /// The returned [`Refs`] do not contain the [`SIGREFS_PARENT`] reference.
    fn read(self) -> Result<Option<Head>, error::Head> {
        let Some(oid) = self
            .repository
            .find_reference(self.reference)
            .map_err(error::Head::Reference)?
        else {
            return Ok(None);
        };

        let verified = CommitReader::new(oid, self.repository)
            .read()
            .map_err(error::Head::Commit)?
            .verify(self.rid, self.verifier)
            .map_err(|err| error::Head::Verify {
                commit: oid,
                source: err,
            })?;

        Ok(Some(Head { verified }))
    }
}