[roads] QA F-04: a dangling owner or group member drops only itself, not every plan
Deleting a person from directory.json without editing devices.json left a dangling owner, a snapshot error, and so no plans for anybody. A well-formed owner or member id that is not a person is now reported on its own codes (devices.owner-unknown, directory.member-unknown) and removed: the device is ownerless, the member leaves the group. Malformed ids, duplicates and bad JSON still stop every plan. Two new structural fixtures keep devices.owner and directory.member covered.
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
//! The two snapshots the compiler reads besides the roads: `directory.json` and `devices.json`
|
||||
//! (docs/FORMATS.md). Both are strict: unknown fields are errors, and any error means no plan is produced.
|
||||
//! (docs/FORMATS.md). Both are strict: unknown fields are errors, and a *structural* error (bad JSON, a wrong
|
||||
//! format, a malformed or duplicate id, key or address) means no plan is produced.
|
||||
//!
|
||||
//! A *dangling reference* is different (QA F-04): a group member or a device owner that is a well-formed id
|
||||
//! but no longer a person in the directory. That is what deleting a person upstream leaves behind. It is
|
||||
//! still an error, and reported, but it only removes that reference: the member leaves the group, the device
|
||||
//! becomes ownerless (reached only by `device:` roads). Everybody else keeps their plans.
|
||||
|
||||
use crate::ids::{
|
||||
DeviceKey, GroupId, Hostname, PersonId, clip, is_device_key, is_hostname, is_opaque_id,
|
||||
@@ -173,11 +179,17 @@ pub fn is_hidden_char(c: char) -> bool {
|
||||
|| matches!(c, '\u{2028}' | '\u{2029}' | '\u{200b}'..='\u{200f}' | '\u{202a}'..='\u{202e}' | '\u{2060}'..='\u{2069}' | '\u{feff}')
|
||||
}
|
||||
|
||||
/// A parsed snapshot and the dangling references dropped from it (errors that do not stop plans).
|
||||
pub type Parsed<T> = Result<(T, Vec<Problem>), Vec<Problem>>;
|
||||
|
||||
impl Directory {
|
||||
pub fn parse(bytes: &[u8]) -> Result<Directory, Vec<Problem>> {
|
||||
/// `Err`: a structural error, no directory. `Ok((dir, dangling))`: `dangling` lists the members left
|
||||
/// out because they are not people in the directory.
|
||||
pub fn parse(bytes: &[u8]) -> Parsed<Directory> {
|
||||
let path = DIRECTORY_FILE;
|
||||
let raw: RawDirectory = read_json(path, bytes)?;
|
||||
let mut problems = Vec::new();
|
||||
let mut dangling = Vec::new();
|
||||
let mut e = |field: String, code, msg: String| {
|
||||
problems.push(Problem::error(path, Some(&field), code, msg))
|
||||
};
|
||||
@@ -247,8 +259,8 @@ impl Directory {
|
||||
}
|
||||
let mut members = BTreeSet::new();
|
||||
for (j, m) in g.members.iter().enumerate() {
|
||||
match PersonId::new(m).filter(|m| dir.people.contains_key(m)) {
|
||||
Some(m) => {
|
||||
match PersonId::new(m) {
|
||||
Some(m) if dir.people.contains_key(&m) => {
|
||||
if !members.insert(m.clone()) {
|
||||
e(
|
||||
format!("groups[{i}].members[{j}]"),
|
||||
@@ -257,10 +269,21 @@ impl Directory {
|
||||
);
|
||||
}
|
||||
}
|
||||
Some(m) => dangling.push(Problem::error(
|
||||
path,
|
||||
Some(&format!("groups[{i}].members[{j}]")),
|
||||
r::DIRECTORY_MEMBER_UNKNOWN,
|
||||
format!(
|
||||
"`{m}` is not a person in the directory (groups are flat: members are people); left out of the group"
|
||||
),
|
||||
)),
|
||||
None => e(
|
||||
format!("groups[{i}].members[{j}]"),
|
||||
r::DIRECTORY_MEMBER,
|
||||
format!("`{}` is not a person in the directory", clip(m)),
|
||||
format!(
|
||||
"`{}` is not a person id: 1-64 of A-Z a-z 0-9 _ -",
|
||||
clip(m)
|
||||
),
|
||||
),
|
||||
}
|
||||
}
|
||||
@@ -284,7 +307,7 @@ impl Directory {
|
||||
}
|
||||
}
|
||||
if problems.is_empty() {
|
||||
Ok(dir)
|
||||
Ok((dir, dangling))
|
||||
} else {
|
||||
Err(problems)
|
||||
}
|
||||
@@ -292,10 +315,13 @@ impl Directory {
|
||||
}
|
||||
|
||||
impl Devices {
|
||||
pub fn parse(bytes: &[u8], directory: Option<&Directory>) -> Result<Devices, Vec<Problem>> {
|
||||
/// `Err`: a structural error, no device list. `Ok((devices, dangling))`: `dangling` lists the owners
|
||||
/// dropped because they are not people in `directory`; those devices are ownerless.
|
||||
pub fn parse(bytes: &[u8], directory: Option<&Directory>) -> Parsed<Devices> {
|
||||
let path = DEVICES_FILE;
|
||||
let raw: RawDevices = read_json(path, bytes)?;
|
||||
let mut problems = Vec::new();
|
||||
let mut dangling = Vec::new();
|
||||
let mut e = |field: String, code, msg: String| {
|
||||
problems.push(Problem::error(path, Some(&field), code, msg))
|
||||
};
|
||||
@@ -345,22 +371,28 @@ impl Devices {
|
||||
}
|
||||
let owner = match &d.owner {
|
||||
None => None,
|
||||
Some(o) => {
|
||||
let known = directory.is_none_or(|dir| {
|
||||
PersonId::new(o).is_some_and(|p| dir.people.contains_key(&p))
|
||||
});
|
||||
match PersonId::new(o).filter(|_| is_opaque_id(o) && known) {
|
||||
Some(p) => Some(p),
|
||||
None => {
|
||||
e(
|
||||
f("owner"),
|
||||
r::DEVICES_OWNER,
|
||||
format!("`{}` is not a person in the directory", clip(o)),
|
||||
);
|
||||
None
|
||||
}
|
||||
Some(o) => match PersonId::new(o).filter(|_| is_opaque_id(o)) {
|
||||
Some(p) if directory.is_none_or(|dir| dir.people.contains_key(&p)) => Some(p),
|
||||
Some(p) => {
|
||||
dangling.push(Problem::error(
|
||||
path,
|
||||
Some(&f("owner")),
|
||||
r::DEVICES_OWNER_UNKNOWN,
|
||||
format!(
|
||||
"`{p}` is not a person in the directory; the device is ownerless until it gets an owner, and only `device:` roads reach it"
|
||||
),
|
||||
));
|
||||
None
|
||||
}
|
||||
}
|
||||
None => {
|
||||
e(
|
||||
f("owner"),
|
||||
r::DEVICES_OWNER,
|
||||
format!("`{}` is not a person id: 1-64 of A-Z a-z 0-9 _ -", clip(o)),
|
||||
);
|
||||
None
|
||||
}
|
||||
},
|
||||
};
|
||||
let address = match d.address.parse::<std::net::IpAddr>() {
|
||||
Ok(a)
|
||||
@@ -506,7 +538,7 @@ impl Devices {
|
||||
}
|
||||
}
|
||||
if problems.is_empty() {
|
||||
Ok(out)
|
||||
Ok((out, dangling))
|
||||
} else {
|
||||
Err(problems)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user