| 1 |
use anyhow::Result; |
| 2 |
use sando_daemon::{ |
| 3 |
config, db, events, git, reconcile, retention, routes, runs, state, sync, topology, |
| 4 |
}; |
| 5 |
use std::net::SocketAddr; |
| 6 |
use std::path::Path; |
| 7 |
use std::sync::Arc; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
fn refuse_unauthenticated_bind(addr: &SocketAddr, has_token: bool) -> bool { |
| 13 |
!has_token && !addr.ip().is_loopback() |
| 14 |
} |
| 15 |
|
| 16 |
fn main() -> Result<()> { |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
if std::env::args().skip(1).any(|a| a == "--check-config") { |
| 27 |
return check_config(); |
| 28 |
} |
| 29 |
run() |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
fn check_config() -> Result<()> { |
| 37 |
let (_daemon, apps) = config::DaemonConfig::load()?; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
for (id, cfg) in &apps { |
| 43 |
let topo = validate_loaded(cfg)?; |
| 44 |
println!( |
| 45 |
"sandod --check-config: OK — app `{id}`, {} tier(s), topology {}", |
| 46 |
topo.tiers.len(), |
| 47 |
cfg.topology_path.display() |
| 48 |
); |
| 49 |
} |
| 50 |
Ok(()) |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
fn validate_loaded(cfg: &config::AppConfig) -> Result<topology::Topology> { |
| 57 |
cfg.validate()?; |
| 58 |
let topo = topology::Topology::load(&cfg.topology_path)?; |
| 59 |
if let Some(h) = cfg.build_host.as_deref() { |
| 60 |
topo.ensure_build_host_not_serving(h)?; |
| 61 |
} |
| 62 |
topo.ensure_migration_checks_have_backups(&cfg.migration_checks)?; |
| 63 |
topo.ensure_node_companions_are_built(&cfg.companions)?; |
| 64 |
topo.ensure_test_target_aux_repos_exist(&cfg.test_targets)?; |
| 65 |
Ok(topo) |
| 66 |
} |
| 67 |
|
| 68 |
#[tokio::main] |
| 69 |
async fn run() -> Result<()> { |
| 70 |
tracing_subscriber::fmt() |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
.with_writer(std::io::stderr) |
| 75 |
.with_env_filter( |
| 76 |
tracing_subscriber::EnvFilter::try_from_default_env() |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
.unwrap_or_else(|_| "sando_daemon=info,sandod=info,tower_http=info".into()), |
| 81 |
) |
| 82 |
.init(); |
| 83 |
|
| 84 |
let (daemon, app_cfgs) = config::DaemonConfig::load()?; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
let mut apps: state::AppMap = std::collections::BTreeMap::new(); |
| 90 |
for (id, cfg) in app_cfgs { |
| 91 |
let topo = Arc::new(topology::Topology::load(&cfg.topology_path)?); |
| 92 |
if let Some(h) = cfg.build_host.as_deref() { |
| 93 |
topo.ensure_build_host_not_serving(h)?; |
| 94 |
} |
| 95 |
topo.ensure_migration_checks_have_backups(&cfg.migration_checks)?; |
| 96 |
topo.ensure_node_companions_are_built(&cfg.companions)?; |
| 97 |
topo.ensure_test_target_aux_repos_exist(&cfg.test_targets)?; |
| 98 |
tokio::fs::create_dir_all(&cfg.workdir).await?; |
| 99 |
tokio::fs::create_dir_all(&cfg.release_root).await?; |
| 100 |
|
| 101 |
if let Some(repo) = topo.repo.as_ref() { |
| 102 |
git::ensure_bare_repo(Path::new(&repo.bare_path)).await?; |
| 103 |
} |
| 104 |
let executors = Arc::new(state::build_executors(&topo)); |
| 105 |
tracing::info!( |
| 106 |
app = %id, |
| 107 |
tiers = topo.tiers.len(), |
| 108 |
bare = topo.repo.as_ref().map_or("(intake-only)", |r| r.bare_path.as_str()), |
| 109 |
"app loaded" |
| 110 |
); |
| 111 |
apps.insert( |
| 112 |
id, |
| 113 |
Arc::new(state::App { |
| 114 |
cfg, |
| 115 |
topo, |
| 116 |
executors, |
| 117 |
}), |
| 118 |
); |
| 119 |
} |
| 120 |
let apps = Arc::new(apps); |
| 121 |
|
| 122 |
|
| 123 |
let default_app = apps |
| 124 |
.keys() |
| 125 |
.next() |
| 126 |
.cloned() |
| 127 |
.expect("DaemonConfig::load refuses an empty app set"); |
| 128 |
let default = apps[&default_app].clone(); |
| 129 |
let (cfg, topo) = (default.cfg.clone(), default.topo.clone()); |
| 130 |
let pool = db::connect(&daemon.db_path).await?; |
| 131 |
db::migrate(&pool).await?; |
| 132 |
|
| 133 |
|
| 134 |
match runs::recover_orphaned_running(&pool).await { |
| 135 |
Ok(0) => {} |
| 136 |
Ok(n) => tracing::warn!( |
| 137 |
reconciled = n, |
| 138 |
"settled orphaned 'building' run(s) from a prior daemon" |
| 139 |
), |
| 140 |
Err(e) => { |
| 141 |
tracing::error!(error = %e, "failed to reconcile orphaned 'building' runs at startup"); |
| 142 |
} |
| 143 |
} |
| 144 |
for (id, app) in apps.iter() { |
| 145 |
sync::sync(&pool, id, &app.topo).await?; |
| 146 |
tracing::debug!(app = %id, "topology synced"); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
match reconcile::recover_unrecorded_deploys(&pool).await { |
| 155 |
Ok(0) => {} |
| 156 |
Ok(n) => tracing::error!( |
| 157 |
flagged = n, |
| 158 |
"flagged tier(s) with an unrecorded deploy from a prior daemon; see /state partial_reason" |
| 159 |
), |
| 160 |
Err(e) => { |
| 161 |
tracing::error!(error = %e, "failed to reconcile deploys against tier_state at startup"); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
for (id, app) in apps.iter() { |
| 173 |
match retention::tier_refs(&pool, id, app.cfg.primary_bin()).await { |
| 174 |
Ok(refs) => { |
| 175 |
let gone = retention::missing(&refs).await; |
| 176 |
if gone.is_empty() { |
| 177 |
tracing::info!(app = %id, referenced = refs.len(), "every referenced release artifact is present"); |
| 178 |
} else { |
| 179 |
tracing::error!( |
| 180 |
app = %id, |
| 181 |
missing = gone.len(), |
| 182 |
detail = %retention::describe(&gone), |
| 183 |
"referenced release artifact(s) missing from the store; promote and rollback \ |
| 184 |
through them will fail at the rsync. See /state missing_artifact" |
| 185 |
); |
| 186 |
} |
| 187 |
} |
| 188 |
Err(e) => { |
| 189 |
tracing::error!(app = %id, error = %e, "could not check referenced release artifacts"); |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
if let Some(scratch_url) = cfg.scratch_db_url.as_deref() { |
| 199 |
sando_daemon::gates::preflight_scratch_privileges(scratch_url).await?; |
| 200 |
} |
| 201 |
|
| 202 |
let addr: SocketAddr = daemon.listen.parse()?; |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
let api_token: Option<Arc<str>> = std::env::var("SANDO_API_TOKEN") |
| 209 |
.ok() |
| 210 |
.map(|s| s.trim().to_string()) |
| 211 |
.filter(|s| !s.is_empty()) |
| 212 |
.map(|s| Arc::from(s.as_str())); |
| 213 |
if refuse_unauthenticated_bind(&addr, api_token.is_some()) { |
| 214 |
anyhow::bail!( |
| 215 |
"SANDO_API_TOKEN is unset but listen={addr} is not loopback; refusing to expose deploy \ |
| 216 |
controls unauthenticated. Set SANDO_API_TOKEN (via EnvironmentFile) or bind 127.0.0.1." |
| 217 |
); |
| 218 |
} |
| 219 |
match &api_token { |
| 220 |
Some(_) => tracing::info!("deploy endpoints require a bearer token"), |
| 221 |
None => { |
| 222 |
tracing::warn!(%addr, "SANDO_API_TOKEN unset; deploy endpoints are UNAUTHENTICATED (loopback bind)"); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
let executors = default.executors.clone(); |
| 227 |
let app_state = state::AppState { |
| 228 |
pool, |
| 229 |
apps, |
| 230 |
default_app, |
| 231 |
topo, |
| 232 |
cfg, |
| 233 |
active_build: Arc::new(tokio::sync::Mutex::new(None)), |
| 234 |
deploy_lock: Arc::new(tokio::sync::Mutex::new(())), |
| 235 |
events: events::channel(), |
| 236 |
executors, |
| 237 |
api_token, |
| 238 |
}; |
| 239 |
let app = routes::router_for_apps(app_state); |
| 240 |
tracing::info!(%addr, "sando daemon listening"); |
| 241 |
let listener = tokio::net::TcpListener::bind(addr).await?; |
| 242 |
|
| 243 |
|
| 244 |
axum::serve( |
| 245 |
listener, |
| 246 |
app.into_make_service_with_connect_info::<SocketAddr>(), |
| 247 |
) |
| 248 |
.await?; |
| 249 |
Ok(()) |
| 250 |
} |
| 251 |
|
| 252 |
#[cfg(test)] |
| 253 |
mod tests { |
| 254 |
use super::{refuse_unauthenticated_bind, validate_loaded}; |
| 255 |
use std::net::SocketAddr; |
| 256 |
|
| 257 |
fn addr(s: &str) -> SocketAddr { |
| 258 |
s.parse().unwrap() |
| 259 |
} |
| 260 |
|
| 261 |
#[test] |
| 262 |
fn check_config_accepts_the_shipped_topology() { |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")); |
| 278 |
let raw = std::fs::read_to_string(root.join("../deploy/sando-daemon.toml.example")) |
| 279 |
.expect("reading the shipped production daemon config"); |
| 280 |
let mut cfg: super::config::AppConfig = |
| 281 |
toml::from_str(&raw).expect("shipped daemon config must parse"); |
| 282 |
cfg.topology_path = root.join("../sando.toml"); |
| 283 |
validate_loaded(&cfg).expect("shipped config must pass --check-config"); |
| 284 |
} |
| 285 |
|
| 286 |
#[test] |
| 287 |
fn non_loopback_without_token_is_refused() { |
| 288 |
|
| 289 |
assert!(refuse_unauthenticated_bind( |
| 290 |
&addr("100.103.89.95:7766"), |
| 291 |
false |
| 292 |
)); |
| 293 |
assert!(refuse_unauthenticated_bind(&addr("0.0.0.0:7766"), false)); |
| 294 |
} |
| 295 |
|
| 296 |
#[test] |
| 297 |
fn loopback_without_token_is_allowed() { |
| 298 |
assert!(!refuse_unauthenticated_bind(&addr("127.0.0.1:7766"), false)); |
| 299 |
assert!(!refuse_unauthenticated_bind(&addr("[::1]:7766"), false)); |
| 300 |
} |
| 301 |
|
| 302 |
#[test] |
| 303 |
fn any_bind_with_token_is_allowed() { |
| 304 |
assert!(!refuse_unauthenticated_bind( |
| 305 |
&addr("100.103.89.95:7766"), |
| 306 |
true |
| 307 |
)); |
| 308 |
assert!(!refuse_unauthenticated_bind(&addr("127.0.0.1:7766"), true)); |
| 309 |
} |
| 310 |
} |
| 311 |
|