vcon-server 0.4.10

Stores vCons encrypted at rest, on local disk or in an S3 bucket (as objects or Parquet files), and returns them to authorized readers.
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
//! A copy of the whole data directory, made while a server may be running:
//! server.sqlite with SQLite's online backup API, then each tenant it lists,
//! as `Store::backup_to` copies one.

use std::collections::BTreeSet;
use std::io;
use std::path::Path;

use super::{TenantError, Tenants};
use crate::audit::{Action, Actor, Entry};
use crate::ids::TenantId;
use crate::store::{self, TRIES};

/// What `backup` copied.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Backup {
    pub tenants: u64,
    pub versions: u64,
    /// How many times server.sqlite was copied.
    pub tries: usize,
}

/// Why a backup was refused or failed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BackupError {
    /// Something is already at the backup's path.
    Exists,
    /// Tenants were removed, or versions purged, after each try's copy.
    KeptChanging,
    /// A file could not be read, written or synced, or a catalog could not
    /// be copied or read.
    Io,
    /// The admin log could not record the backup.
    AuditFailed,
    /// The catalogs are in PostgreSQL, which this command does not copy
    /// into a directory: the database's own backup tools do.
    NoFileBackup,
}

/// What a refused backup of a PostgreSQL catalog says to do.
pub const NO_FILE_BACKUP: &str = "the catalogs are in PostgreSQL, which backup does not copy into \
                                  a directory: back up the catalog database with pg_dump or a \
                                  base backup with WAL archiving, and keep the bucket and the \
                                  master key file with it";

impl Tenants {
    /// Copies the data directory into `to`, which must not exist, and
    /// records the run in the admin log with its outcome.
    ///
    /// Each try copies server.sqlite in one step inside one read
    /// transaction, then each tenant that copy lists, as `Store::backup_to`
    /// copies one. A tenant the copy lists that is gone, or whose versions
    /// kept being purged, was removed meanwhile, and the next try copies
    /// server.sqlite again. Tenant directories the final copy does not
    /// list are removed. Tenants are copied one after another, each as it
    /// was at its own copy.
    pub fn backup(&self, who: Actor, to: &Path) -> Result<Backup, BackupError> {
        let done = self.copy_all(to);
        let status = match done {
            Ok(_) => 200,
            Err(BackupError::Exists) => 409,
            Err(BackupError::KeptChanging) => 503,
            Err(BackupError::Io | BackupError::AuditFailed) => 500,
            Err(BackupError::NoFileBackup) => 501,
        };
        self.record_alone(&Entry {
            actor: who,
            action: Action::Backup,
            target: None,
            version: None,
            status,
        })
        .map_err(|_| BackupError::AuditFailed)?;
        done
    }

    fn copy_all(&self, to: &Path) -> Result<Backup, BackupError> {
        let fail = |_| BackupError::Io;
        match self.fs.list(to) {
            Err(e) if e.kind() == io::ErrorKind::NotFound => {}
            Ok(_) => return Err(BackupError::Exists),
            Err(e) if e.kind() == io::ErrorKind::NotADirectory => return Err(BackupError::Exists),
            Err(_) => return Err(BackupError::Io),
        }
        let tenants = to.join("tenants");
        self.fs.create_dir_all(&tenants).map_err(fail)?;
        // Only the service account may enter the copy, as with the data
        // directory, which is restored by copying this one with its modes.
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(to, std::fs::Permissions::from_mode(crate::fs::DIR_MODE))
                .map_err(fail)?;
        }
        let registry = to.join("server.sqlite");
        'tries: for tries in 1..=TRIES {
            for file in ["server.sqlite", "server.sqlite-wal", "server.sqlite-shm"] {
                remove_if_there(&to.join(file)).map_err(fail)?;
            }
            let listed = match self.registry.backup_into(&registry) {
                Ok(listed) => listed,
                Err(TenantError::NoFileBackup) => {
                    // Nothing was copied: the directory made for the copy
                    // goes, so the path is free for a later backup.
                    self.fs.remove_dir_all(to).map_err(fail)?;
                    return Err(BackupError::NoFileBackup);
                }
                Err(_) => return Err(BackupError::Io),
            };
            #[cfg(test)]
            if let Some(hook) = self.hooks.registry_copied.lock().expect("hook").as_mut() {
                hook(self);
            }
            let mut versions = 0;
            for id in &listed {
                let copied = match self.store(*id) {
                    Ok(store) => store.backup_to(&tenants.join(id.to_string())),
                    Err(TenantError::NotFound | TenantError::Closing) => continue 'tries,
                    Err(_) => return Err(BackupError::Io),
                };
                match copied {
                    Ok(copied) => versions += copied.versions,
                    // A tenant removed while it was copied: its files went
                    // with it, so no try of its own could finish.
                    Err(store::BackupError::KeptChanging) if !self.listed(*id)? => {
                        self.stores().remove(id);
                        continue 'tries;
                    }
                    Err(store::BackupError::KeptChanging) => return Err(BackupError::KeptChanging),
                    Err(store::BackupError::Io) => return Err(BackupError::Io),
                }
            }
            let names: BTreeSet<String> = listed.iter().map(ToString::to_string).collect();
            for name in self.fs.list(&tenants).map_err(fail)? {
                let name = name.into_string().unwrap_or_default();
                if !names.contains(&name) {
                    self.fs.remove_dir_all(&tenants.join(&name)).map_err(fail)?;
                }
            }
            self.fs.sync_dir(&tenants).map_err(fail)?;
            self.fs.sync_dir(to).map_err(fail)?;
            if let Some(parent) = to.parent() {
                self.fs.sync_dir(parent).map_err(fail)?;
            }
            return Ok(Backup {
                tenants: listed.len() as u64,
                versions,
                tries,
            });
        }
        Err(BackupError::KeptChanging)
    }

    /// Whether server.sqlite, as it is now, lists the tenant.
    fn listed(&self, id: TenantId) -> Result<bool, BackupError> {
        let mut tx = self.registry.snapshot().map_err(|_| BackupError::Io)?;
        Ok(tx.tenant(id).map_err(|_| BackupError::Io)?.is_some())
    }
}

