ostrya 0.6.0

Pure-Rust async reimplementation of the ostree repository library
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
//! The hooks that a host gives one receive session.
//!
//! The first hook runs before the update lock of the commit and returns
//! detached-metadata entries. The server-commit hook runs in its place for a
//! `Commit` with a server commit. The last hook runs after the release of the
//! update lock.

use std::any::Any;
use std::future::Future;
use std::pin::Pin;

use ostrya_core::{Checksum, Value};

use super::session::{Failure, ReceiveReport};
use crate::remote::proto::ServerCommit;
use crate::remote::{self, RefUpdate};

/// The future that a hook returns.
///
/// The future is `Send`, so the commit of a session can run on a thread pool.
pub type HookFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;

/// The longest message of a hook, in bytes: the message of a [`HookRefusal`]
/// and the error of [`ReceiveHooks::after_update`].
pub(super) const MAX_MESSAGE: usize = 4096;

/// Cuts `message` to [`MAX_MESSAGE`] bytes at a character boundary.
///
/// If the message is cut, this function releases the memory past the cut.
pub(super) fn cut(message: &mut String) {
    if message.len() > MAX_MESSAGE {
        message.truncate(message.floor_char_boundary(MAX_MESSAGE));
        message.shrink_to_fit();
    }
}

/// Host calls that run before and after the ref update of a receive session.
///
/// A host gives the hooks to one session through
/// [`ReceiveService::hello_with_hooks`](super::ReceiveService::hello_with_hooks).
/// [`ReceiveService::hello`](super::ReceiveService::hello),
/// [`Repo::receive`](crate::Repo::receive),
/// [`Repo::receive_stream`](crate::Repo::receive_stream), and
/// [`Repo::receive_stream_to_commit`](crate::Repo::receive_stream_to_commit)
/// take no hooks. The receive endpoint of `ostrya-server` gives the hooks
/// that its `SessionSetup` holds.
///
/// # When `before_update` runs
///
/// In a session with hooks, [`before_update`](Self::before_update) runs once
/// in each `Commit` with ref updates, just before the update lock. Before the
/// hook, the commit runs all its checks of these items:
///
/// - the ref names
/// - the size of the reply
/// - the commits of the detached metadata
/// - the rights of the policy
/// - the objects
/// - the ref and collection bindings
/// - the signature policy
///
/// The server signatures and the ancestry walks of the fast-forward check
/// also run before the hook. If the commit fails one of these checks, it does
/// not call the hook.
///
/// The hook gets each ref update of the `Commit` message as the client sent
/// it, deletes included. A delete has `new` set to `None`. The `expected`
/// field is the state that the client expects. The hook gets no `force` flag,
/// because the rights of the policy decide what `force` allows.
///
/// The checks of the current value of each ref run after the hook, under the
/// update lock, in this order:
///
/// 1. the `ref-denied` refusals of a ref path, for example a ref that is an
///    alias
/// 2. `ref-mismatch`
/// 3. `delete-denied`
/// 4. `non-fast-forward`
///
/// A commit can fail at these checks after the hook returns a plan. If one of
/// these checks fails, no ref changes. If the commit fails after the hook, the
/// carried value drops once, after the release of the update lock, and
/// `after_update` does not run.
///
/// # A server commit
///
/// For a `Commit` with a [`ServerCommit`],
/// [`before_server_commit`](Self::before_server_commit) runs once in place of
/// `before_update`. Before the hook, the commit runs all its checks of these
/// items:
///
/// - the ref names and the form of the message
/// - the depth of the metadata values
/// - the size of the reply
/// - the objects of the session
/// - the collection anchor ref and the rule of the policy
/// - the completeness of the tree
///
/// Then the staged objects become durable, and the hook runs, just before the
/// update lock. If the commit fails one of these checks, it does not call the
/// hook.
///
/// The hook gets the server commit as the client sent it. The new commit does
/// not exist yet, because the server builds it under the update lock. The
/// hook gives no detached-metadata entries.
///
/// These checks run after the hook, under the update lock, in this order:
///
/// 1. the `ref-denied` refusals of a ref path
/// 2. `non-fast-forward` for `orphan` on a branch that is present, if the rule
///    does not allow it
/// 3. `ref-mismatch` for a tag that is present
///
/// If one of these checks fails, no ref changes. The carried value drops
/// once, after the release of the update lock, and `after_update` does not
/// run.
///
/// In `after_update`, the report holds the branch first. Its `old` is the
/// commit of the branch before the write, or `None` for a new branch. Its
/// `new` is the server commit. Then the report holds each tag, with `old`
/// set to `None`.
///
/// A host that does not implement `before_server_commit` refuses each server
/// commit with `ref-denied`, after the staged objects become durable and
/// before the update lock.
///
/// # The entries of the host
///
/// [`UpdatePlan::metadata`] gives detached-metadata entries for the new
/// commits of the updates. The commit writes them into the detached metadata
/// of each of these commits:
///
/// - A host entry replaces an entry of the client dict with the same key,
///   with no duplicate-key error.
/// - A commit with no client dict gets a new dict.
/// - The rule of `[ex-ostrya] keep-detached-related` does not apply to the
///   host entries.
/// - If [`keep_existing`](HostEntry::keep_existing) is `false`, the host entry
///   replaces the value that the stored dict holds under its key.
/// - If `keep_existing` is `true`, a value that the stored dict holds under
///   the key stays. The commit writes the host value only if the stored dict
///   has no such key.
///
/// The stored dict is the dict that the repository holds under the update
/// lock, when the commit writes the detached metadata.
///
/// The commit writes the entries also for an update that changes no ref
/// because the ref already names the commit. It writes them once for a commit
/// that several updates name. If the plan has no entry, the commit writes no
/// detached metadata.
///
/// A host entry cannot have a signature key: `ostree.gpgsigs`,
/// `ostree.sign.ed25519`, `ostree.sign.spki`, or `ostree.sign.dummy`. If the
/// plan has one of these faults, the commit refuses the plan with `internal`
/// before any merge, and no ref changes:
///
/// - a commit that is not the new commit of an update
/// - a commit that the plan gives in two tuples
/// - an entry under a signature key
/// - a key that the plan gives two times for one commit
/// - a value that is not a [`Value::Variant`]
/// - an entry that does not encode:
///   - an inner value that does not have the type of the variant
///   - a string with a NUL byte
///   - a value or a type nested deeper than the parser reads
///   - a type that the parser does not read, for example a dict entry whose
///     key is not a basic type
///   - a key with a NUL byte
///
/// The first five checks run first over the whole plan. They run in the order
/// of the tuples and, in each tuple, in the order of its entries. Then the
/// encode check runs over the plan in the same order. The first failure is the
/// refusal.
///
/// If a merged dict with host entries is larger than
/// [`MAX_METADATA_SIZE`](crate::MAX_METADATA_SIZE), the commit refuses with
/// `limit-exceeded` before the update lock. If the stored dict changes before
/// the lock, the commit checks the size again under the lock. No ref changes
/// in any of these cases.
///
/// The commit writes the detached metadata and the refs in one transaction
/// commit. The detached metadata is durable before a ref names the commit.
/// As a result, a mirror that pulls when the ref moves reads the entries of
/// the host.
///
/// # When `after_update` runs
///
/// If the transaction commit succeeds, [`after_update`](Self::after_update)
/// runs once in the commit. It runs also if no ref changes, for example if
/// each update names the commit that its ref already names. It runs after
/// these steps:
///
/// - the transaction commit
/// - the removal of the partial markers
/// - the summary step
/// - the release of the update lock
///
/// The hook gets the [`ReceiveReport`] that the commit returns and the
/// carried value of the plan or of `before_server_commit`. From then on, the
/// hook owns the carried value.
///
/// The transaction commit is not atomic. It writes the detached metadata,
/// then each ref, and then runs `fsync` on the ref directories. If one of
/// these writes or an `fsync` of a ref directory fails, the detached metadata
/// and some refs can stay written.
///
/// In this case, the commit returns the error as a failure on the server side.
/// The carried value drops, and `after_update` does not run. The host must
/// make its own records agree with the refs of the repository.
///
/// The report goes to the hook before the host sends the reply, so the
/// report never holds a warning of the step
/// [`ReplyNotDelivered`](super::ReceiveStep::ReplyNotDelivered). The time of
/// the hook adds to the time that the commit takes to return.
///
/// # Locks
///
/// The session holds the repository lock shared
/// ([`LockKind::Shared`](crate::LockKind::Shared)) from `Hello` to the commit
/// of its transaction. The lock order is always this:
///
/// 1. the repository lock, shared
/// 2. the locks that the hook takes
/// 3. the update lock
///
/// The host must obey two rules:
///
/// - It never takes a lock that `before_update` or `before_server_commit`
///   takes while it holds the update lock or an
///   [`UpdateGuard`](crate::UpdateGuard).
/// - It never waits for an exclusive repository lock, for example the lock
///   of a prune, while it holds a lock that `before_update` or
///   `before_server_commit` takes.
///
/// The host can put the guards of its locks in [`UpdatePlan::carried`] or in
/// the value that `before_server_commit` returns. Then the guards stay held
/// from the hook, across the wait for the update lock and the ref update,
/// until `after_update` drops the carried value.
/// `carried` is a `Box<dyn Any + Send>`, which is `'static`, so each guard in
/// it must be an owned guard. The session does not examine the contents of
/// `carried`.
///
/// When `after_update` runs, the session holds no lock. The update lock is
/// released, and the transaction commit released the shared repository lock.
/// The hook can call
/// [`Repo::set_ref_immediate`](crate::Repo::set_ref_immediate) and
/// [`Repo::begin_update`](crate::Repo::begin_update) because the session holds
/// no lock.
///
/// The guards in the carried value are still held, so the two rules still
/// apply. While the hook holds the carried value, it never waits for an
/// exclusive repository lock, for example the lock of a prune. Another session
/// can hold the shared repository lock and wait in `before_update` or
/// `before_server_commit` for a lock in the carried value. Then the hook and
/// that session deadlock.
///
/// # Cancellation
///
/// The transaction commit writes the detached metadata and the refs on the
/// blocking pool. If a [`commit`](super::ReceiveService::commit) future is
/// dropped before it completes, it can release the update lock and drop the
/// carried value while these writes continue. A `commit` future can also be
/// dropped while `after_update` runs. Then it drops the future of the hook and
/// the carried value, and the refs can be written already. The host must run
/// each commit to its end, for example in a task that it joins.
///
/// # Panics in a hook
///
/// The session does not catch a panic in a hook. A panic in `after_update`
/// comes after the refs are written, and resumes in the caller of
/// [`commit`](super::ReceiveService::commit).
pub trait ReceiveHooks: Send + Sync {
    /// Returns the detached-metadata entries of the host for the new commits
    /// of `updates`.
    ///
    /// The plan also holds a value that goes to
    /// [`after_update`](Self::after_update).
    ///
    /// # Errors
    ///
    /// If the hook returns a [`HookRefusal`], the commit fails with the code
    /// of the refusal, and no ref changes.
    fn before_update<'a>(
        &'a self,
        updates: &'a [RefUpdate],
    ) -> HookFuture<'a, Result<UpdatePlan, HookRefusal>>;

    /// Returns the carried value of a `Commit` with the server commit
    /// `commit`.
    ///
    /// The value goes to [`after_update`](Self::after_update). If the commit
    /// fails after this hook, the value drops once, after the release of the
    /// update lock, and `after_update` does not run.
    ///
    /// The default method refuses each server commit with `ref-denied`.
    ///
    /// # Errors
    ///
    /// If the hook returns a [`HookRefusal`], the commit fails with the code
    /// of the refusal, and no ref changes.
    fn before_server_commit<'a>(
        &'a self,
        _commit: &'a ServerCommit,
    ) -> HookFuture<'a, Result<Box<dyn Any + Send>, HookRefusal>> {
        Box::pin(std::future::ready(Err(HookRefusal::denied(
            "the hooks of the host take no server commit",
        ))))
    }

    /// Takes the report of a successful commit and the carried value of the
    /// plan.
    ///
    /// The hook runs only if the transaction commit succeeds. The carried
    /// value is the value of the plan of
    /// [`before_update`](Self::before_update), or the value that
    /// [`before_server_commit`](Self::before_server_commit) returns.
    ///
    /// # Errors
    ///
    /// If the hook returns an error, the commit returns `internal` with the
    /// message of the error, cut to 4096 bytes at a character boundary. Then
    /// the session ends as aborted. The refs and the detached metadata stay
    /// written. The session does not undo them, so an aborted session can
    /// leave its refs written.
    fn after_update<'a>(
        &'a self,
        report: &'a ReceiveReport,
        carried: Box<dyn Any + Send>,
    ) -> HookFuture<'a, Result<(), String>>;
}

