refactor(maestro): drop allow_inbound policy gate, keep JWT session binding (NAP-339)
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>
This commit is contained in:
@@ -15,9 +15,9 @@ use crate::sidecar::SidecarStore;
|
||||
use crate::tmux::Tmux;
|
||||
use crate::worktree::WorktreeManager;
|
||||
use crate::{
|
||||
attach, default_config_path, default_state_dir, load_config, local_api, policy, run_configure,
|
||||
spawn_session_publisher, spawn_sighup_listener, ConfigureInputs, EnvOverrides, JwtVerifier,
|
||||
LocalEventBus, PairAttachVerifier, PolicyState, RegistryClient, SessionManager,
|
||||
attach, default_config_path, default_state_dir, load_config, local_api, run_configure,
|
||||
spawn_session_publisher, ConfigureInputs, EnvOverrides, JwtVerifier, LocalEventBus,
|
||||
PairAttachVerifier, RegistryClient, SessionManager,
|
||||
};
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
@@ -113,11 +113,7 @@ async fn run(config_override: Option<PathBuf>) -> Result<(), Box<dyn Error>> {
|
||||
manager.start_liveness_watcher(DEFAULT_LIVENESS_INTERVAL);
|
||||
|
||||
let verifier = JwtVerifier::new(&config.token, &config.daemon_id);
|
||||
let pair_verifier = PairAttachVerifier::new(
|
||||
&config.token,
|
||||
&config.daemon_id,
|
||||
config.allow_inbound.iter().cloned(),
|
||||
);
|
||||
let pair_verifier = PairAttachVerifier::new(&config.token, &config.daemon_id);
|
||||
|
||||
let events = LocalEventBus::new();
|
||||
spawn_session_publisher(manager.clone(), events.clone());
|
||||
@@ -151,16 +147,9 @@ async fn run(config_override: Option<PathBuf>) -> Result<(), Box<dyn Error>> {
|
||||
self_daemon_id: std::sync::Arc::new(config.daemon_id.clone()),
|
||||
self_perimeter_id: config.perimeter_id.clone().map(std::sync::Arc::new),
|
||||
};
|
||||
let policy_state = PolicyState {
|
||||
verifier: pair_verifier.clone(),
|
||||
config_path: std::sync::Arc::new(config_path.clone()),
|
||||
};
|
||||
spawn_sighup_listener(policy_state.clone());
|
||||
|
||||
let router = attach::router(manager.clone(), verifier, pair_verifier)
|
||||
.merge(local_api::router(local_state))
|
||||
.merge(local_api::broker_router(broker_state))
|
||||
.merge(policy::router(policy_state));
|
||||
.merge(local_api::broker_router(broker_state));
|
||||
|
||||
let listener = TcpListener::bind(config.bind_addr).await?;
|
||||
let bound = listener.local_addr()?;
|
||||
|
||||
@@ -90,9 +90,6 @@ pub struct DaemonConfig {
|
||||
/// Daemon's reachable address on the declared perimeter. Only meaningful
|
||||
/// when `perimeter_id` is set.
|
||||
pub private_addr: Option<String>,
|
||||
/// daemon-ids permitted to broker-attach to this daemon's sessions.
|
||||
/// Default empty (deny all). Local attach bypasses. (NAP-247.)
|
||||
pub allow_inbound: Vec<String>,
|
||||
/// Per-session PTY replay buffer cap in bytes (NAP-229). Each new attach
|
||||
/// receives this much of the prior PTY output before the live stream.
|
||||
/// Default `pty::DEFAULT_REPLAY_BYTES`; `0` disables.
|
||||
@@ -242,7 +239,6 @@ pub fn load_config(path: &Path, env: EnvOverrides) -> Result<DaemonConfig, Confi
|
||||
|
||||
let perimeter_id = env.perimeter_id.or(daemon.perimeter_id);
|
||||
let private_addr = env.private_addr.or(daemon.private_addr);
|
||||
let allow_inbound = daemon.policy.allow_inbound;
|
||||
let pty_replay_bytes = daemon
|
||||
.pty_replay_bytes
|
||||
.unwrap_or(crate::pty::DEFAULT_REPLAY_BYTES);
|
||||
@@ -264,7 +260,6 @@ pub fn load_config(path: &Path, env: EnvOverrides) -> Result<DaemonConfig, Confi
|
||||
worktree_root,
|
||||
perimeter_id,
|
||||
private_addr,
|
||||
allow_inbound,
|
||||
pty_replay_bytes,
|
||||
tmux_tmpdir,
|
||||
})
|
||||
|
||||
@@ -125,9 +125,6 @@ pub fn run(
|
||||
// are operator-edited in config.toml directly. Leave None on bootstrap.
|
||||
perimeter_id: None,
|
||||
private_addr: None,
|
||||
// Default deny: empty allow_inbound means no peer can attach until the
|
||||
// operator opts in. Set in config.toml under [daemon.policy].
|
||||
policy: Default::default(),
|
||||
// None ⇒ daemon falls back to pty::DEFAULT_REPLAY_BYTES. Override in
|
||||
// config.toml as `[daemon] pty_replay_bytes = ...` (NAP-229).
|
||||
pty_replay_bytes: None,
|
||||
|
||||
@@ -2,9 +2,6 @@
|
||||
//! validated locally by each daemon using the shared secret — no callback to
|
||||
//! the registry on the attach hot path. Spec §Auth.
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
use jsonwebtoken::{decode, encode, Algorithm, DecodingKey, EncodingKey, Header, Validation};
|
||||
use maestro_protocol::{JwtClaims, PairAttachClaims, REGISTRY_ISSUER};
|
||||
use uuid::Uuid;
|
||||
@@ -17,8 +14,6 @@ pub enum JwtError {
|
||||
WrongDaemon { wanted: String, ours: String },
|
||||
#[error("session mismatch (token wants `{wanted}`, path is `{path}`)")]
|
||||
WrongSession { wanted: Uuid, path: Uuid },
|
||||
#[error("home daemon `{sub}` not in this daemon's allow_inbound")]
|
||||
NotAllowed { sub: String },
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -68,61 +63,41 @@ pub fn issue(secret: &str, claims: &JwtClaims) -> Result<String, jsonwebtoken::e
|
||||
}
|
||||
|
||||
/// Verifier for pair-scoped attach JWTs (NAP-247). Checks signature, `exp`,
|
||||
/// `aud == self.daemon_id`, and `sub in allow_inbound`. The `allow_inbound`
|
||||
/// set lives behind an [`Arc<RwLock<_>>`] so it can be swapped at runtime by
|
||||
/// [`PairAttachVerifier::reload`] (NAP-237 hot reload via SIGHUP /
|
||||
/// `POST /api/policy/reload`). Clones share the same inner — any reload
|
||||
/// observed by one clone is observed by all of them.
|
||||
/// `iss == REGISTRY_ISSUER`, `aud == self.daemon_id`, and `sid == session_id`
|
||||
/// — i.e. the token was minted by the registry for *this* daemon and *this*
|
||||
/// session, and hasn't expired. This is the session-binding guarantee the
|
||||
/// network perimeter can't provide: a host the firewall admits still can't
|
||||
/// attach to a session it wasn't handed, or replay a token to another
|
||||
/// session/daemon.
|
||||
///
|
||||
/// It does **not** gate on the home daemon's identity (no `allow_inbound`
|
||||
/// allow-list, NAP-339): the operator's perimeter/firewall is the trust
|
||||
/// boundary for *which hosts may connect*, so every daemon on the perimeter is
|
||||
/// equally trusted to attach. Re-introducing a per-pair gate later is a
|
||||
/// verify-time check, not an architecture change.
|
||||
#[derive(Clone)]
|
||||
pub struct PairAttachVerifier {
|
||||
decoding: DecodingKey,
|
||||
daemon_id: String,
|
||||
allow_inbound: Arc<RwLock<HashSet<String>>>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for PairAttachVerifier {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("PairAttachVerifier")
|
||||
.field("daemon_id", &self.daemon_id)
|
||||
.field("allow_inbound", &self.snapshot())
|
||||
.field("decoding", &"<redacted>")
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl PairAttachVerifier {
|
||||
pub fn new(
|
||||
secret: &str,
|
||||
daemon_id: impl Into<String>,
|
||||
allow_inbound: impl IntoIterator<Item = String>,
|
||||
) -> Self {
|
||||
pub fn new(secret: &str, daemon_id: impl Into<String>) -> Self {
|
||||
Self {
|
||||
decoding: DecodingKey::from_secret(secret.as_bytes()),
|
||||
daemon_id: daemon_id.into(),
|
||||
allow_inbound: Arc::new(RwLock::new(allow_inbound.into_iter().collect())),
|
||||
}
|
||||
}
|
||||
|
||||
/// Swap the effective `allow_inbound` set. Affects subsequent `verify`
|
||||
/// calls; in-flight attaches keep their existing connection (per spec
|
||||
/// — revocation requires killing the session).
|
||||
pub fn reload(&self, allow_inbound: impl IntoIterator<Item = String>) {
|
||||
let mut g = self
|
||||
.allow_inbound
|
||||
.write()
|
||||
.unwrap_or_else(|e| e.into_inner());
|
||||
*g = allow_inbound.into_iter().collect();
|
||||
}
|
||||
|
||||
/// Sorted snapshot of the current effective `allow_inbound` set. Used by
|
||||
/// `GET /api/policy` and debug formatting.
|
||||
pub fn snapshot(&self) -> Vec<String> {
|
||||
let g = self.allow_inbound.read().unwrap_or_else(|e| e.into_inner());
|
||||
let mut v: Vec<String> = g.iter().cloned().collect();
|
||||
v.sort();
|
||||
v
|
||||
}
|
||||
|
||||
/// Validate the token for the path-bound `session_id`. On success returns
|
||||
/// the home daemon's id (`sub`) for logging/tracing.
|
||||
pub fn verify(&self, token: &str, session_id: Uuid) -> Result<String, JwtError> {
|
||||
@@ -148,13 +123,6 @@ impl PairAttachVerifier {
|
||||
path: session_id,
|
||||
});
|
||||
}
|
||||
let allowed = {
|
||||
let g = self.allow_inbound.read().unwrap_or_else(|e| e.into_inner());
|
||||
g.contains(&claims.sub)
|
||||
};
|
||||
if !allowed {
|
||||
return Err(JwtError::NotAllowed { sub: claims.sub });
|
||||
}
|
||||
Ok(claims.sub)
|
||||
}
|
||||
}
|
||||
@@ -230,42 +198,21 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pair_verifier_accepts_allow_listed_home() {
|
||||
fn pair_verifier_accepts_registry_minted_token() {
|
||||
let secret = "s";
|
||||
let v = PairAttachVerifier::new(secret, "konishi", ["laptop".into()]);
|
||||
let v = PairAttachVerifier::new(secret, "konishi");
|
||||
let sid = Uuid::new_v4();
|
||||
// The home daemon's identity is no longer gated (NAP-339) — any
|
||||
// registry-minted, correctly-bound token for this daemon+session passes.
|
||||
let token = issue_pair(secret, &pair_claims("laptop", "konishi", sid, 60));
|
||||
let sub = v.verify(&token, sid).unwrap();
|
||||
assert_eq!(sub, "laptop");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pair_verifier_rejects_unlisted_home() {
|
||||
let secret = "s";
|
||||
let v = PairAttachVerifier::new(secret, "konishi", ["other".into()]);
|
||||
let sid = Uuid::new_v4();
|
||||
let token = issue_pair(secret, &pair_claims("laptop", "konishi", sid, 60));
|
||||
let err = v.verify(&token, sid).unwrap_err();
|
||||
assert!(matches!(err, JwtError::NotAllowed { .. }), "{err:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pair_verifier_rejects_empty_allow_inbound() {
|
||||
// Default deny: no peer listed → every cross-daemon attach fails.
|
||||
let secret = "s";
|
||||
let v = PairAttachVerifier::new(secret, "konishi", std::iter::empty::<String>());
|
||||
let sid = Uuid::new_v4();
|
||||
let token = issue_pair(secret, &pair_claims("laptop", "konishi", sid, 60));
|
||||
assert!(matches!(
|
||||
v.verify(&token, sid).unwrap_err(),
|
||||
JwtError::NotAllowed { .. }
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pair_verifier_rejects_wrong_audience() {
|
||||
let secret = "s";
|
||||
let v = PairAttachVerifier::new(secret, "konishi", ["laptop".into()]);
|
||||
let v = PairAttachVerifier::new(secret, "konishi");
|
||||
let sid = Uuid::new_v4();
|
||||
// Token addressed to a different target — must not pass our checks.
|
||||
let token = issue_pair(secret, &pair_claims("laptop", "elsewhere", sid, 60));
|
||||
@@ -278,7 +225,7 @@ mod tests {
|
||||
// `iss` (e.g. minted for some other purpose) must not validate as a
|
||||
// pair-attach credential.
|
||||
let secret = "s";
|
||||
let v = PairAttachVerifier::new(secret, "konishi", ["laptop".into()]);
|
||||
let v = PairAttachVerifier::new(secret, "konishi");
|
||||
let sid = Uuid::new_v4();
|
||||
let mut claims = pair_claims("laptop", "konishi", sid, 60);
|
||||
claims.iss = "somebody-else".into();
|
||||
@@ -289,7 +236,7 @@ mod tests {
|
||||
#[test]
|
||||
fn pair_verifier_rejects_session_id_mismatch() {
|
||||
let secret = "s";
|
||||
let v = PairAttachVerifier::new(secret, "konishi", ["laptop".into()]);
|
||||
let v = PairAttachVerifier::new(secret, "konishi");
|
||||
let sid = Uuid::new_v4();
|
||||
let path = Uuid::new_v4();
|
||||
let token = issue_pair(secret, &pair_claims("laptop", "konishi", sid, 60));
|
||||
@@ -302,57 +249,17 @@ mod tests {
|
||||
#[test]
|
||||
fn pair_verifier_rejects_expired() {
|
||||
let secret = "s";
|
||||
let v = PairAttachVerifier::new(secret, "konishi", ["laptop".into()]);
|
||||
let v = PairAttachVerifier::new(secret, "konishi");
|
||||
let sid = Uuid::new_v4();
|
||||
let token = issue_pair(secret, &pair_claims("laptop", "konishi", sid, -120));
|
||||
assert!(v.verify(&token, sid).is_err());
|
||||
}
|
||||
|
||||
// NAP-237: hot reload. After `reload`, the same verifier (and any
|
||||
// pre-existing clone) sees the new set on the next `verify`.
|
||||
|
||||
#[test]
|
||||
fn pair_verifier_reload_admits_new_peer() {
|
||||
let secret = "s";
|
||||
let v = PairAttachVerifier::new(secret, "konishi", std::iter::empty::<String>());
|
||||
fn pair_verifier_rejects_wrong_secret() {
|
||||
let v = PairAttachVerifier::new("right", "konishi");
|
||||
let sid = Uuid::new_v4();
|
||||
let token = issue_pair(secret, &pair_claims("laptop", "konishi", sid, 60));
|
||||
let token = issue_pair("wrong", &pair_claims("laptop", "konishi", sid, 60));
|
||||
assert!(v.verify(&token, sid).is_err());
|
||||
v.reload(["laptop".into()]);
|
||||
assert_eq!(v.verify(&token, sid).unwrap(), "laptop");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pair_verifier_reload_revokes_removed_peer() {
|
||||
let secret = "s";
|
||||
let v = PairAttachVerifier::new(secret, "konishi", ["laptop".into(), "phone".into()]);
|
||||
let sid = Uuid::new_v4();
|
||||
let token = issue_pair(secret, &pair_claims("laptop", "konishi", sid, 60));
|
||||
assert_eq!(v.verify(&token, sid).unwrap(), "laptop");
|
||||
v.reload(["phone".into()]); // drop laptop
|
||||
assert!(matches!(
|
||||
v.verify(&token, sid).unwrap_err(),
|
||||
JwtError::NotAllowed { .. }
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pair_verifier_clone_shares_inner_set() {
|
||||
// Cloning the verifier (e.g. to hand to axum state and to the SIGHUP
|
||||
// task) must NOT detach the allow_inbound set — both halves observe
|
||||
// every reload.
|
||||
let secret = "s";
|
||||
let v1 = PairAttachVerifier::new(secret, "konishi", std::iter::empty::<String>());
|
||||
let v2 = v1.clone();
|
||||
v1.reload(["laptop".into()]);
|
||||
let sid = Uuid::new_v4();
|
||||
let token = issue_pair(secret, &pair_claims("laptop", "konishi", sid, 60));
|
||||
assert_eq!(v2.verify(&token, sid).unwrap(), "laptop");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pair_verifier_snapshot_is_sorted() {
|
||||
let v = PairAttachVerifier::new("s", "konishi", ["phone".into(), "laptop".into()]);
|
||||
assert_eq!(v.snapshot(), vec!["laptop".to_string(), "phone".into()]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@ pub mod dirlist;
|
||||
pub mod jwt;
|
||||
pub mod local_api;
|
||||
pub mod local_events;
|
||||
pub mod policy;
|
||||
pub mod pty;
|
||||
pub mod registry_client;
|
||||
pub mod session;
|
||||
@@ -30,6 +29,5 @@ pub use config::{default_config_path, default_state_dir, load_config, DaemonConf
|
||||
pub use configure::{run as run_configure, ConfigureError, ConfigureInputs};
|
||||
pub use jwt::{issue as issue_jwt, JwtError, JwtVerifier, PairAttachVerifier};
|
||||
pub use local_events::{spawn_session_publisher, LocalEventBus};
|
||||
pub use policy::{spawn_sighup_listener, PolicyState};
|
||||
pub use registry_client::{ClientError, RegistryClient, HEARTBEAT_INTERVAL};
|
||||
pub use session::{SessionError, SessionEvent, SessionManager};
|
||||
|
||||
@@ -1,196 +0,0 @@
|
||||
//! Hot reload for `[daemon.policy] allow_inbound` (NAP-237).
|
||||
//!
|
||||
//! Two triggers, one execution path:
|
||||
//!
|
||||
//! - **SIGHUP** — `systemctl --user reload maestrod` (or any `kill -HUP`).
|
||||
//! - **`POST /api/policy/reload`** — used by `maestro policy reload` so
|
||||
//! operators don't need to know the PID. Also gives `maestro-web` and
|
||||
//! other local-API consumers a uniform reload hook.
|
||||
//!
|
||||
//! Both re-read `~/.config/maestro/config.toml` and swap the verifier's
|
||||
//! allow-list atomically; in-flight attaches keep their existing connection
|
||||
//! (revoking those requires killing the session, per spec).
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::extract::State;
|
||||
use axum::http::StatusCode;
|
||||
use axum::routing::{get, post};
|
||||
use axum::{Json, Router};
|
||||
use maestro_config::MaestroConfigFile;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::jwt::PairAttachVerifier;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PolicyState {
|
||||
pub verifier: PairAttachVerifier,
|
||||
pub config_path: Arc<PathBuf>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct PolicyView {
|
||||
pub allow_inbound: Vec<String>,
|
||||
}
|
||||
|
||||
pub fn router(state: PolicyState) -> Router {
|
||||
Router::new()
|
||||
.route("/api/policy", get(get_policy))
|
||||
.route("/api/policy/reload", post(reload_policy))
|
||||
.with_state(state)
|
||||
}
|
||||
|
||||
async fn get_policy(State(s): State<PolicyState>) -> Json<PolicyView> {
|
||||
Json(PolicyView {
|
||||
allow_inbound: s.verifier.snapshot(),
|
||||
})
|
||||
}
|
||||
|
||||
async fn reload_policy(
|
||||
State(s): State<PolicyState>,
|
||||
) -> Result<Json<PolicyView>, (StatusCode, String)> {
|
||||
match reload_from_file(&s.config_path, &s.verifier) {
|
||||
Ok(list) => {
|
||||
tracing::info!(?list, source = "http", "reloaded allow_inbound");
|
||||
Ok(Json(PolicyView {
|
||||
allow_inbound: list,
|
||||
}))
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!(error = %e, source = "http", "policy reload failed");
|
||||
Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Re-read the unified config file and apply `[daemon.policy].allow_inbound`
|
||||
/// to the live verifier. Returns the new effective list (sorted), or an
|
||||
/// error if the file can't be read/parsed — in which case the existing
|
||||
/// allow-list stays in place so a bad edit doesn't lock everyone out.
|
||||
pub fn reload_from_file(
|
||||
config_path: &Path,
|
||||
verifier: &PairAttachVerifier,
|
||||
) -> Result<Vec<String>, maestro_config::ConfigError> {
|
||||
let file = MaestroConfigFile::load(config_path)?;
|
||||
let list = file.daemon.policy.allow_inbound;
|
||||
verifier.reload(list.iter().cloned());
|
||||
Ok(verifier.snapshot())
|
||||
}
|
||||
|
||||
/// Spawn a tokio task that re-applies the policy on every SIGHUP. Logs
|
||||
/// success / failure but never exits — a transient bad config (e.g. mid-edit)
|
||||
/// becomes a logged error, not a daemon kill.
|
||||
pub fn spawn_sighup_listener(state: PolicyState) {
|
||||
tokio::spawn(async move {
|
||||
let mut sig = match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::hangup()) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
tracing::error!(error = %e, "failed to install SIGHUP handler");
|
||||
return;
|
||||
}
|
||||
};
|
||||
while sig.recv().await.is_some() {
|
||||
match reload_from_file(&state.config_path, &state.verifier) {
|
||||
Ok(list) => {
|
||||
tracing::info!(?list, source = "sighup", "reloaded allow_inbound");
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!(error = %e, source = "sighup", "policy reload failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn write_config(path: &Path, allow_inbound: &[&str]) {
|
||||
let body = if allow_inbound.is_empty() {
|
||||
r#"
|
||||
[shared]
|
||||
registry_url = "http://r"
|
||||
token = "t"
|
||||
|
||||
[daemon]
|
||||
daemon_url = "ws://x"
|
||||
"#
|
||||
.to_string()
|
||||
} else {
|
||||
let list = allow_inbound
|
||||
.iter()
|
||||
.map(|s| format!("\"{s}\""))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
format!(
|
||||
r#"
|
||||
[shared]
|
||||
registry_url = "http://r"
|
||||
token = "t"
|
||||
|
||||
[daemon]
|
||||
daemon_url = "ws://x"
|
||||
|
||||
[daemon.policy]
|
||||
allow_inbound = [{list}]
|
||||
"#
|
||||
)
|
||||
};
|
||||
std::fs::write(path, body).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reload_from_file_picks_up_new_entries() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("config.toml");
|
||||
write_config(&path, &["laptop"]);
|
||||
let v = PairAttachVerifier::new("s", "konishi", ["laptop".into()]);
|
||||
assert_eq!(v.snapshot(), vec!["laptop".to_string()]);
|
||||
|
||||
write_config(&path, &["laptop", "phone"]);
|
||||
let list = reload_from_file(&path, &v).unwrap();
|
||||
assert_eq!(list, vec!["laptop".to_string(), "phone".into()]);
|
||||
assert_eq!(v.snapshot(), vec!["laptop".to_string(), "phone".into()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reload_from_file_drops_removed_entries() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("config.toml");
|
||||
write_config(&path, &["laptop", "phone"]);
|
||||
let v = PairAttachVerifier::new("s", "konishi", ["laptop".into(), "phone".into()]);
|
||||
|
||||
write_config(&path, &["phone"]);
|
||||
let list = reload_from_file(&path, &v).unwrap();
|
||||
assert_eq!(list, vec!["phone".to_string()]);
|
||||
assert_eq!(v.snapshot(), vec!["phone".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reload_from_file_leaves_set_intact_on_parse_error() {
|
||||
// A mid-edit broken config must not lock everyone out — keep the
|
||||
// previously-effective list and surface the error to the caller.
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("config.toml");
|
||||
write_config(&path, &["laptop"]);
|
||||
let v = PairAttachVerifier::new("s", "konishi", ["laptop".into()]);
|
||||
std::fs::write(&path, "this = is = not = toml").unwrap();
|
||||
assert!(reload_from_file(&path, &v).is_err());
|
||||
assert_eq!(v.snapshot(), vec!["laptop".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reload_from_file_treats_missing_section_as_deny_all() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("config.toml");
|
||||
write_config(&path, &["laptop"]);
|
||||
let v = PairAttachVerifier::new("s", "konishi", ["laptop".into()]);
|
||||
|
||||
write_config(&path, &[]); // no [daemon.policy] at all
|
||||
let list = reload_from_file(&path, &v).unwrap();
|
||||
assert!(list.is_empty());
|
||||
assert!(v.snapshot().is_empty());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user