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
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
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
//! The commands other than `serve`: tenants, keys and the audit logs. Each
//! takes the data directory as its work shares it, opens the registry with
//! recovery when it runs alone and without when it may run beside a server,
//! and prints what the matching admin route answers: one JSON object a
//! line.

use std::fmt;
use std::io::{self, Write};
use std::sync::Arc;

use crate::access::{Scope, Scopes};
use crate::audit::{Actor, AuditError};
use crate::backend;
use crate::clock::{Clock, SystemClock};
use crate::config::{Config, StorageError};
use crate::datadir::{self, LockError, Sharing, Work};
use crate::envelope::{Key, KeyError};
use crate::fs::OsFs;
use crate::ids::{KeyId, TenantId, UserId};
use crate::retention::Retention;
use crate::roles::{Role, Roles};
use crate::store::ExamineError;
use crate::tenants::{
    self, BackupError, Identity, KeyLife, OpenFailure, RegistryError, Removal, TenantError,
    TenantExamination, Tenants, Unexamined, UserError,
};
use time::OffsetDateTime;
use time::format_description::well_known::Rfc3339;

/// A command, as written on the command line.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Command {
    TenantCreate {
        name: String,
        retention: Option<String>,
    },
    TenantList,
    /// `retention` is a period, or `default` for the server's.
    TenantSetRetention {
        id: String,
        retention: String,
    },
    TenantRemove {
        id: String,
        force: bool,
        override_holds: bool,
    },
    /// `metadata` is `none`, `counts` or `full`.
    TenantSetMetadata {
        id: String,
        metadata: String,
    },
    /// Sets the roles a membership in the tenant may be given; `roles`
    /// None removes the cap.
    TenantSetRoleCap {
        id: String,
        roles: Option<Vec<String>>,
    },
    /// Prints the tenant's role cap.
    TenantRoleCap {
        id: String,
    },
    /// Moves every tenant's sealed objects from the data directory to the
    /// bucket `storage.objects = "s3"` names. Runs only while no server
    /// does.
    StorageMoveObjects,
    /// Writes the tenant's kept versions as Parquet files to `to`, a
    /// directory or an `s3://bucket/prefix`, beside a running server.
    /// `since` and `until` are RFC 3339 times.
    ExportParquet {
        tenant: String,
        to: String,
        since: Option<String>,
        until: Option<String>,
    },
    KeyCreate {
        owner: Owner,
        label: String,
        /// When the key expires: by default, in a number of days, or never.
        life: KeyLife,
    },
    KeyList {
        tenant: Option<String>,
    },
    KeyRevoke {
        id: String,
    },
    /// Makes a user. `identity` is the issuer and subject an identity
    /// provider knows the user by; None makes a local account.
    UserAdd {
        label: String,
        identity: Option<(String, String)>,
    },
    UserList,
    UserDeactivate {
        id: String,
    },
    /// Gives a user roles in a tenant, or the operator role.
    MembershipGrant {
        user: String,
        place: Place,
    },
    /// Takes roles from a user's membership; with no role named, all of
    /// them.
    MembershipRevoke {
        user: String,
        place: Place,
    },
    AuditExport {
        log: Log,
        since: Option<i64>,
    },
    /// Rotates a tenant's key.
    RekeyTenant {
        id: String,
    },
    /// Rotates the master key to the one in `new_master`.
    RekeyMaster {
        new_master: std::path::PathBuf,
    },
    /// Examines every tenant and reports what needs attention.
    Check,
    /// Writes a consistent copy of the data directory to `to`, which must
    /// not exist, beside a running server.
    Backup {
        to: std::path::PathBuf,
    },
}

/// Whose key `key create` makes.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Owner {
    Tenant { id: String, scopes: Vec<String> },
    Operator,
}

/// Where a membership is: in a tenant, with the roles the command names,
/// or as an operator, whose one role the command does not name.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Place {
    Tenant { id: String, roles: Vec<String> },
    Operator,
}

/// Which audit log `audit export` writes.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Log {
    Tenant(String),
    Admin,
}

impl Command {
    /// The work this command does in the data directory.
    pub fn work(&self) -> Work {
        match self {
            Command::TenantCreate { .. } => Work::TenantCreate,
            Command::TenantList => Work::TenantList,
            Command::TenantSetRetention { .. } => Work::TenantSetRetention,
            Command::TenantRemove { .. } => Work::TenantRemove,
            Command::TenantSetMetadata { .. } => Work::TenantSetMetadata,
            Command::TenantSetRoleCap { .. } => Work::TenantSetRoleCap,
            Command::TenantRoleCap { .. } => Work::TenantRoleCap,
            Command::ExportParquet { .. } => Work::ExportParquet,
            Command::StorageMoveObjects => Work::StorageMoveObjects,
            Command::KeyCreate { .. } => Work::KeyCreate,
            Command::KeyList { .. } => Work::KeyList,
            Command::KeyRevoke { .. } => Work::KeyRevoke,
            Command::UserAdd { .. } => Work::UserAdd,
            Command::UserList => Work::UserList,
            Command::UserDeactivate { .. } => Work::UserDeactivate,
            Command::MembershipGrant { .. } => Work::MembershipGrant,
            Command::MembershipRevoke { .. } => Work::MembershipRevoke,
            Command::AuditExport { .. } => Work::AuditExport,
            Command::RekeyTenant { .. } => Work::RekeyTenant,
            Command::RekeyMaster { .. } => Work::RekeyMaster,
            Command::Check => Work::Check,
            Command::Backup { .. } => Work::Backup,
        }
    }
}

/// Why a command did not complete.
#[derive(Debug)]
pub enum CommandError {
    DataDir(LockError),
    MasterKey(KeyError),
    /// server.sqlite was made under another master key.
    WrongMasterKey,
    /// The registry in the catalog database was made under another master
    /// key.
    WrongDatabaseKey,
    /// A period that is neither like 7y, 18m or 90d nor, where allowed,
    /// `default`.
    BadPeriod(String),
    Tenant(TenantError),
    /// The registry could not be opened.
    Open(RegistryError),
    /// No key has this id.
    UnknownKey,
    /// A user or membership command was refused.
    User(UserError),
    /// The output could not be written.
    Output(io::Error),
    /// The new master key is the one already in use.
    SameMasterKey,
    /// `check` found this many problems; its report says which.
    CheckFound(usize),
    Backup(BackupError),
    /// The storage the configuration names cannot be used.
    Storage(StorageError),
    /// Not `none`, `counts` or `full`.
    BadLevel(String),
    /// Not an RFC 3339 time.
    BadTime(String),
    /// Neither a directory nor an `s3://bucket/prefix`.
    BadTarget(String),
    /// An `s3://` target with no `[export.s3]` or `[storage.s3]` table, or
    /// in a build without S3.
    NoS3,
    Export(crate::export::ExportError),
    /// Moving a tenant's objects to the bucket stopped; what was moved
    /// stays moved, and running the command again moves the rest.
    Move {
        tenant: TenantId,
        why: String,
    },
}

impl fmt::Display for CommandError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CommandError::DataDir(e) => write!(f, "{e}"),
            CommandError::MasterKey(KeyError::Refused(why)) => {
                write!(f, "the master key cannot be read: {why}")
            }
            CommandError::MasterKey(e) => write!(f, "the master key cannot be read: {e:?}"),
            CommandError::Open(e) => write!(
                f,
                "the registry cannot be opened: {}",
                crate::tenants::with_causes(e)
            ),
            CommandError::WrongMasterKey | CommandError::Tenant(TenantError::WrongKey) => {
                f.write_str("the master key is not the one server.sqlite was made under")
            }
            CommandError::WrongDatabaseKey => f.write_str(crate::tenants::WRONG_DATABASE_KEY),
            CommandError::BadPeriod(text) => write!(
                f,
                "{text:?} is not a period such as 7y, 18m or 90d (or, for set-retention, default)"
            ),
            CommandError::UnknownKey => f.write_str("no key has this id"),
            CommandError::User(e) => f.write_str(match e {
                UserError::CliOnly => "only the command line may do this",
                UserError::BadLabel => {
                    "a user label is 1 to 128 characters, with no control characters and no space at either end"
                }
                UserError::BadIdentity => {
                    "an issuer is an https:// URL of at most 255 bytes with no query or fragment, and a subject is 1 to 255 ASCII characters with no space at either end"
                }
                UserError::IdentityTaken => "an active user has this issuer and subject",
                UserError::NoUser => "no user has this id",
                UserError::Deactivated => "the user is deactivated",
                UserError::NoTenant => "no tenant has this id",
                UserError::Closing => "the tenant is being removed",
                UserError::BadRoles => {
                    "a tenant membership's roles are one or more of reader, producer, records, legal, auditor and tenant-admin; an operator membership holds operator alone"
                }
                UserError::OverCap => "a role named is outside the tenant's role cap",
                UserError::BadCap => {
                    "a role cap is one or more of reader, producer, records, legal, auditor and tenant-admin"
                }
                UserError::NoMembership => "the user has no such membership",
                UserError::Registry(_) => "the data could not be read or written",
            }),
            CommandError::Output(e) => write!(f, "the output could not be written: {e}"),
            CommandError::SameMasterKey => {
                f.write_str("the new master key is the one already in use")
            }
            CommandError::CheckFound(n) => write!(f, "check found {n} problem(s); see its report"),
            CommandError::Storage(e) => write!(f, "the storage cannot be used: {e}"),
            CommandError::BadLevel(text) => write!(
                f,
                "{text:?} is not a metadata level: none, counts or full"
            ),
            CommandError::BadTime(text) => write!(
                f,
                "{text:?} is not an RFC 3339 time such as 2026-11-05T00:00:00Z"
            ),
            CommandError::BadTarget(text) => write!(
                f,
                "{text:?} is not a directory or an s3://bucket/prefix"
            ),
            CommandError::NoS3 => f.write_str(
                "an s3:// target needs an [export.s3] or [storage.s3] table, in a build with the s3 feature",
            ),
            CommandError::Export(e) => write!(f, "{e}"),
            CommandError::Move { tenant, why } => write!(
                f,
                "moving tenant {tenant}'s objects to the bucket stopped: {why}; what moved stays \
                 moved, and running storage move-objects again moves the rest"
            ),
            CommandError::Backup(e) => f.write_str(match e {
                BackupError::Exists => {
                    "the backup's path already exists; a backup is written to a new directory"
                }
                BackupError::KeptChanging => {
                    "tenants were removed or versions purged during each try; try again"
                }
                BackupError::Io => "the backup could not be written, or the data could not be read",
                BackupError::AuditFailed => "the audit record could not be written",
                BackupError::NoFileBackup => tenants::NO_FILE_BACKUP,
            }),
            CommandError::Tenant(e) => f.write_str(match e {
                TenantError::NotFound => "no tenant has this id",
                TenantError::NameTaken => "another tenant has this name",
                TenantError::BadName => {
                    "a tenant name is 1 to 128 characters, with no control characters and no space at either end"
                }
                TenantError::HoldsVcons => "the tenant holds vCons; add --force to remove them too",
                TenantError::UnderHold => {
                    "a vCon of the tenant is under a legal hold; only --override-holds removes it"
                }
                TenantError::BadScopes => {
                    "a tenant key's scopes are one or more of write, read, delete, legal, audit and tenant_admin, each named once"
                }
                TenantError::BadExpiry => {
                    "a key expires in 1 to 365 days, or never where keys.no_expiry allows it"
                }
                TenantError::LimitReached => {
                    "the master key has wrapped all the tenant keys it may; run rekey master"
                }
                TenantError::AuditFailed => "the audit record could not be written",
                TenantError::Closing => "the tenant is being removed",
                TenantError::CliOnly => "only the command line may do this",
                TenantError::BadSetting(_) => {
                    "a setting's name or value is not one the tenant may set"
                }
                TenantError::WrongKey => unreachable!("matched above"),
                TenantError::NoFileBackup => {
                    "the registry is kept in PostgreSQL, which this backend does not copy into a file; back up the catalog database with pg_dump"
                }
                TenantError::Io => "the data could not be read or written",
            }),
        }
    }
}

