vcon-server 0.4.8

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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! The upgrade to schema version 6, tested from a server.sqlite made as
//! schema version 5 made it: the tables below are that version's text,
//! kept here so the upgrade is always run on what it will meet. No
//! release wrote version 5; a build of `main` between releases 0.4.3 and
//! the next did.
//!
//! Version 6 adds a tenant's role cap to server.sqlite: the column
//! `role_cap` of the tenant table, NULL for a tenant with no cap.

use rusqlite::Connection;

use super::v5_tests::{
    KEYS_V4, Old, REGISTRY_ROWS, REGISTRY_V4, TENANT_V4, declared, number, rows, tables, version,
};
use super::{CatalogError, SCHEMA_VERSION, open};
use crate::backend::Registry;
use crate::backend::local::LocalRegistry;
use crate::envelope::Key;

/// The users and memberships tables of schema version 5.
pub(super) const USERS_V5: &str = "
CREATE TABLE user_account (
    id BLOB PRIMARY KEY NOT NULL,
    label TEXT NOT NULL,
    issuer TEXT,
    subject TEXT,
    created_at INTEGER NOT NULL,
    deactivated_at INTEGER,
    CHECK ((issuer IS NULL) = (subject IS NULL))
);
CREATE UNIQUE INDEX user_account_identity ON user_account (issuer, subject)
    WHERE issuer IS NOT NULL AND deactivated_at IS NULL;
CREATE TABLE membership (
    user BLOB NOT NULL,
    tenant BLOB,
    roles TEXT NOT NULL,
    granted_at INTEGER NOT NULL
);
CREATE UNIQUE INDEX membership_in_tenant ON membership (user, tenant)
    WHERE tenant IS NOT NULL;
CREATE UNIQUE INDEX membership_as_operator ON membership (user)
    WHERE tenant IS NULL;
CREATE INDEX membership_of_tenant ON membership (tenant);
";

/// Every catalog's audit log at schema version 5.
pub(super) const AUDIT_V5: &str = "
CREATE TABLE audit (
    seq INTEGER PRIMARY KEY AUTOINCREMENT,
    at TEXT NOT NULL,
    actor TEXT NOT NULL,
    action TEXT NOT NULL,
    target TEXT,
    version TEXT,
    status INTEGER,
    count INTEGER NOT NULL
);
CREATE INDEX audit_counted ON audit (actor, action, status, at);
";

/// Rows a version-5 server.sqlite could hold besides those of version 4:
/// a second tenant, a user, and the user's membership in the first tenant.
pub(super) const ROWS_V5: &str = "
INSERT INTO tenant VALUES (x'b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2', 'beta', NULL, 8, x'0505050505050505',
    x'feed', NULL, 8, NULL);
INSERT INTO user_account VALUES (x'c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3', 'Reader', NULL, NULL, 12, NULL);
INSERT INTO membership VALUES (x'c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3', x'a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1',
    'reader,legal', 13);
INSERT INTO audit (at, actor, action, target, version, status, count) VALUES
    ('2026-11-11T10:00:00Z', 'cli', 'user_add', 'c3c3', NULL, 201, 1),
    ('2026-11-11T10:02:00Z', '', 'listener_full', 'listen', NULL, NULL, 2);
";

fn registry() -> Old {
    Old::at(
        5,
        &[REGISTRY_V4, USERS_V5, AUDIT_V5, REGISTRY_ROWS, ROWS_V5],
    )
}

const TENANT_READ: &str = "SELECT id, name, retention, created_at, tenant_key_id, \
     wrapped_tenant_key, pending_wrapped_tenant_key, key_since, metadata FROM tenant ORDER BY id";

/// The columns of a table, in order.
pub(super) fn columns(conn: &Connection, table: &str) -> Vec<String> {
    rows(
        conn,
        &format!("SELECT name FROM pragma_table_info('{table}')"),
    )
}

// What must work.

#[test]
fn a_version_5_registry_gains_the_role_cap_column() {
    let conn = registry().upgraded();
    assert_eq!(
        version(&conn),
        SCHEMA_VERSION,
        "the upgraded registry's version"
    );
    assert_eq!(
        columns(&conn, "tenant").last().map(String::as_str),
        Some("role_cap"),
        "the new column, after the others"
    );
}

