second-brain-api 0.13.5

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
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
use std::path::Path;
use std::sync::Arc;

use anyhow::{Context, anyhow};
use axum::Json;
use axum::extract::State;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use second_brain_core::keys::{
    DeviceRoute, RecoveryPhrase, every_held_epoch, found_identity, load_device_keys,
};
use second_brain_core::sqlite_store::SqliteStore;
use second_brain_sync::dial;
use second_brain_sync::keyring::{Joined, Keyring, REMOVED, take_admission};
use second_brain_sync::mdns;
use second_brain_sync::pairing::{Admission, JoinRequest, KeyRequest};
use second_brain_sync::pairing::{HostOutcome, JoinCode, PAIR_PORT, PairingHost, Taken, join};
use second_brain_sync::ssh::{device_alias, known_hosts_path_in, pin_alias, pin_host_keys};
use serde::{Deserialize, Serialize};
use zeroize::Zeroize;

use super::{bad_request, error_response, internal_error};
use crate::device_access::DeviceAccess;
use crate::pairing::{advertise_route, local_addresses};
use crate::{AppState, identity_report};

#[derive(Default, Deserialize)]
pub(super) struct StartRequest {
    /// A pairing port to use instead of the default.
    port: Option<u16>,
    /// `sb pair --restore-keys`: hand this device's keys to a device
    /// restoring a backup instead of pairing one.
    #[serde(default)]
    restore_keys: bool,
}

#[derive(Serialize)]
struct FoundedIdentity {
    identity: String,
    phrase: String,
    fingerprint: String,
}

impl Drop for FoundedIdentity {
    fn drop(&mut self) {
        self.phrase.zeroize();
    }
}

#[derive(Serialize)]
struct Started {
    #[serde(flatten)]
    status: StatusView,
    addresses: Vec<String>,
    port: u16,
    default_port: bool,
    /// Present when this device had no identity, so `sb pair` founded one;
    /// the phrase is in this response and nowhere else.
    founded: Option<FoundedIdentity>,
}

#[derive(Serialize)]
struct DeviceView {
    machine_id: String,
    name: String,
}

#[derive(Serialize)]
struct StatusView {
    state: &'static str,
    code: Option<String>,
    invite: Option<String>,
    wrong_tries: u32,
    retired: u32,
    expires_in_secs: u64,
    device: Option<DeviceView>,
    last_error: Option<String>,
}

impl Drop for StatusView {
    fn drop(&mut self) {
        self.code.zeroize();
        self.invite.zeroize();
    }
}

impl StatusView {
    fn none() -> Self {
        Self {
            state: "none",
            code: None,
            invite: None,
            wrong_tries: 0,
            retired: 0,
            expires_in_secs: 0,
            device: None,
            last_error: None,
        }
    }

    fn of(host: &PairingHost) -> Self {
        let status = host.status();
        let (state, device) = match &status.outcome {
            HostOutcome::Waiting => ("waiting", None),
            HostOutcome::Paired { machine_id, name } => (
                "paired",
                Some(DeviceView {
                    machine_id: machine_id.clone(),
                    name: name.clone(),
                }),
            ),
            HostOutcome::HandedOver { name } => (
                "handed_over",
                Some(DeviceView {
                    machine_id: String::new(),
                    name: name.clone(),
                }),
            ),
            HostOutcome::Expired => ("expired", None),
            HostOutcome::Closed => ("closed", None),
        };
        let waiting = status.outcome == HostOutcome::Waiting;
        Self {
            state,
            code: waiting.then(|| status.code.to_string()),
            invite: waiting.then(|| status.invite.to_string()),
            wrong_tries: status.wrong_tries,
            retired: status.retired,
            expires_in_secs: status.remaining.as_secs(),
            device,
            last_error: status.last_error,
        }
    }
}

#[derive(Debug)]
struct Refused(String);

