rustango 0.61.0

Batteries-included web framework for Rust: ORM + migrations + auto-admin + multi-tenancy + audit log + auth (sessions, JWT, OAuth2/OIDC, HMAC) + APIs (ViewSet, OpenAPI auto-derive, JSON:API) + jobs (in-mem + Postgres) + email + media (S3 / R2 / B2 / MinIO + presigned uploads + collections + tags) + production middleware (CSRF, CSP, rate-limiting, compression, idempotency, etc.).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
//! Creating a tenant from the operator console (#1322).
//!
//! Until now the console could list, edit, re-brand and impersonate —
//! but not create. An operator who needed a new tenant needed shell
//! access to a box holding registry credentials.
//!
//! ## Why the stream reads the table instead of a channel
//!
//! The obvious design is an in-process broadcast: provisioning pushes
//! events, the SSE handler subscribes. It does not survive contact
//! with two pods — the operator's stream can land on a different pod
//! from the one doing the work, and they watch an empty page while
//! provisioning succeeds somewhere else. It does not survive a reload
//! either, because a broadcast has no history.
//!
//! So the run is persisted (#1321) and this polls it. Unglamorous, and
//! it is what makes a reconnect resume exactly where it left off and a
//! second pod see everything. `Last-Event-ID` is the `seq` column.
//!
//! ## Who may do this
//!
//! Provisioning routes are mounted **only** when the deployment builds
//! the console through [`super::router_with_provisioning`]. That is a
//! deployment-level gate, and it is the only one: operators are
//! uniformly fully capable by design (#1342), so there is no
//! per-operator permission to check here and none is coming. What a
//! console can do is decided when it is assembled, not per request.

use std::collections::HashMap;
use std::time::Duration;

use axum::body::Body;
use axum::extract::{Extension, Path, Query, State};
use axum::http::{HeaderMap, StatusCode};
use axum::response::sse::{Event, KeepAlive, Sse};
use axum::response::{IntoResponse, Redirect, Response};
use axum::Form;
use tera::Context;

use super::{inject_op_brand, withheld, withheld_in, ConsoleState};
use crate::sql::connect_diagnosis::redact;
use crate::tenancy::auth;
use crate::tenancy::first_admin::{FirstAdmin, Handover};
use crate::tenancy::org::{BackendKind, StorageMode};
use crate::tenancy::preflight::{self, Preflight};
use crate::tenancy::provision::{self, DatabaseSource, ProvisionRequest, TenantStorage};
use crate::tenancy::provision_store::{self as store, RunState};

/// How often the stream looks for new events.
///
/// Short enough that a checklist feels live, long enough that an idle
/// stream is not a busy loop against the registry. Each poll is one
/// indexed `SELECT … WHERE run_id = ? AND seq > ?`.
const POLL_EVERY: Duration = Duration::from_millis(500);

/// Stop streaming a run that never terminates, so an abandoned browser
/// tab cannot hold a connection (and a poll loop) open indefinitely.
/// A provisioning run that has not finished in this long is stuck, and
/// the non-streaming view still shows whatever it did manage.
const STREAM_MAX: Duration = Duration::from_secs(30 * 60);

// ---------------------------------------------------------------- new

/// The create form.
pub(super) async fn org_new_form(
    State(state): State<ConsoleState>,
    Extension(op): Extension<auth::Operator>,
) -> Response<Body> {
    render_form(&state, &op, &HashMap::new(), None).await
}

async fn render_form(
    state: &ConsoleState,
    op: &auth::Operator,
    form: &HashMap<String, String>,
    error: Option<&str>,
) -> Response<Body> {
    let mut ctx = Context::new();
    inject_op_brand(&mut ctx, &state.op_brand);
    ctx.insert("section", "orgs");
    ctx.insert("operator_username", &op.username);
    // A URL may carry a password, so it is never sent back.
    let prefill: HashMap<&String, &String> =
        form.iter().filter(|(k, _)| *k != "database_url").collect();
    ctx.insert("prefill", &prefill);
    ctx.insert("error", &error);
    ctx.insert("key_warning", &super::db_servers::key_warning());
    // Schema-mode is Postgres-only by language. Offering it on a
    // SQLite or MySQL registry would be offering a choice that can
    // only fail, so the form does not.
    ctx.insert(
        "schema_mode_available",
        &(state.registry.dialect().name() == "postgres"),
    );
    // A tenant always uses the registry's backend.
    ctx.insert("backend", BackendKind::of_pool(&state.registry).as_str());
    let servers: Vec<String> = crate::tenancy::db_server::list(&state.registry)
        .await
        .unwrap_or_default()
        .into_iter()
        .filter(|s| s.active)
        .map(|s| s.name)
        .collect();
    ctx.insert(
        "default_source",
        // Isolated by default: the registry path shares its login.
        if servers.is_empty() { "url" } else { "server" },
    );
    ctx.insert("servers", &servers);
    // The first-admin fields appear only where a handover is set up.
    let handover = state
        .provisioner
        .as_ref()
        .and_then(|p| p.first_admin_handover());
    ctx.insert(
        "first_admin",
        &handover.map(|h| if h.mails() { "mail" } else { "link" }),
    );
    // What a derived URL will look like, with the password removed.
    // The real one is built server-side at submit time; the browser
    // never sees the registry's credentials.
    ctx.insert(
        "derived_url_example",
        &state
            .provisioner
            .as_ref()
            .and_then(|p| {
                provision::tenant_url_on_registry_server(&p.registry_url(), "tenant_<slug>")
            })
            .map(|u| redact(&u)),
    );
    render(state, "op_orgs_new.html", &ctx)
}

