Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/hive-c0re/src/workers/egress_connections.rs

802 lines
30 KiB
Rust

//! The SSH connections the hive's forward chain lets agents open: for each
//! agent whose `ssh` switch is on, the addresses behind the `host` of each
//! connection in its permissions record.
//!
//! Every [`INTERVAL`] this reads the permissions the swarm granted this
//! hive's agents ([`crate::permissions`]) and renders one `<agent> <ip>
//! <port>` line per agent, IPv4 address and port into the file named by
//! [`ENV_FILE`]. `hive-priv egress-pin` turns each line into one element of
//! the `agent_allow` set of `nix/host-modules/hive-egress.nix`, keyed by the
//! MAC of the agent's port, which only hive-priv can tell. IPv6 answers are
//! left out: the agent bridge has no IPv6 addressing.
//!
//! A host name is resolved by the system resolver, so it means what it means
//! to the host's other services; an IPv4 literal is taken as it is. Every
//! address a name resolves to is allowed, as the agent may use any of them.
//! An answer is kept for [`RESOLVE_EVERY`], and a host a record newly names
//! is resolved at the next pass. The hosts of a pass are asked concurrently,
//! each for at most [`RESOLVE_TIMEOUT`], so a resolver that hangs delays the
//! file by that long and no more. A name that fails to resolve, or times out,
//! keeps the addresses of its last answer and logs one warning until it
//! resolves again: a resolver outage must not read as the operator removing
//! the connection. An address inside this hive's bridge subnet is dropped,
//! with one warning per host: a flow to it never reaches the forward chain.
//!
//! A failed read of the permissions leaves the file as it is.
use std::collections::{BTreeSet, HashMap, HashSet};
use std::future::Future;
use std::net::{IpAddr, Ipv4Addr};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use anyhow::{Context, Result};
use futures_util::StreamExt as _;
use super::squid_users::write_if_changed;
use crate::permissions;
use crate::stats::sweep_health::{self, SweepHealth};
/// How often the file is re-rendered, which is how soon a new connection
/// or a flipped `ssh` switch takes effect.
pub const INTERVAL: Duration = Duration::from_secs(30);
/// How long an answer of the resolver is used before the name is asked again.
pub const RESOLVE_EVERY: Duration = Duration::from_mins(10);
/// How long one lookup may take before it counts as failed.
pub const RESOLVE_TIMEOUT: Duration = Duration::from_secs(5);
/// How many lookups of one pass run at the same time.
const CONCURRENT_LOOKUPS: usize = 8;
/// At [`INTERVAL`] this is ~90 seconds of failing passes.
const FAILURES_BEFORE_WARNING: u32 = 3;
/// The environment variable naming the file. Set by `nix/host-modules/hive-c0re`
/// from `services.hyperhive.deploy.hive-controller.egress.connectionsFile`;
/// unset means this host's forward chain does not read one.
pub const ENV_FILE: &str = "HIVE_EGRESS_CONNECTIONS_FILE";
/// The environment variable that carries the bridge address and prefix
/// (`10.42.0.1/24`); `nix/host-modules/hive-network.nix` sets it for the daemon.
const ENV_SUBNET: &str = "HIVE_NETWORK_SUBNET";
/// An IPv4 subnet.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Subnet {
network: u32,
mask: u32,
}
impl Subnet {
/// `<ipv4>/<prefix>`, where the address may be any host of the subnet.
fn parse(text: &str) -> Option<Self> {
let (ip, prefix) = text.split_once('/')?;
let prefix: u32 = prefix.parse().ok().filter(|p| *p <= 32)?;
let mask = u32::MAX.checked_shl(32 - prefix).unwrap_or(0);
Some(Self {
network: u32::from(ip.parse::<Ipv4Addr>().ok()?) & mask,
mask,
})
}
fn contains(self, ip: Ipv4Addr) -> bool {
u32::from(ip) & self.mask == self.network
}
}
/// One declared connection of an agent: where it goes.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct Target {
agent: String,
host: String,
port: u16,
}
/// The connections of every agent whose `ssh` switch is on.
fn targets(perms: &permissions::Stored) -> BTreeSet<Target> {
let mut out = BTreeSet::new();
for (agent, record) in perms {
if record.ssh != Some(true) || hive_types::Ident::parse(agent).is_err() {
continue;
}
for c in &record.ssh_connections {
out.insert(Target {
agent: agent.clone(),
host: c.host.clone(),
port: c.port,
});
}
}
out
}
/// What a host resolved to at its last successful ask.
struct Answer {
/// When it was asked; `None` once an ask failed, so the next pass asks
/// again.
at: Option<Instant>,
addrs: Vec<Ipv4Addr>,
}
/// What each host resolved to.
#[derive(Default)]
struct Answers {
by_host: HashMap<String, Answer>,
/// Hosts whose failure was logged and that have not resolved since.
warned: HashSet<String>,
/// Hosts whose answer holds an address of the bridge subnet, logged once.
warned_bridge: HashSet<String>,
}
impl Answers {
/// Ask for every host in `hosts` that has no answer yet or whose answer is
/// older than [`RESOLVE_EVERY`], and forget the others. The asks run
/// [`CONCURRENT_LOOKUPS`] at a time, each for at most [`RESOLVE_TIMEOUT`].
async fn refresh<F, Fut>(
&mut self,
hosts: &BTreeSet<&str>,
now: Instant,
bridge: Option<Subnet>,
resolve: F,
) where
F: Fn(String) -> Fut,
Fut: Future<Output = Result<Vec<IpAddr>>>,
{
self.by_host.retain(|h, _| hosts.contains(h.as_str()));
self.warned.retain(|h| hosts.contains(h.as_str()));
self.warned_bridge.retain(|h| hosts.contains(h.as_str()));
let due: Vec<String> = hosts
.iter()
.filter(|host| {
self.by_host
.get(**host)
.and_then(|a| a.at)
.is_none_or(|at| now.duration_since(at) >= RESOLVE_EVERY)
})
.map(|host| (*host).to_owned())
.collect();
let resolve = &resolve;
let asked: Vec<(String, Result<Vec<IpAddr>>)> = futures_util::stream::iter(due)
.map(|host| async move {
let answer = ask(&host, resolve).await;
(host, answer)
})
.buffer_unordered(CONCURRENT_LOOKUPS)
.collect()
.await;
for (host, result) in asked {
let host = host.as_str();
match result {
Ok(addrs) => {
self.warned.remove(host);
let (usable, inside) = usable_v4(&addrs, bridge);
if inside.is_empty() {
self.warned_bridge.remove(host);
} else if self.warned_bridge.insert(host.to_owned()) {
tracing::warn!(
host,
addresses = ?inside,
"egress connections: the host resolves into the hive's bridge \
subnet; those addresses are not allowed"
);
}
self.by_host.insert(
host.to_owned(),
Answer {
at: Some(now),
addrs: usable,
},
);
}
Err(e) => {
if self.warned.insert(host.to_owned()) {
tracing::warn!(
host,
error = ?e,
"egress connections: resolving failed, keeping the last answer"
);
}
if let Some(answer) = self.by_host.get_mut(host) {
answer.at = None;
}
}
}
}
}
fn addrs(&self, host: &str) -> &[Ipv4Addr] {
self.by_host.get(host).map_or(&[], |a| a.addrs.as_slice())
}
}
/// `host` as an address when it is a literal, else what `resolve` answers
/// within [`RESOLVE_TIMEOUT`].
async fn ask<F, Fut>(host: &str, resolve: &F) -> Result<Vec<IpAddr>>
where
F: Fn(String) -> Fut,
Fut: Future<Output = Result<Vec<IpAddr>>>,
{
if let Ok(ip) = host.parse::<IpAddr>() {
return Ok(vec![ip]);
}
tokio::time::timeout(RESOLVE_TIMEOUT, resolve(host.to_owned()))
.await
.map_err(|_| anyhow::anyhow!("resolving {host} took more than {RESOLVE_TIMEOUT:?}"))?
}
/// The IPv4 addresses among `addrs` that a remote machine can have, and the
/// ones dropped for lying inside the `bridge` subnet.
fn usable_v4(addrs: &[IpAddr], bridge: Option<Subnet>) -> (Vec<Ipv4Addr>, Vec<Ipv4Addr>) {
let mut v4: Vec<Ipv4Addr> = addrs
.iter()
.filter_map(|a| match a {
IpAddr::V4(v4) => Some(*v4),
IpAddr::V6(_) => None,
})
.filter(|a| {
!(a.is_loopback()
|| a.is_link_local()
|| a.is_unspecified()
|| a.is_multicast()
|| a.is_broadcast())
})
.collect();
v4.sort_unstable();
v4.dedup();
v4.into_iter()
.partition(|a| !bridge.is_some_and(|b| b.contains(*a)))
}
/// The file's text for `targets` under the answers in `answers`, sorted.
fn render(targets: &BTreeSet<Target>, answers: &Answers) -> String {
let mut lines = BTreeSet::new();
for t in targets {
for ip in answers.addrs(&t.host) {
lines.insert(format!("{} {ip} {}\n", t.agent, t.port));
}
}
lines.into_iter().collect()
}
/// One pass: refresh the answers for the hosts `perms` names and write the file
/// if it changed.
async fn pass_with<F, Fut>(
perms: &permissions::Stored,
answers: &mut Answers,
file: &Path,
now: Instant,
bridge: Option<Subnet>,
resolve: F,
) -> Result<()>
where
F: Fn(String) -> Fut,
Fut: Future<Output = Result<Vec<IpAddr>>>,
{
let wanted = targets(perms);
let hosts: BTreeSet<&str> = wanted.iter().map(|t| t.host.as_str()).collect();
answers.refresh(&hosts, now, bridge, resolve).await;
let text = render(&wanted, answers);
if write_if_changed(file, &text)? {
tracing::info!(
file = %file.display(),
"egress connections: wrote the list, {} allowed addresses",
text.lines().count()
);
}
Ok(())
}
/// The system resolver's addresses for `host`.
async fn system_resolve(host: String) -> Result<Vec<IpAddr>> {
let found = tokio::net::lookup_host((host.as_str(), 0))
.await
.with_context(|| format!("resolving {host}"))?;
Ok(found.map(|a| a.ip()).collect())
}
fn env_path(name: &str) -> Option<PathBuf> {
std::env::var_os(name)
.filter(|v| !v.is_empty())
.map(PathBuf::from)
}
/// Start the pass loop, when this host names the file.
pub fn spawn(mut shutdown: tokio::sync::watch::Receiver<bool>) {
let Some(file) = env_path(ENV_FILE) else {
return;
};
let bridge = std::env::var(ENV_SUBNET)
.ok()
.and_then(|s| Subnet::parse(&s));
if bridge.is_none() {
tracing::warn!(
"egress connections: {ENV_SUBNET} is unset or invalid, so the bridge subnet is not excluded"
);
}
tokio::spawn(async move {
let mut health =
SweepHealth::new("egress_connections_render", "warn", FAILURES_BEFORE_WARNING);
let mut answers = Answers::default();
loop {
let result = match permissions::read_at(&permissions::path()) {
Ok(perms) => {
pass_with(
&perms,
&mut answers,
&file,
Instant::now(),
bridge,
system_resolve,
)
.await
}
Err(e) => {
Err(anyhow::Error::new(e).context("reading the stored swarm permissions"))
}
};
match result {
Ok(()) => health.record_ok(),
Err(e) => {
tracing::warn!(error = ?e, "egress connections: the pass failed");
let err = format!("{e:#}");
health.record_err(|ctx| {
let age = ctx.since_last_ok.map_or_else(
|| "no success this session".to_owned(),
|d| format!("last ok {} ago", sweep_health::fmt_age(d)),
);
format!(
"rendering the agents' allowed ssh connections is failing ({} \
consecutive, {age}) — the forward chain keeps the list last \
written: {err}",
ctx.consecutive
)
});
}
}
tokio::select! {
() = tokio::time::sleep(INTERVAL) => {}
_ = shutdown.changed() => return,
}
}
});
}
#[cfg(test)]
mod tests {
use std::cell::RefCell;
use std::rc::Rc;
use super::*;
use swarm_wire::agent_permissions::AgentPermissions;
fn record(json: &str) -> AgentPermissions {
serde_json::from_str(json).expect("decodes")
}
fn pc2(agent_ssh: &str) -> AgentPermissions {
record(&format!(
r#"{{"ssh":{agent_ssh},"ssh_connections":[{{"name":"k8s-lab-vm","user":"root","host":"muede-pc2","port":2222}}]}}"#
))
}
fn ip(s: &str) -> IpAddr {
s.parse().expect("an address")
}
type ResolveFuture = std::pin::Pin<Box<dyn Future<Output = Result<Vec<IpAddr>>>>>;
/// A resolver that answers from `table`, counts its calls and fails for
/// names not in it.
#[derive(Clone, Default)]
struct Dns {
table: Rc<RefCell<HashMap<String, Vec<IpAddr>>>>,
calls: Rc<RefCell<Vec<String>>>,
/// Hosts whose lookup never answers.
hung: Rc<RefCell<HashSet<String>>>,
}
impl Dns {
fn set(&self, host: &str, addrs: &[&str]) {
self.table
.borrow_mut()
.insert(host.to_owned(), addrs.iter().map(|a| ip(a)).collect());
}
fn fail(&self, host: &str) {
self.table.borrow_mut().remove(host);
}
fn hang(&self, host: &str) {
self.hung.borrow_mut().insert(host.to_owned());
}
fn resolver(&self) -> impl Fn(String) -> ResolveFuture + use<> {
let (table, calls, hung) = (self.table.clone(), self.calls.clone(), self.hung.clone());
move |host| {
calls.borrow_mut().push(host.clone());
if hung.borrow().contains(&host) {
return Box::pin(std::future::pending());
}
let answer = table
.borrow()
.get(&host)
.cloned()
.ok_or_else(|| anyhow::anyhow!("no such host"));
Box::pin(std::future::ready(answer))
}
}
fn calls(&self) -> usize {
self.calls.borrow().len()
}
}
struct Rig {
dir: tempfile::TempDir,
answers: Answers,
dns: Dns,
start: Instant,
bridge: Option<Subnet>,
}
impl Rig {
fn new() -> Self {
let rig = Self {
dir: tempfile::tempdir().expect("tempdir"),
answers: Answers::default(),
dns: Dns::default(),
start: Instant::now(),
bridge: None,
};
// The empty file `systemd-tmpfiles` creates before c0re starts.
std::fs::write(rig.file(), "").expect("write");
rig
}
fn file(&self) -> PathBuf {
self.dir.path().join("connections")
}
async fn pass(&mut self, perms: &permissions::Stored, after: Duration) -> Result<()> {
let (file, now) = (self.file(), self.start + after);
pass_with(
perms,
&mut self.answers,
&file,
now,
self.bridge,
self.dns.resolver(),
)
.await
}
fn text(&self) -> String {
std::fs::read_to_string(self.file()).expect("the file is there")
}
}
const T0: Duration = Duration::ZERO;
#[tokio::test]
async fn a_name_is_allowed_at_every_address_it_resolves_to() {
let mut rig = Rig::new();
rig.dns
.set("muede-pc2", &["192.168.23.232", "192.168.23.183"]);
let perms = permissions::Stored::from([("atlas".to_owned(), pc2("true"))]);
rig.pass(&perms, T0).await.expect("renders");
assert_eq!(
rig.text(),
"atlas 192.168.23.183 2222\natlas 192.168.23.232 2222\n"
);
}
#[tokio::test]
async fn an_ip_literal_passes_through_without_a_lookup() {
let mut rig = Rig::new();
let perms = permissions::Stored::from([(
"atlas".to_owned(),
record(
r#"{"ssh":true,"ssh_connections":[{"name":"x","user":"root","host":"10.1.2.3","port":22}]}"#,
),
)]);
rig.pass(&perms, T0).await.expect("renders");
assert_eq!(rig.text(), "atlas 10.1.2.3 22\n");
assert_eq!(rig.dns.calls(), 0);
}
#[tokio::test]
async fn the_ssh_switch_off_or_unset_allows_nothing() {
for ssh in ["false", "null"] {
let mut rig = Rig::new();
rig.dns.set("muede-pc2", &["192.168.23.232"]);
let perms = permissions::Stored::from([("atlas".to_owned(), pc2(ssh))]);
rig.pass(&perms, T0).await.expect("renders");
assert_eq!(rig.text(), "", "ssh = {ssh}");
assert_eq!(rig.dns.calls(), 0, "a switched-off agent costs no lookup");
}
// Control: the same record with the switch on is allowed.
let mut rig = Rig::new();
rig.dns.set("muede-pc2", &["192.168.23.232"]);
let perms = permissions::Stored::from([("atlas".to_owned(), pc2("true"))]);
rig.pass(&perms, T0).await.expect("renders");
assert_eq!(rig.text(), "atlas 192.168.23.232 2222\n");
}
#[tokio::test]
async fn switching_ssh_off_removes_the_agents_lines_and_only_its_own() {
let mut rig = Rig::new();
rig.dns.set("muede-pc2", &["192.168.23.232"]);
let mut perms = permissions::Stored::from([
("atlas".to_owned(), pc2("true")),
("argus".to_owned(), pc2("true")),
]);
rig.pass(&perms, T0).await.expect("renders");
assert_eq!(rig.text().lines().count(), 2);
perms.insert("argus".to_owned(), pc2("false"));
rig.pass(&perms, T0).await.expect("renders");
assert_eq!(rig.text(), "atlas 192.168.23.232 2222\n");
}
#[tokio::test]
async fn an_answer_is_kept_for_ten_minutes_then_asked_again() {
let mut rig = Rig::new();
rig.dns.set("muede-pc2", &["192.168.23.232"]);
let perms = permissions::Stored::from([("atlas".to_owned(), pc2("true"))]);
rig.pass(&perms, T0).await.expect("renders");
rig.dns.set("muede-pc2", &["192.168.23.50"]);
rig.pass(&perms, Duration::from_secs(599))
.await
.expect("renders");
assert_eq!(rig.dns.calls(), 1, "not asked before the ten minutes");
assert_eq!(rig.text(), "atlas 192.168.23.232 2222\n");
rig.pass(&perms, RESOLVE_EVERY).await.expect("renders");
assert_eq!(rig.dns.calls(), 2);
assert_eq!(rig.text(), "atlas 192.168.23.50 2222\n");
}
#[tokio::test]
async fn a_host_a_record_change_names_is_resolved_at_once() {
let mut rig = Rig::new();
rig.dns.set("muede-pc2", &["192.168.23.232"]);
rig.dns.set("other", &["192.168.23.9"]);
let mut perms = permissions::Stored::from([("atlas".to_owned(), pc2("true"))]);
rig.pass(&perms, T0).await.expect("renders");
perms.insert(
"atlas".to_owned(),
record(
r#"{"ssh":true,"ssh_connections":[{"name":"x","user":"root","host":"other","port":22}]}"#,
),
);
rig.pass(&perms, Duration::from_secs(30))
.await
.expect("renders");
assert_eq!(rig.text(), "atlas 192.168.23.9 22\n");
assert_eq!(
rig.answers.by_host.keys().collect::<Vec<_>>(),
["other"],
"the answer of a host no record names is dropped"
);
}
#[tokio::test]
async fn a_failing_resolver_keeps_the_last_answer_and_asks_again_next_pass() {
let mut rig = Rig::new();
rig.dns.set("muede-pc2", &["192.168.23.232"]);
let perms = permissions::Stored::from([("atlas".to_owned(), pc2("true"))]);
rig.pass(&perms, T0).await.expect("renders");
rig.dns.fail("muede-pc2");
rig.pass(&perms, RESOLVE_EVERY).await.expect("renders");
assert_eq!(rig.text(), "atlas 192.168.23.232 2222\n", "last good kept");
// The failed ask is retried at the next pass, not in ten minutes.
let calls = rig.dns.calls();
rig.pass(&perms, RESOLVE_EVERY + INTERVAL)
.await
.expect("renders");
assert_eq!(rig.dns.calls(), calls + 1);
assert_eq!(rig.text(), "atlas 192.168.23.232 2222\n");
// And it recovers.
rig.dns.set("muede-pc2", &["192.168.23.60"]);
rig.pass(&perms, RESOLVE_EVERY + INTERVAL * 2)
.await
.expect("renders");
assert_eq!(rig.text(), "atlas 192.168.23.60 2222\n");
}
#[tokio::test]
async fn a_name_that_never_resolved_allows_nothing() {
let mut rig = Rig::new();
let perms = permissions::Stored::from([("atlas".to_owned(), pc2("true"))]);
rig.pass(&perms, T0).await.expect("renders");
assert_eq!(rig.text(), "");
}
#[tokio::test]
async fn a_failure_is_warned_once_until_the_host_resolves_again() {
let mut rig = Rig::new();
let perms = permissions::Stored::from([("atlas".to_owned(), pc2("true"))]);
for n in 0..3 {
rig.pass(&perms, INTERVAL * n).await.expect("renders");
}
assert_eq!(
rig.answers.warned.iter().collect::<Vec<_>>(),
["muede-pc2"],
"one entry however many passes failed"
);
rig.dns.set("muede-pc2", &["192.168.23.232"]);
rig.pass(&perms, INTERVAL * 3).await.expect("renders");
assert!(rig.answers.warned.is_empty(), "armed again by a success");
}
#[tokio::test]
async fn addresses_no_remote_machine_has_and_ipv6_are_dropped() {
let mut rig = Rig::new();
rig.dns.set(
"muede-pc2",
&[
"127.0.0.1",
"169.254.1.1",
"0.0.0.0",
"224.0.0.1",
"255.255.255.255",
"fd00::1",
"192.168.23.232",
],
);
let perms = permissions::Stored::from([("atlas".to_owned(), pc2("true"))]);
rig.pass(&perms, T0).await.expect("renders");
assert_eq!(rig.text(), "atlas 192.168.23.232 2222\n");
}
#[tokio::test]
async fn an_agent_name_that_would_break_the_line_is_skipped() {
let mut rig = Rig::new();
rig.dns.set("muede-pc2", &["192.168.23.232"]);
let perms = permissions::Stored::from([
("bad name\n".to_owned(), pc2("true")),
("atlas".to_owned(), pc2("true")),
]);
rig.pass(&perms, T0).await.expect("renders");
assert_eq!(rig.text(), "atlas 192.168.23.232 2222\n");
}
#[tokio::test]
async fn an_unchanged_pass_leaves_the_file_alone_and_it_is_group_readable() {
use std::os::unix::fs::{MetadataExt as _, PermissionsExt as _};
let mut rig = Rig::new();
rig.dns.set("muede-pc2", &["192.168.23.232"]);
let perms = permissions::Stored::from([("atlas".to_owned(), pc2("true"))]);
rig.pass(&perms, T0).await.expect("renders");
let meta = std::fs::metadata(rig.file()).expect("stat");
assert_eq!(meta.permissions().mode() & 0o777, 0o640);
rig.pass(&perms, INTERVAL).await.expect("renders");
assert_eq!(
std::fs::metadata(rig.file()).expect("stat").ino(),
meta.ino(),
"nothing was written"
);
}
fn two_hosts(first: &str, second: &str) -> permissions::Stored {
permissions::Stored::from([(
"atlas".to_owned(),
record(&format!(
r#"{{"ssh":true,"ssh_connections":[{{"name":"a","user":"root","host":"{first}","port":22}},{{"name":"b","user":"root","host":"{second}","port":2222}}]}}"#
)),
)])
}
/// A lookup that never answers counts as failed after the timeout: the
/// file is still written, with the other host's addresses, and the hung
/// host keeps the answer it had.
#[tokio::test(start_paused = true)]
async fn a_lookup_that_never_answers_does_not_hold_up_the_file() {
let mut rig = Rig::new();
rig.dns.set("fast", &["192.168.23.1"]);
rig.dns.set("slow", &["192.168.23.2"]);
let perms = two_hosts("fast", "slow");
rig.pass(&perms, T0).await.expect("renders");
assert_eq!(
rig.text(),
"atlas 192.168.23.1 22\natlas 192.168.23.2 2222\n"
);
// `slow` is due again and now hangs; `fast` changes its address.
rig.dns.hang("slow");
rig.dns.set("fast", &["192.168.23.9"]);
let began = tokio::time::Instant::now();
rig.pass(&perms, RESOLVE_EVERY).await.expect("renders");
assert!(
began.elapsed() <= RESOLVE_TIMEOUT,
"bounded by one timeout: {:?}",
began.elapsed()
);
assert_eq!(
rig.text(),
"atlas 192.168.23.2 2222\natlas 192.168.23.9 22\n",
"the other host's change landed and the hung host kept its answer"
);
assert_eq!(rig.answers.warned.len(), 1, "one warning for the timeout");
// A host that has never answered gets nothing, and the rest still land.
let mut fresh = Rig::new();
fresh.dns.set("fast", &["192.168.23.1"]);
fresh.dns.hang("slow");
fresh.pass(&perms, T0).await.expect("renders");
assert_eq!(fresh.text(), "atlas 192.168.23.1 22\n");
}
/// Several hung hosts cost one timeout together, not one each.
#[tokio::test(start_paused = true)]
async fn hung_lookups_run_together() {
let mut rig = Rig::new();
let hosts: Vec<String> = (0..6).map(|n| format!("h{n}")).collect();
for h in &hosts {
rig.dns.hang(h);
}
let conns: Vec<String> = hosts
.iter()
.enumerate()
.map(|(n, h)| format!(r#"{{"name":"c{n}","user":"root","host":"{h}","port":22}}"#))
.collect();
let perms = permissions::Stored::from([(
"atlas".to_owned(),
record(&format!(
r#"{{"ssh":true,"ssh_connections":[{}]}}"#,
conns.join(",")
)),
)]);
let began = tokio::time::Instant::now();
rig.pass(&perms, T0).await.expect("renders");
assert_eq!(began.elapsed(), RESOLVE_TIMEOUT);
assert_eq!(rig.dns.calls(), 6);
}
#[test]
fn a_subnet_is_read_from_the_bridge_address_and_prefix() {
let net = Subnet::parse("10.42.0.1/24").expect("parses");
assert!(net.contains("10.42.0.200".parse().unwrap()));
assert!(net.contains("10.42.0.1".parse().unwrap()));
assert!(!net.contains("10.42.1.1".parse().unwrap()));
let wide = Subnet::parse("10.42.5.9/15").expect("parses");
assert!(wide.contains("10.43.255.1".parse().unwrap()));
assert!(!wide.contains("10.44.0.1".parse().unwrap()));
assert!(
Subnet::parse("0.0.0.0/0")
.expect("parses")
.contains("8.8.8.8".parse().unwrap())
);
for bad in ["10.42.0.1", "10.42.0.1/33", "nope/24", "10.42.0.1/x", ""] {
assert_eq!(Subnet::parse(bad), None, "{bad:?}");
}
}
#[tokio::test]
async fn an_address_inside_the_bridge_subnet_is_dropped_and_warned_once() {
let mut rig = Rig::new();
rig.bridge = Subnet::parse("10.42.0.1/24");
rig.dns.set("muede-pc2", &["10.42.0.7", "192.168.23.232"]);
let perms = permissions::Stored::from([("atlas".to_owned(), pc2("true"))]);
rig.pass(&perms, T0).await.expect("renders");
assert_eq!(rig.text(), "atlas 192.168.23.232 2222\n");
assert!(rig.answers.warned_bridge.contains("muede-pc2"));
// Control: without the subnet the same answer allows both.
let mut open = Rig::new();
open.dns.set("muede-pc2", &["10.42.0.7", "192.168.23.232"]);
open.pass(&perms, T0).await.expect("renders");
assert_eq!(
open.text(),
"atlas 10.42.0.7 2222\natlas 192.168.23.232 2222\n"
);
assert!(open.answers.warned_bridge.is_empty());
// The warning is armed again once the host leaves the subnet.
rig.dns.set("muede-pc2", &["192.168.23.232"]);
rig.pass(&perms, RESOLVE_EVERY).await.expect("renders");
assert!(rig.answers.warned_bridge.is_empty());
}
#[tokio::test]
async fn an_ip_literal_inside_the_bridge_subnet_allows_nothing() {
let mut rig = Rig::new();
rig.bridge = Subnet::parse("10.42.0.1/24");
let perms = permissions::Stored::from([(
"atlas".to_owned(),
record(
r#"{"ssh":true,"ssh_connections":[{"name":"x","user":"root","host":"10.42.0.9","port":22}]}"#,
),
)]);
rig.pass(&perms, T0).await.expect("renders");
assert_eq!(rig.text(), "");
}
}