impl std::fmt::Display for Refused {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

impl std::error::Error for Refused {}

/// Hands every epoch this device holds to a device restoring a backup. It
/// founds nothing and writes no row, and a removed device may still do it.
fn start_key_handover(state: &Arc<AppState>, port: Option<u16>) -> anyhow::Result<Started> {
    let device = load_device_keys(&state.data_dir)?
        .context("this device has no device key yet; restart the daemon to create one")?;
    if every_held_epoch(&state.sqlite, &device)?.is_empty() {
        return Err(Refused(
            "this device holds no key of any sync identity, so it has nothing to hand over"
                .to_string(),
        )
        .into());
    }
    let listener = state.pairing.reserve(port)?;
    let name = state.machine.name.clone();
    let addresses = local_addresses(&name);
    let hand_over = {
        let store = state.sqlite.clone();
        let data_dir = state.data_dir.clone();
        Box::new(move |request: &KeyRequest| {
            Keyring::load(&store, &data_dir)?.hand_over_keys(request, &name)
        })
    };
    let port = state
        .pairing
        .start_key_handover(listener, addresses.clone(), hand_over)?;
    let status = state
        .pairing
        .with_host(|host| host.map(StatusView::of))
        .ok_or_else(|| anyhow!("the pairing listener closed as it opened"))?;
    Ok(Started {
        status,
        addresses,
        port,
        default_port: port == PAIR_PORT,
        founded: None,
    })
}

/// Founds an identity first when this device has none, so a store that
/// never synced can always offer pairing.
fn start(state: &Arc<AppState>, port: Option<u16>) -> anyhow::Result<Started> {
    let listener = state.pairing.reserve(port)?;
    let now = state.clock.wall_micros();
    let name = state.machine.name.clone();
    let keyring = Keyring::load(&state.sqlite, &state.data_dir)?;
    if keyring.identity().is_some() && keyring.is_removed()? {
        return Err(Refused(REMOVED.to_string()).into());
    }
    let founded = if keyring.identity().is_none() {
        let device = load_device_keys(&state.data_dir)?
            .context("this device has no device key yet; restart the daemon to create one")?;
        let phrase = RecoveryPhrase::generate()?;
        let founded = found_identity(&state.sqlite, &device, &name, &[phrase.recipient()?], now)?;
        state.sqlite.mark_recovery_offered(now)?;
        if let Err(e) = state.bootstrap.cancel(&state.sqlite) {
            tracing::warn!("cannot end this device's wait to join an identity: {e:#}");
        }
        identity_report::report(&state.sqlite, &state.data_dir, &state.health);
        tracing::info!(
            "founded sync identity {} to pair from",
            founded.tag.as_str()
        );
        Some(FoundedIdentity {
            identity: founded.tag.as_str().to_string(),
            phrase: String::from(phrase.expose_text().as_str()),
            fingerprint: phrase.fingerprint()?,
        })
    } else {
        None
    };

    let addresses = local_addresses(&name);
    let ssh = state.pairing.local_ssh();
    advertise_route(&state.sqlite, &state.pairing, &name, &state.data_dir);
    let admit = {
        let store = state.sqlite.clone();
        let data_dir = state.data_dir.clone();
        let access = state.device_access.clone();
        let addresses = addresses.clone();
        Box::new(move |request: &JoinRequest| {
            let admission = Keyring::load(&store, &data_dir)?.admit(
                request,
                &name,
                ssh.clone(),
                addresses.clone(),
            )?;
            let host_keys = request.ssh.as_ref().map(|s| s.host_keys.clone());
            let route = DeviceRoute {
                machine_id: request.machine_id.clone(),
                addresses: request.addresses.clone(),
                ssh_login: request.ssh.as_ref().and_then(|s| s.login.clone()),
                ssh_port: request.ssh.as_ref().map(|s| s.port),
            };
            let failed = reach_paired(
                &store,
                &data_dir,
                &access,
                route,
                &host_keys.unwrap_or_default(),
            );
            if !failed.is_empty() {
                tracing::warn!(
                    "paired {} but could not finish setting it up: {}",
                    request.machine_id,
                    failed.join("; ")
                );
            }
            Ok(admission)
        })
    };
    let port = state.pairing.start(listener, addresses.clone(), admit)?;
    let status = state
        .pairing
        .with_host(|host| host.map(StatusView::of))
        .ok_or_else(|| anyhow!("the pairing listener closed as it opened"))?;
    Ok(Started {
        status,
        addresses,
        port,
        default_port: port == PAIR_PORT,
        founded,
    })
}

pub(super) async fn pair_start(
    State(state): State<Arc<AppState>>,
    body: Option<Json<StartRequest>>,
) -> Response {
    let body = body.map(|Json(b)| b).unwrap_or_default();
    let (port, restore_keys) = (body.port, body.restore_keys);
    let started = tokio::task::spawn_blocking(move || {
        if restore_keys {
            start_key_handover(&state, port)
        } else {
            start(&state, port)
        }
    });
    match started.await {
        Ok(Ok(started)) => Json(started).into_response(),
        Ok(Err(e)) if e.downcast_ref::<Refused>().is_some() => {
            error_response(StatusCode::CONFLICT, format!("{e:#}"))
        }
        Ok(Err(e)) => internal_error(format!("{e:#}")),
        Err(e) => internal_error(e),
    }
}

pub(super) async fn pair_status(State(state): State<Arc<AppState>>) -> Response {
    let status = state
        .pairing
        .with_host(|host| host.map_or_else(StatusView::none, StatusView::of));
    Json(status).into_response()
}

pub(super) async fn pair_close(State(state): State<Arc<AppState>>) -> Response {
    Json(serde_json::json!({"closed": state.pairing.close()})).into_response()
}

/// `sb pair --stop-waiting`, and `sb setup`'s "start a new identity": ingest
/// now instead of waiting for a bootstrap that may never come, as when the
/// pairing device cannot be reached over ssh from here.
pub(super) async fn pair_stop_waiting(State(state): State<Arc<AppState>>) -> Response {
    match tokio::task::spawn_blocking(move || state.bootstrap.cancel(&state.sqlite)).await {
        Ok(Ok(cancelled)) => Json(serde_json::json!({"cancelled": cancelled})).into_response(),
        Ok(Err(e)) => internal_error(format!("{e:#}")),
        Err(e) => internal_error(e),
    }
}

#[derive(Deserialize)]
pub(super) struct JoinBody {
    code: String,
    host: Option<String>,
}

impl Drop for JoinBody {
    fn drop(&mut self) {
        self.code.zeroize();
    }
}

#[derive(Serialize)]
struct JoinedView {
    identity: String,
    host: DeviceView,
    epochs: usize,
    moved: bool,
    devices: usize,
    known_hosts: Vec<String>,
    /// Set when the join was taken but a step after it failed.
    #[serde(skip_serializing_if = "Option::is_none")]
    warning: Option<String>,
}

fn joined_view(
    joined: &Joined,
    admission_host: DeviceView,
    pinned: Vec<String>,
    warning: Option<String>,
) -> JoinedView {
    JoinedView {
        identity: joined.identity.as_str().to_string(),
        host: admission_host,
        epochs: joined.epochs.len(),
        moved: joined.moved,
        devices: joined.devices,
        known_hosts: pinned,
        warning,
    }
}

fn join_host(
    state: &Arc<AppState>,
    code: &JoinCode,
    addresses: &[(String, u16)],
) -> anyhow::Result<JoinedView> {
    let request = Keyring::load(&state.sqlite, &state.data_dir)?.join_request(
        &state.machine.name,
        state.pairing.local_ssh(),
        local_addresses(&state.machine.name),
    )?;
    let bootstraps = !state.sqlite.holds_replicated_rows()?;
    let marked = bootstraps && !state.bootstrap.is_pending();
    if marked {
        state.bootstrap.mark_pending(&state.sqlite)?;
    }
    let admitted: std::cell::RefCell<Option<(String, String)>> = std::cell::RefCell::new(None);
    let result = join(addresses, code, &request, |admission, dialed| {
        let joined = take_admission(
            &state.sqlite,
            &state.data_dir,
            admission,
            state.clock.wall_micros(),
        )?;
        *admitted.borrow_mut() = Some((admission.machine_id.clone(), admission.name.clone()));
        let follow_up = after_join(state, admission, dialed);
        Ok((joined, follow_up))
    });
    match admitted.into_inner() {
        Some((host_id, host_name)) => {
            if state.bootstrap.is_pending() && !dials_device(state, &host_id) {
                tracing::warn!(
                    "{host_name} runs no sshd this device can reach, so its snapshot never \
                     comes; ingesting without it"
                );
                if let Err(e) = state.bootstrap.cancel(&state.sqlite) {
                    tracing::warn!("cannot end the wait for a bootstrap: {e:#}");
                }
            }
        }
        None if marked && result.is_err() => {
            if let Err(e) = state.bootstrap.cancel(&state.sqlite) {
                tracing::warn!("cannot clear the bootstrap a failed join marked: {e:#}");
            }
        }
        None => {}
    }
    let Taken {
        taken: (joined, follow_up),
        admission,
        unacked,
    } = result?;
    identity_report::report(&state.sqlite, &state.data_dir, &state.health);
    tracing::info!(
        "paired into identity {} through {} ({})",
        joined.identity.as_str(),
        admission.name,
        admission.machine_id
    );
    let warning = join_warning(&admission.name, &follow_up, unacked.as_deref());
    if let Some(warning) = &warning {
        tracing::warn!("{warning}");
    }
    let pinned = follow_up.pinned;
    Ok(joined_view(
        &joined,
        DeviceView {
            machine_id: admission.machine_id.clone(),
            name: admission.name.clone(),
        },
        pinned,
        warning,
    ))
}

/// The bootstrap pulls from the pairing device, so it can only finish when
/// this device can dial it.
fn dials_device(state: &Arc<AppState>, machine_id: &str) -> bool {
    match dial::device_dials(&state.sqlite) {
        Ok(dials) => dials.iter().any(|d| d.key == machine_id),
        Err(e) => {
            tracing::warn!("cannot read the devices to sync with: {e:#}");
            true
        }
    }
}

/// What `sb pair <code>` prints after a join that stands but did not
/// finish cleanly; None when every step after it succeeded.
fn join_warning(host: &str, follow_up: &FollowUp, unacked: Option<&str>) -> Option<String> {
    let mut problems = Vec::new();
    if let Some(e) = unacked {
        problems.push(format!(
            "{host} was not told the join succeeded ({e}), so it may show the pairing as failed"
        ));
    }
    if !follow_up.failed.is_empty() {
        problems.push(format!(
            "setting up {host} did not finish: {}",
            follow_up.failed.join("; ")
        ));
    }
    if follow_up.pin_failed {
        problems.push(format!(
            "the host keys were not pinned, so ssh will ask you to trust {host} the first time \
             it connects"
        ));
    }
    (!problems.is_empty()).then(|| format!("the join is done, but {}", problems.join("; ")))
}

struct FollowUp {
    pinned: Vec<String>,
    pin_failed: bool,
    failed: Vec<String>,
}

/// What follows a taken admission: the floor neighbours, the host's route and
/// ssh access, and its host keys pinned under the names it was reached by.
/// Each step runs even when one before it failed, and failures are logged.
/// Returns the known_hosts entries written and the steps that failed.
fn after_join(state: &Arc<AppState>, admission: &Admission, dialed: &str) -> FollowUp {
    let mut failed: Vec<String> = Vec::new();
    let members: Vec<String> = admission
        .keys
        .devices
        .iter()
        .map(|row| row.record.machine_id.clone())
        .collect();
    if let Err(e) = state.sqlite.seed_floor_neighbours(&members) {
        failed.push(format!("seeding floor neighbours: {e:#}"));
    }
    let mut route_addresses = vec![dialed.to_string()];
    route_addresses.extend(admission.addresses.iter().cloned());
    let route = DeviceRoute {
        machine_id: admission.machine_id.clone(),
        addresses: route_addresses,
        ssh_login: admission.ssh.as_ref().and_then(|s| s.login.clone()),
        ssh_port: admission.ssh.as_ref().map(|s| s.port),
    };
    let host_keys = admission
        .ssh
        .as_ref()
        .map(|s| s.host_keys.clone())
        .unwrap_or_default();
    failed.extend(reach_paired(
        &state.sqlite,
        &state.data_dir,
        &state.device_access,
        route,
        &host_keys,
    ));
    let mut pinned = Vec::new();
    let mut pin_failed = false;
    if let Some(ssh) = &admission.ssh {
        let mut hosts = vec![dialed.to_string()];
        for address in &admission.addresses {
            if !hosts.contains(address) {
                hosts.push(address.clone());
            }
        }
        match pin_host_keys(
            &known_hosts_path_in(&state.data_dir),
            &hosts,
            ssh.port,
            &ssh.host_keys,
        ) {
            Ok(written) => pinned = written,
            Err(e) => {
                pin_failed = true;
                failed.push(format!(
                    "pinning its host keys in {}: {e:#}",
                    known_hosts_path_in(&state.data_dir).display()
                ));
            }
        }
    }
    if !failed.is_empty() {
        tracing::warn!(
            "joined through {} but could not finish setting it up: {}",
            admission.machine_id,
            failed.join("; ")
        );
    }
    FollowUp {
        pinned,
        pin_failed,
        failed,
    }
}

/// Lets the newly paired device in and out over ssh: its route, its host
/// keys pinned by machine id, the forced-command line for it, and a sync
/// round at once so the device list reaches every peer. Each step runs even
/// when one before it failed; returns what failed.
fn reach_paired(
    sqlite: &SqliteStore,
    data_dir: &Path,
    access: &DeviceAccess,
    route: DeviceRoute,
    host_keys: &[String],
) -> Vec<String> {
    let mut failed = Vec::new();
    let alias = device_alias(&route.machine_id);
    if let Err(e) = sqlite.set_device_route(route) {
        failed.push(format!("storing its route: {e:#}"));
    }
    if !host_keys.is_empty()
        && let Err(e) = pin_alias(&known_hosts_path_in(data_dir), &alias, host_keys)
    {
        failed.push(format!("pinning its host keys: {e:#}"));
    }
    if let Err(e) = access.reconcile(sqlite, data_dir) {
        failed.push(format!("updating ssh access: {e:#}"));
    }
    access.kick();
    failed
}

pub(super) async fn pair_join(
    State(state): State<Arc<AppState>>,
    Json(body): Json<JoinBody>,
) -> Response {
    let code = match JoinCode::parse(&body.code) {
        Ok(code) => code,
        Err(e) => return bad_request(format!("{e:#}")),
    };
    let addresses = match code.addresses(body.host.as_deref()) {
        Ok(addresses) => addresses,
        Err(e) => return bad_request(format!("{e:#}")),
    };
    let joined = tokio::task::spawn_blocking(move || {
        let addresses = if addresses.is_empty() {
            pairing_devices_found(&state)
        } else {
            addresses
        };
        if addresses.is_empty() {
            return Ok(None);
        }
        join_host(&state, &code, &addresses).map(Some)
    })
    .await;
    match joined {
        Ok(Ok(Some(joined))) => Json(joined).into_response(),
        Ok(Ok(None)) => bad_request(
            "no pairing device answered on this network; name it with --host, as `sb pair` \
             shows on that device",
        ),
        Ok(Err(e)) => error_response(StatusCode::CONFLICT, format!("{e:#}")),
        Err(e) => internal_error(e),
    }
}

/// The open pairing listeners an mDNS lookup finds, this device's own
/// left out.
fn pairing_devices_found(state: &AppState) -> Vec<(String, u16)> {
    let Some(net) = state.pairing.discovery() else {
        return Vec::new();
    };
    let found = match mdns::browse(net, mdns::PAIR_SERVICE, mdns::LOOKUP_WINDOW) {
        Ok(found) => found,
        Err(e) => {
            tracing::warn!("cannot look for a pairing device on the network: {e:#}");
            return Vec::new();
        }
    };
    let own = state.sqlite.machine_id();
    let mut addresses = Vec::new();
    for listener in found {
        let address = (listener.address.to_string(), listener.port);
        if listener.get(mdns::TXT_ID) != Some(own) && !addresses.contains(&address) {
            addresses.push(address);
        }
    }
    addresses
}

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

