Skip to main content

second_brain_api/
self_update.rs

1use std::collections::BTreeMap;
2use std::fs;
3use std::future::Future;
4use std::io::{self, Read, Write};
5use std::os::unix::fs::PermissionsExt;
6use std::path::{Component, Path, PathBuf};
7use std::time::Duration;
8
9use anyhow::{Context, bail};
10use minisign_verify::{PublicKey, Signature};
11use semver::Version;
12
13pub const RELEASE_PUBLIC_KEY: &str = "RWR7ibyKrwoxCS3nV49/ndvyqAx4vOpSZSnqaijCMK1VlN1mq36HeQ2u";
14
15/// Nonzero because the systemd unit `sb setup` writes restarts only on failure.
16pub const EXIT_FOR_RESTART: i32 = 75;
17
18pub const FIRST_CHECK_DELAY: Duration = Duration::from_secs(10 * 60);
19pub const CHECK_INTERVAL: Duration = Duration::from_secs(6 * 60 * 60);
20pub const MAX_ASSET_BYTES: usize = 256 * 1024 * 1024;
21pub const MAX_SIGNATURE_BYTES: usize = 4096;
22const MAX_UNPACKED_BYTES: usize = 1024 * 1024 * 1024;
23const HTTP_TIMEOUT: Duration = Duration::from_secs(10 * 60);
24
25const PACKAGES: [&str; 3] = ["second-brain-api", "second-brain-cli", "second-brain-mcp"];
26const DAEMON: &str = "second-brain-api";
27const STAGING_DIR: &str = ".second-brain-update";
28const TARGETS: [(&str, &str, &str); 3] = [
29    ("linux", "x86_64", "x86_64-unknown-linux-gnu"),
30    ("linux", "aarch64", "aarch64-unknown-linux-gnu"),
31    ("macos", "aarch64", "aarch64-apple-darwin"),
32];
33
34#[derive(Debug, Clone)]
35pub struct Install {
36    pub dir: PathBuf,
37    pub current: Version,
38    pub target: &'static str,
39}
40
41#[derive(Debug)]
42pub enum Outcome {
43    UpToDate {
44        latest: Version,
45    },
46    Installed {
47        version: Version,
48        replaced: Vec<String>,
49    },
50    Refused {
51        version: Version,
52        reason: String,
53    },
54}
55
56pub fn decide_install(release_build: bool, exe: &Path, os: &str, arch: &str) -> Option<Install> {
57    if !release_build {
58        return None;
59    }
60    let (_, _, target) = TARGETS.iter().find(|(o, a, _)| *o == os && *a == arch)?;
61    Some(Install {
62        dir: exe.parent()?.to_path_buf(),
63        current: Version::parse(env!("CARGO_PKG_VERSION")).ok()?,
64        target,
65    })
66}
67
68pub fn this_install() -> Option<Install> {
69    let exe = std::env::current_exe().and_then(fs::canonicalize).ok()?;
70    decide_install(
71        cfg!(feature = "self-update"),
72        &exe,
73        std::env::consts::OS,
74        std::env::consts::ARCH,
75    )
76}
77
78pub trait ReleaseSource {
79    fn latest_tag(&self) -> impl Future<Output = anyhow::Result<String>> + Send;
80    fn fetch(
81        &self,
82        tag: &str,
83        asset: &str,
84        limit: usize,
85    ) -> impl Future<Output = anyhow::Result<Vec<u8>>> + Send;
86}
87
88pub struct GitHubReleases {
89    owner: String,
90    repo: String,
91    client: reqwest::Client,
92}
93
94impl GitHubReleases {
95    pub fn from_repository(repository: &str) -> Option<Self> {
96        let path = repository
97            .trim_end_matches('/')
98            .trim_end_matches(".git")
99            .strip_prefix("https://github.com/")?;
100        let (owner, repo) = path.split_once('/')?;
101        if owner.is_empty() || repo.is_empty() || repo.contains('/') {
102            return None;
103        }
104        let client = reqwest::Client::builder()
105            .user_agent(concat!("second-brain/", env!("CARGO_PKG_VERSION")))
106            .timeout(HTTP_TIMEOUT)
107            .build()
108            .ok()?;
109        Some(Self {
110            owner: owner.to_string(),
111            repo: repo.to_string(),
112            client,
113        })
114    }
115
116    pub fn download_url(&self, tag: &str, asset: &str) -> String {
117        format!(
118            "https://github.com/{}/{}/releases/download/{tag}/{asset}",
119            self.owner, self.repo
120        )
121    }
122
123    fn latest_url(&self) -> String {
124        format!(
125            "https://api.github.com/repos/{}/{}/releases/latest",
126            self.owner, self.repo
127        )
128    }
129}
130
131#[derive(serde::Deserialize)]
132struct LatestRelease {
133    tag_name: String,
134}
135
136impl ReleaseSource for GitHubReleases {
137    async fn latest_tag(&self) -> anyhow::Result<String> {
138        let latest: LatestRelease = self
139            .client
140            .get(self.latest_url())
141            .header(reqwest::header::ACCEPT, "application/vnd.github+json")
142            .send()
143            .await?
144            .error_for_status()?
145            .json()
146            .await?;
147        Ok(latest.tag_name)
148    }
149
150    async fn fetch(&self, tag: &str, asset: &str, limit: usize) -> anyhow::Result<Vec<u8>> {
151        let mut response = self
152            .client
153            .get(self.download_url(tag, asset))
154            .send()
155            .await?
156            .error_for_status()?;
157        if response.content_length().is_some_and(|n| n > limit as u64) {
158            bail!("{asset} is larger than {limit} bytes");
159        }
160        let mut body = Vec::new();
161        while let Some(chunk) = response.chunk().await? {
162            if body.len() + chunk.len() > limit {
163                bail!("{asset} is larger than {limit} bytes");
164            }
165            body.extend_from_slice(&chunk);
166        }
167        Ok(body)
168    }
169}
170
171struct SignedArchive {
172    package: &'static str,
173    name: String,
174    bytes: Vec<u8>,
175    signature: Vec<u8>,
176}
177
178pub fn archive_name(package: &str, target: &str) -> String {
179    format!("{package}-{target}.tar.xz")
180}
181
182pub fn trusted_comment(version: &Version, archive: &str) -> String {
183    format!("second-brain {version} {archive}")
184}
185
186pub async fn check_once<S: ReleaseSource>(
187    source: &S,
188    install: &Install,
189    key: &PublicKey,
190) -> anyhow::Result<Outcome> {
191    let tag = source.latest_tag().await?;
192    let version = Version::parse(tag.strip_prefix('v').unwrap_or(&tag))
193        .with_context(|| format!("latest release tag {tag:?} is not a version"))?;
194    if version <= install.current {
195        return Ok(Outcome::UpToDate { latest: version });
196    }
197    if !is_regular_file(&install.dir.join(DAEMON)) {
198        return Ok(Outcome::Refused {
199            version,
200            reason: format!("{} has no {DAEMON} to replace", install.dir.display()),
201        });
202    }
203    if let Err(e) = probe_writable(&install.dir) {
204        return Ok(Outcome::Refused {
205            version,
206            reason: format!("cannot write to {}: {e}", install.dir.display()),
207        });
208    }
209
210    let mut archives = Vec::with_capacity(PACKAGES.len());
211    for package in PACKAGES {
212        let name = archive_name(package, install.target);
213        let signature = source
214            .fetch(&tag, &format!("{name}.minisig"), MAX_SIGNATURE_BYTES)
215            .await?;
216        let bytes = source.fetch(&tag, &name, MAX_ASSET_BYTES).await?;
217        let archive = SignedArchive {
218            package,
219            name,
220            bytes,
221            signature,
222        };
223        if let Err(reason) = verify(key, &version, &archive) {
224            return Ok(Outcome::Refused { version, reason });
225        }
226        archives.push(archive);
227    }
228
229    let install = install.clone();
230    tokio::task::spawn_blocking(move || unpack_and_install(&install, version, archives)).await?
231}
232
233fn verify(key: &PublicKey, version: &Version, archive: &SignedArchive) -> Result<(), String> {
234    let name = &archive.name;
235    let text = std::str::from_utf8(&archive.signature)
236        .map_err(|_| format!("{name}.minisig is not text"))?;
237    let signature =
238        Signature::decode(text).map_err(|e| format!("{name}.minisig does not parse: {e}"))?;
239    key.verify(&archive.bytes, &signature, false)
240        .map_err(|e| format!("{name} fails signature verification: {e}"))?;
241    let expected = trusted_comment(version, name);
242    let found = signature.trusted_comment();
243    if found != expected {
244        return Err(format!(
245            "{name} is signed with trusted comment {found:?}, expected {expected:?}"
246        ));
247    }
248    Ok(())
249}
250
251fn unpack_and_install(
252    install: &Install,
253    version: Version,
254    archives: Vec<SignedArchive>,
255) -> anyhow::Result<Outcome> {
256    let files = match select_files(install, &archives) {
257        Ok(files) => files,
258        Err(reason) => return Ok(Outcome::Refused { version, reason }),
259    };
260    drop(archives);
261    swap_in(&install.dir, &files)?;
262    Ok(Outcome::Installed {
263        version,
264        replaced: files.into_keys().collect(),
265    })
266}
267
268fn select_files(
269    install: &Install,
270    archives: &[SignedArchive],
271) -> Result<BTreeMap<String, Vec<u8>>, String> {
272    let mut selected: BTreeMap<String, Vec<u8>> = BTreeMap::new();
273    for archive in archives {
274        let top = format!("{}-{}", archive.package, install.target);
275        let files = unpack(&archive.name, &top, &archive.bytes)?;
276        if archive.package == DAEMON && !files.contains_key(DAEMON) {
277            return Err(format!("{} has no {DAEMON}", archive.name));
278        }
279        for (name, data) in files {
280            if !is_regular_file(&install.dir.join(&name)) {
281                continue;
282            }
283            match selected.get(&name) {
284                Some(existing) if *existing != data => {
285                    return Err(format!("{name} differs between archives"));
286                }
287                Some(_) => {}
288                None => {
289                    selected.insert(name, data);
290                }
291            }
292        }
293    }
294    if !selected.contains_key(DAEMON) {
295        return Err(format!(
296            "{} has no {DAEMON} to replace",
297            install.dir.display()
298        ));
299    }
300    Ok(selected)
301}
302
303fn probe_writable(dir: &Path) -> io::Result<()> {
304    let staging = dir.join(STAGING_DIR);
305    match fs::remove_dir_all(&staging) {
306        Err(e) if e.kind() != io::ErrorKind::NotFound => return Err(e),
307        _ => {}
308    }
309    fs::create_dir(&staging)?;
310    fs::remove_dir(&staging)
311}
312
313fn is_regular_file(path: &Path) -> bool {
314    fs::symlink_metadata(path).is_ok_and(|m| m.is_file())
315}
316
317/// Executable regular files directly under `top`, keyed by file name.
318fn unpack(name: &str, top: &str, xz: &[u8]) -> Result<BTreeMap<String, Vec<u8>>, String> {
319    let mut tar_bytes = Vec::new();
320    lzma_rs::xz_decompress(
321        &mut &xz[..],
322        &mut Capped {
323            inner: &mut tar_bytes,
324            left: MAX_UNPACKED_BYTES,
325        },
326    )
327    .map_err(|e| format!("{name} does not decompress: {e}"))?;
328
329    let mut files = BTreeMap::new();
330    let mut archive = tar::Archive::new(tar_bytes.as_slice());
331    let entries = archive
332        .entries()
333        .map_err(|e| format!("{name} is not a tar archive: {e}"))?;
334    for entry in entries {
335        let mut entry = entry.map_err(|e| format!("{name} is not a valid tar archive: {e}"))?;
336        let path = entry
337            .path()
338            .map_err(|e| format!("{name} has an unreadable entry path: {e}"))?
339            .into_owned();
340        let mut parts = Vec::new();
341        for component in path.components() {
342            match component {
343                Component::Normal(part) => parts.push(part),
344                Component::CurDir => {}
345                _ => {
346                    return Err(format!(
347                        "{name} has an entry outside its directory: {}",
348                        path.display()
349                    ));
350                }
351            }
352        }
353        let kind = entry.header().entry_type();
354        if kind.is_dir() || kind.is_pax_global_extensions() {
355            continue;
356        }
357        if !kind.is_file() {
358            return Err(format!(
359                "{name} has a non-regular entry: {}",
360                path.display()
361            ));
362        }
363        let [dir, file] = parts.as_slice() else {
364            continue;
365        };
366        let mode = entry
367            .header()
368            .mode()
369            .map_err(|e| format!("{name} has an unreadable entry mode: {e}"))?;
370        if *dir != top || mode & 0o111 == 0 {
371            continue;
372        }
373        let file = file
374            .to_str()
375            .ok_or_else(|| format!("{name} has a non-UTF-8 file name"))?
376            .to_string();
377        let mut data = Vec::new();
378        entry
379            .read_to_end(&mut data)
380            .map_err(|e| format!("{name} has an unreadable entry {file}: {e}"))?;
381        files.insert(file, data);
382    }
383    Ok(files)
384}
385
386struct Capped<'a> {
387    inner: &'a mut Vec<u8>,
388    left: usize,
389}
390
391impl Write for Capped<'_> {
392    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
393        if buf.len() > self.left {
394            return Err(io::Error::other(format!(
395                "unpacks to more than {MAX_UNPACKED_BYTES} bytes"
396            )));
397        }
398        self.left -= buf.len();
399        self.inner.extend_from_slice(buf);
400        Ok(buf.len())
401    }
402
403    fn flush(&mut self) -> io::Result<()> {
404        Ok(())
405    }
406}
407
408#[derive(Debug)]
409pub struct PartialInstall;
410
411impl std::fmt::Display for PartialInstall {
412    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
413        f.write_str("self-update left a partial install")
414    }
415}
416
417enum SwapError {
418    RolledBack(io::Error),
419    Stuck(anyhow::Error),
420}
421
422/// Stages every file next to the install so each replacement is a same-filesystem
423/// rename, and restores the originals if any rename fails.
424fn swap_in(dir: &Path, files: &BTreeMap<String, Vec<u8>>) -> anyhow::Result<()> {
425    let staging = dir.join(STAGING_DIR);
426    match fs::remove_dir_all(&staging) {
427        Err(e) if e.kind() != io::ErrorKind::NotFound => {
428            return Err(e).with_context(|| format!("cannot clear {}", staging.display()));
429        }
430        _ => {}
431    }
432    fs::create_dir(&staging).with_context(|| format!("cannot create {}", staging.display()))?;
433    match stage_and_swap(dir, &staging, files) {
434        Ok(()) => {
435            if let Err(e) = fs::remove_dir_all(&staging) {
436                tracing::warn!("cannot remove {}: {e}", staging.display());
437            }
438            Ok(())
439        }
440        Err(SwapError::RolledBack(e)) => {
441            if let Err(cleanup) = fs::remove_dir_all(&staging) {
442                tracing::warn!("cannot remove {}: {cleanup}", staging.display());
443            }
444            Err(e).with_context(|| format!("cannot install into {}", dir.display()))
445        }
446        Err(SwapError::Stuck(e)) => Err(e.context(PartialInstall)),
447    }
448}
449
450fn keep_backups(dir: &Path, old: &Path) -> String {
451    let kept = dir.join(format!(
452        "{STAGING_DIR}-backup-{}",
453        chrono::Utc::now().format("%Y%m%dT%H%M%S")
454    ));
455    match fs::rename(old, &kept) {
456        Ok(()) => kept.display().to_string(),
457        Err(e) => format!(
458            "{}, which the next update deletes because moving them out failed ({e})",
459            old.display()
460        ),
461    }
462}
463
464fn stage_and_swap(
465    dir: &Path,
466    staging: &Path,
467    files: &BTreeMap<String, Vec<u8>>,
468) -> Result<(), SwapError> {
469    let new = staging.join("new");
470    let old = staging.join("old");
471    stage(&new, &old, files).map_err(SwapError::RolledBack)?;
472
473    let mut swapped: Vec<&str> = Vec::new();
474    for name in files.keys() {
475        let live = dir.join(name);
476        let backup = old.join(name);
477        let step = fs::hard_link(&live, &backup)
478            .or_else(|_| fs::copy(&live, &backup).map(drop))
479            .and_then(|()| fs::rename(new.join(name), &live));
480        if let Err(e) = step {
481            return Err(match roll_back(dir, &old, &swapped) {
482                Ok(()) => SwapError::RolledBack(e),
483                Err(rollback) => SwapError::Stuck(anyhow::anyhow!(
484                    "cannot install {} ({e}) or restore the previous binaries ({rollback}); they are in {}",
485                    live.display(),
486                    keep_backups(dir, &old)
487                )),
488            });
489        }
490        swapped.push(name);
491    }
492    if let Err(e) = fs::File::open(dir).and_then(|d| d.sync_all()) {
493        tracing::warn!("cannot sync {}: {e}", dir.display());
494    }
495    Ok(())
496}
497
498fn stage(new: &Path, old: &Path, files: &BTreeMap<String, Vec<u8>>) -> io::Result<()> {
499    fs::create_dir(new)?;
500    fs::create_dir(old)?;
501    for (name, data) in files {
502        let path = new.join(name);
503        let mut file = fs::File::create(&path)?;
504        file.write_all(data)?;
505        file.sync_all()?;
506        fs::set_permissions(&path, fs::Permissions::from_mode(0o755))?;
507    }
508    Ok(())
509}
510
511fn roll_back(dir: &Path, old: &Path, swapped: &[&str]) -> io::Result<()> {
512    for name in swapped.iter().rev() {
513        fs::rename(old.join(name), dir.join(name))?;
514    }
515    Ok(())
516}
517
518/// Logs the outcome and returns the exit code the daemon must exit with, if any.
519pub fn report(outcome: &Outcome) -> Option<i32> {
520    match outcome {
521        Outcome::Installed { version, replaced } => {
522            tracing::info!(
523                "installed second-brain {version} ({}); exiting so the service manager restarts the daemon",
524                replaced.join(", ")
525            );
526            Some(EXIT_FOR_RESTART)
527        }
528        Outcome::Refused { version, reason } => {
529            tracing::error!("refusing to install second-brain {version}: {reason}");
530            None
531        }
532        Outcome::UpToDate { latest } => {
533            tracing::debug!("self-update: latest release {latest} is not newer");
534            None
535        }
536    }
537}
538
539pub fn spawn() {
540    let Some(install) = this_install() else {
541        tracing::info!("self-update off: not a release build for a published target");
542        return;
543    };
544    let Some(source) = GitHubReleases::from_repository(env!("CARGO_PKG_REPOSITORY")) else {
545        tracing::error!(
546            "self-update off: cannot use {} as a GitHub release source",
547            env!("CARGO_PKG_REPOSITORY")
548        );
549        return;
550    };
551    let key = match PublicKey::from_base64(RELEASE_PUBLIC_KEY) {
552        Ok(key) => key,
553        Err(e) => {
554            tracing::error!("self-update off: the release public key does not decode: {e}");
555            return;
556        }
557    };
558    tracing::info!(
559        "self-update on for {} in {}",
560        install.target,
561        install.dir.display()
562    );
563    tokio::spawn(async move {
564        tokio::time::sleep(FIRST_CHECK_DELAY).await;
565        loop {
566            match check_once(&source, &install, &key).await {
567                Ok(outcome) => {
568                    if let Some(code) = report(&outcome) {
569                        std::process::exit(code);
570                    }
571                }
572                Err(e) if e.is::<PartialInstall>() => tracing::error!("{e:#}"),
573                Err(e) => tracing::warn!("self-update check failed: {e:#}"),
574            }
575            tokio::time::sleep(CHECK_INTERVAL).await;
576        }
577    });
578}