driver / standard (pull_request) Successful in 14s
driver / lint (pull_request) Successful in 13s
driver / types (pull_request) Successful in 1m4s
driver / test (3.12) (pull_request) Successful in 1m3s
driver / test (3.13) (pull_request) Successful in 57s
driver / lowest (pull_request) Successful in 28s
driver / build (pull_request) Successful in 26s
driver / security (pull_request) Successful in 1m6s
CI / driver (pull_request) Successful in 2m37s
driver / publish (pull_request) Skipped
_account_create_with_uid makes it with useradd -m -u <uid>; below UID_MIN useradd warns and goes on, as for a system's account.
111 lines
3.9 KiB
Python
111 lines
3.9 KiB
Python
"""Personal accounts on a Linux host (NetOrk/netork#1793).
|
|
|
|
napalm-device-types' ``LocalAccountsMixin`` runs the steps; the driver names
|
|
shadow-utils' commands. A locked account is an expired one (``usermod -e 1``):
|
|
PAM refuses it a key login too, where a ``!`` in front of the password alone
|
|
stops only the password.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
|
|
import pytest
|
|
from napalm_device_types import CommandResult, LocalAccountsMixin
|
|
|
|
from napalm_linux import LinuxDriver
|
|
|
|
HASH = "$6$salt$" + "x" * 86
|
|
_TODAY = int(time.time() // 86400)
|
|
|
|
|
|
class _Driver(LinuxDriver):
|
|
"""A Linux driver whose host answers ``getent shadow`` with *shadow*."""
|
|
|
|
def __init__(self, shadow: str = "alice:$6$old:20000:0:99999:7:::") -> None:
|
|
super().__init__("host", "netadmin", "pw")
|
|
self.shadow = shadow
|
|
self.ran: list[tuple[str, bool]] = []
|
|
|
|
def run_command(self, command, *, privileged=False, timeout=60, stdin=None):
|
|
self.ran.append((command, privileged))
|
|
if command == "getent shadow alice":
|
|
return CommandResult(self.shadow + "\n", "", 0 if self.shadow else 2)
|
|
return CommandResult("", "", 0)
|
|
|
|
|
|
def _passwd() -> list[str]:
|
|
return "alice:x:1001:1001::/home/alice:/bin/bash".split(":")
|
|
|
|
|
|
def test_a_linux_host_can_add_and_lock_personal_accounts() -> None:
|
|
assert issubclass(LinuxDriver, LocalAccountsMixin)
|
|
assert LinuxDriver.ACCOUNT_PASSWORD_SCHEME == "sha512"
|
|
assert LinuxDriver.ACCOUNT_SHELL == "/bin/bash"
|
|
# Debian and Ubuntu grant %sudo, the Red Hat family %wheel, old Ubuntu %admin.
|
|
assert set(LinuxDriver.ACCOUNT_ADMIN_GROUPS) == {"sudo", "wheel", "admin"}
|
|
|
|
|
|
def test_a_new_account_gets_a_home_and_bash() -> None:
|
|
d = _Driver()
|
|
assert d._account_create("alice", HASH) == [
|
|
["useradd", "-m", "-s", "/bin/bash", "-p", HASH, "alice"]
|
|
]
|
|
assert d._account_create("alice", None) == [["useradd", "-m", "-s", "/bin/bash", "alice"]]
|
|
|
|
|
|
def test_locking_expires_the_account_and_locks_its_password() -> None:
|
|
d = _Driver()
|
|
assert d._account_lock("alice", locked=False) == [["usermod", "-L", "-e", "1", "alice"]]
|
|
assert d._account_lock("alice", locked=True) == []
|
|
|
|
|
|
def test_opening_lifts_the_expiry_and_sets_the_new_password() -> None:
|
|
d = _Driver()
|
|
assert d._account_open("alice", HASH, locked=True) == [
|
|
["usermod", "-e", "", "-p", HASH, "alice"]
|
|
]
|
|
|
|
|
|
def test_opening_without_a_new_password_gives_back_the_old_one() -> None:
|
|
d = _Driver(shadow="alice:!$6$old:20000:0:99999:7::1:")
|
|
assert d._account_open("alice", None, locked=True) == [["usermod", "-U", "-e", "", "alice"]]
|
|
|
|
|
|
def test_an_account_that_never_had_a_password_keeps_it_locked() -> None:
|
|
# usermod -U refuses to leave an account without a password.
|
|
for field in ("!", "!!", "!*", "*"):
|
|
d = _Driver(shadow=f"alice:{field}:20000:0:99999:7::1:")
|
|
assert d._account_open("alice", None, locked=True) == [["usermod", "-e", "", "alice"]]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("expire", "locked"),
|
|
[("", False), ("1", True), (str(_TODAY), True), (str(_TODAY + 30), False)],
|
|
)
|
|
def test_an_expired_account_is_locked(expire: str, locked: bool) -> None:
|
|
d = _Driver(shadow=f"alice:$6$old:20000:0:99999:7::{expire}:")
|
|
assert d._account_locked("alice", _passwd()) is locked
|
|
assert d.ran == [("getent shadow alice", True)]
|
|
|
|
|
|
def test_a_shadow_that_cannot_be_read_raises() -> None:
|
|
with pytest.raises(RuntimeError, match="alice"):
|
|
_Driver(shadow="")._account_locked("alice", _passwd())
|
|
|
|
|
|
def test_leaving_a_group_is_gpasswd() -> None:
|
|
assert _Driver()._account_leave_group("alice", "sudo", ["alice", "sudo"]) == [
|
|
"gpasswd",
|
|
"-d",
|
|
"alice",
|
|
"sudo",
|
|
]
|
|
|
|
|
|
def test_netorks_own_account_is_made_with_its_uid() -> None:
|
|
"""A fixed uid on every host, below UID_MIN (NetOrk/netork#1817)."""
|
|
assert _Driver()._account_create_with_uid("netork", 900) == [
|
|
["useradd", "-m", "-u", "900", "-s", "/bin/bash", "netork"]
|
|
]
|