use super::render;

/// Turn the submitted form into a request, or say what is wrong with it.
///
/// `registry_url` is used to derive the tenant's URL when the operator
/// supplied a database *name* rather than a full URL — see
/// [`provision::tenant_url_on_registry_server`]. It is never rendered.
fn request_from_form(
    form: &HashMap<String, String>,
    registry_url: Option<&str>,
) -> Result<ProvisionRequest, String> {
    let field = |k: &str| form.get(k).map(|v| v.trim()).filter(|v| !v.is_empty());

    let slug = field("slug")
        .ok_or("A slug is required — it is the tenant's globally unique name.")?
        .to_owned();

    let mode = match field("storage_mode").unwrap_or("database") {
        "schema" => StorageMode::Schema,
        _ => StorageMode::Database,
    };
    // The form shows the registry's backend; a posted one is checked
    // against it by the provisioner.
    let backend = field("backend_kind")
        .map(|b| BackendKind::parse(b).map_err(|got| format!("Unknown backend `{got}`.")))
        .transpose()?;
    if let Some(backend) = backend {
        backend
            .validate_storage_mode(mode)
            .map_err(ToOwned::to_owned)?;
    }

    let port = match field("port") {
        Some(raw) => Some(
            raw.parse::<i32>()
                .map_err(|_| format!("Port must be a whole number, got `{raw}`."))?,
        ),
        None => None,
    };

    // Where the tenant's database URL comes from, in order of
    // deference to what the operator actually said:
    //
    // 1. an explicit URL (the advanced escape hatch — another server);
    // 2. a database *name* on the registry's server, derived here so
    //    the registry password never reaches the browser and never
    //    travels back in a form post;
    // 3. nothing, for schema-mode, which has no separate database.
    let storage = match (mode, field("database_source")) {
        (StorageMode::Schema, _) => {
            TenantStorage::Schema(field("schema_name").map(ToOwned::to_owned))
        }
        (_, Some("server")) => TenantStorage::Database(DatabaseSource::Server(
            field("server")
                .ok_or("Choose the saved database server to create the database on.")?
                .to_owned(),
        )),
        (_, Some("url")) => TenantStorage::Database(DatabaseSource::Url(
            field("database_url")
                .ok_or("Enter the existing database's URL.")?
                .to_owned(),
        )),
        (_, Some("registry")) => {
            TenantStorage::Database(DatabaseSource::Url(existing_database_url(
                &|k| if k == "database_url" { None } else { field(k) },
                &slug,
                registry_url,
            )?))
        }
        // A post from before the choice existed.
        (_, _) => TenantStorage::Database(DatabaseSource::Url(existing_database_url(
            &field,
            &slug,
            registry_url,
        )?)),
    };

    let first_admin = match (field("admin_email"), field("admin_username")) {
        (Some(email), username) => Some(FirstAdmin::new(email, username)?),
        (None, Some(_)) => {
            return Err("Enter the first admin's email, or clear the username.".into())
        }
        (None, None) => None,
    };

    Ok(ProvisionRequest {
        backend,
        display_name: field("display_name").map(ToOwned::to_owned),
        host_pattern: field("host_pattern").map(ToOwned::to_owned),
        port,
        path_prefix: field("path_prefix").map(ToOwned::to_owned),
        run_migrations: !form.contains_key("no_migrate"),
        first_admin,
        ..ProvisionRequest::new(slug, storage)
    })
}

/// An existing database: the URL given, or a database name derived on
/// the registry's server.
fn existing_database_url<'a>(
    field: &impl Fn(&str) -> Option<&'a str>,
    slug: &str,
    registry_url: Option<&str>,
) -> Result<String, String> {
    match field("database_url") {
        Some(explicit) => Ok(explicit.to_owned()),
        None => {
            // A blank database name means the default the form shows:
            // `tenant_<slug>`. Applying it here and not only in the
            // helper text matters — the page promises it, and a
            // promise the submit does not honour is worse than having
            // no default at all. (It was: the form said "blank uses
            // tenant_globex" and the submit answered "database mode
            // needs a database URL".)
            let name =
                field("database_name").map_or_else(|| format!("tenant_{slug}"), ToOwned::to_owned);
            let registry = registry_url
                .ok_or("This console cannot derive a database URL — supply a full URL instead.")?;
            provision::tenant_url_on_registry_server(registry, &name).ok_or_else(|| {
                format!(
                    "Could not derive a URL for database `{name}` from the registry's own \
                     connection. Supply a full URL instead."
                )
            })
        }
    }
}

/// Create the tenant, then redirect to its run.
pub(super) async fn org_new_submit(
    State(state): State<ConsoleState>,
    Extension(op): Extension<auth::Operator>,
    headers: HeaderMap,
    Form(form): Form<HashMap<String, String>>,
) -> Response<Body> {
    let provisioner = state
        .provisioner
        .as_ref()
        .expect("provisioning routes are only mounted when a provisioner is supplied");

    let registry_url = provisioner.registry_url();
    let request = match request_from_form(&form, Some(&registry_url)) {
        Ok(r) => r,
        Err(msg) => return render_form(&state, &op, &form, Some(&msg)).await,
    };
    let handover = provisioner.first_admin_handover();
    if request.first_admin.is_some() && handover.is_none() {
        let msg = "This console cannot hand over a first admin; leave the admin email empty.";
        return render_form(&state, &op, &form, Some(msg)).await;
    }

    // Provisioning runs inline. It is slower than a request should be
    // — the migrate step is seconds — but the alternative is spawning
    // and losing the failure, and the operator is watching a form they
    // just submitted. The stream view is for *re*-connecting, not for
    // escaping this wait.
    let outcome = provisioner
        .provision(&request, Some(&op.username), None)
        .await;
    let operator_id = op.id.get().copied().unwrap_or_default();
    let mut extra = serde_json::Map::new();
    // A failure can come after the Org row lands, so it is audited too.
    let verb = match &outcome {
        Ok((run, _)) => {
            let run_id = run.id.get().copied().unwrap_or_default();
            extra.insert("run_id".into(), serde_json::json!(run_id));
            "create"
        }
        Err(_) => "create_failed",
    };
    super::emit_op_audit(&state.registry, &request.slug, operator_id, verb, extra).await;
    match outcome {
        Ok((run, mut outcome)) => {
            let run_id = run.id.get().copied().unwrap_or_default();
            let (Some(handover), Some(_)) = (handover, &outcome.first_admin) else {
                return Redirect::to(&format!("provision/{run_id}")).into_response();
            };
            let host = headers
                .get(axum::http::header::HOST)
                .and_then(|v| v.to_str().ok());
            if let Err(e) = handover
                .hand_over_outcome(&state.registry, &mut outcome, host)
                .await
            {
                tracing::warn!(target: "rustango::tenancy::operator_console", run_id, error = %e, "first admin handover failed");
            }
            // Rendered here, not redirected: the link is shown this once.
            let shown = outcome.first_admin.as_ref().map(|admin| HandoverView {
                username: admin.username.clone(),
                mailed_to: match &admin.handover {
                    Some(Handover::Mailed { to }) => Some(to.clone()),
                    _ => None,
                },
                mail_failed: matches!(admin.handover, Some(Handover::MailFailed(_))),
                link: admin
                    .handover
                    .as_ref()
                    .and_then(Handover::link)
                    .map(|l| l.expose().to_owned()),
                hours: handover.ttl_hours(),
            });
            let mut resp = render_run(&state, &op, run_id, shown.as_ref()).await;
            crate::tenancy::first_admin::guard_link_page(resp.headers_mut());
            resp
        }
        // A failure before the row lands — a taken slug, an unreachable
        // database — comes back here rather than as a run to watch,
        // because there is nothing to watch. Re-render with the
        // operator's input intact so they can fix one field.
        Err(e) => {
            let msg = e.user_facing().unwrap_or_else(|| {
                withheld(
                    "operator_console::provision",
                    &request.slug,
                    "Could not create the tenant",
                    &e,
                )
            });
            render_form(&state, &op, &form, Some(&msg)).await
        }
    }
}

// ---------------------------------------------------- test connection

