120 lines
4.9 KiB
Rust
120 lines
4.9 KiB
Rust
//! `/api/agents/{name}/permissions`, and the `permissions` of
|
|
//! `POST /api/agents`.
|
|
|
|
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.
|
|
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct Permissions {
|
|
/// Permission names, `snake_case`: `messaging`, `meta`, `inbox`,
|
|
/// `execution`, `web_tools`, `manage_root_agent`, `logs`, `metrics`.
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
#[cfg_attr(
|
|
feature = "schema",
|
|
schema(value_type = Option<Vec<String>>, example = json!(["messaging", "meta", "inbox", "execution"]))
|
|
)]
|
|
pub permissions: Option<Vec<Permission>>,
|
|
/// 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<bool>,
|
|
/// With `unrestricted_internet` `false`: requests to names the agent's lists
|
|
/// do not allow go through and squid logs each as `would_deny`. `true`
|
|
/// needs `unrestricted_internet: false`, in the request or stored. Setting
|
|
/// `unrestricted_internet` without this field turns it off; leaving both
|
|
/// out keeps the stored value.
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub internet_log: Option<bool>,
|
|
/// How many repositories the agent's forge account may create, 0 to 100.
|
|
/// Absent in a `GET` means the swarm never set it, which counts as 0.
|
|
/// Absent in a `PUT` keeps the stored value. An agent that may create
|
|
/// repositories can add a CI workflow to them, and CI workflows run
|
|
/// unsandboxed on the CI runner.
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
#[cfg_attr(feature = "schema", schema(example = 0, minimum = 0, maximum = 100))]
|
|
pub max_repos: Option<u32>,
|
|
/// Whether the agent may get short-lived SSH certificates from the swarm's SSH
|
|
/// CA. Absent in a `GET` means the swarm never set it, which counts as off.
|
|
/// Absent in a `PUT` keeps the stored value. The agent's store login role
|
|
/// takes the change within five minutes; a certificate already signed stays
|
|
/// valid until it expires, at most three hours.
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub ssh: Option<bool>,
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::Permissions;
|
|
use hive_types::Permission;
|
|
|
|
#[test]
|
|
fn permissions_round_trip() {
|
|
for (value, wire) in [
|
|
(Permissions::default(), "{}"),
|
|
(
|
|
Permissions {
|
|
permissions: Some(vec![]),
|
|
unrestricted_internet: None,
|
|
internet_log: None,
|
|
max_repos: None,
|
|
ssh: None,
|
|
},
|
|
r#"{"permissions":[]}"#,
|
|
),
|
|
(
|
|
Permissions {
|
|
permissions: None,
|
|
unrestricted_internet: Some(false),
|
|
internet_log: None,
|
|
max_repos: None,
|
|
ssh: None,
|
|
},
|
|
r#"{"unrestricted_internet":false}"#,
|
|
),
|
|
(
|
|
Permissions {
|
|
permissions: None,
|
|
unrestricted_internet: Some(false),
|
|
internet_log: Some(true),
|
|
max_repos: None,
|
|
ssh: None,
|
|
},
|
|
r#"{"unrestricted_internet":false,"internet_log":true}"#,
|
|
),
|
|
(
|
|
Permissions {
|
|
permissions: Some(vec![Permission::Messaging, Permission::ManageRootAgent]),
|
|
unrestricted_internet: None,
|
|
internet_log: None,
|
|
max_repos: None,
|
|
ssh: None,
|
|
},
|
|
r#"{"permissions":["messaging","manage_root_agent"]}"#,
|
|
),
|
|
(
|
|
Permissions {
|
|
max_repos: Some(3),
|
|
..Permissions::default()
|
|
},
|
|
r#"{"max_repos":3}"#,
|
|
),
|
|
(
|
|
Permissions {
|
|
ssh: Some(true),
|
|
..Permissions::default()
|
|
},
|
|
r#"{"ssh":true}"#,
|
|
),
|
|
] {
|
|
assert_eq!(serde_json::to_string(&value).unwrap(), wire);
|
|
assert_eq!(serde_json::from_str::<Permissions>(wire).unwrap(), value);
|
|
}
|
|
assert!(serde_json::from_str::<Permissions>(r#"{"permissions":["fly"]}"#).is_err());
|
|
}
|
|
}
|