feat: LocalAccountsMixin, personal accounts added and locked again (NetOrk/netork#1793) #62

Merged
christianmanivong merged 4 commits from feat/local-accounts into main 2026-10-31 23:45:35 +00:00
3 changed files with 23 additions and 10 deletions
Showing only changes of commit 2846824a21 - Show all commits

No files matched your search

+1 -1
View File
@@ -22,7 +22,7 @@ recorded here; their history is the git log.
the key files are read and written as the account itself, and the sudo rule is a
drop-in of its own, checked with `visudo` and `sudo -l`. A driver supplies the
commands that create, open and lock an account and take it out of a group, and may
replace where the keys are kept (`_account_read_keys`, `_account_add_key`,
replace where the keys are kept (`_account_key_files`, `_account_read_keys`, `_account_add_key`,
`_account_remove_keys`; default `~/.ssh/authorized_keys`) (NetOrk/netork#1793).
- `ARP_AGE_UNKNOWN` (`napalm_device_types.arp`, `-1.0`): the `age` a driver reports
in `get_arp_table` and `get_ipv6_neighbors_table` when the device does not say how
+9 -9
View File
@@ -197,8 +197,8 @@ class LocalAccountsMixin:
# -- what a driver supplies ------------------------------------------------------
#
# ``_account_*`` are the driver's: the five below it must supply, the key
# store (``_account_read_keys``, ``_account_add_key``, ``_account_remove_keys``)
# it may replace. ``_accounts_*`` are this mixin's own.
# store (``_account_key_files``, ``_account_read_keys``, ``_account_add_key``,
# ``_account_remove_keys``) it may replace. ``_accounts_*`` are this mixin's own.
def _account_create(self, name: str, password_hash: str | None) -> list[list[str]]:
"""The commands that create *name* with a home and :attr:`ACCOUNT_SHELL`.
@@ -276,14 +276,14 @@ class LocalAccountsMixin:
raise RuntimeError(f"cannot ask sudo about {name}: {_said(result)}")
return bool(_RUN_ALL_RE.search(result.stdout))
def _account_read_keys(self, name: str, home: str) -> list[str] | None:
"""The lines of *name*'s key files, read as *name*; None when it cannot.
def _account_key_files(self, name: str, home: str) -> tuple[str, ...]:
"""Every file sshd reads *name*'s keys from; a host that adds one of its own says so."""
return authorized_keys_paths(home)
Every key sshd would take for *name*: a driver whose host keeps keys in
a place of its own as well reads that too.
"""
def _account_read_keys(self, name: str, home: str) -> list[str] | None:
"""The lines of :meth:`_account_key_files`, read as *name*; None when it cannot."""
lines: list[str] = []
for path in authorized_keys_paths(home):
for path in self._account_key_files(name, home):
there = self.run_command(_command(["test", "-e", path]), privileged=True, timeout=30)
if there.exit_code != 0:
continue
@@ -555,7 +555,7 @@ class LocalAccountsMixin:
return None
def _account_remove_keys(self, name: str, home: str) -> FixStepOutcomeDict:
"""Remove *name*'s key files, as *name*."""
"""Remove *name*'s key files in *home*, as *name*; then no key may be left."""
paths = authorized_keys_paths(home)
failed = self._accounts_run_as(name, [["rm", "-f", "--", *paths]])
if failed is not None:
+13
View File
@@ -677,3 +677,16 @@ def test_a_driver_can_keep_the_keys_elsewhere(host: _Host) -> None:
locked = driver.lock_local_account("alice")
assert locked["success"], locked["output"]
assert driver.store == {}
def test_a_key_file_the_host_adds_is_read_too(host: _Host) -> None:
class _ExtraFile(_Driver):
def _account_key_files(self, name: str, home: str) -> tuple[str, ...]:
return (*super()._account_key_files(name, home), f"/home/{name}/extra")
_add_alice(host, keys=OWN_KEY + "\n")
host.files["/home/alice/extra"] = ALICE_KEY + "\n"
[account] = _ExtraFile(host).get_local_accounts(["alice"])
assert account["authorized_keys"] == [OWN_KEY, ALICE_KEY]