swarmctl shares the create-agent, inference and permissions shapes with the controller
This commit is contained in:
parent
249aab6642
commit
0831b85b73
26 changed files with 675 additions and 434 deletions
|
|
@ -5,24 +5,16 @@
|
|||
//! 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`, whose names are
|
||||
//! `hive_types::Permission`, and `swarm-controller-api` does not
|
||||
//! depend on `hive-sh4re`. Permission names are checked by the controller,
|
||||
//! which answers 422 for one it does not know.
|
||||
//! The body comes from `swarm-controller-api`, which the controller imports
|
||||
//! too. A name that is not a permission fails before the request.
|
||||
|
||||
use anyhow::{Context as _, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use hive_types::Permission;
|
||||
use swarm_controller_api::permissions::Permissions;
|
||||
|
||||
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<Vec<String>>,
|
||||
}
|
||||
|
||||
fn runtime() -> Result<tokio::runtime::Runtime> {
|
||||
tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
|
|
@ -53,13 +45,14 @@ 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: Vec<String>) -> Result<()> {
|
||||
pub(crate) fn set(target: &Target, agent: &str, permissions: &[String]) -> Result<()> {
|
||||
let uri = member(agent)?;
|
||||
let wanted = with_permissions(permissions)?;
|
||||
let stored: Permissions = runtime()?.block_on(send(
|
||||
target,
|
||||
hyper::Method::PUT,
|
||||
&uri,
|
||||
Some(&with_permissions(permissions)),
|
||||
Some(&wanted),
|
||||
"permissions-set",
|
||||
))?;
|
||||
print!("{}", render(&stored));
|
||||
|
|
@ -68,10 +61,31 @@ pub(crate) fn set(target: &Target, agent: &str, permissions: Vec<String>) -> Res
|
|||
|
||||
/// The record granting `permissions`. An empty name, as `--permissions ''`
|
||||
/// gives, is dropped, so that spelling grants nothing.
|
||||
fn with_permissions(permissions: Vec<String>) -> Permissions {
|
||||
Permissions {
|
||||
permissions: Some(permissions.into_iter().filter(|c| !c.is_empty()).collect()),
|
||||
}
|
||||
fn with_permissions(permissions: &[String]) -> Result<Permissions> {
|
||||
let granted = permissions
|
||||
.iter()
|
||||
.filter(|name| !name.is_empty())
|
||||
.map(|name| parse_permission(name))
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
Ok(Permissions {
|
||||
permissions: Some(granted),
|
||||
})
|
||||
}
|
||||
|
||||
/// The permission `name` spells, or an error listing the names that would
|
||||
/// have been accepted.
|
||||
fn parse_permission(name: &str) -> Result<Permission> {
|
||||
Permission::ALL
|
||||
.iter()
|
||||
.copied()
|
||||
.find(|p| <&str>::from(*p) == name)
|
||||
.ok_or_else(|| {
|
||||
let known: Vec<&str> = Permission::ALL.iter().map(|p| <&str>::from(*p)).collect();
|
||||
anyhow::anyhow!(
|
||||
"invalid permission {name:?}: expected one of {}",
|
||||
known.join(", ")
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// Run `swarmctl agent permissions clear`.
|
||||
|
|
@ -92,7 +106,11 @@ 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(", "),
|
||||
Some(perms) => perms
|
||||
.iter()
|
||||
.map(|p| <&str>::from(*p))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", "),
|
||||
};
|
||||
format!("permissions: {caps}\n")
|
||||
}
|
||||
|
|
@ -115,10 +133,24 @@ mod tests {
|
|||
/// `--permissions ''` stores an empty list, not one empty name.
|
||||
#[test]
|
||||
fn an_empty_list_flag_grants_nothing() {
|
||||
let set = with_permissions(vec![String::new()]);
|
||||
let set = with_permissions(&[String::new()]).unwrap();
|
||||
assert_eq!(
|
||||
serde_json::to_string(&set).unwrap(),
|
||||
r#"{"permissions":[]}"#
|
||||
);
|
||||
}
|
||||
|
||||
/// 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()]).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()]).unwrap();
|
||||
assert_eq!(
|
||||
serde_json::to_string(&ok).unwrap(),
|
||||
r#"{"permissions":["web_tools"]}"#
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue