vcon-server 0.4.16

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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
//! The registry in PostgreSQL: tenants, access keys, users, memberships,
//! sessions, settings, master-key use and the admin log, as the tables of
//! schema versions 1 to 6 hold them.
//!
//! A registry transaction sets the marker `registry` where a tenant's sets
//! its id, which the policy `registry_rows` asks for, and a write holds
//! the registry's advisory lock to its end, so the registry has one writer
//! at a time, as the local registry has.
//!
//! What a person typed, and what names one, is sealed under the column key
//! derived from the master key's catalog key, in the registry's context
//! ([`REGISTRY`]): a tenant's name, retention, metadata level and role
//! cap; a key's scopes and label; a user's label and identity; a
//! membership's roles; a session's scopes and authentication methods; a
//! setting's value; an admin record's target. A tenant's name and a
//! user's identity are found by their HMAC under the index key. Ids,
//! times, hashes and wrapped keys are stored as they are.
//!
//! A write that fails aborts its transaction, as PostgreSQL does: every
//! later statement is refused and the commit answers
//! [`CommitError::RolledBack`]. The local registry's transaction goes on
//! after a failed write; no caller relies on that.

use std::io::Write;
use std::path::Path;
use std::sync::Arc;

use time::OffsetDateTime;
use tokio_postgres::Row;
use tokio_postgres::types::{FromSql, Type};

use super::catalog::{
    AuditKey, Begin, Database, PgTx, all_changed, audit_key_id, new_audit_key, open_audit_keys,
    reseal_audit_keys,
};
use crate::audit::{Action, Actor, AuditError, Entry};
use crate::backend::{
    CommitError, KeyRow, MembershipRow, Registry, RegistryTx, SessionRow, TenantRow, UserRow,
};
use crate::clock::nanos;
use crate::envelope::{self as column, ColumnKeys, Key, REGISTRY};
use crate::ids::{KeyId, TenantId, UserId};
use crate::tenants::TenantError;

/// The registry in the catalog database.
pub(crate) struct PgRegistry {
    database: Arc<Database>,
    keys: ColumnKeys,
    /// The keys of the registry's audit log that opened, oldest first:
    /// the last seals the new records (schema version 12).
    audit: Vec<AuditKey>,
    #[cfg(test)]
    fault: super::catalog::Fault,
}

fn io<E>(_: E) -> TenantError {
    TenantError::Io
}

/// `bytes` as an id of `N` bytes.
fn fixed<const N: usize>(bytes: &[u8]) -> Result<[u8; N], TenantError> {
    bytes.try_into().map_err(io)
}

fn col<'r, T: FromSql<'r>>(row: &'r Row, at: usize) -> Result<T, TenantError> {
    row.try_get(at).map_err(io)
}

/// A user's identity as the bytes its HMAC is of and its sealed copy
/// holds: the issuer's length in four bytes, the issuer, then the subject,
/// so no two identities share their bytes.
pub(crate) fn identity_bytes(issuer: &str, subject: &str) -> Vec<u8> {
    let mut bytes = Vec::with_capacity(4 + issuer.len() + subject.len());
    let len = u32::try_from(issuer.len()).unwrap_or(u32::MAX);
    bytes.extend_from_slice(&len.to_be_bytes());
    bytes.extend_from_slice(issuer.as_bytes());
    bytes.extend_from_slice(subject.as_bytes());
    bytes
}

/// The issuer and the subject [`identity_bytes`] wrote.
pub(crate) fn identity_of(bytes: &[u8]) -> Option<(String, String)> {
    let (len, rest) = bytes.split_first_chunk::<4>()?;
    let len = usize::try_from(u32::from_be_bytes(*len)).ok()?;
    if len > rest.len() {
        return None;
    }
    let (issuer, subject) = rest.split_at(len);
    Some((
        String::from_utf8(issuer.to_vec()).ok()?,
        String::from_utf8(subject.to_vec()).ok()?,
    ))
}

/// The row key of a membership in a sealed column's context: the user,
/// then the tenant; the user alone for an operator's.
pub(crate) fn membership_key(user: UserId, tenant: Option<TenantId>) -> Vec<u8> {
    let mut key = user.0.to_vec();
    if let Some(tenant) = tenant {
        key.extend_from_slice(&tenant.0);
    }
    key
}

/// The row key of a setting: the tenant, then the setting's name.
pub(crate) fn setting_key(tenant: TenantId, name: &str) -> Vec<u8> {
    let mut key = tenant.0.to_vec();
    key.extend_from_slice(name.as_bytes());
    key
}

const TENANT_COLUMNS: &str = "id, name_sealed, retention_sealed, metadata_sealed, \
     role_cap_sealed, created_at, tenant_key_id, wrapped_tenant_key, \
     pending_wrapped_tenant_key, key_since";
const KEY_COLUMNS: &str = "id, tenant, token_hash, scopes_sealed, label_sealed, created_at, \
     revoked_at, expires_at, last_used_at";
const USER_COLUMNS: &str = "id, label_sealed, identity_sealed, created_at, deactivated_at";
const MEMBERSHIP_COLUMNS: &str = "user_id, tenant, roles_sealed, granted_at";
const SESSION_COLUMNS: &str = "id_hash, user_id, tenant, scopes_sealed, created_at, \
     last_seen_at, idle_expires_at, expires_at, idle_seconds, amr_sealed, csrf_hash";
/// The membership of a user in a tenant, or as an operator for no tenant.
const MEMBERSHIP_IS: &str = "user_id = $1 AND tenant IS NOT DISTINCT FROM $2";

