4 Commits
Author SHA1 Message Date
christianmanivong 5c13ce1a8b Merge pull request 'chore(release): 0.17.0' (#70) from chore/release-0.17.0 into main
driver / types (push) Successful in 1m7s
driver / standard (push) Successful in 20s
driver / lint (push) Successful in 16s
driver / test (3.12) (push) Successful in 1m13s
driver / test (3.13) (push) Successful in 1m6s
driver / lowest (push) Successful in 38s
driver / build (push) Successful in 30s
driver / security (push) Successful in 1m10s
driver / publish (push) Successful in 30s
CI / driver (push) Successful in 3m31s
2026-11-04 11:19:01 +00:00
Christian Manivong 1c8b1b1c60 chore(release): 0.17.0
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 16s
driver / types (pull_request) Successful in 1m10s
driver / test (3.12) (pull_request) Successful in 1m4s
driver / test (3.13) (pull_request) Successful in 1m10s
driver / lowest (pull_request) Successful in 35s
driver / build (pull_request) Successful in 32s
driver / security (pull_request) Successful in 1m12s
CI / driver (pull_request) Successful in 2m50s
driver / publish (pull_request) Skipped
2026-11-04 12:14:00 +01:00
christianmanivong bd17fbd84d Merge pull request 'feat(accounts): clear a password, replace and remove keys' (#69) from fix/local-account-key-replace into main
driver / lint (push) Successful in 15s
driver / standard (push) Successful in 19s
driver / types (push) Successful in 1m11s
driver / test (3.13) (push) Successful in 1m13s
driver / test (3.12) (push) Successful in 1m25s
driver / lowest (push) Successful in 36s
driver / build (push) Successful in 32s
driver / security (push) Successful in 1m12s
CI / driver (push) Successful in 2m44s
driver / publish (push) Skipped
2026-11-04 11:13:54 +00:00
Christian Manivong d03fe2a2ba 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
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
2026-11-04 11:46:13 +01:00
4 changed files with 134 additions and 2 deletions

No files matched your search

+13
View File
@@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
## [0.17.0] - 2026-11-04
### 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 ## [0.16.0] - 2026-11-03
### Added ### Added
+14
View File
@@ -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 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 the password -- ``usermod -L`` alone -- stops only the password. Opening it
again lifts the expiry and gives the old password back. 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 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. #: What the password field holds when the account never had a password.
_NO_PASSWORD = frozenset({"", "!", "!!", "!*", "*"}) _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): class LinuxLocalAccountsMixin(LocalAccountsMixin):
@@ -53,6 +62,11 @@ class LinuxLocalAccountsMixin(LocalAccountsMixin):
def _account_leave_group(self, name: str, group: str, groups: list[str]) -> list[str]: def _account_leave_group(self, name: str, group: str, groups: list[str]) -> list[str]:
return ["gpasswd", "-d", name, group] 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]: def _shadow(self, name: str) -> list[str]:
"""*name*'s shadow entry, split; only the lock and the expiry are looked at. """*name*'s shadow entry, split; only the lock and the expiry are looked at.
+2 -2
View File
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project] [project]
name = "napalm-linux" name = "napalm-linux"
version = "0.16.0" version = "0.17.0"
description = "NAPALM driver for generic Linux systems via SSH" description = "NAPALM driver for generic Linux systems via SSH"
readme = "README.md" readme = "README.md"
requires-python = ">=3.12" requires-python = ">=3.12"
@@ -34,7 +34,7 @@ classifiers = [
] ]
dependencies = [ dependencies = [
"napalm>=4.0", "napalm>=4.0",
"napalm-device-types>=4.27.0", "napalm-device-types>=4.28.0",
"netmiko>=4.0.0", "netmiko>=4.0.0",
"paramiko>=5.0.0", # CVE-2026-44405 "paramiko>=5.0.0", # CVE-2026-44405
] ]
+105
View File
@@ -8,6 +8,7 @@ stops only the password.
from __future__ import annotations from __future__ import annotations
import shlex
import time import time
import pytest 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) == [ assert _Driver()._account_create_with_uid("netork", 900) == [
["useradd", "-m", "-u", "900", "-s", "/bin/bash", "netork"] ["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() == []