feat: netOrk's own account through the current login (NetOrk/netork#1817)
driver / lint (pull_request) Failing after 7s
driver / standard (pull_request) Successful in 21s
driver / types (pull_request) Successful in 1m10s
driver / test (3.12) (pull_request) Successful in 1m9s
driver / test (3.13) (pull_request) Successful in 1m6s
driver / lowest (pull_request) Successful in 50s
driver / build (pull_request) Successful in 38s
driver / security (pull_request) Successful in 1m19s
CI / driver (pull_request) Failing after 2m35s
driver / publish (pull_request) Skipped

add_service_account(name, public_key) creates or reopens the account through
the login the driver uses today: a key and no password, and a rule of its own
in 85-service-<name> that lets it run everything as root without a password,
checked with visudo and sudo -l. Root is refused; run again with the account
the driver already logs in with, it only adds what is missing.
This commit is contained in:
christianmanivong committed 2026-11-01 12:03:01 +01:00
1 parent 25cc264306
commit 666be091e3
3 files changed
+315 -14

No files matched your search

+199
View File
@@ -0,0 +1,199 @@
"""netOrk's own account on a host: created through the account it logs in with today.
The fake host of :mod:`tests.test_local_accounts`, taught the rule that lets
an account run everything as root without a password.
"""
from __future__ import annotations
import shlex
import pytest
from napalm_device_types.channel import CommandResult
from napalm_device_types.local_accounts import (
service_sudoers_rule,
service_sudoers_rule_path,
sudoers_rule_path,
)
from tests.test_local_accounts import (
OWN_KEY,
SHELL_SYNTAX,
_Account,
_Driver,
_Host,
_root_commands,
)
NETORK_KEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINetork netork@central"
class _ServiceHost(_Host):
"""The fake host, whose sudo also takes the rule without a password."""
def is_passwordless(self, name: str) -> bool:
rule = self.files.get(service_sudoers_rule_path(_Driver.SUDOERS_DIR, name))
return self.includes_dropins and rule is not None
def _sudo(self, args: list[str], privileged: bool) -> CommandResult:
name = args[2]
if not self.is_passwordless(name):
return super()._sudo(args, privileged)
out = f"User {name} may run the following commands on host:\n (ALL) NOPASSWD: ALL\n"
return CommandResult(out, "", 0)
def _visudo(self, args: list[str], privileged: bool) -> CommandResult:
content = self.files[args[1]]
if content.endswith(" ALL=(ALL) NOPASSWD: ALL\n") and "\n" not in content[:-1]:
return CommandResult("parsed OK", "", 0)
return super()._visudo(args, privileged)
@pytest.fixture
def host() -> _ServiceHost:
return _ServiceHost()
@pytest.fixture
def driver(host: _ServiceHost) -> _Driver:
return _Driver(host)
def test_the_rule_runs_everything_as_root_without_a_password() -> None:
assert service_sudoers_rule("netork") == "netork ALL=(ALL) NOPASSWD: ALL\n"
def test_the_rule_has_a_file_of_its_own_no_personal_rule_shares() -> None:
path = service_sudoers_rule_path("/etc/sudoers.d", "net.ork_1")
assert path == "/etc/sudoers.d/85-service-net_2eork__1"
assert path != sudoers_rule_path("/etc/sudoers.d", "net.ork_1")
def test_a_new_service_account_gets_the_key_and_sudo_without_a_password(
host: _ServiceHost, driver: _Driver
) -> None:
result = driver.add_service_account("netork", NETORK_KEY)
assert result["success"], result["output"]
assert [s["step"] for s in result["steps"]] == ["account", "sudo", "keys"]
assert host.accounts["netork"].password is None
assert host.files["/home/netork/.ssh/authorized_keys"] == NETORK_KEY + "\n"
assert host.is_passwordless("netork")
account = result["account"]
assert account is not None and account["admin"]
def test_every_command_root_runs_is_one_fixed_command(host: _ServiceHost, driver: _Driver) -> None:
driver.add_service_account("netork", NETORK_KEY)
for command in _root_commands(host):
assert not any(s in command for s in SHELL_SYNTAX), command
def test_an_existing_account_keeps_its_keys_and_gets_the_rule(
host: _ServiceHost, driver: _Driver
) -> None:
host.accounts["netork"] = _Account(1001, "/home/netork", groups=["netork"])
host.files["/home/netork/.ssh/authorized_keys"] = OWN_KEY + "\n"
result = driver.add_service_account("netork", NETORK_KEY)
assert result["success"], result["output"]
assert host.files["/home/netork/.ssh/authorized_keys"] == f"{OWN_KEY}\n{NETORK_KEY}\n"
assert host.is_passwordless("netork")
def test_an_existing_system_account_may_become_netorks(host: _ServiceHost, driver: _Driver) -> None:
host.accounts["netork"] = _Account(998, "/var/lib/netork", groups=["netork"])
result = driver.add_service_account("netork", NETORK_KEY)
assert result["success"], result["output"]
def test_a_locked_account_is_opened_again(host: _ServiceHost, driver: _Driver) -> None:
host.accounts["netork"] = _Account(1001, "/home/netork", groups=["netork"], locked=True)
result = driver.add_service_account("netork", NETORK_KEY)
assert result["success"], result["output"]
assert not host.accounts["netork"].locked
def test_the_account_netork_logs_in_with_already_only_gets_what_it_lacks(
host: _ServiceHost, driver: _Driver
) -> None:
host.login = "netork"
host.accounts["netork"] = _Account(1001, "/home/netork", groups=["netork"])
host.files["/home/netork/.ssh/authorized_keys"] = NETORK_KEY + "\n"
result = driver.add_service_account("netork", NETORK_KEY)
assert result["success"], result["output"]
assert not any(c.startswith("openuser") for c in _root_commands(host))
assert "already" in result["output"]
def test_root_is_never_netorks_account(host: _ServiceHost, driver: _Driver) -> None:
host.accounts["toor"] = _Account(0, "/root", groups=["wheel"])
with pytest.raises(ValueError, match="root"):
driver.add_service_account("toor", NETORK_KEY)
assert not _root_commands(host)
@pytest.mark.parametrize("name", ["Netork", "net ork", "-n", ""])
def test_a_name_no_host_takes_is_refused(driver: _Driver, name: str) -> None:
with pytest.raises(ValueError):
driver.add_service_account(name, NETORK_KEY)
def test_a_key_that_is_none_is_refused(driver: _Driver) -> None:
with pytest.raises(ValueError):
driver.add_service_account("netork", "not a key")
def test_a_rule_sudo_does_not_read_fails_the_step(host: _ServiceHost, driver: _Driver) -> None:
host.includes_dropins = False
result = driver.add_service_account("netork", NETORK_KEY)
assert not result["success"]
sudo = next(s for s in result["steps"] if s["step"] == "sudo")
assert not sudo["ok"] and "does not read it" in sudo["output"]
def test_a_rule_with_a_password_is_not_enough(host: _ServiceHost, driver: _Driver) -> None:
host.other_rules.add("netork")
host.includes_dropins = False
result = driver.add_service_account("netork", NETORK_KEY)
assert not result["success"]
def test_a_failed_create_stops_before_sudo_and_keys(host: _ServiceHost, driver: _Driver) -> None:
host.failing.add("mkuser")
result = driver.add_service_account("netork", NETORK_KEY)
assert not result["success"]
assert [s["step"] for s in result["steps"]] == ["account"]
assert result["account"] is None
def test_the_staged_files_are_removed_again(host: _ServiceHost, driver: _Driver) -> None:
driver.add_service_account("netork", NETORK_KEY)
assert not [p for p in host.files if p.startswith("/tmp/")]
def test_a_personal_account_of_the_same_name_keeps_its_own_rule_apart(
host: _ServiceHost, driver: _Driver
) -> None:
driver.add_service_account("netork", NETORK_KEY)
rules = [c for c in _root_commands(host) if c.startswith("install")]
assert [shlex.split(c)[-1] for c in rules] == [
service_sudoers_rule_path(_Driver.SUDOERS_DIR, "netork")
]