| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 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 | |
|
| 40 | |
|
| 41 | | pub const RUNNER_TOML: &str = "runner.toml"; |
| 42 | |
|
| 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>, |
| 49 | | |
| 50 | | #[serde(default)] |
| 51 | | pub max_jobs: Option<usize>, |
| 52 | | |
| 53 | | #[serde(default)] |
| 54 | | pub labels: Vec<String>, |
| 55 | | |
| 56 | | #[serde(default)] |
| 57 | | pub memory_mib: Option<u32>, |
| 58 | | #[serde(default)] |
| 59 | | pub cpus: Option<u32>, |
| 60 | | |
| 61 | | |
| 62 | | #[serde(default)] |
| 63 | | pub swarm: Option<SwarmConfig>, |
| 64 | | |
| 65 | | #[serde(default)] |
| 66 | | pub cache_gb: Option<u64>, |
| 67 | | |
| 68 | | |
| 69 | | |
| 70 | | #[serde(default)] |
| 71 | | pub cache_prune_percent: Option<u8>, |
| 72 | | |
| 73 | | |
| 74 | | |
| 75 | | #[serde(default)] |
| 76 | | pub egress: bool, |
| 77 | | |
| 78 | | |
| 79 | | |
| 80 | | |
| 81 | | #[serde(default)] |
| 82 | | pub keyless: bool, |
| 83 | | |
| 84 | | |
| 85 | | #[serde(default)] |
| 86 | | pub metrics: Option<String>, |
| 87 | | |
| 88 | | #[serde(default)] |
| 89 | | pub metrics_token: Option<String>, |
| 90 | | |
| 91 | | #[serde(default)] |
| 92 | | pub update: Option<UpdateConfig>, |
| 93 | | } |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | | #[derive(Clone, Debug, Deserialize)] |
| 98 | | pub struct UpdateConfig { |
| 99 | | |
| 100 | | #[serde(default = "yes")] |
| 101 | | pub allow: bool, |
| 102 | | } |
| 103 | |
|
| 104 | 0× | fn yes() -> bool { |
| 105 | 0× | true |
| 106 | 0× | } |
| 107 | |
|
| 108 | | impl RunnerConfig { |
| 109 | | |
| 110 | | |
| 111 | 0× | pub fn offered_labels(&self) -> Vec<String> { |
| 112 | 0× | let mut l = self.labels.clone(); |
| 113 | 0× | if self.egress && !l.iter().any(|x| x == flow_core::EGRESS_LABEL) { |
| 114 | 0× | l.push(flow_core::EGRESS_LABEL.to_string()); |
| 115 | 0× | } |
| 116 | 0× | l |
| 117 | 0× | } |
| 118 | | } |
| 119 | |
|
| 120 | |
|
| 121 | | #[derive(Clone, Debug, Deserialize)] |
| 122 | | pub struct SwarmConfig { |
| 123 | | |
| 124 | | pub listen: String, |
| 125 | | |
| 126 | | #[serde(default)] |
| 127 | | pub advertise: Option<String>, |
| 128 | | |
| 129 | | #[serde(default)] |
| 130 | | pub site: String, |
| 131 | | |
| 132 | | #[serde(default)] |
| 133 | | pub upload_mib_s: Option<u32>, |
| 134 | | } |
| 135 | |
|
| 136 | | #[derive(Clone, Debug, Default, Deserialize)] |
| 137 | | pub struct Config { |
| 138 | | pub microvm: Option<MicrovmConfig>, |
| 139 | | pub runner: Option<RunnerConfig>, |
| 140 | | } |
| 141 | |
|
| 142 | | pub struct Home { |
| 143 | | pub root: PathBuf, |
| 144 | | pub config: Config, |
| 145 | | |
| 146 | | verified: Mutex<HashMap<PathBuf, (u64, std::time::SystemTime)>>, |
| 147 | | } |
| 148 | |
|
| 149 | | impl Home { |
| 150 | 18× | pub fn locate(explicit: Option<&Path>) -> Result<Home> { |
| 151 | 18× | let root = match explicit { |
| 152 | 18× | Some(p) => p.to_path_buf(), |
| 153 | 0× | None => match std::env::var_os("FLOW_HOME") { |
| 154 | 0× | Some(p) => PathBuf::from(p), |
| 155 | 0× | None => PathBuf::from(std::env::var_os("HOME").context("HOME is not set")?).join(".local/share/flow"), |
| 156 | | }, |
| 157 | | }; |
| 158 | 18× | let file = root.join("flow.toml"); |
| 159 | 18× | let mut config: Config = if file.is_file() { |
| 160 | 0× | let text = std::fs::read_to_string(&file).with_context(|| format!("read {}", file.display()))?; |
| 161 | 0× | toml::from_str(&text).with_context(|| format!("parse {}", file.display()))? |
| 162 | | } else { |
| 163 | 18× | Config::default() |
| 164 | | }; |
| 165 | | |
| 166 | | |
| 167 | 18× | let registered = root.join(RUNNER_TOML); |
| 168 | 18× | if config.runner.is_none() && registered.is_file() { |
| 169 | 9× | let text = std::fs::read_to_string(®istered).with_context(|| format!("read {}", registered.display()))?; |
| 170 | 9× | config.runner = Some(toml::from_str(&text).with_context(|| format!("parse {}", registered.display()))?); |
| 171 | 9× | } |
| 172 | 54× | for d in ["cas", "work", "repos"] { |
| 173 | 54× | std::fs::create_dir_all(root.join(d)).with_context(|| format!("create {}", root.join(d).display()))?; |
| 174 | | } |
| 175 | 18× | Ok(Home { root, config, verified: Mutex::new(HashMap::new()) }) |
| 176 | 18× | } |
| 177 | |
|
| 178 | 0× | pub fn resolve(&self, p: &Path) -> PathBuf { |
| 179 | 0× | if p.is_absolute() { |
| 180 | 0× | p.to_path_buf() |
| 181 | | } else { |
| 182 | 0× | self.root.join(p) |
| 183 | | } |
| 184 | 0× | } |
| 185 | |
|
| 186 | | |
| 187 | 35× | pub fn rootfs(&self, hash: &str) -> PathBuf { |
| 188 | 35× | self.root.join("cas").join(format!("{hash}.ext4")) |
| 189 | 35× | } |
| 190 | |
|
| 191 | | |
| 192 | | |
| 193 | 14× | pub fn image_manifest(&self, hash: &str) -> PathBuf { |
| 194 | 14× | self.root.join("cas").join(format!("{hash}.json")) |
| 195 | 14× | } |
| 196 | |
|
| 197 | | |
| 198 | | |
| 199 | | |
| 200 | | |
| 201 | | |
| 202 | 9× | pub fn image_arch(&self, hash: &str) -> Option<String> { |
| 203 | 9× | let path = self.image_manifest(hash); |
| 204 | 9× | let mut v: serde_json::Value = |
| 205 | 9× | std::fs::read_to_string(&path).ok().and_then(|t| serde_json::from_str(&t).ok()).unwrap_or_else(|| serde_json::json!({})); |
| 206 | 9× | if let Some(a) = v["arch"].as_str() { |
| 207 | 3× | return Some(a.to_string()); |
| 208 | 6× | } |
| 209 | 6× | let image = self.rootfs(hash); |
| 210 | 6× | if v.get("arch_read_ms").is_some() || !image.is_file() { |
| 211 | 1× | return None; |
| 212 | 5× | } |
| 213 | 5× | let arch = jet::rootfs::arch_of_file(&image); |
| 214 | 5× | let o = v.as_object_mut()?; |
| 215 | 5× | if let Some(a) = arch { |
| 216 | 1× | o.insert("arch".into(), a.into()); |
| 217 | 4× | } |
| 218 | 5× | let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or(0); |
| 219 | 5× | o.insert("arch_read_ms".into(), now.into()); |
| 220 | 5× | let tmp = path.with_extension("json.part"); |
| 221 | 5× | if std::fs::write(&tmp, v.to_string()).is_ok() { |
| 222 | 5× | let _ = std::fs::rename(&tmp, &path); |
| 223 | 5× | } |
| 224 | 5× | arch.map(String::from) |
| 225 | 9× | } |
| 226 | |
|
| 227 | | |
| 228 | 1× | pub fn image_archs(&self) -> std::collections::BTreeMap<String, String> { |
| 229 | 1× | self.images().into_iter().filter_map(|h| self.image_arch(&h).map(|a| (h, a))).collect() |
| 230 | 1× | } |
| 231 | |
|
| 232 | | |
| 233 | 10× | pub fn images(&self) -> Vec<String> { |
| 234 | 10× | let Ok(dir) = std::fs::read_dir(self.root.join("cas")) else { |
| 235 | 0× | return Vec::new(); |
| 236 | | }; |
| 237 | 10× | let mut out: Vec<String> = dir |
| 238 | 10× | .filter_map(|e| { |
| 239 | 5× | let name = e.ok()?.file_name().into_string().ok()?; |
| 240 | 5× | let hash = name.strip_suffix(".ext4")?; |
| 241 | 256× | (hash.len() == 64 && hash.chars().all(|c| c.is_ascii_hexdigit())).then(|| hash.to_string()) |
| 242 | 5× | }) |
| 243 | 10× | .collect(); |
| 244 | 10× | out.sort(); |
| 245 | 10× | out |
| 246 | 10× | } |
| 247 | |
|
| 248 | | |
| 249 | | |
| 250 | 4× | pub fn verified(&self, path: &Path, blake3_hex: &str) -> Result<PathBuf> { |
| 251 | 4× | let meta = std::fs::metadata(path).with_context(|| format!("{} is missing", path.display()))?; |
| 252 | 4× | let stamp = (meta.len(), meta.modified()?); |
| 253 | 4× | if self.verified.lock().unwrap().get(path) == Some(&stamp) { |
| 254 | 1× | return Ok(path.to_path_buf()); |
| 255 | 3× | } |
| 256 | 3× | let mut got = hash_file(path)?; |
| 257 | 3× | if got != blake3_hex { |
| 258 | | |
| 259 | | |
| 260 | | |
| 261 | | |
| 262 | 1× | drop_cached(path); |
| 263 | 1× | let again = hash_file(path)?; |
| 264 | 1× | if again == blake3_hex { |
| 265 | 0× | eprintln!( |
| 266 | 0× | "warning: {} hashed {got} from the page cache but {again} from the disk: the cached copy was bad (check this machine's memory)", |
| 267 | 0× | path.display() |
| 268 | 0× | ); |
| 269 | 1× | } |
| 270 | 1× | got = again; |
| 271 | 2× | } |
| 272 | 3× | if got != blake3_hex { |
| 273 | 1× | bail!("{} has BLAKE3 {got}, but {blake3_hex} is pinned", path.display()); |
| 274 | 2× | } |
| 275 | 2× | self.verified.lock().unwrap().insert(path.to_path_buf(), stamp); |
| 276 | 2× | Ok(path.to_path_buf()) |
| 277 | 4× | } |
| 278 | | } |
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | 1× | fn drop_cached(path: &Path) { |
| 283 | | #[cfg(target_os = "linux")] |
| 284 | 1× | if let Ok(f) = std::fs::File::open(path) { |
| 285 | | use std::os::fd::AsRawFd; |
| 286 | | |
| 287 | 1× | unsafe { |
| 288 | 1× | libc::posix_fadvise(f.as_raw_fd(), 0, 0, libc::POSIX_FADV_DONTNEED); |
| 289 | 1× | } |
| 290 | 0× | } |
| 291 | | #[cfg(not(target_os = "linux"))] |
| 292 | | let _ = path; |
| 293 | 1× | } |
| 294 | |
|
| 295 | 21× | pub fn hash_file(path: &Path) -> Result<String> { |
| 296 | 21× | let mut h = blake3::Hasher::new(); |
| 297 | 21× | h.update_reader(std::fs::File::open(path).with_context(|| format!("open {}", path.display()))?)?; |
| 298 | 21× | Ok(h.finalize().to_hex().to_string()) |
| 299 | 21× | } |
| 300 | |
|
| 301 | |
|
| 302 | | pub struct RunnerKey { |
| 303 | | pub identity: oxy_core::identity::Identity, |
| 304 | | pub secret: oxy_core::identity::SecretKey, |
| 305 | | } |
| 306 | |
|
| 307 | | impl Home { |
| 308 | | |
| 309 | | |
| 310 | 4× | pub fn runner_key(&self, name: &str) -> Result<RunnerKey> { |
| 311 | | use oxy_core::identity::{Identity, SecretKey}; |
| 312 | 4× | let key_path = self.root.join("runner.key"); |
| 313 | 4× | let id_path = self.root.join("runner.identity"); |
| 314 | 4× | if key_path.exists() { |
| 315 | 2× | let (key, secret) = SecretKey::decode(&std::fs::read(&key_path)?).context("runner.key")?; |
| 316 | 2× | let identity = Identity::decode(&std::fs::read(&id_path)?).context("runner.identity")?; |
| 317 | 2× | if identity.hash() != key { |
| 318 | 0× | bail!("runner.identity does not belong to runner.key"); |
| 319 | 2× | } |
| 320 | 2× | return Ok(RunnerKey { identity, secret }); |
| 321 | 2× | } |
| 322 | 2× | let secret = SecretKey::generate(); |
| 323 | 2× | let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or(0); |
| 324 | 2× | let identity = secret.identity(&format!("{name} (OxygenFlow runner)"), "", now); |
| 325 | 2× | std::fs::write(&id_path, identity.encode())?; |
| 326 | 2× | let tmp = self.root.join("runner.key.tmp"); |
| 327 | | { |
| 328 | | use std::io::Write; |
| 329 | 2× | let mut f = crate::portable::private(std::fs::OpenOptions::new().write(true).create(true).truncate(true)).open(&tmp)?; |
| 330 | 2× | f.write_all(&secret.encode(&identity.hash()))?; |
| 331 | 2× | f.sync_all()?; |
| 332 | | } |
| 333 | 2× | std::fs::rename(&tmp, &key_path)?; |
| 334 | 2× | Ok(RunnerKey { identity, secret }) |
| 335 | 4× | } |
| 336 | | } |
| 337 | |
|
| 338 | | #[cfg(test)] |
| 339 | | mod tests { |
| 340 | | use super::*; |
| 341 | |
|
| 342 | | #[test] |
| 343 | 1× | fn the_runner_key_is_made_once_and_kept_private() { |
| 344 | | use std::os::unix::fs::PermissionsExt; |
| 345 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 346 | 1× | let home = Home::locate(Some(dir.path())).unwrap(); |
| 347 | 1× | let a = home.runner_key("earth-1").unwrap(); |
| 348 | 1× | assert_eq!(a.identity.name, "earth-1 (OxygenFlow runner)"); |
| 349 | 1× | let mode = std::fs::metadata(dir.path().join("runner.key")).unwrap().permissions().mode(); |
| 350 | 1× | assert_eq!(mode & 0o777, 0o600); |
| 351 | 1× | let b = home.runner_key("renamed").unwrap(); |
| 352 | 1× | assert_eq!(a.identity, b.identity, "the same key after a restart"); |
| 353 | 1× | let sig = b.secret.sign(oxy_core::identity::Domain::Attest, &b.identity.hash(), &oxy_core::hash::ObjectRef([1; 32])); |
| 354 | 1× | oxy_core::identity::verify(&a.identity, &sig, oxy_core::identity::Domain::Attest, &oxy_core::hash::ObjectRef([1; 32])).unwrap(); |
| 355 | 1× | } |
| 356 | | } |