/// Runs `command` against the data directory `config` names, writing its
/// answer to `out`.
/// `vcon-server migrate`: makes or brings up to date the schema of the
/// PostgreSQL catalog `config` names, and prints what it did. It uses no
/// data directory and no master key.
///
/// # Errors
///
/// [`CommandError::Storage`] when the catalog is local, this build has no
/// PostgreSQL support, or the migration was refused.
pub fn migrate(config: &Config, out: &mut dyn Write) -> Result<(), CommandError> {
    let done = backend::migrate_for_command(&config.catalog).map_err(CommandError::Storage)?;
    line(out, &done)
}

pub fn run(config: &Config, command: &Command, out: &mut dyn Write) -> Result<(), CommandError> {
    let sharing = command.work().sharing();
    let _lock = datadir::lock(&config.data_dir, sharing).map_err(CommandError::DataDir)?;
    let master = Key::load_master(&config.master_key).map_err(CommandError::MasterKey)?;
    // With the catalogs in PostgreSQL the command holds the locks of its
    // kind on the database, as it holds the data directory's: beside a
    // server, or alone.
    let catalogs = backend::catalog_for(&config.catalog, sharing).map_err(CommandError::Storage)?;
    run_and_give_up(config, command, master, &catalogs, out)
}

/// Runs `command` on `catalogs`, and then gives up the command's locks on
/// the catalog database, done or refused, while the connections still
/// answer: the next command or server finds the database free at once.
pub(crate) fn run_and_give_up(
    config: &Config,
    command: &Command,
    master: Key,
    catalogs: &backend::Catalogs,
    out: &mut dyn Write,
) -> Result<(), CommandError> {
    let done = run_on(config, command, master, catalogs, out);
    catalogs.give_up_locks();
    done
}

/// [`run`], with the data directory locked and the catalogs reached.
fn run_on(
    config: &Config,
    command: &Command,
    master: Key,
    catalogs: &backend::Catalogs,
    out: &mut dyn Write,
) -> Result<(), CommandError> {
    let sharing = command.work().sharing();
    // A master rotation renames over server.sqlite, so nothing may hold it
    // open: it runs before the registry is opened.
    if let Command::RekeyMaster { new_master } = command {
        return rekey_master(config, &master, new_master, catalogs, out);
    }
    // Refused before anything is opened or claimed: a PostgreSQL catalog
    // has no objects in a data directory to move.
    if matches!(command, Command::StorageMoveObjects) && catalogs.server().is_some() {
        return Err(nothing_to_move_on_a_database());
    }
    // A command beside a running server must not run start-up recovery: it
    // would take the server's work in progress for a crash's leftovers. Nor
    // does `check`, which reports what recovery would remove.
    let open = if sharing == Sharing::Beside || matches!(command, Command::Check) {
        Tenants::open_beside_in
    } else {
        Tenants::open_in
    };
    let objects = backend::objects_for(&config.storage, &config.data_dir, catalogs)
        .map_err(CommandError::Storage)?;
    // The bucket the objects move to, for `storage move-objects`.
    #[cfg(feature = "s3")]
    let bucket = match &objects {
        backend::ObjectPlace::S3(s3) => Some(s3.clone()),
        backend::ObjectPlace::Local => None,
    };
    let tenants = open(
        &config.data_dir,
        master,
        Arc::new(OsFs),
        Arc::new(SystemClock),
        objects,
        catalogs,
    )
    .map_err(|e| match e {
        RegistryError::WrongKey => CommandError::WrongMasterKey,
        RegistryError::WrongDatabaseKey => CommandError::WrongDatabaseKey,
        other => CommandError::Open(other),
    })?;
    tenants.allow_no_expiry(config.keys_no_expiry);
    let who = Actor::Cli;
    match command {
        Command::TenantCreate { name, retention } => {
            let retention = retention.as_deref().map(period).transpose()?;
            let id = tenants.create(who, name, retention)?;
            line(out, &serde_json::json!({ "id": id.to_string() }))
        }
        Command::TenantList => {
            for tenant in tenants.list(who)? {
                line(out, &tenant.to_json())?;
            }
            Ok(())
        }
        Command::TenantSetRetention { id, retention } => {
            let retention = match retention.as_str() {
                "default" => None,
                text => Some(period(text)?),
            };
            let id = tenant_id(id)?;
            tenants.set_retention(who, id, retention)?;
            line(
                out,
                &serde_json::json!({
                    "id": id.to_string(),
                    "retention": retention.map(|r| r.to_string()),
                }),
            )
        }
        Command::TenantRemove {
            id,
            force,
            override_holds,
        } => {
            let removal = match (force, override_holds) {
                (_, true) => Removal::ForceOverridingHolds,
                (true, false) => Removal::Force,
                (false, false) => Removal::Empty,
            };
            Ok(tenants.remove(who, tenant_id(id)?, removal)?)
        }
        Command::KeyCreate { owner, label, life } => {
            let (tenant, scopes) = match owner {
                Owner::Tenant { id, scopes } => (
                    Some(tenant_id(id)?),
                    Scopes::named(scopes).ok_or(CommandError::Tenant(TenantError::BadScopes))?,
                ),
                Owner::Operator => (None, Scopes::of(&[Scope::Admin])),
            };
            let made = tenants.create_key_for(who, tenant, scopes, label, *life)?;
            let shown = made
                .to_json()
                .ok_or(CommandError::Tenant(TenantError::Io))?;
            line(out, &shown)
        }
        Command::KeyList { tenant } => {
            let tenant = tenant.as_deref().map(tenant_id).transpose()?;
            for key in tenants.list_keys(who, tenant)? {
                let shown = key.to_json().ok_or(CommandError::Tenant(TenantError::Io))?;
                line(out, &shown)?;
            }
            Ok(())
        }
        Command::KeyRevoke { id } => {
            let id = KeyId::parse(id).ok_or(CommandError::UnknownKey)?;
            tenants.revoke_key(who, id).map_err(|e| match e {
                TenantError::NotFound => CommandError::UnknownKey,
                other => CommandError::Tenant(other),
            })
        }
        Command::UserAdd { label, identity } => {
            let identity = identity.as_ref().map(|(issuer, subject)| Identity {
                issuer: issuer.clone(),
                subject: subject.clone(),
            });
            let id = tenants.add_user(who, label, identity.as_ref())?;
            line(out, &serde_json::json!({ "id": id.to_string() }))
        }
        Command::UserList => {
            for user in tenants.list_users(who)? {
                let shown = user
                    .to_json()
                    .ok_or(CommandError::Tenant(TenantError::Io))?;
                line(out, &shown)?;
            }
            Ok(())
        }
        Command::UserDeactivate { id } => Ok(tenants.deactivate_user(who, user_id(id)?)?),
        Command::MembershipGrant { user, place } => {
            let user = user_id(user)?;
            let (tenant, roles) = match place {
                Place::Tenant { id, roles } => (Some(tenant_id(id)?), named_roles(roles)?),
                Place::Operator => (None, Roles::of(&[Role::Operator])),
            };
            let held = tenants.grant_membership(who, user, tenant, roles)?;
            membership_line(out, user, tenant, held)
        }
        Command::MembershipRevoke { user, place } => {
            let user = user_id(user)?;
            // No role named takes every role the membership holds.
            let (tenant, roles) = match place {
                Place::Tenant { id, roles } if roles.is_empty() => (Some(tenant_id(id)?), None),
                Place::Tenant { id, roles } => (Some(tenant_id(id)?), Some(named_roles(roles)?)),
                Place::Operator => (None, None),
            };
            let left = tenants.revoke_membership(who, user, tenant, roles)?;
            membership_line(out, user, tenant, left)
        }
        Command::TenantSetMetadata { id, metadata } => {
            let level = crate::export::Metadata::parse(metadata)
                .ok_or_else(|| CommandError::BadLevel(metadata.clone()))?;
            let id = tenant_id(id)?;
            tenants.set_metadata(who, id, level)?;
            line(
                out,
                &serde_json::json!({ "id": id.to_string(), "metadata": level.as_str() }),
            )
        }
        Command::TenantSetRoleCap { id, roles } => {
            // A name that is no role's is refused as a cap that names
            // operator is: neither can be a cap.
            let cap = roles
                .as_ref()
                .map(|names| Roles::named(names).ok_or(CommandError::User(UserError::BadCap)))
                .transpose()?;
            let id = tenant_id(id)?;
            tenants.set_role_cap(who, id, cap)?;
            role_cap_line(out, id, cap)
        }
        Command::TenantRoleCap { id } => {
            let id = tenant_id(id)?;
            let cap = tenants.role_cap(who, id)?;
            role_cap_line(out, id, cap)
        }
        Command::ExportParquet {
            tenant,
            to,
            since,
            until,
        } => export_parquet(
            config,
            &tenants,
            tenant,
            to,
            since.as_deref(),
            until.as_deref(),
            out,
        ),
        Command::RekeyTenant { id } => Ok(tenants.rekey_tenant(who, tenant_id(id)?)?),
        Command::RekeyMaster { .. } => unreachable!("rotated before the registry is opened"),
        Command::Check => check(config, &tenants, out),
        #[cfg(feature = "s3")]
        Command::StorageMoveObjects => match bucket {
            Some(s3) => move_objects(config, &tenants, &s3, out),
            None => Err(not_a_bucket()),
        },
        #[cfg(not(feature = "s3"))]
        Command::StorageMoveObjects => Err(not_a_bucket()),
        Command::Backup { to } => {
            let done = tenants.backup(who, to).map_err(CommandError::Backup)?;
            line(
                out,
                &serde_json::json!({
                    "tenants": done.tenants,
                    "versions": done.versions,
                    "tries": done.tries,
                }),
            )
        }
        Command::AuditExport { log, since } => {
            let exported = match log {
                Log::Admin => tenants.export_admin(*since, out),
                Log::Tenant(id) => tenants.store(tenant_id(id)?)?.export(*since, out),
            };
            exported.map_err(|e| match e {
                AuditError::Write(e) => CommandError::Output(e),
                AuditError::Sqlite(_) | AuditError::Database(_) => {
                    CommandError::Tenant(TenantError::Io)
                }
            })
        }
    }
}

