bestool 2.5.0

BES Deployment tooling
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
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
//! Obtaining TLS certificates from canopy, and serving them to Caddy.
//!
//! Canopy holds the account with the authority and proves control of a name
//! through DNS, so this host needs no DNS credential of its own. What it owns
//! is the key, the collecting, and the serving:
//!
//! - a P-256 key per name, generated here and never sent anywhere
//!   ([`bestool_canopy::certificates`]);
//! - a collection loop on a timer, because proving control through DNS takes
//!   far longer than a client waits;
//! - a certificate endpoint Caddy asks during the handshake, answered entirely
//!   from memory ([`delivery`]).
//!
//! Canopy takes precedence over Caddy's own issuance rather than replacing it. A
//! name this holds a chain for is served from canopy; a name it does not is
//! declined, and Caddy issues for it exactly as it would were this not
//! configured at all. That is what lets a host run this before its DNS
//! credential is withdrawn.
//!
//! spec: TLS
//! spec: TLSD

use std::{
	collections::{BTreeMap, BTreeSet},
	path::PathBuf,
	sync::Arc,
	time::Duration,
};

use bestool_alertd::ownership::{self, CaddySites, HostApplication, Ownership};
use bestool_canopy::{
	certificates::{self as certs, KeyPair, KeyStore, StoredRefusal},
	names::{AppEntitlement, Entitlement, Refusal, RefusalKind},
	schema::{ApplicationType, RegisterNameArgs, RegisteredName, RequestCertificateArgs},
};
use futures::future::BoxFuture;
use jiff::Timestamp;
use miette::{IntoDiagnostic as _, Result, WrapErr as _, miette};
use serde_json::{Value, json};
use tokio::sync::{Mutex, RwLock};
use tracing::{debug, info, warn};

use crate::alertd::{
	BackgroundTask, TaskContext, TaskEndpoint, TaskEndpointResponse, tasks::TaskEndpointHandler,
};

/// A report's whole cause chain, joined.
///
/// A bare `Display` gives only the outermost wrap — "asking canopy what this
/// server may do" — which says what was being done and nothing about what went
/// wrong. What an operator needs is the cause underneath it.
fn why(err: &miette::Report) -> String {
	err.chain()
		.map(ToString::to_string)
		.collect::<Vec<_>>()
		.join(": ")
}

pub mod delivery;
pub mod peer;

/// The name the daemon mounts this task's endpoints under.
pub const TASK_NAME: &str = "canopy-names";

/// How often the task wakes.
///
/// The daemon ticks a task at one fixed interval, so both rhythms this needs
/// come from inside: it wakes at this rate and rate-limits its own steady-state
/// work. That sits better with the watchdog, which counts each tick as activity,
/// than doing its own waiting within a run.
const TICK: Duration = Duration::from_secs(60);

/// How often a pass is made when nothing is waiting on one.
///
/// A canopy-issued chain's lifetime is not known before one arrives, so this
/// suits the shortest profile the authority might use rather than any particular
/// one: a six-day chain renewed at a third of its life still gets two days of
/// passes before it matters.
const STEADY_INTERVAL: Duration = Duration::from_secs(30 * 60);

/// The most names a handshake may leave waiting for a pass.
///
/// `wanted` is a backstop between passes, not a discovery route: what a pass
/// acts on comes from Caddy's configuration, which this only anticipates. A site
/// configured for a wildcard makes the name remote input, because Caddy passes
/// the client's own server name through — so the set is bounded, and past the
/// bound a new name is dropped rather than an old one evicted. Evicting would
/// let a stream of invented names push out the one a real client asked for.
const WANTED_LIMIT: usize = 64;

/// The most DNS names a command may have asked for at once.
///
/// What a command asked for is kept until it is collected and no longer served,
/// so this is what stops requests that are never fulfilled from accumulating.
/// A request past the bound is refused rather than an older one evicted, so the
/// operator is told rather than a request already accepted being forgotten.
const EXPLICIT_LIMIT: usize = 64;

/// How soon a name whose order is pending is asked about again.
///
/// Sooner than the steady interval, so an order in flight is collected promptly
/// rather than waiting one out; not every tick, so a name that stays pending for
/// an hour is not asked sixty times.
const PENDING_RETRY: Duration = Duration::from_secs(120);

/// What canopy said about one DNS name, the last time it was asked.
#[derive(Clone, Debug, Default)]
pub struct Order {
	/// `pending`, `issued`, `failed`, or `revoked` as canopy reports an order, or
	/// `undeclared`, `denied`, or `refused` where canopy refused the request.
	pub state: String,
	/// Why the last attempt failed, while canopy is still retrying. Surfaced
	/// rather than retried into.
	pub last_error: Option<String>,
	pub not_after: Option<Timestamp>,
	pub revoked: bool,
	pub key_must_be_replaced: bool,
	pub asked_at: Option<Timestamp>,
	/// The type of the application the request carried.
	pub application_type: Option<String>,
	/// How canopy refused the last request, where it did.
	pub refusal: Option<Refusal>,
}

impl Order {
	fn pending(&self) -> bool {
		self.state == "pending"
	}

	/// Why the order is not going through, from whichever side said so.
	fn reason(&self) -> Option<&str> {
		self.refusal
			.as_ref()
			.map(|refusal| refusal.reason.as_str())
			.or(self.last_error.as_deref())
	}
}

/// Whether a DNS name a command asked for may be ordered now, for the
/// application the command named.
fn explicit_orderable(entitlement: &Entitlement, name: &str, application_type: &str) -> bool {
	entitlement
		.for_type(application_type)
		.is_some_and(|app| app.may_certify(name))
}

/// The grant a command needs from the application it names.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Grant {
	Tls,
	Dns,
}

impl Grant {
	fn held_by(self, app: &AppEntitlement) -> bool {
		match self {
			Self::Tls => app.may_manage_tls,
			Self::Dns => app.may_manage_dns,
		}
	}

	fn held_by_any(self, entitlement: &Entitlement) -> bool {
		match self {
			Self::Tls => entitlement.holds_tls_grant(),
			Self::Dns => entitlement.holds_dns_grant(),
		}
	}

	fn describe(self) -> &'static str {
		match self {
			Self::Tls => "obtain certificates from canopy",
			Self::Dns => "manage its own DNS records",
		}
	}
}

/// Whether a command may act on `name` for the application it names, before
/// canopy is asked.
///
/// The application named is the one whose pause, grant, and domains apply,
/// tested in the order canopy tests them. A type the machine hosts no
/// application of is refused, naming the types it does host; a machine canopy
/// answers for as one application matches whatever type is named, as canopy
/// does. A command naming no application, which only a withdrawal does, is
/// tested against the machine as a whole.
///
/// spec: NAM#how-canopy-resolves-a-request
fn permit(
	entitlement: &Entitlement,
	name: &str,
	application_type: Option<&str>,
	grant: Grant,
) -> Result<()> {
	let named = match application_type {
		Some(t) => match entitlement.for_type(t) {
			Some(app) => Some((t, app)),
			None => {
				let hosted: Vec<&str> = entitlement
					.applications
					.iter()
					.filter_map(|app| app.type_slug.as_deref())
					.collect();
				return Err(miette!(
					"this machine hosts no {t} application; it hosts {}",
					if hosted.is_empty() {
						"none canopy knows of".to_owned()
					} else {
						hosted.join(", ")
					}
				));
			}
		},
		None => None,
	};
	match named {
		Some((application_type, app)) => {
			if app.paused {
				return Err(miette!(
					"the {application_type} application is paused in canopy; nothing is asked for until the pause is lifted"
				));
			}
			if !grant.held_by(app) {
				return Err(miette!(
					"the {application_type} application may not {}",
					grant.describe()
				));
			}
			if !app.covers(name) {
				return Err(miette!(
					"{name} is not within a domain the {application_type} application's group controls"
				));
			}
		}
		None => {
			if !grant.held_by_any(entitlement) {
				return Err(miette!("this server may not {}", grant.describe()));
			}
			if !entitlement.covers(name) {
				return Err(miette!(
					"{name} is not within a domain this server's group controls"
				));
			}
		}
	}
	Ok(())
}

/// The order state a DNS name canopy refused shows as.
fn refusal_state(kind: RefusalKind) -> &'static str {
	match kind {
		RefusalKind::Undeclared => "undeclared",
		RefusalKind::Denied => "denied",
		RefusalKind::Conflict | RefusalKind::Other => "refused",
	}
}

/// A DNS name the daemon asks canopy about, and the application it asks for.
#[derive(Clone, Debug, PartialEq, Eq)]
struct Target {
	name: String,
	/// The type of the application the DNS name belongs to, which the request
	/// carries.
	application_type: String,
}

/// Why asking canopy about a DNS name did not go through.
struct Failure {
	/// How canopy refused, where it answered with a refusal.
	refusal: Option<Refusal>,
	report: miette::Report,
}

impl From<miette::Report> for Failure {
	fn from(report: miette::Report) -> Self {
		Self {
			refusal: None,
			report,
		}
	}
}

/// A chain this host holds and can serve, with the key it covers.
///
/// Held in memory because the endpoint answering with it sits on the TLS
/// handshake path: it reaches no network, opens no key store and reads no file.
#[derive(Clone)]
pub struct Held {
	pub chain: String,
	pub key_pem: String,
	pub not_after: Option<Timestamp>,
	/// Whether the chain can be served: canopy reports it neither revoked nor
	/// past its expiry.
	pub usable: bool,
	pub revoked: bool,
}

impl std::fmt::Debug for Held {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		f.debug_struct("Held")
			.field("not_after", &self.not_after)
			.field("usable", &self.usable)
			.field("revoked", &self.revoked)
			.finish_non_exhaustive()
	}
}

/// Everything the collection loop and the delivery endpoint share.
pub struct CertificateState {
	/// Where the key store and the collected chains live.
	dir: PathBuf,
	/// Who may fetch a certificate, beyond the superuser.
	permitted: peer::Permitted,

	/// What can be served right now. Read on the handshake path, so nothing
	/// here is loaded lazily.
	held: RwLock<BTreeMap<String, Held>>,
	/// What canopy last said this server may do. `None` until the first ask.
	entitlement: RwLock<Option<Entitlement>>,
	/// What canopy last said about each name asked about.
	orders: RwLock<BTreeMap<String, Order>>,
	/// DNS names a handshake asked for that nothing is held for, so an order
	/// follows. A backstop between passes rather than a discovery route: Caddy
	/// only asks for DNS names it is configured to serve, which a pass reads from
	/// the same configuration.
	wanted: Mutex<BTreeSet<String>>,
	/// DNS names a command asked for, with the application type it named.
	/// Ordered whether or not Caddy serves them or their site names the daemon's
	/// endpoint, until the first chain arrives and then while Caddy serves them.
	/// Kept on disk, so a restart does not forget a request still waiting.
	explicit: Mutex<BTreeMap<String, String>>,
	/// The requests last written to disk, so a pass writes only on a change.
	persisted_requests: Mutex<BTreeMap<String, String>>,
	/// The refusals last written to disk, so a pass writes only on a change.
	persisted_refusals: Mutex<BTreeMap<String, StoredRefusal>>,
	/// The applications on this host, as last discovered. Discovering them reads
	/// the Tamanu install and may reach its container runtime or database, so a
	/// steady pass does it and the passes between reuse what it found.
	applications: RwLock<Option<Vec<HostApplication>>>,

	/// The keys, held across passes so scrypt is not re-run every tick. Loaded
	/// on the first pass and written through on change.
	keys: Mutex<Option<KeyStore>>,
	/// When the last steady-state pass ran.
	last_pass: RwLock<Option<Timestamp>>,
	/// When a pass last stood down, where the last one did.
	///
	/// A handshake records a name whatever this server may do, and a server
	/// standing down holds no chains — so every handshake it declines records
	/// one. Without remembering the stand-down, those names would wake a pass,
	/// and an entitlement request with it, on every tick until the grant
	/// returns.
	stood_down: RwLock<Option<Timestamp>>,
	/// When a pass last could not establish what the host serves, where the last
	/// one could not: Caddy's configuration or the applications on the host
	/// could not be read.
	///
	/// Such a pass orders only what a command asked for, so a handshake's name
	/// gets no attempt recorded and would otherwise wake a pass on every tick.
	incomplete: RwLock<Option<Timestamp>>,
	/// What went wrong on the last pass, where something did.
	last_error: RwLock<Option<String>>,
}

impl CertificateState {
	pub fn new(dir: PathBuf, permitted: peer::Permitted) -> Self {
		Self {
			dir,
			permitted,
			held: RwLock::new(BTreeMap::new()),
			entitlement: RwLock::new(None),
			orders: RwLock::new(BTreeMap::new()),
			wanted: Mutex::new(BTreeSet::new()),
			explicit: Mutex::new(BTreeMap::new()),
			persisted_requests: Mutex::new(BTreeMap::new()),
			persisted_refusals: Mutex::new(BTreeMap::new()),
			applications: RwLock::new(None),
			keys: Mutex::new(None),
			last_pass: RwLock::new(None),
			stood_down: RwLock::new(None),
			incomplete: RwLock::new(None),
			last_error: RwLock::new(None),
		}
	}

	pub fn permitted(&self) -> peer::Permitted {
		self.permitted
	}

	/// The chain and key to serve for `name`, or `None` to decline.
	///
	/// Declines for a name outside the domains the group controls, a name with
	/// nothing collected yet, and a chain that is no longer usable because it
	/// has been revoked or has expired. Every one of those hands the handshake
	/// back to Caddy, which issues for the name itself.
	///
	/// spec: TLSD#declining-and-failing
	pub async fn serve(&self, name: &str) -> Option<(String, String)> {
		let name = name.trim_end_matches('.').to_ascii_lowercase();

		// What the endpoint serves turns on the chain in hand rather than on
		// what the server may currently ask for: a withdrawn grant and a pause
		// both stop new requests without taking a collected chain out of
		// service. The domain test is separate — a name outside the group's
		// domains was never this server's to serve.
		if let Some(entitlement) = self.entitlement.read().await.as_ref()
			&& !entitlement.covers(&name)
		{
			return None;
		}

		let held = self.held.read().await;
		let chain = held.get(&name)?;
		// `usable` is what canopy last said, or what the chain's own dates said
		// when it was loaded; neither is re-evaluated as time passes. So the
		// expiry is tested here too: where collection has stopped — canopy
		// unreachable, or a pass standing down — the cached flag would otherwise
		// have the daemon hand Caddy an expired chain indefinitely.
		let live = chain.usable && chain.not_after.is_none_or(|at| at > Timestamp::now());
		live.then(|| (chain.chain.clone(), chain.key_pem.clone()))
	}

	/// Record a name a handshake asked for and nothing was held for, so an order
	/// follows on the next pass.
	///
	/// Subject to the same entitlement test as a name read from Caddy's
	/// configuration, so a handshake cannot conjure an order for a name outside
	/// the server's reach.
	///
	/// spec: TLS#which-dns-names-are-certified
	pub async fn note_wanted(&self, name: &str) {
		let name = name.trim_end_matches('.').to_ascii_lowercase();
		if certs::plausible_name(&name).is_err() {
			return;
		}
		if self.held.read().await.contains_key(&name) {
			return;
		}

		let mut wanted = self.wanted.lock().await;
		if wanted.len() >= WANTED_LIMIT && !wanted.contains(&name) {
			debug!(
				name,
				"not recording a name asked for during a handshake; too many are already waiting"
			);
			return;
		}
		if wanted.insert(name.clone()) {
			debug!(name, "recorded a name asked for during a handshake");
		}
	}

	/// Record a DNS name a command asked for, with the application type it named.
	///
	/// spec: TLS#which-dns-names-are-certified
	async fn note_explicit(&self, name: &str, application_type: String) -> Result<()> {
		// What a restart kept is read first, so the bound counts it and the write
		// below does not replace it.
		self.load_from_disk().await?;
		{
			let mut explicit = self.explicit.lock().await;
			if explicit.len() >= EXPLICIT_LIMIT && !explicit.contains_key(name) {
				return Err(miette!(
					"{EXPLICIT_LIMIT} DNS names requested by command are already being collected; no more can be requested until some are"
				));
			}
			explicit.insert(name.to_owned(), application_type);
		}
		self.persist_requests().await;
		Ok(())
	}

	/// Keep the DNS names a command asked for, where they changed.
	///
	/// spec: TLS#which-dns-names-are-certified
	async fn persist_requests(&self) {
		let now = self.explicit.lock().await.clone();
		let mut persisted = self.persisted_requests.lock().await;
		if *persisted == now {
			return;
		}
		match certs::store_requests(&self.dir, &now).await {
			Ok(()) => *persisted = now,
			Err(err) => warn!(%err, "could not keep the DNS names requested by command"),
		}
	}

	/// Load what this host already holds, so a restart serves its chains at once
	/// and collects an order already placed rather than placing a new one.
	///
	/// spec: TLS#keys
	async fn load_from_disk(&self) -> Result<()> {
		let mut keys = self.keys.lock().await;
		if keys.is_some() {
			return Ok(());
		}

		let store = certs::load_keys(&self.dir).await?;
		let chains = certs::load_chains(&self.dir).await?;

		let mut held = self.held.write().await;
		for (name, chain) in chains {
			let Some(key_pem) = store.key_pem(&name) else {
				// A chain whose key is gone cannot be served: the key is half of
				// what the endpoint answers with, and canopy keys what it holds
				// by name and key, so this one is asked for afresh.
				debug!(name, "holding a chain with no key; it will be re-ordered");
				continue;
			};
			let not_after =
				certs::chain_not_after(&chain).and_then(|secs| Timestamp::from_second(secs).ok());
			held.insert(
				name,
				Held {
					chain,
					key_pem: key_pem.to_owned(),
					not_after,
					// Nothing canopy has said yet this run, so the chain's own
					// dates are what says whether it can be served. Revocation
					// arrives from canopy and takes it out of service then.
					usable: not_after.is_none_or(|at| at > Timestamp::now()),
					revoked: false,
				},
			);
		}
		info!(names = held.len(), "loaded the chains this host holds");
		drop(held);

		// A DNS name waiting on an operator is not mistaken for a failing one
		// before the daemon has asked again.
		//
		// spec: TLS#undeclared-and-denied-dns-names
		let kept = certs::load_refusals(&self.dir).await;
		{
			let mut orders = self.orders.write().await;
			for (name, kept) in &kept {
				orders.entry(name.clone()).or_insert_with(|| Order {
					state: refusal_state(kept.kind).to_owned(),
					asked_at: kept.asked_at.as_deref().and_then(|at| at.parse().ok()),
					application_type: Some(kept.application_type.clone()),
					refusal: Some(Refusal {
						kind: kept.kind,
						reason: kept.reason.clone(),
					}),
					..Order::default()
				});
			}
		}
		*self.persisted_refusals.lock().await = kept;

		// A request a command made is not forgotten by a restart before its first
		// chain arrives.
		//
		// spec: TLS#which-dns-names-are-certified
		let requests = certs::load_requests(&self.dir).await;
		{
			let mut explicit = self.explicit.lock().await;
			for (name, application_type) in &requests {
				explicit
					.entry(name.clone())
					.or_insert_with(|| application_type.clone());
			}
		}
		*self.persisted_requests.lock().await = requests;

		*keys = Some(store);
		Ok(())
	}

	/// Keep the refusals canopy gave, where they changed.
	///
	/// spec: TLS#undeclared-and-denied-dns-names
	async fn persist_refusals(&self) {
		let now: BTreeMap<String, StoredRefusal> = self
			.orders
			.read()
			.await
			.iter()
			.filter_map(|(name, order)| {
				let refusal = order.refusal.as_ref()?;
				Some((
					name.clone(),
					StoredRefusal {
						kind: refusal.kind,
						reason: refusal.reason.clone(),
						application_type: order.application_type.clone().unwrap_or_default(),
						asked_at: order.asked_at.map(|at| at.to_string()),
					},
				))
			})
			.collect();

		let mut persisted = self.persisted_refusals.lock().await;
		if *persisted == now {
			return;
		}
		match certs::store_refusals(&self.dir, &now).await {
			Ok(()) => *persisted = now,
			Err(err) => warn!(%err, "could not keep the refusals canopy gave"),
		}
	}

	/// Whether a pass is due: an order in flight, a name asked for and not held,
	/// or the steady interval having come round.
	async fn pass_due(&self) -> bool {
		let now = Timestamp::now();
		let steady_due = match *self.last_pass.read().await {
			None => true,
			Some(last) => (now - last).get_seconds() >= STEADY_INTERVAL.as_secs() as i64,
		};
		if steady_due {
			return true;
		}

		// A server standing down is answered the same way until the steady
		// interval comes round again, so nothing short of that wakes a pass.
		if let Some(at) = *self.stood_down.read().await
			&& (now - at).get_seconds() < STEADY_INTERVAL.as_secs() as i64
		{
			return false;
		}

		if let Some(at) = *self.incomplete.read().await
			&& (now - at).get_seconds() < PENDING_RETRY.as_secs() as i64
		{
			return false;
		}

		// Each name asked for is tested rather than the set merely being
		// non-empty: a name whose last attempt failed is still asked for, and
		// waking a full pass — which asks canopy what this server may do before
		// it does anything else — on every tick until it succeeds is what the
		// per-name backoff exists to stop.
		let mut wanted: Vec<String> = self.wanted.lock().await.iter().cloned().collect();
		{
			// A name whose application is paused or without the grant is kept, but
			// waits for the steady pass that finds it lifted. One already holding a
			// chain is renewed on the ordinary schedule.
			let entitlement = self.entitlement.read().await;
			let held = self.held.read().await;
			wanted.extend(
				self.explicit
					.lock()
					.await
					.iter()
					.filter(|(name, application_type)| {
						!held.contains_key(*name)
							&& entitlement
								.as_ref()
								.is_none_or(|e| explicit_orderable(e, name, application_type))
					})
					.map(|(name, _)| name.clone()),
			);
		}
		for name in &wanted {
			if self.name_due(name, now).await {
				return true;
			}
		}

		self.orders.read().await.values().any(|order| {
			order.pending()
				&& order
					.asked_at
					.is_none_or(|at| (now - at).get_seconds() >= PENDING_RETRY.as_secs() as i64)
		})
	}

	/// Ask canopy what this server may do, and keep the answer.
	async fn refresh_entitlement(&self, ctx: &TaskContext) -> Result<Entitlement> {
		let client = ctx
			.canopy_client
			.as_ref()
			.ok_or_else(|| miette!("no canopy client; this host is not enrolled"))?;
		let wire = client
			.names_entitlements()
			.await
			.into_diagnostic()
			.wrap_err("asking canopy what this server may do")?;
		let entitlement = Entitlement::from_wire(&wire);
		*self.entitlement.write().await = Some(entitlement.clone());
		Ok(entitlement)
	}

	/// The entitlement as last answered, asking canopy where none has been.
	async fn entitlement(&self, ctx: &TaskContext) -> Result<Entitlement> {
		if let Some(held) = self.entitlement.read().await.clone() {
			return Ok(held);
		}
		self.refresh_entitlement(ctx).await
	}

	/// One collection pass: what may be certified, asked about and collected.
	///
	/// A steady pass asks about every DNS name; any other asks about the ones
	/// due, and about `asked`, the one a command asked for, whatever canopy last
	/// said about it.
	///
	/// spec: TLS#requesting-and-collecting
	async fn pass(&self, ctx: &TaskContext, steady: bool, asked: Option<&str>) -> Result<()> {
		self.load_from_disk().await?;
		let entitlement = self.refresh_entitlement(ctx).await?;

		if let Some(reason) = stands_down(&entitlement) {
			debug!(reason, "not requesting certificates");
			self.stand_down().await;
			return Ok(());
		}
		*self.stood_down.write().await = None;

		let (targets, complete) = self.targets(&entitlement, steady).await;
		*self.incomplete.write().await = (!complete).then(Timestamp::now);
		if complete {
			let names: Vec<String> = targets.iter().map(|t| t.name.clone()).collect();
			self.prune_orders(&names).await;
		}
		self.persist_refusals().await;
		self.persist_requests().await;

		if targets.is_empty() {
			debug!("no DNS name to certify on this host");
			return Ok(());
		}

		let now = Timestamp::now();
		for target in targets {
			if !self.target_due(&target.name, steady, asked, now).await {
				continue;
			}
			match self.collect_one(ctx, &target).await {
				Ok(()) => {
					self.wanted.lock().await.remove(&target.name);
				}
				Err(failure) => {
					// One DNS name failing must not stop the rest: a stuck order on
					// one site is not a reason to leave every other one uncollected.
					self.record_failure(&target, failure, now).await;
				}
			}
		}
		self.persist_refusals().await;
		self.persist_requests().await;
		Ok(())
	}

	/// Record why asking about a DNS name did not go through.
	///
	/// Stamped although canopy recorded nothing, so a failed attempt backs off
	/// exactly as a pending order does. The DNS name stays asked-for: dropping it
	/// on a transient failure would leave one Caddy is serving waiting out the
	/// steady interval for its first chain.
	async fn record_failure(&self, target: &Target, failure: Failure, now: Timestamp) {
		let Failure { refusal, report } = failure;
		match refusal.as_ref().map(|refusal| refusal.kind) {
			// Waiting on an operator rather than failing: not a fault on this host.
			Some(kind) if kind.awaits_operator() => {
				info!(
					name = target.name,
					kind = kind.as_str(),
					"canopy needs an operator to act on this DNS name"
				);
			}
			_ => warn!(name = target.name, err = %why(&report), "could not collect a certificate"),
		}

		let mut orders = self.orders.write().await;
		let order = orders.entry(target.name.clone()).or_default();
		order.application_type = Some(target.application_type.clone());
		order.asked_at = Some(now);
		order.last_error = Some(why(&report));
		// A failure that is not an answer about the DNS name leaves what canopy
		// last said standing.
		//
		// spec: TLS#undeclared-and-denied-dns-names
		let Some(refusal) = refusal else {
			return;
		};
		order.state = refusal_state(refusal.kind).to_owned();
		if refusal.kind.awaits_operator() {
			order.last_error = None;
		} else if refusal.kind == RefusalKind::Other {
			// The request as made is refused, with a type mismatch or a DNS name
			// outside the group's domains, and the report says so; asking again
			// would only repeat it. Canopy unable to act for a reason on its own
			// side keeps the request, for when that is put right.
			//
			// spec: TLS#which-dns-names-are-certified
			self.explicit.lock().await.remove(&target.name);
		}
		order.refusal = Some(refusal);
	}

