driver / lint (pull_request) Successful in 15s
driver / standard (pull_request) Successful in 18s
driver / types (pull_request) Successful in 1m7s
driver / test (3.13) (pull_request) Successful in 1m1s
driver / test (3.12) (pull_request) Successful in 1m10s
driver / lowest (pull_request) Successful in 39s
driver / build (pull_request) Successful in 38s
driver / security (pull_request) Successful in 1m11s
CI / driver (pull_request) Successful in 2m30s
driver / publish (pull_request) Skipped
An access point runs no container engine. configure_zabbix_agent(settings) installs zabbix-agentd from the feed after loading the package index, writes the caller's values into /etc/zabbix_agentd.conf (the old file stays as .bak), enables the agent in UCI, without which its init script does not start it, and opens tcp/ListenPort for the allowed sources in the zone that owns the management address where a firewall runs. Zabbix publishes no repository for OpenWrt; the feed is the only source. The SNMP agent's firewall step writes its rule through _allow_in_management_zone now, split into the zone, the rule and the reload; the rule is the same. Needs napalm-device-types 4.40.0.
237 lines
7.8 KiB
Python
237 lines
7.8 KiB
Python
"""configure_zabbix_agent: the feed's C agent on an access point (NetOrk/netork#2571).
|
|
|
|
An AP has no container engine, so the agent cannot run as a container there; the
|
|
feed has ``zabbix-agentd``. Its init script starts it only while UCI enables it,
|
|
and the package index lives in RAM, gone after every restart.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
from napalm_device_types import CommandResult
|
|
|
|
from napalm_openwrt import OpenWrtDriver
|
|
from tests.unit.test_driver import UCI_FIREWALL_HEALTHY, UCI_NETWORK_STATIC
|
|
|
|
CONFIG = "/etc/zabbix_agentd.conf"
|
|
STOCK = "LogFile=/tmp/zabbix_agentd.log\nServer=127.0.0.1\nServerActive=127.0.0.1\nHostname=Zabbix server\n"
|
|
SETTINGS = {
|
|
"server": "zabbix.example.com",
|
|
"server_active": "zabbix.example.com",
|
|
"listen_port": 10050,
|
|
"allowed_sources": ["198.51.100.20"],
|
|
"hostname": "ap-1og",
|
|
"release": "7.4",
|
|
}
|
|
|
|
|
|
class _Shell:
|
|
"""The interactive shell: package manager, init scripts, UCI, the firewall."""
|
|
|
|
def __init__(self, *, firewall_runs=True, fail="", installed=False, nft_hits="1"):
|
|
self.firewall_runs = firewall_runs
|
|
self.fail = fail
|
|
self.installed = installed
|
|
self.nft_hits = nft_hits
|
|
self.issued: list[str] = []
|
|
|
|
def __call__(self, cmd, **kw):
|
|
self.issued.append(cmd)
|
|
if "command -v apk" in cmd:
|
|
return "/usr/bin/apk"
|
|
if "apk list --installed" in cmd:
|
|
return (
|
|
"zabbix-agentd-7.0.32-r1 mips_24kc {zabbix} (GPL) [installed]"
|
|
if self.installed
|
|
else ""
|
|
)
|
|
if "nft list table inet fw4" in cmd:
|
|
return "on" if self.firewall_runs else ""
|
|
if "__OWRT_RC" in cmd:
|
|
if self.fail and self.fail in cmd:
|
|
return f"failed: {self.fail}\n__OWRT_RC=1"
|
|
if cmd.startswith("apk add"):
|
|
self.installed = True
|
|
return "ok\n__OWRT_RC=0"
|
|
if cmd.startswith("uci show firewall"):
|
|
return UCI_FIREWALL_HEALTHY
|
|
if cmd.startswith("uci show network"):
|
|
return UCI_NETWORK_STATIC
|
|
if "$SSH_CONNECTION" in cmd:
|
|
return "198.51.100.20 51234 192.0.2.1 22"
|
|
if "dport 10050" in cmd:
|
|
return self.nft_hits
|
|
return ""
|
|
|
|
|
|
class _Channel:
|
|
"""The exec channel: the config file and UCI's enable, as root."""
|
|
|
|
def __init__(self, config=STOCK, refuse=""):
|
|
self.config = config
|
|
self.refuse = refuse
|
|
self.calls: list[tuple[str, bool, bytes | None]] = []
|
|
|
|
def __call__(self, command, *, privileged=False, timeout=60, stdin=None):
|
|
self.calls.append((command, privileged, stdin))
|
|
if self.refuse and self.refuse in command:
|
|
return CommandResult("", f"{self.refuse}: refused", 1)
|
|
if command == f"cat {CONFIG}":
|
|
return CommandResult(self.config, "", 0)
|
|
if f"cat > {CONFIG}" in command:
|
|
self.config = (stdin or b"").decode()
|
|
return CommandResult("", "", 0)
|
|
|
|
|
|
@pytest.fixture
|
|
def shell():
|
|
return _Shell()
|
|
|
|
|
|
@pytest.fixture
|
|
def channel():
|
|
return _Channel()
|
|
|
|
|
|
@pytest.fixture
|
|
def owrt(shell, channel):
|
|
d = OpenWrtDriver("ap", "root", "pw")
|
|
d._send_command = shell
|
|
d.run_command = channel
|
|
return d
|
|
|
|
|
|
def test_install_configure_open_and_start(owrt, shell, channel):
|
|
result = owrt.configure_zabbix_agent(SETTINGS)
|
|
|
|
assert result["success"] is True, result["output"]
|
|
assert [s["step"] for s in result["steps"]] == ["install", "configure", "firewall", "start"]
|
|
init = [c.split(" 2>")[0] for c in shell.issued if c.startswith("/etc/init.d/zabbix_agentd")]
|
|
assert init == ["/etc/init.d/zabbix_agentd enable", "/etc/init.d/zabbix_agentd restart"]
|
|
|
|
|
|
def test_the_index_is_loaded_before_the_feed_s_package_is_added(owrt, shell):
|
|
owrt.configure_zabbix_agent(SETTINGS)
|
|
|
|
pkg = [c.split(" 2>")[0] for c in shell.issued if c.startswith("apk ")]
|
|
assert pkg[0] == "apk update"
|
|
assert "apk add zabbix-agentd" in pkg
|
|
|
|
|
|
def test_an_index_that_will_not_load_is_named_when_the_install_fails(owrt, shell):
|
|
shell.fail = "apk "
|
|
|
|
result = owrt.configure_zabbix_agent(SETTINGS)
|
|
|
|
assert result["success"] is False
|
|
assert "package index not loaded" in result["output"]
|
|
|
|
|
|
def test_there_is_no_repository_to_fall_back_to(owrt, shell):
|
|
shell.fail = "apk add"
|
|
|
|
result = owrt.configure_zabbix_agent(SETTINGS)
|
|
|
|
assert result["success"] is False
|
|
assert [s["step"] for s in result["steps"]] == ["install"]
|
|
|
|
|
|
def test_the_config_gets_the_caller_s_values_and_keeps_the_rest(owrt, channel):
|
|
owrt.configure_zabbix_agent(SETTINGS)
|
|
|
|
assert "Server=zabbix.example.com\n" in channel.config
|
|
assert "Hostname=ap-1og\n" in channel.config
|
|
assert channel.config.startswith("LogFile=/tmp/zabbix_agentd.log\n")
|
|
write = next(c for c in channel.calls if f"cat > {CONFIG}" in c[0])
|
|
assert write[1] is True
|
|
assert f"cp -p {CONFIG} {CONFIG}.bak" in write[0]
|
|
|
|
|
|
def test_the_agent_is_enabled_in_uci(owrt, channel):
|
|
owrt.configure_zabbix_agent(SETTINGS)
|
|
|
|
uci = next(c[0] for c in channel.calls if "uci commit zabbix_agentd" in c[0])
|
|
assert "uci set zabbix_agentd.general.enabled='1'" in uci
|
|
|
|
|
|
def test_a_config_that_already_says_it_is_not_written(owrt, channel):
|
|
owrt.configure_zabbix_agent(SETTINGS)
|
|
channel.calls.clear()
|
|
|
|
result = owrt.configure_zabbix_agent(SETTINGS)
|
|
|
|
assert result["success"] is True
|
|
assert not any(f"cat > {CONFIG}" in c[0] for c in channel.calls)
|
|
|
|
|
|
def test_a_config_that_cannot_be_read_stops_the_fix(owrt, channel, shell):
|
|
channel.refuse = f"cat {CONFIG}"
|
|
|
|
result = owrt.configure_zabbix_agent(SETTINGS)
|
|
|
|
assert result["success"] is False
|
|
assert result["steps"][-1]["step"] == "configure"
|
|
assert not any(c.startswith("/etc/init.d/zabbix_agentd") for c in shell.issued)
|
|
|
|
|
|
def test_the_firewall_rule_is_tcp_on_the_listen_port_for_the_sources(owrt, shell):
|
|
owrt.configure_zabbix_agent(SETTINGS)
|
|
|
|
uci = " ".join(shell.issued)
|
|
assert "uci set firewall.allow_zabbix_agent_from_lan=rule" in uci
|
|
assert "firewall.allow_zabbix_agent_from_lan.proto='tcp'" in uci
|
|
assert "firewall.allow_zabbix_agent_from_lan.dest_port='10050'" in uci
|
|
assert "uci add_list firewall.allow_zabbix_agent_from_lan.src_ip='198.51.100.20/32'" in uci
|
|
assert "netork" not in uci.lower()
|
|
|
|
|
|
def test_without_a_running_firewall_nothing_is_opened(owrt, shell):
|
|
shell.firewall_runs = False
|
|
|
|
result = owrt.configure_zabbix_agent(SETTINGS)
|
|
|
|
assert result["success"] is True
|
|
assert not any("firewall.allow_zabbix_agent" in c for c in shell.issued)
|
|
assert "no firewall runs" in result["output"]
|
|
|
|
|
|
def test_without_sources_the_firewall_is_left_alone(owrt, shell):
|
|
result = owrt.configure_zabbix_agent({**SETTINGS, "allowed_sources": []})
|
|
|
|
assert result["success"] is True
|
|
assert not any("firewall.allow_zabbix_agent" in c for c in shell.issued)
|
|
|
|
|
|
def test_a_rule_that_does_not_reach_the_packet_filter_fails(owrt, shell):
|
|
shell.nft_hits = "0"
|
|
|
|
result = owrt.configure_zabbix_agent(SETTINGS)
|
|
|
|
assert result["success"] is False
|
|
assert result["steps"][-1]["step"] == "firewall"
|
|
|
|
|
|
def test_the_snmp_rule_is_written_as_before(owrt, shell):
|
|
shell.nft_hits = "1"
|
|
owrt._send_command = _SnmpShell(shell)
|
|
|
|
owrt._allow_snmp_sources({"community": "c", "allowed_sources": ["198.51.100.20"]})
|
|
|
|
uci = " ".join(shell.issued)
|
|
assert "firewall.allow_snmp_from_lan.proto='udp'" in uci
|
|
assert "firewall.allow_snmp_from_lan.dest_port='161'" in uci
|
|
assert "firewall.allow_snmp_from_lan.name='Allow-SNMP'" in uci
|
|
|
|
|
|
class _SnmpShell:
|
|
"""The same shell, answering the SNMP rule's live check."""
|
|
|
|
def __init__(self, shell):
|
|
self.shell = shell
|
|
|
|
def __call__(self, cmd, **kw):
|
|
if "dport 161" in cmd:
|
|
self.shell.issued.append(cmd)
|
|
return "1"
|
|
return self.shell(cmd, **kw)
|