#[test]
fn every_tenant_of_an_upgraded_registry_has_no_cap() {
    let conn = registry().upgraded();
    assert_eq!(
        rows(&conn, "SELECT name, role_cap FROM tenant ORDER BY name"),
        ["acme|NULL", "beta|NULL"],
        "NULL: every tenant role is allowed, as before the upgrade"
    );
}

#[test]
fn a_version_5_registry_keeps_its_tenants_as_they_were() {
    let old = registry();
    let before = rows(&old.raw(), TENANT_READ);
    assert_eq!(before.len(), 2, "two tenants to keep");
    assert_eq!(rows(&old.upgraded(), TENANT_READ), before, "every column");
}

#[test]
fn a_version_5_registry_keeps_its_users_memberships_keys_and_log() {
    let old = registry();
    let read = |conn: &Connection| {
        [
            rows(conn, "SELECT * FROM user_account ORDER BY id"),
            rows(conn, "SELECT * FROM membership ORDER BY user"),
            rows(conn, KEYS_V4),
            rows(conn, "SELECT * FROM master_key_use"),
            rows(conn, "SELECT * FROM audit ORDER BY seq"),
        ]
    };
    let before = read(&old.raw());
    assert!(before.iter().all(|table| !table.is_empty()), "rows to keep");
    assert_eq!(read(&old.upgraded()), before, "every row of every table");
}

#[test]
fn an_upgraded_version_5_registry_is_declared_as_a_new_one_is() {
    let dir = tempfile::tempdir().expect("temp dir");
    let new = LocalRegistry::open(dir.path(), Key::generate().expect("key")).expect("a new one");
    assert_eq!(
        declared(&registry().upgraded()),
        declared(&new.local_conn()),
        "tables and indexes, with their text"
    );
}

#[test]
fn a_new_registry_is_made_at_version_6_with_the_column() {
    let dir = tempfile::tempdir().expect("temp dir");
    let new = LocalRegistry::open(dir.path(), Key::generate().expect("key")).expect("a new one");
    let conn = new.local_conn();
    assert_eq!(version(&conn), SCHEMA_VERSION, "a new registry's version");
    assert!(
        columns(&conn, "tenant").contains(&"role_cap".to_owned()),
        "the column is in a new registry"
    );
}

#[test]
fn the_column_of_an_upgraded_registry_takes_a_cap_and_gives_it_up() {
    let conn = registry().upgraded();
    conn.execute(
        "UPDATE tenant SET role_cap = 'reader,auditor' WHERE name = 'acme'",
        [],
    )
    .expect("a cap");
    assert_eq!(
        rows(&conn, "SELECT name, role_cap FROM tenant ORDER BY name"),
        ["acme|reader,auditor", "beta|NULL"],
        "one tenant's cap"
    );
    conn.execute("UPDATE tenant SET role_cap = NULL", [])
        .expect("no cap");
    assert_eq!(
        number(
            &conn,
            "SELECT count(*) FROM tenant WHERE role_cap IS NOT NULL"
        ),
        0,
        "NULL again"
    );
}

#[test]
fn a_0_4_2_registry_reaches_version_6_in_one_step() {
    let old = Old::registry();
    let conn = old.upgraded();
    assert_eq!(version(&conn), SCHEMA_VERSION, "from version 4");
    assert!(
        tables(&conn).contains(&"membership".to_owned()),
        "version 5's tables"
    );
    assert_eq!(
        rows(&conn, "SELECT name, role_cap FROM tenant"),
        ["acme|NULL"],
        "version 6's column"
    );
}

#[test]
fn registries_stamped_with_every_older_version_gain_the_column() {
    for from in 1..=5 {
        let old = Old::at(from, &[]);
        old.raw()
            .execute_batch("CREATE TABLE tenant (id BLOB PRIMARY KEY NOT NULL)")
            .expect("a tenant table");
        let conn = old.upgraded();
        assert_eq!(version(&conn), SCHEMA_VERSION, "from {from}");
        assert!(
            columns(&conn, "tenant").contains(&"role_cap".to_owned()),
            "from {from}"
        );
    }
}