impl From<TenantError> for CommandError {
    fn from(e: TenantError) -> CommandError {
        CommandError::Tenant(e)
    }
}

impl From<UserError> for CommandError {
    /// A registry failure is the error every other command gives for it.
    fn from(e: UserError) -> CommandError {
        match e {
            UserError::Registry(e) => CommandError::Tenant(e),
            other => CommandError::User(other),
        }
    }
}

/// A user id as written; text that is no id names no user.
fn user_id(text: &str) -> Result<UserId, CommandError> {
    UserId::parse(text).ok_or(CommandError::User(UserError::NoUser))
}

/// The roles `names` name; a name that is no role's is refused as roles
/// no membership can hold.
fn named_roles(names: &[String]) -> Result<Roles, CommandError> {
    Roles::named(names).ok_or(CommandError::User(UserError::BadRoles))
}

/// The line a role cap command prints: the tenant, and the roles of its
/// cap or null for no cap.
fn role_cap_line(
    out: &mut dyn Write,
    tenant: TenantId,
    cap: Option<Roles>,
) -> Result<(), CommandError> {
    line(
        out,
        &serde_json::json!({
            "id": tenant.to_string(),
            "role_cap": cap.map(Roles::names),
        }),
    )
}

/// The line a membership command prints: the user, the tenant (null for
/// an operator membership) and the roles the membership now holds.
fn membership_line(
    out: &mut dyn Write,
    user: UserId,
    tenant: Option<TenantId>,
    roles: Roles,
) -> Result<(), CommandError> {
    line(
        out,
        &serde_json::json!({
            "user": user.to_string(),
            "tenant": tenant.map(|t| t.to_string()),
            "roles": roles.names(),
        }),
    )
}

/// Exports a tenant's kept versions as Parquet files and prints what was
/// written. Every argument is checked before anything is written.
fn export_parquet(
    config: &Config,
    tenants: &Tenants,
    tenant: &str,
    to: &str,
    since: Option<&str>,
    until: Option<&str>,
    out: &mut dyn Write,
) -> Result<(), CommandError> {
    use crate::export::target::Place;
    use crate::export::{self, Plan};
    let time = |text: Option<&str>| {
        text.map(|t| {
            OffsetDateTime::parse(t, &Rfc3339)
                .map(crate::clock::nanos)
                .map_err(|_| CommandError::BadTime(t.to_owned()))
        })
        .transpose()
    };
    let (since, until) = (time(since)?, time(until)?);
    let place = Place::parse(to).ok_or_else(|| CommandError::BadTarget(to.to_owned()))?;
    let id = tenant_id(tenant)?;
    let level = tenants.metadata(id)?;
    let target: Box<dyn export::Target> = match place {
        Place::Dir(dir) => Box::new(export::DirTarget::new(&dir, Arc::new(OsFs))),
        Place::S3 { bucket, prefix } => s3_target(config, &bucket, &prefix)?,
    };
    let store = tenants.store(id)?;
    let plan = Plan {
        level,
        since,
        until,
        sizes: export::SIZES,
        limits: vcon::read::Limits::new(config.inflated_limit),
    };
    let done = export::export(&store, &plan, target.as_ref(), SystemClock.now())
        .map_err(CommandError::Export)?;
    line(
        out,
        &serde_json::json!({
            "tenant": id.to_string(),
            "metadata": level.as_str(),
            "to": target.describe(),
            "manifest": done.manifest,
            "seq": done.seq,
            "files": done.files.len(),
            "rows": done.rows,
            "bytes": done.bytes,
            "skipped": done.skipped,
            "unread": done.unread,
        }),
    )
}

/// The bucket target `--to` names, reached through `[export.s3]`, else
/// `[storage.s3]`'s service and keys.
#[cfg(feature = "s3")]
fn s3_target(
    config: &Config,
    bucket: &str,
    prefix: &str,
) -> Result<Box<dyn crate::export::Target>, CommandError> {
    use crate::config::{ExportS3, ObjectStore};
    let (service, table) = match (&config.export.s3, &config.storage.objects) {
        (Some(service), _) => (service.clone(), "export.s3"),
        (None, ObjectStore::S3(storage)) => (ExportS3::from(storage), "storage.s3"),
        (None, ObjectStore::Local) => return Err(CommandError::NoS3),
    };
    let target = crate::export::s3::S3Target::connect(&service.for_bucket(bucket, prefix), table)
        .map_err(CommandError::Storage)?;
    Ok(Box::new(target))
}

#[cfg(not(feature = "s3"))]
fn s3_target(
    _config: &Config,
    _bucket: &str,
    _prefix: &str,
) -> Result<Box<dyn crate::export::Target>, CommandError> {
    Err(CommandError::NoS3)
}

/// How many rows of a table a master rotation in PostgreSQL reads and
/// writes in one statement.
#[cfg(feature = "postgres")]
const REKEY_MASTER_BATCH: i64 = 1_000;

/// The refusal of `storage move-objects` with the catalogs in PostgreSQL.
fn nothing_to_move_on_a_database() -> CommandError {
    CommandError::Storage(StorageError(
        "storage move-objects moves the objects of a data directory's catalogs to the bucket; \
         with catalog.backend = \"postgres\" every object is in the bucket from its first \
         write and there is nothing to move: to move a data directory's objects, run it with \
         the [catalog] and [catalog.postgres] tables removed"
            .to_owned(),
    ))
}

/// Rotates the master key and says what the operator does next.
fn rekey_master(
    config: &Config,
    master: &Key,
    new_master: &std::path::Path,
    catalogs: &backend::Catalogs,
    out: &mut dyn Write,
) -> Result<(), CommandError> {
    let new =
        Key::load_master_named(new_master, "--new-master").map_err(CommandError::MasterKey)?;
    if new.id() == master.id() {
        return Err(CommandError::SameMasterKey);
    }
    match catalogs {
        backend::Catalogs::Local => {
            tenants::rekey_master(&config.data_dir, master, &new, SystemClock.now()).map_err(
                |e| match e {
                    TenantError::WrongKey => CommandError::WrongMasterKey,
                    other => CommandError::Tenant(other),
                },
            )?;
        }
        // The registry's rows are made again under the new key in one
        // transaction, with this command's locks held: no server and no
        // other command has the database.
        #[cfg(feature = "postgres")]
        backend::Catalogs::Postgres(database) => {
            backend::postgres::PgRegistry::rekey_master(
                database,
                master,
                &new,
                SystemClock.now(),
                REKEY_MASTER_BATCH,
            )
            .map_err(|e| match e {
                TenantError::WrongKey => CommandError::WrongDatabaseKey,
                other => CommandError::Tenant(other),
            })?;
        }
    }
    line(
        out,
        &serde_json::json!({
            "master_key_id": new.id().to_string(),
            "next": "point master_key in the configuration at the new key file, start the server, then destroy every copy of the old master key",
        }),
    )
}

/// Examines every tenant, prints a line for each and a summary, and fails
/// when anything needs the operator.
/// The refusal of `storage move-objects` with the objects kept in the data
/// directory.
fn not_a_bucket() -> CommandError {
    CommandError::Storage(StorageError(
        "storage move-objects moves the objects to the bucket that storage.objects = \"s3\" \
         names; this configuration keeps them in the data directory"
            .to_owned(),
    ))
}

/// `storage move-objects`: each tenant's object files, under its directory
/// in the data directory, moved to the bucket, as `backend::migrate` does;
/// one line per tenant with what moved and what `check` then finds missing,
/// and a total.
#[cfg(feature = "s3")]
fn move_objects(
    config: &Config,
    tenants: &Tenants,
    s3: &Arc<crate::backend::s3::S3>,
    out: &mut dyn Write,
) -> Result<(), CommandError> {
    use crate::backend::migrate::MoveError;
    let mut total = 0;
    for id in tenants.ids()? {
        let dir = config
            .data_dir
            .join("tenants")
            .join(id.to_string())
            .join("objects");
        let moved = if dir.exists() {
            let from = crate::backend::local::LocalObjects::new(dir, Arc::new(OsFs));
            crate::backend::migrate::move_objects(&from, &s3.tenant(id)).map_err(|e| {
                let why = match e {
                    MoveError::From(e) => {
                        format!("an object file could not be read or removed: {e}")
                    }
                    MoveError::To(e) => format!("the bucket could not be written or read: {e}"),
                    MoveError::Differs(version) => format!(
                        "version {version} read back from the bucket differs from its file, \
                         which stays in the data directory"
                    ),
                };
                CommandError::Move { tenant: id, why }
            })?
        } else {
            0
        };
        let missing = tenants
            .store(id)?
            .check()
            .map_err(|e| CommandError::Move {
                tenant: id,
                why: format!("the objects in the bucket could not be checked: {e:?}"),
            })?
            .len();
        total += moved;
        line(
            out,
            &serde_json::json!({ "tenant": id.to_string(), "moved": moved, "missing": missing }),
        )?;
    }
    line(out, &serde_json::json!({ "moved": total }))
}