/// The plan that [`ReceiveHooks::before_update`] returns.
#[derive(Debug)]
pub struct UpdatePlan {
    /// The detached-metadata entries of the host for the new commits.
    ///
    /// The plan holds at most one tuple for each commit.
    pub metadata: Vec<(Checksum, Vec<HostEntry>)>,
    /// A value that goes to [`ReceiveHooks::after_update`].
    ///
    /// If the commit fails after `before_update`, the value drops once, after
    /// the release of the update lock, and `after_update` does not run. A
    /// failure of the transaction commit can leave the detached metadata and
    /// some refs written.
    pub carried: Box<dyn Any + Send>,
}

/// One detached-metadata entry of the host.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HostEntry {
    /// The key.
    pub key: String,
    /// The value, a [`Value::Variant`].
    pub value: Value,
    /// The rule for a key that the stored dict already holds.
    ///
    /// If `false`, the value replaces the value that the stored dict or the
    /// client dict holds under the key. If `true`, a value that the stored
    /// dict holds under the key stays. The commit writes the value only if the
    /// stored dict has no such key.
    pub keep_existing: bool,
}

/// A refusal of the commit from [`ReceiveHooks::before_update`] or
/// [`ReceiveHooks::before_server_commit`].
///
/// If the hook returns a refusal, no ref changes.
#[derive(Debug)]
pub struct HookRefusal {
    kind: Kind,
    message: String,
}

#[derive(Debug)]
enum Kind {
    Denied,
    Internal,
}

impl HookRefusal {
    /// Creates a refusal with the code `ref-denied`.
    ///
    /// The receive endpoint of `ostrya-server` answers it with 422. If the
    /// message is longer than 4096 bytes, this function cuts it to 4096 bytes
    /// at a character boundary.
    pub fn denied(message: impl Into<String>) -> HookRefusal {
        HookRefusal::new(Kind::Denied, message.into())
    }

    /// Creates a refusal with the code `internal`.
    ///
    /// The receive endpoint of `ostrya-server` answers it with 500. If the
    /// message is longer than 4096 bytes, this function cuts it to 4096 bytes
    /// at a character boundary.
    pub fn internal(message: impl Into<String>) -> HookRefusal {
        HookRefusal::new(Kind::Internal, message.into())
    }

    fn new(kind: Kind, mut message: String) -> HookRefusal {
        cut(&mut message);
        HookRefusal { kind, message }
    }

    /// Returns the failure of the session for the refusal.
    pub(super) fn into_failure(self) -> Failure {
        Failure::Wire(match self.kind {
            Kind::Denied => remote::Error::RefDenied(self.message),
            Kind::Internal => remote::Error::Internal(self.message),
        })
    }
}