/// Reach the database the form names, and report the taxonomy.
///
/// Writes nothing, creates nothing. Returns a fragment the form drops
/// in place, so this is usable from a button without a page reload.
pub(super) async fn test_connection(
    State(state): State<ConsoleState>,
    Extension(op): Extension<auth::Operator>,
    Form(form): Form<HashMap<String, String>>,
) -> Response<Body> {
    let field = |k: &str| form.get(k).map(|s| s.trim()).filter(|s| !s.is_empty());

    // A new database on a saved server: report its admin's rights.
    if field("storage_mode") != Some("schema") && field("database_source") == Some("server") {
        let Some(name) = field("server") else {
            return probe_bad("Choose a saved database server first.");
        };
        // With a slug, everything a submit checks before DDL, dry.
        if field("slug").is_some() {
            let Some(p) = state.provisioner.as_ref() else {
                return probe_bad("This console cannot create tenants.");
            };
            let request = match request_from_form(&form, None) {
                Ok(r) => r,
                Err(msg) => return probe_bad(&msg),
            };
            let secrets = state
                .pools
                .as_ref()
                .map_or_else(crate::tenancy::TenantSecrets::standard, |p| {
                    p.tenant_secrets()
                });
            let dry = provision::dry_run_on_server(
                &state.registry,
                &secrets,
                &p.registry_url(),
                &request,
            )
            .await;
            if let Err(e) = dry {
                let msg = e.user_facing().unwrap_or_else(|| {
                    withheld(
                        "operator_console::test_connection",
                        name,
                        "Could not check",
                        &e,
                    )
                });
                return probe_bad(&msg);
            }
        }
        return super::db_servers::probe_response(&state, &op, name).await;
    }

    // Probe exactly what a submit would use — an explicit URL, or the
    // one derived from the database name. Probing a different target
    // from the one about to be provisioned is worse than not probing
    // at all, because it reports confidence in the wrong thing.
    let target = if let Some(explicit) = field("database_url") {
        Some(explicit.to_owned())
    } else {
        {
            let db = field("database_name")
                .map(ToOwned::to_owned)
                .or_else(|| field("slug").map(|s| format!("tenant_{s}")));
            match (state.provisioner.as_ref(), db) {
                (Some(p), Some(db)) => {
                    provision::tenant_url_on_registry_server(&p.registry_url(), &db)
                }
                _ => None,
            }
        }
    };
    let Some(url) = target else {
        return probe_bad("Enter a slug, a database name, or a full URL first.");
    };
    // Answer the same question the submit will: resolve the reference,
    // then the refusals that have nothing to do with reachability. The
    // registry's own database is reachable and this role *can* create
    // tables in it, so the probe used to bless the one target
    // provisioning refuses outright.
    // Checks only: a probe stores nothing, so it neither seals nor warns.
    let secrets = state
        .pools
        .as_ref()
        .map_or_else(crate::tenancy::TenantSecrets::standard, |p| {
            p.tenant_secrets()
        });
    let registry_url = state.provisioner.as_ref().map(|p| p.registry_url());
    let resolved =
        provision::check_database_url(&url, &secrets, &state.registry, registry_url.as_deref())
            .await;
    let url = match resolved {
        Ok(u) => u,
        Err(crate::tenancy::TenancyError::Validation(msg)) => return probe_bad(&msg),
        Err(_) => return probe_bad("Could not check this database URL."),
    };

    match preflight::check(&url, &Preflight::default()).await {
        Ok(ok) => Probe::Ok {
            endpoint: Some(ok.endpoint),
            message: "This role can create tables, so migrations will run.",
        }
        .into_response(),
        // The diagnosis already leads with what to change — see
        // `sql::connect_diagnosis`. Rendering it verbatim is the point.
        Err(d) => probe_bad(&d.to_string()),
    }
}

// ------------------------------------------------------------ run view

/// Probe an *existing* tenant's database.
///
/// The create form's probe takes a URL from the form, which an existing
/// tenant cannot use: its URL carries a password and may be a secret
/// reference. This resolves server-side from the slug, so the URL never
/// reaches the browser.
///
/// Without it, testing a tenant whose database moved meant starting to
/// create a different tenant and borrowing that button.
pub(super) async fn test_tenant_connection(
    State(state): State<ConsoleState>,
    Extension(_op): Extension<auth::Operator>,
    Path(slug): Path<String>,
) -> Response<Body> {
    use crate::core::Column as _;
    use crate::sql::FetcherPool as _;

    let Some(pools) = state.pools.clone() else {
        return probe_bad("This console is read-only.");
    };
    let rows: Vec<super::super::Org> = match super::super::Org::objects()
        .where_(super::super::Org::slug.eq(slug.clone()))
        .fetch(&state.registry)
        .await
    {
        Ok(r) => r,
        Err(e) => {
            return probe_bad(&withheld(
                "operator_console::test_tenant_connection",
                &slug,
                "Could not read the tenant",
                &e,
            ))
        }
    };
    let Some(org) = rows.into_iter().next() else {
        return probe_bad(&format!("No tenant `{slug}`."));
    };

    if org.database_url.is_none() {
        return Probe::Ok {
            endpoint: None,
            message: "Schema-mode: this tenant lives in the registry's own database, which is \
                      already connected. There is no separate connection to test.",
        }
        .into_response();
    }

    let url = match pools.resolved_database_url(&org).await {
        Ok(u) => u,
        Err(e) => {
            return probe_bad(&withheld(
                "operator_console::test_tenant_connection",
                &slug,
                "Could not resolve the tenant's database URL",
                &e,
            ))
        }
    };
    match preflight::check(&url, &Preflight::default()).await {
        Ok(ok) => Probe::Ok {
            endpoint: Some(ok.endpoint),
            message: "This role can create tables.",
        }
        .into_response(),
        Err(d) => probe_bad(&d.to_string()),
    }
}

/// A connection probe's result, as JSON: the console builds the DOM
/// from it, so no server HTML reaches `innerHTML` (#2144).
#[derive(serde::Serialize)]
#[serde(tag = "status", rename_all = "lowercase")]
enum Probe {
    Ok {
        endpoint: Option<String>,
        message: &'static str,
    },
    Bad {
        message: String,
    },
}

impl IntoResponse for Probe {
    fn into_response(self) -> Response<Body> {
        axum::Json(self).into_response()
    }
}

fn probe_bad(message: &str) -> Response<Body> {
    Probe::Bad {
        message: message.to_owned(),
    }
    .into_response()
}

