swarm: unrestricted_internet flag on the agent permissions record, unset reads as on, create writes false
This commit is contained in:
parent
e77459d2cb
commit
8699857024
1 changed files with 381 additions and 107 deletions
|
|
@ -50,6 +50,15 @@ pub fn held(record: &AgentPermissions) -> Vec<hive_types::Permission> {
|
|||
.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.
|
||||
///
|
||||
|
|
@ -129,6 +138,35 @@ 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<Vec<hive_types::Permission>>,
|
||||
}
|
||||
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")]
|
||||
|
|
|
|||
Loading…
Reference in a new issue