zc2 0.0.50

P2P compute broker with yen-denominated billing, WAL, and broker mesh support
//! The per-node worker key (`ZAKURO_WORKER_KEY`, ADR ZC-0012).
//!
//! The broker's `POST /workers`, `/workers/heartbeat` and `DELETE /workers/{id}`
//! take a shared secret in `X-Worker-Key`. With no key configured a request
//! from loopback is let through and one from anywhere else is counted
//! (`zero_trust.keyless_worker_nonloopback`), so a broker that listens on the
//! mesh took worker registrations from any host that could reach it.
//!
//! A broker this node's agent (or `zc up`) starts gets a random key of its own
//! when none is configured, kept `0600` at `<zakuro home>/worker.key` so every
//! restart, and every `zc serve` that registers a worker with that broker,
//! uses the same one. An explicit `ZAKURO_WORKER_KEY` always wins and nothing
//! is written. The key is for the process that REGISTERS workers (`zc serve`)
//! and for the agent's own `DELETE` calls; it is not handed to the worker
//! processes or containers, which run job code and never register themselves.

use std::io::Write;
use std::path::{Path, PathBuf};

/// File name under the zakuro home.
pub const FILE: &str = "worker.key";
/// Env var read by the broker and sent as `X-Worker-Key`.
pub const ENV: &str = "ZAKURO_WORKER_KEY";

fn path(dir: &Path) -> PathBuf {
    dir.join(FILE)
}

fn clean(v: Option<String>) -> Option<String> {
    v.map(|s| s.trim().to_string()).filter(|s| !s.is_empty())
}

/// The key a client should send: the env value when set, else the one saved
/// beside the broker's state. `None` when neither exists (an unkeyed broker).
pub fn resolve(env: Option<String>, dir: Option<&Path>) -> Option<String> {
    clean(env).or_else(|| clean(dir.and_then(|d| std::fs::read_to_string(path(d)).ok())))
}

/// The key to send to the broker at `broker_url`: an explicit
/// `ZAKURO_WORKER_KEY` goes to whichever broker was named; the key this node
/// generated for its own broker only to a loopback one, so it never leaves the
/// machine it protects.
pub fn for_broker(broker_url: &str, env: Option<String>, dir: Option<&Path>) -> Option<String> {
    clean(env).or_else(|| {
        if is_loopback(broker_url) {
            resolve(None, dir)
        } else {
            None
        }
    })
}

/// Is `broker_url` a broker on this machine (loopback host)?
pub fn is_loopback(broker_url: &str) -> bool {
    let authority = broker_url
        .split("://")
        .nth(1)
        .unwrap_or(broker_url)
        .split('/')
        .next()
        .unwrap_or("");
    let host = match authority.strip_prefix('[') {
        Some(v6) => v6.split(']').next().unwrap_or(""),
        None => authority.split(':').next().unwrap_or(""),
    };
    host.starts_with("127.") || host == "localhost" || host == "::1"
}

/// A key FILE for a trusted local client that reads its key from a path (the
/// La Forge host runner's `FORGE_ATELIER_ZC_WORKER_KEY_FILE`), holding the
/// key this node's broker runs with: the saved key file itself, or, when an
/// explicit `ZAKURO_WORKER_KEY` is set, a `0600` copy of it beside it (the
/// saved file is never overwritten). A file, not an env var, so the key is
/// not inherited by the job processes such a client starts.
pub fn file_for_local_client(env: Option<String>, dir: &Path) -> std::io::Result<PathBuf> {
    match clean(env) {
        Some(k) => {
            let p = dir.join("worker.key.env");
            let _ = std::fs::remove_file(&p);
            std::fs::create_dir_all(dir)?;
            create_private(&p, &k)?;
            Ok(p)
        }
        None => {
            ensure(None, dir)?;
            Ok(path(dir))
        }
    }
}

/// [`for_broker`] from the process environment and the zakuro home.
pub fn for_broker_default(broker_url: &str) -> Option<String> {
    for_broker(
        broker_url,
        std::env::var(ENV).ok(),
        crate::credentials::dir().as_deref(),
    )
}

/// The key a broker this node starts should run with: the env value when set,
/// else the saved one, creating it (random, `0600`) on first use.
pub fn ensure(env: Option<String>, dir: &Path) -> std::io::Result<String> {
    if let Some(k) = clean(env) {
        return Ok(k);
    }
    if let Some(k) = clean(std::fs::read_to_string(path(dir)).ok()) {
        return Ok(k);
    }
    std::fs::create_dir_all(dir)?;
    let mut raw = [0u8; 32];
    ring::rand::SecureRandom::fill(&ring::rand::SystemRandom::new(), &mut raw)
        .map_err(|_| std::io::Error::other("no random bytes for the worker key"))?;
    let key = hex::encode(raw);
    match create_private(&path(dir), &key) {
        Ok(()) => Ok(key),
        // The agent and `zc up` can start together: the loser reads the winner's.
        Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {
            clean(std::fs::read_to_string(path(dir)).ok()).ok_or(e)
        }
        Err(e) => Err(e),
    }
}

