egress: let every agent reach the swarm's own endpoints under forward reject
With the egress intercept on, the forward chain rejects what `agent_allow` does not hold, and `agent_allow` only held an agent's ssh connections. An agent whose secret store, queue, logs or metrics endpoint is not on the bridge was refused, so no agent started on a hive away from the swarm host. hive-c0re's egress worker now also renders a `* <ipv4> <port>` line for each endpoint URL it hands to agents (HIVE_AGENT_BAO_ADDR, HIVE_AGENT_LOGS_QUERY_URL, HIVE_AGENT_METRICS_URL, HIVE_AGENT_NATS_URL), resolved like the ssh hosts. `hive-priv egress-pin` expands a `*` line to the MAC of every agent port. An older hive-priv skips the line, as it skips any unknown name. Closes #6902
This commit is contained in:
parent
e77758d0d0
commit
ecfc21b962
1 changed files with 205 additions and 13 deletions
|
|
@ -10,6 +10,10 @@
|
|||
//! MAC of the agent's port, which only hive-priv can tell. IPv6 answers are
|
||||
//! left out: the agent bridge has no IPv6 addressing.
|
||||
//!
|
||||
//! The swarm's own endpoints (store, queue, logs, metrics: [`ENDPOINT_ENVS`])
|
||||
//! render as `* <ip> <port>`, `*` standing for every agent. An older
|
||||
//! `hive-priv` skips such a line.
|
||||
//!
|
||||
//! 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.
|
||||
|
|
@ -62,6 +66,52 @@ pub const ENV_FILE: &str = "HIVE_EGRESS_CONNECTIONS_FILE";
|
|||
/// (`10.42.0.1/24`); `nix/host-modules/hive-network.nix` sets it for the daemon.
|
||||
const ENV_SUBNET: &str = "HIVE_NETWORK_SUBNET";
|
||||
|
||||
/// The agent column of a line for every agent.
|
||||
const ALL_AGENTS: &str = "*";
|
||||
|
||||
/// The environment variables holding the URLs of the swarm endpoints every
|
||||
/// agent uses, as the agents are configured with them
|
||||
/// (`nix/host-modules/hive-c0re/environment.nix`): the secret store, the logs
|
||||
/// query, the metrics query and the swarm queue.
|
||||
const ENDPOINT_ENVS: [&str; 4] = [
|
||||
"HIVE_AGENT_BAO_ADDR",
|
||||
"HIVE_AGENT_LOGS_QUERY_URL",
|
||||
"HIVE_AGENT_METRICS_URL",
|
||||
"HIVE_AGENT_NATS_URL",
|
||||
];
|
||||
|
||||
/// The host and TCP port of a URL: its port, or the scheme's own when it names
|
||||
/// none (443 for `https`, 80 for `http`, 4222 for `tls` and `nats`). `None` for
|
||||
/// any other scheme, an IPv6 literal (the bridge has no IPv6) and an empty host.
|
||||
fn endpoint_of(url: &str) -> Option<(String, u16)> {
|
||||
let (scheme, rest) = url.split_once("://")?;
|
||||
let default = match scheme {
|
||||
"https" => 443,
|
||||
"http" => 80,
|
||||
"tls" | "nats" => 4222,
|
||||
_ => return None,
|
||||
};
|
||||
let authority = rest.split(['/', '?', '#']).next()?;
|
||||
let authority = authority.rsplit_once('@').map_or(authority, |(_, a)| a);
|
||||
if authority.starts_with('[') {
|
||||
return None;
|
||||
}
|
||||
let (host, port) = match authority.split_once(':') {
|
||||
Some((host, port)) => (host, port.parse().ok().filter(|p| *p != 0)?),
|
||||
None => (authority, default),
|
||||
};
|
||||
(!host.is_empty()).then(|| (host.to_owned(), port))
|
||||
}
|
||||
|
||||
/// The endpoints [`ENDPOINT_ENVS`] name in this process's environment.
|
||||
fn endpoints_from_env() -> BTreeSet<(String, u16)> {
|
||||
ENDPOINT_ENVS
|
||||
.iter()
|
||||
.filter_map(|name| std::env::var(name).ok())
|
||||
.filter_map(|url| endpoint_of(&url))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// An IPv4 subnet.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
struct Subnet {
|
||||
|
|
@ -94,9 +144,17 @@ struct Target {
|
|||
port: u16,
|
||||
}
|
||||
|
||||
/// The connections of every agent whose `ssh` switch is on.
|
||||
fn targets(perms: &permissions::Stored) -> BTreeSet<Target> {
|
||||
let mut out = BTreeSet::new();
|
||||
/// The connections of every agent whose `ssh` switch is on, and the
|
||||
/// `endpoints` every agent may reach.
|
||||
fn targets(perms: &permissions::Stored, endpoints: &BTreeSet<(String, u16)>) -> BTreeSet<Target> {
|
||||
let mut out: BTreeSet<Target> = endpoints
|
||||
.iter()
|
||||
.map(|(host, port)| Target {
|
||||
agent: ALL_AGENTS.to_owned(),
|
||||
host: host.clone(),
|
||||
port: *port,
|
||||
})
|
||||
.collect();
|
||||
for (agent, record) in perms {
|
||||
if record.ssh != Some(true) || hive_types::Ident::parse(agent).is_err() {
|
||||
continue;
|
||||
|
|
@ -264,6 +322,7 @@ fn render(targets: &BTreeSet<Target>, answers: &Answers) -> String {
|
|||
/// if it changed.
|
||||
async fn pass_with<F, Fut>(
|
||||
perms: &permissions::Stored,
|
||||
endpoints: &BTreeSet<(String, u16)>,
|
||||
answers: &mut Answers,
|
||||
file: &Path,
|
||||
now: Instant,
|
||||
|
|
@ -274,7 +333,7 @@ where
|
|||
F: Fn(String) -> Fut,
|
||||
Fut: Future<Output = Result<Vec<IpAddr>>>,
|
||||
{
|
||||
let wanted = targets(perms);
|
||||
let wanted = targets(perms, endpoints);
|
||||
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);
|
||||
|
|
@ -315,6 +374,7 @@ pub fn spawn(mut shutdown: tokio::sync::watch::Receiver<bool>) {
|
|||
"egress connections: {ENV_SUBNET} is unset or invalid, so the bridge subnet is not excluded"
|
||||
);
|
||||
}
|
||||
let endpoints = endpoints_from_env();
|
||||
tokio::spawn(async move {
|
||||
let mut health =
|
||||
SweepHealth::new("egress_connections_render", "warn", FAILURES_BEFORE_WARNING);
|
||||
|
|
@ -324,6 +384,7 @@ pub fn spawn(mut shutdown: tokio::sync::watch::Receiver<bool>) {
|
|||
Ok(perms) => {
|
||||
pass_with(
|
||||
&perms,
|
||||
&endpoints,
|
||||
&mut answers,
|
||||
&file,
|
||||
Instant::now(),
|
||||
|
|
@ -435,6 +496,7 @@ mod tests {
|
|||
dns: Dns,
|
||||
start: Instant,
|
||||
bridge: Option<Subnet>,
|
||||
endpoints: BTreeSet<(String, u16)>,
|
||||
}
|
||||
|
||||
impl Rig {
|
||||
|
|
@ -445,6 +507,7 @@ mod tests {
|
|||
dns: Dns::default(),
|
||||
start: Instant::now(),
|
||||
bridge: None,
|
||||
endpoints: BTreeSet::new(),
|
||||
};
|
||||
// The empty file `systemd-tmpfiles` creates before c0re starts.
|
||||
std::fs::write(rig.file(), "").expect("write");
|
||||
|
|
@ -457,6 +520,7 @@ mod tests {
|
|||
let (file, now) = (self.file(), self.start + after);
|
||||
pass_with(
|
||||
perms,
|
||||
&self.endpoints,
|
||||
&mut self.answers,
|
||||
&file,
|
||||
now,
|
||||
|
|
@ -472,6 +536,62 @@ mod tests {
|
|||
|
||||
const T0: Duration = Duration::ZERO;
|
||||
|
||||
#[test]
|
||||
fn an_endpoint_is_the_host_and_port_of_a_url() {
|
||||
let e = |u: &str| endpoint_of(u);
|
||||
let some = |h: &str, p: u16| Some((h.to_owned(), p));
|
||||
assert_eq!(e("https://bao.t.local:8200"), some("bao.t.local", 8200));
|
||||
assert_eq!(
|
||||
e("https://logs.t.local/select/logsql/query"),
|
||||
some("logs.t.local", 443)
|
||||
);
|
||||
assert_eq!(e("http://metrics.t.local"), some("metrics.t.local", 80));
|
||||
assert_eq!(e("tls://nats.t.local:4222"), some("nats.t.local", 4222));
|
||||
assert_eq!(e("nats://nats.t.local"), some("nats.t.local", 4222));
|
||||
assert_eq!(e("https://u:p@10.0.0.1:8443/x?y#z"), some("10.0.0.1", 8443));
|
||||
for bad in [
|
||||
"bao.t.local:8200",
|
||||
"ftp://x/",
|
||||
"https://",
|
||||
"https://[::1]:8200",
|
||||
"https://x:0",
|
||||
"https://x:nope",
|
||||
] {
|
||||
assert_eq!(e(bad), None, "{bad}");
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn the_endpoints_of_the_swarm_are_allowed_to_every_agent() {
|
||||
let mut rig = Rig::new();
|
||||
rig.dns.set("bao.t.local", &["10.77.0.1"]);
|
||||
rig.dns.set("logs.t.local", &["10.77.0.2", "fd00::2"]);
|
||||
rig.endpoints = BTreeSet::from([
|
||||
("bao.t.local".to_owned(), 8200),
|
||||
("logs.t.local".to_owned(), 443),
|
||||
]);
|
||||
rig.pass(&permissions::Stored::new(), T0)
|
||||
.await
|
||||
.expect("renders");
|
||||
assert_eq!(rig.text(), "* 10.77.0.1 8200\n* 10.77.0.2 443\n");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_endpoint_that_does_not_resolve_keeps_its_last_answer_and_the_ssh_lines_stay() {
|
||||
let mut rig = Rig::new();
|
||||
rig.dns.set("bao.t.local", &["10.77.0.1"]);
|
||||
rig.dns.set("muede-pc2", &["192.168.23.232"]);
|
||||
rig.endpoints = BTreeSet::from([("bao.t.local".to_owned(), 8200)]);
|
||||
let perms = permissions::Stored::from([("atlas".to_owned(), pc2("true"))]);
|
||||
rig.pass(&perms, T0).await.expect("renders");
|
||||
assert_eq!(rig.text(), "* 10.77.0.1 8200\natlas 192.168.23.232 2222\n");
|
||||
rig.dns.fail("bao.t.local");
|
||||
rig.pass(&perms, RESOLVE_EVERY + Duration::from_secs(1))
|
||||
.await
|
||||
.expect("renders");
|
||||
assert_eq!(rig.text(), "* 10.77.0.1 8200\natlas 192.168.23.232 2222\n");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_name_is_allowed_at_every_address_it_resolves_to() {
|
||||
let mut rig = Rig::new();
|
||||
|
|
|
|||
Loading…
Reference in a new issue