/// Every provisioning run, newest first.
///
/// A run was reachable only by its id — in practice only from the
/// redirect that created it, so navigating away stranded the record.
pub(super) async fn provision_runs_index(
    State(state): State<ConsoleState>,
    Extension(op): Extension<auth::Operator>,
    Query(q): Query<super::PageQuery>,
) -> Response<Body> {
    use crate::core::Model as _;

    let paged =
        match super::Paged::of_model(&state.registry, store::ProvisioningRun::SCHEMA, q.page).await
        {
            Ok(p) => p,
            Err(e) => {
                return super::server_error("could not count the runs", &e);
            }
        };
    let runs = match store::recent_runs(&state.registry, paged.limit, paged.offset).await {
        Ok(r) => r,
        Err(e) => {
            return super::server_error("could not read the run list", &e);
        }
    };

    let view: Vec<_> = runs
        .iter()
        .map(|r| {
            let state_str = r.state.clone();
            let kind = store::RunKind::parse(&r.kind);
            serde_json::json!({
                "id": r.id.get().copied().unwrap_or_default(),
                "kind": kind.as_str(),
                "is_migrate": kind == store::RunKind::Migrate,
                // A batch migrate spans every tenant, so it stores no
                // slug; the events name them as they go.
                "slug": if r.slug.is_empty() { "all tenants".to_owned() } else { r.slug.clone() },
                "state": state_str,
                "failed": RunState::parse(&r.state) == RunState::Failed,
                "running": !RunState::parse(&r.state).is_terminal(),
                "storage_mode": r.storage_mode,
                "backend_kind": r.backend_kind,
                "requested_by": r.requested_by,
                "error": r.error,
                "started_at": r.started_at.get()
                    .map(|t| t.format("%Y-%m-%d %H:%M:%S UTC").to_string()),
                // How long it took, rather than a second wide timestamp
                // column: the finish time on its own says little that
                // the start time and a duration do not.
                "took": took(r),
            })
        })
        .collect();

    let mut ctx = Context::new();
    inject_op_brand(&mut ctx, &state.op_brand);
    ctx.insert("section", "orgs");
    ctx.insert("operator_username", &op.username);
    ctx.insert("runs", &view);
    paged.inject(&mut ctx, "/orgs/provision", "");
    render(&state, "op_provision_runs.html", &ctx)
}

/// How long a finished run took, or `None` while it is still going.
fn took(run: &store::ProvisioningRun) -> Option<String> {
    let started = run.started_at.get()?;
    let finished = run.finished_at?;
    let ms = (finished - *started).num_milliseconds();
    if ms < 0 {
        // Clocks on two pods can disagree; a negative duration is not
        // information worth rendering.
        return None;
    }
    // Integer arithmetic rather than a float divide: one decimal place
    // of a millisecond count needs no `f64`, and `i64 as f64` loses
    // precision for large values.
    Some(if ms < 1000 {
        format!("{ms}ms")
    } else {
        format!("{}.{}s", ms / 1000, (ms % 1000) / 100)
    })
}

/// The non-streaming view of a run: what it did, whether it finished.
///
/// Also the page a finished run settles into, and the audit trail
/// someone comes back to later.
pub(super) async fn provision_run_view(
    State(state): State<ConsoleState>,
    Extension(op): Extension<auth::Operator>,
    Path(run_id): Path<i64>,
) -> Response<Body> {
    render_run(&state, &op, run_id, None).await
}

/// A first admin's handover, for the one render that shows it.
#[derive(serde::Serialize)]
struct HandoverView {
    username: String,
    mailed_to: Option<String>,
    mail_failed: bool,
    link: Option<String>,
    hours: u64,
}

async fn render_run(
    state: &ConsoleState,
    op: &auth::Operator,
    run_id: i64,
    handover: Option<&HandoverView>,
) -> Response<Body> {
    let Some(mut run) = (match store::run_by_id(&state.registry, run_id).await {
        Ok(r) => r,
        Err(e) => return super::server_error("operator_console", &e),
    }) else {
        return (StatusCode::NOT_FOUND, format!("no run {run_id}")).into_response();
    };
    // A run whose process died reads as interrupted, not `running`.
    if matches!(store::close_if_stale(&state.registry, &run).await, Ok(true)) {
        if let Ok(Some(fresh)) = store::run_by_id(&state.registry, run_id).await {
            run = fresh;
        }
    }
    let events = store::events_since(&state.registry, run_id, 0)
        .await
        .unwrap_or_default();

    let state_str = run.state.clone();
    let parsed = RunState::parse(&state_str);
    let mut ctx = Context::new();
    inject_op_brand(&mut ctx, &state.op_brand);
    ctx.insert("section", "orgs");
    ctx.insert("operator_username", &op.username);
    ctx.insert("run_id", &run_id);
    let kind = store::RunKind::parse(&run.kind);
    let is_migrate = kind == store::RunKind::Migrate;
    ctx.insert("is_migrate", &is_migrate);
    // Chosen here rather than in the template: Tera has no ternary.
    ctx.insert("verb", kind.verb());
    ctx.insert(
        "slug",
        &if run.slug.is_empty() {
            "all tenants".to_owned()
        } else {
            run.slug.clone()
        },
    );
    ctx.insert("state", &state_str);
    ctx.insert("terminal", &parsed.is_terminal());
    ctx.insert("error", &run.error);
    ctx.insert(
        "events",
        &step_rows(&events)
            .iter()
            .map(|r| {
                serde_json::json!({
                    "seq": r.seq,
                    "step": r.step,
                    "status": r.status,
                    "parts": message_parts(&r.message),
                })
            })
            .collect::<Vec<_>>(),
    );
    ctx.insert("handover", &handover);
    render(state, "op_provision_run.html", &ctx)
}