/// A column of a registry table's primary key, as a rotation of the
/// master key reads it and finds the row by it again.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum KeyColumn {
    /// Bytes, never NULL.
    Bytes(&'static str),
    /// Bytes, or NULL.
    BytesOrNone(&'static str),
    /// Text, never NULL.
    Text(&'static str),
}

/// A column a rotation of the master key makes again.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Column {
    /// A sealed value, or NULL.
    Sealed(&'static str),
    /// A sealed value, or NULL, and the column of its HMAC.
    SealedWithMac {
        /// The sealed copy.
        sealed: &'static str,
        /// The HMAC the value is found by.
        mac: &'static str,
    },
    /// A tenant key wrapped under the master key, or NULL.
    TenantKey(&'static str),
}

/// A table of the registry whose columns a rotation makes again.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct Remade {
    /// The table, in the schema `vcon`.
    pub(super) table: &'static str,
    /// The columns of its primary key, in the order their bytes make the
    /// row's key in a sealed column's context.
    pub(super) key: &'static [KeyColumn],
    /// The columns made again.
    pub(super) columns: &'static [Column],
}

/// One value of a row's primary key.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) enum KeyValue {
    /// Bytes, or NULL.
    Bytes(Option<Vec<u8>>),
    /// Text.
    Text(String),
}

/// Every table a rotation of the master key makes again, with every
/// column of it that is sealed, is an HMAC or holds a wrapped key. The
/// registry's log is not here: no record of it is written again, and its
/// keys are sealed again by the function a tenant's log has.
pub(super) const REMADE: [Remade; 6] = [
    Remade {
        table: "tenant",
        key: &[KeyColumn::Bytes("id")],
        columns: &[
            Column::SealedWithMac {
                sealed: "name_sealed",
                mac: "name_mac",
            },
            Column::Sealed("retention_sealed"),
            Column::Sealed("metadata_sealed"),
            Column::Sealed("role_cap_sealed"),
            Column::TenantKey("wrapped_tenant_key"),
            Column::TenantKey("pending_wrapped_tenant_key"),
        ],
    },
    Remade {
        table: "api_key",
        key: &[KeyColumn::Bytes("id")],
        columns: &[
            Column::Sealed("scopes_sealed"),
            Column::Sealed("label_sealed"),
        ],
    },
    Remade {
        table: "user_account",
        key: &[KeyColumn::Bytes("id")],
        columns: &[
            Column::Sealed("label_sealed"),
            Column::SealedWithMac {
                sealed: "identity_sealed",
                mac: "identity_mac",
            },
        ],
    },
    Remade {
        table: "membership",
        key: &[
            KeyColumn::Bytes("user_id"),
            KeyColumn::BytesOrNone("tenant"),
        ],
        columns: &[Column::Sealed("roles_sealed")],
    },
    Remade {
        table: "tenant_setting",
        key: &[KeyColumn::Bytes("tenant"), KeyColumn::Text("name")],
        columns: &[Column::Sealed("value_sealed")],
    },
    Remade {
        table: "session",
        key: &[KeyColumn::Bytes("id_hash")],
        columns: &[
            Column::Sealed("scopes_sealed"),
            Column::Sealed("amr_sealed"),
        ],
    },
];

/// The row's key in a sealed column's context: the bytes of each value
/// of its primary key, one after the other. A NULL adds none, and a text
/// its UTF-8. [`membership_key`] and [`setting_key`] give the same bytes
/// for the rows they name.
pub(super) fn row_key(values: &[KeyValue]) -> Vec<u8> {
    let mut key = Vec::new();
    for value in values {
        match value {
            KeyValue::Bytes(Some(bytes)) => key.extend_from_slice(bytes),
            KeyValue::Bytes(None) => {}
            KeyValue::Text(text) => key.extend_from_slice(text.as_bytes()),
        }
    }
    key
}

impl KeyColumn {
    fn name(self) -> &'static str {
        match self {
            KeyColumn::Bytes(name) | KeyColumn::BytesOrNone(name) | KeyColumn::Text(name) => name,
        }
    }
}

impl Remade {
    /// The columns a rotation writes, in the order it reads them: an
    /// HMAC before the sealed copy it is made from.
    fn written(&self) -> Vec<&'static str> {
        let mut names = Vec::new();
        for column in self.columns {
            match *column {
                Column::Sealed(name) | Column::TenantKey(name) => names.push(name),
                Column::SealedWithMac { sealed, mac } => names.extend([mac, sealed]),
            }
        }
        names
    }

    /// The names of the key's columns, then of the columns written.
    fn names(&self) -> Vec<&'static str> {
        let mut names: Vec<&str> = self.key.iter().map(|column| column.name()).collect();
        names.extend(self.written());
        names
    }

    /// The statement of the cursor that reads the table's rows.
    pub(super) fn fetch_text(&self) -> String {
        format!(
            "SELECT {} FROM vcon.{}",
            self.names().join(", "),
            self.table
        )
    }

    /// The statement that writes the rows of one batch: one array for
    /// each column [`Remade::fetch_text`] reads, and each row found by
    /// its key.
    pub(super) fn update_text(&self) -> String {
        let written = self.written();
        let set: Vec<String> = written.iter().map(|c| format!("{c} = n.{c}")).collect();
        let kinds = self
            .key
            .iter()
            .map(|column| match column {
                KeyColumn::Text(_) => "text",
                KeyColumn::Bytes(_) | KeyColumn::BytesOrNone(_) => "bytea",
            })
            .chain(written.iter().map(|_| "bytea"));
        let arrays: Vec<String> = kinds
            .enumerate()
            .map(|(at, kind)| format!("${}::{kind}[]", at + 1))
            .collect();
        let found: Vec<String> = self
            .key
            .iter()
            .map(|column| match *column {
                KeyColumn::BytesOrNone(c) => format!("t.{c} IS NOT DISTINCT FROM n.{c}"),
                KeyColumn::Bytes(c) | KeyColumn::Text(c) => format!("t.{c} = n.{c}"),
            })
            .collect();
        format!(
            "UPDATE vcon.{} t SET {} FROM unnest({}) AS n({}) WHERE {}",
            self.table,
            set.join(", "),
            arrays.join(", "),
            self.names().join(", "),
            found.join(" AND ")
        )
    }
}

/// The values of one column of a primary key, for the rows of a batch.
enum KeyArray {
    Bytes(Vec<Option<Vec<u8>>>),
    Text(Vec<String>),
}

impl KeyArray {
    fn of(column: KeyColumn) -> KeyArray {
        match column {
            KeyColumn::Text(_) => KeyArray::Text(Vec::new()),
            KeyColumn::Bytes(_) | KeyColumn::BytesOrNone(_) => KeyArray::Bytes(Vec::new()),
        }
    }

    /// Adds a row's value; false when it is not of the column's kind.
    fn push(&mut self, value: KeyValue) -> bool {
        match (self, value) {
            (KeyArray::Bytes(all), KeyValue::Bytes(bytes)) => all.push(bytes),
            (KeyArray::Text(all), KeyValue::Text(text)) => all.push(text),
            _ => return false,
        }
        true
    }

    fn param(&self) -> (&(dyn tokio_postgres::types::ToSql + Sync), Type) {
        match self {
            KeyArray::Bytes(all) => (all, Type::BYTEA_ARRAY),
            KeyArray::Text(all) => (all, Type::TEXT_ARRAY),
        }
    }
}

impl PgRegistry {
    /// The registry in `database`, its columns sealed under the keys
    /// derived from `catalog_key`, the master key's catalog key.
    ///
    /// The database names the key the registry is under, in the one row
    /// of `vcon.registry_key`. The first open of a database writes that
    /// row and makes the key of the registry's audit log, with the
    /// registry's lock held, so of several first opens at once one
    /// writes; every later open compares the id and opens the log's keys.
    ///
    /// # Errors
    ///
    /// [`TenantError::WrongKey`] when the database names another key, or
    /// names none and holds a row that does not open under this one.
    /// [`TenantError::Io`] when the database cannot be read or written,
    /// when a key's page cannot be locked, and when the newest key of the
    /// audit log is missing or does not open.
    pub(crate) fn open(
        database: Arc<Database>,
        catalog_key: &Key,
    ) -> Result<PgRegistry, TenantError> {
        let mut registry = PgRegistry {
            database,
            keys: ColumnKeys::derive(catalog_key).map_err(io)?,
            audit: Vec::new(),
            #[cfg(test)]
            fault: std::sync::Mutex::new(None),
        };
        let ours = catalog_key.id().0;
        let audit = {
            let read = registry.begin(Begin::Read)?;
            match read.named()? {
                Some(held) if held == ours => read.log_keys()?,
                Some(_) => return Err(TenantError::WrongKey),
                None => {
                    // The read ends before the write begins: one
                    // connection at a time.
                    drop(read);
                    registry.claim(&ours)?
                }
            }
        };
        if audit.is_empty() {
            // A log with no key takes no record.
            return Err(TenantError::Io);
        }
        registry.audit = audit;
        Ok(registry)
    }

