The peer (T2) attach path had a per-pair access-control layer (`[daemon.policy].allow_inbound`): a target-side allow-list of daemon ids permitted to broker-attach, enforced inside JWT verification, hot-reloadable via SIGHUP / `POST /api/policy/reload`, with a `maestro policy` CLI. On a flat-trust perimeter (the operator's VPN/firewall is the boundary for which hosts connect) that granularity is unused, and deny-by-default made it config friction — cross-daemon attach silently failed until every peer was listed. Remove the allow-list; keep the registry-minted pair-attach JWT and ALL its binding checks (iss=registry, aud=target, sid=session, exp). The firewall gates host->port reachability; it does not stop a reachable host from attaching to a session it wasn't handed or replaying a token elsewhere — that's the JWT's job and it stays. Only the `sub in allow_inbound` check is dropped. Removed: DaemonPolicySection / [daemon.policy].allow_inbound (config + daemon), policy.rs (SIGHUP listener + GET/POST /api/policy), the `maestro policy` CLI, PairAttachVerifier's allow-list field/reload/snapshot + JwtError::NotAllowed, and the e2e broker_rejects_when_target_doesnt_allow_inbound test. The NAP-265 pre-upgrade-502 path stays covered by broker_attach_returns_502_on_perimeter_mismatch. Also dropped the unit's ExecReload=kill -HUP: with no SIGHUP handler left, default SIGHUP disposition is terminate, so wiring `systemctl reload` to it would kill the daemon. Re-introducing a per-pair gate later is a verify-time change. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
216 lines
7.7 KiB
Rust
216 lines
7.7 KiB
Rust
//! systemd detection + user-unit rendering.
|
|
//!
|
|
//! The wizard only ever writes a *user* unit (`~/.config/systemd/user/`), since
|
|
//! `maestro daemon` runs per-user and uses tmux sockets in the user's runtime dir.
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
/// Standard probe path. Present on hosts booted under systemd.
|
|
pub const SYSTEMD_PROBE_PATH: &str = "/run/systemd/system";
|
|
|
|
/// Filename written under `~/.config/systemd/user/` for the daemon (NAP-245
|
|
/// renamed this from `maestrod.service`).
|
|
pub const UNIT_FILE_NAME: &str = "maestro.service";
|
|
|
|
/// Filename for the optional `maestro-web` browser UI unit (NAP-249).
|
|
pub const WEB_UNIT_FILE_NAME: &str = "maestro-web.service";
|
|
|
|
/// `true` when the given path exists. Production callers should pass
|
|
/// [`SYSTEMD_PROBE_PATH`]; tests inject a temp path so they're hermetic.
|
|
pub fn is_systemd_host(probe: &Path) -> bool {
|
|
probe.exists()
|
|
}
|
|
|
|
/// `~/.config/systemd/user/<unit>`. `None` if `$HOME` / config dir is
|
|
/// unresolvable.
|
|
pub fn user_unit_path_for(unit_name: &str) -> Option<PathBuf> {
|
|
let base = dirs::config_dir()?;
|
|
Some(base.join("systemd").join("user").join(unit_name))
|
|
}
|
|
|
|
/// Convenience: path for `maestro.service`. Kept as a free function so
|
|
/// existing callers don't need to thread the unit name around.
|
|
pub fn user_unit_path() -> Option<PathBuf> {
|
|
user_unit_path_for(UNIT_FILE_NAME)
|
|
}
|
|
|
|
/// Convenience: path for `maestro-web.service` (NAP-249).
|
|
pub fn user_web_unit_path() -> Option<PathBuf> {
|
|
user_web_unit_path_for(WEB_UNIT_FILE_NAME)
|
|
}
|
|
|
|
fn user_web_unit_path_for(unit_name: &str) -> Option<PathBuf> {
|
|
user_unit_path_for(unit_name)
|
|
}
|
|
|
|
/// Render the daemon unit. `exec_start` is the absolute path to `maestro` —
|
|
/// we default to `~/.cargo/bin/maestro` because that's where `cargo install`
|
|
/// lands binaries; systemd doesn't inherit the user's PATH, so we can't rely
|
|
/// on a bare `maestro`. The unit runs `maestro daemon` (NAP-245).
|
|
pub fn render_user_unit(exec_start: &Path) -> String {
|
|
format!(
|
|
"[Unit]\n\
|
|
Description=Maestro per-host daemon\n\
|
|
Documentation=https://git.naps.pt/naps62/maestro\n\
|
|
After=network-online.target\n\
|
|
\n\
|
|
[Service]\n\
|
|
Type=simple\n\
|
|
ExecStart={exec} daemon\n\
|
|
# Signal only the daemon on stop/restart, not its whole cgroup —\n\
|
|
# otherwise systemd kills the tmux server (and every session) the\n\
|
|
# daemon spawned, defeating tmux persistence. recover() re-adopts the\n\
|
|
# surviving sessions on the next start (NAP-330).\n\
|
|
KillMode=process\n\
|
|
Restart=on-failure\n\
|
|
RestartSec=5\n\
|
|
\n\
|
|
[Install]\n\
|
|
WantedBy=default.target\n",
|
|
exec = exec_start.display()
|
|
)
|
|
}
|
|
|
|
/// Render the `maestro-web` user unit (NAP-249). Orders after `maestro.service`
|
|
/// so the proxy doesn't return 502s on startup; `Wants=` keeps it best-effort
|
|
/// — operators with daemon-less hosts can still install web alone if they
|
|
/// later remove the dependency.
|
|
pub fn render_web_unit(exec_start: &Path) -> String {
|
|
format!(
|
|
"[Unit]\n\
|
|
Description=Maestro web UI (maestro-web)\n\
|
|
Documentation=https://git.naps.pt/naps62/maestro\n\
|
|
After=network-online.target maestro.service\n\
|
|
Wants=maestro.service\n\
|
|
\n\
|
|
[Service]\n\
|
|
Type=simple\n\
|
|
ExecStart={exec}\n\
|
|
Restart=on-failure\n\
|
|
RestartSec=5\n\
|
|
\n\
|
|
[Install]\n\
|
|
WantedBy=default.target\n",
|
|
exec = exec_start.display()
|
|
)
|
|
}
|
|
|
|
/// Default `ExecStart` target: `~/.cargo/bin/maestro`. `None` if `$HOME` is
|
|
/// unresolvable.
|
|
pub fn default_maestro_path() -> Option<PathBuf> {
|
|
Some(dirs::home_dir()?.join(".cargo").join("bin").join("maestro"))
|
|
}
|
|
|
|
/// Default `ExecStart` target for the web unit: `~/.cargo/bin/maestro-web`.
|
|
pub fn default_maestro_web_path() -> Option<PathBuf> {
|
|
Some(
|
|
dirs::home_dir()?
|
|
.join(".cargo")
|
|
.join("bin")
|
|
.join("maestro-web"),
|
|
)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn is_systemd_host_true_when_probe_exists() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let probe = dir.path().join("system");
|
|
std::fs::create_dir(&probe).unwrap();
|
|
assert!(is_systemd_host(&probe));
|
|
}
|
|
|
|
#[test]
|
|
fn is_systemd_host_false_when_probe_missing() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let probe = dir.path().join("not-here");
|
|
assert!(!is_systemd_host(&probe));
|
|
}
|
|
|
|
#[test]
|
|
fn unit_renders_with_absolute_exec_start() {
|
|
let body = render_user_unit(Path::new("/home/alice/.cargo/bin/maestro"));
|
|
assert!(body.contains("ExecStart=/home/alice/.cargo/bin/maestro daemon"));
|
|
assert!(body.contains("[Unit]"));
|
|
assert!(body.contains("[Service]"));
|
|
assert!(body.contains("[Install]"));
|
|
assert!(body.contains("WantedBy=default.target"));
|
|
assert!(body.contains("Restart=on-failure"));
|
|
}
|
|
|
|
#[test]
|
|
fn unit_sets_killmode_process_so_restart_keeps_sessions() {
|
|
// NAP-330: without KillMode=process, `systemctl restart` SIGKILLs the
|
|
// daemon's whole cgroup — including the tmux server and every session.
|
|
let body = render_user_unit(Path::new("/x/maestro"));
|
|
assert!(
|
|
body.contains("\nKillMode=process\n"),
|
|
"daemon unit must set KillMode=process; got:\n{body}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn web_unit_does_not_set_killmode() {
|
|
// The web unit spawns no tmux, so it has no cgroup-kill problem and
|
|
// shouldn't carry the override.
|
|
let body = render_web_unit(Path::new("/x/maestro-web"));
|
|
assert!(
|
|
!body.contains("KillMode"),
|
|
"web unit needs no KillMode; got:\n{body}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn unit_has_no_execreload() {
|
|
// NAP-339 removed the daemon's SIGHUP handler (it only ever reloaded
|
|
// the now-deleted `[daemon.policy]`). Without a handler, SIGHUP's
|
|
// default disposition is to terminate, so the unit must NOT wire
|
|
// `systemctl reload` to it — that would kill the daemon.
|
|
let body = render_user_unit(Path::new("/x/maestro"));
|
|
assert!(!body.contains("ExecReload"));
|
|
}
|
|
|
|
#[test]
|
|
fn unit_ends_with_newline() {
|
|
let body = render_user_unit(Path::new("/x"));
|
|
assert!(body.ends_with('\n'), "unit must end with newline");
|
|
}
|
|
|
|
#[test]
|
|
fn user_unit_path_points_under_config_dir() {
|
|
let p = user_unit_path().expect("config dir resolvable in test env");
|
|
assert!(p.ends_with("systemd/user/maestro.service"), "got {p:?}");
|
|
}
|
|
|
|
#[test]
|
|
fn user_web_unit_path_points_under_config_dir() {
|
|
let p = user_web_unit_path().expect("config dir resolvable in test env");
|
|
assert!(p.ends_with("systemd/user/maestro-web.service"), "got {p:?}");
|
|
}
|
|
|
|
#[test]
|
|
fn web_unit_renders_with_dependency_on_daemon() {
|
|
let body = render_web_unit(Path::new("/home/alice/.cargo/bin/maestro-web"));
|
|
assert!(body.contains("ExecStart=/home/alice/.cargo/bin/maestro-web"));
|
|
assert!(body.contains("After=network-online.target maestro.service"));
|
|
assert!(body.contains("Wants=maestro.service"));
|
|
assert!(body.contains("WantedBy=default.target"));
|
|
assert!(body.ends_with('\n'));
|
|
}
|
|
|
|
#[test]
|
|
fn default_maestro_path_under_cargo_bin() {
|
|
let p = default_maestro_path().expect("HOME resolvable in test env");
|
|
assert!(p.ends_with(".cargo/bin/maestro"), "got {p:?}");
|
|
}
|
|
|
|
#[test]
|
|
fn default_maestro_web_path_under_cargo_bin() {
|
|
let p = default_maestro_web_path().expect("HOME resolvable in test env");
|
|
assert!(p.ends_with(".cargo/bin/maestro-web"), "got {p:?}");
|
|
}
|
|
}
|