| 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 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
#[derive(Debug, Clone, Deserialize)] |
| 122 |
#[serde(deny_unknown_fields)] |
| 123 |
pub struct Handoff { |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
pub host: String, |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
pub staging_root: PathBuf, |
| 137 |
|
| 138 |
|
| 139 |
pub url: String, |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
#[serde(default)] |
| 144 |
pub sando_app: Option<String>, |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
#[serde(default)] |
| 158 |
pub token_env: Option<String>, |
| 159 |
} |
| 160 |
|
| 161 |
fn default_true() -> bool { |
| 162 |
true |
| 163 |
} |
| 164 |
|
| 165 |
fn default_deploy_installer() -> String { |
| 166 |
"sudo /usr/local/lib/bento/install-service.sh".into() |
| 167 |
} |
| 168 |
|
| 169 |
fn default_logs_root() -> PathBuf { |
| 170 |
PathBuf::from("/srv/bento/logs") |
| 171 |
} |
| 172 |
|
| 173 |
impl Config { |
| 174 |
pub fn load() -> Result<Self> { |
| 175 |
let path = std::env::var("BENTO_CONFIG").unwrap_or_else(|_| "bento-daemon.toml".into()); |
| 176 |
let raw = std::fs::read_to_string(&path) |
| 177 |
.with_context(|| format!("reading daemon config at {path}"))?; |
| 178 |
let cfg: Self = toml::from_str(&raw)?; |
| 179 |
cfg.validate() |
| 180 |
.with_context(|| format!("daemon config at {path}"))?; |
| 181 |
Ok(cfg) |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
fn validate(&self) -> Result<()> { |
| 190 |
if let Some(a) = &self.archive { |
| 191 |
anyhow::ensure!( |
| 192 |
!a.host.trim().is_empty(), |
| 193 |
"[archive] host is empty; set it to an ssh destination or `local`" |
| 194 |
); |
| 195 |
anyhow::ensure!( |
| 196 |
a.root.is_absolute() |
| 197 |
&& !a |
| 198 |
.root |
| 199 |
.components() |
| 200 |
.any(|c| c == std::path::Component::ParentDir), |
| 201 |
"[archive] root `{}` must be an absolute path with no `..`", |
| 202 |
a.root.display() |
| 203 |
); |
| 204 |
} |
| 205 |
for (app, h) in &self.handoff { |
| 206 |
anyhow::ensure!( |
| 207 |
!h.host.trim().is_empty(), |
| 208 |
"[handoff.{app}] host is empty; set it to an ssh destination or `local`" |
| 209 |
); |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
anyhow::ensure!( |
| 215 |
h.staging_root.is_absolute() |
| 216 |
&& !h |
| 217 |
.staging_root |
| 218 |
.components() |
| 219 |
.any(|c| c == std::path::Component::ParentDir), |
| 220 |
"[handoff.{app}] staging_root `{}` must be an absolute path with no `..`", |
| 221 |
h.staging_root.display() |
| 222 |
); |
| 223 |
anyhow::ensure!( |
| 224 |
h.url.starts_with("http://") || h.url.starts_with("https://"), |
| 225 |
"[handoff.{app}] url `{}` must be an http(s) URL for sandod", |
| 226 |
h.url |
| 227 |
); |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
if let Some(t) = &h.token_env { |
| 235 |
anyhow::ensure!( |
| 236 |
!t.trim().is_empty() |
| 237 |
&& t.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') |
| 238 |
&& !t.starts_with(|c: char| c.is_ascii_digit()), |
| 239 |
"[handoff.{app}] token_env `{t}` must name an environment variable \ |
| 240 |
(letters, digits and underscores), not hold the token or a path" |
| 241 |
); |
| 242 |
} |
| 243 |
} |
| 244 |
Ok(()) |
| 245 |
} |
| 246 |
|
| 247 |
#[cfg(test)] |
| 248 |
pub fn for_tests(root: &std::path::Path) -> Self { |
| 249 |
Self { |
| 250 |
listen: "127.0.0.1:0".into(), |
| 251 |
db_path: root.join("bento.db"), |
| 252 |
topology_path: root.join("bento.toml"), |
| 253 |
secrets_root: root.join("secrets"), |
| 254 |
dist_root: root.join("dist"), |
| 255 |
|
| 256 |
|
| 257 |
archive: None, |
| 258 |
|
| 259 |
|
| 260 |
handoff: HashMap::new(), |
| 261 |
logs_root: root.join("logs"), |
| 262 |
step_timeout_secs: None, |
| 263 |
notarize_backoff_secs: None, |
| 264 |
|
| 265 |
|
| 266 |
pin_release_sha: false, |
| 267 |
deploy_installer: default_deploy_installer(), |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
#[cfg(test)] |
| 273 |
mod tests { |
| 274 |
use super::*; |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
fn parse(body: &str) -> Result<Config> { |
| 279 |
let base = r#" |
| 280 |
listen = "127.0.0.1:8765" |
| 281 |
db_path = "/var/lib/bento/bento.db" |
| 282 |
topology_path = "/etc/bento/bento.toml" |
| 283 |
secrets_root = "/home/max/Code/_private" |
| 284 |
dist_root = "/home/max/Dist" |
| 285 |
"#; |
| 286 |
let cfg: Config = toml::from_str(&format!("{base}{body}"))?; |
| 287 |
cfg.validate()?; |
| 288 |
Ok(cfg) |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
#[test] |
| 295 |
fn the_archive_table_is_optional() { |
| 296 |
assert!(parse("").unwrap().archive.is_none()); |
| 297 |
} |
| 298 |
|
| 299 |
#[test] |
| 300 |
fn an_archive_host_and_root_parse() { |
| 301 |
let cfg = |
| 302 |
parse("[archive]\nhost = \"astra\"\nroot = \"/var/lib/bento/artifacts\"\n").unwrap(); |
| 303 |
let a = cfg.archive.expect("archive configured"); |
| 304 |
assert_eq!(a.host, "astra"); |
| 305 |
assert_eq!(a.root, PathBuf::from("/var/lib/bento/artifacts")); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
#[test] |
| 313 |
fn a_relative_or_dot_dot_archive_root_is_rejected() { |
| 314 |
assert!(parse("[archive]\nhost = \"astra\"\nroot = \"artifacts\"\n").is_err()); |
| 315 |
assert!(parse("[archive]\nhost = \"astra\"\nroot = \"/var/../etc/bento\"\n").is_err()); |
| 316 |
} |
| 317 |
|
| 318 |
#[test] |
| 319 |
fn an_empty_archive_host_is_rejected() { |
| 320 |
assert!(parse("[archive]\nhost = \"\"\nroot = \"/var/lib/bento/artifacts\"\n").is_err()); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
#[test] |
| 326 |
fn handoff_is_per_app_and_absent_by_default() { |
| 327 |
assert!(parse("").unwrap().handoff.is_empty()); |
| 328 |
} |
| 329 |
|
| 330 |
#[test] |
| 331 |
fn a_handoff_parses_with_its_optional_fields_absent() { |
| 332 |
let cfg = parse( |
| 333 |
r#" |
| 334 |
[handoff.pom] |
| 335 |
host = "sando@fw13" |
| 336 |
staging_root = "/srv/sando/staging" |
| 337 |
url = "http://100.103.89.95:7766" |
| 338 |
"#, |
| 339 |
) |
| 340 |
.unwrap(); |
| 341 |
let h = cfg.handoff.get("pom").expect("pom hands off"); |
| 342 |
assert_eq!(h.host, "sando@fw13"); |
| 343 |
assert_eq!(h.staging_root, PathBuf::from("/srv/sando/staging")); |
| 344 |
|
| 345 |
|
| 346 |
assert!(h.sando_app.is_none()); |
| 347 |
assert!(h.token_env.is_none()); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
#[test] |
| 354 |
fn a_relative_or_dot_dot_staging_root_is_rejected() { |
| 355 |
let with = |root: &str| { |
| 356 |
format!( |
| 357 |
"[handoff.pom]\nhost = \"fw13\"\nstaging_root = \"{root}\"\nurl = \"http://x:1\"\n" |
| 358 |
) |
| 359 |
}; |
| 360 |
assert!(parse(&with("staging")).is_err()); |
| 361 |
assert!(parse(&with("/srv/../etc/sando")).is_err()); |
| 362 |
assert!(parse(&with("/srv/sando/staging")).is_ok()); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
#[test] |
| 370 |
fn a_token_env_that_is_a_path_or_a_pasted_secret_is_rejected() { |
| 371 |
let with = |token: &str| { |
| 372 |
format!( |
| 373 |
"[handoff.pom]\nhost = \"fw13\"\nstaging_root = \"/srv/sando/staging\"\n\ |
| 374 |
url = \"http://x:1\"\ntoken_env = \"{token}\"\n" |
| 375 |
) |
| 376 |
}; |
| 377 |
assert!(parse(&with("sando/api-token")).is_err()); |
| 378 |
assert!(parse(&with("../../etc/shadow")).is_err()); |
| 379 |
assert!(parse(&with("")).is_err()); |
| 380 |
|
| 381 |
|
| 382 |
assert!(parse(&with("2f9c4e1a-77bd-4f0e-9a3e-1c8d5b6e0f21")).is_err()); |
| 383 |
assert!(parse(&with("BENTO_SANDO_TOKEN")).is_ok()); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
#[test] |
| 391 |
fn the_retired_token_file_key_is_refused() { |
| 392 |
let err = parse( |
| 393 |
"[handoff.pom]\nhost = \"fw13\"\nstaging_root = \"/srv/sando/staging\"\n\ |
| 394 |
url = \"http://x:1\"\ntoken_file = \"sando/api-token\"\n", |
| 395 |
) |
| 396 |
.expect_err("token_file was replaced by token_env"); |
| 397 |
assert!(format!("{err:#}").contains("token_file"), "{err:#}"); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
#[test] |
| 403 |
fn a_handoff_url_must_name_a_scheme() { |
| 404 |
let with = |url: &str| { |
| 405 |
format!( |
| 406 |
"[handoff.pom]\nhost = \"fw13\"\nstaging_root = \"/srv/sando/staging\"\nurl = \"{url}\"\n" |
| 407 |
) |
| 408 |
}; |
| 409 |
assert!(parse(&with("100.103.89.95:7766")).is_err()); |
| 410 |
assert!(parse(&with("http://100.103.89.95:7766")).is_ok()); |
| 411 |
} |
| 412 |
} |
| 413 |
|