fn remove_if_there(path: &Path) -> io::Result<()> {
    match std::fs::remove_file(path) {
        Err(e) if e.kind() != io::ErrorKind::NotFound => Err(e),
        _ => Ok(()),
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};

    use super::*;
    use crate::clock::Clock;
    use crate::fs::Fs;
    use crate::ids::TenantId;
    use crate::store::Caller;
    use crate::store::testing::vcon;
    use crate::tenants::Removal;
    use crate::tenants::tests::{Fixture, fixture};

    /// Two tenants, each holding a vCon.
    fn two(f: &Fixture) -> [TenantId; 2] {
        let caller = Caller {
            key: crate::envelope::Key::generate().expect("key").id(),
        };
        let ids = ["acme", "globex"]
            .map(|name| f.tenants.create(Actor::Cli, name, None).expect("creates"));
        for (n, id) in ids.iter().enumerate() {
            let (found, body) = vcon(&format!("01a07da8-c2bb-83e5-b9a2-27900000000{n}"), "held");
            f.tenants
                .store(*id)
                .expect("store")
                .create(&caller, &found, &body)
                .expect("create");
        }
        ids
    }

    /// The backup at `to` opened as a registry under the same master key.
    fn opened(f: &Fixture, to: &Path) -> Tenants {
        Tenants::open_beside(to, f.master_key(), f.fs.clone(), f.clock.clone())
            .expect("the backup opens under the master key")
    }

    /// Checks the backup `backup` holds `tenants` tenants, each whole with
    /// its one version, examined at the fixture's time.
    fn assert_whole(backup: &Tenants, tenants: usize, now: time::OffsetDateTime) {
        let found = backup
            .examine(crate::retention::Retention::Years(7), now)
            .expect("examines");
        assert_eq!(
            found.tenants.len(),
            tenants,
            "the backup holds every tenant the registry did"
        );
        for tenant in &found.tenants {
            let store = tenant
                .store
                .as_ref()
                .expect("each catalog opens under its key");
            assert!(
                store.damaged.is_empty(),
                "every copied version opens: {:?}",
                store.damaged
            );
            assert!(
                store.leftovers.is_empty(),
                "the copy holds no file its catalog does not name: {:?}",
                store.leftovers
            );
            assert_eq!(store.versions, 1, "each tenant stored one version");
        }
    }

    fn records(f: &Fixture, action: &str) -> Vec<serde_json::Value> {
        f.tenants
            .admin_records()
            .into_iter()
            .filter(|r| r["action"] == action)
            .collect()
    }

    #[test]
    fn a_backup_opens_whole_under_the_master_key_and_is_recorded() {
        let f = fixture();
        two(&f);
        let to = f.dir.path().join("backup");
        let done = f.tenants.backup(Actor::Cli, &to).expect("backs up");
        assert_eq!(
            done,
            Backup {
                tenants: 2,
                versions: 2,
                tries: 1
            }
        );
        let backup = opened(&f, &to);
        assert_whole(&backup, 2, f.clock.now());
        use std::os::unix::fs::PermissionsExt;
        let mode = std::fs::metadata(&to)
            .expect("the backup")
            .permissions()
            .mode()
            & 0o777;
        assert_eq!(
            mode, 0o700,
            "only the service account may enter the backup: {mode:o}"
        );
        let names: Vec<String> = backup
            .list(Actor::Cli)
            .expect("lists")
            .into_iter()
            .map(|t| t.name)
            .collect();
        assert_eq!(names, ["acme", "globex"]);
        let recorded = records(&f, "backup");
        assert_eq!(recorded.len(), 1);
        assert_eq!(recorded[0]["status"], 200);
    }

    #[test]
    fn a_backup_survives_a_crash_once_it_returns() {
        let f = fixture();
        two(&f);
        let to = f.dir.path().join("backup");
        f.tenants.backup(Actor::Cli, &to).expect("backs up");
        f.fs.crash();
        assert_eq!(
            f.fs.list(&to.join("tenants"))
                .expect("the backup is there")
                .len(),
            2
        );
        assert_whole(&opened(&f, &to), 2, f.clock.now());
    }

    #[test]
    fn a_path_already_there_is_refused_and_recorded() {
        let f = fixture();
        two(&f);
        let to = f.dir.path().join("backup");
        f.fs.create_dir_all(&to).expect("dir");
        assert_eq!(f.tenants.backup(Actor::Cli, &to), Err(BackupError::Exists));
        assert!(
            f.fs.list(&to).expect("list").is_empty(),
            "nothing is written"
        );
        assert_eq!(records(&f, "backup")[0]["status"], 409);
    }

    #[test]
    fn a_tenant_removed_after_the_registry_is_copied_starts_the_backup_again() {
        let f = fixture();
        let [acme, _] = two(&f);
        let calls = Arc::new(AtomicUsize::new(0));
        let seen = calls.clone();
        *f.tenants.hooks.registry_copied.lock().expect("hook") =
            Some(Box::new(move |tenants: &Tenants| {
                if seen.fetch_add(1, Ordering::SeqCst) == 0 {
                    tenants
                        .remove(Actor::Cli, acme, Removal::Force)
                        .expect("removes");
                }
            }));
        let to = f.dir.path().join("backup");
        let done = f.tenants.backup(Actor::Cli, &to).expect("backs up");
        assert_eq!(
            done,
            Backup {
                tenants: 1,
                versions: 1,
                tries: 2
            }
        );
        assert_whole(&opened(&f, &to), 1, f.clock.now());
        let dirs = f.fs.list(&to.join("tenants")).expect("list");
        assert_eq!(
            dirs.len(),
            1,
            "no directory is left for the removed tenant: {dirs:?}"
        );
    }

    #[test]
    fn a_tenant_removed_while_its_data_is_copied_starts_the_backup_again() {
        let f = fixture();
        let [acme, _] = two(&f);
        // Another registry on the same directory, as the server would be,
        // removes the tenant while this one copies its files: after the
        // tenant's catalog is copied, before its object files are.
        let server =
            Tenants::open_beside(f.dir.path(), f.master_key(), f.fs.clone(), f.clock.clone())
                .expect("a second registry");
        let once = std::sync::Mutex::new(Some(server));
        f.tenants
            .store(acme)
            .expect("store")
            .on_backup_copied(Box::new(move |_: &crate::store::Store| {
                if let Some(server) = once.lock().expect("once").take() {
                    server
                        .remove(Actor::Cli, acme, Removal::Force)
                        .expect("removes");
                }
            }));
        let to = f.dir.path().join("backup");
        let done = f.tenants.backup(Actor::Cli, &to).expect("backs up");
        assert_eq!(
            done,
            Backup {
                tenants: 1,
                versions: 1,
                tries: 2
            },
            "the second try copies the one tenant left and its one version"
        );
        assert_whole(&opened(&f, &to), 1, f.clock.now());
        let dirs = f.fs.list(&to.join("tenants")).expect("list");
        assert_eq!(
            dirs.len(),
            1,
            "the removed tenant's partial copy is gone: {dirs:?}"
        );
    }

    #[test]
    fn a_registry_that_keeps_changing_fails_after_its_tries_and_is_recorded() {
        let f = fixture();
        let [acme, _] = two(&f);
        let calls = Arc::new(AtomicUsize::new(0));
        let seen = calls.clone();
        let listed = std::sync::Mutex::new(acme);
        *f.tenants.hooks.registry_copied.lock().expect("hook") =
            Some(Box::new(move |tenants: &Tenants| {
                // Each try: a tenant the copy lists is removed before its
                // data is copied, and a new one is made, which the next
                // copy lists.
                let n = seen.fetch_add(1, Ordering::SeqCst);
                let mut listed = listed.lock().expect("listed");
                tenants
                    .remove(Actor::Cli, *listed, Removal::Force)
                    .expect("removes");
                *listed = tenants
                    .create(Actor::Cli, &format!("brief {n}"), None)
                    .expect("creates");
            }));
        let to = f.dir.path().join("backup");
        assert_eq!(
            f.tenants.backup(Actor::Cli, &to),
            Err(BackupError::KeptChanging)
        );
        assert_eq!(calls.load(Ordering::SeqCst), crate::store::TRIES);
        assert_eq!(records(&f, "backup")[0]["status"], 503);
    }
}