second-brain-api 0.13.0

HTTP API server for second-brain: REST endpoints for recall, remember, and ingest
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
mod common;

use std::net::{IpAddr, Ipv4Addr, TcpListener};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::{Duration, Instant};

use axum::http::StatusCode;
use second_brain_api::AppState;
use second_brain_api::bootstrap::Bootstrap;
use second_brain_api::clock_gate::ClockGate;
use second_brain_api::device_access::DeviceAccess;
use second_brain_api::pairing::{Pairing, PairingConfig, advertise_route};
use second_brain_api::routes;
use second_brain_core::keys::{DeviceKeys, DeviceRecord, ensure_device_keys};
use second_brain_core::sqlite_store::SqliteStore;
use second_brain_sync::authorized_keys::granted;
use second_brain_sync::dial::{Route, dials};
use serde_json::{Value, json};
use tempfile::TempDir;
use tower::ServiceExt;

use common::{json_body, post_json, state_in};

const FOREIGN: &str = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIForeignKeyOfTheUser me@laptop";
const WAIT: Duration = Duration::from_secs(30);

struct Daemon {
    app: axum::Router,
    host_key: String,
    state: Arc<AppState>,
    dir: TempDir,
    _sshd: Option<TcpListener>,
}

impl Daemon {
    fn new(id: &str, sshd: bool) -> Self {
        let dir = tempfile::tempdir().unwrap();
        let store = SqliteStore::in_memory(id.to_string()).unwrap();
        ensure_device_keys(&store, dir.path()).unwrap();
        let host_key = DeviceKeys::generate().unwrap().ssh_public_key();
        let key_dir = dir.path().join("etc-ssh");
        std::fs::create_dir(&key_dir).unwrap();
        let listener = sshd.then(|| {
            std::fs::write(
                key_dir.join("ssh_host_ed25519_key.pub"),
                format!("{host_key} root@{id}\n"),
            )
            .unwrap();
            TcpListener::bind("127.0.0.1:0").unwrap()
        });
        let config = PairingConfig {
            listen_ip: IpAddr::V4(Ipv4Addr::LOCALHOST),
            port: 0,
            ssh_host_key_dir: key_dir,
            ssh_port: listener
                .as_ref()
                .map_or(1, |l| l.local_addr().unwrap().port()),
            discovery: None,
        };
        let ssh_dir = dir.path().join("home-ssh");
        std::fs::create_dir(&ssh_dir).unwrap();
        std::fs::write(ssh_dir.join("authorized_keys"), format!("{FOREIGN}\n")).unwrap();
        let base = Arc::into_inner(state_in(store, dir.path(), ClockGate::open_for_tests()))
            .expect("a new state has one owner");
        let state = Arc::new(AppState {
            pairing: Arc::new(Pairing::new(config)),
            device_access: Arc::new(DeviceAccess::with_authorized_keys(
                ssh_dir.join("authorized_keys"),
                PathBuf::from("/opt/second brain/sb"),
            )),
            bootstrap: Arc::new(Bootstrap::default()),
            ..base
        });
        Self {
            app: routes::router().with_state(state.clone()),
            host_key,
            state,
            dir,
            _sshd: listener,
        }
    }

    async fn call(&self, request: axum::http::Request<axum::body::Body>) -> (StatusCode, Value) {
        let response = self.app.clone().oneshot(request).await.unwrap();
        let status = response.status();
        (status, json_body(response).await)
    }

    fn id(&self) -> String {
        self.state.sqlite.machine_id().to_string()
    }

    fn authorized_keys(&self) -> String {
        std::fs::read_to_string(self.dir.path().join("home-ssh/authorized_keys")).unwrap()
    }

    fn granted(&self) -> Vec<String> {
        granted(&self.dir.path().join("home-ssh/authorized_keys")).unwrap()
    }

    fn known_hosts(&self) -> String {
        std::fs::read_to_string(self.dir.path().join("known_hosts")).unwrap_or_default()
    }

    fn row(&self, machine_id: &str) -> DeviceRecord {
        self.state
            .sqlite
            .device_record(machine_id)
            .unwrap()
            .unwrap()
            .current
            .record
    }

    /// Sends `peer` this device's hello, as the first round of a sync does.
    async fn hello_to(&self, peer: &Daemon) {
        let hello = second_brain_api::sync_hello::hello_for(
            &self.state.sqlite,
            &self.state.clock,
            self.dir.path(),
            &peer.id(),
            None,
        )
        .unwrap();
        let response = peer
            .app
            .clone()
            .oneshot(post_json("/v1/sync/accept-hello", json!({"hello": hello})))
            .await
            .unwrap();
        assert!(response.status().is_success(), "{}", response.status());
    }