    fn follow_up(failed: &[&str], pin_failed: bool) -> FollowUp {
        FollowUp {
            pinned: Vec::new(),
            pin_failed,
            failed: failed.iter().map(|s| s.to_string()).collect(),
        }
    }

    #[test]
    fn a_join_that_stands_warns_about_each_step_that_did_not_finish() {
        assert_eq!(join_warning("desk", &follow_up(&[], false), None), None);

        let unacked = join_warning("desk", &follow_up(&[], false), Some("broken pipe")).unwrap();
        assert!(unacked.starts_with("the join is done, but "), "{unacked}");
        assert!(
            unacked.contains("desk was not told the join succeeded (broken pipe)"),
            "{unacked}"
        );
        assert!(!unacked.contains("pinned"), "{unacked}");

        let unpinned = join_warning(
            "desk",
            &follow_up(&["pinning its host keys in /d/known_hosts: denied"], true),
            None,
        )
        .unwrap();
        assert!(unpinned.contains("/d/known_hosts: denied"), "{unpinned}");
        assert!(unpinned.contains("host keys were not pinned"), "{unpinned}");
        assert!(
            unpinned.contains("ssh will ask you to trust desk"),
            "{unpinned}"
        );
        assert!(!unpinned.contains("was not told"), "{unpinned}");
    }
}