	/// Record a pass that stood down.
	///
	/// Nothing will be ordered for while this stands, and the names a handshake
	/// left behind are the reason a pass was due at all — so they go, and the
	/// stand-down is remembered so the ones arriving next do not wake another.
	/// The grant and the pause are canopy's to lift; asking again before the
	/// steady interval only earns the same refusal.
	///
	/// spec: TLS#when-the-grant-is-absent-or-the-server-is-paused
	async fn stand_down(&self) {
		self.wanted.lock().await.clear();
		*self.stood_down.write().await = Some(Timestamp::now());
	}

	/// Drop what canopy said about names no longer in play.
	///
	/// A name reaches the target list from Caddy's configuration or from a
	/// handshake, and one that has left both is not going to be asked about
	/// again. Without this a host serving a wildcard keeps an entry for every
	/// name any client ever offered.
	async fn prune_orders(&self, targets: &[String]) {
		let held = self.held.read().await;
		let targets: BTreeSet<&String> = targets.iter().collect();
		self.orders
			.write()
			.await
			.retain(|name, _| targets.contains(name) || held.contains_key(name));
	}

	/// Whether a pass asks about one DNS name.
	///
	/// A DNS name a command asked for is asked about at once, even one waiting
	/// on an operator: the operator who has just declared it, or lifted its
	/// denial, is the one asking.
	///
	/// spec: TLS#undeclared-and-denied-dns-names
	async fn target_due(
		&self,
		name: &str,
		steady: bool,
		asked: Option<&str>,
		now: Timestamp,
	) -> bool {
		steady || asked == Some(name) || self.name_due(name, now).await
	}

	/// Whether one name is due to be asked about outside a steady pass.
	///
	/// A name nothing has been attempted for is due at once, which is what makes
	/// a handshake's ask prompt. Once an attempt has been made — whether canopy
	/// recorded an order or the attempt failed outright — the retry backoff
	/// governs, so neither a pending order nor a failing one is asked about every
	/// tick.
	async fn name_due(&self, name: &str, now: Timestamp) -> bool {
		// A name a command asked for is pressing only until its first chain
		// arrives; from then on it is renewed like any other.
		//
		// Each lock is taken and released in its own statement: elsewhere `held` is
		// taken before `wanted` and `explicit`, and holding either of those while
		// waiting on `held` could deadlock against a pass.
		let handshake = self.wanted.lock().await.contains(name);
		let requested = self.explicit.lock().await.contains_key(name);
		let wanted = handshake || (requested && !self.held.read().await.contains_key(name));
		match self.orders.read().await.get(name) {
			None => true,
			// Refused, so only the steady schedule asks again: asking sooner earns
			// the same answer. Canopy shows an operator an undeclared request only
			// while the machine keeps making it, and a lifted denial or a corrected
			// configuration is noticed the same way.
			//
			// spec: TLS#undeclared-and-denied-dns-names
			Some(order) if order.refusal.is_some() => false,
			Some(order) if wanted || order.pending() => order
				.asked_at
				.is_none_or(|at| (now - at).get_seconds() >= PENDING_RETRY.as_secs() as i64),
			Some(_) => false,
		}
	}

	/// The DNS names to certify, each with the application it is for.
	///
	/// A DNS name is a hooked site address, or one a handshake asked for, that
	/// belongs to an application on the host which could certify it. One meeting
	/// some of those tests and not the rest is left to Caddy's own issuance.
	/// Explicitly requested names are ordered whether or not Caddy serves them or
	/// their site is hooked.
	///
	/// Where Caddy's configuration or the applications on the host cannot be
	/// read, only the names a command asked for are returned, since they are the
	/// ones that depend on neither, and the list is marked incomplete. Read as
	/// nothing served or nothing installed, either would drop every other name's
	/// order and refusal, and the handshake names waiting for a pass.
	///
	/// spec: TLS#which-dns-names-are-certified
	async fn targets(&self, entitlement: &Entitlement, steady: bool) -> (Vec<Target>, bool) {
		let sites = match delivery::caddy_sites().await {
			Ok(sites) => sites,
			Err(err) => {
				warn!(%err, "could not read Caddy's configuration; ordering only DNS names requested by command");
				return (self.explicit_targets(entitlement).await, false);
			}
		};
		let Some(applications) = self.host_applications(steady).await else {
			return (self.explicit_targets(entitlement).await, false);
		};
		(
			self.targets_from(entitlement, &sites, &applications).await,
			true,
		)
	}

	/// The applications on this host: discovered afresh on a steady pass or where
	/// none have been, and as last discovered otherwise.
	///
	/// Where discovery fails, the applications last discovered stand; `None`
	/// where there are none to fall back on.
	async fn host_applications(&self, steady: bool) -> Option<Vec<HostApplication>> {
		let known = self.applications.read().await.clone();
		if !steady && known.is_some() {
			return known;
		}
		match ownership::discover_host_applications().await {
			Ok(found) => {
				*self.applications.write().await = Some(found.clone());
				Some(found)
			}
			Err(err) => {
				warn!(%err, "could not discover the applications on this host");
				known
			}
		}
	}

	/// The DNS names a command asked for that may be ordered now.
	async fn explicit_targets(&self, entitlement: &Entitlement) -> Vec<Target> {
		self.explicit
			.lock()
			.await
			.iter()
			.filter(|(name, application_type)| {
				explicit_orderable(entitlement, name, application_type)
			})
			.map(|(name, application_type)| Target {
				name: name.clone(),
				application_type: application_type.clone(),
			})
			.collect()
	}

	async fn targets_from(
		&self,
		entitlement: &Entitlement,
		sites: &CaddySites,
		applications: &[HostApplication],
	) -> Vec<Target> {
		let ownership = Ownership::resolve(sites, applications, entitlement);
		let certifiable = |name: &str| {
			let owner = ownership.owner(name)?;
			entitlement
				.for_type(owner)
				.is_some_and(|app| app.may_certify(name))
				.then(|| owner.to_owned())
		};

		let mut names: BTreeSet<String> = sites.hooked_addresses();
		// A name is recorded during a handshake before any entitlement has been
		// asked for, so the test is applied here rather than there, and a name this
		// entitlement will never order for is dropped from the record as well as
		// from the list. Left there it would keep `pass_due` true on every tick,
		// which spends a full entitlement request a minute on a name that can never
		// be ordered for.
		{
			let mut wanted = self.wanted.lock().await;
			wanted.retain(|name| certifiable(name).is_some());
			names.extend(wanted.iter().cloned());
		}

		let mut targets: BTreeMap<String, Target> = names
			.into_iter()
			.filter(|name| certs::plausible_name(name).is_ok())
			.filter_map(|name| {
				let application_type = certifiable(&name)?;
				Some((
					name.clone(),
					Target {
						name,
						application_type,
					},
				))
			})
			.collect();

		// A name a command asked for carries the application the command named,
		// whatever its site says. It is kept until its first chain arrives and then
		// while Caddy serves it, through a pause or a withdrawn grant, which are
		// canopy's to lift; it is ordered only while its application may certify
		// it.
		//
		// spec: TLS#which-dns-names-are-certified
		{
			let held = self.held.read().await;
			self.explicit.lock().await.retain(|name, application_type| {
				let wanted_still = !held.contains_key(name) || sites.serves(name);
				let within = entitlement
					.for_type(application_type)
					.is_some_and(|app| app.covers(name));
				wanted_still && within
			});
		}
		for target in self.explicit_targets(entitlement).await {
			targets.insert(target.name.clone(), target);
		}

		targets.into_values().collect()
	}

	/// Ask canopy for one DNS name, and take what comes back.
	///
	/// Request and collect are the same call and it is safe to repeat: a DNS name
	/// and key canopy already holds a certificate for is answered from what it
	/// holds rather than ordered again.
	async fn collect_one(&self, ctx: &TaskContext, target: &Target) -> Result<(), Failure> {
		let name = target.name.as_str();
		let client = ctx
			.canopy_client
			.as_ref()
			.ok_or_else(|| miette!("no canopy client; this host is not enrolled"))?;

		let key = self.key_for(name).await?;
		let csr = certs::signing_request(name, &key)?;
		let answer = match client
			.certificates_request(
				&RequestCertificateArgs::builder()
					.csr(csr)
					.name(name.to_owned())
					.application_type(ApplicationType::from(target.application_type.clone()))
					.build(),
			)
			.await
		{
			Ok(answer) => answer,
			Err(err) => {
				let refusal = Refusal::from_error(&err);
				let report = Result::<(), _>::Err(err)
					.into_diagnostic()
					.wrap_err_with(|| format!("asking canopy to certify {name}"))
					.unwrap_err();
				return Err(Failure { refusal, report });
			}
		};

		let not_after = answer.not_after.as_deref().and_then(certs::parse_not_after);
		let order = Order {
			state: answer.state.clone(),
			last_error: answer.last_error.clone(),
			not_after,
			revoked: answer.revoked,
			key_must_be_replaced: answer.key_must_be_replaced,
			asked_at: Some(Timestamp::now()),
			application_type: Some(target.application_type.clone()),
			refusal: None,
		};
		if let Some(ref err) = order.last_error {
			warn!(name, error = %err, state = %order.state, "canopy reports this order failing");
		}
		self.orders
			.write()
			.await
			.insert(name.to_owned(), order.clone());

		match disposition(&order, answer.chain.as_deref()) {
			// A key canopy condemns is replaced before the next request, rather
			// than asked against again. It is never certified again for any
			// name, so replacing it is the only way forward and no operator has
			// to act.
			//
			// spec: TLS#revocation-and-key-replacement
			Disposition::ReplaceKey => {
				info!(name, "canopy condemned this key; generating a replacement");
				self.replace_key(name).await?;
			}
			// A certificate canopy reports as revoked stops being served at
			// once. A replacement is requested under the ordinary schedule:
			// revoking pauses the server, so asking now would only be refused,
			// and the refusal is an operator deciding when this host may have a
			// certificate again.
			Disposition::DropHeld => {
				info!(
					name,
					"canopy reports this certificate revoked; taking it out of service"
				);
				self.drop_held(name).await?;
			}
			Disposition::TakeChain => {
				let chain = answer.chain.unwrap_or_default();
				self.take_chain(name, chain, not_after, answer.usable)
					.await?;
			}
			Disposition::Wait => debug!(name, state = %answer.state, "no chain yet"),
		}
		Ok(())
	}

	/// The key for `name`, generating and storing one where there is none.
	async fn key_for(&self, name: &str) -> Result<KeyPair> {
		let mut keys = self.keys.lock().await;
		let store = keys.get_or_insert_with(KeyStore::default);
		if let Some(key) = store.key(name) {
			return key;
		}

		let key = certs::generate_key()?;
		store.replace(name, &key);
		certs::store_keys(&self.dir, store).await?;
		info!(name, "generated a key for this name");
		Ok(key)
	}

	/// Replace the key held for `name`, and drop what it covered.
	async fn replace_key(&self, name: &str) -> Result<()> {
		let key = certs::generate_key()?;
		{
			let mut keys = self.keys.lock().await;
			let store = keys.get_or_insert_with(KeyStore::default);
			store.replace(name, &key);
			certs::store_keys(&self.dir, store).await?;
		}
		// The chain covered the condemned key, so it goes with it.
		self.drop_held(name).await
	}

	/// Take a collected chain into service, and keep it across a restart.
	async fn take_chain(
		&self,
		name: &str,
		chain: String,
		not_after: Option<Timestamp>,
		usable: bool,
	) -> Result<()> {
		let key_pem = {
			let keys = self.keys.lock().await;
			keys.as_ref()
				.and_then(|store| store.key_pem(name).map(ToOwned::to_owned))
				.ok_or_else(|| miette!("collected a chain for {name} with no key to serve it"))?
		};

		let replacing = self
			.held
			.read()
			.await
			.get(name)
			.is_some_and(|held| held.chain != chain);
		certs::store_chain(&self.dir, name, &chain).await?;
		self.held.write().await.insert(
			name.to_owned(),
			Held {
				chain,
				key_pem,
				not_after,
				usable,
				revoked: false,
			},
		);
		if replacing {
			info!(name, "a renewed chain replaced the one held");
		} else {
			info!(name, "collected a chain");
		}
		Ok(())
	}

	/// Take a name's chain out of service and off the disk.
	async fn drop_held(&self, name: &str) -> Result<()> {
		self.held.write().await.remove(name);
		certs::forget_chain(&self.dir, name).await?;
		Ok(())
	}

