| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
use anyhow::{Context, Result}; |
| 6 |
use serde::Deserialize; |
| 7 |
use std::collections::HashMap; |
| 8 |
use std::path::PathBuf; |
| 9 |
|
| 10 |
#[derive(Debug, Clone, Deserialize)] |
| 11 |
pub struct Config { |
| 12 |
pub listen: String, |
| 13 |
pub db_path: PathBuf, |
| 14 |
pub topology_path: PathBuf, |
| 15 |
|
| 16 |
|
| 17 |
pub secrets_root: PathBuf, |
| 18 |
|
| 19 |
|
| 20 |
pub dist_root: PathBuf, |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#[serde(default)] |
| 26 |
pub archive: Option<Archive>, |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#[serde(default)] |
| 32 |
pub handoff: HashMap<String, Handoff>, |
| 33 |
|
| 34 |
|
| 35 |
#[serde(default = "default_logs_root")] |
| 36 |
pub logs_root: PathBuf, |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#[serde(default)] |
| 42 |
pub step_timeout_secs: Option<u64>, |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
#[serde(default)] |
| 48 |
pub notarize_backoff_secs: Option<u64>, |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
#[serde(default = "default_true")] |
| 55 |
pub pin_release_sha: bool, |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
#[serde(default = "default_deploy_installer")] |
| 70 |
pub deploy_installer: String, |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
#[derive(Debug, Clone, Deserialize)] |
| 85 |
pub struct Archive { |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
pub host: String, |
| 93 |
|
| 94 |
|
| 95 |
pub root: PathBuf, |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
#[derive(Debug, Clone, Deserialize)] |
| 118 |
pub struct Handoff { |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
pub host: String, |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
pub staging_root: PathBuf, |
| 132 |
|
| 133 |
|
| 134 |
pub url: String, |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
#[serde(default)] |
| 139 |
pub sando_app: Option<String>, |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
#[serde(default)] |
| 146 |
pub token_file: Option<PathBuf>, |
| 147 |
} |
| 148 |
|
| 149 |
fn default_true() -> bool { |
| 150 |
true |
| 151 |
} |
| 152 |
|
| 153 |
fn default_deploy_installer() -> String { |
| 154 |
"sudo /usr/local/lib/bento/install-service.sh".into() |
| 155 |
} |
| 156 |
|
| 157 |
fn default_logs_root() -> PathBuf { |
| 158 |
PathBuf::from("/srv/bento/logs") |
| 159 |
} |
| 160 |
|
| 161 |
impl Config { |
| 162 |
pub fn load() -> Result<Self> { |
| 163 |
let path = std::env::var("BENTO_CONFIG").unwrap_or_else(|_| "bento-daemon.toml".into()); |
| 164 |
let raw = std::fs::read_to_string(&path) |
| 165 |
.with_context(|| format!("reading daemon config at {path}"))?; |
| 166 |
let cfg: Self = toml::from_str(&raw)?; |
| 167 |
cfg.validate() |
| 168 |
.with_context(|| format!("daemon config at {path}"))?; |
| 169 |
Ok(cfg) |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
fn validate(&self) -> Result<()> { |
| 178 |
if let Some(a) = &self.archive { |
| 179 |
anyhow::ensure!( |
| 180 |
!a.host.trim().is_empty(), |
| 181 |
"[archive] host is empty; set it to an ssh destination or `local`" |
| 182 |
); |
| 183 |
anyhow::ensure!( |
| 184 |
a.root.is_absolute() |
| 185 |
&& !a |
| 186 |
.root |
| 187 |
.components() |
| 188 |
.any(|c| c == std::path::Component::ParentDir), |
| 189 |
"[archive] root `{}` must be an absolute path with no `..`", |
| 190 |
a.root.display() |
| 191 |
); |
| 192 |
} |
| 193 |
for (app, h) in &self.handoff { |
| 194 |
anyhow::ensure!( |
| 195 |
!h.host.trim().is_empty(), |
| 196 |
"[handoff.{app}] host is empty; set it to an ssh destination or `local`" |
| 197 |
); |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
anyhow::ensure!( |
| 203 |
h.staging_root.is_absolute() |
| 204 |
&& !h |
| 205 |
.staging_root |
| 206 |
.components() |
| 207 |
.any(|c| c == std::path::Component::ParentDir), |
| 208 |
"[handoff.{app}] staging_root `{}` must be an absolute path with no `..`", |
| 209 |
h.staging_root.display() |
| 210 |
); |
| 211 |
anyhow::ensure!( |
| 212 |
h.url.starts_with("http://") || h.url.starts_with("https://"), |
| 213 |
"[handoff.{app}] url `{}` must be an http(s) URL for sandod", |
| 214 |
h.url |
| 215 |
); |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
if let Some(t) = &h.token_file { |
| 221 |
anyhow::ensure!( |
| 222 |
t.is_relative() |
| 223 |
&& !t.components().any(|c| c == std::path::Component::ParentDir), |
| 224 |
"[handoff.{app}] token_file `{}` must be a relative path under secrets_root", |
| 225 |
t.display() |
| 226 |
); |
| 227 |
} |
| 228 |
} |
| 229 |
Ok(()) |
| 230 |
} |
| 231 |
|
| 232 |
#[cfg(test)] |
| 233 |
pub fn for_tests(root: &std::path::Path) -> Self { |
| 234 |
Self { |
| 235 |
listen: "127.0.0.1:0".into(), |
| 236 |
db_path: root.join("bento.db"), |
| 237 |
topology_path: root.join("bento.toml"), |
| 238 |
secrets_root: root.join("secrets"), |
| 239 |
dist_root: root.join("dist"), |
| 240 |
|
| 241 |
|
| 242 |
archive: None, |
| 243 |
|
| 244 |
|
| 245 |
handoff: HashMap::new(), |
| 246 |
logs_root: root.join("logs"), |
| 247 |
step_timeout_secs: None, |
| 248 |
notarize_backoff_secs: None, |
| 249 |
|
| 250 |
|
| 251 |
pin_release_sha: false, |
| 252 |
deploy_installer: default_deploy_installer(), |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
#[cfg(test)] |
| 258 |
mod tests { |
| 259 |
use super::*; |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
fn parse(body: &str) -> Result<Config> { |
| 264 |
let base = r#" |
| 265 |
listen = "127.0.0.1:8765" |
| 266 |
db_path = "/var/lib/bento/bento.db" |
| 267 |
topology_path = "/etc/bento/bento.toml" |
| 268 |
secrets_root = "/home/max/Code/_private" |
| 269 |
dist_root = "/home/max/Dist" |
| 270 |
"#; |
| 271 |
let cfg: Config = toml::from_str(&format!("{base}{body}"))?; |
| 272 |
cfg.validate()?; |
| 273 |
Ok(cfg) |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
#[test] |
| 280 |
fn the_archive_table_is_optional() { |
| 281 |
assert!(parse("").unwrap().archive.is_none()); |
| 282 |
} |
| 283 |
|
| 284 |
#[test] |
| 285 |
fn an_archive_host_and_root_parse() { |
| 286 |
let cfg = |
| 287 |
parse("[archive]\nhost = \"astra\"\nroot = \"/var/lib/bento/artifacts\"\n").unwrap(); |
| 288 |
let a = cfg.archive.expect("archive configured"); |
| 289 |
assert_eq!(a.host, "astra"); |
| 290 |
assert_eq!(a.root, PathBuf::from("/var/lib/bento/artifacts")); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
#[test] |
| 298 |
fn a_relative_or_dot_dot_archive_root_is_rejected() { |
| 299 |
assert!(parse("[archive]\nhost = \"astra\"\nroot = \"artifacts\"\n").is_err()); |
| 300 |
assert!(parse("[archive]\nhost = \"astra\"\nroot = \"/var/../etc/bento\"\n").is_err()); |
| 301 |
} |
| 302 |
|
| 303 |
#[test] |
| 304 |
fn an_empty_archive_host_is_rejected() { |
| 305 |
assert!(parse("[archive]\nhost = \"\"\nroot = \"/var/lib/bento/artifacts\"\n").is_err()); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
#[test] |
| 311 |
fn handoff_is_per_app_and_absent_by_default() { |
| 312 |
assert!(parse("").unwrap().handoff.is_empty()); |
| 313 |
} |
| 314 |
|
| 315 |
#[test] |
| 316 |
fn a_handoff_parses_with_its_optional_fields_absent() { |
| 317 |
let cfg = parse( |
| 318 |
r#" |
| 319 |
[handoff.pom] |
| 320 |
host = "sando@fw13" |
| 321 |
staging_root = "/srv/sando/staging" |
| 322 |
url = "http://100.103.89.95:7766" |
| 323 |
"#, |
| 324 |
) |
| 325 |
.unwrap(); |
| 326 |
let h = cfg.handoff.get("pom").expect("pom hands off"); |
| 327 |
assert_eq!(h.host, "sando@fw13"); |
| 328 |
assert_eq!(h.staging_root, PathBuf::from("/srv/sando/staging")); |
| 329 |
|
| 330 |
|
| 331 |
assert!(h.sando_app.is_none()); |
| 332 |
assert!(h.token_file.is_none()); |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
#[test] |
| 339 |
fn a_relative_or_dot_dot_staging_root_is_rejected() { |
| 340 |
let with = |root: &str| { |
| 341 |
format!( |
| 342 |
"[handoff.pom]\nhost = \"fw13\"\nstaging_root = \"{root}\"\nurl = \"http://x:1\"\n" |
| 343 |
) |
| 344 |
}; |
| 345 |
assert!(parse(&with("staging")).is_err()); |
| 346 |
assert!(parse(&with("/srv/../etc/sando")).is_err()); |
| 347 |
assert!(parse(&with("/srv/sando/staging")).is_ok()); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
#[test] |
| 353 |
fn a_token_file_that_escapes_secrets_root_is_rejected() { |
| 354 |
let with = |token: &str| { |
| 355 |
format!( |
| 356 |
"[handoff.pom]\nhost = \"fw13\"\nstaging_root = \"/srv/sando/staging\"\n\ |
| 357 |
url = \"http://x:1\"\ntoken_file = \"{token}\"\n" |
| 358 |
) |
| 359 |
}; |
| 360 |
assert!(parse(&with("../../etc/shadow")).is_err()); |
| 361 |
assert!(parse(&with("/etc/shadow")).is_err()); |
| 362 |
assert!(parse(&with("sando/api-token")).is_ok()); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
#[test] |
| 368 |
fn a_handoff_url_must_name_a_scheme() { |
| 369 |
let with = |url: &str| { |
| 370 |
format!( |
| 371 |
"[handoff.pom]\nhost = \"fw13\"\nstaging_root = \"/srv/sando/staging\"\nurl = \"{url}\"\n" |
| 372 |
) |
| 373 |
}; |
| 374 |
assert!(parse(&with("100.103.89.95:7766")).is_err()); |
| 375 |
assert!(parse(&with("http://100.103.89.95:7766")).is_ok()); |
| 376 |
} |
| 377 |
} |
| 378 |
|