/// One row per step at its latest status; `info` rows stay as they are.
#[derive(Debug, PartialEq)]
struct StepRow<'a> {
    seq: i64,
    step: &'a str,
    status: &'a str,
    message: &'a str,
}

fn step_rows(events: &[store::ProvisioningEvent]) -> Vec<StepRow<'_>> {
    let mut rows: Vec<StepRow<'_>> = Vec::with_capacity(events.len());
    for e in events {
        let row = StepRow {
            seq: e.seq,
            step: &e.step,
            status: &e.status,
            message: &e.message,
        };
        let earlier = (e.status != "info")
            .then(|| {
                rows.iter_mut()
                    .rev()
                    .find(|r| r.step == row.step && r.status != "info")
            })
            .flatten();
        match earlier {
            Some(r) => {
                // Keep an earlier detail unless this event brings its own,
                // or the earlier one was a failure since retried.
                if !row.message.is_empty() || r.status == "failed" {
                    r.message = row.message;
                }
                r.seq = row.seq;
                r.status = row.status;
            }
            None => rows.push(row),
        }
    }
    rows
}

/// `a `b` c` as text and code parts, for `<code>` spans the template escapes.
fn message_parts(message: &str) -> Vec<serde_json::Value> {
    let pieces: Vec<&str> = message.split('`').collect();
    // An odd number of backticks is not markup: show it as written.
    if pieces.len() % 2 == 0 {
        return vec![serde_json::json!({ "text": message, "code": false })];
    }
    pieces
        .iter()
        .enumerate()
        .filter(|(_, p)| !p.is_empty())
        .map(|(i, p)| serde_json::json!({ "text": p, "code": i % 2 == 1 }))
        .collect()
}

#[derive(serde::Deserialize, Default)]
pub(super) struct StreamQuery {
    /// Resume point, for clients that would rather pass it explicitly
    /// than through the `Last-Event-ID` header.
    after: Option<i64>,
}

