feat(accounts): clear a password, replace and remove keys
driver / lint (pull_request) Successful in 17s
driver / standard (pull_request) Successful in 19s
driver / types (pull_request) Successful in 1m9s
driver / test (3.13) (pull_request) Successful in 1m2s
driver / test (3.12) (pull_request) Successful in 1m11s
driver / lowest (pull_request) Successful in 32s
driver / build (pull_request) Successful in 30s
driver / security (pull_request) Successful in 1m6s
CI / driver (pull_request) Successful in 2m27s
driver / publish (pull_request) Skipped
driver / lint (pull_request) Successful in 17s
driver / standard (pull_request) Successful in 19s
driver / types (pull_request) Successful in 1m9s
driver / test (3.13) (pull_request) Successful in 1m2s
driver / test (3.12) (pull_request) Successful in 1m11s
driver / lowest (pull_request) Successful in 32s
driver / build (pull_request) Successful in 30s
driver / security (pull_request) Successful in 1m6s
CI / driver (pull_request) Successful in 2m27s
driver / publish (pull_request) Skipped
napalm-device-types 4.28.0 lets add_local_account remove the old keys (replaces) and the password (clear_password), and adds remove_local_account_keys. The key changes run through the mixin's own key files as the person; the password goes with usermod -p '*', which leaves the expiry (the lock) alone and keeps the key login working with and without PAM, where an empty password or a '!' would not. See NetOrk/netork#2013
This commit is contained in:
1 parent
650379680f
commit
d03fe2a2ba
4 files changed
+131
-1
No files matched your search
@@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
- Personal accounts follow what the person changes (NetOrk/netork#2013):
|
||||
`add_local_account(..., replaces=[...])` takes the old keys out of the account's key
|
||||
files as the new one goes in, `remove_local_account_keys` takes deleted keys out and
|
||||
leaves the account open, and `add_local_account(..., clear_password=True)` removes the
|
||||
password (`usermod -p '*'`): the account logs in with its key only and has no sudo.
|
||||
Keys the person added on the host stay.
|
||||
|
||||
### Changed
|
||||
- Needs napalm-device-types 4.28.0.
|
||||
|
||||
## [0.16.0] - 2026-11-03
|
||||
|
||||
### Added
|
||||
|
||||
@@ -5,6 +5,13 @@ commands. A locked account is an expired one (``usermod -L -e 1``): PAM's
|
||||
account check refuses it every login, a key's too, where a ``!`` in front of
|
||||
the password -- ``usermod -L`` alone -- stops only the password. Opening it
|
||||
again lifts the expiry and gives the old password back.
|
||||
|
||||
An account whose password is removed (NetOrk/netork#2013) gets ``*`` as its
|
||||
password field, which no hash matches: it logs in with a key only. Not an
|
||||
empty field (``passwd -d``), which pam_unix's ``nullok`` -- on by default on
|
||||
Debian and Ubuntu -- takes as "no password needed" for ``su`` and the console.
|
||||
Not ``!``, which sshd takes for a locked account and refuses a key too where
|
||||
PAM is off (``UsePAM no``). And not the expiry, which is the lock.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -16,6 +23,8 @@ from napalm_device_types import LocalAccountsMixin
|
||||
|
||||
#: What the password field holds when the account never had a password.
|
||||
_NO_PASSWORD = frozenset({"", "!", "!!", "!*", "*"})
|
||||
#: The password field of an account that logs in with a key only: no hash matches it.
|
||||
_KEY_ONLY = "*"
|
||||
|
||||
|
||||
class LinuxLocalAccountsMixin(LocalAccountsMixin):
|
||||
@@ -53,6 +62,11 @@ class LinuxLocalAccountsMixin(LocalAccountsMixin):
|
||||
def _account_leave_group(self, name: str, group: str, groups: list[str]) -> list[str]:
|
||||
return ["gpasswd", "-d", name, group]
|
||||
|
||||
def _account_clear_password(self, name: str) -> list[list[str]]:
|
||||
# usermod -p leaves the expiry -- the lock -- alone, and dates the change today,
|
||||
# so pam_unix asks for no new password at the next key login.
|
||||
return [["usermod", "-p", _KEY_ONLY, name]]
|
||||
|
||||
def _shadow(self, name: str) -> list[str]:
|
||||
"""*name*'s shadow entry, split; only the lock and the expiry are looked at.
|
||||
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ classifiers = [
|
||||
]
|
||||
dependencies = [
|
||||
"napalm>=4.0",
|
||||
"napalm-device-types>=4.27.0",
|
||||
"napalm-device-types>=4.28.0",
|
||||
"netmiko>=4.0.0",
|
||||
"paramiko>=5.0.0", # CVE-2026-44405
|
||||
]
|
||||
|
||||
@@ -8,6 +8,7 @@ stops only the password.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import shlex
|
||||
import time
|
||||
|
||||
import pytest
|
||||
@@ -108,3 +109,107 @@ def test_netorks_own_account_is_made_with_its_uid() -> None:
|
||||
assert _Driver()._account_create_with_uid("netork", 900) == [
|
||||
["useradd", "-m", "-u", "900", "-s", "/bin/bash", "netork"]
|
||||
]
|
||||
|
||||
|
||||
# -- what a person changes follows them (NetOrk/netork#2013) ----------------------------
|
||||
|
||||
ALICE_KEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAlice alice@laptop"
|
||||
OLD_KEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOld alice@lost-laptop"
|
||||
OWN_KEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQOwn own@desktop"
|
||||
_KEYS = "/home/alice/.ssh/authorized_keys"
|
||||
|
||||
|
||||
class _Host(LinuxDriver):
|
||||
"""A Linux host with Alice's account, her key files and what ran as whom."""
|
||||
|
||||
def __init__(self, shadow: str, keys: str) -> None:
|
||||
super().__init__("host", "netadmin", "pw")
|
||||
self.shadow = shadow
|
||||
self.files = {_KEYS: keys}
|
||||
self.ran: list[tuple[str, bool]] = []
|
||||
|
||||
def run_command(self, command, *, privileged=False, timeout=60, stdin=None):
|
||||
self.ran.append((command, privileged))
|
||||
if stdin is not None:
|
||||
self.files["/tmp/staged"] = stdin.decode()
|
||||
return CommandResult("/tmp/staged", "", 0)
|
||||
argv = shlex.split(command)
|
||||
answers = {
|
||||
"id -un": "netadmin\n",
|
||||
"getent passwd alice": "alice:x:1001:1001::/home/alice:/bin/bash\n",
|
||||
"getent shadow alice": self.shadow + "\n",
|
||||
"id -Gn alice": "alice sudo\n",
|
||||
"sudo -l -U alice": "User alice is not allowed to run sudo on host.\n",
|
||||
}
|
||||
if command in answers:
|
||||
return CommandResult(answers[command], "", 0)
|
||||
if argv[:2] == ["test", "-e"]:
|
||||
return CommandResult("", "", 0 if argv[2] in self.files else 1)
|
||||
if argv[:4] == ["sudo", "-n", "-u", "alice"]:
|
||||
return self._as_alice(argv[4:])
|
||||
return CommandResult("", "", 0)
|
||||
|
||||
def _as_alice(self, argv: list[str]) -> CommandResult:
|
||||
if argv[0] == "cat":
|
||||
return CommandResult(self.files.get(argv[-1], ""), "", 0)
|
||||
if argv[0] == "install":
|
||||
self.files[argv[-1]] = self.files[argv[-2]]
|
||||
return CommandResult("", "", 0)
|
||||
|
||||
def as_root(self) -> list[list[str]]:
|
||||
"""What root ran that changes something: no reads, nothing as Alice."""
|
||||
reads = ("getent", "id", "test")
|
||||
words = [shlex.split(c) for c, privileged in self.ran if privileged]
|
||||
return [
|
||||
w for w in words if w[0] not in reads and w[:2] not in (["sudo", "-n"], ["sudo", "-l"])
|
||||
]
|
||||
|
||||
|
||||
_OPEN = "alice:$6$old:20000:0:99999:7:::"
|
||||
|
||||
|
||||
def test_clearing_the_password_is_a_star_no_hash_matches() -> None:
|
||||
# Not passwd -d: an empty password and pam_unix's nullok let anyone su to it.
|
||||
# Not "!": sshd without PAM takes it for a lock and refuses the key too.
|
||||
# Not an expiry: that is the lock, and PAM refuses a key of an expired account.
|
||||
assert _Driver()._account_clear_password("alice") == [["usermod", "-p", "*", "alice"]]
|
||||
|
||||
|
||||
def test_a_key_only_account_opens_again_without_its_password_coming_back() -> None:
|
||||
d = _Driver(shadow="alice:!*:20000:0:99999:7::1:")
|
||||
assert d._account_open("alice", None, locked=True) == [["usermod", "-e", "", "alice"]]
|
||||
|
||||
|
||||
def test_removing_the_password_runs_usermod_and_takes_sudo_away() -> None:
|
||||
host = _Host(_OPEN, keys=ALICE_KEY + "\n")
|
||||
|
||||
result = host.add_local_account("alice", ALICE_KEY, clear_password=True)
|
||||
|
||||
assert result["success"], result["output"]
|
||||
assert host.as_root() == [
|
||||
["usermod", "-U", "-e", "", "alice"],
|
||||
["rm", "-f", "--", "/etc/sudoers.d/80-account-alice"],
|
||||
["gpasswd", "-d", "alice", "sudo"],
|
||||
["usermod", "-p", "*", "alice"],
|
||||
]
|
||||
|
||||
|
||||
def test_a_replaced_key_is_written_out_of_the_key_file_as_the_person() -> None:
|
||||
host = _Host(_OPEN, keys=f"{OLD_KEY}\n{OWN_KEY}\n")
|
||||
|
||||
result = host.add_local_account("alice", ALICE_KEY, replaces=[OLD_KEY])
|
||||
|
||||
assert result["success"], result["output"]
|
||||
assert host.files[_KEYS] == f"{OWN_KEY}\n{ALICE_KEY}\n"
|
||||
installs = [c for c, _ in host.ran if "install -m 600" in c]
|
||||
assert installs and all(c.startswith("sudo -n -u alice install") for c in installs)
|
||||
|
||||
|
||||
def test_a_deleted_key_goes_and_the_account_stays_open() -> None:
|
||||
host = _Host(_OPEN, keys=f"{ALICE_KEY}\n{OWN_KEY}\n")
|
||||
|
||||
result = host.remove_local_account_keys("alice", [ALICE_KEY])
|
||||
|
||||
assert result["success"], result["output"]
|
||||
assert host.files[_KEYS] == f"{OWN_KEY}\n"
|
||||
assert host.as_root() == []
|
||||
Reference in new issue
Block a user