    /// The first open: names the key `ours` in the database and gives the
    /// audit log its first key, in one write, which holds the registry's
    /// lock. A row another open wrote while this one waited is compared,
    /// not written again. Gives the keys of the log.
    fn claim(&self, ours: &[u8]) -> Result<Vec<AuditKey>, TenantError> {
        let tx = self.begin(Begin::Write)?;
        match tx.named()? {
            Some(held) if held == ours => return tx.log_keys(),
            Some(_) => return Err(TenantError::WrongKey),
            None => {}
        }
        // No id is named. Rows that are there were sealed by a registry
        // of before schema version 12, under its key: this key must open
        // them, or it is not that key.
        if !tx.opens_what_is_there()? {
            return Err(TenantError::WrongKey);
        }
        let mut audit = tx.log_keys().map_err(|_| TenantError::WrongKey)?;
        if audit.is_empty() {
            let (key, wrapped) = new_audit_key(REGISTRY, &self.keys).map_err(io)?;
            tx.0.execute(
                "INSERT INTO vcon.admin_audit_key (key_id, ordinal, wrapped) VALUES ($1, 1, $2)",
                &[(&audit_key_id(&key), Type::BYTEA), (&wrapped, Type::BYTEA)],
            )
            .map_err(io)?;
            audit.push(key);
        }
        tx.0.execute(
            "INSERT INTO vcon.registry_key (catalog_key_id) VALUES ($1)",
            &[(&ours, Type::BYTEA)],
        )
        .map_err(io)?;
        Box::new(tx).commit().map_err(io)?;
        Ok(audit)
    }

    /// Moves the registry in `database` from the master key `old` to
    /// `new`, in one transaction that holds the registry's lock: every
    /// tenant key, a pending one too, is wrapped again under `new`; every
    /// sealed column and every HMAC of the tables in [`REMADE`] is made
    /// again with the keys of `new`'s catalog key; each key of the
    /// registry's audit log is sealed again; the rows of the master
    /// keys' use give way to one for `new`, which counts the tenant keys
    /// wrapped and is in use since `now`; the database then names `new`'s
    /// catalog key, and the rotation is recorded in the registry's log.
    /// It reads and writes at most `batch` rows of a table in one
    /// statement.
    ///
    /// No audit record is changed: a record's target is sealed under the
    /// log's audit key, which stays the same. A registry that is under
    /// `new` already is left as it is, so a rotation that was made can be
    /// asked for again. A database that names no key is first opened
    /// under `old`, as [`PgRegistry::open`] opens it. Every server is
    /// stopped for a rotation: an open registry holds the keys it was
    /// opened with.
    ///
    /// # Errors
    ///
    /// [`TenantError::WrongKey`] when the registry is under neither key.
    /// [`TenantError::Io`] when a value does not open under `old`, the
    /// database cannot be read or written, or `batch` is not positive,
    /// and [`TenantError::AuditFailed`] when the record cannot be
    /// written. After an error nothing is changed, unless the commit got
    /// no answer: a second run then finds which key the registry is
    /// under.
    pub(crate) fn rekey_master(
        database: &Arc<Database>,
        old: &Key,
        new: &Key,
        now: OffsetDateTime,
        batch: i64,
    ) -> Result<(), TenantError> {
        if batch < 1 {
            return Err(TenantError::Io);
        }
        let old_catalog = column::catalog_key(old).map_err(io)?;
        match PgRegistry::open(database.clone(), &old_catalog) {
            Ok(registry) => registry.rotate(old, new, now, batch),
            Err(TenantError::WrongKey) => {
                // Under the new key, a rotation was made already.
                let new_catalog = column::catalog_key(new).map_err(io)?;
                PgRegistry::open(database.clone(), &new_catalog).map(drop)
            }
            Err(e) => Err(e),
        }
    }

    /// The rotation [`PgRegistry::rekey_master`] describes, of this
    /// registry, which was opened under `old`'s catalog key. The id the
    /// database names is compared again with the registry's lock held:
    /// another run may have made the rotation since the open.
    pub(super) fn rotate(
        &self,
        old: &Key,
        new: &Key,
        now: OffsetDateTime,
        batch: i64,
    ) -> Result<(), TenantError> {
        if batch < 1 {
            return Err(TenantError::Io);
        }
        let old_id = column::catalog_key(old).map_err(io)?.id().0;
        let new_catalog = column::catalog_key(new).map_err(io)?;
        let new_id = new_catalog.id().0;
        let to = ColumnKeys::derive(&new_catalog).map_err(io)?;
        let tx = self.begin(Begin::Write)?;
        match tx.named()? {
            Some(held) if held == new_id => return Ok(()),
            Some(held) if held == old_id => {}
            _ => return Err(TenantError::WrongKey),
        }
        reseal_audit_keys(&tx.0, &to).map_err(io)?;
        let mut wraps = 0_i64;
        for table in &REMADE {
            wraps += tx.remake(table, &to, old, new, batch)?;
        }
        tx.run("DELETE FROM vcon.master_key_use", &[])?;
        tx.run(
            "INSERT INTO vcon.master_key_use (master_key_id, wraps, since) VALUES ($1, $2, $3)",
            &[
                (&new.id().0.as_slice(), Type::BYTEA),
                (&wraps, Type::INT8),
                (&nanos(now), Type::INT8),
            ],
        )?;
        let named =
            tx.0.execute(
                "UPDATE vcon.registry_key SET catalog_key_id = $1 WHERE catalog_key_id = $2",
                &[
                    (&new_id.as_slice(), Type::BYTEA),
                    (&old_id.as_slice(), Type::BYTEA),
                ],
            )
            .map_err(io)?;
        all_changed(named, 1).map_err(io)?;
        let entry = Entry {
            actor: Actor::Cli,
            action: Action::RekeyMaster,
            target: None,
            version: None,
            status: 200,
        };
        tx.0.record(&entry, now)
            .map_err(|_| TenantError::AuditFailed)?;
        tx.0.finish().map_err(io)
    }

    fn begin(&self, how: Begin) -> Result<PgRegistryTx<'_>, TenantError> {
        let lease = self.database.lease().map_err(io)?;
        let tx = PgTx::begin(&self.database, lease, REGISTRY, Some(&self.keys), how)
            .with_audit(&self.audit);
        #[cfg(test)]
        let tx = tx.with_fault(&self.fault);
        Ok(PgRegistryTx(tx))
    }
}

