//! `swarmctl agent permissions` — the permissions the swarm grants one //! agent, through swarm-controller's `/api/agents/{name}/permissions` routes. //! //! The agent applies its tool permissions at its next turn; the hive running //! it restarts it, after its current turn, when `manage_root_agent` changes. //! Unset leaves the agent on the default set; an empty list grants nothing. //! //! The shape below mirrors the controller's `Permissions`, for the reason //! [`crate::agent`]'s module doc gives. Permission names are checked by the //! controller, which answers 422 for one it does not know. use anyhow::{Context as _, Result}; use serde::{Deserialize, Serialize}; use crate::agent::{parse_ident, send}; use crate::remote::Target; /// One agent's permissions; an absent field is not set at swarm level. #[derive(Debug, Default, PartialEq, Eq, Serialize, Deserialize)] struct Permissions { #[serde(default, skip_serializing_if = "Option::is_none")] permissions: Option>, } fn runtime() -> Result { tokio::runtime::Builder::new_current_thread() .enable_all() .build() .context("starting a tokio runtime for the controller request") } /// The agent's permissions path. The name is checked first, so it cannot /// reshape the URI. fn member(agent: &str) -> Result { let agent = parse_ident(agent, "agent name")?; Ok(format!("/api/agents/{agent}/permissions")) } /// Run `swarmctl agent permissions get`. pub(crate) fn get(target: &Target, agent: &str) -> Result<()> { let uri = member(agent)?; let stored: Permissions = runtime()?.block_on(send( target, hyper::Method::GET, &uri, None::<&()>, "permissions-get", ))?; print!("{}", render(&stored)); Ok(()) } /// Run `swarmctl agent permissions set`: store the given permissions, which /// replace the agent's record whole. pub(crate) fn set(target: &Target, agent: &str, permissions: Vec) -> Result<()> { let uri = member(agent)?; let stored: Permissions = runtime()?.block_on(send( target, hyper::Method::PUT, &uri, Some(&with_permissions(permissions)), "permissions-set", ))?; print!("{}", render(&stored)); Ok(()) } /// The record granting `permissions`. An empty name, as `--permissions ''` /// gives, is dropped, so that spelling grants nothing. fn with_permissions(permissions: Vec) -> Permissions { Permissions { permissions: Some(permissions.into_iter().filter(|c| !c.is_empty()).collect()), } } /// Run `swarmctl agent permissions clear`. pub(crate) fn clear(target: &Target, agent: &str) -> Result<()> { let uri = member(agent)?; runtime()?.block_on(send::<(), ()>( target, hyper::Method::DELETE, &uri, None, "permissions-clear", ))?; println!("cleared; {agent} goes back to the default"); Ok(()) } fn render(p: &Permissions) -> String { let caps = match p.permissions.as_deref() { None => "(not set; the default)".to_owned(), Some([]) => "(none)".to_owned(), Some(caps) => caps.join(", "), }; format!("permissions: {caps}\n") } #[cfg(test)] mod tests { use super::*; #[test] fn get_tells_unset_from_none() { let unset: Permissions = serde_json::from_str("{}").unwrap(); assert_eq!(render(&unset), "permissions: (not set; the default)\n"); let none: Permissions = serde_json::from_str(r#"{"permissions":[]}"#).unwrap(); assert_eq!(render(&none), "permissions: (none)\n"); let ruth: Permissions = serde_json::from_str(r#"{"permissions":["manage_root_agent"]}"#).unwrap(); assert_eq!(render(&ruth), "permissions: manage_root_agent\n"); } /// `--permissions ''` stores an empty list, not one empty name. #[test] fn an_empty_list_flag_grants_nothing() { let set = with_permissions(vec![String::new()]); assert_eq!( serde_json::to_string(&set).unwrap(), r#"{"permissions":[]}"# ); } }