feat(hardening): get_hardening through napalm-device-types' HardeningMixin (NetOrk/netork#2706)
The transport runs the shared, read-only command on an exec channel, as root first for AppArmor's profiles and as the login user where root is refused.
This commit is contained in:
1 parent
670e62e0f0
commit
a9ed615b9b
3 files changed
+64
No files matched your search
@@ -2085,6 +2085,53 @@ class TestGetSystemIdentity:
|
||||
driver._device.send_command.assert_not_called()
|
||||
|
||||
|
||||
class TestGetHardening:
|
||||
"""How the host is hardened (NetOrk/netork#2706): napalm-device-types' command on an
|
||||
exec channel, as root first for AppArmor's profiles, as the login user where root is
|
||||
refused. ``aa-status --json`` prints one long line, past what the PTY holds reliably."""
|
||||
|
||||
_REPORT = (
|
||||
"HARDENING_BEGIN\n[lsm]\nlockdown,capability,yama,selinux,bpf\n"
|
||||
"[selinux]\nEnforcing\n[crypto_policy]\nDEFAULT\nHARDENING_END\n"
|
||||
)
|
||||
|
||||
def test_it_reads_the_shared_command_as_root_on_an_exec_channel(self, driver):
|
||||
from napalm_device_types import CommandResult, HardeningMixin
|
||||
|
||||
assert isinstance(driver, HardeningMixin)
|
||||
calls = []
|
||||
|
||||
def run_command(command, *, privileged=False, timeout=60, stdin=None):
|
||||
calls.append((command, privileged))
|
||||
return CommandResult(self._REPORT, "", 0)
|
||||
|
||||
driver.run_command = run_command
|
||||
|
||||
reading = driver.get_hardening()
|
||||
|
||||
assert reading["selinux"] == {"mode": "enforcing", "configured": None}
|
||||
assert reading["crypto_policy"] == "DEFAULT"
|
||||
assert [privileged for _, privileged in calls] == [True]
|
||||
assert calls[0][0].startswith("sh -c ")
|
||||
driver._device.send_command.assert_not_called()
|
||||
|
||||
def test_root_refused_reads_as_the_login_user(self, driver):
|
||||
from napalm_device_types import CommandResult
|
||||
|
||||
calls = []
|
||||
|
||||
def run_command(command, *, privileged=False, timeout=60, stdin=None):
|
||||
calls.append(privileged)
|
||||
if privileged:
|
||||
return CommandResult("", "sudo: a password is required", 1)
|
||||
return CommandResult(self._REPORT, "", 0)
|
||||
|
||||
driver.run_command = run_command
|
||||
|
||||
assert driver.get_hardening()["lsm"][3] == "selinux"
|
||||
assert calls == [True, False]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# get_host_firewall (napalm-device-types HostFirewallMixin, NetOrk/netork#1344)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in new issue
Block a user