    /// The sshd this device runs after a restart, when it had none before.
    fn sshd_up(&self) -> (Pairing, TcpListener, String) {
        let host_key = DeviceKeys::generate().unwrap().ssh_public_key();
        let key_dir = self.dir.path().join("etc-ssh");
        std::fs::write(
            key_dir.join("ssh_host_ed25519_key.pub"),
            format!("{host_key} root@{}\n", self.id()),
        )
        .unwrap();
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let pairing = Pairing::new(PairingConfig {
            listen_ip: IpAddr::V4(Ipv4Addr::LOCALHOST),
            port: 0,
            ssh_host_key_dir: key_dir,
            ssh_port: listener.local_addr().unwrap().port(),
            discovery: None,
        });
        (pairing, listener, host_key)
    }

    fn dialled(&self) -> Vec<(String, Vec<String>)> {
        dials(&self.state.sqlite, self.dir.path())
            .unwrap()
            .into_iter()
            .map(|dial| (dial.key, dial.routes.iter().map(Route::describe).collect()))
            .collect()
    }

    /// Pairs `joiner` into this device's identity through the pairing
    /// routes, as `sb pair` and `sb pair <code> --host` do.
    async fn pair(&self, joiner: &Daemon) {
        let (status, started) = self.call(post_json("/v1/pair/start", json!({}))).await;
        assert_eq!(status, StatusCode::OK, "{started}");
        let code = started["code"].as_str().unwrap().to_string();
        let port = started["port"].as_u64().unwrap();
        let (status, joined) = joiner
            .call(post_json(
                "/v1/pair/join",
                json!({"code": code, "host": format!("127.0.0.1:{port}")}),
            ))
            .await;
        assert_eq!(status, StatusCode::OK, "{joined}");
        let start = Instant::now();
        loop {
            let (_, status) = self.call(common::get("/v1/pair/status")).await;
            if status["state"] == "paired" {
                break;
            }
            assert!(start.elapsed() < WAIT, "pairing never finished: {status}");
            tokio::time::sleep(Duration::from_millis(20)).await;
        }
        self.call(post_json("/v1/pair/close", json!({}))).await;
    }
}

fn forced_lines(text: &str) -> Vec<&str> {
    text.lines()
        .filter(|line| line.contains("second-brain:"))
        .collect()
}