impl Registry for PgRegistry {
    fn immediate(&self) -> Result<Box<dyn RegistryTx + '_>, TenantError> {
        Ok(Box::new(self.begin(Begin::Write)?))
    }

    fn snapshot(&self) -> Result<Box<dyn RegistryTx + '_>, TenantError> {
        Ok(Box::new(self.begin(Begin::Read)?))
    }

    fn export(&self, after: i64, out: &mut dyn Write) -> Result<(), AuditError> {
        let tx = self
            .begin(Begin::Read)
            .map_err(|_| AuditError::Database("the database could not be reached".to_owned()))?;
        tx.0.audit_lines(after, None, out)
    }

    /// Not for a registry in PostgreSQL: it is no file, and its backup is
    /// the database's own (`pg_dump` of the catalog database), which the
    /// how-to covers. Nothing is written at `to`.
    fn backup_into(&self, _to: &Path) -> Result<Vec<TenantId>, TenantError> {
        Err(TenantError::NoFileBackup)
    }

    #[cfg(test)]
    fn fail_commit(&self, nth: usize, how: CommitError) {
        *self.fault.lock().expect("fault") = Some((nth, how));
    }

    #[cfg(test)]
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

/// A transaction on the registry, rolled back when dropped without a
/// commit.
struct PgRegistryTx<'a>(PgTx<'a>);

