vcon-server 0.4.7

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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! Schema version 7: the order versions were stored in (`seq` of the
//! version table), and the application role's leave to vacuum the tables a
//! purge deletes from. Each live test starts from a database at version
//! 6, as a build of `main` before the catalog's statements left one.

use super::super::super::schema::{MIGRATIONS, SCHEMA_VERSION, version_refusal};

/// The position of this version's migration.
const THIS: usize = 6;

/// The statement that adds the column.
const ADD: &str = "ALTER TABLE vcon.version ADD COLUMN seq bigint GENERATED ALWAYS AS IDENTITY;";

#[test]
fn version_7_is_the_seventh_migration() {
    assert!(
        MIGRATIONS.len() > THIS,
        "there are {} migrations",
        MIGRATIONS.len()
    );
    assert!(
        MIGRATIONS[THIS].lines().any(|line| line == ADD),
        "the seventh adds seq, numbered by the server"
    );
}

#[test]
fn version_7_grants_the_vacuum_privilege_only_where_the_server_has_it() {
    let text = MIGRATIONS[THIS];
    assert!(
        text.contains("IF current_setting('server_version_num')::integer >= 170000 THEN"),
        "PostgreSQL 16 has no MAINTAIN privilege"
    );
    assert!(
        text.contains("'GRANT MAINTAIN ON vcon.version, vcon.vcon, vcon.tombstone TO vcon_app'"),
        "the three tables a purge deletes from, and no other"
    );
}

#[test]
fn the_binary_s_version_is_at_least_7() {
    assert!(
        usize::try_from(SCHEMA_VERSION).expect("positive") > THIS,
        "this binary's version: {SCHEMA_VERSION}"
    );
}

#[test]
fn a_version_6_database_is_refused_with_the_command_that_upgrades_it() {
    let why = version_refusal(Some(6)).expect("refused");
    assert!(
        why.contains(&format!(
            "version 6, older than this binary's {SCHEMA_VERSION}"
        )) && why.contains("vcon-server migrate"),
        "the versions and the command: {why}"
    );
}

#[test]
fn version_7_changes_no_row_by_a_statement_and_makes_no_table() {
    // The owner role is held to row-level security too, so an UPDATE or
    // DELETE in a migration would raise. A statement begins its line.
    for statement in ["UPDATE ", "DELETE ", "DROP ", "TRUNCATE ", "CREATE "] {
        assert!(
            !MIGRATIONS[THIS]
                .lines()
                .any(|line| line.trim_start().to_uppercase().starts_with(statement)),
            "no {statement}in version 7"
        );
    }
}

#[cfg(feature = "backend-tests")]
mod on_a_server {
    use super::super::super::super::connect::Purpose;
    use super::super::super::super::schema::{MIGRATIONS, Migrated, SCHEMA_VERSION};
    use super::super::super::live::value;
    use super::super::live::{Db, TENANT_A, TENANT_B, db_message, in_scope};
    use super::{ADD, THIS};

    /// A database at version 6.
    async fn at_version_6() -> Db {
        let db = Db::fresh().await;
        assert_eq!(
            db.migrate_with(&MIGRATIONS[..THIS])
                .await
                .expect("version 6"),
            Migrated { from: 0, to: 6 },
            "the first six migrations alone"
        );
        db
    }

    /// A version row of `tenant` whose id ends in `n`.
    fn insert_version(tenant: &str, n: u8) -> String {
        format!(
            "INSERT INTO vcon.version (tenant, version, uuid_mac, form_sealed, gzip_sealed, \
             size_sealed, stored_at, tenant_key_id, wrapped_key) \
             VALUES ('\\x{tenant}', '\\x{n:02x}', '\\x02', '\\x03', '\\x04', '\\x05', 1, '\\x06', \
             '\\x07')"
        )
    }

    /// The one value `sql` gives the application role in `tenant`'s scope.
    async fn read(db: &Db, tenant: &str, sql: &str) -> Option<String> {
        in_scope(&db.app().await, tenant, sql)
            .await
            .expect("reads")
            .iter()
            .find_map(|m| match m {
                tokio_postgres::SimpleQueryMessage::Row(r) => Some(r.get(0).map(str::to_owned)),
                _ => None,
            })
            .expect("a row")
    }

    const COLUMN: &str = "SELECT count(*) FROM information_schema.columns WHERE \
        table_schema = 'vcon' AND table_name = 'version' AND column_name = 'seq' \
        AND data_type = 'bigint' AND is_identity = 'YES' AND identity_generation = 'ALWAYS'";

    // What must work.

    #[tokio::test]
    async fn a_version_6_database_is_brought_to_version_7_with_its_versions_numbered() {
        let db = at_version_6().await;
        // A server is refused at version 6, so the tester writes the rows.
        let app = db.admin().await;
        for n in 1..=3 {
            in_scope(&app, TENANT_A, &insert_version(TENANT_A, n))
                .await
                .expect("a version, stored before the upgrade");
        }
        for n in 1..=2 {
            in_scope(&app, TENANT_B, &insert_version(TENANT_B, n))
                .await
                .expect("another tenant's");
        }
        drop(app);
        assert_eq!(
            db.migrate().await,
            Migrated {
                from: 6,
                to: SCHEMA_VERSION
            },
            "every migration after the sixth runs"
        );
        assert_eq!(
            read(
                &db,
                TENANT_A,
                "SELECT count(DISTINCT seq) || ' ' || count(*) FROM vcon.version"
            )
            .await
            .as_deref(),
            Some("3 3"),
            "the three versions kept their rows, each with a number of its own"
        );
        assert_eq!(
            read(
                &db,
                TENANT_B,
                "SELECT count(DISTINCT seq) || ' ' || count(*) FROM vcon.version"
            )
            .await
            .as_deref(),
            Some("2 2"),
            "and the other tenant's two"
        );
    }

    #[tokio::test]
    async fn the_column_is_made_by_version_7_and_not_before() {
        let db = at_version_6().await;
        let admin = db.admin().await;
        assert_eq!(value(&admin, COLUMN).await, "0", "not at version 6");
        db.migrate().await;
        assert_eq!(value(&admin, COLUMN).await, "1", "there at version 7");
    }

    #[tokio::test]
    async fn versions_take_rising_numbers_in_the_order_they_are_stored() {
        let db = Db::migrated().await;
        let app = db.app().await;
        // The ids fall while the numbers rise: the order is the storing's.
        for n in [9, 5, 1] {
            in_scope(&app, TENANT_A, &insert_version(TENANT_A, n))
                .await
                .expect("stores");
        }
        assert_eq!(
            read(
                &db,
                TENANT_A,
                "SELECT string_agg(encode(version, 'hex'), ' ' ORDER BY seq) FROM vcon.version"
            )
            .await
            .as_deref(),
            Some("09 05 01"),
            "by seq, the versions are in the order they were stored"
        );
    }

    #[tokio::test]
    async fn a_serving_connection_is_accepted_once_version_7_is_there() {
        let db = at_version_6().await;
        let e = db
            .connect("vcon_app", Purpose::Serve)
            .await
            .expect_err("refused at version 6")
            .to_string();
        assert!(e.contains("version 6, older"), "says the version: {e}");
        db.migrate().await;
        assert!(
            db.connect("vcon_app", Purpose::Serve).await.is_ok(),
            "accepted at this binary's version"
        );
    }

    #[tokio::test]
    async fn a_new_database_has_the_column() {
        let db = Db::migrated().await;
        assert_eq!(
            value(&db.admin().await, COLUMN).await,
            "1",
            "every migration from none makes it"
        );
    }

    #[tokio::test]
    async fn the_application_role_may_vacuum_the_purged_tables_from_postgresql_17() {
        let db = Db::migrated().await;
        let admin = db.admin().await;
        let from_17 = value(
            &admin,
            "SELECT current_setting('server_version_num')::int >= 170000",
        )
        .await
            == "t";
        if from_17 {
            assert_eq!(
                value(
                    &admin,
                    "SELECT string_agg(c.relname, ' ' ORDER BY c.relname) FROM pg_class c \
                     JOIN pg_namespace n ON n.oid = c.relnamespace \
                     WHERE n.nspname = 'vcon' AND c.relkind = 'r' \
                       AND has_table_privilege('vcon_app', c.oid, 'MAINTAIN')"
                )
                .await,
                "tombstone vcon version",
                "the three tables a purge deletes from, and no other"
            );
        }
        db.app()
            .await
            .batch_execute("VACUUM vcon.version, vcon.vcon, vcon.tombstone")
            .await
            .expect("VACUUM by the application role is no error on any version");
    }

    // What must be refused.

    #[tokio::test]
    async fn a_version_7_that_fails_leaves_version_6_whole() {
        let db = at_version_6().await;
        let broken = format!("{ADD}\nSELECT boom;");
        let mut migrations: Vec<&str> = MIGRATIONS[..THIS].to_vec();
        migrations.push(&broken);
        let e = db.migrate_with(&migrations).await.expect_err("fails");
        assert!(e.starts_with("version 7: "), "names the version: {e}");
        let admin = db.admin().await;
        assert_eq!(
            value(&admin, COLUMN).await,
            "0",
            "the column was rolled back with it"
        );
        assert_eq!(
            db.migrate().await,
            Migrated {
                from: 6,
                to: SCHEMA_VERSION
            },
            "and the real migration then runs from 6"
        );
    }

    #[tokio::test]
    async fn the_application_role_cannot_give_a_version_its_number() {
        let db = Db::migrated().await;
        let statement = insert_version(TENANT_A, 1)
            .replace("wrapped_key) VALUES", "wrapped_key, seq) VALUES")
            .replace("'\\x07')", "'\\x07', 99)");
        assert!(
            statement.contains(", seq) VALUES") && statement.ends_with(", 99)"),
            "{statement}"
        );
        let e = in_scope(&db.app().await, TENANT_A, &statement)
            .await
            .expect_err("refused");
        assert!(
            e.contains("cannot insert a non-DEFAULT value into column \"seq\""),
            "the server numbers the row: {e}"
        );
    }

    #[tokio::test]
    async fn the_application_role_cannot_change_a_versions_number() {
        let db = Db::migrated().await;
        let app = db.app().await;
        in_scope(&app, TENANT_A, &insert_version(TENANT_A, 1))
            .await
            .expect("stores");
        let e = in_scope(&app, TENANT_A, "UPDATE vcon.version SET seq = 1000")
            .await
            .expect_err("refused");
        assert!(
            e.contains("column \"seq\" can only be updated to DEFAULT"),
            "the number is the server's: {e}"
        );
    }

    #[tokio::test]
    async fn the_application_role_cannot_drop_or_retype_the_column() {
        let db = Db::migrated().await;
        let app = db.app().await;
        for statement in [
            "ALTER TABLE vcon.version DROP COLUMN seq",
            "ALTER TABLE vcon.version ALTER COLUMN seq TYPE integer",
            "ALTER TABLE vcon.version ALTER COLUMN seq DROP IDENTITY",
        ] {
            let e = app.batch_execute(statement).await.expect_err("refused");
            assert!(
                db_message(&e).contains("must be owner of table version"),
                "{statement}: {e}"
            );
        }
    }

    #[tokio::test]
    async fn a_tenants_transaction_reads_no_number_of_another_tenant() {
        let db = Db::migrated().await;
        let app = db.app().await;
        in_scope(&app, TENANT_A, &insert_version(TENANT_A, 1))
            .await
            .expect("stores");
        assert_eq!(
            read(&db, TENANT_B, "SELECT count(seq) FROM vcon.version")
                .await
                .as_deref(),
            Some("0"),
            "row-level security covers the new column as the others"
        );
    }

    // The limits and odd inputs.

    #[tokio::test]
    async fn migrating_twice_from_version_6_does_the_work_once() {
        let db = at_version_6().await;
        assert_eq!(
            db.migrate().await,
            Migrated {
                from: 6,
                to: SCHEMA_VERSION
            },
            "the first run"
        );
        assert_eq!(
            db.migrate().await,
            Migrated {
                from: SCHEMA_VERSION,
                to: SCHEMA_VERSION
            },
            "the second finds nothing to do"
        );
    }

    #[tokio::test]
    async fn a_version_6_database_with_no_version_gains_the_column() {
        let db = at_version_6().await;
        db.migrate().await;
        assert_eq!(
            read(&db, TENANT_A, "SELECT count(*) FROM vcon.version")
                .await
                .as_deref(),
            Some("0"),
            "no row was made"
        );
        in_scope(&db.app().await, TENANT_A, &insert_version(TENANT_A, 1))
            .await
            .expect("and the first version stored takes a number");
    }

    #[tokio::test]
    async fn the_numbers_of_two_tenants_come_from_one_sequence() {
        let db = Db::migrated().await;
        let app = db.app().await;
        in_scope(&app, TENANT_A, &insert_version(TENANT_A, 1))
            .await
            .expect("stores");
        in_scope(&app, TENANT_B, &insert_version(TENANT_B, 1))
            .await
            .expect("stores");
        in_scope(&app, TENANT_A, &insert_version(TENANT_A, 2))
            .await
            .expect("stores");
        let a = read(
            &db,
            TENANT_A,
            "SELECT max(seq) - min(seq) FROM vcon.version",
        )
        .await;
        assert_eq!(
            a.as_deref(),
            Some("4"),
            "the other tenant's version took the numbers between, two a row since version 8: the \
             order is the database's"
        );
    }

    #[tokio::test]
    async fn a_number_is_not_given_again_after_its_version_is_deleted() {
        let db = Db::migrated().await;
        let app = db.app().await;
        in_scope(&app, TENANT_A, &insert_version(TENANT_A, 1))
            .await
            .expect("stores");
        let first = read(&db, TENANT_A, "SELECT seq FROM vcon.version").await;
        in_scope(&app, TENANT_A, "DELETE FROM vcon.version")
            .await
            .expect("purges");
        in_scope(&app, TENANT_A, &insert_version(TENANT_A, 1))
            .await
            .expect("stores the same id again");
        let second = read(&db, TENANT_A, "SELECT seq FROM vcon.version").await;
        assert!(
            second.as_deref().and_then(|s| s.parse::<i64>().ok())
                > first.as_deref().and_then(|s| s.parse::<i64>().ok()),
            "a later version has a later number: {first:?} then {second:?}"
        );
    }
}