	/// What this task holds, for the status endpoint and the CLI.
	async fn report(&self) -> Value {
		let entitlement = self.entitlement.read().await.clone();
		let held = self.held.read().await;
		let orders = self.orders.read().await;

		let names: Vec<Value> = held
			.iter()
			.map(|(name, chain)| {
				json!({
					"name": name,
					"notAfter": chain.not_after.map(|at| at.to_string()),
					"usable": chain.usable,
					"revoked": chain.revoked,
				})
			})
			.collect();

		let orders: Vec<Value> = orders
			.iter()
			.map(|(name, order)| {
				json!({
					"name": name,
					"state": order.state,
					"lastError": order.last_error,
					"notAfter": order.not_after.map(|at| at.to_string()),
					"revoked": order.revoked,
					"keyMustBeReplaced": order.key_must_be_replaced,
					"askedAt": order.asked_at.map(|at| at.to_string()),
					"applicationType": order.application_type,
					"refusal": order.refusal.as_ref().map(|refusal| refusal.kind.as_str()),
					"reason": order.reason(),
				})
			})
			.collect();

		json!({
			"entitled": entitlement.as_ref().is_some_and(Entitlement::holds_tls_grant),
			"dnsEntitled": entitlement.as_ref().is_some_and(Entitlement::holds_dns_grant),
			"paused": entitlement.as_ref().is_some_and(Entitlement::fully_paused),
			"domains": entitlement.as_ref().map(Entitlement::domains).unwrap_or_default(),
			"registeredNames": entitlement
				.as_ref()
				.map(Entitlement::registered_names)
				.unwrap_or_default(),
			"canopyHolds": entitlement.as_ref().map(|e| e
				.certificates()
				.iter()
				.map(|held| json!({
					"name": held.name,
					"keyFingerprint": held.key_fingerprint,
					"notAfter": held.not_after,
					"usable": held.usable,
					"revoked": held.revoked,
					"keyMustBeReplaced": held.key_must_be_replaced,
				}))
				.collect::<Vec<_>>())
				.unwrap_or_default(),
			"held": names,
			"orders": orders,
			"wanted": self.wanted.lock().await.iter().cloned().collect::<Vec<_>>(),
			"lastPass": self.last_pass.read().await.map(|at| at.to_string()),
			"lastError": self.last_error.read().await.clone(),
		})
	}

	/// Publish the addresses a DNS name resolves to, or withdraw it with none.
	///
	/// A registration names the application it is for; a withdrawal names only
	/// the DNS name.
	///
	/// spec: NAM#registering-addresses-for-a-dns-name
	async fn register_name(
		&self,
		ctx: &TaskContext,
		name: &str,
		application_type: Option<&str>,
		addresses: Vec<String>,
	) -> Result<RegisteredName> {
		certs::plausible_name(name)?;
		// The daemon is the component holding the DNS grant, so it is where an
		// address is tested rather than at whichever client happened to call:
		// canopy publishes what it is given, and the CLI is not the only way
		// here. Parsed and re-rendered, so what is published is one canonical
		// spelling of the address rather than whatever form arrived.
		let addresses = addresses
			.iter()
			.map(|address| {
				address
					.parse::<std::net::IpAddr>()
					.map(|parsed| parsed.to_string())
					.map_err(|_| miette!("{address:?} is not an IP address"))
			})
			.collect::<Result<Vec<String>>>()?;

		let client = ctx
			.canopy_client
			.as_ref()
			.ok_or_else(|| miette!("no canopy client; this host is not enrolled"))?;

		let entitlement = self.entitlement(ctx).await?;
		permit(&entitlement, name, application_type, Grant::Dns)?;

		let answer = client
			.names_register(
				&RegisterNameArgs::builder()
					.addresses(addresses)
					.name(name.to_owned())
					.maybe_application_type(
						application_type.map(|t| ApplicationType::from(t.to_owned())),
					)
					.build(),
			)
			.await
			.into_diagnostic()
			.wrap_err_with(|| format!("registering {name} with canopy"))?;

		// The registration changed what canopy holds for this server, so the
		// cached answer is stale.
		let _ = self.refresh_entitlement(ctx).await;
		Ok(answer)
	}
}

/// What one answer from canopy means for what this host holds.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Disposition {
	/// Replace the key, and take nothing from this answer.
	ReplaceKey,
	/// Take the certificate held for this name out of service.
	DropHeld,
	/// Take the chain this answer carries into service.
	TakeChain,
	/// Nothing to act on yet.
	Wait,
}

/// Read an answer for what to do with it.
///
/// The order is the point. A condemned key comes first and takes nothing else
/// from the answer: any chain alongside it covers the key being replaced, so
/// installing the two together would hand Caddy a chain and a key that do not
/// match — which fails the handshake outright, where declining would have let
/// Caddy issue for the name itself. The next pass requests against the new key.
///
/// spec: TLS#revocation-and-key-replacement
fn disposition(order: &Order, chain: Option<&str>) -> Disposition {
	if order.key_must_be_replaced {
		Disposition::ReplaceKey
	} else if order.revoked {
		Disposition::DropHeld
	} else if chain.is_some_and(|chain| !chain.trim().is_empty()) {
		Disposition::TakeChain
	} else {
		Disposition::Wait
	}
}

/// Why this server makes no requests, where it makes none.
///
/// A server that may not obtain certificates stops requesting them, and so does
/// one canopy reports as paused. Neither takes a collected chain out of service,
/// so what is held stays served either way: withdrawing a grant from a host
/// under suspicion stops it obtaining anything new without also dropping every
/// name it currently answers on.
///
/// A pause is canopy's to lift and no length of pause is escalated from here, so
/// a paused server waits rather than retrying against the refusal.
///
/// spec: TLS#when-the-grant-is-absent-or-the-server-is-paused
fn stands_down(entitlement: &Entitlement) -> Option<&'static str> {
	if !entitlement.holds_tls_grant() {
		Some("this server holds no TLS grant")
	} else if entitlement.fully_paused() {
		Some("canopy reports this server paused")
	} else {
		None
	}
}

/// The background task: the collection loop, and the endpoints the CLI reaches.
pub struct CanopyNames {
	state: Arc<CertificateState>,
}

impl CanopyNames {
	pub fn new(state: Arc<CertificateState>) -> Self {
		Self { state }
	}
}

impl BackgroundTask for CanopyNames {
	fn name(&self) -> &'static str {
		TASK_NAME
	}

	fn interval(&self) -> Duration {
		TICK
	}

	fn run<'a>(&'a self, ctx: &'a TaskContext) -> BoxFuture<'a, Result<()>> {
		Box::pin(async move {
			// Chains are loaded whatever else happens, so a restart serves what
			// it held even where canopy cannot be reached at all.
			if let Err(err) = self.state.load_from_disk().await {
				warn!(%err, "could not load the chains this host holds");
			}

			if !self.state.pass_due().await {
				return Ok(());
			}

			let steady = match *self.state.last_pass.read().await {
				None => true,
				Some(last) => {
					(Timestamp::now() - last).get_seconds() >= STEADY_INTERVAL.as_secs() as i64
				}
			};

			match self.state.pass(ctx, steady, None).await {
				Ok(()) => {
					*self.state.last_error.write().await = None;
				}
				Err(err) => {
					warn!(%err, "certificate collection pass failed");
					*self.state.last_error.write().await = Some(why(&err));
				}
			}
			if steady {
				*self.state.last_pass.write().await = Some(Timestamp::now());
			}
			Ok(())
		})
	}

	fn http_endpoints(&self) -> Vec<TaskEndpoint> {
		vec![
			endpoint("status", self.state.clone(), |state, _ctx| {
				Box::pin(async move { Ok(state.report().await) })
			}),
			guarded_endpoint("collect", self.state.clone(), |state, ctx| {
				Box::pin(async move {
					state.pass(&ctx, true, None).await?;
					*state.last_pass.write().await = Some(Timestamp::now());
					Ok(state.report().await)
				})
			}),
			guarded_endpoint("request", self.state.clone(), |state, ctx| {
				Box::pin(async move {
					let name = required(&ctx, "name")?;
					certs::plausible_name(&name)?;
					let name = name.to_ascii_lowercase();
					// A DNS name cannot be attributed from Caddy's configuration
					// ahead of its site, so the command says which application it is
					// for.
					let application_type = required(&ctx, "type")?;
					let entitlement = state.entitlement(&ctx).await?;
					// Refused rather than recorded where the named application could
					// not have it ordered: a pass would drop it or hold it back, and the
					// operator needs to be told that rather than handed a report that
					// looks like the ask was taken.
					permit(&entitlement, &name, Some(&application_type), Grant::Tls)?;
					state.note_explicit(&name, application_type).await?;
					state.pass(&ctx, false, Some(&name)).await?;
					Ok(state.report().await)
				})
			}),
			guarded_endpoint("dns-register", self.state.clone(), |state, ctx| {
				Box::pin(async move {
					let name = required(&ctx, "name")?;
					let addresses = ctx
						.query
						.get("addresses")
						.map(|list| {
							list.split(',')
								.map(str::trim)
								.filter(|a| !a.is_empty())
								.map(ToOwned::to_owned)
								.collect::<Vec<_>>()
						})
						.unwrap_or_default();
					// An empty list is what withdraws a name, so registering
					// with none would take the records down — and would look
					// like a publish that had worked. Withdrawing is its own
					// endpoint, asked for deliberately.
					if addresses.is_empty() {
						return Err(miette!(
							"registering {name} needs at least one address; use dns-withdraw to take its records down"
						));
					}
					let application_type = required(&ctx, "type")?;
					let answer = state
						.register_name(&ctx, &name, Some(&application_type), addresses)
						.await?;
					Ok(registered(&answer))
				})
			}),
			guarded_endpoint("dns-withdraw", self.state.clone(), |state, ctx| {
				Box::pin(async move {
					let name = required(&ctx, "name")?;
					// An empty address list is what withdraws a name: the
					// records come down and the name is freed.
					let answer = state.register_name(&ctx, &name, None, Vec::new()).await?;
					Ok(registered(&answer))
				})
			}),
		]
	}
}

fn registered(answer: &RegisteredName) -> Value {
	json!({
		"name": answer.name,
		"addresses": answer.addresses,
		"publishedAddresses": answer.published_addresses,
		"published": answer.published,
		"lastError": answer.last_error,
	})
}

fn required(ctx: &TaskContext, key: &str) -> Result<String> {
	ctx.query
		.get(key)
		.filter(|value| !value.is_empty())
		.cloned()
		.ok_or_else(|| miette!("this endpoint needs a `{key}` query parameter"))
}

/// Wire one endpoint, turning a failure into a 400 with its message.
///
/// A command reaching this is an operator asking for something; what went wrong
/// is what they need back, not a bare status.
fn endpoint(
	name: &'static str,
	state: Arc<CertificateState>,
	run: fn(Arc<CertificateState>, TaskContext) -> BoxFuture<'static, Result<Value>>,
) -> TaskEndpoint {
	TaskEndpoint::open(name, wrap(state, run))
}

/// [`endpoint`], for one that changes state: the superuser only, over POST.
///
/// Publishing or withdrawing a name changes what the world resolves for a
/// production deployment, and a request costs the authority an order — neither
/// is something any process that can reach loopback may ask for.
///
/// spec: NAM#commands
fn guarded_endpoint(
	name: &'static str,
	state: Arc<CertificateState>,
	run: fn(Arc<CertificateState>, TaskContext) -> BoxFuture<'static, Result<Value>>,
) -> TaskEndpoint {
	TaskEndpoint::guarded(name, wrap(state, run))
}

fn wrap(
	state: Arc<CertificateState>,
	run: fn(Arc<CertificateState>, TaskContext) -> BoxFuture<'static, Result<Value>>,
) -> TaskEndpointHandler {
	Arc::new(move |ctx: TaskContext| {
		let state = state.clone();
		Box::pin(async move {
			match run(state, ctx).await {
				Ok(value) => TaskEndpointResponse::Json(value),
				Err(err) => TaskEndpointResponse::Error {
					status: 400,
					message: why(&err),
				},
			}
		})
	})
}

#[cfg(test)]
mod tests {
	use super::*;

	fn awaits_operator(order: &Order) -> bool {
		order
			.refusal
			.as_ref()
			.is_some_and(|refusal| refusal.kind.awaits_operator())
	}

	fn state() -> (tempfile::TempDir, CertificateState) {
		let dir = tempfile::tempdir().unwrap();
		let state = CertificateState::new(dir.path().to_path_buf(), peer::Permitted::default());
		(dir, state)
	}

	async fn with_entitlement(state: &CertificateState, domains: &[&str], tls: bool, paused: bool) {
		let wire = bestool_canopy::schema::Entitlements::builder()
			.applications(vec![])
			.certificates(vec![])
			.domains(domains.iter().map(|d| (*d).to_string()).collect::<Vec<_>>())
			.may_manage_dns(false)
			.may_manage_tls(tls)
			.paused(paused)
			.registered_names(vec![])
			.build();
		*state.entitlement.write().await = Some(Entitlement::from_wire(&wire));
	}

	/// A context with nothing behind it: no canopy, and an HTTP client whose
	/// caddy admin API is not running, so the subjects it reads are empty.
	fn detached_ctx() -> TaskContext {
		TaskContext {
			http_client: reqwest::Client::new(),
			canopy_client: None,
			reload: tokio::sync::watch::channel(0u64).1,
			#[cfg(windows)]
			restart: None,
			query: Default::default(),
		}
	}

	fn central() -> HostApplication {
		HostApplication {
			type_slug: "tamanu-central".into(),
			canonical_hosts: BTreeSet::new(),
			service_names: ["api.central.tamanu.internal".to_owned()].into(),
			shared_names: BTreeSet::new(),
			local_ports: BTreeSet::new(),
		}
	}

