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