@@ -84,7 +84,8 @@ pub fn attach_url(info: &ConnectInfo, session_id: Uuid) -> Result<String, Attach
|
||||
/// The broker endpoint (`WS /api/attach/:id`, NAP-243) lives on the daemon's
|
||||
/// `bind_addr`, so we take the HTTP base (`http://127.0.0.1:7070`), swap the
|
||||
/// scheme to its WS equivalent, and append the route. The daemon picks the
|
||||
/// transport and enforces `allow_inbound` on the target side; the CLI just
|
||||
/// transport and dials the target (which verifies the registry-minted
|
||||
/// pair-attach JWT — session binding, not a per-peer allow-list); the CLI just
|
||||
/// pumps frames.
|
||||
#[allow(clippy::result_large_err)]
|
||||
pub fn broker_attach_url(
|
||||
|
||||
@@ -42,23 +42,6 @@ pub enum Command {
|
||||
Kill(KillArgs),
|
||||
/// Set the default registry URL persisted in the CLI config.
|
||||
SetRegistry(SetRegistryArgs),
|
||||
/// Inspect or hot-reload the local daemon's `[daemon.policy]` (NAP-237).
|
||||
#[command(subcommand)]
|
||||
Policy(PolicyCmd),
|
||||
}
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
pub enum PolicyCmd {
|
||||
/// Re-read `~/.config/maestro/config.toml` and apply
|
||||
/// `[daemon.policy].allow_inbound` without restarting the daemon. Affects
|
||||
/// only new attach attempts — in-flight sessions keep their connection.
|
||||
Reload,
|
||||
/// Print the daemon's current effective `allow_inbound` list.
|
||||
Show {
|
||||
/// Output as JSON instead of one entry per line.
|
||||
#[arg(long)]
|
||||
json: bool,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, clap::Args)]
|
||||
@@ -212,21 +195,6 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_policy_reload() {
|
||||
let cli = Cli::try_parse_from(["maestro", "policy", "reload"]).unwrap();
|
||||
assert!(matches!(cli.command, Command::Policy(PolicyCmd::Reload)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_policy_show_json() {
|
||||
let cli = Cli::try_parse_from(["maestro", "policy", "show", "--json"]).unwrap();
|
||||
match cli.command {
|
||||
Command::Policy(PolicyCmd::Show { json }) => assert!(json),
|
||||
other => panic!("wrong subcommand: {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_env_without_equals() {
|
||||
let err = Cli::try_parse_from([
|
||||
|
||||
@@ -11,7 +11,8 @@
|
||||
//! * `list` reads the daemon's local catalog mirror (own ∪ peer sessions).
|
||||
//! * `spawn` / `kill` hit the daemon's local session endpoints.
|
||||
//! * `attach` opens the daemon's `WS /api/attach/:id` broker, which picks the
|
||||
//! transport and enforces `allow_inbound` on the target side.
|
||||
//! transport and dials the target (the target verifies the registry-minted
|
||||
//! pair-attach JWT — session binding, not a per-peer allow-list).
|
||||
//!
|
||||
//! If the home daemon is **unreachable** (connection refused / not running),
|
||||
//! every command falls back to the legacy registry path (registry HTTP +
|
||||
@@ -23,10 +24,9 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::attach::{broker_attach_url, parse_detach_key, run_attach, run_attach_url};
|
||||
use crate::cli::{AttachArgs, Command, KillArgs, ListArgs, PolicyCmd, SetRegistryArgs, SpawnArgs};
|
||||
use crate::cli::{AttachArgs, Command, KillArgs, ListArgs, SetRegistryArgs, SpawnArgs};
|
||||
use crate::config::{default_config_path, CliConfig, ConfigError};
|
||||
use crate::http::{HttpError, RegistryClient};
|
||||
use crate::policy::{run_reload as run_policy_reload, run_show as run_policy_show};
|
||||
use crate::sessions::{fetch_connect_info, run_kill, run_list, run_spawn};
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
@@ -40,8 +40,6 @@ pub enum CliError {
|
||||
Attach(#[from] crate::attach::AttachError),
|
||||
#[error("{0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
#[error("{0}")]
|
||||
Policy(#[from] crate::policy::PolicyError),
|
||||
}
|
||||
|
||||
/// Dispatch a parsed top-level subcommand. The umbrella binary passes the
|
||||
@@ -61,7 +59,6 @@ pub async fn dispatch(
|
||||
|
||||
match command {
|
||||
Command::SetRegistry(args) => set_registry(&config_path, args),
|
||||
Command::Policy(sub) => policy(&config_path, sub).await,
|
||||
Command::List(args) => {
|
||||
list(
|
||||
&config_path,
|
||||
@@ -109,16 +106,6 @@ fn set_registry(config_path: &Path, args: SetRegistryArgs) -> Result<(), CliErro
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(clippy::result_large_err)]
|
||||
async fn policy(config_path: &Path, sub: PolicyCmd) -> Result<(), CliError> {
|
||||
let mut stdout = std::io::stdout().lock();
|
||||
match sub {
|
||||
PolicyCmd::Reload => run_policy_reload(config_path, &mut stdout).await?,
|
||||
PolicyCmd::Show { json } => run_policy_show(config_path, json, &mut stdout).await?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(clippy::result_large_err)]
|
||||
async fn list(
|
||||
config_path: &Path,
|
||||
|
||||
@@ -10,7 +10,6 @@ pub mod cli;
|
||||
pub mod config;
|
||||
pub mod dispatch;
|
||||
pub mod http;
|
||||
pub mod policy;
|
||||
pub mod sessions;
|
||||
|
||||
pub use dispatch::{dispatch, CliError};
|
||||
|
||||
@@ -1,127 +0,0 @@
|
||||
//! `maestro policy reload | show` — talk to the local daemon's
|
||||
//! `/api/policy` endpoints (NAP-237).
|
||||
//!
|
||||
//! The local daemon's bind port is read from `[daemon].bind_addr` in the
|
||||
//! unified config; the host is always `127.0.0.1` (these endpoints are
|
||||
//! same-host by design — the daemon's bind may be `0.0.0.0` for attach,
|
||||
//! but operators reload policy from the same machine).
|
||||
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
|
||||
use maestro_config::MaestroConfigFile;
|
||||
use serde::Deserialize;
|
||||
|
||||
const DEFAULT_PORT: u16 = 7070;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum PolicyError {
|
||||
#[error("config: {0}")]
|
||||
Config(#[from] maestro_config::ConfigError),
|
||||
#[error("http: {0}")]
|
||||
Http(#[from] reqwest::Error),
|
||||
#[error("daemon returned {status}: {body}")]
|
||||
DaemonError {
|
||||
status: reqwest::StatusCode,
|
||||
body: String,
|
||||
},
|
||||
#[error("io: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct PolicyView {
|
||||
pub allow_inbound: Vec<String>,
|
||||
}
|
||||
|
||||
pub async fn run_reload(config_path: &Path, out: &mut impl Write) -> Result<(), PolicyError> {
|
||||
let url = format!("{}/api/policy/reload", local_base_url(config_path)?);
|
||||
let resp = crate::http::build_client().post(&url).send().await?;
|
||||
let status = resp.status();
|
||||
if !status.is_success() {
|
||||
let body = resp.text().await.unwrap_or_default();
|
||||
return Err(PolicyError::DaemonError { status, body });
|
||||
}
|
||||
let view: PolicyView = resp.json().await?;
|
||||
writeln!(
|
||||
out,
|
||||
"reloaded — allow_inbound now has {} entr{}",
|
||||
view.allow_inbound.len(),
|
||||
if view.allow_inbound.len() == 1 {
|
||||
"y"
|
||||
} else {
|
||||
"ies"
|
||||
},
|
||||
)?;
|
||||
for entry in &view.allow_inbound {
|
||||
writeln!(out, " {entry}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn run_show(
|
||||
config_path: &Path,
|
||||
json: bool,
|
||||
out: &mut impl Write,
|
||||
) -> Result<(), PolicyError> {
|
||||
let url = format!("{}/api/policy", local_base_url(config_path)?);
|
||||
let resp = crate::http::build_client().get(&url).send().await?;
|
||||
let status = resp.status();
|
||||
if !status.is_success() {
|
||||
let body = resp.text().await.unwrap_or_default();
|
||||
return Err(PolicyError::DaemonError { status, body });
|
||||
}
|
||||
let view: PolicyView = resp.json().await?;
|
||||
if json {
|
||||
writeln!(
|
||||
out,
|
||||
"{}",
|
||||
serde_json::to_string(&view.allow_inbound).unwrap()
|
||||
)?;
|
||||
} else if view.allow_inbound.is_empty() {
|
||||
writeln!(
|
||||
out,
|
||||
"(allow_inbound is empty — every cross-daemon attach is denied)"
|
||||
)?;
|
||||
} else {
|
||||
for entry in &view.allow_inbound {
|
||||
writeln!(out, "{entry}")?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// `http://127.0.0.1:<port>` where `<port>` is `[daemon].bind_addr`'s port
|
||||
/// or `7070` if unset / file missing.
|
||||
fn local_base_url(config_path: &Path) -> Result<String, PolicyError> {
|
||||
let file = MaestroConfigFile::load_or_default(config_path)?;
|
||||
let port = file
|
||||
.daemon
|
||||
.bind_addr
|
||||
.map(|a| a.port())
|
||||
.unwrap_or(DEFAULT_PORT);
|
||||
Ok(format!("http://127.0.0.1:{port}"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn local_base_url_uses_bind_port_or_default() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("config.toml");
|
||||
// missing file → default
|
||||
assert_eq!(local_base_url(&path).unwrap(), "http://127.0.0.1:7070");
|
||||
|
||||
std::fs::write(
|
||||
&path,
|
||||
r#"
|
||||
[daemon]
|
||||
bind_addr = "0.0.0.0:9090"
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(local_base_url(&path).unwrap(), "http://127.0.0.1:9090");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user