Watch
0
0
Fork
You've already forked hyperhive
0

egress: the forward chain allows each agent's declared ssh connections

This commit is contained in:
atlas 2026-11-20 03:07:14 +01:00
commit 8ece0b7e0b

View file

@ -1645,6 +1645,57 @@ let
&& !(lib.any (l: lib.hasSuffix " drop" l || lib.hasSuffix " reject" l) rules)
&& lib.hasInfix "chain input" text;
}
{
# An agent's declared SSH connections are accepted before the chain
# counts, logs or refuses, in `log` and in `reject`. The set takes MAC,
# address and port. The host without a controller has the set and no
# chain (the presence control) and no wiring of the connections file.
# hive-c0re, the units that fill the sets and the path unit that runs a
# pass are told the same file.
name = "the egress forward chain accepts the agents' declared ssh connections before it logs or refuses";
ok =
let
rulesetOf = m: m.services.hyperhive.network.egress.ruleset;
inetOf = m: lib.head (lib.splitString "table bridge hive_egress {" (rulesetOf m));
rulesOf =
m:
lib.filter (l: l != "" && l != "}") (
map lib.trim (lib.splitString "\n" (lib.last (lib.splitString "chain forward" (inetOf m))))
);
idxIn = rules: pat: lib.lists.findFirstIndex (lib.hasInfix pat) 1000 rules;
allow = ''iifname "hive-br0" oifname != "hive-br0" meta nfproto ipv4 meta l4proto tcp ether saddr . ip daddr . tcp dport @agent_allow accept'';
set = "set agent_allow { type ether_addr . ipv4_addr . inet_service; }";
inOrder =
m:
let
rules = rulesOf m;
idx = idxIn rules;
in
lib.elem allow rules
&& lib.length (lib.filter (r: r == allow) rules) == 1
&& idx "ether saddr @direct_mac accept" < idx "@agent_allow accept"
&& idx "@agent_allow accept" < idx "counter"
&& idx "@agent_allow accept" < idx "update @log_v4"
&& lib.hasInfix set (inetOf m);
file = "/var/lib/hive-squid/ssh-connections";
envOf = m: name: m.systemd.services.${name}.environment;
in
inOrder bare
&& inOrder egressForwardReject
&& lib.hasInfix set (inetOf egressForwardAccept)
&& !(lib.hasInfix "@agent_allow accept" (rulesetOf egressForwardAccept))
&& bare.services.hyperhive.deploy.hive-controller.egress.connectionsFile == file
&& bare.systemd.services.hive-c0re.environment.HIVE_EGRESS_CONNECTIONS_FILE == file
&& lib.all (name: ((envOf bare name).HIVE_EGRESS_CONNECTIONS or null) == file) [
"hive-egress"
"hive-egress-pin@"
"hive-egress-pin"
]
&& bare.systemd.paths.hive-egress-connections.pathConfig.PathChanged == file
&& bare.systemd.paths.hive-egress-connections.pathConfig.Unit == "hive-egress-pin.service"
&& !((envOf egressForwardAccept "hive-egress") ? HIVE_EGRESS_CONNECTIONS)
&& !(egressForwardAccept.systemd.paths ? hive-egress-connections);
}
{
# The bridge table drops what a port sends from an address that is not
# its own, and logs it. The ruleset is read as text: each `spoof_<reason>`
@ -1807,7 +1858,7 @@ let
chain = lib.last (lib.splitString "chain forward" text);
rules = lib.filter (l: l != "" && l != "}") (map lib.trim (lib.splitString "\n" chain));
idx = pat: lib.lists.findFirstIndex (lib.hasInfix pat) 1000 rules;
tcp = lib.filter (lib.hasInfix " meta l4proto tcp ") rules;
tcp = lib.filter (l: lib.hasInfix " meta l4proto tcp " l && !(lib.hasInfix "@agent_allow" l)) rules;
other = lib.filter (lib.hasInfix " meta l4proto != tcp") rules;
logged = lib.filter (lib.hasInfix "log prefix") rules;
bareText = bare.services.hyperhive.network.egress.ruleset;
@ -1820,7 +1871,7 @@ let
&& !(lib.hasInfix "wg-hive" text)
&& idx "ether saddr @direct_mac accept" < idx "counter"
&& idx "counter" < idx "reject"
&& lib.length (lib.filter (lib.hasSuffix " accept") rules) == 1
&& lib.length (lib.filter (lib.hasSuffix " accept") rules) == 2
&& lib.length tcp == 4
&& lib.length other == 4
&& lib.all (lib.hasSuffix " reject with tcp reset") tcp