#[test]
fn a_re_encrypted_upgraded_registry_keeps_the_column_and_a_cap() {
    let old = registry();
    let conn = old.upgraded();
    conn.execute(
        "UPDATE tenant SET role_cap = 'legal' WHERE name = 'beta'",
        [],
    )
    .expect("a cap");
    drop(conn);
    let new_key = Key::generate().expect("key");
    super::reencrypt(&old.path(), &old.key, &new_key).expect("re-encrypted");
    let conn = open(&old.path(), &new_key, "").expect("opens under the new key");
    assert_eq!(
        version(&conn),
        SCHEMA_VERSION,
        "the version after re-encryption"
    );
    assert_eq!(
        rows(&conn, "SELECT name, role_cap FROM tenant ORDER BY name"),
        ["acme|NULL", "beta|legal"],
        "the caps after re-encryption"
    );
}

#[test]
fn opening_an_upgraded_version_5_registry_again_changes_nothing() {
    let old = registry();
    let first = old.upgraded();
    let (text, tenants) = (declared(&first), rows(&first, "SELECT * FROM tenant"));
    drop(first);
    let second = old.upgraded();
    assert_eq!(declared(&second), text, "the same tables");
    assert_eq!(
        rows(&second, "SELECT * FROM tenant"),
        tenants,
        "the same rows"
    );
}

#[test]
fn an_upgraded_registry_serves_the_role_cap_calls() {
    let old = registry();
    drop(old.upgraded());
    let Old { dir, key } = old;
    let registry = LocalRegistry::open(dir.path(), key).expect("the upgraded registry opens");
    let mut tx = registry.immediate().expect("a transaction");
    let acme = crate::ids::TenantId([0xa1; 16]);
    assert_eq!(
        tx.tenant(acme).expect("read").expect("acme").role_cap,
        None,
        "no cap after the upgrade"
    );
    assert_eq!(
        tx.set_role_cap(acme, Some("reader")),
        Ok(true),
        "the cap is set"
    );
    assert_eq!(
        tx.tenant(acme)
            .expect("read")
            .expect("acme")
            .role_cap
            .as_deref(),
        Some("reader"),
        "and read back"
    );
}

// What must be refused.

#[test]
fn an_upgrade_that_fails_at_the_column_leaves_version_5_whole() {
    let old = registry();
    old.raw()
        .execute_batch("ALTER TABLE tenant ADD COLUMN role_cap INTEGER")
        .expect("a column in the way");
    let before = declared(&old.raw());
    assert!(
        matches!(old.open(), Err(CatalogError::Sqlite(_))),
        "the upgrade fails on the column in its way"
    );
    let after = old.raw();
    assert_eq!(version(&after), 5, "still version 5");
    assert_eq!(declared(&after), before, "nothing of the upgrade is kept");
}

#[test]
fn a_failed_upgrade_from_0_4_2_keeps_none_of_its_steps() {
    let old = Old::registry();
    old.raw()
        .execute_batch("ALTER TABLE tenant ADD COLUMN role_cap INTEGER")
        .expect("a column in the way");
    let before = declared(&old.raw());
    assert!(
        matches!(old.open(), Err(CatalogError::Sqlite(_))),
        "the last step fails"
    );
    let after = old.raw();
    assert_eq!(version(&after), 4, "still version 4");
    assert_eq!(
        declared(&after),
        before,
        "the user tables and the new log made earlier in the transaction are gone"
    );
    assert_eq!(
        number(&after, "SELECT count(*) FROM audit WHERE status = 0"),
        2,
        "the log still holds its zeros"
    );
}

#[test]
fn a_wrong_key_upgrades_no_version_5_registry() {
    let old = registry();
    let other = Key::generate().expect("key");
    assert!(
        matches!(open(&old.path(), &other, ""), Err(CatalogError::WrongKey)),
        "the wrong key is named"
    );
    assert_eq!(version(&old.raw()), 5, "still version 5");
    assert!(
        !columns(&old.raw(), "tenant").contains(&"role_cap".to_owned()),
        "no column was added"
    );
}