#[tokio::test(flavor = "multi_thread")]
async fn pairing_grants_exactly_one_restricted_line_each_way_and_dials_by_machine_id() {
    let desk = Daemon::new("desk", true);
    let laptop = Daemon::new("laptop", true);
    desk.pair(&laptop).await;

    let lines = desk.authorized_keys();
    assert_eq!(lines.lines().next(), Some(FOREIGN), "{lines}");
    let forced = forced_lines(&lines);
    assert_eq!(forced.len(), 1, "{lines}");
    assert!(
        forced[0].starts_with(
            "command=\"'/opt/second brain/sb' sync-serve --machine laptop\",restrict ssh-ed25519 "
        ),
        "{lines}"
    );
    assert!(forced[0].ends_with(" second-brain:laptop"), "{lines}");
    assert_eq!(laptop.granted(), vec![desk.id()]);

    assert!(
        desk.known_hosts()
            .lines()
            .any(|l| l == format!("sb-laptop {}", laptop.host_key)),
        "{}",
        desk.known_hosts()
    );
    assert!(
        laptop
            .known_hosts()
            .lines()
            .any(|l| l == format!("sb-desk {}", desk.host_key)),
        "{}",
        laptop.known_hosts()
    );

    let login = std::env::var("USER").unwrap_or_default();
    let dialled = laptop.dialled();
    assert_eq!(dialled.len(), 1, "{dialled:?}");
    assert_eq!(dialled[0].0, "desk");
    let port = desk._sshd.as_ref().unwrap().local_addr().unwrap().port();
    assert_eq!(
        dialled[0].1.first(),
        Some(&format!("{login}@127.0.0.1:{port}"))
    );
    assert!(
        desk.state.device_access.take_kick(),
        "a pairing starts a sync"
    );
    assert!(laptop.state.device_access.take_kick());

    desk.state
        .device_access
        .reconcile(&desk.state.sqlite, desk.dir.path())
        .unwrap();
    assert_eq!(
        desk.authorized_keys(),
        lines,
        "a second sweep changes nothing"
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn an_empty_device_waits_for_its_bootstrap_and_one_with_data_does_not() {
    let desk = Daemon::new("desk", true);
    let laptop = Daemon::new("laptop", false);
    desk.pair(&laptop).await;
    assert!(laptop.state.bootstrap.is_pending());
    assert!(!desk.state.bootstrap.is_pending());
    let (status, body) = laptop.call(post_json("/v1/ingest", json!({}))).await;
    assert_eq!(status, StatusCode::OK, "{body}");
    assert_eq!(body["deferred"], true, "{body}");

    let used = Daemon::new("used", false);
    let (status, body) = used
        .call(post_json(
            "/v1/remember",
            json!({"content": "a note written before pairing", "memory_type": "semantic"}),
        ))
        .await;
    assert!(status.is_success(), "{body}");
    desk.pair(&used).await;
    assert!(!used.state.bootstrap.is_pending());
}

#[tokio::test(flavor = "multi_thread")]
async fn a_removed_device_loses_its_line_on_the_remover_and_on_a_peer_after_a_handshake() {
    let desk = Daemon::new("desk", true);
    let laptop = Daemon::new("laptop", true);
    let pi = Daemon::new("pi", true);
    desk.pair(&laptop).await;
    desk.pair(&pi).await;
    assert_eq!(
        pi.granted(),
        vec![desk.id(), laptop.id()],
        "pi learns laptop from the device list"
    );
    assert!(
        pi.known_hosts().contains("sb-laptop "),
        "{}",
        pi.known_hosts()
    );

    let (status, removed) = desk
        .call(post_json("/v1/devices/remove", json!({"device": "laptop"})))
        .await;
    assert_eq!(status, StatusCode::OK, "{removed}");
    assert_eq!(desk.granted(), vec![pi.id()]);
    assert_eq!(desk.authorized_keys().lines().next(), Some(FOREIGN));
    assert!(
        !desk.known_hosts().contains("sb-laptop "),
        "{}",
        desk.known_hosts()
    );
    assert!(
        desk.dialled().iter().all(|(key, _)| key != "laptop"),
        "{:?}",
        desk.dialled()
    );

    let hello = second_brain_api::sync_hello::hello_for(
        &desk.state.sqlite,
        &desk.state.clock,
        desk.dir.path(),
        &pi.id(),
        None,
    )
    .unwrap();
    let response = pi
        .app
        .clone()
        .oneshot(post_json("/v1/sync/accept-hello", json!({"hello": hello})))
        .await
        .unwrap();
    assert!(response.status().is_success(), "{}", response.status());
    assert_eq!(pi.granted(), vec![desk.id()]);
    assert_eq!(pi.authorized_keys().lines().next(), Some(FOREIGN));
    assert!(pi.dialled().iter().all(|(key, _)| key != "laptop"));
}

#[tokio::test(flavor = "multi_thread")]
async fn a_removal_cuts_the_ssh_line_even_when_known_hosts_cannot_be_written() {
    let desk = Daemon::new("desk", true);
    let laptop = Daemon::new("laptop", true);
    desk.pair(&laptop).await;
    let known_hosts = desk.dir.path().join("known_hosts");
    std::fs::remove_file(&known_hosts).unwrap();
    std::fs::create_dir(&known_hosts).unwrap();

    let (status, removed) = desk
        .call(post_json("/v1/devices/remove", json!({"device": "laptop"})))
        .await;
    assert_eq!(status, StatusCode::OK, "{removed}");
    assert_eq!(desk.granted(), Vec::<String>::new());
}

#[tokio::test(flavor = "multi_thread")]
async fn pairing_completes_on_both_sides_when_authorized_keys_cannot_be_written() {
    let desk = Daemon::new("desk", true);
    let laptop = Daemon::new("laptop", true);
    for daemon in [&desk, &laptop] {
        let path = daemon.dir.path().join("home-ssh/authorized_keys");
        std::fs::remove_file(&path).unwrap();
        std::fs::create_dir(&path).unwrap();
    }
    desk.pair(&laptop).await;
    assert!(laptop.state.bootstrap.is_pending());
    assert!(
        dials(&laptop.state.sqlite, laptop.dir.path())
            .unwrap()
            .iter()
            .any(|dial| dial.key == desk.id())
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn a_failed_join_leaves_the_bootstrap_as_it_was() {
    let desk = Daemon::new("desk", true);
    let laptop = Daemon::new("laptop", false);
    let waiting = Daemon::new("waiting", false);
    waiting
        .state
        .bootstrap
        .mark_pending(&waiting.state.sqlite)
        .unwrap();
    let (status, started) = desk.call(post_json("/v1/pair/start", json!({}))).await;
    assert_eq!(status, StatusCode::OK, "{started}");
    let port = started["port"].as_u64().unwrap();
    let wrong = second_brain_sync::pairing::HostSecrets::generate()
        .code()
        .to_string();
    for joiner in [&laptop, &waiting] {
        let (status, body) = joiner
            .call(post_json(
                "/v1/pair/join",
                json!({"code": wrong, "host": format!("127.0.0.1:{port}")}),
            ))
            .await;
        assert!(!status.is_success(), "{body}");
    }
    assert!(!laptop.state.bootstrap.is_pending());
    assert!(waiting.state.bootstrap.is_pending());
}

#[tokio::test(flavor = "multi_thread")]
async fn founding_an_identity_ends_a_wait_to_join_one() {
    let laptop = Daemon::new("laptop", false);
    laptop
        .state
        .bootstrap
        .mark_pending(&laptop.state.sqlite)
        .unwrap();
    let (status, started) = laptop.call(post_json("/v1/pair/start", json!({}))).await;
    assert_eq!(status, StatusCode::OK, "{started}");
    assert!(started["founded"].is_object(), "{started}");
    assert!(!laptop.state.bootstrap.is_pending());
}

#[tokio::test(flavor = "multi_thread")]
async fn a_device_that_moves_to_another_identity_drops_the_old_ones_host_keys() {
    let desk = Daemon::new("desk", true);
    let laptop = Daemon::new("laptop", true);
    let other = Daemon::new("other", true);
    desk.pair(&laptop).await;
    let desk_key = desk.host_key.split_whitespace().nth(1).unwrap().to_string();
    assert!(
        laptop.known_hosts().contains(&desk_key),
        "{}",
        laptop.known_hosts()
    );

    other.pair(&laptop).await;
    let known_hosts = laptop.known_hosts();
    assert!(!known_hosts.contains(&desk_key), "{known_hosts}");
    assert!(!known_hosts.contains("sb-desk "), "{known_hosts}");
    let other_key = other.host_key.split_whitespace().nth(1).unwrap();
    assert!(known_hosts.contains(other_key), "{known_hosts}");
    assert_eq!(laptop.granted(), vec![other.id()]);
}

#[tokio::test(flavor = "multi_thread")]
async fn a_joiner_that_cannot_write_known_hosts_still_stores_the_route_and_grants_its_host() {
    let desk = Daemon::new("desk", true);
    let laptop = Daemon::new("laptop", true);
    std::fs::create_dir(laptop.dir.path().join("known_hosts")).unwrap();
    desk.pair(&laptop).await;
    assert!(
        laptop.dialled().iter().any(|(key, _)| *key == desk.id()),
        "{:?}",
        laptop.dialled()
    );
    assert_eq!(laptop.granted(), vec![desk.id()]);
}

#[tokio::test(flavor = "multi_thread")]
async fn a_joiner_that_cannot_dial_its_host_does_not_wait_for_a_snapshot() {
    let desk = Daemon::new("desk", false);
    let laptop = Daemon::new("laptop", true);
    laptop
        .state
        .bootstrap
        .mark_pending(&laptop.state.sqlite)
        .unwrap();
    desk.pair(&laptop).await;
    assert!(!laptop.state.bootstrap.is_pending());
    assert!(laptop.dialled().iter().all(|(key, _)| *key != desk.id()));
}

#[tokio::test(flavor = "multi_thread")]
async fn a_waiting_device_stops_waiting_when_told_whether_or_not_it_joined() {
    let laptop = Daemon::new("laptop", false);
    laptop
        .state
        .bootstrap
        .mark_pending(&laptop.state.sqlite)
        .unwrap();
    let desk = Daemon::new("desk", true);
    let joined = Daemon::new("joined", false);
    desk.pair(&joined).await;
    assert!(joined.state.bootstrap.is_pending());
    for device in [&laptop, &joined] {
        let (_, health) = device.call(common::get("/health")).await;
        assert_eq!(health["bootstrap_pending"], true, "{health}");
        let (status, body) = device
            .call(post_json("/v1/pair/stop-waiting", json!({})))
            .await;
        assert_eq!(status, StatusCode::OK, "{body}");
        assert_eq!(body["cancelled"], true, "{body}");
        assert!(!device.state.bootstrap.is_pending());
        let (_, health) = device.call(common::get("/health")).await;
        assert!(health.get("bootstrap_pending").is_none(), "{health}");
    }
    let (_, body) = laptop
        .call(post_json("/v1/pair/stop-waiting", json!({})))
        .await;
    assert_eq!(body["cancelled"], false, "{body}");
}

#[tokio::test(flavor = "multi_thread")]
async fn a_push_from_a_paired_device_counts_as_a_sync_on_the_device_it_dials() {
    let desk = Daemon::new("desk", true);
    let laptop = Daemon::new("laptop", false);
    desk.pair(&laptop).await;
    assert!(
        second_brain_sync::peers::last_success(desk.dir.path())
            .unwrap()
            .is_none()
    );
    let hello = second_brain_api::sync_hello::hello_for(
        &laptop.state.sqlite,
        &laptop.state.clock,
        laptop.dir.path(),
        &desk.id(),
        None,
    )
    .unwrap();
    let header = second_brain_sync::protocol::SyncHeader {
        hello: Some(hello),
        ..second_brain_sync::protocol::SyncHeader::local()
    };
    let (status, body) = desk
        .call(post_json(
            "/v1/sync/import",
            json!({"header": header, "lines": []}),
        ))
        .await;
    assert_eq!(status, StatusCode::OK, "{body}");
    assert!(
        second_brain_sync::peers::last_success(desk.dir.path())
            .unwrap()
            .is_some()
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn a_device_paired_through_another_pins_and_dials_the_founder() {
    let desk = Daemon::new("desk", true);
    let laptop = Daemon::new("laptop", true);
    let phone = Daemon::new("phone", true);
    desk.pair(&laptop).await;
    laptop.pair(&phone).await;
    laptop.hello_to(&phone).await;
    laptop.hello_to(&desk).await;

    let founder = phone.row(&desk.id());
    assert_eq!(
        founder.ssh_host_key.as_deref(),
        Some(desk.host_key.as_str())
    );
    assert!(
        phone
            .known_hosts()
            .lines()
            .any(|l| l == format!("sb-desk {}", desk.host_key)),
        "{}",
        phone.known_hosts()
    );
    let login = std::env::var("USER").unwrap_or_default();
    let port = desk._sshd.as_ref().unwrap().local_addr().unwrap().port();
    let dialled = phone.dialled();
    let to_desk = dialled
        .iter()
        .find(|(key, _)| *key == desk.id())
        .unwrap_or_else(|| panic!("phone never dials desk: {dialled:?}"));
    assert_eq!(
        to_desk.1.first(),
        Some(&format!("{login}@127.0.0.1:{port}"))
    );
    assert!(desk.granted().contains(&phone.id()), "{:?}", desk.granted());
}

#[tokio::test(flavor = "multi_thread")]
async fn a_founder_whose_row_lacks_its_ssh_fills_it_in_on_start_and_peers_take_it() {
    let desk = Daemon::new("desk", false);
    let laptop = Daemon::new("laptop", true);
    desk.pair(&laptop).await;
    let before = laptop.row(&desk.id());
    assert_eq!(before.ssh_host_key, None);
    assert_eq!(before.ssh_login, None);

    let (pairing, sshd, host_key) = desk.sshd_up();
    advertise_route(&desk.state.sqlite, &pairing, "desk", desk.dir.path());
    let own = desk.row(&desk.id());
    assert_eq!(own.ssh_host_key.as_deref(), Some(host_key.as_str()));
    assert!(own.ssh_login.is_some(), "{own:?}");
    assert_eq!(own.ssh_port, Some(sshd.local_addr().unwrap().port()));
    assert_eq!(own.version, before.version + 1);

    advertise_route(&desk.state.sqlite, &pairing, "desk", desk.dir.path());
    assert_eq!(
        desk.row(&desk.id()),
        own,
        "an unchanged sshd signs no new row"
    );

    desk.hello_to(&laptop).await;
    assert_eq!(laptop.row(&desk.id()), own);
    assert!(
        laptop
            .known_hosts()
            .lines()
            .any(|l| l == format!("sb-desk {host_key}")),
        "{}",
        laptop.known_hosts()
    );
}