feat(network): verify selected uplinks, roll back failures and manage NetBird policy
This commit is contained in:
@@ -144,7 +144,14 @@ def test_client_does_not_request_dns_routes_remote_shell_or_motion():
|
||||
assert "--disable-client-routes" in client.SAFE_FLAGS
|
||||
assert "--disable-server-routes" in client.SAFE_FLAGS
|
||||
assert "--allow-server-ssh=false" in client.SAFE_FLAGS
|
||||
assert "--network-monitor=true" in client.SAFE_FLAGS
|
||||
|
||||
|
||||
def test_platform_native_network_monitor_policy(monkeypatch):
|
||||
for name, expected in (("Linux", "false"), ("FreeBSD", "false"),
|
||||
("Darwin", "false"), ("Windows", "true")):
|
||||
monkeypatch.setattr(client.platform, "system", lambda: name)
|
||||
assert "--network-monitor=" + expected in client.safe_flags()
|
||||
assert set(client.SAFE_FLAGS).issubset(client.safe_flags())
|
||||
|
||||
|
||||
def test_preflight_refuses_redirect_before_contacting_destination():
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
import copy
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from k1link.fleet.netbird import profile_policy as policy
|
||||
|
||||
|
||||
def prepared(tmp_path, monkeypatch):
|
||||
owner = tmp_path / "owner.json"
|
||||
owner.write_text(json.dumps({"server_url": "https://network.example:8443"}))
|
||||
artifact = tmp_path / "policy.pyz"
|
||||
artifact.write_bytes(b"synthetic-package")
|
||||
monkeypatch.setattr(policy.sys, "argv", [str(artifact)])
|
||||
monkeypatch.setattr(policy.os, "geteuid", lambda: 0)
|
||||
monkeypatch.setattr(policy.platform, "system", lambda: "Darwin")
|
||||
monkeypatch.setattr(policy, "RECEIPT", tmp_path / "receipt.json")
|
||||
active = {"username": "operator", "name": "a" * 32}
|
||||
config = {
|
||||
"Name": "mission-core",
|
||||
"NetworkMonitor": True,
|
||||
"PrivateKey": "synthetic-test-key",
|
||||
"DisableDNS": True,
|
||||
}
|
||||
current = {
|
||||
"daemonVersion": "0.82.0",
|
||||
"profileName": "mission-core",
|
||||
"publicKey": "synthetic-public",
|
||||
"netbirdIp": "10.204.1.2/16",
|
||||
"management": {"url": "https://network.example:8443", "connected": True},
|
||||
"signal": {"connected": True},
|
||||
}
|
||||
monkeypatch.setattr(policy, "config", lambda: (copy.deepcopy(active), copy.deepcopy(config)))
|
||||
monkeypatch.setattr(policy, "status", lambda: copy.deepcopy(current))
|
||||
calls = []
|
||||
|
||||
def invoke(*args):
|
||||
calls.append(args)
|
||||
assert args[1:3] == ("--profile", "a" * 32) and "service" not in args
|
||||
config["NetworkMonitor"] = "--network-monitor=true" in args
|
||||
return ""
|
||||
|
||||
monkeypatch.setattr(policy, "invoke", invoke)
|
||||
return owner, config, current, calls
|
||||
|
||||
|
||||
def test_preserves_active_owner_identity_and_explicit_rollback(tmp_path, monkeypatch):
|
||||
owner, config, status, calls = prepared(tmp_path, monkeypatch)
|
||||
before = copy.deepcopy(config)
|
||||
p = policy.plan(owner)
|
||||
assert "synthetic-test-key" not in json.dumps(p)
|
||||
assert policy.apply(owner, p["plan_sha256"])["identity_preserved"]
|
||||
assert config == {**before, "NetworkMonitor": False}
|
||||
assert not policy.apply(owner, policy.plan(owner)["plan_sha256"])["changed"]
|
||||
p = policy.plan(owner, rollback=True)
|
||||
policy.apply(owner, p["plan_sha256"], rollback=True)
|
||||
assert config == before and len(calls) == 2
|
||||
|
||||
|
||||
def test_changed_profile_and_foreign_network_are_refused(tmp_path, monkeypatch):
|
||||
owner, config, status, calls = prepared(tmp_path, monkeypatch)
|
||||
p = policy.plan(owner)
|
||||
config["DisableDNS"] = False
|
||||
with pytest.raises(ValueError, match="Plan changed"):
|
||||
policy.apply(owner, p["plan_sha256"])
|
||||
assert not calls
|
||||
status["management"]["url"] = "https://foreign.example"
|
||||
with pytest.raises(ValueError, match="Owned"):
|
||||
policy.plan(owner)
|
||||
|
||||
|
||||
def test_failed_reconnection_rolls_back_only_flag(tmp_path, monkeypatch):
|
||||
owner, config, status, calls = prepared(tmp_path, monkeypatch)
|
||||
checks = []
|
||||
|
||||
def health(before):
|
||||
checks.append(True)
|
||||
if len(checks) == 1:
|
||||
raise ValueError("connection failed")
|
||||
|
||||
monkeypatch.setattr(policy, "await_health", health)
|
||||
with pytest.raises(ValueError, match="connection failed"):
|
||||
policy.apply(owner, policy.plan(owner)["plan_sha256"])
|
||||
assert config["NetworkMonitor"] is True and len(calls) == 2
|
||||
assert json.loads(policy.RECEIPT.read_text())["state"] == "rolled_back"
|
||||
|
||||
|
||||
def test_package_deterministic(tmp_path):
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
|
||||
path = Path(__file__).resolve().parents[2] / "scripts/build_netbird_policy.py"
|
||||
spec = importlib.util.spec_from_file_location("policy_builder", path)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
a = module.build(tmp_path / "one.pyz")
|
||||
b = module.build(tmp_path / "two.pyz")
|
||||
assert a["sha256"] == b["sha256"]
|
||||
|
||||
|
||||
def test_official_cli_runs_as_active_profile_owner(tmp_path, monkeypatch):
|
||||
from types import SimpleNamespace
|
||||
|
||||
monkeypatch.setattr(policy, "ROOT", tmp_path)
|
||||
(tmp_path / "active_profile.json").write_text(
|
||||
json.dumps({"username": "operator", "name": "a" * 32})
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
policy.pwd,
|
||||
"getpwnam",
|
||||
lambda name: (
|
||||
SimpleNamespace(pw_uid=501, pw_gid=20, pw_dir="/Users/operator")
|
||||
if name == "operator"
|
||||
else pytest.fail("Unexpected profile owner")
|
||||
),
|
||||
)
|
||||
monkeypatch.setenv("NB_MANAGEMENT_URL", "https://foreign.invalid")
|
||||
monkeypatch.setenv("SUDO_USER", "root")
|
||||
monkeypatch.setenv("HOME", "/var/root")
|
||||
calls = []
|
||||
|
||||
def run(command, **kwargs):
|
||||
calls.append((command, kwargs))
|
||||
return SimpleNamespace(returncode=0, stdout="ready", stderr="")
|
||||
|
||||
monkeypatch.setattr(policy.subprocess, "run", run)
|
||||
assert policy.invoke("up", "--profile", "a" * 32, "--network-monitor=false") == "ready"
|
||||
command, options = calls[0]
|
||||
assert command == [
|
||||
policy.BINARY,
|
||||
"up",
|
||||
"--profile",
|
||||
"a" * 32,
|
||||
"--network-monitor=false",
|
||||
"--log-file",
|
||||
"console",
|
||||
]
|
||||
assert (options["user"], options["group"], options["extra_groups"]) == (501, 20, [])
|
||||
assert options["env"]["HOME"] == "/Users/operator"
|
||||
assert not any(k.startswith(("NB_", "SUDO_")) for k in options["env"])
|
||||
assert options["timeout"] == 45 and options["capture_output"]
|
||||
Reference in New Issue
Block a user