cliban-server 0.7.0

cliban SSH daemon (cliband): russh listener, host key, TOML config
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
use std::sync::Arc;
use std::time::Duration;

use cliban_core::contexts::{issues, projects};
use cliban_server::commands::USAGE;
use cliban_server::config::ServerConfig;
use cliban_server::server::{russh_config, AppState, ClibandServer};
use russh::keys::{Algorithm, PrivateKey, PrivateKeyWithHashAlg};
use russh::server::Server as _;
use russh::ChannelMsg;
use tokio::sync::broadcast;

struct TrustingClient;

impl russh::client::Handler for TrustingClient {
    type Error = russh::Error;

    async fn check_server_key(
        &mut self,
        _key: &russh::keys::ssh_key::PublicKey,
    ) -> Result<bool, Self::Error> {
        Ok(true)
    }
}

fn temp_data_dir() -> std::path::PathBuf {
    let dir = std::env::temp_dir().join(format!(
        "cliband-e2e-{}-{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    ));
    std::fs::create_dir_all(&dir).unwrap();
    dir
}

async fn start_server() -> (
    std::net::SocketAddr,
    russh::server::RunningServerHandle,
    tokio::task::JoinHandle<std::io::Result<()>>,
) {
    let (addr, handle, join, _state) = start_server_with_state().await;
    (addr, handle, join)
}

/// Like [`start_server`], but also hands back the server's `AppState` so a
/// test can reach the tenant manager directly (seeding data, inspecting a
/// tenant's change feed).
async fn start_server_with_state() -> (
    std::net::SocketAddr,
    russh::server::RunningServerHandle,
    tokio::task::JoinHandle<std::io::Result<()>>,
    Arc<AppState>,
) {
    let cfg = ServerConfig {
        data_dir: temp_data_dir(),
        signup_token: Some("sesame".into()),
        ..ServerConfig::default()
    };
    let state = Arc::new(AppState::from_config(&cfg).unwrap());
    let server_state = state.clone();
    let key = PrivateKey::random(&mut rand::rng(), Algorithm::Ed25519).unwrap();
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    let (tx, rx) = tokio::sync::oneshot::channel();
    let join = tokio::spawn(async move {
        let mut server = ClibandServer {
            state: server_state,
        };
        let running = server.run_on_socket(russh_config(key), &listener);
        let _ = tx.send(running.handle());
        running.await
    });
    (addr, rx.await.unwrap(), join, state)
}

/// Seed one project holding one backlog issue into a tenant's DB through the
/// server's own tenant manager — the same cached store the board sessions
/// use, so no publish is needed (sessions opened afterwards read it fresh).
async fn seed_issue(state: &AppState, slug: &str, title: &str) {
    let tenant = state
        .manager
        .registry()
        .tenant_by_slug(slug)
        .unwrap()
        .expect("tenant exists");
    let handle = state.manager.handle(&tenant.id).unwrap();
    let title = title.to_string();
    handle
        .store
        .call(move |conn| {
            projects::create(
                conn,
                projects::CreateProject {
                    key: "LIVE".into(),
                    name: "LIVE".into(),
                    ..Default::default()
                },
            )?;
            issues::create(
                conn,
                "LIVE",
                issues::CreateIssue {
                    title,
                    ..Default::default()
                },
            )?;
            Ok(())
        })
        .await
        .unwrap();
}

fn client_key() -> PrivateKey {
    PrivateKey::random(&mut rand::rng(), Algorithm::Ed25519).unwrap()
}

async fn connect(
    addr: std::net::SocketAddr,
    user: &str,
    key: &PrivateKey,
) -> russh::client::Handle<TrustingClient> {
    let cfg = Arc::new(russh::client::Config::default());
    let mut session = russh::client::connect(cfg, addr, TrustingClient)
        .await
        .unwrap();
    let auth = session
        .authenticate_publickey(
            user,
            PrivateKeyWithHashAlg::new(Arc::new(key.clone()), None),
        )
        .await
        .unwrap();
    assert!(auth.success());
    session
}

/// Skip Success/WindowAdjusted/etc.; return the next Data payload.
async fn next_data(channel: &mut russh::Channel<russh::client::Msg>) -> Vec<u8> {
    loop {
        let msg = tokio::time::timeout(Duration::from_secs(10), channel.wait())
            .await
            .expect("timed out waiting for channel message")
            .expect("channel closed before data arrived");
        if let ChannelMsg::Data { data } = msg {
            return data.to_vec();
        }
    }
}

/// Drop ANSI escape sequences (CSI and two-byte ESC sequences) so tests can
/// assert on rendered text.
///
/// Ratatui only writes cells that changed from the previous frame; on a
/// fresh alternate screen the "previous" frame is blank, so a run of blank
/// cells (e.g. the spaces inside a title like `" pick a board "`) is never
/// written at all — the renderer just cursor-jumps past it. Left alone,
/// that glues adjacent words together ("pickaboard"). To keep needles like
/// `"pick a board"` meaningful, an absolute cursor-position escape
/// (`CSI row;col H`/`f`) that jumps forward within the same row is turned
/// back into literal spaces.
fn strip_ansi(bytes: &[u8]) -> String {
    let mut out = String::new();
    let mut i = 0;
    let mut cursor: Option<(u32, u32)> = None;
    while i < bytes.len() {
        if bytes[i] == 0x1b {
            i += 1;
            if bytes.get(i) == Some(&b'[') {
                i += 1;
                let start = i;
                while i < bytes.len() && !(0x40..=0x7e).contains(&bytes[i]) {
                    i += 1;
                }
                if bytes.get(i) == Some(&b'H') || bytes.get(i) == Some(&b'f') {
                    let params = std::str::from_utf8(&bytes[start..i]).unwrap_or("");
                    let mut parts = params.splitn(2, ';');
                    let row: u32 = parts.next().and_then(|s| s.parse().ok()).unwrap_or(1);
                    let col: u32 = parts.next().and_then(|s| s.parse().ok()).unwrap_or(1);
                    if let Some((last_row, last_col)) = cursor {
                        if row == last_row && col > last_col {
                            for _ in 0..(col - last_col) {
                                out.push(' ');
                            }
                        }
                    }
                    cursor = Some((row, col));
                }
            }
            i += 1; // final byte (or the single char after ESC)
        } else {
            out.push(bytes[i] as char);
            if let Some((_, col)) = cursor.as_mut() {
                *col += 1;
            }
            i += 1;
        }
    }
    out
}

/// Accumulate channel data until the stripped text contains `needle`.
async fn read_until(channel: &mut russh::Channel<russh::client::Msg>, needle: &str) -> String {
    let mut text = String::new();
    for _ in 0..200 {
        text.push_str(&strip_ansi(&next_data(channel).await));
        if text.contains(needle) {
            return text;
        }
    }
    panic!("never saw {needle:?} on the channel; got:\n{text}");
}

/// Open a session channel with a pty and a shell, as `ssh host` would.
async fn open_shell(
    session: &russh::client::Handle<TrustingClient>,
) -> russh::Channel<russh::client::Msg> {
    let channel = session.channel_open_session().await.unwrap();
    channel
        .request_pty(true, "xterm-256color", 100, 30, 0, 0, &[])
        .await
        .unwrap();
    channel.request_shell(true).await.unwrap();
    channel
}

/// Drain to Close; returns (raw bytes seen, exit status if any). Bounded by
/// a total deadline, not per-message: a still-rendering board produces data
/// every tick, so a per-message timeout alone can never fire.
async fn drain_to_close(
    channel: &mut russh::Channel<russh::client::Msg>,
) -> (Vec<u8>, Option<u32>) {
    let deadline = tokio::time::Instant::now() + Duration::from_secs(30);
    let mut raw = Vec::new();
    let mut status = None;
    loop {
        let msg = tokio::time::timeout_at(deadline, channel.wait())
            .await
            .expect("timed out draining channel (no Close before deadline)");
        match msg {
            Some(ChannelMsg::Data { data }) => raw.extend_from_slice(&data),
            Some(ChannelMsg::ExitStatus { exit_status }) => status = Some(exit_status),
            Some(ChannelMsg::Close) | None => break,
            _ => {}
        }
    }
    (raw, status)
}

async fn exec(session: &russh::client::Handle<TrustingClient>, cmd: &str) -> (String, u32) {
    let mut channel = session.channel_open_session().await.unwrap();
    channel.exec(true, cmd).await.unwrap();
    let mut out = Vec::new();
    let mut status = None;
    loop {
        let msg = tokio::time::timeout(Duration::from_secs(10), channel.wait())
            .await
            .expect("timed out draining exec channel");
        match msg {
            Some(ChannelMsg::Data { data }) => out.extend_from_slice(&data),
            Some(ChannelMsg::ExitStatus { exit_status }) => status = Some(exit_status),
            Some(ChannelMsg::Close) | None => break,
            _ => {}
        }
    }
    (
        String::from_utf8(out).unwrap(),
        status.expect("no exit status"),
    )
}

#[tokio::test]
async fn unrouted_exec_prints_usage_with_exit_1() {
    let (addr, handle, join) = start_server().await;
    let session = connect(addr, "tester", &client_key()).await;
    let (out, status) = exec(&session, "cliban issue list").await;
    assert_eq!(out, USAGE);
    assert_eq!(status, 1);
    handle.shutdown("test done".into());
    join.await.unwrap().unwrap();
}

#[tokio::test]
async fn pty_shell_serves_board_resizes_and_quits_cleanly() {
    let (addr, handle, join) = start_server().await;
    let session = connect(addr, "alice", &client_key()).await;
    let (out, status) = exec(&session, "signup team-shell sesame").await;
    assert_eq!(status, 0, "{out}");

    let mut channel = open_shell(&session).await;
    // Single tenant: straight to the board.
    read_until(&mut channel, "BACKLOG").await;

    // window-change must be accepted and the session must stay live:
    // the help overlay still opens afterwards.
    channel.window_change(140, 45, 0, 0).await.unwrap();
    channel.data(&b"?"[..]).await.unwrap();
    read_until(&mut channel, "Help").await;

    // In the help overlay any key closes it, so plain "qqy" is deterministic
    // regardless of how the transport chunks bytes: q closes help, q opens
    // confirm-quit, y confirms. (Esc is deliberately avoided here: an ESC
    // byte followed closely by more bytes parses as Alt+<char>, exactly like
    // a local crossterm terminal.)
    channel.data(&b"qqy"[..]).await.unwrap();
    let (raw, status) = drain_to_close(&mut channel).await;
    assert_eq!(status, Some(0));
    // Teardown restored the client's screen.
    let tail = String::from_utf8_lossy(&raw);
    assert!(
        tail.contains("\x1b[?1049l"),
        "leave-alt-screen missing: {tail:?}"
    );

    handle.shutdown("test done".into());
    join.await.unwrap().unwrap();
}

#[tokio::test]
async fn shell_from_unknown_key_prints_usage_guidance() {
    let (addr, handle, join) = start_server().await;
    let session = connect(addr, "drifter", &client_key()).await;
    let mut channel = session.channel_open_session().await.unwrap();
    channel
        .request_pty(true, "xterm-256color", 80, 24, 0, 0, &[])
        .await
        .unwrap();
    channel.request_shell(true).await.unwrap();
    let text = String::from_utf8(next_data(&mut channel).await).unwrap();
    assert_eq!(text, USAGE.replace('\n', "\r\n"));

    // The server also reports failure and closes the channel — no shell.
    let mut status = None;
    loop {
        let msg = tokio::time::timeout(Duration::from_secs(10), channel.wait())
            .await
            .expect("timed out draining shell channel");
        match msg {
            Some(ChannelMsg::ExitStatus { exit_status }) => status = Some(exit_status),
            Some(ChannelMsg::Close) | None => break,
            _ => {}
        }
    }
    assert_eq!(status, Some(1));

    handle.shutdown("test done".into());
    join.await.unwrap().unwrap();
}

#[tokio::test]
async fn full_signup_invite_accept_flow_over_ssh() {
    let (addr, handle, join) = start_server().await;

    // Alice signs up with the server's token.
    let alice = connect(addr, "alice", &client_key()).await;
    let (out, status) = exec(&alice, "signup team-a sesame").await;
    assert_eq!(status, 0, "{out}");

    // Wrong token is refused.
    let mallory = connect(addr, "mallory", &client_key()).await;
    let (out, status) = exec(&mallory, "signup team-m wrong").await;
    assert_eq!(status, 1, "{out}");

    // whoami reflects ownership.
    let (out, status) = exec(&alice, "whoami").await;
    assert_eq!(status, 0, "{out}");
    assert!(out.contains("team-a (owner)"), "{out}");

    // Alice mints an invite; the code is the last word of line 1.
    let (out, status) = exec(&alice, "invite").await;
    assert_eq!(status, 0, "{out}");
    let code = out.lines().next().unwrap().rsplit(' ').next().unwrap();
    assert_eq!(code.len(), 32, "{out}");

    // Bob (brand-new key) accepts and lands as member.
    let bob = connect(addr, "bob", &client_key()).await;
    let (out, status) = exec(&bob, &format!("accept {code}")).await;
    assert_eq!(status, 0, "{out}");
    let (out, status) = exec(&bob, "members").await;
    assert_eq!(status, 0, "{out}");
    assert!(out.contains("alice (owner)"), "{out}");
    assert!(out.contains("bob (member)"), "{out}");

    // The invite was one-time.
    let carol = connect(addr, "carol", &client_key()).await;
    let (_, status) = exec(&carol, &format!("accept {code}")).await;
    assert_eq!(status, 1);

    handle.shutdown("test done".into());
    join.await.unwrap().unwrap();
}

#[tokio::test]
async fn none_and_password_auth_are_rejected_pubkey_accepted() {
    let (addr, handle, join) = start_server().await;
    let cfg = Arc::new(russh::client::Config::default());
    let mut session = russh::client::connect(cfg, addr, TrustingClient)
        .await
        .unwrap();

    let none = session.authenticate_none("tester").await.unwrap();
    assert!(!none.success());
    let pw = session
        .authenticate_password("tester", "hunter2")
        .await
        .unwrap();
    assert!(!pw.success());

    let key = client_key();
    let auth = session
        .authenticate_publickey("tester", PrivateKeyWithHashAlg::new(Arc::new(key), None))
        .await
        .unwrap();
    assert!(auth.success());

    handle.shutdown("test done".into());
    join.await.unwrap().unwrap();
}

#[tokio::test]
async fn shell_without_pty_asks_for_a_tty_and_closes() {
    let (addr, handle, join) = start_server().await;
    let session = connect(addr, "alice", &client_key()).await;
    let (out, status) = exec(&session, "signup team-nopty sesame").await;
    assert_eq!(status, 0, "{out}");

    let mut channel = session.channel_open_session().await.unwrap();
    channel.request_shell(true).await.unwrap(); // no pty-req on purpose
    let text = read_until(&mut channel, "requires a TTY").await;
    assert!(text.contains("ssh -t"), "{text}");
    let (_, status) = drain_to_close(&mut channel).await;
    assert_eq!(status, Some(1));

    handle.shutdown("test done".into());
    join.await.unwrap().unwrap();
}

#[tokio::test]
async fn multi_tenant_shell_shows_picker_then_picked_board() {
    let (addr, handle, join) = start_server().await;
    let session = connect(addr, "alice", &client_key()).await;
    let (out, status) = exec(&session, "signup team-a sesame").await;
    assert_eq!(status, 0, "{out}");
    let (out, status) = exec(&session, "signup team-b sesame").await;
    assert_eq!(status, 0, "{out}");

    let mut channel = open_shell(&session).await;
    let text = read_until(&mut channel, "team-b").await;
    assert!(text.contains("team-a"), "picker lists both tenants: {text}");
    assert!(text.contains("pick a board"), "{text}");

    // Down to team-b, Enter: the board replaces the picker.
    channel.data(&b"j\r"[..]).await.unwrap();
    read_until(&mut channel, "BACKLOG").await;

    channel.data(&b"qy"[..]).await.unwrap();
    let (_, status) = drain_to_close(&mut channel).await;
    assert_eq!(status, Some(0));

    handle.shutdown("test done".into());
    join.await.unwrap().unwrap();
}

#[tokio::test]
async fn cancelling_the_picker_hangs_up_cleanly() {
    let (addr, handle, join) = start_server().await;
    let session = connect(addr, "alice", &client_key()).await;
    for slug in ["team-x", "team-y"] {
        let (out, status) = exec(&session, &format!("signup {slug} sesame")).await;
        assert_eq!(status, 0, "{out}");
    }

    let mut channel = open_shell(&session).await;
    read_until(&mut channel, "pick a board").await;
    channel.data(&b"q"[..]).await.unwrap();
    let (raw, status) = drain_to_close(&mut channel).await;
    assert_eq!(status, Some(0));
    let tail = String::from_utf8_lossy(&raw);
    assert!(tail.contains("\x1b[?1049l"), "screen restored: {tail:?}");

    handle.shutdown("test done".into());
    join.await.unwrap().unwrap();
}

#[tokio::test]
async fn enrolled_key_with_no_memberships_gets_guidance() {
    let (addr, handle, join) = start_server().await;
    let session = connect(addr, "loner", &client_key()).await;
    // A failed accept still enrolls the key (documented behavior), leaving
    // it with zero memberships.
    let (_, status) = exec(&session, "accept bogus-code").await;
    assert_eq!(status, 1);

    let mut channel = open_shell(&session).await;
    let text = read_until(&mut channel, "no boards yet").await;
    assert!(text.contains("signup"), "{text}");
    let (_, status) = drain_to_close(&mut channel).await;
    assert_eq!(status, Some(1));

    handle.shutdown("test done".into());
    join.await.unwrap().unwrap();
}

#[tokio::test]
async fn a_move_in_one_session_appears_in_the_other_without_input() {
    let (addr, handle, join, state) = start_server_with_state().await;

    let key = client_key();
    let alice = connect(addr, "alice", &key).await;
    let (out, status) = exec(&alice, "signup team-live sesame").await;
    assert_eq!(status, 0, "{out}");
    // A bystander tenant whose change feed must stay silent throughout.
    let bob = connect(addr, "bob", &client_key()).await;
    let (out, status) = exec(&bob, "signup team-other sesame").await;
    assert_eq!(status, 0, "{out}");

    seed_issue(&state, "team-live", "live-wire").await;
    let other = state
        .manager
        .registry()
        .tenant_by_slug("team-other")
        .unwrap()
        .unwrap();
    let mut other_feed = state.manager.handle(&other.id).unwrap().changes.subscribe();

    // Two independent connections, two boards on the same tenant. Each
    // read_until consumes that session's initial render, so anything read
    // afterwards can only come from a live refresh.
    let alice_again = connect(addr, "alice", &key).await;
    let mut a = open_shell(&alice).await;
    read_until(&mut a, "live-wire").await;
    let mut b = open_shell(&alice_again).await;
    read_until(&mut b, "live-wire").await;

    // A moves the focused card right (backlog -> in-progress). B's board
    // polls its change feed at the 100ms tick and re-renders the card in
    // its new column — with no input ever sent on B's channel. The bound
    // is generous for CI; the mechanism answers in ~a tick.
    a.data(&b"L"[..]).await.unwrap();
    tokio::time::timeout(Duration::from_secs(5), read_until(&mut b, "live-wire"))
        .await
        .expect("session B never refreshed after A's move");

    // Tenant isolation: team-other's feed saw nothing.
    match other_feed.try_recv() {
        Err(broadcast::error::TryRecvError::Empty) => {}
        got => panic!("cross-tenant leak: team-other's feed got {got:?}"),
    }

    // Both sessions still quit cleanly.
    a.data(&b"qy"[..]).await.unwrap();
    b.data(&b"qy"[..]).await.unwrap();
    let (_, sa) = drain_to_close(&mut a).await;
    let (_, sb) = drain_to_close(&mut b).await;
    assert_eq!(sa, Some(0));
    assert_eq!(sb, Some(0));

    handle.shutdown("test done".into());
    join.await.unwrap().unwrap();
}

#[tokio::test]
async fn client_disconnect_mid_board_leaves_server_healthy() {
    let (addr, handle, join) = start_server().await;
    let key = client_key();
    let session = connect(addr, "alice", &key).await;
    let (out, status) = exec(&session, "signup team-drop sesame").await;
    assert_eq!(status, 0, "{out}");

    let mut channel = open_shell(&session).await;
    read_until(&mut channel, "BACKLOG").await;
    // Vanish without closing the channel.
    session
        .disconnect(russh::Disconnect::ByApplication, "gone", "")
        .await
        .unwrap();

    // The server keeps serving: the same key reconnects onto the board.
    let session = connect(addr, "alice", &key).await;
    let mut channel = open_shell(&session).await;
    read_until(&mut channel, "BACKLOG").await;
    channel.data(&b"qy"[..]).await.unwrap();
    let (_, status) = drain_to_close(&mut channel).await;
    assert_eq!(status, Some(0));

    handle.shutdown("test done".into());
    join.await.unwrap().unwrap();
}