fusee-server 1.0.0

Fusee server mode: the server holds the log, workflow code runs in the application
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
//! TLS and token authentication on real sockets: workers and clients against
//! a server, and replicas against each other and against an attacker.
use fusee::{Ctx, Engine, Error, FlushMethod, LocalOptions};
use fusee_core::frame;
use fusee_core::vsr::{Msg, Timing, encode_msg};
use fusee_server::replicated::{ReplicaConfig, ReplicaNode};
use fusee_server::security::{Scope, Security, Tls, Token};
use fusee_server::{Client, Server, ServerConfig};
use rcgen::{
    BasicConstraints, CertificateParams, DnType, ExtendedKeyUsagePurpose, IsCa, Issuer, KeyPair,
    KeyUsagePurpose,
};
use serde_json::json;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{Duration, Instant};

fn tmp(name: &str) -> PathBuf {
    let d = std::env::temp_dir().join(format!("fusee-security-{name}-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&d);
    std::fs::create_dir_all(&d).unwrap();
    d
}

/// A CA generated for one test, writing PEM files into `dir`.
struct Pki {
    dir: PathBuf,
    issuer: Issuer<'static, KeyPair>,
}

impl Pki {
    fn new(dir: &Path, name: &str) -> Self {
        let mut params = CertificateParams::new(Vec::<String>::new()).unwrap();
        params.is_ca = IsCa::Ca(BasicConstraints::Unconstrained);
        params.distinguished_name.push(DnType::CommonName, name);
        params.key_usages = vec![
            KeyUsagePurpose::KeyCertSign,
            KeyUsagePurpose::CrlSign,
            KeyUsagePurpose::DigitalSignature,
        ];
        let key = KeyPair::generate().unwrap();
        let cert = params.self_signed(&key).unwrap();
        std::fs::write(dir.join(format!("{name}.pem")), cert.pem()).unwrap();
        Self {
            dir: dir.join(name),
            issuer: Issuer::new(params, key),
        }
    }

    fn ca(&self) -> PathBuf {
        self.dir.with_extension("pem")
    }

    /// Writes `<ca>-<who>.pem` and `<ca>-<who>.key`; returns their paths.
    fn leaf(&self, who: &str, names: &[&str]) -> (PathBuf, PathBuf) {
        let mut params =
            CertificateParams::new(names.iter().map(|n| n.to_string()).collect::<Vec<_>>())
                .unwrap();
        params.distinguished_name.push(DnType::CommonName, who);
        params.use_authority_key_identifier_extension = true;
        params.key_usages = vec![KeyUsagePurpose::DigitalSignature];
        params.extended_key_usages = vec![
            ExtendedKeyUsagePurpose::ServerAuth,
            ExtendedKeyUsagePurpose::ClientAuth,
        ];
        let key = KeyPair::generate().unwrap();
        let cert = params.signed_by(&key, &self.issuer).unwrap();
        let base = self.dir.to_string_lossy().into_owned();
        let (c, k) = (
            PathBuf::from(format!("{base}-{who}.pem")),
            PathBuf::from(format!("{base}-{who}.key")),
        );
        std::fs::write(&c, cert.pem()).unwrap();
        write_private(&k, key.serialize_pem().as_bytes());
        (c, k)
    }
}

fn write_private(path: &Path, bytes: &[u8]) {
    use std::os::unix::fs::OpenOptionsExt;
    let mut f = std::fs::OpenOptions::new()
        .create(true)
        .truncate(true)
        .write(true)
        .mode(0o600)
        .open(path)
        .unwrap();
    f.write_all(bytes).unwrap();
}

fn token_file(dir: &Path, name: &str, secret: &str) -> PathBuf {
    let path = dir.join(name);
    write_private(&path, format!("{secret}\n").as_bytes());
    path
}

fn local() -> LocalOptions {
    LocalOptions {
        flush: FlushMethod::Fsync,
        ..Default::default()
    }
}

async fn start_server(dir: &Path, security: Security) -> (String, Arc<Server>) {
    let server = Server::new(ServerConfig {
        dir: dir.join("data"),
        shards: 2,
        local: local(),
        security,
    })
    .unwrap();
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap().to_string();
    tokio::spawn(fusee_server::server::serve_tcp(server.clone(), listener));
    (addr, server)
}

fn register(engine: &Engine) -> fusee::WorkflowRef<u32, u32> {
    engine.register("double", |ctx: Ctx, n: u32| async move {
        let go: Option<u32> = ctx.wait_for("go").await?;
        ctx.step(
            "x",
            || async move { Ok::<_, Error>(n * 2 + go.unwrap_or(0)) },
        )
        .await
    })
}

async fn worker(address: String) -> std::io::Result<(fusee::Config, fusee_server::remote::Remote)> {
    tokio::task::spawn_blocking(move || fusee_server::remote_worker(&address))
        .await
        .unwrap()
}

/// The error a worker that should be refused gets.
async fn worker_refused(address: String) -> std::io::Error {
    match worker(address).await {
        Ok(_) => panic!("the server admitted a worker it should refuse"),
        Err(e) => e,
    }
}

async fn client(address: String) -> std::io::Result<Client> {
    tokio::task::spawn_blocking(move || Client::connect(&address))
        .await
        .unwrap()
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn tls_and_a_token_carry_workers_and_clients_end_to_end() {
    let dir = tmp("round-trip");
    let pki = Pki::new(&dir, "ca");
    let (cert, key) = pki.leaf("server", &["127.0.0.1", "localhost"]);
    let token = token_file(&dir, "token", "a9c1d93b7f0e4a6c8d2b5e7f1a3c5e7f");
    let security = Security {
        tls: Some(Tls::from_pem_files(Some(&cert), Some(&key), None).unwrap()),
        token: Some(Token::from_file(&token).unwrap()),
    };
    let (addr, server) = start_server(&dir, security).await;
    let socket = dir.join("fusee.sock");
    let unix = tokio::net::UnixListener::bind(&socket).unwrap();
    tokio::spawn(fusee_server::server::serve_unix(server, unix));
    let options = format!("ca={}&token-file={}", pki.ca().display(), token.display());

    let (cfg, remote) = worker(format!("{addr}?{options}")).await.unwrap();
    let engine = Engine::open(cfg).await.unwrap();
    let wf = register(&engine);
    tokio::spawn(fusee_server::serve_calls(remote, engine.clone()));
    engine.start(&wf, "job-1", &20).await.unwrap();

    // A client over TLS, by host name, signals the run.
    let tls = client(format!(
        "localhost:{}?{options}",
        addr.rsplit_once(':').unwrap().1
    ))
    .await
    .unwrap();
    tokio::task::spawn_blocking(move || tls.signal("job-1", "go", &json!(2)))
        .await
        .unwrap()
        .unwrap();
    // A client on the Unix socket proves the same token without TLS.
    let local = client(format!(
        "unix:{}?token-file={}",
        socket.display(),
        token.display()
    ))
    .await
    .unwrap();
    let result = tokio::task::spawn_blocking(move || local.result("job-1"))
        .await
        .unwrap()
        .unwrap();
    assert_eq!(result, json!(42));
    engine.shutdown().await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_wrong_or_missing_token_is_refused() {
    let dir = tmp("tokens");
    let right = token_file(&dir, "right", "0f1e2d3c4b5a69788796a5b4c3d2e1f0");
    let wrong = token_file(&dir, "wrong", "ffffffffffffffffffffffffffffffff");
    let security = Security {
        tls: None,
        token: Some(Token::from_file(&right).unwrap()),
    };
    let (addr, _server) = start_server(&dir, security).await;

    let e = worker_refused(format!("{addr}?token-file={}", wrong.display())).await;
    assert_eq!(e.kind(), std::io::ErrorKind::PermissionDenied, "{e}");
    assert!(e.to_string().contains("wrong token"), "{e}");
    let e = worker_refused(addr.clone()).await;
    assert!(
        e.to_string().contains("requires token authentication"),
        "{e}"
    );
    let e = client(format!("{addr}?token-file={}", wrong.display()))
        .await
        .unwrap_err();
    assert_eq!(e.kind(), std::io::ErrorKind::PermissionDenied, "{e}");
    // A client without a token connects, and its first call says why the
    // server hung up.
    let bare = client(addr.clone()).await.unwrap();
    let e = tokio::task::spawn_blocking(move || bare.result("job"))
        .await
        .unwrap()
        .unwrap_err();
    assert!(
        e.to_string().contains("requires token authentication"),
        "{e}"
    );

    // A server without a token says so to a worker that brings one.
    let open_dir = dir.join("open");
    std::fs::create_dir_all(&open_dir).unwrap();
    let (open, _open_server) = start_server(&open_dir, Security::default()).await;
    let e = worker_refused(format!("{open}?token-file={}", right.display())).await;
    assert!(
        e.to_string().contains("does not use token authentication"),
        "{e}"
    );

    // The refusals left the server serving: the right token gets the lease.
    let (cfg, _remote) = worker(format!("{addr}?token-file={}", right.display()))
        .await
        .unwrap();
    let engine = Engine::open(cfg).await.unwrap();
    let wf = register(&engine);
    let run = engine.start(&wf, "job", &1).await.unwrap();
    engine.run::<u32>("job").signal("go", &0).await.unwrap();
    assert_eq!(run.result().await.unwrap(), 2);
    engine.shutdown().await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn mutual_tls_refuses_a_worker_without_a_trusted_certificate() {
    let dir = tmp("mtls");
    let pki = Pki::new(&dir, "ca");
    let rogue = Pki::new(&dir, "rogue");
    let (cert, key) = pki.leaf("server", &["127.0.0.1"]);
    let (worker_cert, worker_key) = pki.leaf("worker", &["worker"]);
    let (rogue_cert, rogue_key) = rogue.leaf("worker", &["worker"]);
    let security = Security {
        tls: Some(Tls::from_pem_files(Some(&cert), Some(&key), Some(&pki.ca())).unwrap()),
        token: None,
    };
    let (addr, _server) = start_server(&dir, security).await;
    let ca = pki.ca().display().to_string();

    let e = worker_refused(format!("{addr}?ca={ca}")).await;
    assert_eq!(e.kind(), std::io::ErrorKind::PermissionDenied, "{e}");
    let e = worker_refused(format!(
        "{addr}?ca={ca}&cert={}&key={}",
        rogue_cert.display(),
        rogue_key.display()
    ))
    .await;
    assert_eq!(e.kind(), std::io::ErrorKind::PermissionDenied, "{e}");
    // A worker that does not trust the server's CA refuses it, too.
    let e = worker_refused(format!(
        "{addr}?ca={}&cert={}&key={}",
        rogue.ca().display(),
        worker_cert.display(),
        worker_key.display()
    ))
    .await;
    assert_eq!(e.kind(), std::io::ErrorKind::PermissionDenied, "{e}");

    let (cfg, _remote) = worker(format!(
        "{addr}?ca={ca}&cert={}&key={}",
        worker_cert.display(),
        worker_key.display()
    ))
    .await
    .unwrap();
    let engine = Engine::open(cfg).await.unwrap();
    let wf = register(&engine);
    let run = engine.start(&wf, "job", &5).await.unwrap();
    engine.run::<u32>("job").signal("go", &1).await.unwrap();
    assert_eq!(run.result().await.unwrap(), 11);
    engine.shutdown().await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

// ---------------------------------------------------------------- replicas

fn free_ports(n: usize) -> Vec<String> {
    let ls: Vec<std::net::TcpListener> = (0..n)
        .map(|_| std::net::TcpListener::bind("127.0.0.1:0").unwrap())
        .collect();
    ls.iter()
        .map(|l| l.local_addr().unwrap().to_string())
        .collect()
}

async fn group(
    root: &Path,
    peers: &[String],
    security: impl Fn(u8) -> Security,
) -> Vec<ReplicaNode> {
    let mut nodes = Vec::new();
    for id in 0..peers.len() as u8 {
        nodes.push(
            ReplicaNode::start(ReplicaConfig {
                id,
                peers: peers.to_vec(),
                join: false,
                dir: root.join(format!("r{id}")),
                local: local(),
                timing: Timing {
                    heartbeat_ms: 25,
                    election_ms: 1000,
                },
                security: security(id),
            })
            .await
            .unwrap(),
        );
    }
    nodes
}

/// The only ready primary, once it has stayed so for 300 ms.
async fn stable_primary(nodes: &[ReplicaNode]) -> usize {
    let deadline = Instant::now() + Duration::from_secs(30);
    let mut stable: Option<((usize, u64), Instant)> = None;
    loop {
        let mut ready = nodes.iter().enumerate().filter_map(|(i, n)| {
            let s = n.status();
            s.ready.then_some((i, s.view))
        });
        match (ready.next(), ready.next()) {
            (Some(leader), None) => match stable {
                Some((prior, since)) if prior == leader => {
                    if since.elapsed() > Duration::from_millis(300) {
                        return leader.0;
                    }
                }
                _ => stable = Some((leader, Instant::now())),
            },
            _ => stable = None,
        }
        assert!(Instant::now() < deadline, "no stable primary");
        tokio::time::sleep(Duration::from_millis(10)).await;
    }
}

async fn commit_a_workflow(node: &ReplicaNode, id: &str) {
    let engine = Engine::open(node.engine_config()).await.unwrap();
    let wf = register(&engine);
    let run = engine.start(&wf, id, &20).await.unwrap();
    engine.run::<u32>(id).signal("go", &2).await.unwrap();
    assert_eq!(run.result().await.unwrap(), 42);
    engine.shutdown().await.unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_replica_group_with_mutual_tls_and_a_token_elects_and_commits() {
    let root = tmp("replica-tls");
    let pki = Pki::new(&root, "ca");
    let token = token_file(&root, "token", "5d41402abc4b2a76b9719d911017c592");
    let peers = free_ports(3);
    let leaves: Vec<_> = (0..3)
        .map(|i| pki.leaf(&format!("member-{i}"), &["127.0.0.1"]))
        .collect();
    let nodes = group(&root, &peers, |id| Security {
        tls: Some(
            Tls::from_pem_files(
                Some(&leaves[id as usize].0),
                Some(&leaves[id as usize].1),
                Some(&pki.ca()),
            )
            .unwrap(),
        ),
        token: Some(Token::from_file(&token).unwrap()),
    })
    .await;
    let p = stable_primary(&nodes).await;
    commit_a_workflow(&nodes[p], "tls-job").await;
    let committed = nodes[p].status().commit;
    // Backups learn the commit over the same authenticated streams.
    let deadline = Instant::now() + Duration::from_secs(10);
    while nodes.iter().any(|n| n.status().commit < committed) {
        assert!(Instant::now() < deadline, "backups did not catch up");
        tokio::time::sleep(Duration::from_millis(10)).await;
    }
    for n in &nodes {
        let (_, sent, received) = n.observer().snapshot();
        assert!(sent > 0 && received > 0);
    }
    drop(nodes);
    let _ = std::fs::remove_dir_all(&root);
}

/// Forged `StartViewChange` frames for a much later view, claiming every
/// sender ID, on a fresh stream to `addr`. Returns the stream.
fn attack(addr: &str, view: u64, n: u8) -> std::net::TcpStream {
    let mut s = std::net::TcpStream::connect(addr).unwrap();
    // Before the member can reset the stream: macOS refuses socket options
    // on a reset socket.
    s.set_read_timeout(Some(Duration::from_secs(15))).unwrap();
    let mut body = Vec::new();
    encode_msg(&Msg::StartViewChange { view }, &mut body);
    let mut out = Vec::new();
    for from in 0..n {
        frame::encode(from, &body, &mut out);
    }
    let _ = s.write_all(&out);
    s
}

/// The replica closed the stream (EOF or reset). Returns what it sent first:
/// nothing for a stream that skipped the token exchange, a refusal for one
/// that failed it.
fn closed_by_peer(mut s: std::net::TcpStream) -> Option<Vec<u8>> {
    // Fails on a socket the member already reset, which then reads as closed.
    let _ = s.set_read_timeout(Some(Duration::from_secs(15)));
    let mut got = Vec::new();
    let mut buf = [0u8; 256];
    loop {
        match s.read(&mut buf) {
            Ok(0) => return Some(got),
            Err(e)
                if matches!(
                    e.kind(),
                    std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut
                ) =>
            {
                return None;
            }
            Err(_) => return Some(got),
            Ok(n) => got.extend_from_slice(&buf[..n]),
        }
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn frames_from_an_unauthenticated_stream_change_nothing() {
    // Control: without a token, one forged frame moves a member's view.
    let root = tmp("attack-open");
    let peers = free_ports(3);
    let nodes = group(&root, &peers, |_| Security::default()).await;
    let p = stable_primary(&nodes).await;
    let view = nodes[p].status().view;
    // The primary: a backup bound by its read-lease promise defers a
    // request to change views, the primary joins it at once.
    let target = p;
    let _s = attack(&peers[target], view + 10, 3);
    let deadline = Instant::now() + Duration::from_secs(10);
    while nodes[target].status().view <= view {
        assert!(
            Instant::now() < deadline,
            "the forged frame should move an open member's view"
        );
        tokio::time::sleep(Duration::from_millis(10)).await;
    }
    drop(nodes);
    let _ = std::fs::remove_dir_all(&root);

    // With a token, the same frames are never applied, on any member.
    let root = tmp("attack-token");
    let token = token_file(&root, "token", "c4ca4238a0b923820dcc509a6f75849b");
    let peers = free_ports(3);
    let nodes = group(&root, &peers, |_| Security {
        tls: None,
        token: Some(Token::from_file(&token).unwrap()),
    })
    .await;
    let p = stable_primary(&nodes).await;
    let before: Vec<_> = nodes.iter().map(|n| n.status()).collect();
    let view = before[p].view;
    let mut streams = Vec::new();
    for addr in &peers {
        streams.push(attack(addr, view + 10, 3));
    }
    // A stream that tries the token exchange with the wrong secret, then
    // sends frames anyway.
    let mut liar = std::net::TcpStream::connect(&peers[(p + 1) % 3]).unwrap();
    liar.set_read_timeout(Some(Duration::from_secs(15)))
        .unwrap();
    let mut hello = vec![1u8, 2, 0];
    hello.extend_from_slice(&[7u8; 32]);
    let mut out = Vec::new();
    frame::encode(0xF0, &hello, &mut out);
    liar.write_all(&out).unwrap();
    let mut challenge = [0u8; 13 + 33];
    liar.read_exact(&mut challenge).unwrap();
    let mut out = Vec::new();
    frame::encode(0xF2, &[0u8; 32], &mut out);
    let mut body = Vec::new();
    encode_msg(&Msg::StartViewChange { view: view + 20 }, &mut body);
    frame::encode(0, &body, &mut out);
    let _ = liar.write_all(&out);
    streams.push(liar);

    let liar = streams.pop().unwrap();
    for s in streams {
        let answer = closed_by_peer(s).expect("the member must drop the stream");
        assert!(
            answer.is_empty(),
            "no protocol reply to an unauthenticated stream"
        );
    }
    let refusal = closed_by_peer(liar).expect("the member must drop the stream");
    assert!(
        String::from_utf8_lossy(&refusal).contains("wrong token"),
        "{refusal:?}"
    );
    tokio::time::sleep(Duration::from_millis(500)).await;
    for (node, before) in nodes.iter().zip(&before) {
        let now = node.status();
        assert_eq!(
            now.view, before.view,
            "an unauthenticated frame moved a view"
        );
    }
    assert!(nodes[p].status().ready);
    // The group still commits over its own authenticated streams.
    commit_a_workflow(&nodes[p], "after-attack").await;
    assert_eq!(nodes[p].status().view, view);
    drop(nodes);
    let _ = std::fs::remove_dir_all(&root);
}

fn pem_certs(path: &Path) -> Vec<rustls::pki_types::CertificateDer<'static>> {
    use rustls::pki_types::pem::PemObject;
    rustls::pki_types::CertificateDer::pem_slice_iter(&std::fs::read(path).unwrap())
        .collect::<Result<_, _>>()
        .unwrap()
}

/// An attacker's TLS client: trusts `ca`, presents `cert`.
fn tls_client(ca: &Path, cert: &Path, key: &Path) -> Arc<rustls::ClientConfig> {
    use rustls::pki_types::pem::PemObject;
    let mut roots = rustls::RootCertStore::empty();
    for c in pem_certs(ca) {
        roots.add(c).unwrap();
    }
    let key =
        rustls::pki_types::PrivateKeyDer::from_pem_slice(&std::fs::read(key).unwrap()).unwrap();
    Arc::new(
        rustls::ClientConfig::builder_with_provider(Arc::new(
            rustls::crypto::ring::default_provider(),
        ))
        .with_protocol_versions(&[&rustls::version::TLS13])
        .unwrap()
        .with_root_certificates(roots)
        .with_client_auth_cert(pem_certs(cert), key)
        .unwrap(),
    )
}

type TlsAttack = rustls::StreamOwned<rustls::ClientConnection, std::net::TcpStream>;

/// [`attack`] inside a TLS session made with `config`.
fn tls_attack(addr: &str, config: Arc<rustls::ClientConfig>, view: u64, n: u8) -> TlsAttack {
    let tcp = std::net::TcpStream::connect(addr).unwrap();
    tcp.set_read_timeout(Some(Duration::from_secs(15))).unwrap();
    let name = rustls::pki_types::ServerName::try_from("127.0.0.1").unwrap();
    let mut tls =
        rustls::StreamOwned::new(rustls::ClientConnection::new(config, name).unwrap(), tcp);
    let mut body = Vec::new();
    encode_msg(&Msg::StartViewChange { view }, &mut body);
    let mut out = Vec::new();
    for from in 0..n {
        frame::encode(from, &body, &mut out);
    }
    let _ = tls.write_all(&out).and_then(|()| tls.flush());
    tls
}

/// The member ended the TLS session without sending a byte of plaintext.
fn tls_dropped(mut s: TlsAttack) {
    let mut buf = [0u8; 256];
    match s.read(&mut buf) {
        Ok(0) => {}
        Ok(n) => panic!("a member answered an unauthorised stream with {n} bytes"),
        Err(e) => assert!(
            !matches!(
                e.kind(),
                std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut
            ),
            "the member kept the stream open"
        ),
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn mutual_tls_replicas_admit_only_member_certificates_and_serve_secured_workers() {
    let root = tmp("replica-mtls");
    let pki = Pki::new(&root, "ca");
    let rogue = Pki::new(&root, "rogue");
    let token = token_file(&root, "token", "1b6453892473a467d07372d45eb05abc");
    let peers = free_ports(3);
    let leaves: Vec<_> = (0..3)
        .map(|i| pki.leaf(&format!("member-{i}"), &["127.0.0.1"]))
        .collect();
    let member = |id: usize, ca: bool| {
        Tls::from_pem_files(
            Some(&leaves[id].0),
            Some(&leaves[id].1),
            ca.then(|| pki.ca()).as_deref(),
        )
        .unwrap()
    };
    let nodes = group(&root, &peers, |id| Security {
        tls: Some(member(id as usize, true)),
        token: None,
    })
    .await;
    // Each member also serves workers: TLS with its certificate, a token.
    let mut endpoints = Vec::new();
    for (id, node) in nodes.iter().enumerate() {
        let server = Server::replicated(node);
        server
            .set_security(&Security {
                tls: Some(member(id, false)),
                token: Some(Token::from_file(&token).unwrap()),
            })
            .unwrap();
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        endpoints.push(listener.local_addr().unwrap().to_string());
        tokio::spawn(fusee_server::server::serve_tcp(server, listener));
    }
    let p = stable_primary(&nodes).await;
    let before: Vec<_> = nodes.iter().map(|n| n.status().view).collect();
    let view = before[p];

    // The group's CA signed this certificate, but for a worker's name, not
    // a member's host; the other one comes from a CA the group does not trust.
    let (worker_cert, worker_key) = pki.leaf("worker", &["worker.internal"]);
    let (rogue_cert, rogue_key) = rogue.leaf("member-x", &["127.0.0.1"]);
    let not_a_member = tls_client(&pki.ca(), &worker_cert, &worker_key);
    let untrusted = tls_client(&pki.ca(), &rogue_cert, &rogue_key);
    let mut tls_streams = Vec::new();
    let mut plain_streams = Vec::new();
    for addr in &peers {
        tls_streams.push(tls_attack(addr, not_a_member.clone(), view + 10, 3));
        tls_streams.push(tls_attack(addr, untrusted.clone(), view + 10, 3));
        plain_streams.push(attack(addr, view + 10, 3));
    }
    let dropped = tokio::task::spawn_blocking(move || {
        for s in tls_streams {
            tls_dropped(s);
        }
        for s in plain_streams {
            let answer = closed_by_peer(s).expect("the member must drop the stream");
            assert!(
                answer.len() < 64,
                "a protocol reply to a plain stream: {answer:?}"
            );
        }
    });
    dropped.await.unwrap();
    tokio::time::sleep(Duration::from_millis(500)).await;
    for (node, before) in nodes.iter().zip(&before) {
        assert_eq!(
            node.status().view,
            *before,
            "an unauthorised frame moved a view"
        );
    }
    assert!(nodes[p].status().ready);

    // A worker finds the primary through every member's TLS listener and
    // runs a workflow the group commits.
    let options = format!("ca={}&token-file={}", pki.ca().display(), token.display());
    let address = format!("replicas://{}?{options}", endpoints.join(","));
    let refused = worker_refused(format!(
        "replicas://{}?ca={}",
        endpoints.join(","),
        pki.ca().display()
    ))
    .await;
    assert!(
        refused
            .to_string()
            .contains("requires token authentication"),
        "{refused}"
    );
    let (cfg, _remote) = worker(address).await.unwrap();
    let engine = Engine::open(cfg).await.unwrap();
    let wf = register(&engine);
    let run = engine.start(&wf, "mtls-job", &4).await.unwrap();
    engine
        .run::<u32>("mtls-job")
        .signal("go", &1)
        .await
        .unwrap();
    assert_eq!(run.result().await.unwrap(), 9);
    engine.shutdown().await.unwrap();
    assert_eq!(nodes[p].status().view, view);
    drop(nodes);
    let _ = std::fs::remove_dir_all(&root);
}

/// Per-shard leases and the shared log under TLS and the token: two
/// workers each hold one shard (one over TLS, one on the Unix socket with
/// the token; the second may hold two but finds one free), clients reach
/// both, and when one leaves the other is granted its shard and finishes
/// its run.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn workers_sharing_shards_over_tls_and_a_token_hand_shards_over() {
    let dir = tmp("shared-shards");
    let pki = Pki::new(&dir, "ca");
    let (cert, key) = pki.leaf("server", &["127.0.0.1", "localhost"]);
    let token = token_file(&dir, "token", "5be1d93b7f0e4a6c8d2b5e7f1a3c5e70");
    let security = Security {
        tls: Some(Tls::from_pem_files(Some(&cert), Some(&key), None).unwrap()),
        token: Some(Token::from_file(&token).unwrap()),
    };
    let (addr, server) = start_server(&dir, security).await;
    assert!(server.shared_log());
    let socket = dir.join("fusee.sock");
    let unix = tokio::net::UnixListener::bind(&socket).unwrap();
    tokio::spawn(fusee_server::server::serve_unix(server, unix));
    let tls = format!(
        "{addr}?ca={}&max-shards=1&token-file={}",
        pki.ca().display(),
        token.display()
    );
    let on_unix = format!(
        "unix:{}?max-shards=2&token-file={}",
        socket.display(),
        token.display()
    );

    let (cfg, remote_a) = worker(tls.clone()).await.unwrap();
    let engine_a = Engine::open(cfg).await.unwrap();
    register(&engine_a);
    let calls_a = tokio::spawn(fusee_server::serve_calls(
        remote_a.clone(),
        engine_a.clone(),
    ));
    let (cfg, remote_b) = worker(on_unix).await.unwrap();
    let engine_b = Engine::open(cfg).await.unwrap();
    register(&engine_b);
    let calls_b = tokio::spawn(fusee_server::serve_calls(
        remote_b.clone(),
        engine_b.clone(),
    ));
    let (held_a, held_b) = (remote_a.held_shards(), remote_b.held_shards());
    assert_eq!((held_a.len(), held_b.len()), (1, 1));
    assert_ne!(held_a, held_b);

    // One run on each shard, started through a TLS client.
    let mut ids = [None, None];
    for k in 0.. {
        let id = format!("s-{k}");
        let s = fusee_core::shard::shard_of(&id, 2) as usize;
        ids[s].get_or_insert(id);
        if ids.iter().all(Option::is_some) {
            break;
        }
    }
    let ids = ids.map(Option::unwrap);
    let c = client(tls.clone()).await.unwrap();
    for (n, id) in ids.iter().enumerate() {
        let (c, id) = (c.clone(), id.clone());
        tokio::task::spawn_blocking(move || c.start("double", &id, &json!(n as u32 + 1)))
            .await
            .unwrap()
            .unwrap();
    }
    // A leaves; B is granted A's shard and finishes both runs.
    calls_a.abort();
    let _ = calls_a.await;
    engine_a.shutdown().await.unwrap();
    drop((engine_a, remote_a));
    let deadline = Instant::now() + Duration::from_secs(20);
    while remote_b.held_shards().len() < 2 {
        assert!(Instant::now() < deadline, "B was never granted A's shard");
        tokio::time::sleep(Duration::from_millis(10)).await;
    }
    for (n, id) in ids.iter().enumerate() {
        let (c, id) = (c.clone(), id.clone());
        let result = tokio::task::spawn_blocking(move || {
            for _ in 0..200 {
                if c.signal(&id, "go", &json!(5)).is_ok() {
                    return c.result(&id);
                }
                std::thread::sleep(Duration::from_millis(20));
            }
            c.result(&id)
        })
        .await
        .unwrap()
        .unwrap();
        assert_eq!(result, json!((n as u32 + 1) * 2 + 5));
    }
    calls_b.abort();
    engine_b.shutdown().await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

/// A client's token makes calls and holds no shard; a reader's makes only
/// calls that read. The scope is decided at the connection's first frame.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn client_and_read_tokens_reach_only_their_scope() {
    let dir = tmp("scopes");
    let worker_token = token_file(&dir, "worker", "a9c1d93b7f0e4a6c8d2b5e7f1a3c5e7f");
    let client_token = token_file(&dir, "client", "c1d2e3f4a5b6c7d8e9f0a1b2c3d4e5f6");
    let read_token = token_file(&dir, "read", "0f1e2d3c4b5a69788796a5b4c3d2e1f0");
    let security = Security {
        tls: None,
        token: Some(Token::from_file(&worker_token).unwrap()),
    };
    let (addr, server) = start_server(&dir, security.clone()).await;
    server
        .set_security_scoped(
            &security,
            vec![
                (Token::from_file(&client_token).unwrap(), Scope::Client),
                (Token::from_file(&read_token).unwrap(), Scope::Read),
            ],
        )
        .unwrap();
    let at = |t: &Path| format!("{addr}?token-file={}", t.display());

    // A worker with a client's or a reader's token is refused its shards.
    for t in [&client_token, &read_token] {
        let e = worker_refused(at(t)).await;
        assert!(e.to_string().contains("cannot hold shards"), "{e}");
    }
    let (cfg, remote) = worker(at(&worker_token)).await.unwrap();
    let engine = Engine::open(cfg).await.unwrap();
    register(&engine);
    tokio::spawn(fusee_server::serve_calls(remote, engine.clone()));

    // The client's token starts and signals, naming who asked (audit): the
    // name crosses the server to the worker's record.
    let c = client(at(&client_token)).await.unwrap();
    let journal = tokio::task::spawn_blocking(move || {
        c.start("double", "s-1", &json!(20))?;
        c.call(
            "signal",
            &json!({"id": "s-1", "name": "go", "value": 2, "by": "cli:ana"}),
        )?;
        c.call("describe", &json!({"id": "s-1"}))
    })
    .await
    .unwrap()
    .unwrap()["journal"]
        .clone();
    let signal = journal
        .as_array()
        .unwrap()
        .iter()
        .find(|r| r["kind"] == "signal")
        .unwrap();
    assert_eq!(signal["by"], "cli:ana", "{journal}");
    // The reader's token reads, and is refused a change.
    let r = client(at(&read_token)).await.unwrap();
    let (result, refused) =
        tokio::task::spawn_blocking(move || (r.result("s-1"), r.start("double", "s-2", &json!(1))))
            .await
            .unwrap();
    assert_eq!(result.unwrap(), json!(42));
    let e = refused.unwrap_err();
    assert!(e.to_string().contains("only reads"), "{e}");
    // A token the server does not know is refused outright.
    let stranger = token_file(&dir, "stranger", "ffffffffffffffffffffffffffffffff");
    assert!(client(at(&stranger)).await.is_err());
    engine.shutdown().await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

/// The HTTP API of `fusee server --http` reaches its own server inside the
/// process, so the worker listener's TLS and token do not stand in its way,
/// and on a replica group it works once its member is the ready primary,
/// though that member closed every connection until then. A backup's API
/// says that calls go to the primary.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_servers_own_http_api_reaches_it_over_tls_and_on_a_replica_group() {
    use fusee::http::Calls;
    let root = tmp("local-calls");
    let pki = Pki::new(&root, "ca");
    let token = token_file(&root, "token", "c2f3a1b4d5e6f708192a3b4c5d6e7f80");
    let peers = free_ports(3);
    let leaves: Vec<_> = (0..3)
        .map(|i| pki.leaf(&format!("member-{i}"), &["127.0.0.1"]))
        .collect();
    let tls = |id: usize, ca: bool| {
        Tls::from_pem_files(
            Some(&leaves[id].0),
            Some(&leaves[id].1),
            ca.then(|| pki.ca()).as_deref(),
        )
        .unwrap()
    };
    let nodes = group(&root, &peers, |id| Security {
        tls: Some(tls(id as usize, true)),
        token: None,
    })
    .await;
    // Each member serves workers over TLS with a token, and has its API's
    // calls from the start, before any member is ready.
    let mut endpoints = Vec::new();
    let mut apis = Vec::new();
    for (id, node) in nodes.iter().enumerate() {
        let server = Server::replicated(node);
        server
            .set_security(&Security {
                tls: Some(tls(id, false)),
                token: Some(Token::from_file(&token).unwrap()),
            })
            .unwrap();
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        endpoints.push(listener.local_addr().unwrap().to_string());
        apis.push(fusee_server::client::LocalCalls::new(&server));
        tokio::spawn(fusee_server::server::serve_tcp(server, listener));
    }
    let p = stable_primary(&nodes).await;
    let options = format!("ca={}&token-file={}", pki.ca().display(), token.display());
    let (cfg, remote) = worker(format!("replicas://{}?{options}", endpoints.join(",")))
        .await
        .unwrap();
    let engine = Engine::open(cfg).await.unwrap();
    register(&engine);
    tokio::spawn(fusee_server::serve_calls(remote, engine.clone()));

    let api = &apis[p];
    api.call(
        "start",
        json!({"type": "double", "id": "http-job", "input": 20}),
    )
    .await
    .unwrap();
    api.call(
        "signal",
        json!({"id": "http-job", "name": "go", "value": 2}),
    )
    .await
    .unwrap();
    assert_eq!(
        api.call("result", json!({"id": "http-job"})).await.unwrap(),
        json!(42)
    );
    let listed = api
        .call("list", json!({"where": {}, "limit": 10}))
        .await
        .unwrap();
    assert_eq!(listed[0]["id"], "http-job", "{listed}");
    let backup = (p + 1) % 3;
    let e = apis[backup].call("list", json!({})).await.unwrap_err();
    assert!(
        matches!(&e, Error::Connection(m) if m.contains("backup")),
        "{e}"
    );
    engine.shutdown().await.unwrap();
    drop(nodes);
    let _ = std::fs::remove_dir_all(&root);
}