	/// Caddy sites proxying to Tamanu central, with the daemon's endpoint named for
	/// `hooked` of them.
	fn sites(all: &[&str], hooked: &[&str]) -> CaddySites {
		let routes: Vec<Value> = all
			.iter()
			.map(|host| {
				json!({
					"match": [{ "host": [host] }],
					"handle": [{ "handler": "reverse_proxy", "dynamic_upstreams": {
						"source": "a", "name": "api.central.tamanu.internal", "port": "3000" } }],
				})
			})
			.collect();
		let policies = if hooked.is_empty() {
			json!([])
		} else {
			json!([{
				"subjects": hooked,
				"get_certificate": [{ "via": "http", "url": "http://127.0.0.1:8271/certificate" }],
			}])
		};
		CaddySites::from_config(&json!({"apps": {
			"http": { "servers": { "s": { "routes": routes } } },
			"tls": { "automation": { "policies": policies } },
		}}))
	}

	/// Canopy's answer for a host with two applications: one holding the TLS grant
	/// over `one.test`, one without it over `two.test`.
	fn applications_wire() -> bestool_canopy::schema::Entitlements {
		use bestool_canopy::schema::{ApplicationEntitlements, Entitlements};

		let app = |type_slug: &str, domain: &str, tls: bool| {
			ApplicationEntitlements::builder()
				.certificates(vec![])
				.domains(vec![domain.to_string()])
				.may_manage_dns(false)
				.may_manage_tls(tls)
				.paused(false)
				.registered_names(vec![])
				.type_(type_slug.parse().unwrap())
				.build()
		};
		Entitlements::builder()
			.applications(vec![
				app("tamanu-central", "one.test", true),
				app("tamanu-facility", "two.test", false),
			])
			.certificates(vec![])
			.domains(vec![])
			.may_manage_dns(false)
			.may_manage_tls(false)
			.paused(false)
			.registered_names(vec![])
			.build()
	}

	fn target(name: &str) -> Target {
		Target {
			name: name.into(),
			application_type: "tamanu-central".into(),
		}
	}

	fn refused(kind: RefusalKind, reason: &str) -> Failure {
		Failure {
			refusal: Some(Refusal {
				kind,
				reason: reason.into(),
			}),
			report: miette!("asking canopy to certify: canopy returned 403 Forbidden: {reason}"),
		}
	}

	fn names(targets: &[Target]) -> Vec<&str> {
		targets.iter().map(|t| t.name.as_str()).collect()
	}

	async fn entitled(state: &CertificateState) -> Entitlement {
		state.entitlement.read().await.clone().unwrap()
	}

	async fn hold(state: &CertificateState, name: &str, usable: bool) {
		hold_until(state, name, usable, None).await;
	}

	async fn hold_until(
		state: &CertificateState,
		name: &str,
		usable: bool,
		not_after: Option<Timestamp>,
	) {
		state.held.write().await.insert(
			name.to_owned(),
			Held {
				chain: format!("chain for {name}"),
				key_pem: format!("key for {name}"),
				not_after,
				usable,
				revoked: !usable,
			},
		);
	}

	#[tokio::test]
	async fn a_held_usable_chain_is_served_with_its_key() {
		let (_dir, state) = state();
		with_entitlement(&state, &["example.com"], true, false).await;
		hold(&state, "app.example.com", true).await;

		let (chain, key) = state.serve("app.example.com").await.unwrap();
		assert_eq!(chain, "chain for app.example.com");
		assert_eq!(key, "key for app.example.com");
		// The name as the client spelled it, trailing dot and all.
		assert!(state.serve("APP.example.com.").await.is_some());
	}

	#[tokio::test]
	async fn every_case_with_no_chain_to_offer_declines() {
		// A decline hands the handshake back to Caddy; an error would take down
		// a name Caddy would otherwise have covered. So each of these is a
		// decline, not a failure.
		let (_dir, state) = state();
		with_entitlement(&state, &["example.com"], true, false).await;

		// Nothing collected yet.
		assert!(state.serve("app.example.com").await.is_none());

		// Out of the domains the group controls.
		hold(&state, "app.elsewhere.test", true).await;
		assert!(state.serve("app.elsewhere.test").await.is_none());

		// Collected, then revoked or expired.
		hold(&state, "old.example.com", false).await;
		assert!(state.serve("old.example.com").await.is_none());
	}

	#[tokio::test]
	async fn a_held_chain_keeps_being_served_when_the_grant_goes_and_while_paused() {
		// Neither a withdrawn grant nor a pause is a revocation, and revocation
		// is what takes a chain out of service — so containment stops this host
		// obtaining anything new without dropping every name it answers on.
		let (_dir, state) = state();
		hold(&state, "app.example.com", true).await;

		with_entitlement(&state, &["example.com"], false, false).await;
		assert!(state.serve("app.example.com").await.is_some());

		with_entitlement(&state, &["example.com"], true, true).await;
		assert!(state.serve("app.example.com").await.is_some());
	}

	#[tokio::test]
	async fn a_handshake_for_a_name_with_nothing_held_records_it() {
		let (_dir, state) = state();
		state.note_wanted("app.example.com").await;
		assert!(state.wanted.lock().await.contains("app.example.com"));

		// A name already held needs no order.
		hold(&state, "other.example.com", true).await;
		state.note_wanted("other.example.com").await;
		assert!(!state.wanted.lock().await.contains("other.example.com"));

		// Something that could not be a name is not recorded at all.
		state.note_wanted("not a name/../etc").await;
		assert_eq!(state.wanted.lock().await.len(), 1);
	}

	/// `usable` is what canopy last said, and nothing re-evaluates it as time
	/// passes. A chain that has since expired is a decline — which hands the
	/// name back to Caddy's own issuance — rather than an expired certificate
	/// served forever because collection stopped.
	///
	/// spec: TLSD#declining-and-failing
	#[tokio::test]
	async fn a_chain_that_has_expired_since_it_was_collected_is_declined() {
		let (_dir, state) = state();
		with_entitlement(&state, &["example.com"], true, false).await;

		let hour = jiff::SignedDuration::from_hours(1);
		hold_until(
			&state,
			"app.example.com",
			true,
			Some(Timestamp::now() + hour),
		)
		.await;
		assert!(state.serve("app.example.com").await.is_some());

		hold_until(
			&state,
			"app.example.com",
			true,
			Some(Timestamp::now() - hour),
		)
		.await;
		assert!(state.serve("app.example.com").await.is_none());
	}

	/// A name recorded during a handshake that the entitlement turns out not to
	/// cover leaves the record as well as the target list: kept, it would make a
	/// pass due on every tick and spend an entitlement request each time on a name
	/// that can never be ordered for.
	#[tokio::test]
	async fn a_name_the_entitlement_does_not_cover_is_dropped_from_the_record() {
		let (_dir, state) = state();
		with_entitlement(&state, &["example.com"], true, false).await;
		state.note_wanted("app.elsewhere.test").await;
		assert!(state.pass_due().await);

		let none = sites(&["app.elsewhere.test"], &[]);
		let targets = state
			.targets_from(&entitled(&state).await, &none, &[central()])
			.await;
		assert!(targets.is_empty());

		// Nothing is waiting on a pass any more, so the steady interval governs
		// again rather than every tick making one due.
		assert!(state.wanted.lock().await.is_empty());
		*state.last_pass.write().await = Some(Timestamp::now());
		assert!(!state.pass_due().await);
	}

	#[tokio::test]
	async fn a_recorded_name_is_still_subject_to_the_entitlement_and_to_ownership() {
		// A handshake cannot conjure an order for a name outside the server's
		// reach: the recorded name meets the same tests as one read from Caddy.
		let (_dir, state) = state();
		with_entitlement(&state, &["example.com"], true, false).await;
		state.note_wanted("app.elsewhere.test").await;
		state.note_wanted("app.example.com").await;
		state.note_wanted("nobody.example.com").await;

		// The site is not hooked: a handshake only happens for a hooked one, so the
		// recorded name needs no hook of its own.
		let seen = sites(&["app.elsewhere.test", "app.example.com"], &[]);
		let targets = state
			.targets_from(&entitled(&state).await, &seen, &[central()])
			.await;
		assert_eq!(
			targets,
			vec![Target {
				name: "app.example.com".into(),
				application_type: "tamanu-central".into(),
			}]
		);
	}

	/// A condemned key takes precedence over everything else the answer carries.
	///
	/// Canopy can answer with a chain and `key_must_be_replaced` together: the
	/// chain covers the key being condemned. Taking it would pair the old chain
	/// with the new key, and the endpoint hands Caddy both — a mismatch fails
	/// the handshake outright, where declining would have let Caddy issue for
	/// the name itself.
	///
	/// spec: TLS#revocation-and-key-replacement
	#[test]
	fn a_condemned_key_is_replaced_and_nothing_else_in_the_answer_is_taken() {
		let condemned = Order {
			state: "issued".into(),
			key_must_be_replaced: true,
			..Order::default()
		};
		assert_eq!(
			disposition(&condemned, Some("-----BEGIN CERTIFICATE-----")),
			Disposition::ReplaceKey
		);
		// Even alongside a revocation, which would otherwise be the action.
		let both = Order {
			revoked: true,
			..condemned.clone()
		};
		assert_eq!(
			disposition(&both, Some("-----BEGIN CERTIFICATE-----")),
			Disposition::ReplaceKey
		);
	}

	/// The rest of the precedence: a revocation drops what is held whatever
	/// chain came with it, a chain is taken, and an empty one waits.
	#[test]
	fn a_revocation_drops_what_is_held_and_a_chain_is_otherwise_taken() {
		let revoked = Order {
			revoked: true,
			..Order::default()
		};
		assert_eq!(disposition(&revoked, Some("chain")), Disposition::DropHeld);

		let issued = Order {
			state: "issued".into(),
			..Order::default()
		};
		assert_eq!(disposition(&issued, Some("chain")), Disposition::TakeChain);

		let pending = Order {
			state: "pending".into(),
			..Order::default()
		};
		assert_eq!(disposition(&pending, None), Disposition::Wait);
		assert_eq!(disposition(&pending, Some("   ")), Disposition::Wait);
	}

	/// A name whose attempt failed outright stays asked for, so it is retried
	/// rather than waiting out the steady interval — but it backs off like a
	/// pending order, rather than waking a full pass on every tick.
	#[tokio::test]
	async fn a_failed_attempt_is_retried_on_the_backoff_rather_than_every_tick() {
		let (_dir, state) = state();
		with_entitlement(&state, &["example.com"], true, false).await;
		state.note_wanted("app.example.com").await;
		*state.last_pass.write().await = Some(Timestamp::now());

		// Nothing attempted yet, so the name asked for makes a pass due.
		let now = Timestamp::now();
		assert!(state.pass_due().await);
		assert!(state.name_due("app.example.com", now).await);

		// A pass that failed on it: the failure is recorded and stamped, and the
		// name is still asked for.
		{
			let mut orders = state.orders.write().await;
			let order = orders.entry("app.example.com".to_string()).or_default();
			order.last_error = Some("canopy unreachable".into());
			order.asked_at = Some(now);
		}
		assert!(state.wanted.lock().await.contains("app.example.com"));
		assert!(!state.name_due("app.example.com", now).await);
		assert!(!state.pass_due().await);

		// Once the backoff has elapsed it is due again.
		let later = now + jiff::SignedDuration::from_secs(PENDING_RETRY.as_secs() as i64 + 1);
		assert!(state.name_due("app.example.com", later).await);
	}

	/// An empty address list is what withdraws a name, so registering with none
	/// would take a production name's records down while reading as a publish
	/// that had worked. Withdrawing is its own endpoint, asked for deliberately.
	///
	/// spec: NAM#registering-addresses-for-a-dns-name
	#[tokio::test]
	async fn registering_with_no_address_is_refused_rather_than_withdrawing() {
		let (_dir, state) = state();
		with_entitlement(&state, &["example.com"], true, false).await;
		let task = CanopyNames::new(Arc::new(state));
		let endpoints = task.http_endpoints();
		let register = endpoints
			.iter()
			.find(|endpoint| endpoint.name == "dns-register")
			.expect("the task exposes dns-register");

		for query in [vec![], vec![("addresses", "")], vec![("addresses", " , ")]] {
			let mut ctx = detached_ctx();
			ctx.query.insert("name".into(), "app.example.com".into());
			for (key, value) in &query {
				ctx.query.insert((*key).into(), (*value).into());
			}
			match (register.handler)(ctx).await {
				TaskEndpointResponse::Error { status, message } => {
					assert_eq!(status, 400);
					assert!(
						message.contains("dns-withdraw"),
						"the refusal must point at the endpoint that does withdraw: {message}"
					);
				}
				_ => panic!("expected a refusal for {query:?}"),
			}
		}
	}