/// Create `path` holding `key`, mode `0600` from the first byte, failing with
/// `AlreadyExists` when it exists. Written whole to a private temp file and
/// then linked into place, so a concurrent reader never sees it half written.
fn create_private(path: &Path, key: &str) -> std::io::Result<()> {
    let tmp = path.with_extension(format!("tmp-{}-{}", std::process::id(), &key[..8]));
    let mut opts = std::fs::OpenOptions::new();
    opts.write(true).create_new(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        opts.mode(0o600);
    }
    let mut f = opts.open(&tmp)?;
    let written = f.write_all(key.as_bytes()).and_then(|_| f.sync_all());
    drop(f);
    let linked = written.and_then(|_| std::fs::hard_link(&tmp, path));
    let _ = std::fs::remove_file(&tmp);
    match linked {
        // A filesystem without hard links: rename is not exclusive, but the
        // reader still never sees a partial file.
        Err(e) if e.kind() == std::io::ErrorKind::Unsupported => {
            std::fs::write(path, key).and_then(|_| restrict(path))
        }
        other => other,
    }
}

#[cfg(unix)]
fn restrict(path: &Path) -> std::io::Result<()> {
    use std::os::unix::fs::PermissionsExt;
    std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600))
}

#[cfg(not(unix))]
fn restrict(_path: &Path) -> std::io::Result<()> {
    Ok(())
}

#[cfg(test)]
mod tests {

    #[test]
    fn a_local_client_gets_a_file_holding_the_brokers_key() {
        let dir = std::env::temp_dir().join(format!(
            "zc-wk-client-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        // No explicit key: the saved file, created on first use.
        let p = file_for_local_client(None, &dir).unwrap();
        let saved = std::fs::read_to_string(&p).unwrap();
        assert_eq!(Some(saved.trim().to_string()), resolve(None, Some(&dir)));
        // An explicit key: a private copy beside it; the saved file is untouched.
        let q = file_for_local_client(Some("explicit-key".into()), &dir).unwrap();
        assert_ne!(p, q);
        assert_eq!(std::fs::read_to_string(&q).unwrap().trim(), "explicit-key");
        assert_eq!(std::fs::read_to_string(&p).unwrap(), saved);
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            assert_eq!(
                std::fs::metadata(&q).unwrap().permissions().mode() & 0o777,
                0o600
            );
        }
        assert!(is_loopback("http://127.0.0.1:9000") && is_loopback("http://localhost:9001/x"));
        assert!(is_loopback("http://[::1]:9000") && !is_loopback("http://10.13.13.4:9000"));
        let _ = std::fs::remove_dir_all(&dir);
    }
    use super::*;

    fn tmp(tag: &str) -> PathBuf {
        let d = std::env::temp_dir().join(format!("zc-wk-{}-{tag}", std::process::id()));
        let _ = std::fs::remove_dir_all(&d);
        d
    }

    #[test]
    fn a_key_is_made_once_kept_private_and_reused() {
        let d = tmp("make");
        let k1 = ensure(None, &d).unwrap();
        assert_eq!(k1.len(), 64);
        assert!(k1.bytes().all(|b| b.is_ascii_hexdigit()));
        assert_eq!(ensure(None, &d).unwrap(), k1, "stable across restarts");
        assert_eq!(resolve(None, Some(&d)).as_deref(), Some(k1.as_str()));
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mode = std::fs::metadata(path(&d)).unwrap().permissions().mode();
            assert_eq!(mode & 0o777, 0o600);
        }
        let _ = std::fs::remove_dir_all(&d);
    }

    #[test]
    fn an_explicit_key_wins_and_writes_nothing() {
        let d = tmp("env");
        assert_eq!(ensure(Some(" mine ".into()), &d).unwrap(), "mine");
        assert!(!d.exists(), "nothing written when the env sets it");
        assert_eq!(
            resolve(Some("mine".into()), Some(&d)).as_deref(),
            Some("mine")
        );
        // a blank env value is no key
        assert_eq!(resolve(Some("  ".into()), Some(&d)), None);
        let k = ensure(Some("".into()), &d).unwrap();
        assert_eq!(k.len(), 64, "blank env falls through to a generated key");
        let _ = std::fs::remove_dir_all(&d);
    }

    #[test]
    fn the_generated_key_goes_only_to_a_loopback_broker() {
        let d = tmp("scope");
        let k = ensure(None, &d).unwrap();
        for url in [
            "http://127.0.0.1:9000",
            "http://localhost:9000/",
            "http://[::1]:9000",
        ] {
            assert_eq!(
                for_broker(url, None, Some(&d)).as_deref(),
                Some(k.as_str()),
                "{url}"
            );
        }
        for url in ["http://10.13.13.5:9000", "https://broker.example.com"] {
            assert_eq!(for_broker(url, None, Some(&d)), None, "{url}");
            // an explicit env key is the caller's choice: sent anywhere
            assert_eq!(
                for_broker(url, Some("mine".into()), Some(&d)).as_deref(),
                Some("mine")
            );
        }
        let _ = std::fs::remove_dir_all(&d);
    }

    #[test]
    fn a_client_with_no_key_anywhere_sends_none() {
        let d = tmp("none");
        assert_eq!(resolve(None, Some(&d)), None);
        assert_eq!(resolve(None, None), None);
    }

    #[test]
    fn two_starters_agree_on_one_key() {
        let d = tmp("race");
        let handles: Vec<_> = (0..8)
            .map(|_| {
                let d = d.clone();
                std::thread::spawn(move || ensure(None, &d).unwrap())
            })
            .collect();
        let keys: std::collections::HashSet<String> =
            handles.into_iter().map(|h| h.join().unwrap()).collect();
        assert_eq!(keys.len(), 1, "{keys:?}");
        let _ = std::fs::remove_dir_all(&d);
    }
}