impl PgRegistryTx<'_> {
    /// The id of the catalog key the database names for the registry, or
    /// None when it names none yet.
    fn named(&self) -> Result<Option<Vec<u8>>, TenantError> {
        let rows = self
            .0
            .query("SELECT catalog_key_id FROM vcon.registry_key", &[])
            .map_err(io)?;
        rows.first().map(|row| col(row, 0)).transpose()
    }

    /// The keys of the registry's audit log that open under the
    /// registry's keys, oldest first; none when the log has no key.
    ///
    /// # Errors
    ///
    /// [`TenantError::Io`] when the newest does not open.
    fn log_keys(&self) -> Result<Vec<AuditKey>, TenantError> {
        let rows = self
            .0
            .query(
                "SELECT key_id, wrapped FROM vcon.admin_audit_key ORDER BY ordinal",
                &[],
            )
            .map_err(io)?;
        open_audit_keys(REGISTRY, self.0.keys().map_err(io)?, &rows).map_err(io)
    }

    /// Whether the registry's keys open the rows that are there: one
    /// tenant, one API key and one user, each the first by its id. Every
    /// other sealed row of the registry belongs to one of those three
    /// tables' rows (a setting to a tenant, a membership and a session to
    /// a user), and all were sealed under one key.
    fn opens_what_is_there(&self) -> Result<bool, TenantError> {
        let first = |columns: &str, table: &str| {
            self.0
                .query(
                    &format!("SELECT {columns} FROM vcon.{table} ORDER BY id LIMIT 1"),
                    &[],
                )
                .map_err(io)
        };
        let tenant = first(TENANT_COLUMNS, "tenant")?;
        let key = first(KEY_COLUMNS, "api_key")?;
        let user = first(USER_COLUMNS, "user_account")?;
        Ok(tenant
            .first()
            .is_none_or(|row| self.tenant_row(row).is_ok())
            && key.first().is_none_or(|row| self.key_row(row).is_ok())
            && user.first().is_none_or(|row| self.user_row(row).is_ok()))
    }

    fn seal(
        &self,
        table: &str,
        column: &str,
        row: &[u8],
        text: &[u8],
    ) -> Result<Vec<u8>, TenantError> {
        self.0.seal(table, column, row, text).map_err(io)
    }

    fn seal_some(
        &self,
        table: &str,
        column: &str,
        row: &[u8],
        text: Option<&str>,
    ) -> Result<Option<Vec<u8>>, TenantError> {
        text.map(|text| self.seal(table, column, row, text.as_bytes()))
            .transpose()
    }

    fn text(
        &self,
        table: &str,
        column: &str,
        row: &[u8],
        sealed: &[u8],
    ) -> Result<String, TenantError> {
        let bytes = self.0.unseal(table, column, row, sealed).map_err(io)?;
        String::from_utf8(bytes).map_err(io)
    }

    fn text_some(
        &self,
        table: &str,
        column: &str,
        row: &[u8],
        sealed: Option<Vec<u8>>,
    ) -> Result<Option<String>, TenantError> {
        sealed
            .map(|sealed| self.text(table, column, row, &sealed))
            .transpose()
    }

    fn name_mac(&self, name: &str) -> Result<[u8; 32], TenantError> {
        Ok(column::mac(self.0.keys().map_err(io)?, name.as_bytes()))
    }

    fn tenant_row(&self, row: &Row) -> Result<TenantRow, TenantError> {
        let id: Vec<u8> = col(row, 0)?;
        let open = |name: &str, at: usize| -> Result<Option<String>, TenantError> {
            self.text_some("tenant", name, &id, col(row, at)?)
        };
        Ok(TenantRow {
            id: TenantId(fixed(&id)?),
            name: self.text("tenant", "name_sealed", &id, &col::<Vec<u8>>(row, 1)?)?,
            retention: open("retention_sealed", 2)?,
            metadata: open("metadata_sealed", 3)?,
            role_cap: open("role_cap_sealed", 4)?,
            created_at: col(row, 5)?,
            tenant_key_id: KeyId(fixed(&col::<Vec<u8>>(row, 6)?)?),
            wrapped: col(row, 7)?,
            pending: col(row, 8)?,
            key_since: col(row, 9)?,
        })
    }

    fn key_row(&self, row: &Row) -> Result<KeyRow, TenantError> {
        let id: Vec<u8> = col(row, 0)?;
        let tenant: Option<Vec<u8>> = col(row, 1)?;
        Ok(KeyRow {
            id: KeyId(fixed(&id)?),
            tenant: tenant.map(|t| fixed(&t).map(TenantId)).transpose()?,
            token_hash: col(row, 2)?,
            scopes: self.text("api_key", "scopes_sealed", &id, &col::<Vec<u8>>(row, 3)?)?,
            label: self.text("api_key", "label_sealed", &id, &col::<Vec<u8>>(row, 4)?)?,
            created_at: col(row, 5)?,
            revoked_at: col(row, 6)?,
            expires_at: col(row, 7)?,
            last_used_at: col(row, 8)?,
        })
    }

    fn user_row(&self, row: &Row) -> Result<UserRow, TenantError> {
        let id: Vec<u8> = col(row, 0)?;
        let identity: Option<Vec<u8>> = col(row, 2)?;
        let identity = identity
            .map(|sealed| {
                let bytes = self
                    .0
                    .unseal("user_account", "identity_sealed", &id, &sealed)
                    .map_err(io)?;
                identity_of(&bytes).ok_or(TenantError::Io)
            })
            .transpose()?;
        Ok(UserRow {
            id: UserId(fixed(&id)?),
            label: self.text(
                "user_account",
                "label_sealed",
                &id,
                &col::<Vec<u8>>(row, 1)?,
            )?,
            identity,
            created_at: col(row, 3)?,
            deactivated_at: col(row, 4)?,
        })
    }

    fn membership_row(&self, row: &Row) -> Result<MembershipRow, TenantError> {
        let user = UserId(fixed(&col::<Vec<u8>>(row, 0)?)?);
        let tenant: Option<Vec<u8>> = col(row, 1)?;
        let tenant = tenant.map(|t| fixed(&t).map(TenantId)).transpose()?;
        let key = membership_key(user, tenant);
        Ok(MembershipRow {
            user,
            tenant,
            roles: self.text("membership", "roles_sealed", &key, &col::<Vec<u8>>(row, 2)?)?,
            granted_at: col(row, 3)?,
        })
    }

    fn session_row(&self, row: &Row) -> Result<SessionRow, TenantError> {
        let id_hash: Vec<u8> = col(row, 0)?;
        let tenant: Option<Vec<u8>> = col(row, 2)?;
        Ok(SessionRow {
            user: UserId(fixed(&col::<Vec<u8>>(row, 1)?)?),
            tenant: tenant.map(|t| fixed(&t).map(TenantId)).transpose()?,
            scopes: self.text(
                "session",
                "scopes_sealed",
                &id_hash,
                &col::<Vec<u8>>(row, 3)?,
            )?,
            created_at: col(row, 4)?,
            last_seen_at: col(row, 5)?,
            idle_expires_at: col(row, 6)?,
            expires_at: col(row, 7)?,
            idle_seconds: col(row, 8)?,
            amr: self.text("session", "amr_sealed", &id_hash, &col::<Vec<u8>>(row, 9)?)?,
            csrf_hash: col(row, 10)?,
            id_hash,
        })
    }

    /// Sets one sealed column of a tenant, or with None empties it; false
    /// if no tenant has the id.
    fn set_sealed(
        &self,
        id: TenantId,
        column: &str,
        value: Option<&str>,
    ) -> Result<bool, TenantError> {
        let sealed = self.seal_some("tenant", column, &id.0, value)?;
        let changed = self
            .0
            .execute(
                &format!("UPDATE vcon.tenant SET {column} = $2 WHERE id = $1"),
                &[(&id.0.as_slice(), Type::BYTEA), (&sealed, Type::BYTEA)],
            )
            .map_err(io)?;
        Ok(changed != 0)
    }

    /// Opens `sealed`, a value of `table.column` in the row `row`, and
    /// seals it again with the keys `to`: the plain value and its new
    /// sealed copy.
    fn sealed_again(
        &self,
        table: &str,
        column: &str,
        row: &[u8],
        sealed: &[u8],
        to: &ColumnKeys,
    ) -> Result<(Vec<u8>, Vec<u8>), TenantError> {
        let plain = self.0.unseal(table, column, row, sealed).map_err(io)?;
        let ctx = column::ColumnContext {
            tenant: REGISTRY,
            table,
            column,
            row,
        };
        let again = column::seal_column(to, &ctx, &plain).map_err(io)?;
        Ok((plain, again))
    }

    /// Makes every column of `spec` again in every row of its table:
    /// a sealed value with the keys `to`, an HMAC from the value it is
    /// of, and a tenant key from the master key `old` to `new`. The rows
    /// are read through a cursor, `batch` at a time, and each batch is
    /// written in one statement. Gives the number of tenant keys wrapped.
    fn remake(
        &self,
        spec: &Remade,
        to: &ColumnKeys,
        old: &Key,
        new: &Key,
        batch: i64,
    ) -> Result<i64, TenantError> {
        let tx = &self.0;
        tx.batch(&format!(
            "DECLARE rekey_rows NO SCROLL CURSOR FOR {}",
            spec.fetch_text()
        ))
        .map_err(io)?;
        let fetch = format!("FETCH {batch} FROM rekey_rows");
        let update = spec.update_text();
        let columns = spec.written().len();
        let mut wraps = 0_i64;
        loop {
            let rows = tx.query(&fetch, &[]).map_err(io)?;
            if rows.is_empty() {
                break;
            }
            let mut keys: Vec<KeyArray> = spec.key.iter().map(|c| KeyArray::of(*c)).collect();
            let mut made: Vec<Vec<Option<Vec<u8>>>> = vec![Vec::new(); columns];
            for row in &rows {
                let mut values = Vec::with_capacity(spec.key.len());
                for (at, column) in spec.key.iter().enumerate() {
                    values.push(match column {
                        KeyColumn::Text(_) => KeyValue::Text(col(row, at)?),
                        KeyColumn::Bytes(_) | KeyColumn::BytesOrNone(_) => {
                            KeyValue::Bytes(col(row, at)?)
                        }
                    });
                }
                let key = row_key(&values);
                // Where the column is in the row read, and in `made`.
                let mut at = spec.key.len();
                let mut out = 0;
                for column in spec.columns {
                    match *column {
                        Column::Sealed(name) => {
                            let sealed: Option<Vec<u8>> = col(row, at)?;
                            let again = sealed
                                .map(|s| self.sealed_again(spec.table, name, &key, &s, to))
                                .transpose()?;
                            made[out].push(again.map(|(_, sealed)| sealed));
                            at += 1;
                            out += 1;
                        }
                        Column::SealedWithMac { sealed: name, .. } => {
                            let sealed: Option<Vec<u8>> = col(row, at + 1)?;
                            let again = sealed
                                .map(|s| self.sealed_again(spec.table, name, &key, &s, to))
                                .transpose()?;
                            let (mac, sealed) = match again {
                                Some((plain, sealed)) => {
                                    (Some(column::mac(to, &plain).to_vec()), Some(sealed))
                                }
                                None => (None, None),
                            };
                            made[out].push(mac);
                            made[out + 1].push(sealed);
                            at += 2;
                            out += 2;
                        }
                        Column::TenantKey(_) => {
                            let wrapped: Option<Vec<u8>> = col(row, at)?;
                            let again = wrapped
                                .map(|wrapped| {
                                    let Some(KeyValue::Bytes(Some(id))) = values.first() else {
                                        return Err(TenantError::Io);
                                    };
                                    let ctx = column::Context::Tenant {
                                        tenant: TenantId(fixed(id)?),
                                    };
                                    column::rewrap(old, new, &ctx, &wrapped).map_err(io)
                                })
                                .transpose()?;
                            wraps += i64::from(again.is_some());
                            made[out].push(again);
                            at += 1;
                            out += 1;
                        }
                    }
                }
                for (array, value) in keys.iter_mut().zip(values) {
                    if !array.push(value) {
                        return Err(TenantError::Io);
                    }
                }
            }
            let mut params: Vec<_> = keys.iter().map(KeyArray::param).collect();
            for values in &made {
                params.push((values, Type::BYTEA_ARRAY));
            }
            let changed = tx.execute(&update, &params).map_err(io)?;
            all_changed(changed, rows.len()).map_err(io)?;
        }
        tx.batch("CLOSE rekey_rows").map_err(io)?;
        Ok(wraps)
    }

    /// Runs a write whose count of rows the caller does not need.
    fn run(
        &self,
        sql: &str,
        params: &[(&(dyn tokio_postgres::types::ToSql + Sync), Type)],
    ) -> Result<(), TenantError> {
        self.0.execute(sql, params).map(drop).map_err(io)
    }
}

