feat(zabbix): an active-only agent closes the port netOrk opened for it (NetOrk/netork#2693)
Every rule the firewall step adds in ufw is recorded in napalm-device-types' opened_ports record, only when ufw did not have it before. For an active-only agent, _revoke_zabbix_agent_sources removes exactly the agent's recorded rules and reports what ufw has left for the port.
This commit is contained in:
1 parent
bc3eb86723
commit
370aeed482
6 files changed
+183
-8
No files matched your search
@@ -82,7 +82,8 @@ class TestSnmpAgent:
|
||||
def test_no_temp_file_with_an_application_name(self, driver, node):
|
||||
driver.configure_snmp_agent(SETTINGS)
|
||||
|
||||
assert not any("netork" in c for c in node.ran)
|
||||
# The record of opened rules (/var/lib/netork, NetOrk/netork#2693) is no temp file.
|
||||
assert not any(re.search(r"/tmp/\S*netork", c) for c in node.ran)
|
||||
|
||||
def test_ufw_opens_only_for_the_allowed_sources(self, driver, node):
|
||||
node.status["command -v ufw"] = 0
|
||||
|
||||
@@ -12,6 +12,7 @@ import base64
|
||||
import re
|
||||
|
||||
import pytest
|
||||
from napalm_device_types import OPENED_PORTS_RECORD
|
||||
|
||||
from napalm_proxmox import ProxmoxBackupDriver, ProxmoxDriver
|
||||
|
||||
@@ -38,6 +39,11 @@ class _Node:
|
||||
self.installed = False
|
||||
self.refuse = refuse
|
||||
self.ran: list[str] = []
|
||||
#: What reading the record of opened rules prints (NetOrk/netork#2693).
|
||||
self.record = ""
|
||||
#: What counting the rules left for the port prints.
|
||||
self.left = "0"
|
||||
self.record_written: str | None = None
|
||||
|
||||
def __call__(self, command: str) -> str:
|
||||
self.ran.append(command)
|
||||
@@ -50,6 +56,14 @@ class _Node:
|
||||
def _answer(self, command: str) -> tuple[str, int]:
|
||||
if self.refuse and self.refuse in command:
|
||||
return "E: refused", 1
|
||||
if "stat -c" in command and OPENED_PORTS_RECORD in command:
|
||||
return self.record, 0
|
||||
if "grep -c" in command:
|
||||
return self.left, 0
|
||||
if "base64 -d" in command and OPENED_PORTS_RECORD in command:
|
||||
written = re.search(r"echo (\S*) \|", command).group(1)
|
||||
self.record_written = base64.b64decode(written).decode()
|
||||
return "", 0
|
||||
if command.startswith("dpkg-query"):
|
||||
return ("zabbix-agent\tinstalled\n" if self.installed else "curl\tinstalled\n"), 0
|
||||
if f"cat {CONFIG}" in command:
|
||||
@@ -144,3 +158,86 @@ def test_a_config_that_cannot_be_read_stops_the_fix(driver, node):
|
||||
|
||||
assert result["success"] is False
|
||||
assert result["steps"][-1]["step"] == "configure"
|
||||
|
||||
|
||||
# --- The record of the rules netOrk opened (NetOrk/netork#2693) ---------------------
|
||||
|
||||
ACTIVE_ONLY = {**SETTINGS, "active_only": True, "allowed_sources": []}
|
||||
ROOT = "0 755 directory\n0 644 regular file\n"
|
||||
ZABBIX_UFW = "v1 zabbix-agent ufw tcp 10050 10.40.100.254/32"
|
||||
SNMP_UFW = "v1 snmp ufw udp 161 10.40.100.254/32"
|
||||
|
||||
|
||||
def test_ufw_records_the_rule_it_added_under_the_agent(driver, node):
|
||||
driver.configure_zabbix_agent(SETTINGS)
|
||||
|
||||
opened = next(c for c in node.ran if "ufw allow" in c)
|
||||
assert "*Skipping*) ;;" in opened
|
||||
assert f'"{ZABBIX_UFW}" >> {OPENED_PORTS_RECORD}' in opened
|
||||
|
||||
|
||||
def test_the_snmp_rule_is_recorded_under_snmp(node):
|
||||
driver = ProxmoxDriver("pve", "root@pam", "pw")
|
||||
driver._exec_ssh_command = node
|
||||
|
||||
driver.configure_snmp_agent({"community": "c0mm", "allowed_sources": ["10.40.100.254"]})
|
||||
|
||||
assert any(f'"{SNMP_UFW}" >>' in c for c in node.ran)
|
||||
|
||||
|
||||
def test_active_only_removes_the_agent_s_recorded_rules_and_keeps_the_rest(driver, node):
|
||||
node.record = ROOT + f"{ZABBIX_UFW}\n{SNMP_UFW}\n"
|
||||
|
||||
result = driver.configure_zabbix_agent(ACTIVE_ONLY)
|
||||
|
||||
assert result["success"] is True, result["output"]
|
||||
deletes = [c for c in node.ran if "ufw delete" in c]
|
||||
assert len(deletes) == 1
|
||||
assert "ufw delete allow from 10.40.100.254/32 to any port 10050 proto tcp" in deletes[0]
|
||||
assert node.record_written == f"{SNMP_UFW}\n"
|
||||
assert "removed" in result["steps"][2]["output"]
|
||||
assert not any("ufw allow" in c for c in node.ran)
|
||||
|
||||
|
||||
def test_without_a_record_nothing_is_removed_and_what_is_left_is_reported(driver, node):
|
||||
node.left = "1"
|
||||
|
||||
result = driver.configure_zabbix_agent(ACTIVE_ONLY)
|
||||
|
||||
assert result["success"] is True
|
||||
assert not any("ufw delete" in c for c in node.ran)
|
||||
assert node.record_written is None
|
||||
firewall = result["steps"][2]["output"]
|
||||
assert "netOrk recorded no rule" in firewall
|
||||
assert "1 rule(s) for tcp/10050" in firewall
|
||||
|
||||
|
||||
def test_a_record_someone_else_could_write_is_not_acted_on(driver, node):
|
||||
node.record = f"0 755 directory\n0 666 regular file\n{ZABBIX_UFW}\n"
|
||||
|
||||
result = driver.configure_zabbix_agent(ACTIVE_ONLY)
|
||||
|
||||
assert result["success"] is False
|
||||
assert "writable" in result["output"]
|
||||
assert not any("ufw delete" in c for c in node.ran)
|
||||
|
||||
|
||||
def test_a_rule_that_will_not_go_stays_in_the_record(driver, node):
|
||||
node.record = ROOT + f"{ZABBIX_UFW}\n"
|
||||
node.refuse = "ufw delete"
|
||||
|
||||
result = driver.configure_zabbix_agent(ACTIVE_ONLY)
|
||||
|
||||
assert result["success"] is False
|
||||
assert node.record_written is None
|
||||
|
||||
|
||||
def test_a_recorded_rule_of_another_firewall_is_not_touched(driver, node):
|
||||
"""A node's rules are ufw's; a firewalld line is not one this driver removes."""
|
||||
node.record = ROOT + "v1 zabbix-agent iptables tcp 10050 10.40.100.254/32\n"
|
||||
|
||||
result = driver.configure_zabbix_agent(ACTIVE_ONLY)
|
||||
|
||||
assert result["success"] is False
|
||||
assert "iptables" in result["output"]
|
||||
assert node.record_written is None
|
||||
Reference in new issue
Block a user