/// Stream a run's events as they land.
///
/// Replays everything from `after` (or `Last-Event-ID`, or the
/// beginning) and then follows, **terminating** when the run reaches a
/// terminal state. A stream that never ends is a connection leak and
/// gives a browser no way to know it is watching a finished run.
pub(super) async fn provision_run_stream(
    State(state): State<ConsoleState>,
    Extension(_op): Extension<auth::Operator>,
    Path(run_id): Path<i64>,
    Query(q): Query<StreamQuery>,
    headers: HeaderMap,
    // `impl IntoResponse` rather than naming `Sse<impl Stream<…>>`:
    // the `Stream` trait lives in `futures-core`, which is in the tree
    // transitively but not a declared dependency. Not worth adding one
    // to spell a return type.
) -> impl IntoResponse {
    // The error type is pinned once: nothing in this stream can fail
    // in a way axum has to render — a store read that errors is
    // reported *as an event* and closes the stream, which is what a
    // watcher can actually act on.
    type Item = Result<Event, std::convert::Infallible>;

    // `Last-Event-ID` is how the browser's own EventSource resumes
    // after a dropped connection — it replays the header without being
    // asked. Honouring it is what makes a reconnect seamless rather
    // than a duplicate log.
    let resume = q.after.or_else(|| {
        headers
            .get(axum::http::header::HeaderName::from_static("last-event-id"))
            .and_then(|v| v.to_str().ok())
            .and_then(|s| s.parse::<i64>().ok())
    });

    let registry = state.registry.clone();
    let stream = async_stream::stream! {
        let mut last = resume.unwrap_or(0);
        let started = std::time::Instant::now();

        loop {
            if started.elapsed() > STREAM_MAX {
                yield Item::Ok(Event::default()
                    .event("error")
                    .data("stream timed out; the run is still unfinished"));
                return;
            }
            // A logout, deactivation or idle expiry ends the stream too.
            match super::active_session(&state, &headers).await {
                Ok(Some(_)) => {}
                Ok(None) => {
                    yield Item::Ok(Event::default().event("error").data("session ended; sign in again"));
                    return;
                }
                // A failed lookup is no logout: try again next poll.
                Err(e) => {
                    tracing::warn!(target: "rustango::tenancy::operator_console", run_id, error = %e, "stream session check failed");
                    tokio::time::sleep(POLL_EVERY).await;
                    continue;
                }
            }
            match store::events_since(&registry, run_id, last).await {
                Ok(events) => {
                    for event in events {
                        last = event.seq;
                        let data = serde_json::json!({
                            "seq": event.seq,
                            "step": event.step,
                            "status": event.status,
                            "message": event.message,
                        });
                        yield Item::Ok(Event::default()
                            // The id IS the resume point. Without it a
                            // reconnect starts over.
                            .id(event.seq.to_string())
                            .event("step")
                            .data(data.to_string()));
                    }
                }
                Err(e) => {
                    // Driver text is logged, not streamed (#2193).
                    let msg = withheld_in(
                        &tracing::error_span!("operator_console", run_id),
                        "operator_console::provision_stream",
                        "Could not read the run",
                        &e,
                    );
                    yield Item::Ok(Event::default().event("error").data(msg));
                    return;
                }
            }

            // Terminal check *after* draining, so the last events are
            // always delivered before the stream closes.
            let finished = matches!(
                store::run_by_id(&registry, run_id).await,
                Ok(Some(run)) if RunState::parse(&run.state).is_terminal()
            );
            if finished {
                yield Item::Ok(Event::default().event("done").data(last.to_string()));
                return;
            }
            tokio::time::sleep(POLL_EVERY).await;
        }
    };

    Sse::new(stream).keep_alive(KeepAlive::default())
}

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

    /// Stands in for a deployment's registry connection.
    const TEST_REGISTRY: &str = "postgres://app:pw@db.internal:5432/registry_db";

    fn form(pairs: &[(&str, &str)]) -> HashMap<String, String> {
        pairs
            .iter()
            .map(|(k, v)| ((*k).to_owned(), (*v).to_owned()))
            .collect()
    }

    fn ev(seq: i64, step: &str, status: &str, message: &str) -> store::ProvisioningEvent {
        store::ProvisioningEvent {
            id: crate::sql::Auto::default(),
            run_id: 1,
            seq,
            step: step.into(),
            status: status.into(),
            message: message.into(),
            at: crate::sql::Auto::default(),
        }
    }

    /// `started` + `ok` read as one row; forwarded `info` rows stay.
    #[test]
    fn a_run_shows_one_row_per_step() {
        let events = [
            ev(1, "migrate", "started", ""),
            ev(2, "migration", "info", "a: 1 pending"),
            ev(3, "migration", "info", "a: applied 0001"),
            ev(4, "migrate", "ok", ""),
            ev(5, "alter", "started", ""),
            ev(6, "alter", "ok", "login `x`"),
        ];
        let rows = step_rows(&events);
        let shape: Vec<_> = rows.iter().map(|r| (r.seq, r.step, r.status)).collect();
        assert_eq!(
            shape,
            [
                (4, "migrate", "ok"),
                (2, "migration", "info"),
                (3, "migration", "info"),
                (6, "alter", "ok"),
            ]
        );
        assert_eq!(rows[3].message, "login `x`");
    }

    #[test]
    fn backticks_become_code_parts() {
        let parts = message_parts("database `<b>`, login `x`");
        assert_eq!(
            parts,
            [
                serde_json::json!({"text": "database ", "code": false}),
                serde_json::json!({"text": "<b>", "code": true}),
                serde_json::json!({"text": ", login ", "code": false}),
                serde_json::json!({"text": "x", "code": true}),
            ]
        );
        // Unbalanced: shown as written.
        assert_eq!(message_parts("a `b")[0]["text"], "a `b");
    }

    #[test]
    fn a_minimal_form_becomes_a_database_mode_request() {
        let r = request_from_form(
            &form(&[("slug", "acme"), ("database_url", "postgres://u:p@h/acme")]),
            Some(TEST_REGISTRY),
        )
        .expect("valid");
        assert_eq!(r.slug, "acme");
        assert_eq!(r.mode(), StorageMode::Database);
        assert!(r.run_migrations, "migrations run unless opted out");
    }

    /// Each choice reads only its own fields.
    #[test]
    fn the_registry_and_url_choices_read_their_own_fields() {
        let both = [
            ("slug", "acme"),
            ("database_name", "acme_db"),
            ("database_url", "postgres://u:p@elsewhere/own"),
        ];
        let mut f = form(&both);
        f.insert("database_source".into(), "registry".into());
        let r = request_from_form(&f, Some(TEST_REGISTRY)).unwrap();
        assert!(
            r.database_url()
                .unwrap()
                .ends_with("db.internal:5432/acme_db"),
            "{r:?}"
        );
        f.insert("database_source".into(), "url".into());
        let r = request_from_form(&f, Some(TEST_REGISTRY)).unwrap();
        assert_eq!(r.database_url(), Some("postgres://u:p@elsewhere/own"));
        let err = request_from_form(
            &form(&[("slug", "acme"), ("database_source", "url")]),
            Some(TEST_REGISTRY),
        )
        .expect_err("no URL");
        assert!(err.contains("URL"), "{err}");
    }

    /// The saved-server choice names the server, and only that.
    #[test]
    fn the_server_choice_becomes_a_server_source() {
        let r = request_from_form(
            &form(&[
                ("slug", "acme"),
                ("database_source", "server"),
                ("server", "main"),
                ("database_url", "postgres://u:p@h/ignored"),
            ]),
            Some(TEST_REGISTRY),
        )
        .expect("valid");
        assert_eq!(r.server(), Some("main"));
        assert_eq!(r.database_url(), None);
        let err = request_from_form(
            &form(&[("slug", "acme"), ("database_source", "server")]),
            Some(TEST_REGISTRY),
        )
        .expect_err("no server chosen");
        assert!(err.contains("server"), "{err}");
    }

    /// Blank fields are absent fields. A form posts every input it
    /// has, so treating "" as `Some("")` would write empty strings
    /// into columns that mean something by being NULL.
    #[test]
    fn blank_fields_are_treated_as_unset() {
        let r = request_from_form(
            &form(&[
                ("slug", "acme"),
                ("database_url", "postgres://u:p@h/acme"),
                ("display_name", "   "),
                ("host_pattern", ""),
            ]),
            Some(TEST_REGISTRY),
        )
        .expect("valid");
        assert_eq!(r.display_name, None);
        assert_eq!(r.host_pattern, None);
    }

    #[test]
    fn a_missing_slug_is_refused_with_a_useful_message() {
        let err = request_from_form(&form(&[("database_url", "x")]), Some(TEST_REGISTRY))
            .expect_err("no slug");
        assert!(err.contains("slug"), "{err}");
    }

    #[test]
    fn the_admin_fields_become_a_first_admin() {
        let base = [("slug", "acme"), ("database_url", "sqlite://x.db")];
        let none = request_from_form(&form(&base), None).unwrap();
        assert!(none.first_admin.is_none());
        let with = [
            &base[..],
            &[("admin_email", " ann@acme.test "), ("admin_username", "")],
        ]
        .concat();
        let admin = request_from_form(&form(&with), None)
            .unwrap()
            .first_admin
            .unwrap();
        assert_eq!(
            (admin.email(), admin.username()),
            ("ann@acme.test", "ann@acme.test")
        );
        let orphan = [&base[..], &[("admin_username", "ann")]].concat();
        assert!(request_from_form(&form(&orphan), None).is_err());
        let bad = [&base[..], &[("admin_email", "nope")]].concat();
        assert!(request_from_form(&form(&bad), None).is_err());
    }

    #[test]
    fn a_non_numeric_port_is_refused() {
        let err = request_from_form(
            &form(&[("slug", "a"), ("port", "eighty")]),
            Some(TEST_REGISTRY),
        )
        .expect_err("bad port");
        assert!(err.contains("whole number"), "{err}");
    }

    /// The checkbox is absent when unticked, which is how HTML forms
    /// work — so its *presence* is what turns migrations off.
    #[test]
    fn the_no_migrate_checkbox_inverts_correctly() {
        let on = request_from_form(
            &form(&[("slug", "a"), ("no_migrate", "on")]),
            Some(TEST_REGISTRY),
        )
        .unwrap();
        assert!(!on.run_migrations);
        let off = request_from_form(&form(&[("slug", "a")]), Some(TEST_REGISTRY)).unwrap();
        assert!(off.run_migrations);
    }

    /// Schema-mode on a non-PG backend is refused at the form, not
    /// deep in the pool layer.
    #[test]
    fn schema_mode_on_a_non_pg_backend_is_refused_early() {
        let err = request_from_form(
            &form(&[
                ("slug", "a"),
                ("storage_mode", "schema"),
                ("backend_kind", "sqlite"),
            ]),
            Some(TEST_REGISTRY),
        )
        .expect_err("sqlite cannot do schema mode");
        assert!(!err.is_empty());
    }
}

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

    const REGISTRY: &str = "postgres://app:pw@db.internal:5432/registry_db";

    fn form(pairs: &[(&str, &str)]) -> HashMap<String, String> {
        pairs
            .iter()
            .map(|(k, v)| ((*k).to_owned(), (*v).to_owned()))
            .collect()
    }

    /// A slug alone is enough. This is the whole point: an operator
    /// should not retype a host, a port and a password they already
    /// configured once.
    #[test]
    fn a_slug_alone_derives_a_url_on_the_registry_server() {
        let r = request_from_form(&form(&[("slug", "globex")]), Some(REGISTRY)).expect("valid");
        assert_eq!(
            r.database_url(),
            Some("postgres://app:pw@db.internal:5432/tenant_globex")
        );
    }

    /// The default the form *advertises* must be the default the
    /// submit *applies*. It was not: the page said "blank uses
    /// `tenant_globex`" and the submit answered "database mode needs a
    /// database URL".
    #[test]
    fn a_blank_database_name_uses_the_advertised_default() {
        let r = request_from_form(
            &form(&[("slug", "globex"), ("database_name", "   ")]),
            Some(REGISTRY),
        )
        .expect("valid");
        assert!(r.database_url().unwrap().ends_with("/tenant_globex"));
    }

    #[test]
    fn an_explicit_database_name_wins_over_the_default() {
        let r = request_from_form(
            &form(&[("slug", "globex"), ("database_name", "acme_prod")]),
            Some(REGISTRY),
        )
        .unwrap();
        assert!(r.database_url().unwrap().ends_with("/acme_prod"));
    }

    /// The escape hatch: a tenant on another server entirely.
    #[test]
    fn an_explicit_url_overrides_the_derivation() {
        let r = request_from_form(
            &form(&[
                ("slug", "globex"),
                ("database_name", "ignored"),
                ("database_url", "postgres://u:p@elsewhere:5432/own"),
            ]),
            Some(REGISTRY),
        )
        .unwrap();
        assert_eq!(r.database_url(), Some("postgres://u:p@elsewhere:5432/own"));
    }

    /// Schema mode has no separate database, so nothing is derived —
    /// and a URL is not silently carried along either.
    #[test]
    fn schema_mode_derives_nothing() {
        let r = request_from_form(
            &form(&[
                ("slug", "globex"),
                ("storage_mode", "schema"),
                ("database_name", "would_be_ignored"),
            ]),
            Some(REGISTRY),
        )
        .unwrap();
        assert_eq!(r.database_url(), None);
    }
}