	/// A handshake records a name whatever this server may do, and a server
	/// standing down holds no chains — so every handshake it declines records
	/// one. Those names must not wake a pass, or a paused server spends an
	/// entitlement request every tick on an answer that cannot change.
	///
	/// spec: TLS#when-the-grant-is-absent-or-the-server-is-paused
	#[tokio::test]
	async fn a_server_standing_down_waits_rather_than_asking_every_tick() {
		let (_dir, state) = state();
		// The grant is gone, which is what a pass will find.
		with_entitlement(&state, &["example.com"], false, false).await;
		*state.last_pass.write().await = Some(Timestamp::now());

		state.note_wanted("app.example.com").await;
		assert!(state.pass_due().await, "a name asked for makes a pass due");

		// What a pass finds, and so which branch it takes.
		let entitlement = state.entitlement.read().await.clone().unwrap();
		assert_eq!(
			stands_down(&entitlement),
			Some("this server holds no TLS grant")
		);

		// It drops what it will never order for, and remembers having done so.
		state.stand_down().await;
		assert!(state.wanted.lock().await.is_empty());
		assert!(!state.pass_due().await);

		// And a handshake arriving after it does not wake another.
		state.note_wanted("other.example.com").await;
		assert!(
			!state.pass_due().await,
			"a name recorded under a stand-down must not wake a pass"
		);
	}

	/// Caddy passes the client's own server name through for a wildcard site, so
	/// the name reaching `note_wanted` is remote input. The set it feeds is
	/// bounded, and past the bound a new name is dropped rather than an old one
	/// evicted — evicting would let invented names push out a real client's.
	#[tokio::test]
	async fn a_stream_of_invented_names_cannot_grow_the_set_without_bound() {
		let (_dir, state) = state();
		state.note_wanted("real.example.com").await;
		for n in 0..WANTED_LIMIT * 2 {
			state
				.note_wanted(&format!("invented-{n}.example.com"))
				.await;
		}

		let wanted = state.wanted.lock().await;
		assert_eq!(wanted.len(), WANTED_LIMIT);
		assert!(
			wanted.contains("real.example.com"),
			"the name a real client asked for must not be pushed out"
		);
	}

	/// An order is kept for a name still in play and dropped for one that has
	/// left both Caddy's configuration and the set a handshake feeds, so a host
	/// serving a wildcard does not keep an entry per name ever offered.
	#[tokio::test]
	async fn what_canopy_said_about_a_name_no_longer_in_play_is_dropped() {
		let (_dir, state) = state();
		with_entitlement(&state, &["example.com"], true, false).await;
		let entitlement = entitled(&state).await;
		hold(&state, "held.example.com", true).await;

		for name in ["gone.example.com", "held.example.com", "app.example.com"] {
			state.orders.write().await.insert(
				name.to_string(),
				Order {
					state: "issued".into(),
					..Order::default()
				},
			);
		}
		let seen = sites(&["app.example.com"], &["app.example.com"]);
		let targets = state.targets_from(&entitlement, &seen, &[central()]).await;
		assert_eq!(names(&targets), vec!["app.example.com"]);
		let kept: Vec<String> = targets.into_iter().map(|t| t.name).collect();
		state.prune_orders(&kept).await;

		let orders = state.orders.read().await;
		assert!(orders.contains_key("app.example.com"), "a target is kept");
		assert!(
			orders.contains_key("held.example.com"),
			"a name still holding a chain is kept"
		);
		assert!(
			!orders.contains_key("gone.example.com"),
			"a name in neither is dropped"
		);
	}

	/// Canopy publishes what it is handed, so an address that is not one is
	/// refused here rather than at whichever client happened to call: the daemon
	/// is the component holding the DNS grant.
	///
	/// spec: NAM#registering-addresses-for-a-dns-name
	#[tokio::test]
	async fn an_address_that_is_not_an_ip_is_refused_before_canopy_sees_it() {
		let (_dir, state) = state();
		with_entitlement(&state, &["example.com"], true, false).await;

		let err = state
			.register_name(
				&detached_ctx(),
				"app.example.com",
				Some("tamanu-central"),
				vec!["203.0.113.5".into(), "not-an-address".into()],
			)
			.await
			.unwrap_err();
		assert!(
			why(&err).contains("not-an-address"),
			"the offending value must be named: {}",
			why(&err)
		);

		// Well-formed addresses get past the parse and fail for the reason they
		// should: this host has no DNS grant and no canopy client.
		let err = state
			.register_name(
				&detached_ctx(),
				"app.example.com",
				Some("tamanu-central"),
				vec!["::1".into()],
			)
			.await
			.unwrap_err();
		assert!(!why(&err).contains("is not an IP address"), "{}", why(&err));
	}

	/// A grant withdrawn under an incident, and a pause, both stop new requests
	/// — and neither is a revocation, so what is already held stays served.
	#[tokio::test]
	async fn a_withdrawn_grant_and_a_pause_both_stop_requesting_without_dropping_what_is_held() {
		let (_dir, state) = state();
		hold(&state, "app.example.com", true).await;

		with_entitlement(&state, &["example.com"], false, false).await;
		let withdrawn = state.entitlement.read().await.clone().unwrap();
		assert_eq!(
			stands_down(&withdrawn),
			Some("this server holds no TLS grant")
		);
		assert!(state.serve("app.example.com").await.is_some());

		with_entitlement(&state, &["example.com"], true, true).await;
		let paused = state.entitlement.read().await.clone().unwrap();
		assert_eq!(
			stands_down(&paused),
			Some("canopy reports this server paused")
		);
		assert!(state.serve("app.example.com").await.is_some());

		with_entitlement(&state, &["example.com"], true, false).await;
		let entitled = state.entitlement.read().await.clone().unwrap();
		assert_eq!(stands_down(&entitled), None);
	}

	/// A name has to meet every test: a hooked site, an owner, and an entitlement
	/// that covers it. One without the rest is left to Caddy's own issuance.
	///
	/// spec: TLS#which-dns-names-are-certified
	#[tokio::test]
	async fn a_name_failing_any_test_is_not_ordered_for() {
		let (_dir, state) = state();
		with_entitlement(&state, &["example.com"], true, false).await;
		let entitlement = entitled(&state).await;

		// Hooked and owned, but outside the domains.
		let outside = sites(&["app.elsewhere.test"], &["app.elsewhere.test"]);
		assert!(
			state
				.targets_from(&entitlement, &outside, &[central()])
				.await
				.is_empty()
		);

		// Owned and covered, but the site does not name the daemon's endpoint.
		let unhooked = sites(&["app.example.com"], &[]);
		assert!(
			state
				.targets_from(&entitlement, &unhooked, &[central()])
				.await
				.is_empty()
		);

		// Hooked and covered, but no application on the host owns it.
		let hooked = sites(&["app.example.com"], &["app.example.com"]);
		assert!(
			state
				.targets_from(&entitlement, &hooked, &[])
				.await
				.is_empty()
		);

		// A host where no site names the endpoint orders nothing at all.
		assert!(
			state
				.targets_from(&entitlement, &CaddySites::default(), &[central()])
				.await
				.is_empty()
		);

		// And all of them together is ordered, for the owner.
		let targets = state
			.targets_from(&entitlement, &hooked, &[central()])
			.await;
		assert_eq!(names(&targets), vec!["app.example.com"]);
		assert_eq!(targets[0].application_type, "tamanu-central");
	}

	/// An explicit request is ordered whether or not Caddy serves the name or its
	/// site names the endpoint, until its first chain arrives, and after that
	/// while Caddy serves it.
	///
	/// spec: TLS#which-dns-names-are-certified
	#[tokio::test]
	async fn an_explicit_request_is_kept_until_its_first_chain_and_then_while_caddy_serves_it() {
		let (_dir, state) = state();
		with_entitlement(&state, &["example.com"], true, false).await;
		let entitlement = entitled(&state).await;
		state
			.explicit
			.lock()
			.await
			.insert("pre.example.com".into(), "msupply".into());

		// Caddy knows nothing of it yet, and the application named is the one
		// asked for, whatever the host's own attribution would say.
		let targets = state
			.targets_from(&entitlement, &CaddySites::default(), &[central()])
			.await;
		assert_eq!(
			targets,
			vec![Target {
				name: "pre.example.com".into(),
				application_type: "msupply".into(),
			}]
		);

		// Collected, and Caddy serves it on a site with no hook: still collected.
		hold(&state, "pre.example.com", true).await;
		let unhooked = sites(&["pre.example.com"], &[]);
		let targets = state
			.targets_from(&entitlement, &unhooked, &[central()])
			.await;
		assert_eq!(names(&targets), vec!["pre.example.com"]);
		assert_eq!(targets[0].application_type, "msupply");

		// Collected and no longer served: collection stops.
		let targets = state
			.targets_from(&entitlement, &CaddySites::default(), &[central()])
			.await;
		assert!(targets.is_empty());
		assert!(state.explicit.lock().await.is_empty());
	}

	/// The application named by a command is the one whose entitlement applies. A
	/// request kept for a type the machine no longer hosts is dropped.
	#[tokio::test]
	async fn an_explicit_request_is_held_to_the_application_it_names() {
		let (_dir, state) = state();
		*state.entitlement.write().await = Some(Entitlement::from_wire(&applications_wire()));
		let entitlement = entitled(&state).await;
		let mut explicit = state.explicit.lock().await;
		explicit.insert("a.one.test".into(), "tamanu-central".into());
		explicit.insert("c.one.test".into(), "nonesuch".into());
		// Named for the application that holds no TLS grant: not ordered.
		explicit.insert("b.two.test".into(), "tamanu-facility".into());
		drop(explicit);

		let targets = state
			.targets_from(&entitlement, &CaddySites::default(), &[])
			.await;
		assert_eq!(names(&targets), vec!["a.one.test"]);
		let explicit = state.explicit.lock().await;
		// Not ordered, but kept for when the grant arrives.
		assert!(explicit.contains_key("b.two.test"));
		assert!(!explicit.contains_key("c.one.test"));
	}

	/// With Caddy's configuration unreadable, what is known about the other names
	/// is left as it was rather than read as Caddy serving nothing.
	#[tokio::test]
	async fn an_unreadable_caddy_configuration_orders_only_requested_names_and_drops_nothing() {
		if delivery::caddy_sites().await.is_ok() {
			// A Caddy is answering on this machine; the case cannot be staged.
			return;
		}
		let (_dir, state) = state();
		with_entitlement(&state, &["example.com"], true, false).await;
		let entitlement = entitled(&state).await;
		state.note_wanted("hooked.example.com").await;
		state
			.record_failure(
				&target("wait.example.com"),
				refused(RefusalKind::Undeclared, "declare it"),
				Timestamp::now(),
			)
			.await;
		state
			.note_explicit("pre.example.com", "tamanu-central".into())
			.await
			.unwrap();

		let (targets, complete) = state.targets(&entitlement, false).await;
		assert!(!complete);
		assert_eq!(names(&targets), vec!["pre.example.com"]);
		assert!(state.wanted.lock().await.contains("hooked.example.com"));
		assert!(state.orders.read().await.contains_key("wait.example.com"));
	}

	/// A request a command made survives a restart until its first chain
	/// arrives.
	///
	/// spec: TLS#which-dns-names-are-certified
	#[tokio::test]
	async fn a_request_by_command_survives_a_restart() {
		let dir = tempfile::tempdir().unwrap();
		let before = CertificateState::new(dir.path().to_path_buf(), peer::Permitted::default());
		before
			.note_explicit("pre.example.com", "msupply".into())
			.await
			.unwrap();

		let after = CertificateState::new(dir.path().to_path_buf(), peer::Permitted::default());
		after
			.note_explicit("other.example.com", "msupply".into())
			.await
			.unwrap();
		let explicit = after.explicit.lock().await.clone();
		assert_eq!(explicit["pre.example.com"], "msupply");
		assert!(explicit.contains_key("other.example.com"));
		assert_eq!(certs::load_requests(dir.path()).await, explicit);
	}

	/// Once its first chain arrives, a requested name is renewed on the ordinary
	/// schedule rather than asked about on the pending backoff.
	///
	/// spec: TLS#which-dns-names-are-certified
	#[tokio::test]
	async fn a_requested_name_with_a_chain_is_not_pressing() {
		let (_dir, state) = state();
		*state.last_pass.write().await = Some(Timestamp::now());
		state
			.note_explicit("pre.example.com", "tamanu-central".into())
			.await
			.unwrap();
		let long_ago = Timestamp::now() - std::time::Duration::from_secs(3600);
		state.orders.write().await.insert(
			"pre.example.com".into(),
			Order {
				state: "issued".into(),
				asked_at: Some(long_ago),
				..Order::default()
			},
		);
		assert!(state.name_due("pre.example.com", Timestamp::now()).await);

		hold(&state, "pre.example.com", true).await;
		assert!(!state.name_due("pre.example.com", Timestamp::now()).await);
		assert!(!state.pass_due().await);
	}

	/// A pass that could not read Caddy's configuration or the host's
	/// applications backs off before the next, rather than one waking on every
	/// tick for a handshake's name it could not attempt.
	#[tokio::test]
	async fn a_pass_that_could_not_read_what_the_host_serves_backs_off_the_next() {
		let (_dir, state) = state();
		*state.last_pass.write().await = Some(Timestamp::now());
		state.note_wanted("app.example.com").await;
		assert!(state.pass_due().await);

		*state.incomplete.write().await = Some(Timestamp::now());
		assert!(!state.pass_due().await);

		*state.incomplete.write().await =
			Some(Timestamp::now() - std::time::Duration::from_secs(PENDING_RETRY.as_secs() + 1));
		assert!(state.pass_due().await);
	}