impl RegistryTx for PgRegistryTx<'_> {
    fn tenant(&mut self, id: TenantId) -> Result<Option<TenantRow>, TenantError> {
        let rows = self
            .0
            .query(
                &format!("SELECT {TENANT_COLUMNS} FROM vcon.tenant WHERE id = $1"),
                &[(&id.0.as_slice(), Type::BYTEA)],
            )
            .map_err(io)?;
        rows.first().map(|row| self.tenant_row(row)).transpose()
    }

    /// Every tenant, by name. The names are sealed, so the order is made
    /// here, by their bytes, as the local registry's is.
    fn tenants(&mut self) -> Result<Vec<TenantRow>, TenantError> {
        let rows = self
            .0
            .query(&format!("SELECT {TENANT_COLUMNS} FROM vcon.tenant"), &[])
            .map_err(io)?;
        let mut tenants = rows
            .iter()
            .map(|row| self.tenant_row(row))
            .collect::<Result<Vec<_>, _>>()?;
        tenants.sort_by(|a, b| a.name.as_bytes().cmp(b.name.as_bytes()));
        Ok(tenants)
    }

    fn name_taken(&mut self, name: &str) -> Result<bool, TenantError> {
        let mac = self.name_mac(name)?;
        self.0
            .exists(
                "SELECT 1 FROM vcon.tenant WHERE name_mac = $1",
                &[(&mac.as_slice(), Type::BYTEA)],
            )
            .map_err(io)
    }

    fn insert_tenant(&mut self, row: &TenantRow) -> Result<(), TenantError> {
        let id = row.id.0.as_slice();
        let mac = self.name_mac(&row.name)?;
        let name = self.seal("tenant", "name_sealed", id, row.name.as_bytes())?;
        let retention =
            self.seal_some("tenant", "retention_sealed", id, row.retention.as_deref())?;
        let metadata = self.seal_some("tenant", "metadata_sealed", id, row.metadata.as_deref())?;
        let role_cap = self.seal_some("tenant", "role_cap_sealed", id, row.role_cap.as_deref())?;
        self.run(
            "INSERT INTO vcon.tenant (id, name_mac, name_sealed, retention_sealed, \
             metadata_sealed, role_cap_sealed, created_at, tenant_key_id, wrapped_tenant_key, \
             pending_wrapped_tenant_key, key_since)
             VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)",
            &[
                (&id, Type::BYTEA),
                (&mac.as_slice(), Type::BYTEA),
                (&name, Type::BYTEA),
                (&retention, Type::BYTEA),
                (&metadata, Type::BYTEA),
                (&role_cap, Type::BYTEA),
                (&row.created_at, Type::INT8),
                (&row.tenant_key_id.0.as_slice(), Type::BYTEA),
                (&row.wrapped, Type::BYTEA),
                (&row.pending, Type::BYTEA),
                (&row.key_since, Type::INT8),
            ],
        )
    }

    fn set_retention(
        &mut self,
        id: TenantId,
        retention: Option<&str>,
    ) -> Result<bool, TenantError> {
        self.set_sealed(id, "retention_sealed", retention)
    }

    fn set_metadata(&mut self, id: TenantId, level: Option<&str>) -> Result<bool, TenantError> {
        self.set_sealed(id, "metadata_sealed", level)
    }

    fn set_role_cap(&mut self, id: TenantId, cap: Option<&str>) -> Result<bool, TenantError> {
        self.set_sealed(id, "role_cap_sealed", cap)
    }

    fn delete_tenant(&mut self, id: TenantId, at: i64) -> Result<(), TenantError> {
        self.run(
            "DELETE FROM vcon.tenant WHERE id = $1",
            &[(&id.0.as_slice(), Type::BYTEA)],
        )?;
        self.run(
            "UPDATE vcon.api_key SET revoked_at = $2 WHERE tenant = $1 AND revoked_at IS NULL",
            &[(&id.0.as_slice(), Type::BYTEA), (&at, Type::INT8)],
        )
    }

    fn set_pending(&mut self, id: TenantId, pending: &[u8]) -> Result<(), TenantError> {
        self.run(
            "UPDATE vcon.tenant SET pending_wrapped_tenant_key = $1 WHERE id = $2",
            &[(&pending, Type::BYTEA), (&id.0.as_slice(), Type::BYTEA)],
        )
    }

    fn promote_pending(&mut self, id: TenantId, key: KeyId, at: i64) -> Result<(), TenantError> {
        self.run(
            "UPDATE vcon.tenant SET tenant_key_id = $1,
                 wrapped_tenant_key = pending_wrapped_tenant_key,
                 pending_wrapped_tenant_key = NULL, key_since = $2
             WHERE id = $3",
            &[
                (&key.0.as_slice(), Type::BYTEA),
                (&at, Type::INT8),
                (&id.0.as_slice(), Type::BYTEA),
            ],
        )
    }

    fn master_wraps(&mut self, key: KeyId) -> Result<u64, TenantError> {
        let rows = self
            .0
            .query(
                "SELECT wraps FROM vcon.master_key_use WHERE master_key_id = $1",
                &[(&key.0.as_slice(), Type::BYTEA)],
            )
            .map_err(io)?;
        let wraps: Option<i64> = rows.first().map(|row| col(row, 0)).transpose()?;
        u64::try_from(wraps.unwrap_or(0)).map_err(io)
    }

    fn add_master_wrap(&mut self, key: KeyId, at: i64) -> Result<(), TenantError> {
        self.run(
            "INSERT INTO vcon.master_key_use AS u (master_key_id, wraps, since)
             VALUES ($1, 1, $2)
             ON CONFLICT (master_key_id) DO UPDATE SET wraps = u.wraps + 1",
            &[(&key.0.as_slice(), Type::BYTEA), (&at, Type::INT8)],
        )
    }

    fn master_since(&mut self, key: KeyId) -> Result<Option<i64>, TenantError> {
        let rows = self
            .0
            .query(
                "SELECT since FROM vcon.master_key_use WHERE master_key_id = $1",
                &[(&key.0.as_slice(), Type::BYTEA)],
            )
            .map_err(io)?;
        rows.first().map(|row| col(row, 0)).transpose()
    }

    fn key(&mut self, id: KeyId) -> Result<Option<KeyRow>, TenantError> {
        let rows = self
            .0
            .query(
                &format!("SELECT {KEY_COLUMNS} FROM vcon.api_key WHERE id = $1"),
                &[(&id.0.as_slice(), Type::BYTEA)],
            )
            .map_err(io)?;
        rows.first().map(|row| self.key_row(row)).transpose()
    }

    fn keys(&mut self, tenant: Option<TenantId>) -> Result<Vec<KeyRow>, TenantError> {
        let tenant = tenant.map(|t| t.0.to_vec());
        let rows = self
            .0
            .query(
                &format!(
                    "SELECT {KEY_COLUMNS} FROM vcon.api_key
                     WHERE $1 IS NULL OR tenant = $1 ORDER BY created_at, id"
                ),
                &[(&tenant, Type::BYTEA)],
            )
            .map_err(io)?;
        rows.iter().map(|row| self.key_row(row)).collect()
    }

    fn insert_key(&mut self, row: &KeyRow) -> Result<(), TenantError> {
        let id = row.id.0.as_slice();
        let tenant = row.tenant.map(|t| t.0.to_vec());
        let scopes = self.seal("api_key", "scopes_sealed", id, row.scopes.as_bytes())?;
        let label = self.seal("api_key", "label_sealed", id, row.label.as_bytes())?;
        self.run(
            &format!(
                "INSERT INTO vcon.api_key ({KEY_COLUMNS})
                 VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)"
            ),
            &[
                (&id, Type::BYTEA),
                (&tenant, Type::BYTEA),
                (&row.token_hash, Type::BYTEA),
                (&scopes, Type::BYTEA),
                (&label, Type::BYTEA),
                (&row.created_at, Type::INT8),
                (&row.revoked_at, Type::INT8),
                (&row.expires_at, Type::INT8),
                (&row.last_used_at, Type::INT8),
            ],
        )
    }

    fn revoke_key(&mut self, id: KeyId, at: i64) -> Result<(), TenantError> {
        self.run(
            "UPDATE vcon.api_key SET revoked_at = $2 WHERE id = $1 AND revoked_at IS NULL",
            &[(&id.0.as_slice(), Type::BYTEA), (&at, Type::INT8)],
        )
    }

    fn touch_key(&mut self, id: KeyId, at: i64) -> Result<bool, TenantError> {
        let changed = self
            .0
            .execute(
                "UPDATE vcon.api_key SET last_used_at = $2
                 WHERE id = $1 AND (last_used_at IS NULL OR last_used_at < $2)",
                &[(&id.0.as_slice(), Type::BYTEA), (&at, Type::INT8)],
            )
            .map_err(io)?;
        Ok(changed > 0)
    }

    fn user(&mut self, id: UserId) -> Result<Option<UserRow>, TenantError> {
        let rows = self
            .0
            .query(
                &format!("SELECT {USER_COLUMNS} FROM vcon.user_account WHERE id = $1"),
                &[(&id.0.as_slice(), Type::BYTEA)],
            )
            .map_err(io)?;
        rows.first().map(|row| self.user_row(row)).transpose()
    }

    fn users(&mut self) -> Result<Vec<UserRow>, TenantError> {
        let rows = self
            .0
            .query(
                &format!("SELECT {USER_COLUMNS} FROM vcon.user_account ORDER BY created_at, id"),
                &[],
            )
            .map_err(io)?;
        rows.iter().map(|row| self.user_row(row)).collect()
    }

    fn identity_taken(&mut self, issuer: &str, subject: &str) -> Result<bool, TenantError> {
        let keys = self.0.keys().map_err(io)?;
        let mac = column::mac(keys, &identity_bytes(issuer, subject));
        self.0
            .exists(
                "SELECT 1 FROM vcon.user_account
                 WHERE identity_mac = $1 AND deactivated_at IS NULL",
                &[(&mac.as_slice(), Type::BYTEA)],
            )
            .map_err(io)
    }

    fn insert_user(&mut self, row: &UserRow) -> Result<(), TenantError> {
        let id = row.id.0.as_slice();
        let label = self.seal("user_account", "label_sealed", id, row.label.as_bytes())?;
        let (mac, identity) = match &row.identity {
            Some((issuer, subject)) => {
                let bytes = identity_bytes(issuer, subject);
                let mac = column::mac(self.0.keys().map_err(io)?, &bytes);
                let sealed = self.seal("user_account", "identity_sealed", id, &bytes)?;
                (Some(mac.to_vec()), Some(sealed))
            }
            None => (None, None),
        };
        self.run(
            "INSERT INTO vcon.user_account
                 (id, label_sealed, identity_mac, identity_sealed, created_at, deactivated_at)
             VALUES ($1, $2, $3, $4, $5, $6)",
            &[
                (&id, Type::BYTEA),
                (&label, Type::BYTEA),
                (&mac, Type::BYTEA),
                (&identity, Type::BYTEA),
                (&row.created_at, Type::INT8),
                (&row.deactivated_at, Type::INT8),
            ],
        )
    }

    fn deactivate_user(&mut self, id: UserId, at: i64) -> Result<(), TenantError> {
        self.run(
            "UPDATE vcon.user_account SET deactivated_at = $2
             WHERE id = $1 AND deactivated_at IS NULL",
            &[(&id.0.as_slice(), Type::BYTEA), (&at, Type::INT8)],
        )
    }

    fn membership(
        &mut self,
        user: UserId,
        tenant: Option<TenantId>,
    ) -> Result<Option<MembershipRow>, TenantError> {
        let tenant = tenant.map(|t| t.0.to_vec());
        let rows = self
            .0
            .query(
                &format!("SELECT {MEMBERSHIP_COLUMNS} FROM vcon.membership WHERE {MEMBERSHIP_IS}"),
                &[(&user.0.as_slice(), Type::BYTEA), (&tenant, Type::BYTEA)],
            )
            .map_err(io)?;
        rows.first().map(|row| self.membership_row(row)).transpose()
    }

    /// Every membership, by user, then time, then tenant, an operator's
    /// before a tenant's at one time, as the local registry lists them.
    fn memberships(&mut self) -> Result<Vec<MembershipRow>, TenantError> {
        let rows = self
            .0
            .query(
                &format!(
                    "SELECT {MEMBERSHIP_COLUMNS} FROM vcon.membership
                     ORDER BY user_id, granted_at, tenant NULLS FIRST"
                ),
                &[],
            )
            .map_err(io)?;
        rows.iter().map(|row| self.membership_row(row)).collect()
    }

    fn insert_membership(&mut self, row: &MembershipRow) -> Result<(), TenantError> {
        let key = membership_key(row.user, row.tenant);
        let tenant = row.tenant.map(|t| t.0.to_vec());
        let roles = self.seal("membership", "roles_sealed", &key, row.roles.as_bytes())?;
        self.run(
            &format!("INSERT INTO vcon.membership ({MEMBERSHIP_COLUMNS}) VALUES ($1, $2, $3, $4)"),
            &[
                (&row.user.0.as_slice(), Type::BYTEA),
                (&tenant, Type::BYTEA),
                (&roles, Type::BYTEA),
                (&row.granted_at, Type::INT8),
            ],
        )
    }

    fn set_roles(&mut self, row: &MembershipRow) -> Result<(), TenantError> {
        let key = membership_key(row.user, row.tenant);
        let tenant = row.tenant.map(|t| t.0.to_vec());
        let roles = self.seal("membership", "roles_sealed", &key, row.roles.as_bytes())?;
        self.run(
            &format!("UPDATE vcon.membership SET roles_sealed = $3 WHERE {MEMBERSHIP_IS}"),
            &[
                (&row.user.0.as_slice(), Type::BYTEA),
                (&tenant, Type::BYTEA),
                (&roles, Type::BYTEA),
            ],
        )
    }

    fn delete_membership(
        &mut self,
        user: UserId,
        tenant: Option<TenantId>,
    ) -> Result<(), TenantError> {
        let tenant = tenant.map(|t| t.0.to_vec());
        self.run(
            &format!("DELETE FROM vcon.membership WHERE {MEMBERSHIP_IS}"),
            &[(&user.0.as_slice(), Type::BYTEA), (&tenant, Type::BYTEA)],
        )
    }

    fn delete_memberships_of_user(&mut self, user: UserId) -> Result<(), TenantError> {
        self.run(
            "DELETE FROM vcon.membership WHERE user_id = $1",
            &[(&user.0.as_slice(), Type::BYTEA)],
        )
    }

    fn delete_memberships_in(&mut self, tenant: TenantId) -> Result<(), TenantError> {
        self.run(
            "DELETE FROM vcon.membership WHERE tenant = $1",
            &[(&tenant.0.as_slice(), Type::BYTEA)],
        )
    }

    fn session(&mut self, id_hash: &[u8]) -> Result<Option<SessionRow>, TenantError> {
        let rows = self
            .0
            .query(
                &format!("SELECT {SESSION_COLUMNS} FROM vcon.session WHERE id_hash = $1"),
                &[(&id_hash, Type::BYTEA)],
            )
            .map_err(io)?;
        rows.first().map(|row| self.session_row(row)).transpose()
    }

    fn insert_session(&mut self, row: &SessionRow) -> Result<(), TenantError> {
        let id = row.id_hash.as_slice();
        let tenant = row.tenant.map(|t| t.0.to_vec());
        let scopes = self.seal("session", "scopes_sealed", id, row.scopes.as_bytes())?;
        let amr = self.seal("session", "amr_sealed", id, row.amr.as_bytes())?;
        self.run(
            &format!(
                "INSERT INTO vcon.session ({SESSION_COLUMNS})
                 VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)"
            ),
            &[
                (&id, Type::BYTEA),
                (&row.user.0.as_slice(), Type::BYTEA),
                (&tenant, Type::BYTEA),
                (&scopes, Type::BYTEA),
                (&row.created_at, Type::INT8),
                (&row.last_seen_at, Type::INT8),
                (&row.idle_expires_at, Type::INT8),
                (&row.expires_at, Type::INT8),
                (&row.idle_seconds, Type::INT8),
                (&amr, Type::BYTEA),
                (&row.csrf_hash, Type::BYTEA),
            ],
        )
    }

    fn touch_session(
        &mut self,
        id_hash: &[u8],
        last_seen_at: i64,
        idle_expires_at: i64,
    ) -> Result<(), TenantError> {
        self.run(
            "UPDATE vcon.session SET last_seen_at = $2, idle_expires_at = $3 WHERE id_hash = $1",
            &[
                (&id_hash, Type::BYTEA),
                (&last_seen_at, Type::INT8),
                (&idle_expires_at, Type::INT8),
            ],
        )
    }

    fn delete_session(&mut self, id_hash: &[u8]) -> Result<(), TenantError> {
        self.run(
            "DELETE FROM vcon.session WHERE id_hash = $1",
            &[(&id_hash, Type::BYTEA)],
        )
    }

    fn delete_sessions_of_user(&mut self, user: UserId) -> Result<(), TenantError> {
        self.run(
            "DELETE FROM vcon.session WHERE user_id = $1",
            &[(&user.0.as_slice(), Type::BYTEA)],
        )
    }

    fn delete_sessions_in(&mut self, tenant: TenantId) -> Result<(), TenantError> {
        self.run(
            "DELETE FROM vcon.session WHERE tenant = $1",
            &[(&tenant.0.as_slice(), Type::BYTEA)],
        )
    }

    fn delete_sessions_ended(&mut self, at: i64) -> Result<(), TenantError> {
        self.run(
            "DELETE FROM vcon.session WHERE idle_expires_at <= $1 OR expires_at <= $1",
            &[(&at, Type::INT8)],
        )
    }

    /// A tenant's settings by name, in the order of the names' bytes, as
    /// the local registry lists them.
    fn settings(&mut self, tenant: TenantId) -> Result<Vec<(String, String)>, TenantError> {
        let rows = self
            .0
            .query(
                "SELECT name, value_sealed FROM vcon.tenant_setting
                 WHERE tenant = $1 ORDER BY name COLLATE \"C\"",
                &[(&tenant.0.as_slice(), Type::BYTEA)],
            )
            .map_err(io)?;
        rows.iter()
            .map(|row| {
                let name: String = col(row, 0)?;
                let value = self.text(
                    "tenant_setting",
                    "value_sealed",
                    &setting_key(tenant, &name),
                    &col::<Vec<u8>>(row, 1)?,
                )?;
                Ok((name, value))
            })
            .collect()
    }

    fn put_setting(
        &mut self,
        tenant: TenantId,
        name: &str,
        value: &str,
    ) -> Result<(), TenantError> {
        let sealed = self.seal(
            "tenant_setting",
            "value_sealed",
            &setting_key(tenant, name),
            value.as_bytes(),
        )?;
        self.run(
            "INSERT INTO vcon.tenant_setting (tenant, name, value_sealed) VALUES ($1, $2, $3)
             ON CONFLICT (tenant, name) DO UPDATE SET value_sealed = EXCLUDED.value_sealed",
            &[
                (&tenant.0.as_slice(), Type::BYTEA),
                (&name, Type::TEXT),
                (&sealed, Type::BYTEA),
            ],
        )
    }

    fn delete_setting(&mut self, tenant: TenantId, name: &str) -> Result<(), TenantError> {
        self.run(
            "DELETE FROM vcon.tenant_setting WHERE tenant = $1 AND name = $2",
            &[(&tenant.0.as_slice(), Type::BYTEA), (&name, Type::TEXT)],
        )
    }

    fn delete_settings_in(&mut self, tenant: TenantId) -> Result<(), TenantError> {
        self.run(
            "DELETE FROM vcon.tenant_setting WHERE tenant = $1",
            &[(&tenant.0.as_slice(), Type::BYTEA)],
        )
    }

    fn record_times(&mut self, action: Action, status: u16) -> Result<Vec<String>, TenantError> {
        let status = i64::from(status);
        let rows = self
            .0
            .query(
                "SELECT at FROM vcon.admin_audit WHERE action = $1 AND status = $2",
                &[(&action.as_str(), Type::TEXT), (&status, Type::INT8)],
            )
            .map_err(io)?;
        rows.iter().map(|row| col(row, 0)).collect()
    }

    fn record(&mut self, entry: &Entry, now: OffsetDateTime) -> Result<(), AuditError> {
        self.0.record(entry, now)
    }

    fn count(&mut self, entry: &Entry, now: OffsetDateTime) -> Result<(), AuditError> {
        self.0.count_record(entry, now, 1)
    }

    fn count_many(
        &mut self,
        entry: &Entry,
        now: OffsetDateTime,
        times: u64,
    ) -> Result<(), AuditError> {
        self.0.count_record(entry, now, times)
    }

    fn audit_lines(
        &mut self,
        after: i64,
        limit: Option<u32>,
        out: &mut dyn Write,
    ) -> Result<(), AuditError> {
        self.0.audit_lines(after, limit.map(i64::from), out)
    }

    fn commit(self: Box<Self>) -> Result<(), CommitError> {
        self.0.finish()
    }
}