agent capabilities live in a swarm KV record, set from swarmctl
An agent's capabilities move from the hive's meta repo
(`meta/capabilities.json`, injected as `HIVE_CAPABILITIES`) into the
`capabilities` field of its key in a new `agent-permissions` KV bucket,
which swarm-controller alone writes.
- swarm-queue-client: `agent_permissions` (bucket, key, `AgentPermissions`,
direct-get read, the import subject and its wire types).
- swarm-controller: creates the bucket at start; `GET|PUT|DELETE
/api/agents/{name}/permissions` (deny_unknown_fields and unknown
capability ⇒ 422, duplicate ⇒ 400, agent must be declared by a hive);
answers each hive's one-shot import, writing a key only where there is
none and only for agents that hive's wanted state declares.
- swarmctl: `agent permissions get|set|clear`.
- swarm-nats-auth: a hive reads every key of the bucket and sends its
import on its own subject; the controller subscribes to the imports and
may answer them (`responses`).
- hive-c0re: reads each of its agents' keys every 30s into
/var/lib/hyperhive/swarm-agent-permissions.json and restarts an agent,
gracefully, when its capabilities change. Unset is the compiled-in
default (ruth: manage_root_agent; everyone else: none). A failed read
keeps the stored value. On first boot it seeds that file from
meta/capabilities.json, sends the grants to the controller, and deletes
the file once the controller answers.
Deleted: the capabilities.json reader/writer, `HIVE_CAPABILITIES` in the
rendered meta flake, ruth's capability seed, the capabilities half of the
permissions rebuild job (`PermPayload::Capabilities`/`Combined`), the
dashboard's capabilities table and `capabilities_changed` event, and the
now-unused `Capability::description`.
Part of #5170.
This commit is contained in:
parent
fcda1f4a04
commit
f862354e43
47 changed files with 1943 additions and 1146 deletions
|
|
@ -29,6 +29,7 @@ mod agent;
|
|||
mod forge;
|
||||
mod inference;
|
||||
mod login;
|
||||
mod permissions;
|
||||
mod remote;
|
||||
mod schedule;
|
||||
mod users;
|
||||
|
|
@ -290,6 +291,43 @@ enum AgentVerb {
|
|||
#[command(subcommand)]
|
||||
command: InferenceVerb,
|
||||
},
|
||||
/// Show or change the capabilities the swarm grants an agent.
|
||||
///
|
||||
/// The hive running the agent reads them every 30 seconds and, when they
|
||||
/// change, restarts the agent after its current turn. Unset leaves the
|
||||
/// agent on its hive's default: none, except `ruth`, who gets
|
||||
/// `manage_root_agent`.
|
||||
Permissions {
|
||||
#[command(subcommand)]
|
||||
command: PermissionsVerb,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum PermissionsVerb {
|
||||
/// Show the capabilities the swarm grants an agent, or that it sets none.
|
||||
Get(InferenceAgentArgs),
|
||||
/// Replace the capabilities the swarm grants an agent.
|
||||
Set(PermissionsSetArgs),
|
||||
/// Clear the agent's permissions, so its hive's default applies again.
|
||||
Clear(InferenceAgentArgs),
|
||||
}
|
||||
|
||||
#[derive(Args)]
|
||||
struct PermissionsSetArgs {
|
||||
/// Agent name. A hive must declare it.
|
||||
agent: String,
|
||||
/// The capabilities to grant, comma-separated (`manage_root_agent`).
|
||||
/// `--capabilities ''` grants none.
|
||||
#[arg(
|
||||
long,
|
||||
value_name = "CAPABILITIES",
|
||||
value_delimiter = ',',
|
||||
required = true
|
||||
)]
|
||||
capabilities: Vec<String>,
|
||||
#[command(flatten)]
|
||||
socket: ControllerSocketArg,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
|
|
@ -602,6 +640,9 @@ fn main() -> Result<()> {
|
|||
Verb::Agent {
|
||||
command: AgentVerb::Inference { command },
|
||||
} => inference(&remote, command),
|
||||
Verb::Agent {
|
||||
command: AgentVerb::Permissions { command },
|
||||
} => permissions(&remote, command),
|
||||
// Same socket-resolution reasoning as `Create` above.
|
||||
Verb::Forge {
|
||||
command: ForgeVerb::MakeAdmin(args),
|
||||
|
|
@ -671,6 +712,16 @@ fn inference(remote: &RemoteArgs, command: InferenceVerb) -> Result<()> {
|
|||
}
|
||||
}
|
||||
|
||||
fn permissions(remote: &RemoteArgs, command: PermissionsVerb) -> Result<()> {
|
||||
match command {
|
||||
PermissionsVerb::Get(a) => permissions::get(&a.socket.resolve(remote)?, &a.agent),
|
||||
PermissionsVerb::Set(a) => {
|
||||
permissions::set(&a.socket.resolve(remote)?, &a.agent, a.capabilities)
|
||||
}
|
||||
PermissionsVerb::Clear(a) => permissions::clear(&a.socket.resolve(remote)?, &a.agent),
|
||||
}
|
||||
}
|
||||
|
||||
fn schedule(remote: &RemoteArgs, command: ScheduleVerb) -> Result<()> {
|
||||
match command {
|
||||
ScheduleVerb::Add(a) => schedule::add(
|
||||
|
|
|
|||
126
swarmctl/src/permissions.rs
Normal file
126
swarmctl/src/permissions.rs
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
//! `swarmctl agent permissions` — the capabilities the swarm grants one
|
||||
//! agent, through swarm-controller's `/api/agents/{name}/permissions` routes.
|
||||
//!
|
||||
//! The hive running the agent reads its key every 30 seconds and restarts the
|
||||
//! agent, after its current turn, when its capabilities change. Unset leaves
|
||||
//! the agent on its hive's default; an empty list grants nothing.
|
||||
//!
|
||||
//! The shape below mirrors the controller's `Permissions`, for the reason
|
||||
//! [`crate::agent`]'s module doc gives. Capability 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")]
|
||||
capabilities: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
fn runtime() -> Result<tokio::runtime::Runtime> {
|
||||
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<String> {
|
||||
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 capabilities, which
|
||||
/// replace the agent's record whole.
|
||||
pub(crate) fn set(target: &Target, agent: &str, capabilities: Vec<String>) -> Result<()> {
|
||||
let uri = member(agent)?;
|
||||
let stored: Permissions = runtime()?.block_on(send(
|
||||
target,
|
||||
hyper::Method::PUT,
|
||||
&uri,
|
||||
Some(&with_capabilities(capabilities)),
|
||||
"permissions-set",
|
||||
))?;
|
||||
print!("{}", render(&stored));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The record granting `capabilities`. An empty name, as `--capabilities ''`
|
||||
/// gives, is dropped, so that spelling grants nothing.
|
||||
fn with_capabilities(capabilities: Vec<String>) -> Permissions {
|
||||
Permissions {
|
||||
capabilities: Some(capabilities.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 its hive's default");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn render(p: &Permissions) -> String {
|
||||
let caps = match p.capabilities.as_deref() {
|
||||
None => "(not set; the hive's default)".to_owned(),
|
||||
Some([]) => "(none)".to_owned(),
|
||||
Some(caps) => caps.join(", "),
|
||||
};
|
||||
format!("capabilities: {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),
|
||||
"capabilities: (not set; the hive's default)\n"
|
||||
);
|
||||
let none: Permissions = serde_json::from_str(r#"{"capabilities":[]}"#).unwrap();
|
||||
assert_eq!(render(&none), "capabilities: (none)\n");
|
||||
let ruth: Permissions =
|
||||
serde_json::from_str(r#"{"capabilities":["manage_root_agent"]}"#).unwrap();
|
||||
assert_eq!(render(&ruth), "capabilities: manage_root_agent\n");
|
||||
}
|
||||
|
||||
/// `--capabilities ''` stores an empty list, not one empty name.
|
||||
#[test]
|
||||
fn an_empty_list_flag_grants_nothing() {
|
||||
let set = with_capabilities(vec![String::new()]);
|
||||
assert_eq!(
|
||||
serde_json::to_string(&set).unwrap(),
|
||||
r#"{"capabilities":[]}"#
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue