[roads] QA F-01: a service road grants only that service
A firewall sees an address and a port, never a hostname, so a road to photos.home.arpa compiled to home-server:443 and also opened git.home.arpa there. Rules are now per service: - devices.json services may carry their own mesh address (same rules as a device address, unique among all of them). A road to such a service compiles to that address only; the hosting device's plan lists it in the new field services[] so it answers on it, and DNS points there. - A road to a service that shares its address and a port with another service is refused (service.shared), naming the others, instead of opening both. Narrowing ports: to a port nobody else serves on that address is allowed. - A device: road reaches the whole device: its own address and its services' own addresses. No Ianitor identity check is needed for this. The home fixture gives photos its own address (100.96.1.2); tests that encoded the old host:port model are updated, three new snapshot fixtures cover the address rules, and the golden plans and pinned digest are regenerated. Mutation-checked: dropping the refusal, or ignoring the service address, each fails an F-01 test.
This commit is contained in:
@@ -45,6 +45,17 @@ pub struct Directory {
|
||||
pub struct Service {
|
||||
pub host: Hostname,
|
||||
pub ports: Vec<PortSpec>,
|
||||
/// The service's own mesh address on its device, if it has one. A firewall sees addresses and ports, not
|
||||
/// hostnames, so only a service with its own address (or alone on its ports) can be granted by itself
|
||||
/// (QA F-01).
|
||||
pub address: Option<Addr>,
|
||||
}
|
||||
|
||||
impl Service {
|
||||
/// Where a road to this service sends traffic: its own address, or its device's.
|
||||
pub fn endpoint(&self, device: &Device) -> Addr {
|
||||
self.address.unwrap_or(device.address)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -79,9 +90,15 @@ impl Devices {
|
||||
.find_map(|d| d.services.iter().find(|s| &s.host == host).map(|s| (d, s)))
|
||||
}
|
||||
|
||||
/// Every mesh address: each device's own. Never goes direct (QA F-02).
|
||||
/// Every mesh address: each device's own and its services' own. Never goes direct (QA F-02).
|
||||
pub fn mesh_addresses(&self) -> impl Iterator<Item = (&Device, Addr)> {
|
||||
self.devices.values().map(|d| (d, d.address))
|
||||
self.devices.values().flat_map(|d| {
|
||||
std::iter::once((d, d.address)).chain(
|
||||
d.services
|
||||
.iter()
|
||||
.filter_map(move |s| s.address.map(|a| (d, a))),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// The one device whose routes cover `net`.
|
||||
@@ -148,6 +165,8 @@ struct RawDevice {
|
||||
struct RawService {
|
||||
host: String,
|
||||
ports: Vec<String>,
|
||||
#[serde(default)]
|
||||
address: Option<String>,
|
||||
}
|
||||
|
||||
fn read_json<T: for<'de> Deserialize<'de>>(path: &str, bytes: &[u8]) -> Result<T, Vec<Problem>> {
|
||||
@@ -172,6 +191,22 @@ fn read_json<T: for<'de> Deserialize<'de>>(path: &str, bytes: &[u8]) -> Result<T
|
||||
})
|
||||
}
|
||||
|
||||
/// A mesh address: one canonical IP address, never loopback, link-local, multicast, IPv4-embedding IPv6 or an
|
||||
/// internet address, so an exit's `internet` rule can never cover a device or service of the mesh (QA F-08).
|
||||
fn mesh_address(text: &str) -> Option<Addr> {
|
||||
let a = text.parse::<std::net::IpAddr>().ok()?;
|
||||
let n = Net::host(a);
|
||||
(a.to_string() == text && !n.is_special() && !n.embeds_ipv4() && !n.reaches_internet())
|
||||
.then_some(Addr(a))
|
||||
}
|
||||
|
||||
fn not_mesh(text: &str) -> String {
|
||||
format!(
|
||||
"`{}` is not one private IP address (a mesh address is never loopback, link-local, multicast, embedded IPv4 or on the internet; use 100.64.0.0/10 or a ULA)",
|
||||
clip(text)
|
||||
)
|
||||
}
|
||||
|
||||
fn good_name(s: &str) -> bool {
|
||||
let n = s.chars().count();
|
||||
(1..=100).contains(&n) && !s.chars().any(is_hidden_char) && s.trim() == s
|
||||
@@ -399,33 +434,19 @@ impl Devices {
|
||||
}
|
||||
},
|
||||
};
|
||||
let address = match d.address.parse::<std::net::IpAddr>() {
|
||||
// A mesh address is never an internet address, so an exit's `internet` rule can never
|
||||
// cover another device of the mesh (QA F-08).
|
||||
Ok(a)
|
||||
if a.to_string() == d.address
|
||||
&& !Net::host(a).is_special()
|
||||
&& !Net::host(a).embeds_ipv4()
|
||||
&& !Net::host(a).reaches_internet() =>
|
||||
{
|
||||
if !addresses.insert(a) {
|
||||
let address = match mesh_address(&d.address) {
|
||||
Some(a) => {
|
||||
if !addresses.insert(a.0) {
|
||||
e(
|
||||
f("address"),
|
||||
r::DEVICES_ADDRESS,
|
||||
format!("{a} is used by two devices"),
|
||||
format!("{a} is used twice"),
|
||||
);
|
||||
}
|
||||
Some(Addr(a))
|
||||
Some(a)
|
||||
}
|
||||
_ => {
|
||||
e(
|
||||
f("address"),
|
||||
r::DEVICES_ADDRESS,
|
||||
format!(
|
||||
"`{}` is not one private IP address (a mesh address is never loopback, link-local, multicast, embedded IPv4 or on the internet; 100.64.0.0/10 or a ULA)",
|
||||
clip(&d.address)
|
||||
),
|
||||
);
|
||||
None => {
|
||||
e(f("address"), r::DEVICES_ADDRESS, not_mesh(&d.address));
|
||||
None
|
||||
}
|
||||
};
|
||||
@@ -496,7 +517,29 @@ impl Devices {
|
||||
"a service lists the ports it serves".into(),
|
||||
);
|
||||
}
|
||||
services.push(Service { host, ports });
|
||||
let field = format!("devices[{i}].services[{j}].address");
|
||||
let address = match s.address.as_deref().map(mesh_address) {
|
||||
None => None,
|
||||
Some(Some(a)) => {
|
||||
if !addresses.insert(a.0) {
|
||||
e(field, r::DEVICES_ADDRESS, format!("{a} is used twice"));
|
||||
}
|
||||
Some(a)
|
||||
}
|
||||
Some(None) => {
|
||||
e(
|
||||
field,
|
||||
r::DEVICES_ADDRESS,
|
||||
not_mesh(s.address.as_deref().unwrap_or_default()),
|
||||
);
|
||||
None
|
||||
}
|
||||
};
|
||||
services.push(Service {
|
||||
host,
|
||||
ports,
|
||||
address,
|
||||
});
|
||||
}
|
||||
services.sort_by(|a, b| a.host.cmp(&b.host));
|
||||
let Some(address) = address else { continue };
|
||||
@@ -535,15 +578,12 @@ impl Devices {
|
||||
);
|
||||
}
|
||||
}
|
||||
for d in out.devices.values() {
|
||||
if na.contains_addr(&d.address.0) {
|
||||
for (d, a) in out.mesh_addresses() {
|
||||
if na.contains_addr(&a.0) {
|
||||
e(
|
||||
format!("devices.{}.address", d.name),
|
||||
r::DEVICES_ADDRESS,
|
||||
format!(
|
||||
"{} of {} is inside the route {na} of {}",
|
||||
d.address, d.name, da.name
|
||||
),
|
||||
format!("{a} of {} is inside the route {na} of {}", d.name, da.name),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user