	/// Past the bound a new request is refused, and one already accepted can
	/// still be asked again.
	///
	/// spec: TLS#which-dns-names-are-certified
	#[tokio::test]
	async fn requests_by_command_are_bounded() {
		let (_dir, state) = state();
		for n in 0..EXPLICIT_LIMIT {
			state
				.note_explicit(&format!("n{n}.example.com"), "msupply".into())
				.await
				.unwrap();
		}
		assert!(
			state
				.note_explicit("over.example.com", "msupply".into())
				.await
				.is_err()
		);
		state
			.note_explicit("n0.example.com", "tamanu-central".into())
			.await
			.unwrap();
		assert_eq!(
			state.explicit.lock().await["n0.example.com"],
			"tamanu-central"
		);
	}

	/// A pause on the named application holds a requested name back without
	/// forgetting it, and neither it nor the name wakes a pass in the meantime.
	///
	/// spec: TLS#which-dns-names-are-certified
	#[tokio::test]
	async fn an_explicit_request_waits_out_its_applications_pause() {
		let (_dir, state) = state();
		*state.last_pass.write().await = Some(Timestamp::now());
		let mut wire = applications_wire();
		wire.applications[0].paused = true;
		*state.entitlement.write().await = Some(Entitlement::from_wire(&wire));
		let paused = entitled(&state).await;
		let type_slug = paused.applications[0].type_slug.clone().unwrap();
		let name = format!("pre.{}", paused.applications[0].domains[0]);
		state
			.explicit
			.lock()
			.await
			.insert(name.clone(), type_slug.clone());

		assert!(!state.pass_due().await);
		let targets = state
			.targets_from(&paused, &CaddySites::default(), &[])
			.await;
		assert!(!names(&targets).contains(&name.as_str()));
		assert!(state.explicit.lock().await.contains_key(&name));

		*state.entitlement.write().await = Some(Entitlement::from_wire(&applications_wire()));
		let lifted = entitled(&state).await;
		assert!(state.pass_due().await);
		let targets = state
			.targets_from(&lifted, &CaddySites::default(), &[])
			.await;
		assert!(names(&targets).contains(&name.as_str()));
	}

	#[tokio::test]
	async fn a_pending_order_is_retried_sooner_than_the_steady_pass_and_stops_once_it_resolves() {
		let (_dir, state) = state();
		*state.last_pass.write().await = Some(Timestamp::now());

		state.orders.write().await.insert(
			"app.example.com".into(),
			Order {
				state: "pending".into(),
				asked_at: Some(Timestamp::now()),
				..Order::default()
			},
		);
		assert!(!state.pass_due().await, "just asked; not due yet");

		let stale = Timestamp::now() - std::time::Duration::from_secs(300);
		state
			.orders
			.write()
			.await
			.get_mut("app.example.com")
			.unwrap()
			.asked_at = Some(stale);
		assert!(state.pass_due().await, "a pending order is retried sooner");

		state
			.orders
			.write()
			.await
			.get_mut("app.example.com")
			.unwrap()
			.state = "issued".into();
		assert!(
			!state.pass_due().await,
			"an order that resolved stops being retried"
		);
	}

	#[tokio::test]
	async fn a_name_asked_for_makes_a_pass_due() {
		let (_dir, state) = state();
		*state.last_pass.write().await = Some(Timestamp::now());
		assert!(!state.pass_due().await);
		state.note_wanted("app.example.com").await;
		assert!(state.pass_due().await);
	}

	#[tokio::test]
	async fn a_restart_serves_what_it_held_and_keeps_the_key_that_covers_it() {
		// Canopy keys what it holds by name and key, so a key that did not
		// survive would turn a collection into a fresh order.
		let dir = tempfile::tempdir().unwrap();
		let key = certs::generate_key().unwrap();
		let mut store = KeyStore::default();
		store.replace("app.example.com", &key);
		certs::store_keys(dir.path(), &store).await.unwrap();
		certs::store_chain(
			dir.path(),
			"app.example.com",
			"-----BEGIN CERTIFICATE-----\n",
		)
		.await
		.unwrap();

		let state = CertificateState::new(dir.path().to_path_buf(), peer::Permitted::default());
		state.load_from_disk().await.unwrap();

		let (chain, served_key) = state.serve("app.example.com").await.unwrap();
		assert!(chain.contains("BEGIN CERTIFICATE"));
		assert_eq!(
			certs::key_fingerprint(&KeyPair::from_pem(&served_key).unwrap()),
			certs::key_fingerprint(&key)
		);
	}

	#[tokio::test]
	async fn a_chain_whose_key_is_gone_is_not_served() {
		let dir = tempfile::tempdir().unwrap();
		certs::store_chain(
			dir.path(),
			"app.example.com",
			"-----BEGIN CERTIFICATE-----\n",
		)
		.await
		.unwrap();

		let state = CertificateState::new(dir.path().to_path_buf(), peer::Permitted::default());
		state.load_from_disk().await.unwrap();
		assert!(state.serve("app.example.com").await.is_none());
	}

	/// A request canopy refuses as undeclared is waiting on an operator, not
	/// failing: the order shows it with the reason canopy gave, and the DNS name
	/// is asked about on the steady schedule and no sooner.
	///
	/// spec: TLS#undeclared-and-denied-dns-names
	#[tokio::test]
	async fn an_undeclared_refusal_is_recorded_and_not_asked_again_between_steady_passes() {
		let (_dir, state) = state();
		*state.last_pass.write().await = Some(Timestamp::now());
		state.note_wanted("app.example.com").await;
		assert!(state.pass_due().await);

		let long_ago = Timestamp::now() - std::time::Duration::from_secs(3600);
		state
			.record_failure(
				&target("app.example.com"),
				refused(RefusalKind::Undeclared, "declare it in canopy"),
				long_ago,
			)
			.await;

		let orders = state.orders.read().await;
		let order = &orders["app.example.com"];
		assert_eq!(order.state, "undeclared");
		assert_eq!(order.reason(), Some("declare it in canopy"));
		assert_eq!(order.application_type.as_deref(), Some("tamanu-central"));
		drop(orders);

		// Still asked for, and long past any retry backoff, yet neither the name
		// nor a handshake for it brings a pass forward.
		assert!(state.wanted.lock().await.contains("app.example.com"));
		assert!(!state.name_due("app.example.com", Timestamp::now()).await);
		assert!(!state.pass_due().await);
		state.note_wanted("app.example.com").await;
		assert!(!state.pass_due().await);
	}

	/// A pass between steady ones reuses the applications last discovered rather
	/// than discovering them again.
	#[tokio::test]
	async fn the_host_applications_are_reused_between_steady_passes() {
		let (_dir, state) = state();
		*state.applications.write().await = Some(vec![msupply()]);
		assert_eq!(state.host_applications(false).await, Some(vec![msupply()]));
	}

	/// The command an operator runs after declaring a DNS name, or lifting its
	/// denial, asks canopy at once rather than waiting for the steady schedule.
	///
	/// spec: TLS#undeclared-and-denied-dns-names
	#[tokio::test]
	async fn a_name_a_command_asks_for_is_due_even_while_waiting_on_an_operator() {
		let (_dir, state) = state();
		for (name, kind) in [
			("a.example.com", RefusalKind::Undeclared),
			("b.example.com", RefusalKind::Denied),
		] {
			state
				.record_failure(
					&target(name),
					refused(kind, "an operator's"),
					Timestamp::now(),
				)
				.await;
			let now = Timestamp::now();
			assert!(!state.target_due(name, false, None, now).await);
			assert!(
				!state
					.target_due(name, false, Some("other.example.com"), now)
					.await
			);
			assert!(state.target_due(name, false, Some(name), now).await);
			assert!(state.target_due(name, true, None, now).await);
		}
	}

	#[tokio::test]
	async fn a_denied_refusal_is_held_the_same_way_and_keeps_the_chain_in_hand() {
		let (_dir, state) = state();
		with_entitlement(&state, &["example.com"], true, false).await;
		hold(&state, "app.example.com", true).await;
		state
			.record_failure(
				&target("app.example.com"),
				refused(RefusalKind::Denied, "an operator denied this"),
				Timestamp::now(),
			)
			.await;

		let orders = state.orders.read().await;
		assert_eq!(orders["app.example.com"].state, "denied");
		assert!(awaits_operator(&orders["app.example.com"]));
		drop(orders);
		assert!(!state.name_due("app.example.com", Timestamp::now()).await);

		// A denial is not a revocation.
		assert!(state.serve("app.example.com").await.is_some());
	}

	/// A later answer replaces the refusal, and the name is due again like any
	/// other.
	#[tokio::test]
	async fn an_accepted_request_replaces_the_refusal() {
		let (_dir, state) = state();
		state
			.record_failure(
				&target("app.example.com"),
				refused(RefusalKind::Undeclared, "declare it"),
				Timestamp::now(),
			)
			.await;
		assert!(awaits_operator(
			&state.orders.read().await["app.example.com"]
		));

		state.orders.write().await.insert(
			"app.example.com".into(),
			Order {
				state: "pending".into(),
				asked_at: Some(Timestamp::now() - std::time::Duration::from_secs(300)),
				application_type: Some("tamanu-central".into()),
				..Order::default()
			},
		);
		assert!(!awaits_operator(
			&state.orders.read().await["app.example.com"]
		));
		assert!(state.name_due("app.example.com", Timestamp::now()).await);
	}

	/// A type mismatch and `name-not-entitled` are ordinary failures: shown with
	/// canopy's reason, asked about again only on the steady schedule, and not
	/// mistaken for an operator being waited on.
	#[tokio::test]
	async fn any_other_refusal_is_an_ordinary_failure_with_canopys_reason() {
		let (_dir, state) = state();
		state
			.explicit
			.lock()
			.await
			.insert("app.example.com".into(), "msupply".into());
		state
			.record_failure(
				&target("app.example.com"),
				refused(RefusalKind::Other, "this machine hosts tamanu-central"),
				Timestamp::now(),
			)
			.await;

		let orders = state.orders.read().await;
		let order = &orders["app.example.com"];
		assert_eq!(order.state, "refused");
		assert!(!awaits_operator(order));
		assert_eq!(order.reason(), Some("this machine hosts tamanu-central"));
		assert!(order.last_error.is_some());
		drop(orders);

		// The command that named the type was answered by this report.
		assert!(state.explicit.lock().await.is_empty());
		state.note_wanted("app.example.com").await;
		assert!(
			!state
				.name_due(
					"app.example.com",
					Timestamp::now() + std::time::Duration::from_secs(3600)
				)
				.await
		);
	}

	/// Canopy unable to act on a requested DNS name for a reason on its own side
	/// keeps the request, asked about again on the steady schedule.
	///
	/// spec: TLS#which-dns-names-are-certified
	#[tokio::test]
	async fn a_conflict_keeps_the_request_for_the_steady_schedule() {
		let (_dir, state) = state();
		*state.last_pass.write().await = Some(Timestamp::now());
		state
			.note_explicit("pre.example.com", "tamanu-central".into())
			.await
			.unwrap();
		let long_ago = Timestamp::now() - std::time::Duration::from_secs(3600);
		state
			.record_failure(
				&target("pre.example.com"),
				refused(RefusalKind::Conflict, "no zone covers it"),
				long_ago,
			)
			.await;

		assert!(state.explicit.lock().await.contains_key("pre.example.com"));
		assert_eq!(
			state.orders.read().await["pre.example.com"].reason(),
			Some("no zone covers it")
		);
		assert!(!state.name_due("pre.example.com", Timestamp::now()).await);
		assert!(!state.pass_due().await);
		assert!(
			state
				.target_due(
					"pre.example.com",
					false,
					Some("pre.example.com"),
					Timestamp::now()
				)
				.await
		);
	}

	/// A failure with no answer from canopy keeps whatever state the order had,
	/// so a pending order is still retried.
	#[tokio::test]
	async fn a_failure_with_no_answer_keeps_the_order_state() {
		let (_dir, state) = state();
		state.orders.write().await.insert(
			"app.example.com".into(),
			Order {
				state: "pending".into(),
				..Order::default()
			},
		);
		state
			.record_failure(
				&target("app.example.com"),
				Failure {
					refusal: None,
					report: miette!("reaching canopy: timed out"),
				},
				Timestamp::now(),
			)
			.await;
		let orders = state.orders.read().await;
		assert!(orders["app.example.com"].pending());
		assert!(orders["app.example.com"].refusal.is_none());
		assert_eq!(
			orders["app.example.com"].reason(),
			Some("reaching canopy: timed out")
		);
	}

	/// A failure with no answer from canopy is not a later answer, so a refusal
	/// it follows is kept, in memory and on disk.
	///
	/// spec: TLS#undeclared-and-denied-dns-names
	#[tokio::test]
	async fn a_failure_with_no_answer_keeps_the_refusal() {
		let (dir, state) = state();
		state
			.record_failure(
				&target("app.example.com"),
				refused(RefusalKind::Undeclared, "declare it"),
				Timestamp::now(),
			)
			.await;
		state.persist_refusals().await;
		state
			.record_failure(
				&target("app.example.com"),
				Failure {
					refusal: None,
					report: miette!("reaching canopy: timed out"),
				},
				Timestamp::now(),
			)
			.await;
		state.persist_refusals().await;

		let orders = state.orders.read().await;
		assert!(awaits_operator(&orders["app.example.com"]));
		assert_eq!(orders["app.example.com"].reason(), Some("declare it"));
		drop(orders);
		assert!(
			certs::load_refusals(dir.path())
				.await
				.contains_key("app.example.com")
		);
	}

	/// A DNS name waiting on an operator is not mistaken for a failing one before
	/// the daemon has asked again.
	///
	/// spec: TLS#undeclared-and-denied-dns-names
	#[tokio::test]
	async fn refusals_survive_a_restart() {
		let dir = tempfile::tempdir().unwrap();
		let before = CertificateState::new(dir.path().to_path_buf(), peer::Permitted::default());
		before
			.record_failure(
				&target("a.example.com"),
				refused(RefusalKind::Undeclared, "declare it"),
				Timestamp::now(),
			)
			.await;
		before
			.record_failure(
				&target("b.example.com"),
				refused(RefusalKind::Denied, "denied"),
				Timestamp::now(),
			)
			.await;
		before
			.record_failure(
				&target("c.example.com"),
				refused(RefusalKind::Other, "mismatch"),
				Timestamp::now(),
			)
			.await;
		before.persist_refusals().await;

		let after = CertificateState::new(dir.path().to_path_buf(), peer::Permitted::default());
		after.load_from_disk().await.unwrap();
		let orders = after.orders.read().await;
		assert_eq!(orders["a.example.com"].state, "undeclared");
		assert_eq!(orders["a.example.com"].reason(), Some("declare it"));
		assert_eq!(orders["b.example.com"].state, "denied");
		assert!(orders["a.example.com"].application_type.is_some());
		assert_eq!(orders["c.example.com"].state, "refused");
		assert_eq!(orders["c.example.com"].reason(), Some("mismatch"));
		assert!(!awaits_operator(&orders["c.example.com"]));
	}

	/// A refusal is dropped once the DNS name leaves the set the daemon asks
	/// about, from memory and from disk.
	#[tokio::test]
	async fn a_refusal_for_a_name_no_longer_asked_about_is_dropped() {
		let dir = tempfile::tempdir().unwrap();
		let state = CertificateState::new(dir.path().to_path_buf(), peer::Permitted::default());
		state
			.record_failure(
				&target("a.example.com"),
				refused(RefusalKind::Undeclared, "declare it"),
				Timestamp::now(),
			)
			.await;
		state.persist_refusals().await;
		assert!(!certs::load_refusals(dir.path()).await.is_empty());

		state.prune_orders(&[]).await;
		state.persist_refusals().await;
		assert!(state.orders.read().await.is_empty());
		assert!(certs::load_refusals(dir.path()).await.is_empty());
	}

	#[tokio::test]
	async fn the_status_report_carries_the_refusal_the_reason_and_the_type_sent() {
		let (_dir, state) = state();
		state
			.record_failure(
				&target("a.example.com"),
				refused(RefusalKind::Undeclared, "declare it"),
				Timestamp::now(),
			)
			.await;
		let report = state.report().await;
		let row = &report["orders"][0];
		assert_eq!(row["name"], "a.example.com");
		assert_eq!(row["state"], "undeclared");
		assert_eq!(row["refusal"], "undeclared");
		assert_eq!(row["reason"], "declare it");
		assert_eq!(row["applicationType"], "tamanu-central");
	}

	/// The status endpoint is open: reporting what this server holds needs no
	/// privilege.
	#[test]
	fn only_the_status_endpoint_is_open() {
		let (_dir, state) = state();
		let task = CanopyNames::new(Arc::new(state));
		for endpoint in task.http_endpoints() {
			assert_eq!(
				endpoint.name == "status",
				!endpoint.guarded,
				"{}",
				endpoint.name
			);
		}
	}

	fn msupply() -> HostApplication {
		HostApplication {
			type_slug: "msupply".into(),
			service_names: ["api.msupply.internal".to_owned()].into(),
			..central()
		}
	}

	/// Two applications on one box, both holding the TLS grant: `a.shared.test`
	/// and `b.shared.test` are Tamanu central's and mSupply's by their sites.
	fn two_applications_wire(
		central_covers: &str,
		central_paused: bool,
	) -> bestool_canopy::schema::Entitlements {
		use bestool_canopy::schema::{ApplicationEntitlements, Entitlements};

		let app = |type_slug: &str, domain: &str, paused: bool| {
			ApplicationEntitlements::builder()
				.certificates(vec![])
				.domains(vec![domain.to_string()])
				.may_manage_dns(false)
				.may_manage_tls(true)
				.paused(paused)
				.registered_names(vec![])
				.type_(type_slug.parse().unwrap())
				.build()
		};
		Entitlements::builder()
			.applications(vec![
				app("tamanu-central", central_covers, central_paused),
				app("msupply", "supply.test", false),
			])
			.certificates(vec![])
			.domains(vec![])
			.may_manage_dns(false)
			.may_manage_tls(false)
			.paused(false)
			.registered_names(vec![])
			.build()
	}

	fn hooked_sites(hosts: &[(&str, &str)]) -> CaddySites {
		let routes: Vec<Value> = hosts
			.iter()
			.map(|(host, upstream)| {
				json!({
					"match": [{ "host": [host] }],
					"handle": [{ "handler": "reverse_proxy", "dynamic_upstreams": {
						"source": "a", "name": upstream, "port": "3000" } }],
				})
			})
			.collect();
		let subjects: Vec<&str> = hosts.iter().map(|(host, _)| *host).collect();
		CaddySites::from_config(&json!({"apps": {
			"http": { "servers": { "s": { "routes": routes } } },
			"tls": { "automation": { "policies": [{
				"subjects": subjects,
				"get_certificate": [{ "via": "http", "url": "http://127.0.0.1:8271/certificate" }],
			}] } },
		}}))
	}

	/// Every request carries the type of the application the DNS name belongs to,
	/// and is tested against that application's entitlement rather than the
	/// union.
	///
	/// spec: NAM#which-application-a-dns-name-belongs-to
	#[tokio::test]
	async fn each_dns_name_is_requested_for_the_application_it_belongs_to() {
		let (_dir, state) = state();
		let entitlement = Entitlement::from_wire(&two_applications_wire("tam.test", false));
		let seen = hooked_sites(&[
			("a.tam.test", "api.central.tamanu.internal"),
			("b.supply.test", "api.msupply.internal"),
			// Attributed to mSupply, under a domain only Tamanu central covers.
			("c.tam.test", "api.msupply.internal"),
		]);
		let targets = state
			.targets_from(&entitlement, &seen, &[central(), msupply()])
			.await;
		let by_name: Vec<(&str, &str)> = targets
			.iter()
			.map(|t| (t.name.as_str(), t.application_type.as_str()))
			.collect();
		assert_eq!(
			by_name,
			vec![
				("a.tam.test", "tamanu-central"),
				("b.supply.test", "msupply")
			]
		);
	}

	#[tokio::test]
	async fn a_dns_name_declared_by_an_application_is_requested_for_it() {
		use bestool_canopy::schema::{ApplicationEntitlements, Entitlements};

		let (_dir, state) = state();
		let declaring = ApplicationEntitlements::builder()
			.certificates(vec![])
			.domains(vec!["supply.test".to_string()])
			.may_manage_dns(false)
			.may_manage_tls(true)
			.paused(false)
			.registered_names(vec!["decl.supply.test".to_string()])
			.type_("msupply".parse().unwrap())
			.build();
		let entitlement = Entitlement::from_wire(
			&Entitlements::builder()
				.applications(vec![declaring])
				.certificates(vec![])
				.domains(vec![])
				.may_manage_dns(false)
				.may_manage_tls(false)
				.paused(false)
				.registered_names(vec![])
				.build(),
		);
		// A site no application is attributed, hooked, serving a declared name.
		let seen = hooked_sites(&[("decl.supply.test", "10.0.0.9:9000")]);
		let targets = state
			.targets_from(&entitlement, &seen, &[central(), msupply()])
			.await;
		assert_eq!(
			targets,
			vec![Target {
				name: "decl.supply.test".into(),
				application_type: "msupply".into(),
			}]
		);
	}

	/// One application being paused leaves the other collecting, and the server
	/// stands down only when none holds the grant or every one is paused.
	///
	/// spec: NAM#machines-hosting-several-applications
	#[tokio::test]
	async fn a_paused_application_is_skipped_and_the_server_stands_down_only_when_all_are() {
		let (_dir, state) = state();
		let mut entitlement = Entitlement::from_wire(&two_applications_wire("tam.test", true));
		let seen = hooked_sites(&[
			("a.tam.test", "api.central.tamanu.internal"),
			("b.supply.test", "api.msupply.internal"),
		]);
		let targets = state
			.targets_from(&entitlement, &seen, &[central(), msupply()])
			.await;
		assert_eq!(names(&targets), vec!["b.supply.test"]);
		assert_eq!(stands_down(&entitlement), None);

		entitlement.applications[1].paused = true;
		assert_eq!(
			stands_down(&entitlement),
			Some("canopy reports this server paused")
		);

		entitlement.applications[1].paused = false;
		entitlement
			.applications
			.iter_mut()
			.for_each(|app| app.may_manage_tls = false);
		assert_eq!(
			stands_down(&entitlement),
			Some("this server holds no TLS grant")
		);
	}

	/// The domain test is against the named application, even where another
	/// application on the host covers the DNS name.
	///
	/// spec: TLS#commands
	#[tokio::test]
	async fn requesting_outside_the_named_applications_domains_is_refused() {
		let (_dir, state) = state();
		*state.entitlement.write().await = Some(Entitlement::from_wire(&two_applications_wire(
			"tam.test", false,
		)));
		let task = CanopyNames::new(Arc::new(state));
		let endpoints = task.http_endpoints();
		let request = endpoints
			.iter()
			.find(|endpoint| endpoint.name == "request")
			.unwrap();

		let ask = |name: &str, application_type: Option<&str>| {
			let mut ctx = detached_ctx();
			ctx.query.insert("name".into(), name.into());
			if let Some(application_type) = application_type {
				ctx.query.insert("type".into(), application_type.into());
			}
			(request.handler)(ctx)
		};

		// mSupply covers `supply.test`, Tamanu central does not.
		match ask("x.supply.test", Some("tamanu-central")).await {
			TaskEndpointResponse::Error { message, .. } => {
				assert!(message.contains("tamanu-central"), "{message}")
			}
			_ => panic!("expected a refusal"),
		}
		// And a request that does not say which application is refused outright.
		match ask("x.tam.test", None).await {
			TaskEndpointResponse::Error { message, .. } => {
				assert!(message.contains("`type`"), "{message}")
			}
			_ => panic!("expected a refusal"),
		}
	}

	/// Both commands are held to the named application's pause, grant, and
	/// domains, in that order; a type canopy has no entry for is held to the
	/// machine's.
	///
	/// spec: NAM#how-canopy-resolves-a-request
	#[test]
	fn a_command_is_permitted_by_the_application_it_names() {
		let paused = Entitlement::from_wire(&two_applications_wire("tam.test", true));
		let err = permit(&paused, "x.tam.test", Some("tamanu-central"), Grant::Tls).unwrap_err();
		assert!(err.to_string().contains("paused"), "{err}");

		let live = Entitlement::from_wire(&two_applications_wire("tam.test", false));
		permit(&live, "x.tam.test", Some("tamanu-central"), Grant::Tls).unwrap();
		let err = permit(&live, "x.tam.test", Some("tamanu-central"), Grant::Dns).unwrap_err();
		assert!(err.to_string().contains("DNS records"), "{err}");
		let err = permit(&live, "x.supply.test", Some("tamanu-central"), Grant::Tls).unwrap_err();
		assert!(err.to_string().contains("tamanu-central"), "{err}");

		let err = permit(&live, "x.supply.test", Some("nonesuch"), Grant::Tls).unwrap_err();
		assert!(err.to_string().contains("tamanu-central, msupply"), "{err}");
		permit(&live, "x.supply.test", None, Grant::Tls).unwrap();
		let err = permit(&live, "x.other.test", None, Grant::Tls).unwrap_err();
		assert!(err.to_string().contains("this server's group"), "{err}");
	}

	/// A request for an application that is paused is refused with the reason,
	/// rather than accepted and held back with nothing to say why.
	///
	/// spec: TLS#commands
	#[tokio::test]
	async fn requesting_for_a_paused_application_is_refused() {
		let (_dir, state) = state();
		*state.entitlement.write().await = Some(Entitlement::from_wire(&two_applications_wire(
			"tam.test", true,
		)));
		let state = Arc::new(state);
		let task = CanopyNames::new(state.clone());
		let endpoints = task.http_endpoints();
		let request = endpoints
			.iter()
			.find(|endpoint| endpoint.name == "request")
			.unwrap();

		let mut ctx = detached_ctx();
		ctx.query.insert("name".into(), "x.tam.test".into());
		ctx.query.insert("type".into(), "tamanu-central".into());
		match (request.handler)(ctx).await {
			TaskEndpointResponse::Error { message, .. } => {
				assert!(message.contains("paused"), "{message}")
			}
			_ => panic!("expected a refusal"),
		}
		assert!(state.explicit.lock().await.is_empty());
	}
}