diff --git a/docs/process/conventions.md b/docs/process/conventions.md index 9739928b4..36551b015 100644 --- a/docs/process/conventions.md +++ b/docs/process/conventions.md @@ -311,17 +311,6 @@ the default (`Permission::DEFAULT`): `messaging`, `meta`, `inbox`, none. swarm-controller logs every refused `PUT` or `DELETE` on that route at `warn` as `agent permissions: request refused`, with the fields `op`, `agent`, `status` and `detail`. -**Internet flag** — the record also carries `unrestricted_internet`, a -boolean that isn't a `Permission`. An absent value means on, so every agent -that existed before the field keeps the open access it had. `POST /api/agents` -writes `false` for an agent that has no record, unless the request names a -value. Over an existing record (a re-create) it keeps the stored list and flag, -an absent flag included, and a value in the request wins. A `PUT` that leaves -the field out and a `DELETE` (`swarmctl agent permissions clear`) also keep the -stored value; a `DELETE` on an agent that has one rewrites the record to just -that value. Read it with -`swarm_queue_client::agent_permissions::unrestricted`. Nothing acts on it yet. - **How a change applies** — - The agent's harness reads its own key every 10 seconds and builds every turn's `--tools`, `--allowedTools` and MCP config from the last value, so a diff --git a/docs/tools/swarmctl-cli.md b/docs/tools/swarmctl-cli.md index 1f948028d..e032ad98b 100644 --- a/docs/tools/swarmctl-cli.md +++ b/docs/tools/swarmctl-cli.md @@ -221,7 +221,7 @@ The agent applies its tool permissions at its next turn; the hive running it res ###### **Subcommands:** * `get` — Show the permissions the swarm grants an agent, or that it sets none -* `set` — Replace the permissions the swarm grants an agent; the internet flag stays unless given +* `set` — Replace the permissions the swarm grants an agent * `clear` — Clear the agent's permissions, so the default applies again @@ -246,7 +246,7 @@ Show the permissions the swarm grants an agent, or that it sets none ## `swarmctl agent permissions set` -Replace the permissions the swarm grants an agent; the internet flag stays unless given +Replace the permissions the swarm grants an agent **Usage:** `swarmctl agent permissions set [OPTIONS] --permissions ` @@ -257,10 +257,6 @@ Replace the permissions the swarm grants an agent; the internet flag stays unles ###### **Options:** * `--permissions ` — The permissions to grant, comma-separated: any of `messaging`, `meta`, `inbox`, `execution`, `web_tools`, `manage_root_agent`. `--permissions ''` grants none -* `--unrestricted-internet ` — Whether the agent's traffic may leave the host unfiltered. Left out, the stored value stays - - Possible values: `true`, `false` - * `--controller-socket ` — swarm-controller's unix socket. Supplied by the nix module that installs this binary, from the same `socketPath` option the daemon binds; falls back to `SWARM_CONTROLLER_SOCKET`. diff --git a/hive-c0re/src/workers/permissions_import.rs b/hive-c0re/src/workers/permissions_import.rs index 0e886dfca..bacf8a378 100644 --- a/hive-c0re/src/workers/permissions_import.rs +++ b/hive-c0re/src/workers/permissions_import.rs @@ -142,7 +142,6 @@ fn seed_local_at(meta: &Path, stored: &Path) -> Result<()> { agent, AgentPermissions { permissions: Some(perms), - ..AgentPermissions::default() }, ) }) diff --git a/swarm-controller-api/src/agents.rs b/swarm-controller-api/src/agents.rs index 4b3d5550c..14449e08a 100644 --- a/swarm-controller-api/src/agents.rs +++ b/swarm-controller-api/src/agents.rs @@ -76,17 +76,11 @@ pub struct CreateAgentRequest { /// /api/agents/{name}/inference` takes them. Absent as `limits`. #[serde(default, skip_serializing_if = "Option::is_none")] pub inference: Option, - /// The agent's permissions, stored before the job is queued. Left out, an - /// existing record keeps its list and otherwise the agent holds the default - /// set. + /// The agent's permissions, stored before the job is queued. Left out, the + /// agent holds the default set. #[serde(default, skip_serializing_if = "Option::is_none")] #[cfg_attr(feature = "schema", schema(value_type = Option>))] pub permissions: Option>, - /// Whether the agent's traffic leaves the host unfiltered. Left out, an - /// agent with no stored value is created with `false`, and one that has a - /// record (a re-create) keeps its value. - #[serde(default, skip_serializing_if = "Option::is_none")] - pub unrestricted_internet: Option, /// The archetype to build the agent from, one `GET /api/archetypes` /// lists. The agent then gets no `agent-configs/` repo: its hive /// builds it from the archetype's module. Absent is a custom agent. @@ -122,7 +116,6 @@ mod tests { limits: None, inference: None, permissions: None, - unrestricted_internet: None, archetype: None, agents_md: None, }; diff --git a/swarm-controller-api/src/permissions.rs b/swarm-controller-api/src/permissions.rs index 94d2ee9ee..a4ce7fcc7 100644 --- a/swarm-controller-api/src/permissions.rs +++ b/swarm-controller-api/src/permissions.rs @@ -5,8 +5,7 @@ use hive_types::Permission; use serde::{Deserialize, Serialize}; /// One agent's permissions. A field left out is not set by the swarm, and the -/// compiled-in default applies; an empty list grants nothing. A -/// `PUT` that leaves out `unrestricted_internet` keeps the stored value. +/// compiled-in default applies; an empty list grants nothing. #[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)] #[cfg_attr(feature = "schema", derive(utoipa::ToSchema))] #[serde(deny_unknown_fields)] @@ -19,11 +18,6 @@ pub struct Permissions { schema(value_type = Option>, example = json!(["messaging", "meta", "inbox", "execution"])) )] pub permissions: Option>, - /// Whether the agent's traffic leaves the host unfiltered. Absent in a - /// `GET` means the swarm never set it, which counts as on. Absent in a - /// `PUT` keeps the stored value. - #[serde(default, skip_serializing_if = "Option::is_none")] - pub unrestricted_internet: Option, } #[cfg(test)] @@ -38,21 +32,12 @@ mod tests { ( Permissions { permissions: Some(vec![]), - unrestricted_internet: None, }, r#"{"permissions":[]}"#, ), - ( - Permissions { - permissions: None, - unrestricted_internet: Some(false), - }, - r#"{"unrestricted_internet":false}"#, - ), ( Permissions { permissions: Some(vec![Permission::Messaging, Permission::ManageRootAgent]), - unrestricted_internet: None, }, r#"{"permissions":["messaging","manage_root_agent"]}"#, ), diff --git a/swarm-controller/src/agent_permissions.rs b/swarm-controller/src/agent_permissions.rs index 7aa6d95fe..cd996bdfb 100644 --- a/swarm-controller/src/agent_permissions.rs +++ b/swarm-controller/src/agent_permissions.rs @@ -36,13 +36,14 @@ use crate::wanted::WantedWriter; /// `wanted::MAX_ATTEMPTS`. const MAX_ATTEMPTS: usize = 5; -/// The permissions to store for `req`, whose list is already checked. The list -/// replaces the stored one; `unrestricted_internet` left out keeps the one in `current`. -fn into_stored(req: Permissions, current: &AgentPermissions) -> AgentPermissions { - AgentPermissions { - permissions: req.permissions, - unrestricted_internet: req.unrestricted_internet.or(current.unrestricted_internet), +/// The permissions to store, or why they are refused. +fn into_stored(req: Permissions) -> Result { + if let Some(perms) = &req.permissions { + unique(perms)?; } + Ok(AgentPermissions { + permissions: req.permissions, + }) } /// Refuse a list that names a permission twice. @@ -61,7 +62,6 @@ pub(crate) fn unique(perms: &[Permission]) -> Result<(), String> { fn view(v: AgentPermissions) -> Permissions { Permissions { permissions: v.permissions, - unrestricted_internet: v.unrestricted_internet, } } @@ -127,8 +127,7 @@ pub(crate) async fn get_agent_permissions( /// Set the permissions the swarm grants one agent. /// -/// The permission list replaces the stored one; `unrestricted_internet` left -/// out keeps the stored flag. The hive running the agent reads +/// The value replaces the stored one whole. The hive running the agent reads /// it within 30 seconds and restarts the agent, after its current turn, when /// `manage_root_agent` changed; the agent applies the rest at its next turn. #[utoipa::path( @@ -157,9 +156,8 @@ pub(crate) async fn put_agent_permissions( let Json(req) = req.map_err(|rejection| refuse(error_problem(rejection.status(), &rejection.body_text())))?; let agent = agent_name(&name).map_err(|r| refuse(refused(r)))?; - if let Some(perms) = &req.permissions { - unique(perms).map_err(|why| refuse(error_problem(StatusCode::BAD_REQUEST, &why)))?; - } + let value = + into_stored(req).map_err(|why| refuse(error_problem(StatusCode::BAD_REQUEST, &why)))?; let client = queue(&state).map_err(|r| refuse(refused(r)))?; if !live(&state).await.map_err(refuse)?.contains(&agent) { return Err(refuse(error_problem( @@ -172,7 +170,6 @@ pub(crate) async fn put_agent_permissions( let (current, revision, undecodable) = read_or_default::(&store, &agent) .await .map_err(|e| queue_problem(&e))?; - let value = into_stored(req.clone(), ¤t); if (current == value && !undecodable) || write(&store, &agent, &value, revision) .await @@ -192,8 +189,7 @@ pub(crate) async fn put_agent_permissions( path = "/api/agents/{name}/permissions", params(("name" = String, Path, description = "agent name")), responses( - (status = 204, description = "cleared; the hive's default applies again, and a stored \ - `unrestricted_internet` is kept"), + (status = 204, description = "cleared; the hive's default applies again"), (status = 400, description = "not an identifier (problem+json)", body = String), (status = 503, description = "no swarm queue is configured on this host, or it is \ not connected (problem+json)", body = String), @@ -210,38 +206,17 @@ pub(crate) async fn delete_agent_permissions( let agent = agent_name(&name).map_err(|r| refuse(refused(r)))?; let client = queue(&state).map_err(|r| refuse(refused(r)))?; let store = open_or_create(&client).await?; - for _ in 0..MAX_ATTEMPTS { - let (current, revision, _) = read_or_default::(&store, &agent) - .await - .map_err(|e| queue_problem(&e))?; - // Only the flag outlives a clear: dropping it would turn an agent - // created with `false` on. - let kept = AgentPermissions { - unrestricted_internet: current.unrestricted_internet, - ..AgentPermissions::default() - }; - if kept == AgentPermissions::default() { - store - .delete(&agent) - .await - .map_err(|e| queue_problem(&anyhow::Error::new(e)))?; - } else if !write(&store, &agent, &kept, revision) - .await - .map_err(|e| queue_problem(&e))? - { - continue; - } - tracing::info!(%agent, "cleared agent permissions"); - return Ok(StatusCode::NO_CONTENT); - } - Err(queue_problem(&anyhow::anyhow!( - "gave up clearing the permissions of {agent} after {MAX_ATTEMPTS} conflicting writes" - ))) + store + .delete(&agent) + .await + .map_err(|e| queue_problem(&anyhow::Error::new(e)))?; + tracing::info!(%agent, "cleared agent permissions"); + Ok(StatusCode::NO_CONTENT) } /// Delete a destroyed agent's key. A name is free again once its agent is -/// destroyed, and a create keeps the list and flag of a record it finds, so a -/// key left here would carry over to the next agent of that name. +/// destroyed, and a create left on the default stores nothing, so a key left +/// here would hand the next agent of that name this one's grant. /// /// Like the credential revoke it runs beside, a failure is logged and does /// not stop the destroy; declaring the agent destroyed again re-runs it. @@ -262,8 +237,8 @@ pub(crate) async fn clear_on_destroy(state: &AppState, hive: &str, agent: &str) agent, error = %format!("{e:#}"), "clearing this agent's permissions FAILED; it was destroyed anyway, so a new \ - agent of the same name created later keeps this record's list and flag. \ - Declaring the agent destroyed again re-runs this." + agent of the same name created on the default gets its grant. Declaring the \ + agent destroyed again re-runs this." ), } } @@ -292,51 +267,20 @@ pub(crate) async fn get_permission_catalogue() -> Json { }) } -/// The value `POST /api/agents` asks [`store`] to merge in, or why it is -/// refused. A field the request leaves out stays unset here: [`store`] keeps -/// the stored value, and gives an agent with no stored flag `false`. +/// The value `POST /api/agents` stores for `perms`, or why it is refused. /// The agent is not declared on its hive yet, so the "declared by a hive" /// check of [`put_agent_permissions`] does not apply. -pub(crate) fn at_create( - perms: Option>, - unrestricted_internet: Option, -) -> Result { - if let Some(perms) = &perms { - unique(perms)?; - } +pub(crate) fn at_create(perms: Vec) -> Result { + unique(&perms)?; Ok(AgentPermissions { - permissions: perms, - unrestricted_internet, + permissions: Some(perms), }) } -/// What a create leaves in the key: the request's fields win, and a field it -/// leaves out keeps the stored value. Only an agent with no record at all -/// (`held` false) gets `unrestricted_internet: false`, so re-creating an -/// existing agent keeps its list and its flag, a legacy record's absent flag -/// included. -fn merged_at_create( - value: &AgentPermissions, - current: &AgentPermissions, - held: bool, -) -> AgentPermissions { - AgentPermissions { - permissions: value - .permissions - .clone() - .or_else(|| current.permissions.clone()), - unrestricted_internet: value - .unrestricted_internet - .or(current.unrestricted_internet) - .or_else(|| (!held).then_some(false)), - } -} - /// What [`store`] replaced, for [`restore`]; `None` when it wrote nothing. pub(crate) type Replaced = Option>; -/// Merge `value` into `agent`'s stored permissions, as [`merged_at_create`] -/// says. +/// Replace `agent`'s stored permissions with `value`. pub(crate) async fn store( client: &async_nats::Client, agent: &str, @@ -347,18 +291,17 @@ pub(crate) async fn store( let (current, revision, undecodable) = read_or_default::(&store, agent) .await .map_err(|e| queue_problem(&e))?; + if current == *value && !undecodable { + tracing::info!(%agent, ?value, "set agent permissions at create"); + return Ok(None); + } // A deleted key reads as present at its marker's revision, so ask - // whether it holds a value. + // whether it holds a value before keeping one to put back. let held = store .get(agent) .await .map_err(|e| queue_problem(&anyhow::Error::new(e)))? .is_some(); - let value = &merged_at_create(value, ¤t, held); - if current == *value && !undecodable { - tracing::info!(%agent, ?value, "set agent permissions at create"); - return Ok(None); - } if let Some(written) = write_revision(&store, agent, value, revision) .await .map_err(|e| queue_problem(&e))? @@ -541,7 +484,6 @@ async fn seed_into( perms.push(Permission::ManageRootAgent); let value = AgentPermissions { permissions: Some(perms), - ..AgentPermissions::default() }; let written = write(store, SEEDED_AGENT, &value, revision).await?; if written { @@ -569,7 +511,6 @@ async fn import_into( } let value = AgentPermissions { permissions: Some(caps), - ..AgentPermissions::default() }; if write(store, &agent, &value, None).await? { written.push(agent); @@ -610,7 +551,6 @@ mod tests { fn granted() -> Permissions { Permissions { permissions: Some(vec![Permission::ManageRootAgent]), - unrestricted_internet: None, } } @@ -654,7 +594,6 @@ mod tests { let none = Permissions { permissions: Some(Vec::new()), - unrestricted_internet: None, }; let _ = put(&state, "atlas", none.clone()).await.expect("set"); assert_eq!( @@ -670,127 +609,6 @@ mod tests { assert_eq!(get(&state, "atlas").await, Permissions::default()); } - /// An agent stored with `unrestricted_internet: false` keeps it through a - /// `PUT` that leaves it out and through `DELETE`; a `PUT` that names it - /// changes it. Without the flag, `DELETE` still removes the key. - #[tokio::test] - async fn put_and_delete_keep_the_internet_flag_unless_named() { - let nats = TestNats::start(); - let client = nats.client().await; - let state = crate::agent_limits::tests::declared(&client).await; - let off = r#"{"unrestricted_internet":false}"#; - crate::agent_permissions::store( - &client, - "atlas", - &super::at_create(None, None).expect("valid"), - ) - .await - .expect("created"); - assert_eq!(published(&client, "atlas").await.as_deref(), Some(off)); - - let stored = put(&state, "atlas", granted()).await.expect("set").0; - assert_eq!(stored.unrestricted_internet, Some(false)); - assert_eq!( - published(&client, "atlas").await.as_deref(), - Some(r#"{"permissions":["manage_root_agent"],"unrestricted_internet":false}"#) - ); - - assert_eq!( - delete_agent_permissions(State(state.clone()), Path("atlas".to_owned())).await, - Ok(StatusCode::NO_CONTENT) - ); - assert_eq!(published(&client, "atlas").await.as_deref(), Some(off)); - assert_eq!( - get(&state, "atlas").await.unrestricted_internet, - Some(false) - ); - - let on = Permissions { - unrestricted_internet: Some(true), - ..granted() - }; - let stored = put(&state, "atlas", on).await.expect("set").0; - assert_eq!(stored.unrestricted_internet, Some(true)); - assert_eq!( - delete_agent_permissions(State(state.clone()), Path("atlas".to_owned())).await, - Ok(StatusCode::NO_CONTENT) - ); - assert_eq!( - published(&client, "atlas").await.as_deref(), - Some(r#"{"unrestricted_internet":true}"#) - ); - - // Control: an agent that never had the flag has nothing left after a - // clear, and a `PUT` does not invent one. - swarm_queue_client::agent_permissions::open_or_create(&client) - .await - .expect("bucket") - .put("atlas", "{}".into()) - .await - .expect("an older record"); - let _ = put(&state, "atlas", granted()).await.expect("set"); - assert_eq!(get(&state, "atlas").await.unrestricted_internet, None); - assert_eq!( - delete_agent_permissions(State(state.clone()), Path("atlas".to_owned())).await, - Ok(StatusCode::NO_CONTENT) - ); - assert_eq!(published(&client, "atlas").await, None); - } - - /// A create over an existing record merges into it: a list and a flag - /// that were set stay, and a record without a flag (an older agent) - /// stays without one. Control: with no record, the flag is `false`. - #[tokio::test] - async fn a_re_create_keeps_the_record_it_finds() { - let nats = TestNats::start(); - let client = nats.client().await; - let state = crate::agent_limits::tests::declared(&client).await; - let create = |permissions, unrestricted_internet| { - crate::routes::agents::create_agent( - State(state.clone()), - axum::Json(swarm_controller_api::agents::CreateAgentRequest { - name: "atlas".to_owned(), - hive: "pr1ma".to_owned(), - limits: None, - inference: None, - permissions, - unrestricted_internet, - archetype: None, - agents_md: None, - }), - ) - }; - - let on = Permissions { - unrestricted_internet: Some(true), - ..granted() - }; - let _ = put(&state, "atlas", on).await.expect("set"); - let _ = create(None, None).await.expect("re-created"); - assert_eq!( - published(&client, "atlas").await.as_deref(), - Some(r#"{"permissions":["manage_root_agent"],"unrestricted_internet":true}"#) - ); - - // A value in the request wins over the stored one. - let _ = create(None, Some(false)).await.expect("re-created"); - assert_eq!( - published(&client, "atlas").await.as_deref(), - Some(r#"{"permissions":["manage_root_agent"],"unrestricted_internet":false}"#) - ); - - // A record from before the flag existed stays without one. - let older = r#"{"permissions":["manage_root_agent"]}"#; - swarm_queue_client::agent_permissions::open_or_create(&client) - .await - .expect("bucket") - .put("atlas", older.into()) - .await - .expect("an older record"); - let _ = create(None, None).await.expect("re-created"); - assert_eq!(published(&client, "atlas").await.as_deref(), Some(older)); - } - /// A name no hive declares, or a name that is not an identifier, is a 400 /// and writes nothing. #[tokio::test] @@ -1085,7 +903,7 @@ mod tests { let nats = TestNats::start(); let client = nats.client().await; let state = crate::agent_limits::tests::declared(&client).await; - let create = |name: &str, permissions, unrestricted_internet| { + let create = |name: &str, permissions| { crate::routes::agents::create_agent( State(state.clone()), axum::Json(swarm_controller_api::agents::CreateAgentRequest { @@ -1094,7 +912,6 @@ mod tests { limits: None, inference: None, permissions, - unrestricted_internet, archetype: None, agents_md: None, }), @@ -1104,7 +921,6 @@ mod tests { let problem = create( "iris", Some(vec![Permission::Messaging, Permission::Messaging]), - None, ) .await .expect_err("a duplicate is refused"); @@ -1119,31 +935,21 @@ mod tests { .count(); assert_eq!(queued, 0, "nothing is queued for a refused create"); - let _ = create("iris", Some(vec![Permission::Messaging]), None) + let _ = create("iris", Some(vec![Permission::Messaging])) .await .expect("created"); assert_eq!( published(&client, "iris").await.as_deref(), - Some(r#"{"permissions":["messaging"],"unrestricted_internet":false}"#) + Some(r#"{"permissions":["messaging"]}"#) ); - // No `permissions` still writes the record: the explicit `false`. - let _ = create("damocles", None, None).await.expect("created"); - assert_eq!( - published(&client, "damocles").await.as_deref(), - Some(r#"{"unrestricted_internet":false}"#) - ); - - // The request can name the flag. - let _ = create("hermes", None, Some(true)).await.expect("created"); - assert_eq!( - published(&client, "hermes").await.as_deref(), - Some(r#"{"unrestricted_internet":true}"#) - ); + // Control: no `permissions` writes no key, so the default applies. + let _ = create("damocles", None).await.expect("created"); + assert_eq!(published(&client, "damocles").await, None); } /// Destroying an agent deletes its key, so a new agent of the same name - /// created on the default gets only its own record, not the old grant. + /// created on the default gets the default, not the old grant. #[tokio::test] async fn a_recreated_agent_does_not_inherit_the_destroyed_ones_grant() { let nats = TestNats::start(); @@ -1168,7 +974,6 @@ mod tests { limits: None, inference: None, permissions: None, - unrestricted_internet: None, archetype: None, agents_md: None, }), @@ -1176,10 +981,8 @@ mod tests { .await .expect("recreated"); - assert_eq!( - published(&client, "atlas").await.as_deref(), - Some(r#"{"unrestricted_internet":false}"#) - ); + assert_eq!(published(&client, "atlas").await, None); + assert_eq!(get(&state, "atlas").await, Permissions::default()); } #[tokio::test] diff --git a/swarm-controller/src/agents_md_initial.rs b/swarm-controller/src/agents_md_initial.rs index 54b584c0a..a0c70df8e 100644 --- a/swarm-controller/src/agents_md_initial.rs +++ b/swarm-controller/src/agents_md_initial.rs @@ -148,7 +148,6 @@ mod tests { limits: None, inference: None, permissions: None, - unrestricted_internet: None, archetype: None, agents_md, } diff --git a/swarm-controller/src/jobq.rs b/swarm-controller/src/jobq.rs index 47b103ea1..08f736622 100644 --- a/swarm-controller/src/jobq.rs +++ b/swarm-controller/src/jobq.rs @@ -1787,7 +1787,7 @@ pub(crate) fn queue_agent_renewals( #[cfg(test)] mod tests { use super::*; - use crate::routes::tests::state_with_roster_queued; + use crate::routes::tests::state_with_roster; pub(crate) fn changed(agent: &str, rev: &str, pr: u64) -> crate::config_pr::ChangedConfigPr { crate::config_pr::ChangedConfigPr { @@ -2219,7 +2219,7 @@ mod tests { async fn the_deploy_waits_for_the_pause_to_be_declared() { use hive_jobq_wire::WireNode as _; - let (_nats, state, sched) = state_with_roster_queued().await; + let (state, sched) = state_with_roster(); let _queued = crate::routes::agents::create_agent( axum::extract::State(state), axum::Json(swarm_controller_api::agents::CreateAgentRequest { @@ -2228,7 +2228,6 @@ mod tests { limits: None, inference: None, permissions: None, - unrestricted_internet: None, archetype: None, agents_md: None, }), @@ -3032,7 +3031,7 @@ mod tests { async fn the_deploy_waits_for_the_mint_and_is_not_cancelled_by_it() { use hive_jobq_wire::WireNode as _; - let (_nats, state, sched) = state_with_roster_queued().await; + let (state, sched) = state_with_roster(); let _queued = crate::routes::agents::create_agent( axum::extract::State(state), axum::Json(swarm_controller_api::agents::CreateAgentRequest { @@ -3041,7 +3040,6 @@ mod tests { limits: None, inference: None, permissions: None, - unrestricted_internet: None, archetype: None, agents_md: None, }), @@ -3085,7 +3083,7 @@ mod tests { async fn the_forge_token_mint_follows_the_user_and_does_not_block_the_deploy() { use hive_jobq_wire::WireNode as _; - let (_nats, state, sched) = state_with_roster_queued().await; + let (state, sched) = state_with_roster(); let _queued = crate::routes::agents::create_agent( axum::extract::State(state), axum::Json(swarm_controller_api::agents::CreateAgentRequest { @@ -3094,7 +3092,6 @@ mod tests { limits: None, inference: None, permissions: None, - unrestricted_internet: None, archetype: None, agents_md: None, }), @@ -3146,7 +3143,7 @@ mod tests { async fn the_docs_grant_follows_the_user_and_is_not_a_deploy_parent() { use hive_jobq_wire::WireNode as _; - let (_nats, state, sched) = state_with_roster_queued().await; + let (state, sched) = state_with_roster(); let _queued = crate::routes::agents::create_agent( axum::extract::State(state), axum::Json(swarm_controller_api::agents::CreateAgentRequest { @@ -3155,7 +3152,6 @@ mod tests { limits: None, inference: None, permissions: None, - unrestricted_internet: None, archetype: None, agents_md: None, }), @@ -3354,7 +3350,7 @@ mod tests { async fn the_matrix_account_mint_is_a_root_and_does_not_block_the_deploy() { use hive_jobq_wire::WireNode as _; - let (_nats, state, sched) = state_with_roster_queued().await; + let (state, sched) = state_with_roster(); let _queued = crate::routes::agents::create_agent( axum::extract::State(state), axum::Json(swarm_controller_api::agents::CreateAgentRequest { @@ -3363,7 +3359,6 @@ mod tests { limits: None, inference: None, permissions: None, - unrestricted_internet: None, archetype: None, agents_md: None, }), diff --git a/swarm-controller/src/routes/agents.rs b/swarm-controller/src/routes/agents.rs index 7f9836fdc..2dfea217b 100644 --- a/swarm-controller/src/routes/agents.rs +++ b/swarm-controller/src/routes/agents.rs @@ -646,16 +646,21 @@ pub(crate) async fn create_agent( // reads these by agent name when it first runs the agent. A failure from // here on puts back what was stored, so a refused creation leaves no // settings behind for the name. - let stored = StoredSettings::store( - &settings_queue, - &agent, - settings.limits.as_ref(), - settings.inference.as_ref(), - Some(&settings.permissions), - settings.archetype.as_ref(), - settings.agents_md.as_deref(), - ) - .await?; + let stored = match &settings_queue { + Some(client) => { + StoredSettings::store( + client, + &agent, + settings.limits.as_ref(), + settings.inference.as_ref(), + settings.permissions.as_ref(), + settings.archetype.as_ref(), + settings.agents_md.as_deref(), + ) + .await? + } + None => StoredSettings::default(), + }; let queued = { let mut sched = state .jobq @@ -668,7 +673,9 @@ pub(crate) async fn create_agent( let ids = match queued { Ok(ids) => ids, Err(e) => { - stored.roll_back(&settings_queue, &agent).await; + if let Some(client) = &settings_queue { + stored.roll_back(client, &agent).await; + } return Err(error_problem( axum::http::StatusCode::INTERNAL_SERVER_ERROR, &e.to_string(), @@ -688,7 +695,7 @@ pub(crate) async fn create_agent( struct CreateSettings { limits: Option, inference: Option, - permissions: swarm_wire::agent_permissions::AgentPermissions, + permissions: Option, archetype: Option, agents_md: Option, } @@ -702,7 +709,6 @@ impl CreateSettings { limits, inference, permissions, - unrestricted_internet, archetype, agents_md, .. @@ -719,24 +725,35 @@ impl CreateSettings { Ok(Self { limits: limits.as_ref().map(agent_limits::into_stored).transpose()?, inference: inference.map(inference::into_stored).transpose()?, - permissions: agent_permissions::at_create(permissions, unrestricted_internet)?, + permissions: permissions.map(agent_permissions::at_create).transpose()?, archetype, agents_md, }) } - /// The queue to store the settings through: the permissions are always - /// stored, so it is always needed. An archetype is refused here unless the - /// stored list holds it. + /// Whether any setting is given. + fn any(&self) -> bool { + self.limits.is_some() + || self.inference.is_some() + || self.permissions.is_some() + || self.archetype.is_some() + || self.agents_md.is_some() + } + + /// The queue to store the settings through, `None` when none is given. + /// An archetype is refused here unless the stored list holds it. async fn queue( &self, state: &AppState, - ) -> Result { + ) -> Result, problem_details::ProblemDetails> { + if !self.any() { + return Ok(None); + } let client = schedules::queue(state).map_err(schedules::refused)?; if let Some(archetype) = &self.archetype { agent_archetype::check(&client, archetype).await?; } - Ok(client) + Ok(Some(client)) } } @@ -1142,7 +1159,7 @@ pub(crate) async fn get_agent_icon( #[cfg(test)] mod tests { use super::*; - use crate::routes::tests::{SharedSched, state_with_roster, state_with_roster_queued}; + use crate::routes::tests::{SharedSched, state_with_roster}; /// A swarm with no identity bridge cannot answer the roster, and must /// say so rather than answer `[]`. @@ -1227,7 +1244,6 @@ mod tests { limits: None, inference: None, permissions: None, - unrestricted_internet: None, archetype: None, agents_md: None, }), @@ -1257,7 +1273,7 @@ mod tests { /// could equally mean the endpoint refuses everything. #[tokio::test] async fn a_hive_in_the_roster_is_accepted_and_queues_the_graph() { - let (_nats, state, sched) = state_with_roster_queued().await; + let (state, sched) = state_with_roster(); // The response is bound rather than asserted on: node ids start at // zero, so every property I reached for first ("> 0") was a claim @@ -1271,7 +1287,6 @@ mod tests { limits: None, inference: None, permissions: None, - unrestricted_internet: None, archetype: None, agents_md: None, }), @@ -1320,7 +1335,7 @@ mod tests { (r#"{"model":"-opus"}"#, "inference"), ]; for (value, field) in bad { - let (_nats, state, sched) = state_with_roster_queued().await; + let (state, sched) = state_with_roster(); let body = format!(r#"{{"name":"atlas","hive":"pr1ma","{field}":{value}}}"#); let req: CreateAgentRequest = serde_json::from_str(&body).expect("body parses"); let err = super::create_agent(axum::extract::State(state), axum::Json(req)) @@ -1477,11 +1492,8 @@ mod tests { .await .expect("a bucket that refuses any value"); let state = crate::agent_limits::tests::state(&client); - let earlier = crate::agent_permissions::at_create( - Some(vec![hive_types::Permission::Messaging]), - None, - ) - .expect("valid"); + let earlier = crate::agent_permissions::at_create(vec![hive_types::Permission::Messaging]) + .expect("valid"); crate::agent_permissions::store(&client, "atlas", &earlier) .await .expect("an earlier grant"); @@ -1514,7 +1526,7 @@ mod tests { |value: Option| value.map(|v| String::from_utf8_lossy(&v).into_owned()); assert_eq!( held(bucket.get("atlas").await.expect("read")).as_deref(), - Some(r#"{"permissions":["messaging"],"unrestricted_internet":false}"#), + Some(r#"{"permissions":["messaging"]}"#), "the earlier grant comes back" ); assert_eq!( @@ -1552,12 +1564,8 @@ mod tests { } /// `state_with_roster` with a second hive, `sec0nd`. - async fn state_with_two_hives() -> ( - crate::test_nats::TestNats, - crate::routes::AppState, - SharedSched, - ) { - let (nats, state, sched) = state_with_roster_queued().await; + fn state_with_two_hives() -> (crate::routes::AppState, SharedSched) { + let (state, sched) = state_with_roster(); let mut hives = (*state.hives).clone(); hives.push(HiveEntry { name: "sec0nd".to_owned(), @@ -1568,7 +1576,7 @@ mod tests { hives: std::sync::Arc::new(hives), ..state }; - (nats, state, sched) + (state, sched) } pub(crate) async fn create( @@ -1584,7 +1592,6 @@ mod tests { limits: None, inference: None, permissions: None, - unrestricted_internet: None, archetype: None, agents_md: None, }), @@ -1607,7 +1614,7 @@ mod tests { /// wanted-state read alone would miss. #[tokio::test] async fn a_name_placed_on_another_hive_is_refused_before_anything_is_queued() { - let (_nats, state, sched) = state_with_two_hives().await; + let (state, sched) = state_with_two_hives(); create(&state, "atlas", "pr1ma") .await .expect("the first creation of a name must be accepted"); @@ -1636,7 +1643,7 @@ mod tests { /// that agent being re-created, and queues again. #[tokio::test] async fn re_creating_an_agent_on_its_own_hive_is_accepted() { - let (_nats, state, sched) = state_with_two_hives().await; + let (state, sched) = state_with_two_hives(); create(&state, "atlas", "pr1ma") .await .expect("the first creation of a name must be accepted"); @@ -1652,7 +1659,7 @@ mod tests { /// "placed nowhere", and queues nothing. #[tokio::test] async fn an_unreadable_wanted_state_refuses_creation() { - let (_nats, state, sched) = state_with_two_hives().await; + let (state, sched) = state_with_two_hives(); let state = crate::routes::AppState { wanted: Some(std::sync::Arc::new(wanted::WantedWriter::new( disconnected_client().await, @@ -2091,7 +2098,7 @@ mod tests { agent: &str, wanted_state: swarm_wire::wanted::AgentState, ) -> problem_details::ProblemDetails { - let (_nats, state, _sched) = state_with_two_hives().await; + let (state, _sched) = state_with_two_hives(); let state = crate::routes::AppState { wanted: Some(std::sync::Arc::new(wanted::WantedWriter::new( disconnected_client().await, diff --git a/swarm-controller/src/routes/mod.rs b/swarm-controller/src/routes/mod.rs index 003d677be..47e55b746 100644 --- a/swarm-controller/src/routes/mod.rs +++ b/swarm-controller/src/routes/mod.rs @@ -315,21 +315,6 @@ pub(crate) mod tests { (state, sched) } - /// [`state_with_roster`] with a connected queue, for creations: they - /// store the agent's permissions through it. The server lives as long as - /// the first element. - pub(crate) async fn state_with_roster_queued() - -> (crate::test_nats::TestNats, super::AppState, SharedSched) { - let nats = crate::test_nats::TestNats::start(); - let client = nats.client().await; - let (mut state, sched) = state_with_roster(); - state.status = Some(std::sync::Arc::new(crate::status::StatusReader::new( - client, - std::time::Duration::from_mins(1), - ))); - (nats, state, sched) - } - /// `wanted_error_status` as a pure function first: build the exact /// `swarm_queue_client::Error::NotConnected` `ensure_connected` raises, /// wrap it the same way `?` inside `WantedWriter::view`/`set` does, and diff --git a/swarm-queue-client/src/agent_permissions.rs b/swarm-queue-client/src/agent_permissions.rs index 73aca48b8..51e60224f 100644 --- a/swarm-queue-client/src/agent_permissions.rs +++ b/swarm-queue-client/src/agent_permissions.rs @@ -50,15 +50,6 @@ pub fn held(record: &AgentPermissions) -> Vec { .unwrap_or_else(|| hive_types::Permission::DEFAULT.to_vec()) } -/// Whether `record` lets the agent's traffic leave unfiltered: its flag when -/// set, **on** when absent. A record written before the flag existed, or a -/// missing key, is an agent that has always had open access. -#[cfg(feature = "agent-permissions")] -#[must_use] -pub fn unrestricted(record: &AgentPermissions) -> bool { - record.unrestricted_internet.unwrap_or(true) -} - /// Read `agent`'s current permissions with one direct get: the one subject /// an agent's own grant covers, and one of those a hive's covers. /// @@ -138,35 +129,6 @@ mod tests { ); } - /// An absent flag reads on, and so does a missing or deleted key; only an - /// explicit `false` reads off. - #[test] - fn an_absent_internet_flag_reads_on() { - let record = |json: &str| -> AgentPermissions { serde_json::from_str(json).unwrap() }; - assert!(unrestricted(&record("{}"))); - assert!(unrestricted(&record(r#"{"permissions":[]}"#))); - assert!(unrestricted(&record(r#"{"unrestricted_internet":true}"#))); - assert!(!unrestricted(&record(r#"{"unrestricted_internet":false}"#))); - assert!(unrestricted(&decode(Some("DEL"), b"").unwrap())); - } - - /// A decoder built before the flag existed has no such field and ignores - /// unknown ones; it reads a record carrying the flag. - #[test] - fn a_decoder_without_the_flag_tolerates_it() { - #[derive(serde::Deserialize)] - struct Old { - permissions: Option>, - } - let old: Old = - serde_json::from_str(r#"{"permissions":["web_tools"],"unrestricted_internet":false}"#) - .unwrap(); - assert_eq!( - old.permissions, - Some(vec![hive_types::Permission::WebTools]) - ); - } - /// Fields a newer controller adds must not cost an older hive the ones it /// knows; a deleted key is unset. #[cfg(feature = "agent-permissions")] diff --git a/swarm-wire/src/agent_permissions.rs b/swarm-wire/src/agent_permissions.rs index 7ba46fde0..acafb0d7c 100644 --- a/swarm-wire/src/agent_permissions.rs +++ b/swarm-wire/src/agent_permissions.rs @@ -71,12 +71,6 @@ pub const IMPORT_REPLY_DEADLINE: std::time::Duration = std::time::Duration::from pub struct AgentPermissions { #[serde(default, skip_serializing_if = "Option::is_none")] pub permissions: Option>, - /// Whether the agent's traffic leaves the host unfiltered. Absent is "on": - /// an agent whose key predates the field keeps the open access it had, and - /// the controller writes an explicit `false` when it creates an agent. Read - /// it through `swarm_queue_client::agent_permissions::unrestricted`. - #[serde(default, skip_serializing_if = "Option::is_none")] - pub unrestricted_internet: Option, } /// What a hive sends once: the permissions its meta repo held, by agent. @@ -146,19 +140,4 @@ mod tests { serde_json::from_str(r#"{"permissions":["manage_root_agent"]}"#).unwrap(); assert_eq!(ruth.permissions, Some(vec![Permission::ManageRootAgent])); } - - /// The flag is left out when unset, so a record that never named it - /// encodes as before, and an explicit `false` is kept on the wire. - #[test] - fn the_internet_flag_is_left_out_unless_set() { - let off = AgentPermissions { - unrestricted_internet: Some(false), - ..AgentPermissions::default() - }; - let wire = r#"{"unrestricted_internet":false}"#; - assert_eq!(serde_json::to_string(&off).unwrap(), wire); - assert_eq!(serde_json::from_str::(wire).unwrap(), off); - let unset: AgentPermissions = serde_json::from_str("{}").unwrap(); - assert_eq!(unset.unrestricted_internet, None); - } } diff --git a/swarmctl/src/agent.rs b/swarmctl/src/agent.rs index ee3337c0f..d941e1510 100644 --- a/swarmctl/src/agent.rs +++ b/swarmctl/src/agent.rs @@ -128,7 +128,6 @@ async fn post_create( limits: None, inference: None, permissions: None, - unrestricted_internet: None, archetype: None, agents_md: agents_md.map(str::to_owned), }, diff --git a/swarmctl/src/main.rs b/swarmctl/src/main.rs index 1561c1a44..5f12945d4 100644 --- a/swarmctl/src/main.rs +++ b/swarmctl/src/main.rs @@ -346,8 +346,7 @@ struct AgentsMdApplyArgs { enum PermissionsVerb { /// Show the permissions the swarm grants an agent, or that it sets none. Get(InferenceAgentArgs), - /// Replace the permissions the swarm grants an agent; the internet flag - /// stays unless given. + /// Replace the permissions the swarm grants an agent. Set(PermissionsSetArgs), /// Clear the agent's permissions, so the default applies again. Clear(InferenceAgentArgs), @@ -367,10 +366,6 @@ struct PermissionsSetArgs { required = true )] permissions: Vec, - /// Whether the agent's traffic may leave the host unfiltered. Left out, - /// the stored value stays. - #[arg(long, value_name = "BOOL")] - unrestricted_internet: Option, #[command(flatten)] socket: ControllerSocketArg, } @@ -770,12 +765,9 @@ 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.permissions, - a.unrestricted_internet, - ), + PermissionsVerb::Set(a) => { + permissions::set(&a.socket.resolve(remote)?, &a.agent, &a.permissions) + } PermissionsVerb::Clear(a) => permissions::clear(&a.socket.resolve(remote)?, &a.agent), } } diff --git a/swarmctl/src/permissions.rs b/swarmctl/src/permissions.rs index 28686f0c7..ea39c37c8 100644 --- a/swarmctl/src/permissions.rs +++ b/swarmctl/src/permissions.rs @@ -45,14 +45,9 @@ pub(crate) fn get(target: &Target, agent: &str) -> Result<()> { /// 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: &[String], - unrestricted_internet: Option, -) -> Result<()> { +pub(crate) fn set(target: &Target, agent: &str, permissions: &[String]) -> Result<()> { let uri = member(agent)?; - let wanted = with_permissions(permissions, unrestricted_internet)?; + let wanted = with_permissions(permissions)?; let stored: Permissions = runtime()?.block_on(send( target, hyper::Method::PUT, @@ -66,10 +61,7 @@ pub(crate) fn set( /// The record granting `permissions`. An empty name, as `--permissions ''` /// gives, is dropped, so that spelling grants nothing. -fn with_permissions( - permissions: &[String], - unrestricted_internet: Option, -) -> Result { +fn with_permissions(permissions: &[String]) -> Result { let granted = permissions .iter() .filter(|name| !name.is_empty()) @@ -77,7 +69,6 @@ fn with_permissions( .collect::>>()?; Ok(Permissions { permissions: Some(granted), - unrestricted_internet, }) } @@ -121,12 +112,7 @@ fn render(p: &Permissions) -> String { .collect::>() .join(", "), }; - let internet = match p.unrestricted_internet { - None => "(not set; unrestricted)", - Some(true) => "unrestricted", - Some(false) => "restricted", - }; - format!("permissions: {caps}\nunrestricted_internet: {internet}\n") + format!("permissions: {caps}\n") } #[cfg(test)] @@ -136,48 +122,18 @@ mod tests { #[test] fn get_tells_unset_from_none() { let unset: Permissions = serde_json::from_str("{}").unwrap(); - assert_eq!( - render(&unset), - "permissions: (not set; the default)\nunrestricted_internet: (not set; unrestricted)\n" - ); + 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)\nunrestricted_internet: (not set; unrestricted)\n" - ); + 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\nunrestricted_internet: (not set; unrestricted)\n" - ); - } - - /// An explicit flag is shown and sent; left out it is not sent, so a `PUT` - /// keeps the stored one. - #[test] - fn the_internet_flag_is_sent_only_when_given() { - let off = with_permissions(&[], Some(false)).unwrap(); - assert_eq!( - serde_json::to_string(&off).unwrap(), - r#"{"permissions":[],"unrestricted_internet":false}"# - ); - assert_eq!( - render(&off), - "permissions: (none)\nunrestricted_internet: restricted\n" - ); - let left_out = with_permissions(&[], None).unwrap(); - assert!( - !serde_json::to_string(&left_out) - .unwrap() - .contains("internet") - ); + 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(&[String::new()], None).unwrap(); + let set = with_permissions(&[String::new()]).unwrap(); assert_eq!( serde_json::to_string(&set).unwrap(), r#"{"permissions":[]}"# @@ -187,11 +143,11 @@ mod tests { /// A name that is not a permission fails locally and lists the valid ones. #[test] fn an_unknown_permission_is_refused() { - let err = with_permissions(&["messaging".to_owned(), "fly".to_owned()], None).unwrap_err(); + let err = with_permissions(&["messaging".to_owned(), "fly".to_owned()]).unwrap_err(); let text = err.to_string(); assert!(text.contains("invalid permission \"fly\""), "{text}"); assert!(text.contains("manage_root_agent"), "{text}"); - let ok = with_permissions(&["web_tools".to_owned()], None).unwrap(); + let ok = with_permissions(&["web_tools".to_owned()]).unwrap(); assert_eq!( serde_json::to_string(&ok).unwrap(), r#"{"permissions":["web_tools"]}"#