fn check(config: &Config, tenants: &Tenants, out: &mut dyn Write) -> Result<(), CommandError> {
    let now = SystemClock.now();
    let found = tenants.examine(config.retention_default, now)?;
    let time = |at: Option<OffsetDateTime>| at.and_then(|at| at.format(&Rfc3339).ok());
    let mut total = 0;
    for tenant in &found.tenants {
        let named = problems(
            tenant,
            config.purge_interval,
            now,
            config.storage.parquet.is_some(),
        );
        total += named.len();
        let store = tenant.store.as_ref().ok();
        line(
            out,
            &serde_json::json!({
                "tenant": tenant.id.to_string(),
                "name": tenant.name,
                "versions": store.map(|s| s.versions),
                "packed": store.map(|s| s.packed),
                "damaged": store.map(|s| s.damaged.iter().map(ToString::to_string).collect::<Vec<_>>()),
                "leftovers": store.map(|s| &s.leftovers),
                "past_due": store.map(|s| s.due),
                "oldest_past_due": time(store.and_then(|s| s.oldest_due_since)),
                "purges_awaiting_rotation": store.map(|s| s.purges_since_key),
                "oldest_purge_awaiting_rotation": time(store.and_then(|s| s.oldest_purge_since_key)),
                "rotation_stopped": tenant.rotation_stopped,
                "problems": named,
            }),
        )?;
    }
    line(
        out,
        &serde_json::json!({
            "tenants": found.tenants.len(),
            "removed_tenants_awaiting_master_rotation": found.removed_since_master,
            "oldest_removal_awaiting_master_rotation": time(found.oldest_removed_since_master),
            "problems": total,
        }),
    )?;
    match total {
        0 => Ok(()),
        n => Err(CommandError::CheckFound(n)),
    }
}

/// What in one tenant's examination needs the operator. A stalled sweep is
/// one whose oldest due version has waited longer than `purge_interval`;
/// a shorter wait is the next sweep's work. Packed versions are a problem
/// when the configuration has no `[storage.parquet]` (`parquet` false),
/// since `serve` then refuses to start. Leftover files and work waiting
/// for rotation are reported but are not problems.
pub fn problems(
    tenant: &TenantExamination,
    purge_interval: std::time::Duration,
    now: OffsetDateTime,
    parquet: bool,
) -> Vec<String> {
    let mut found = Vec::new();
    if tenant.rotation_stopped {
        found.push("a key rotation was stopped part way; run rekey tenant again".to_owned());
    }
    match &tenant.store {
        Err(Unexamined::Open(OpenFailure::WrongKey)) => {
            found.push("its catalog does not open under its tenant key".to_owned());
        }
        Err(Unexamined::Open(OpenFailure::Access { path, error })) => {
            found.push(format!(
                "its catalog cannot be opened: {}: {error}",
                path.display()
            ));
        }
        Err(Unexamined::Open(OpenFailure::Refused(e))) => {
            found.push(format!("its catalog cannot be opened: {e:?}"));
        }
        Err(Unexamined::Examine(ExamineError::Objects { kind, os_error })) => {
            let number = os_error
                .map(|n| format!(" (os error {n})"))
                .unwrap_or_default();
            found.push(format!("its objects cannot be read: {kind}{number}"));
        }
        Err(Unexamined::Examine(ExamineError::Failed(e))) => {
            found.push(format!("it could not be examined: {e:?}"));
        }
        Ok(store) => {
            if store.packed > 0 && !parquet {
                found.push(format!(
                    "{} version(s) are packed into Parquet files, and the configuration has no \
                     [storage.parquet]: serve refuses to start until the table is put back",
                    store.packed
                ));
            }
            for version in &store.damaged {
                found.push(format!("version {version} is missing or does not open"));
            }
            if let Some(since) = store.oldest_due_since
                && now - since > purge_interval
            {
                found.push(format!(
                    "{} version(s) due for purge, the oldest longer than purge_interval: the sweep is not keeping up",
                    store.due
                ));
            }
        }
    }
    found
}

/// A period as a command line writes it.
fn period(text: &str) -> Result<Retention, CommandError> {
    Retention::parse(text).ok_or_else(|| CommandError::BadPeriod(text.to_owned()))
}

/// A tenant id as a command line writes it; one that does not parse names
/// no tenant.
fn tenant_id(text: &str) -> Result<TenantId, CommandError> {
    TenantId::parse(text).ok_or(CommandError::Tenant(TenantError::NotFound))
}

/// Writes one JSON object as a line.
fn line(out: &mut dyn Write, value: &serde_json::Value) -> Result<(), CommandError> {
    writeln!(out, "{value}").map_err(CommandError::Output)
}

#[cfg(test)]
mod tests {
    use serde_json::{Value, json};

    use super::*;
    use crate::store::Caller;
    use crate::store::testing::vcon;

    const UUID: &str = "01a07da8-c2bb-83e5-b9a2-279e0d16bc46";

    struct Setup {
        _dir: tempfile::TempDir,
        config: Config,
    }

    fn chmod(path: &std::path::Path, mode: u32) {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode)).expect("chmod");
    }

    fn setup() -> Setup {
        let dir = tempfile::tempdir().expect("temp dir");
        let data = dir.path().join("data");
        std::fs::create_dir(&data).expect("data dir");
        let master = dir.path().join("master.key");
        std::fs::write(&master, [0x5a; 32]).expect("master key file");
        chmod(&master, 0o600);
        let config = crate::config::parse(&format!(
            "listen = \"127.0.0.1:1\"\ndata_dir = {data:?}\nmaster_key = {master:?}\n\
             [tls]\ncert = \"c\"\nkey = \"k\"\n"
        ))
        .expect("a configuration");
        Setup { _dir: dir, config }
    }

    impl Setup {
        /// Runs `command` and parses each line it prints.
        fn run(&self, command: Command) -> Result<Vec<Value>, CommandError> {
            let mut out = Vec::new();
            run(&self.config, &command, &mut out)?;
            Ok(String::from_utf8(out)
                .expect("text")
                .lines()
                .map(|line| serde_json::from_str(line).expect("a JSON line"))
                .collect())
        }

        fn create(&self, name: &str) -> String {
            let out = self
                .run(Command::TenantCreate {
                    name: name.to_owned(),
                    retention: None,
                })
                .expect("made");
            out[0]["id"].as_str().expect("an id").to_owned()
        }

        /// The registry as a test reads it, with the command's locks free.
        fn tenants(&self) -> Tenants {
            Tenants::open_beside(
                &self.config.data_dir,
                Key::load_master(&self.config.master_key).expect("master"),
                Arc::new(OsFs),
                Arc::new(SystemClock),
            )
            .expect("opens")
        }

        fn tenant_dirs(&self) -> Vec<String> {
            let mut names: Vec<String> = std::fs::read_dir(self.config.data_dir.join("tenants"))
                .expect("tenants dir")
                .map(|e| e.expect("entry").file_name().to_string_lossy().into_owned())
                .collect();
            names.sort();
            names
        }
    }

    /// `key create` for a tenant key that reads, with this life.
    fn key_with(s: &Setup, tenant: &str, life: KeyLife) -> Result<Vec<Value>, CommandError> {
        s.run(Command::KeyCreate {
            owner: Owner::Tenant {
                id: tenant.to_owned(),
                scopes: vec!["read".to_owned()],
            },
            label: "dated".to_owned(),
            life,
        })
    }

    fn rfc3339(value: &Value) -> time::OffsetDateTime {
        time::OffsetDateTime::parse(
            value.as_str().expect("a time as text"),
            &time::format_description::well_known::Rfc3339,
        )
        .expect("RFC 3339")
    }

    /// The second a key was made in. A key's expiry is a whole second:
    /// its life is counted from here.
    fn made_in(value: &Value) -> time::OffsetDateTime {
        rfc3339(value)
            .replace_nanosecond(0)
            .expect("zero is in range")
    }

    #[test]
    fn key_create_prints_the_expiry_of_the_key_it_makes() {
        let s = setup();
        let tenant = s.create("acme");
        let made = key_with(&s, &tenant, KeyLife::Default).expect("made");
        let listed = s.run(Command::KeyList { tenant: None }).expect("listed");
        assert_eq!(
            rfc3339(&made[0]["expires_at"]) - made_in(&listed[0]["created_at"]),
            time::Duration::days(90),
            "the default life"
        );
        assert_eq!(
            made[0]["expires_at"], listed[0]["expires_at"],
            "the list shows the same expiry"
        );
        assert!(listed[0]["last_used_at"].is_null(), "a key never used");
    }

    #[test]
    fn key_create_makes_a_key_for_the_days_asked_or_with_no_expiry() {
        let s = setup();
        let tenant = s.create("acme");
        let week = key_with(&s, &tenant, KeyLife::Days(7)).expect("made");
        let never = key_with(&s, &tenant, KeyLife::Never).expect("made");
        let listed = s.run(Command::KeyList { tenant: None }).expect("listed");
        assert_eq!(
            rfc3339(&week[0]["expires_at"]) - made_in(&listed[0]["created_at"]),
            time::Duration::days(7),
            "seven days"
        );
        assert!(
            never[0]["expires_at"].is_null(),
            "no expiry is printed as null"
        );
        assert!(listed[1]["expires_at"].is_null(), "and listed as null");
    }

    #[test]
    fn key_create_refuses_days_outside_1_to_365() {
        let s = setup();
        let tenant = s.create("acme");
        for days in [0, 366] {
            assert!(
                matches!(
                    key_with(&s, &tenant, KeyLife::Days(days)),
                    Err(CommandError::Tenant(TenantError::BadExpiry))
                ),
                "{days} days"
            );
        }
        let listed = s.run(Command::KeyList { tenant: None }).expect("listed");
        assert_eq!(listed.len(), 0, "no key was made");
    }

    #[test]
    fn key_create_refuses_no_expiry_where_the_configuration_says_so() {
        let mut s = setup();
        let tenant = s.create("acme");
        s.config.keys_no_expiry = false;
        assert!(
            matches!(
                key_with(&s, &tenant, KeyLife::Never),
                Err(CommandError::Tenant(TenantError::BadExpiry))
            ),
            "a tenant key"
        );
        let operator = s.run(Command::KeyCreate {
            owner: Owner::Operator,
            label: "ops".to_owned(),
            life: KeyLife::Never,
        });
        assert!(
            matches!(operator, Err(CommandError::Tenant(TenantError::BadExpiry))),
            "an operator key"
        );
        assert!(
            key_with(&s, &tenant, KeyLife::Default).is_ok(),
            "a key with an expiry is still made"
        );
    }

    #[test]
    fn an_operator_key_may_have_no_expiry_by_default() {
        let s = setup();
        let made = s
            .run(Command::KeyCreate {
                owner: Owner::Operator,
                label: "ops".to_owned(),
                life: KeyLife::Never,
            })
            .expect("made");
        assert!(made[0]["expires_at"].is_null(), "no expiry");
    }

    fn tenant_error(result: Result<Vec<Value>, CommandError>) -> TenantError {
        match result {
            Err(CommandError::Tenant(e)) => e,
            // What a command prints is not shown: `key create` prints a token.
            Ok(_) => panic!("the command succeeded, and a tenant error was expected"),
            Err(other) => panic!("not a tenant error: {other:?}"),
        }
    }

    #[test]
    fn tenant_create_prints_the_new_id_and_list_shows_it() {
        let s = setup();
        let out = s
            .run(Command::TenantCreate {
                name: "acme".to_owned(),
                retention: Some("18m".to_owned()),
            })
            .expect("made");
        assert_eq!(out.len(), 1);
        let id = out[0]["id"].as_str().expect("an id").to_owned();
        assert!(TenantId::parse(&id).is_some(), "{id}");
        assert_eq!(out[0], json!({ "id": id }));
        let beta = s.create("beta");
        assert_eq!(
            s.run(Command::TenantList).expect("listed"),
            [
                json!({ "id": id, "name": "acme", "retention": "18m", "metadata": "none" }),
                json!({ "id": beta, "name": "beta", "retention": null, "metadata": "none" }),
            ]
        );
    }

    #[test]
    fn a_bad_period_is_refused_before_anything_is_made() {
        let s = setup();
        let error = s
            .run(Command::TenantCreate {
                name: "acme".to_owned(),
                retention: Some("7w".to_owned()),
            })
            .expect_err("refused");
        assert!(
            matches!(&error, CommandError::BadPeriod(p) if p == "7w"),
            "{error:?}"
        );
        assert!(error.to_string().contains("7w"), "{error}");
        assert_eq!(
            s.run(Command::TenantList).expect("listed"),
            Vec::<Value>::new()
        );
        let id = s.create("acme");
        let error = s
            .run(Command::TenantSetRetention {
                id,
                retention: "forever".to_owned(),
            })
            .expect_err("refused");
        assert!(matches!(&error, CommandError::BadPeriod(p) if p == "forever"));
    }

    #[test]
    fn set_retention_takes_a_period_or_default() {
        let s = setup();
        let id = s.create("acme");
        let set = |retention: &str| {
            s.run(Command::TenantSetRetention {
                id: id.clone(),
                retention: retention.to_owned(),
            })
            .expect("set")
        };
        assert_eq!(set("90d"), [json!({ "id": id, "retention": "90d" })]);
        assert_eq!(
            s.run(Command::TenantList).expect("listed")[0]["retention"],
            "90d"
        );
        assert_eq!(set("default"), [json!({ "id": id, "retention": null })]);
        assert_eq!(
            s.run(Command::TenantList).expect("listed")[0]["retention"],
            Value::Null
        );
    }

    #[test]
    fn an_unknown_or_malformed_tenant_is_not_found() {
        let s = setup();
        for id in [TenantId::generate().to_string(), "not-an-id".to_owned()] {
            assert_eq!(
                tenant_error(s.run(Command::TenantSetRetention {
                    id: id.clone(),
                    retention: "7y".to_owned()
                })),
                TenantError::NotFound
            );
            assert_eq!(
                tenant_error(s.run(Command::TenantRemove {
                    id: id.clone(),
                    force: true,
                    override_holds: false
                })),
                TenantError::NotFound
            );
            assert_eq!(
                tenant_error(s.run(Command::KeyCreate {
                    owner: Owner::Tenant {
                        id: id.clone(),
                        scopes: vec!["read".to_owned()]
                    },
                    label: "l".to_owned(),
                    life: KeyLife::Default,
                })),
                TenantError::NotFound
            );
            assert_eq!(
                tenant_error(s.run(Command::KeyList {
                    tenant: Some(id.clone())
                })),
                TenantError::NotFound
            );
            assert_eq!(
                tenant_error(s.run(Command::AuditExport {
                    log: Log::Tenant(id.clone()),
                    since: None
                })),
                TenantError::NotFound
            );
        }
    }

    /// Without --force a tenant holding vCons stays; --force does not pass
    /// a legal hold; --override-holds does.
    #[test]
    fn remove_goes_as_far_as_its_flags_allow() {
        let s = setup();
        let empty = s.create("empty");
        let full = s.create("full");
        {
            let tenants = s.tenants();
            let store = tenants
                .store(TenantId::parse(&full).expect("id"))
                .expect("store");
            let caller = Caller { key: KeyId([1; 8]) };
            let (found, body) = vcon(UUID, "kept");
            store.create(&caller, &found, &body).expect("stored");
            store.set_hold(&caller, found.uuid, true).expect("held");
        }
        let remove = |id: &str, force, override_holds| {
            s.run(Command::TenantRemove {
                id: id.to_owned(),
                force,
                override_holds,
            })
        };
        assert_eq!(
            remove(&empty, false, false).expect("removed"),
            Vec::<Value>::new()
        );
        assert_eq!(
            tenant_error(remove(&full, false, false)),
            TenantError::HoldsVcons
        );
        assert_eq!(
            tenant_error(remove(&full, true, false)),
            TenantError::UnderHold
        );
        assert_eq!(
            remove(&full, true, true).expect("removed"),
            Vec::<Value>::new()
        );
        assert_eq!(
            s.run(Command::TenantList).expect("listed"),
            Vec::<Value>::new()
        );
        assert_eq!(s.tenant_dirs(), Vec::<String>::new());
    }

    #[test]
    fn keys_are_made_listed_and_revoked() {
        let s = setup();
        let tenant = s.create("acme");
        let made = s
            .run(Command::KeyCreate {
                owner: Owner::Tenant {
                    id: tenant.clone(),
                    scopes: vec!["write".to_owned(), "read".to_owned()],
                },
                label: "producer".to_owned(),
                life: KeyLife::Default,
            })
            .expect("made");
        let id = made[0]["id"].as_str().expect("id").to_owned();
        let token = made[0]["token"].as_str().expect("token").to_owned();
        assert_eq!(made.len(), 1);
        assert_eq!(made[0].as_object().expect("object").len(), 3);
        assert!(
            token.starts_with(&format!("vcs_{id}_")),
            "the token starts with vcs_ and the key's id {id}"
        );
        let operator = s
            .run(Command::KeyCreate {
                owner: Owner::Operator,
                label: "ops".to_owned(),
                life: KeyLife::Default,
            })
            .expect("made")[0]["id"]
            .as_str()
            .expect("id")
            .to_owned();

        let listed = s.run(Command::KeyList { tenant: None }).expect("listed");
        assert_eq!(listed.len(), 2);
        assert_eq!(listed[0]["id"], id.as_str());
        assert_eq!(listed[0]["tenant"], tenant.as_str());
        assert_eq!(listed[0]["scopes"], json!(["write", "read"]));
        assert_eq!(listed[0]["label"], "producer");
        assert_eq!(listed[0]["revoked_at"], Value::Null);
        assert!(
            listed[0]["created_at"]
                .as_str()
                .is_some_and(|t| t.ends_with('Z'))
        );
        assert!(
            listed.iter().all(|k| k.get("token").is_none()),
            "never a token"
        );
        assert_eq!(listed[1]["id"], operator.as_str());
        assert_eq!(listed[1]["tenant"], Value::Null);
        assert_eq!(listed[1]["scopes"], json!(["admin"]));

        let own = s
            .run(Command::KeyList {
                tenant: Some(tenant.clone()),
            })
            .expect("listed");
        assert_eq!(own, listed[..1]);

        assert_eq!(
            s.run(Command::KeyRevoke { id: id.clone() })
                .expect("revoked"),
            Vec::<Value>::new()
        );
        let listed = s.run(Command::KeyList { tenant: None }).expect("listed");
        assert!(listed[0]["revoked_at"].as_str().is_some(), "{listed:?}");
        assert_eq!(listed[1]["revoked_at"], Value::Null);
    }

    #[test]
    fn bad_scopes_and_unknown_keys_are_refused() {
        let s = setup();
        let tenant = s.create("acme");
        for scopes in [
            vec!["superuser"],
            vec!["read", "superuser"],
            vec!["read", "read"],
            vec!["admin"],
            vec![],
        ] {
            let error = s.run(Command::KeyCreate {
                owner: Owner::Tenant {
                    id: tenant.clone(),
                    scopes: scopes.iter().map(|s| (*s).to_owned()).collect(),
                },
                label: "l".to_owned(),
                life: KeyLife::Default,
            });
            assert_eq!(tenant_error(error), TenantError::BadScopes, "{scopes:?}");
        }
        assert_eq!(
            s.run(Command::KeyList { tenant: None }).expect("listed"),
            Vec::<Value>::new()
        );
        for id in ["0123456789abcdef".to_owned(), "nope".to_owned()] {
            assert!(
                matches!(
                    s.run(Command::KeyRevoke { id }),
                    Err(CommandError::UnknownKey)
                ),
                "unknown"
            );
        }
    }

    #[test]
    fn audit_export_writes_either_log_from_a_sequence_number() {
        let s = setup();
        let tenant = s.create("acme");
        s.run(Command::TenantList).expect("listed");
        s.run(Command::KeyCreate {
            owner: Owner::Tenant {
                id: tenant.clone(),
                scopes: vec!["read".to_owned()],
            },
            label: "l".to_owned(),
            life: KeyLife::Default,
        })
        .expect("made");

        let admin = s
            .run(Command::AuditExport {
                log: Log::Admin,
                since: None,
            })
            .expect("exported");
        let actions: Vec<&str> = admin
            .iter()
            .map(|r| r["action"].as_str().expect("action"))
            .collect();
        assert_eq!(actions, ["tenant_create", "tenant_list", "key_create"]);
        assert!(admin.iter().all(|r| r["actor"] == "cli"));
        let since = admin[0]["seq"].as_i64().expect("seq");
        let later = s
            .run(Command::AuditExport {
                log: Log::Admin,
                since: Some(since),
            })
            .expect("exported");
        assert_eq!(later, admin[1..]);

        let own = s
            .run(Command::AuditExport {
                log: Log::Tenant(tenant),
                since: None,
            })
            .expect("exported");
        let actions: Vec<&str> = own
            .iter()
            .map(|r| r["action"].as_str().expect("action"))
            .collect();
        assert_eq!(actions, ["key_create"]);
    }

    #[test]
    fn rekey_tenant_rotates_the_key_and_prints_nothing() {
        let s = setup();
        let id = s.create("acme");
        let tenant = TenantId::parse(&id).expect("id");
        let before = s.tenants().tenant_key_id_for_test(tenant);
        assert_eq!(
            s.run(Command::RekeyTenant { id: id.clone() })
                .expect("rekeyed"),
            Vec::<Value>::new()
        );
        assert_ne!(s.tenants().tenant_key_id_for_test(tenant), before);
        assert_eq!(
            tenant_error(s.run(Command::RekeyTenant {
                id: TenantId::generate().to_string()
            })),
            TenantError::NotFound
        );
        let server = datadir::lock(&s.config.data_dir, Sharing::Serve).expect("a server");
        assert!(matches!(
            s.run(Command::RekeyTenant { id }),
            Err(CommandError::DataDir(LockError::InUse(Sharing::Alone)))
        ));
        drop(server);
    }

    #[test]
    fn rekey_master_moves_the_data_to_the_new_master_key() {
        let mut s = setup();
        s.create("acme");
        let new_file = s.config.master_key.with_file_name("new.key");
        std::fs::write(&new_file, [0x5c; 32]).expect("new key");
        chmod(&new_file, 0o600);
        assert!(matches!(
            s.run(Command::RekeyMaster {
                new_master: s.config.master_key.clone()
            }),
            Err(CommandError::SameMasterKey)
        ));
        let server = datadir::lock(&s.config.data_dir, Sharing::Serve).expect("a server");
        assert!(matches!(
            s.run(Command::RekeyMaster {
                new_master: new_file.clone()
            }),
            Err(CommandError::DataDir(LockError::InUse(Sharing::Alone)))
        ));
        drop(server);
        let out = s
            .run(Command::RekeyMaster {
                new_master: new_file.clone(),
            })
            .expect("rotated");
        let new_id = Key::load_master(&new_file).expect("key").id().to_string();
        assert_eq!(out.len(), 1);
        assert_eq!(out[0]["master_key_id"], new_id.as_str());
        assert!(
            out[0]["next"]
                .as_str()
                .is_some_and(|n| n.contains("destroy"))
        );
        assert!(matches!(
            s.run(Command::TenantList),
            Err(CommandError::WrongMasterKey)
        ));
        s.config.master_key = new_file;
        assert_eq!(s.run(Command::TenantList).expect("listed").len(), 1);
    }

    mod check {
        use time::macros::datetime;

        use super::*;
        use crate::ids::VersionId;
        use crate::store::{Examination, StoreError};

        const NOW: OffsetDateTime = datetime!(2026-10-25 12:00 UTC);
        const HOUR: std::time::Duration = std::time::Duration::from_secs(3600);

        fn sound() -> Examination {
            Examination {
                versions: 3,
                packed: 0,
                damaged: vec![],
                leftovers: vec!["a.tmp".to_owned()],
                due: 0,
                oldest_due_since: None,
                purges_since_key: 4,
                oldest_purge_since_key: Some(datetime!(2026-10-01 00:00 UTC)),
            }
        }

        fn tenant(store: Result<Examination, Unexamined>, stopped: bool) -> TenantExamination {
            TenantExamination {
                id: TenantId([1; 16]),
                name: "acme".to_owned(),
                rotation_stopped: stopped,
                store,
            }
        }

        #[test]
        fn leftovers_and_work_waiting_for_rotation_are_not_problems() {
            assert_eq!(
                problems(&tenant(Ok(sound()), false), HOUR, NOW, true),
                Vec::<String>::new()
            );
        }

        #[test]
        fn each_problem_is_named() {
            let damaged = Examination {
                damaged: vec![VersionId([2; 16]), VersionId([3; 16])],
                ..sound()
            };
            assert_eq!(
                problems(&tenant(Ok(damaged), false), HOUR, NOW, true),
                [
                    "version 02020202020202020202020202020202 is missing or does not open",
                    "version 03030303030303030303030303030303 is missing or does not open",
                ],
                "one line per damaged version, in its order"
            );
            assert_eq!(
                problems(&tenant(Ok(sound()), true), HOUR, NOW, true),
                ["a key rotation was stopped part way; run rekey tenant again"],
                "a stopped rotation is named with the command that finishes it"
            );
            assert_eq!(
                problems(
                    &tenant(
                        Err(Unexamined::Open(OpenFailure::Refused(TenantError::Io))),
                        false
                    ),
                    HOUR,
                    NOW,
                    true,
                ),
                ["its catalog cannot be opened: Io"],
                "a refused catalog names the refusal, not a key"
            );
        }

        /// Only a catalog its key does not open is a key problem; one the
        /// process may not open is named with its path and the system's
        /// error.
        #[test]
        fn a_key_problem_and_an_access_problem_are_told_apart() {
            let key = problems(
                &tenant(Err(Unexamined::Open(OpenFailure::WrongKey)), false),
                HOUR,
                NOW,
                true,
            );
            assert_eq!(key, ["its catalog does not open under its tenant key"]);
            let access = OpenFailure::Access {
                path: std::path::PathBuf::from("/srv/data/tenants/01/catalog.sqlite"),
                error: std::io::Error::from_raw_os_error(libc::EACCES),
            };
            assert_eq!(
                problems(
                    &tenant(Err(Unexamined::Open(access)), false),
                    HOUR,
                    NOW,
                    true
                ),
                [
                    "its catalog cannot be opened: /srv/data/tenants/01/catalog.sqlite: Permission denied (os error 13)"
                ]
            );
        }

        /// Packed versions with no `[storage.parquet]` in the
        /// configuration are a problem, named with the setting; with the
        /// table, or with nothing packed, they are not.
        #[test]
        fn packed_versions_without_storage_parquet_are_a_problem() {
            let packed = Examination {
                packed: 2,
                ..sound()
            };
            assert_eq!(
                problems(&tenant(Ok(packed.clone()), false), HOUR, NOW, false),
                [
                    "2 version(s) are packed into Parquet files, and the configuration has no [storage.parquet]: serve refuses to start until the table is put back"
                ],
                "without the table"
            );
            assert_eq!(
                problems(&tenant(Ok(packed), false), HOUR, NOW, true),
                Vec::<String>::new(),
                "with the table"
            );
            assert_eq!(
                problems(&tenant(Ok(sound()), false), HOUR, NOW, false),
                Vec::<String>::new(),
                "nothing packed"
            );
        }

        /// A Store that opened and whose objects the storage did not read
        /// is named as that, with the error's kind and the system's error
        /// number when there is one, and never as a catalog that does not
        /// open; any other failed examination says so with its error.
        #[test]
        fn objects_that_cannot_be_read_are_told_apart_from_a_catalog() {
            let unread = |kind, os_error| {
                problems(
                    &tenant(
                        Err(Unexamined::Examine(ExamineError::Objects {
                            kind,
                            os_error,
                        })),
                        false,
                    ),
                    HOUR,
                    NOW,
                    true,
                )
            };
            assert_eq!(
                unread(std::io::ErrorKind::PermissionDenied, Some(libc::EACCES)),
                ["its objects cannot be read: permission denied (os error 13)"],
                "a file the process may not read"
            );
            assert_eq!(
                unread(std::io::ErrorKind::TimedOut, None),
                ["its objects cannot be read: timed out"],
                "a service's failure has a kind and no system error number"
            );
            assert_eq!(
                problems(
                    &tenant(
                        Err(Unexamined::Examine(ExamineError::Failed(StoreError::Io))),
                        false
                    ),
                    HOUR,
                    NOW,
                    true,
                ),
                ["it could not be examined: Io"],
                "any other failure, with its error"
            );
        }

        #[test]
        fn a_sweep_is_stalled_only_once_a_version_waited_longer_than_the_interval() {
            let waited = |since| Examination {
                due: 1,
                oldest_due_since: Some(since),
                ..sound()
            };
            let ok = waited(datetime!(2026-10-25 11:00 UTC));
            assert_eq!(
                problems(&tenant(Ok(ok), false), HOUR, NOW, true),
                Vec::<String>::new()
            );
            let stalled = waited(datetime!(2026-10-25 10:59:59 UTC));
            assert_eq!(
                problems(&tenant(Ok(stalled), false), HOUR, NOW, true),
                [
                    "1 version(s) due for purge, the oldest longer than purge_interval: the sweep is not keeping up"
                ],
                "one second past purge_interval is a stalled sweep"
            );
        }

        #[test]
        fn check_reports_each_tenant_and_a_summary_and_exits_1_on_a_problem() {
            let s = setup();
            let id = s.create("acme");
            let out = s.run(Command::Check).expect("nothing wrong");
            assert_eq!(out.len(), 2);
            assert_eq!(out[0]["tenant"], id.as_str());
            assert_eq!(out[0]["problems"], json!([]));
            assert_eq!(out[1]["tenants"], 1);
            assert_eq!(out[1]["problems"], 0);

            let tenant = TenantId::parse(&id).expect("id");
            let version = {
                let tenants = s.tenants();
                let store = tenants.store(tenant).expect("store");
                let (found, body) = vcon(UUID, "kept");
                let Ok(crate::store::Created::New(v)) =
                    store.create(&Caller { key: KeyId([1; 8]) }, &found, &body)
                else {
                    panic!("stored")
                };
                v
            };
            let objects = s.config.data_dir.join("tenants").join(&id).join("objects");
            std::fs::remove_file(objects.join(version.to_string())).expect("remove");
            std::fs::write(objects.join(format!("{version}.tmp")), b"x").expect("leftover");
            let mut printed = Vec::new();
            let found = run(&s.config, &Command::Check, &mut printed);
            assert!(
                matches!(found, Err(CommandError::CheckFound(1))),
                "{found:?}"
            );
            let lines: Vec<Value> = String::from_utf8(printed)
                .expect("text")
                .lines()
                .map(|l| serde_json::from_str(l).expect("json"))
                .collect();
            assert_eq!(lines[0]["damaged"], json!([version.to_string()]));
            assert_eq!(lines[0]["leftovers"], json!([format!("{version}.tmp")]));
            assert_eq!(lines[1]["problems"], 1);
            assert!(
                objects.join(format!("{version}.tmp")).exists(),
                "check reports leftovers; it does not remove them"
            );
            let server = datadir::lock(&s.config.data_dir, Sharing::Serve).expect("a server");
            assert!(matches!(
                s.run(Command::Check),
                Err(CommandError::DataDir(LockError::InUse(Sharing::Alone)))
            ));
            drop(server);
        }
    }

    /// The commands that run alone are refused while a server holds the
    /// data directory; the others run beside it.
    #[test]
    fn beside_a_server_only_the_beside_commands_run() {
        let s = setup();
        let tenant = s.create("acme");
        let server = datadir::lock(&s.config.data_dir, Sharing::Serve).expect("a server");
        for command in [
            Command::TenantCreate {
                name: "beta".to_owned(),
                retention: None,
            },
            Command::TenantRemove {
                id: tenant.clone(),
                force: true,
                override_holds: false,
            },
        ] {
            assert!(
                matches!(
                    s.run(command.clone()),
                    Err(CommandError::DataDir(LockError::InUse(Sharing::Alone)))
                ),
                "{command:?}"
            );
        }
        s.run(Command::TenantList).expect("beside");
        s.run(Command::TenantSetRetention {
            id: tenant.clone(),
            retention: "1y".to_owned(),
        })
        .expect("beside");
        s.run(Command::KeyList { tenant: None }).expect("beside");
        let made = s
            .run(Command::KeyCreate {
                owner: Owner::Operator,
                label: "l".to_owned(),
                life: KeyLife::Default,
            })
            .expect("beside");
        s.run(Command::KeyRevoke {
            id: made[0]["id"].as_str().expect("an id").to_owned(),
        })
        .expect("beside");
        s.run(Command::AuditExport {
            log: Log::Admin,
            since: None,
        })
        .expect("beside");
        s.run(Command::Backup {
            to: s._dir.path().join("backup"),
        })
        .expect("beside");
        drop(server);
        assert_eq!(s.run(Command::TenantList).expect("listed").len(), 1);
    }

    /// A command beside a server opens without recovery, so a create the
    /// server has in progress stays; one that runs alone recovers.
    #[test]
    fn only_a_command_that_runs_alone_recovers() {
        let s = setup();
        s.create("acme");
        let in_progress = TenantId::generate().to_string();
        std::fs::create_dir(s.config.data_dir.join("tenants").join(&in_progress))
            .expect("a directory with no row");
        s.run(Command::TenantList).expect("listed");
        s.run(Command::KeyList { tenant: None }).expect("listed");
        assert!(
            s.tenant_dirs().contains(&in_progress),
            "left by beside commands"
        );
        s.create("beta");
        assert!(
            !s.tenant_dirs().contains(&in_progress),
            "removed by an alone command"
        );
    }

    #[test]
    fn a_master_key_other_users_may_read_stops_every_command() {
        let s = setup();
        s.create("acme");
        chmod(&s.config.master_key, 0o644);
        let error = s.run(Command::TenantList).expect_err("refused");
        let path = s.config.master_key.display();
        assert_eq!(
            error.to_string(),
            format!(
                "the master key cannot be read: master_key {path}: other users may read or \
                 change the file (mode 0644); run `chmod 600 {path}`, or `chmod 640 {path}` for \
                 a group that only reads it"
            ),
            "the command stops with the rule's text, naming the key, the file and the fix"
        );
    }

    #[test]
    fn rekey_master_refuses_a_new_key_other_users_may_read_under_its_option() {
        let s = setup();
        s.create("acme");
        let new_file = s.config.master_key.with_file_name("new.key");
        std::fs::write(&new_file, [0x5c; 32]).expect("new key");
        chmod(&new_file, 0o644);
        let error = s
            .run(Command::RekeyMaster {
                new_master: new_file.clone(),
            })
            .expect_err("refused");
        assert!(
            error
                .to_string()
                .contains(&format!("--new-master {}: other users", new_file.display())),
            "{error}"
        );
    }

    #[cfg(feature = "s3")]
    #[test]
    fn an_export_secret_other_users_may_read_is_refused_under_export_s3() {
        let s = setup();
        let secret = s.config.master_key.with_file_name("export.secret");
        std::fs::write(&secret, "k3y-0123\n").expect("secret");
        chmod(&secret, 0o644);
        let mut config = s.config.clone();
        config.export = crate::config::parse(&format!(
            "listen = \"127.0.0.1:1\"\ndata_dir = \"/d\"\nmaster_key = \"/m\"\n\
             [tls]\ncert = \"c\"\nkey = \"k\"\n\
             [export.s3]\nendpoint = \"http://127.0.0.1:9\"\nregion = \"eu-west-3\"\n\
             access_key_id = \"id\"\nsecret_access_key_file = {secret:?}\nallow_http = true\n"
        ))
        .expect("a configuration")
        .export;
        let error = s3_target(&config, "vcon", "").err().expect("refused");
        let text = error.to_string();
        assert!(
            text.contains(&format!(
                "export.s3.secret_access_key_file {}: other users",
                secret.display()
            )),
            "{text}"
        );
        assert!(!text.contains("k3y"), "{text}");
    }

    /// A user or membership command that fails in the registry says what
    /// every other command says for the same failure.
    #[test]
    fn a_user_commands_registry_failure_is_the_registrys_error() {
        for (failure, words) in [
            (
                TenantError::AuditFailed,
                "the audit record could not be written",
            ),
            (
                TenantError::WrongKey,
                "is not the one server.sqlite was made under",
            ),
        ] {
            let error = CommandError::from(UserError::Registry(failure));
            assert!(
                matches!(error, CommandError::Tenant(e) if e == failure),
                "{failure:?} is carried as the registry's error: {error:?}"
            );
            assert!(error.to_string().contains(words), "{error}");
        }
        assert!(
            matches!(
                CommandError::from(UserError::NoUser),
                CommandError::User(UserError::NoUser)
            ),
            "a refusal stays a user error"
        );
    }

    #[test]
    fn a_wrong_master_key_is_named() {
        let s = setup();
        s.create("acme");
        std::fs::write(&s.config.master_key, [0x5b; 32]).expect("another key");
        let error = s.run(Command::TenantList).expect_err("refused");
        assert!(matches!(error, CommandError::WrongMasterKey), "{error:?}");
        assert!(error.to_string().contains("master key"), "{error}");
        std::fs::write(&s.config.master_key, [0x5a; 31]).expect("short key");
        let error = s.run(Command::TenantList).expect_err("refused");
        assert!(matches!(error, CommandError::MasterKey(_)), "{error:?}");
    }

    #[test]
    fn backup_prints_what_it_copied_and_check_finds_the_copy_whole() {
        let s = setup();
        s.create("acme");
        s.create("globex");
        let to = s._dir.path().join("backup");
        let out = s.run(Command::Backup { to: to.clone() }).expect("backs up");
        assert_eq!(
            out,
            [serde_json::json!({ "tenants": 2, "versions": 0, "tries": 1 })]
        );
        let copy = Setup {
            _dir: tempfile::tempdir().expect("temp dir"),
            config: Config {
                data_dir: to,
                ..s.config.clone()
            },
        };
        let checked = copy.run(Command::Check).expect("check finds no problem");
        assert_eq!(checked.last().expect("a summary")["problems"], 0);
        let names: Vec<_> = copy
            .run(Command::TenantList)
            .expect("lists")
            .iter()
            .map(|t| t["name"].clone())
            .collect();
        assert_eq!(names, ["acme", "globex"]);
    }

    #[test]
    fn a_backup_path_already_there_is_refused() {
        let s = setup();
        let to = s._dir.path().join("backup");
        std::fs::create_dir(&to).expect("dir");
        let error = s.run(Command::Backup { to }).expect_err("refused");
        assert!(
            matches!(error, CommandError::Backup(BackupError::Exists)),
            "{error:?}"
        );
    }

    /// Words the message of `error` must hold to say what went wrong. The
    /// matches have no catch-all arm, so a new error fails to compile here
    /// until its words are given.
    fn says(error: &CommandError) -> &'static str {
        match error {
            CommandError::DataDir(_) => "the data directory is in use",
            CommandError::MasterKey(_) => "the master key cannot be read",
            // The step, the file and the error under it, all three.
            CommandError::Open(_) => "opening /srv/data/server.sqlite failed: disk I/O error",
            CommandError::WrongMasterKey => "is not the one server.sqlite was made under",
            CommandError::WrongDatabaseKey => {
                "is not the one the registry in the catalog database was made under"
            }
            CommandError::BadPeriod(_) => "\"7w\" is not a period",
            CommandError::UnknownKey => "no key has this id",
            CommandError::User(e) => match e {
                UserError::CliOnly => "command line",
                UserError::BadLabel => "a user label is",
                UserError::BadIdentity => "an issuer is an https:// URL",
                UserError::IdentityTaken => "an active user has this issuer and subject",
                UserError::NoUser => "no user has this id",
                UserError::Deactivated => "the user is deactivated",
                UserError::NoTenant => "no tenant has this id",
                UserError::Closing => "being removed",
                UserError::BadRoles => "reader, producer, records, legal, auditor and tenant-admin",
                UserError::OverCap => "outside the tenant's role cap",
                UserError::BadCap => "a role cap is one or more of",
                UserError::NoMembership => "the user has no such membership",
                UserError::Registry(_) => "could not be read or written",
            },
            CommandError::Output(_) => "the output could not be written",
            CommandError::SameMasterKey => "already in use",
            CommandError::CheckFound(_) => "check found 2 problem(s)",
            CommandError::Storage(_) => "the storage cannot be used: bucket \"vcon\"",
            CommandError::BadLevel(_) => "\"most\" is not a metadata level",
            CommandError::BadTime(_) => "\"yesterday\" is not an RFC 3339 time",
            CommandError::BadTarget(_) => "\"s3://\" is not a directory or an s3://bucket/prefix",
            CommandError::NoS3 => "[export.s3] or [storage.s3]",
            CommandError::Export(_) => "another export wrote",
            CommandError::Move { .. } => "running storage move-objects again moves the rest",
            CommandError::Backup(e) => match e {
                BackupError::Exists => "already exists",
                BackupError::KeptChanging => "try again",
                BackupError::Io => "could not be written",
                BackupError::AuditFailed => "audit record",
                BackupError::NoFileBackup => "back up the catalog database with pg_dump",
            },
            CommandError::Tenant(e) => match e {
                TenantError::NotFound => "no tenant",
                TenantError::NameTaken => "another tenant has this name",
                TenantError::BadName => "a tenant name is",
                TenantError::HoldsVcons => "--force",
                TenantError::UnderHold => "--override-holds",
                TenantError::LimitReached => "rekey master",
                TenantError::AuditFailed => "audit record",
                TenantError::Closing => "being removed",
                TenantError::CliOnly => "command line",
                TenantError::BadScopes => "scopes",
                TenantError::BadExpiry => "1 to 365 days",
                TenantError::BadSetting(_) => "setting",
                // Display folds a registry made under another master key
                // into WrongMasterKey's message.
                TenantError::WrongKey => "is not the one server.sqlite was made under",
                TenantError::NoFileBackup => "back up the catalog database with pg_dump",
                TenantError::Io => "could not be read or written",
            },
        }
    }

    /// With the objects kept in the data directory there is no bucket to
    /// move them to: the command is refused, naming the setting.
    #[test]
    fn storage_move_objects_needs_a_bucket() {
        let s = setup();
        s.create("acme");
        let error = s.run(Command::StorageMoveObjects).expect_err("refused");
        assert!(
            error.to_string().contains("storage.objects = \"s3\""),
            "names the setting: {error}"
        );
    }

    /// Against the test service: a data directory's objects move to the
    /// bucket, byte for byte; the vCon then reads back from the bucket, no
    /// object file is left, and a second run moves nothing.
    #[cfg(feature = "backend-tests")]
    #[test]
    fn storage_move_objects_moves_each_tenants_objects_to_the_bucket() {
        use crate::backend::Objects;
        let s = setup();
        let id = s.create("acme");
        store_one(&s, &id);
        let tenant = TenantId::parse(&id).expect("an id");
        let local = s.config.data_dir.join("tenants").join(&id).join("objects");
        let files = || std::fs::read_dir(&local).expect("listed").count();
        assert_eq!(files(), 1, "one object file");
        let (bucket, mut settings) = crate::backend::s3::remote::bucket(false);
        let secret = crate::backend::s3::remote::secret_file();
        settings.secret_access_key_file = Some(secret.1.clone());
        let mut config = s.config.clone();
        config.storage = crate::config::Storage {
            objects: crate::config::ObjectStore::S3(settings),
            parquet: None,
        };
        let moving = |config: &Config| -> Vec<Value> {
            let mut out = Vec::new();
            run(config, &Command::StorageMoveObjects, &mut out).expect("moved");
            String::from_utf8(out)
                .expect("text")
                .lines()
                .map(|l| serde_json::from_str(l).expect("a JSON line"))
                .collect()
        };
        assert_eq!(
            moving(&config),
            [
                json!({"tenant": id, "moved": 1, "missing": 0}),
                json!({"moved": 1})
            ],
            "one object moved, and nothing missing"
        );
        assert_eq!(files(), 0, "no object file is left");
        assert_eq!(
            bucket.tenant(tenant).list().expect("listed").len(),
            1,
            "the object is in the bucket"
        );
        let tenants = Tenants::open_beside_in(
            &config.data_dir,
            Key::load_master(&config.master_key).expect("master"),
            Arc::new(OsFs),
            Arc::new(SystemClock),
            crate::backend::ObjectPlace::S3(bucket.clone()),
            &crate::backend::Catalogs::Local,
        )
        .expect("opens");
        let (_, body) = vcon(UUID, "exported");
        assert_eq!(
            tenants
                .store(tenant)
                .expect("its store")
                .get(&Caller { key: KeyId([7; 8]) }, UUID.parse().expect("uuid"))
                .expect("reads")
                .body,
            body,
            "the vCon reads back from the bucket"
        );
        drop(tenants);
        assert_eq!(
            moving(&config),
            [
                json!({"tenant": id, "moved": 0, "missing": 0}),
                json!({"moved": 0})
            ],
            "a second run moves nothing"
        );
    }

    /// One vCon stored in the tenant `id`.
    fn store_one(s: &Setup, id: &str) {
        let store = s
            .tenants()
            .store(TenantId::parse(id).expect("an id"))
            .expect("the store");
        let (found, body) = vcon(UUID, "exported");
        store
            .create(&Caller { key: KeyId([7; 8]) }, &found, &body)
            .expect("stored");
    }

    #[test]
    fn tenant_set_metadata_prints_the_level_and_list_shows_it() {
        let s = setup();
        let id = s.create("acme");
        let out = s
            .run(Command::TenantSetMetadata {
                id: id.clone(),
                metadata: "counts".to_owned(),
            })
            .expect("set");
        assert_eq!(
            out,
            [json!({ "id": id, "metadata": "counts" })],
            "the command prints the tenant and its level"
        );
        assert_eq!(
            s.run(Command::TenantList).expect("listed")[0]["metadata"],
            "counts",
            "the list shows the level"
        );
        assert!(
            matches!(
                s.run(Command::TenantSetMetadata {
                    id: id.clone(),
                    metadata: "most".to_owned(),
                }),
                Err(CommandError::BadLevel(text)) if text == "most"
            ),
            "a level that is not one is refused, naming it"
        );
        assert_eq!(
            tenant_error(s.run(Command::TenantSetMetadata {
                id: TenantId::generate().to_string(),
                metadata: "full".to_owned(),
            })),
            TenantError::NotFound,
            "a tenant id no tenant has is not found"
        );
    }

    /// The export runs beside a server, at the tenant's level, and prints
    /// one line; it is refused beside a command that runs alone.
    #[test]
    fn export_parquet_runs_beside_a_server_and_prints_what_it_wrote() {
        let s = setup();
        let id = s.create("acme");
        store_one(&s, &id);
        s.run(Command::TenantSetMetadata {
            id: id.clone(),
            metadata: "counts".to_owned(),
        })
        .expect("set");
        let to = s._dir.path().join("export");
        let export = Command::ExportParquet {
            tenant: id.clone(),
            to: to.display().to_string(),
            since: Some("2000-01-01T00:00:00Z".to_owned()),
            until: None,
        };
        let server = datadir::lock(&s.config.data_dir, Sharing::Serve).expect("a server");
        let out = s.run(export.clone()).expect("exports beside a server");
        drop(server);
        assert_eq!(out.len(), 1, "{out:?}");
        let line = &out[0];
        let manifest = format!("tenant={id}/manifest/00000000000000000001.json");
        assert_eq!(line["tenant"], id, "the line names the tenant");
        assert_eq!(
            line["metadata"], "counts",
            "the export is at the tenant's level"
        );
        assert_eq!(
            line["to"],
            to.display().to_string(),
            "the line names the target"
        );
        assert_eq!(line["manifest"], manifest, "the line names the manifest");
        assert_eq!(line["seq"], 1, "the first export to the target");
        assert_eq!(line["files"], 1, "one store date, one file");
        assert_eq!(line["rows"], 1, "the one vCon stored");
        assert_eq!(line["skipped"], 0, "nothing was purged during the run");
        assert_eq!(line["unread"], 0, "every body was read");
        let m: Value =
            serde_json::from_slice(&std::fs::read(to.join(&manifest)).expect("the manifest"))
                .expect("JSON");
        assert_eq!(m["bytes"], line["bytes"], "the bytes the manifest names");
        assert_eq!(
            m["since"], "2000-01-01T00:00:00Z",
            "the manifest records --since"
        );
        let alone = datadir::lock(&s.config.data_dir, Sharing::Alone).expect("alone");
        assert!(
            matches!(
                s.run(export),
                Err(CommandError::DataDir(LockError::InUse(Sharing::Beside)))
            ),
            "an export does not run beside a command that runs alone"
        );
        drop(alone);
    }

    #[test]
    fn export_parquet_refuses_a_bad_time_target_or_tenant() {
        let s = setup();
        let id = s.create("acme");
        let export = |to: &str, since: Option<&str>, until: Option<&str>| Command::ExportParquet {
            tenant: id.clone(),
            to: to.to_owned(),
            since: since.map(str::to_owned),
            until: until.map(str::to_owned),
        };
        let dir = s._dir.path().join("out").display().to_string();
        assert!(
            matches!(
                s.run(export(&dir, Some("yesterday"), None)),
                Err(CommandError::BadTime(t)) if t == "yesterday"
            ),
            "a word that is not an RFC 3339 time is refused, naming it"
        );
        assert!(
            matches!(
                s.run(export(&dir, None, Some("2026-13-01T00:00:00Z"))),
                Err(CommandError::BadTime(_))
            ),
            "a month 13 is not a time"
        );
        assert!(
            matches!(
                s.run(export("s3://", None, None)),
                Err(CommandError::BadTarget(t)) if t == "s3://"
            ),
            "an s3:// URL with no bucket is refused, naming it"
        );
        assert!(
            matches!(
                s.run(export("s3://reports/vcon", None, None)),
                Err(CommandError::NoS3)
            ),
            "no S3 service is configured"
        );
        assert_eq!(
            tenant_error(s.run(Command::ExportParquet {
                tenant: TenantId::generate().to_string(),
                to: dir.clone(),
                since: None,
                until: None,
            })),
            TenantError::NotFound,
            "an unknown tenant is not found"
        );
        assert!(
            !std::path::Path::new(&dir).exists(),
            "a refused export writes nothing"
        );
    }

    #[test]
    fn each_error_says_what_went_wrong() {
        let tenant = [
            TenantError::NotFound,
            TenantError::NameTaken,
            TenantError::BadName,
            TenantError::HoldsVcons,
            TenantError::UnderHold,
            TenantError::LimitReached,
            TenantError::AuditFailed,
            TenantError::Closing,
            TenantError::CliOnly,
            TenantError::BadScopes,
            TenantError::BadExpiry,
            TenantError::BadSetting(crate::settings::SettingError::UnknownName),
            TenantError::WrongKey,
            TenantError::NoFileBackup,
            TenantError::Io,
        ]
        .map(CommandError::Tenant);
        let backup = [
            BackupError::Exists,
            BackupError::KeptChanging,
            BackupError::Io,
            BackupError::AuditFailed,
            BackupError::NoFileBackup,
        ]
        .map(CommandError::Backup);
        let rest = [
            CommandError::DataDir(LockError::InUse(Sharing::Alone)),
            CommandError::MasterKey(crate::envelope::KeyError::WrongSize),
            CommandError::Open(RegistryError::failed(
                tenants::OpenStep::OpenCatalog,
                std::path::PathBuf::from("/srv/data/server.sqlite"),
                io::Error::other("disk I/O error"),
            )),
            CommandError::WrongMasterKey,
            CommandError::WrongDatabaseKey,
            CommandError::BadPeriod("7w".to_owned()),
            CommandError::UnknownKey,
            CommandError::Output(io::Error::other("closed")),
            CommandError::SameMasterKey,
            CommandError::CheckFound(2),
            CommandError::Storage(StorageError(
                "bucket \"vcon\" cannot be reached or read".to_owned(),
            )),
            CommandError::BadLevel("most".to_owned()),
            CommandError::BadTime("yesterday".to_owned()),
            CommandError::BadTarget("s3://".to_owned()),
            CommandError::NoS3,
            CommandError::Export(crate::export::ExportError::Raced("m".to_owned())),
            CommandError::Move {
                tenant: TenantId([1; 16]),
                why: "the bucket could not be written".to_owned(),
            },
        ];
        let user = [
            UserError::CliOnly,
            UserError::BadLabel,
            UserError::BadIdentity,
            UserError::IdentityTaken,
            UserError::NoUser,
            UserError::Deactivated,
            UserError::NoTenant,
            UserError::Closing,
            UserError::BadRoles,
            UserError::NoMembership,
            UserError::Registry(TenantError::Io),
        ]
        .map(CommandError::User);
        for error in tenant.into_iter().chain(backup).chain(rest).chain(user) {
            let text = error.to_string();
            assert!(text.contains(says(&error)), "{error:?}: {text}");
        }
    }
}