egress: the forward chain allows each agent's declared ssh connections
This commit is contained in:
parent
934da6f31f
commit
8ece0b7e0b
1 changed files with 1110 additions and 20 deletions
|
|
@ -99,6 +99,12 @@ const LOCK: &str = "/run/lock/hive-egress-pin.lock";
|
|||
/// `HIVE_EGRESS_UNRESTRICTED` names. Unset or unreadable lists no agent.
|
||||
const UNRESTRICTED_ENV: &str = "HIVE_EGRESS_UNRESTRICTED";
|
||||
|
||||
/// The file of the agents' allowed connections, one `<agent> <ipv4> <port>` per
|
||||
/// line, that `HIVE_EGRESS_CONNECTIONS` names. Unset leaves the `agent_allow` set
|
||||
/// alone: a ruleset that does not declare it still loads. Set but unreadable
|
||||
/// lists no connection.
|
||||
const CONNECTIONS_ENV: &str = "HIVE_EGRESS_CONNECTIONS";
|
||||
|
||||
/// The prefix of an agent container's machine name.
|
||||
const AGENT_PREFIX: &str = "h-";
|
||||
|
||||
|
|
@ -315,9 +321,66 @@ pub(crate) fn direct_macs(pins: &[Pin], unrestricted: &[String]) -> Vec<Mac> {
|
|||
macs
|
||||
}
|
||||
|
||||
/// One connection an agent may open: its name, the address and the TCP port.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub(crate) struct Connection {
|
||||
pub(crate) agent: String,
|
||||
pub(crate) ip: std::net::Ipv4Addr,
|
||||
pub(crate) port: u16,
|
||||
}
|
||||
|
||||
/// The connections in `text`, one `<agent> <ipv4> <port>` per line. A line of
|
||||
/// another shape, a port 0 or a name that is no identifier is skipped.
|
||||
pub(crate) fn parse_connections(text: &str) -> Vec<Connection> {
|
||||
text.lines()
|
||||
.filter_map(|line| {
|
||||
let mut cols = line.split_whitespace();
|
||||
let (agent, ip, port) = (cols.next()?, cols.next()?, cols.next()?);
|
||||
if cols.next().is_some() {
|
||||
return None;
|
||||
}
|
||||
hive_types::Ident::parse(agent).ok()?;
|
||||
Some(Connection {
|
||||
agent: agent.to_owned(),
|
||||
ip: ip.parse().ok()?,
|
||||
port: port.parse().ok().filter(|p| *p != 0)?,
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The `agent_allow` elements of `connections`: for each, one per MAC bound to
|
||||
/// a port of the agent's container (`h-<agent>`). An agent with no binding has
|
||||
/// none, so nothing is allowed for it until its port is bound.
|
||||
pub(crate) fn connection_elements(pins: &[Pin], connections: &[Connection]) -> String {
|
||||
let mut rows: Vec<(Mac, std::net::Ipv4Addr, u16)> = Vec::new();
|
||||
for c in connections {
|
||||
for p in pins {
|
||||
if p.machine.strip_prefix(AGENT_PREFIX) == Some(c.agent.as_str()) {
|
||||
rows.push((p.mac, c.ip, c.port));
|
||||
}
|
||||
}
|
||||
}
|
||||
rows.sort_by_key(|(m, ip, port)| (m.0, *ip, *port));
|
||||
rows.dedup();
|
||||
let mut script = String::new();
|
||||
for (mac, ip, port) in rows {
|
||||
let _ = writeln!(
|
||||
script,
|
||||
"add element {INET_TABLE} agent_allow {{ {mac} . {ip} . {port} }}"
|
||||
);
|
||||
}
|
||||
script
|
||||
}
|
||||
|
||||
/// The nft input that adds the bindings `pins` to the two sets of the bridge
|
||||
/// table and the [`direct_macs`] to the exemption set of the inet table.
|
||||
pub(crate) fn pin_elements(pins: &[Pin], unrestricted: &[String]) -> String {
|
||||
/// table, the [`direct_macs`] to the exemption set of the inet table and, when
|
||||
/// `connections` is given, its [`connection_elements`].
|
||||
pub(crate) fn pin_elements(
|
||||
pins: &[Pin],
|
||||
unrestricted: &[String],
|
||||
connections: Option<&[Connection]>,
|
||||
) -> String {
|
||||
let mut script = String::new();
|
||||
for Pin { port, mac, .. } in pins {
|
||||
let _ = writeln!(
|
||||
|
|
@ -330,17 +393,30 @@ pub(crate) fn pin_elements(pins: &[Pin], unrestricted: &[String]) -> String {
|
|||
for mac in direct_macs(pins, unrestricted) {
|
||||
let _ = writeln!(script, "add element {INET_TABLE} direct_mac {{ {mac} }}");
|
||||
}
|
||||
if let Some(connections) = connections {
|
||||
script.push_str(&connection_elements(pins, connections));
|
||||
}
|
||||
script
|
||||
}
|
||||
|
||||
/// The nft input that makes the bindings and the exemptions exactly those of
|
||||
/// `pins` and `unrestricted`, in one transaction: a reader never sees a set
|
||||
/// empty in between.
|
||||
pub(crate) fn pin_script(pins: &[Pin], unrestricted: &[String]) -> String {
|
||||
/// The nft input that makes the bindings, the exemptions and, when
|
||||
/// `connections` is given, the allowed connections exactly those of `pins`,
|
||||
/// `unrestricted` and `connections`, in one transaction: a reader never sees a
|
||||
/// set empty in between.
|
||||
pub(crate) fn pin_script(
|
||||
pins: &[Pin],
|
||||
unrestricted: &[String],
|
||||
connections: Option<&[Connection]>,
|
||||
) -> String {
|
||||
let flush_connections = if connections.is_some() {
|
||||
format!("flush set {INET_TABLE} agent_allow\n")
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
format!(
|
||||
"flush set {TABLE} port_mac\nflush set {TABLE} port_chaddr\n\
|
||||
flush set {INET_TABLE} direct_mac\n{}",
|
||||
pin_elements(pins, unrestricted)
|
||||
flush set {INET_TABLE} direct_mac\n{flush_connections}{}",
|
||||
pin_elements(pins, unrestricted, connections)
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -360,6 +436,15 @@ fn unrestricted_names() -> Vec<String> {
|
|||
.collect()
|
||||
}
|
||||
|
||||
/// The connections in the file `HIVE_EGRESS_CONNECTIONS` names, or `None` when
|
||||
/// it names none. A file that cannot be read lists no connection.
|
||||
fn connection_list() -> Option<Vec<Connection>> {
|
||||
let path = std::env::var_os(CONNECTIONS_ENV).filter(|p| !p.is_empty())?;
|
||||
Some(parse_connections(
|
||||
&std::fs::read_to_string(path).unwrap_or_default(),
|
||||
))
|
||||
}
|
||||
|
||||
/// The agent whose container holds the IPv4 address `ip`, as dnsmasq's lease
|
||||
/// file and the bindings of the ports say: the lease names a MAC, the MAC is
|
||||
/// bound to one port, and that port's machine is `h-<agent>`. An address in the
|
||||
|
|
@ -900,7 +985,11 @@ fn pin(args: &[String]) -> Result<()> {
|
|||
let survey = {
|
||||
let _lock = lock(deadline)?;
|
||||
let survey = survey(&bridge)?;
|
||||
nft(&pin_script(&survey.resolution.pins, &unrestricted_names()))?;
|
||||
nft(&pin_script(
|
||||
&survey.resolution.pins,
|
||||
&unrestricted_names(),
|
||||
connection_list().as_deref(),
|
||||
))?;
|
||||
commit(&survey)?;
|
||||
survey
|
||||
};
|
||||
|
|
@ -964,7 +1053,11 @@ fn load(args: &[String]) -> Result<()> {
|
|||
let _lock = lock(Instant::now() + WAIT)?;
|
||||
let found = survey(&bridge);
|
||||
let pins = match &found {
|
||||
Ok(found) => pin_elements(&found.resolution.pins, &unrestricted_names()),
|
||||
Ok(found) => pin_elements(
|
||||
&found.resolution.pins,
|
||||
&unrestricted_names(),
|
||||
connection_list().as_deref(),
|
||||
),
|
||||
Err(e) => {
|
||||
eprintln!("{LOAD}: binding the ports failed, loading without bindings: {e:#}");
|
||||
String::new()
|
||||
|
|
@ -1241,7 +1334,7 @@ mod tests {
|
|||
#[test]
|
||||
fn the_ruleset_loads_with_the_bindings_and_leases_in_one_script() {
|
||||
let calls = std::cell::RefCell::new(Vec::new());
|
||||
let pins = pin_elements(&[pin("vb-a", 10, 10, "a")], &[]);
|
||||
let pins = pin_elements(&[pin("vb-a", 10, 10, "a")], &[], None);
|
||||
let leases = lease_script("add", "02:00:00:00:00:0a", "10.42.0.10", 90).unwrap();
|
||||
load_ruleset("table bridge hive_egress { }", &pins, &leases, |s| {
|
||||
calls.borrow_mut().push(s.to_string());
|
||||
|
|
@ -1260,7 +1353,7 @@ mod tests {
|
|||
#[test]
|
||||
fn a_lease_load_that_fails_is_a_warning_and_the_bindings_still_load() {
|
||||
let calls = std::cell::RefCell::new(Vec::new());
|
||||
let pins = pin_elements(&[pin("vb-a", 10, 10, "a")], &[]);
|
||||
let pins = pin_elements(&[pin("vb-a", 10, 10, "a")], &[], None);
|
||||
let leases = lease_script("add", "02:00:00:00:00:0a", "10.42.0.10", 90).unwrap();
|
||||
let result = load_ruleset("RULESET", &pins, &leases, |s| {
|
||||
calls.borrow_mut().push(s.to_string());
|
||||
|
|
@ -1284,7 +1377,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn pin_script_replaces_all_three_sets_in_one_script() {
|
||||
let script = pin_script(&[pin("vb-h-a", 10, 10, "h-a")], &[]);
|
||||
let script = pin_script(&[pin("vb-h-a", 10, 10, "h-a")], &[], None);
|
||||
assert_eq!(
|
||||
script,
|
||||
"flush set bridge hive_egress port_mac\n\
|
||||
|
|
@ -1293,7 +1386,7 @@ mod tests {
|
|||
add element bridge hive_egress port_mac { \"vb-h-a\" . 02:00:00:00:00:0a }\n\
|
||||
add element bridge hive_egress port_chaddr { \"vb-h-a\" . 0x02000000000a }\n"
|
||||
);
|
||||
assert!(!pin_script(&[], &[]).contains("add element"));
|
||||
assert!(!pin_script(&[], &[], None).contains("add element"));
|
||||
}
|
||||
|
||||
/// Service containers and the agents the file lists are exempt; an agent the
|
||||
|
|
@ -1310,13 +1403,102 @@ mod tests {
|
|||
assert_eq!(direct_macs(&pins, &listed), vec![mac(2), mac(3)]);
|
||||
// Control: with nothing listed, only the service container stays.
|
||||
assert_eq!(direct_macs(&pins, &[]), vec![mac(3)]);
|
||||
let script = pin_elements(&pins, &listed);
|
||||
let script = pin_elements(&pins, &listed, None);
|
||||
assert!(script.contains("add element inet hive_egress direct_mac { 02:00:00:00:00:02 }"));
|
||||
assert!(script.contains("add element inet hive_egress direct_mac { 02:00:00:00:00:03 }"));
|
||||
assert!(!script.contains("direct_mac { 02:00:00:00:00:01 }"));
|
||||
assert!(!script.contains("direct_mac { 02:00:00:00:00:04 }"));
|
||||
}
|
||||
|
||||
fn conn(agent: &str, ip: &str, port: u16) -> Connection {
|
||||
Connection {
|
||||
agent: agent.into(),
|
||||
ip: ip.parse().unwrap(),
|
||||
port,
|
||||
}
|
||||
}
|
||||
|
||||
/// An agent's connection is allowed from the MAC of that agent's port only,
|
||||
/// not from a container whose name merely starts the same or from a service
|
||||
/// container.
|
||||
#[test]
|
||||
fn a_connection_is_allowed_from_its_agents_mac_only() {
|
||||
let pins = [
|
||||
pin("vb-h-atlas", 10, 1, "h-atlas"),
|
||||
pin("vb-h-atlasx", 11, 2, "h-atlasx"),
|
||||
pin("vb-matrix", 12, 3, "atlas"),
|
||||
];
|
||||
let script = connection_elements(&pins, &[conn("atlas", "192.168.23.232", 2222)]);
|
||||
assert_eq!(
|
||||
script,
|
||||
"add element inet hive_egress agent_allow \
|
||||
{ 02:00:00:00:00:01 . 192.168.23.232 . 2222 }\n"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_connection_of_an_unbound_agent_adds_nothing_and_duplicates_collapse() {
|
||||
let pins = [pin("vb-h-atlas", 10, 1, "h-atlas")];
|
||||
assert_eq!(
|
||||
connection_elements(&pins, &[conn("uma", "10.0.0.1", 22)]),
|
||||
""
|
||||
);
|
||||
let twice = [conn("atlas", "10.0.0.1", 22), conn("atlas", "10.0.0.1", 22)];
|
||||
assert_eq!(connection_elements(&pins, &twice).lines().count(), 1);
|
||||
assert_eq!(connection_elements(&[], &twice), "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_connections_file_is_parsed_line_by_line_and_bad_lines_are_skipped() {
|
||||
let text = "atlas 192.168.23.232 2222\n\
|
||||
argus 10.0.0.1 22\n\
|
||||
\n\
|
||||
atlas not-an-ip 22\n\
|
||||
atlas 10.0.0.2 0\n\
|
||||
atlas 10.0.0.2 70000\n\
|
||||
bad;name 10.0.0.3 22\n\
|
||||
atlas 10.0.0.4 22 extra\n\
|
||||
atlas 10.0.0.5\n";
|
||||
assert_eq!(
|
||||
parse_connections(text),
|
||||
[
|
||||
conn("atlas", "192.168.23.232", 2222),
|
||||
conn("argus", "10.0.0.1", 22)
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/// With a connections file the script flushes `agent_allow` in the same
|
||||
/// transaction that refills it; without one it does not name the set, so a
|
||||
/// ruleset that never declared it still loads.
|
||||
#[test]
|
||||
fn the_script_flushes_and_fills_agent_allow_only_when_a_file_is_named() {
|
||||
let pins = [pin("vb-h-atlas", 10, 1, "h-atlas")];
|
||||
let with = pin_script(&pins, &[], Some(&[conn("atlas", "10.0.0.1", 22)]));
|
||||
let flush = with
|
||||
.find("flush set inet hive_egress agent_allow\n")
|
||||
.expect("flushed");
|
||||
let add = with
|
||||
.find("add element inet hive_egress agent_allow { 02:00:00:00:00:01 . 10.0.0.1 . 22 }")
|
||||
.expect("added");
|
||||
assert!(flush < add, "the flush comes first");
|
||||
// Control: a named file with no connection still flushes the old ones.
|
||||
let empty = pin_script(&pins, &[], Some(&[]));
|
||||
assert!(empty.contains("flush set inet hive_egress agent_allow\n"));
|
||||
assert!(!empty.contains("add element inet hive_egress agent_allow"));
|
||||
let none = pin_script(&pins, &[], None);
|
||||
assert!(!none.contains("agent_allow"), "{none}");
|
||||
assert!(none.contains("flush set inet hive_egress direct_mac"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_load_script_fills_agent_allow_without_flushing_it() {
|
||||
let pins = [pin("vb-h-atlas", 10, 1, "h-atlas")];
|
||||
let script = pin_elements(&pins, &[], Some(&[conn("atlas", "10.0.0.1", 22)]));
|
||||
assert!(script.contains("agent_allow { 02:00:00:00:00:01 . 10.0.0.1 . 22 }"));
|
||||
assert!(!script.contains("flush"));
|
||||
}
|
||||
|
||||
const NOW: u64 = 1_000;
|
||||
|
||||
fn lease_line(expiry: u64, last: u8, ip: &str) -> String {
|
||||
|
|
|
|||
Loading…
Reference in a new issue