OXYGENHUB
# c58efc6a74db oxygenflow / crates / flow / src / home.rs
8.2 KiB · Rust · last changed A runner's claim lists the rootfs images it holds (refs andymccall/oxygenhub#73) 1 month ago source blame history raw
e0918b88 Andy McCall 1
//! `$FLOW_HOME` (default `~/.local/share/flow`): the pinned parts of each
2
//! isolation tier, rootfs images by hash, scratch space and the runner's
3
//! configuration, all described by `flow.toml`.
4
//!
5
//! ```toml
6
//! [microvm]
7
//! firecracker = { path = "bin/firecracker", blake3 = "…" }
8
//! kernel = { path = "kernel/vmlinux", blake3 = "…" }
9
//! init = { path = "bin/flow-init", blake3 = "…" }
10
//!
11
//! [runner]                       # only for `flow runner`
12
//! hub = "https://hub.example:7343"
13
//! token = "…"                    # a runner token from the hub's [flow] runner_tokens
14
//! ca = "/path/to/hub-ca.pem"     # when the hub's certificate is self-signed
15
//! name = "earth-1"
16
//! ```
47c1d64d Andy McCall ↶ 17
//!
18
//! A runner signs its receipts with a composite key kept beside `flow.toml`
19
//! (`runner.key`, `runner.identity`), made on first start.
e0918b88 Andy McCall 20
21
use anyhow::{bail, Context, Result};
22
use serde::Deserialize;
23
use std::collections::HashMap;
24
use std::path::{Path, PathBuf};
25
use std::sync::Mutex;
26
27
#[derive(Clone, Debug, Deserialize)]
28
pub struct Pinned {
29
    pub path: PathBuf,
30
    pub blake3: String,
31
}
32
33
#[derive(Clone, Debug, Deserialize)]
34
pub struct MicrovmConfig {
35
    pub firecracker: Pinned,
36
    pub kernel: Pinned,
37
    pub init: Pinned,
38
}
39
f98a8f78 Andy McCall ↶ 40
/// Where `flow runner register` keeps the runner's hub and credential.
41
pub const RUNNER_TOML: &str = "runner.toml";
42
e0918b88 Andy McCall 43
#[derive(Clone, Debug, Deserialize)]
44
pub struct RunnerConfig {
45
    pub hub: String,
46
    pub token: String,
47
    pub ca: Option<PathBuf>,
48
    pub name: Option<String>,
bb643c25 Andy McCall ↶ 49
    /// Jobs run at once (default 1).
50
    #[serde(default)]
51
    pub max_jobs: Option<usize>,
f98a8f78 Andy McCall ↶ 52
    /// Labels the runner offers, e.g. `gpu`, `big-disk`.
53
    #[serde(default)]
54
    pub labels: Vec<String>,
bb643c25 Andy McCall ↶ 55
    /// Memory (MiB) and vCPUs the jobs running at once may use together.
56
    #[serde(default)]
57
    pub memory_mib: Option<u32>,
58
    #[serde(default)]
59
    pub cpus: Option<u32>,
e0918b88 Andy McCall 60
}
61
62
#[derive(Clone, Debug, Default, Deserialize)]
63
pub struct Config {
64
    pub microvm: Option<MicrovmConfig>,
65
    pub runner: Option<RunnerConfig>,
66
}
67
68
pub struct Home {
69
    pub root: PathBuf,
70
    pub config: Config,
71
    /// Files already verified, by path, with the (len, mtime) they had then.
72
    verified: Mutex<HashMap<PathBuf, (u64, std::time::SystemTime)>>,
73
}
74
75
impl Home {
76
    pub fn locate(explicit: Option<&Path>) -> Result<Home> {
77
        let root = match explicit {
78
            Some(p) => p.to_path_buf(),
79
            None => match std::env::var_os("FLOW_HOME") {
80
                Some(p) => PathBuf::from(p),
81
                None => PathBuf::from(std::env::var_os("HOME").context("HOME is not set")?).join(".local/share/flow"),
82
            },
83
        };
84
        let file = root.join("flow.toml");
f98a8f78 Andy McCall ↶ 85
        let mut config: Config = if file.is_file() {
e0918b88 Andy McCall 86
            let text = std::fs::read_to_string(&file).with_context(|| format!("read {}", file.display()))?;
87
            toml::from_str(&text).with_context(|| format!("parse {}", file.display()))?
88
        } else {
89
            Config::default()
90
        };
f98a8f78 Andy McCall ↶ 91
        // `flow runner register` writes the runner's hub and credential to
92
        // runner.toml, which stands in for a [runner] section flow.toml lacks.
93
        let registered = root.join(RUNNER_TOML);
94
        if config.runner.is_none() && registered.is_file() {
95
            let text = std::fs::read_to_string(&registered).with_context(|| format!("read {}", registered.display()))?;
96
            config.runner = Some(toml::from_str(&text).with_context(|| format!("parse {}", registered.display()))?);
97
        }
e0918b88 Andy McCall 98
        for d in ["cas", "work", "repos"] {
99
            std::fs::create_dir_all(root.join(d)).with_context(|| format!("create {}", root.join(d).display()))?;
100
        }
101
        Ok(Home { root, config, verified: Mutex::new(HashMap::new()) })
102
    }
103
104
    pub fn resolve(&self, p: &Path) -> PathBuf {
105
        if p.is_absolute() {
106
            p.to_path_buf()
107
        } else {
108
            self.root.join(p)
109
        }
110
    }
111
112
    /// Where a rootfs image with this hash lives.
113
    pub fn rootfs(&self, hash: &str) -> PathBuf {
114
        self.root.join("cas").join(format!("{hash}.ext4"))
115
    }
116
8c0a4e3a Andy McCall ↶ 117
    /// The rootfs images this home holds: the hashes of `cas/*.ext4`.
118
    pub fn images(&self) -> Vec<String> {
119
        let Ok(dir) = std::fs::read_dir(self.root.join("cas")) else {
120
            return Vec::new();
121
        };
122
        let mut out: Vec<String> = dir
123
            .filter_map(|e| {
124
                let name = e.ok()?.file_name().into_string().ok()?;
125
                let hash = name.strip_suffix(".ext4")?;
126
                (hash.len() == 64 && hash.chars().all(|c| c.is_ascii_hexdigit())).then(|| hash.to_string())
127
            })
128
            .collect();
129
        out.sort();
130
        out
131
    }
132
e0918b88 Andy McCall 133
    /// A pinned file, checked against its BLAKE3 before use (again only
134
    /// when its size or modification time changed since).
135
    pub fn verified(&self, path: &Path, blake3_hex: &str) -> Result<PathBuf> {
136
        let meta = std::fs::metadata(path).with_context(|| format!("{} is missing", path.display()))?;
137
        let stamp = (meta.len(), meta.modified()?);
138
        if self.verified.lock().unwrap().get(path) == Some(&stamp) {
139
            return Ok(path.to_path_buf());
140
        }
141
        let got = hash_file(path)?;
142
        if got != blake3_hex {
143
            bail!("{} has BLAKE3 {got}, but {blake3_hex} is pinned", path.display());
144
        }
145
        self.verified.lock().unwrap().insert(path.to_path_buf(), stamp);
146
        Ok(path.to_path_buf())
147
    }
148
}
149
150
pub fn hash_file(path: &Path) -> Result<String> {
151
    let mut h = blake3::Hasher::new();
152
    h.update_reader(std::fs::File::open(path).with_context(|| format!("open {}", path.display()))?)?;
153
    Ok(h.finalize().to_hex().to_string())
47c1d64d Andy McCall ↶ 154
}
155
156
/// The key a runner signs receipts with (Oxygen spec §14.6).
157
pub struct RunnerKey {
158
    pub identity: oxy_core::identity::Identity,
159
    pub secret: oxy_core::identity::SecretKey,
160
}
161
162
impl Home {
163
    /// The runner's key, made on first use as `runner.key` (owner-only)
164
    /// and `runner.identity`, named `name`.
165
    pub fn runner_key(&self, name: &str) -> Result<RunnerKey> {
166
        use oxy_core::identity::{Identity, SecretKey};
167
        let key_path = self.root.join("runner.key");
168
        let id_path = self.root.join("runner.identity");
169
        if key_path.exists() {
170
            let (key, secret) = SecretKey::decode(&std::fs::read(&key_path)?).context("runner.key")?;
171
            let identity = Identity::decode(&std::fs::read(&id_path)?).context("runner.identity")?;
172
            if identity.hash() != key {
173
                bail!("runner.identity does not belong to runner.key");
174
            }
175
            return Ok(RunnerKey { identity, secret });
176
        }
177
        let secret = SecretKey::generate();
178
        let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or(0);
179
        let identity = secret.identity(&format!("{name} (OxygenFlow runner)"), "", now);
180
        std::fs::write(&id_path, identity.encode())?;
181
        let tmp = self.root.join("runner.key.tmp");
182
        {
183
            use std::io::Write;
184
            use std::os::unix::fs::OpenOptionsExt;
185
            let mut f = std::fs::OpenOptions::new().write(true).create(true).truncate(true).mode(0o600).open(&tmp)?;
186
            f.write_all(&secret.encode(&identity.hash()))?;
187
            f.sync_all()?;
188
        }
189
        std::fs::rename(&tmp, &key_path)?;
190
        Ok(RunnerKey { identity, secret })
191
    }
192
}
193
194
#[cfg(test)]
195
mod tests {
196
    use super::*;
197
198
    #[test]
199
    fn the_runner_key_is_made_once_and_kept_private() {
200
        use std::os::unix::fs::PermissionsExt;
201
        let dir = tempfile::tempdir().unwrap();
202
        let home = Home::locate(Some(dir.path())).unwrap();
203
        let a = home.runner_key("earth-1").unwrap();
204
        assert_eq!(a.identity.name, "earth-1 (OxygenFlow runner)");
205
        let mode = std::fs::metadata(dir.path().join("runner.key")).unwrap().permissions().mode();
206
        assert_eq!(mode & 0o777, 0o600);
207
        let b = home.runner_key("renamed").unwrap();
208
        assert_eq!(a.identity, b.identity, "the same key after a restart");
209
        let sig = b.secret.sign(oxy_core::identity::Domain::Attest, &b.identity.hash(), &oxy_core::hash::ObjectRef([1; 32]));
210
        oxy_core::identity::verify(&a.identity, &sig, oxy_core::identity::Domain::Attest, &oxy_core::hash::ObjectRef([1; 32])).unwrap();
211
    }
e0918b88 Andy McCall 212
}