#[test]
fn the_version_after_this_builds_is_refused_and_left_as_it_was() {
    let next = SCHEMA_VERSION + 1;
    let old = Old::at(next, &[REGISTRY_V4, USERS_V5, AUDIT_V5]);
    let before = declared(&old.raw());
    assert!(
        matches!(old.open(), Err(CatalogError::UnknownSchema(n)) if n == next),
        "a version this build does not know"
    );
    assert_eq!(version(&old.raw()), next, "the number is left");
    assert_eq!(declared(&old.raw()), before, "the tables are left");
}

#[test]
fn this_build_makes_a_version_a_version_5_build_refuses() {
    // A build at version 5 opens versions 0 to 5 and refuses every other
    // number, so it cannot write a tenant row without the column's place.
    let conn = registry().upgraded();
    assert!(version(&conn) > 5, "the number: {}", version(&conn));
}

#[test]
fn the_column_does_not_check_what_it_is_given() {
    // The registry's code is what refuses a cap that names no role
    // (`Roles::parse`); the column itself is plain text.
    let conn = registry().upgraded();
    conn.execute("UPDATE tenant SET role_cap = 'owner'", [])
        .expect("any text is stored");
    assert_eq!(
        number(
            &conn,
            "SELECT count(*) FROM tenant WHERE role_cap = 'owner'"
        ),
        2,
        "both rows"
    );
}

// Limits and odd inputs.

#[test]
fn a_version_5_tenant_catalog_changes_only_its_number() {
    let old = Old::at(5, &[TENANT_V4, AUDIT_V5]);
    let before = declared(&old.raw());
    let conn = old.upgraded();
    assert_eq!(version(&conn), SCHEMA_VERSION, "the number");
    assert_eq!(
        declared(&conn),
        before,
        "a tenant catalog has no tenant table"
    );
}

#[test]
fn a_version_5_registry_with_no_tenant_gains_the_column() {
    let old = Old::at(5, &[REGISTRY_V4, USERS_V5, AUDIT_V5]);
    let conn = old.upgraded();
    assert_eq!(number(&conn, "SELECT count(*) FROM tenant"), 0, "no tenant");
    assert!(
        columns(&conn, "tenant").contains(&"role_cap".to_owned()),
        "the column is there for the first tenant"
    );
}

#[test]
fn a_version_5_catalog_with_no_tenant_table_changes_only_its_number() {
    let old = Old::at(5, &["CREATE TABLE note (text TEXT)"]);
    let before = declared(&old.raw());
    let conn = old.upgraded();
    assert_eq!(version(&conn), SCHEMA_VERSION, "the number");
    assert_eq!(declared(&conn), before, "nothing else");
}

#[test]
fn a_registry_of_many_tenants_keeps_each_and_gives_each_no_cap() {
    let old = registry();
    {
        let conn = old.raw();
        for n in 0..100_u8 {
            conn.execute(
                "INSERT INTO tenant VALUES (?1, ?2, NULL, 1, x'01', x'02', NULL, 1, NULL)",
                rusqlite::params![vec![n; 16], format!("tenant-{n}")],
            )
            .expect("a tenant");
        }
    }
    let conn = old.upgraded();
    assert_eq!(
        number(&conn, "SELECT count(*) FROM tenant"),
        102,
        "all kept"
    );
    assert_eq!(
        number(&conn, "SELECT count(*) FROM tenant WHERE role_cap IS NULL"),
        102,
        "none has a cap"
    );
}

#[test]
fn tenant_rows_still_in_the_wal_are_upgraded_too() {
    let old = registry();
    let writer = old.raw();
    writer
        .execute(
            "INSERT INTO tenant VALUES (x'd4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4', 'gamma', NULL, 9,
                 x'06', x'07', NULL, 9, NULL)",
            [],
        )
        .expect("a row not yet checkpointed");
    let conn = old.upgraded();
    assert_eq!(
        rows(&conn, "SELECT name, role_cap FROM tenant ORDER BY name"),
        ["acme|NULL", "beta|NULL", "gamma|NULL"],
        "the row in the WAL too"
    );
    drop(writer);
}