From 0545d1d814498a93d14672079bb0ab4a384f07ea Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sat, 31 Oct 2026 23:29:46 +0100 Subject: [PATCH] feat: a driver may keep the account keys elsewhere (NetOrk/netork#1793) OpenMediaVault keeps a user's keys in its own database and deploys them to /var/lib/openmediavault/ssh/authorized_keys, and creates a home only when its home directory setting is on. The key store becomes three hooks with the ~/.ssh default: _account_read_keys, _account_add_key and _account_remove_keys, each given the account's home. --- CHANGELOG.md | 5 +-- napalm_device_types/local_accounts.py | 49 ++++++++++++++++++++------- tests/test_local_accounts.py | 38 +++++++++++++++++++++ 3 files changed, 77 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c0d4d1..b134692 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,8 +21,9 @@ recorded here; their history is the git log. every login while keeping its home. Every command root runs is one fixed command, 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 - (NetOrk/netork#1793). + 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`, + `_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 old an entry is. `0.0` means "confirmed just now" and is no stand-in for it diff --git a/napalm_device_types/local_accounts.py b/napalm_device_types/local_accounts.py index 553eea8..f4d23d4 100644 --- a/napalm_device_types/local_accounts.py +++ b/napalm_device_types/local_accounts.py @@ -195,6 +195,10 @@ class LocalAccountsMixin: ) -> CommandResult: ... # -- 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. 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`. @@ -263,7 +267,7 @@ class LocalAccountsMixin: "groups": groups.stdout.split(), "locked": self._account_locked(name, passwd), "admin": self._accounts_admin(name), - "authorized_keys": self._accounts_keys(name, home), + "authorized_keys": self._account_read_keys(name, home), } def _accounts_admin(self, name: str) -> bool: @@ -272,8 +276,12 @@ class LocalAccountsMixin: raise RuntimeError(f"cannot ask sudo about {name}: {_said(result)}") return bool(_RUN_ALL_RE.search(result.stdout)) - def _accounts_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_read_keys(self, name: str, home: str) -> list[str] | None: + """The lines of *name*'s key files, read as *name*; None when it cannot. + + Every key sshd would take for *name*: a driver whose host keeps keys in + a place of its own as well reads that too. + """ lines: list[str] = [] for path in authorized_keys_paths(home): there = self.run_command(_command(["test", "-e", path]), privileged=True, timeout=30) @@ -483,13 +491,30 @@ class LocalAccountsMixin: def _accounts_unstage(self, path: str) -> None: self.run_command(_command(["rm", "-f", "--", path]), timeout=30) - def _accounts_add_key(self, name: str, public_key: str) -> FixStepOutcomeDict: + def _accounts_home(self, name: str) -> str | None: passwd = self._accounts_passwd(name) - if passwd is None: + return passwd[5] if passwd is not None else None + + def _accounts_add_key(self, name: str, public_key: str) -> FixStepOutcomeDict: + home = self._accounts_home(name) + if home is None: return {"ok": False, "output": f"no account {name}"} - home = passwd[5] + return self._account_add_key(name, home, public_key) + + def _accounts_remove_keys(self, name: str) -> FixStepOutcomeDict: + home = self._accounts_home(name) + if home is None: + return {"ok": False, "output": f"no account {name}"} + return self._account_remove_keys(name, home) + + def _account_add_key(self, name: str, home: str, public_key: str) -> FixStepOutcomeDict: + """Add *public_key* to *name*'s first key file, as *name*; the others stay. + + A driver whose host keeps keys elsewhere overrides this, with + :meth:`_account_read_keys` and :meth:`_account_remove_keys`. + """ path = authorized_keys_paths(home)[0] - current = self._accounts_keys(name, home) + current = self._account_read_keys(name, home) if current is None: return {"ok": False, "output": f"cannot read the key files of {name}"} blob = _key_blob(public_key) @@ -529,15 +554,13 @@ class LocalAccountsMixin: return result return None - def _accounts_remove_keys(self, name: str) -> FixStepOutcomeDict: - passwd = self._accounts_passwd(name) - if passwd is None: - return {"ok": False, "output": f"no account {name}"} - paths = authorized_keys_paths(passwd[5]) + def _account_remove_keys(self, name: str, home: str) -> FixStepOutcomeDict: + """Remove *name*'s key files, as *name*.""" + paths = authorized_keys_paths(home) failed = self._accounts_run_as(name, [["rm", "-f", "--", *paths]]) if failed is not None: return {"ok": False, "output": f"cannot remove the keys of {name}: {_said(failed)}"} - if self._accounts_keys(name, passwd[5]): + if self._account_read_keys(name, home): return {"ok": False, "output": f"{name} still has keys"} return {"ok": True, "output": f"keys of {name} removed"} diff --git a/tests/test_local_accounts.py b/tests/test_local_accounts.py index 4c36b7a..c9ed53c 100644 --- a/tests/test_local_accounts.py +++ b/tests/test_local_accounts.py @@ -20,6 +20,7 @@ from napalm_device_types.local_accounts import ( sudoers_rule, sudoers_rule_path, ) +from napalm_device_types.models import FixStepOutcomeDict ALICE_KEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAlice alice@laptop" OWN_KEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQOwn own@desktop" @@ -639,3 +640,40 @@ def test_an_account_that_cannot_be_read_back_is_said(host: _Host, driver: _Drive assert result["account"] is None assert "[read back] cannot ask sudo about alice" in result["output"] + + +# -- a key store of the driver's own --------------------------------------------------- + + +class _OwnKeyStore(_Driver): + """A driver whose host keeps keys in a place of its own.""" + + def __init__(self, host: _Host) -> None: + super().__init__(host) + self.store: dict[str, list[str]] = {} + + def _account_read_keys(self, name: str, home: str) -> list[str] | None: + return list(self.store.get(name, [])) + + def _account_add_key(self, name: str, home: str, public_key: str) -> FixStepOutcomeDict: + self.store.setdefault(name, []).append(f"{home} {public_key}") + return {"ok": True, "output": "stored"} + + def _account_remove_keys(self, name: str, home: str) -> FixStepOutcomeDict: + self.store.pop(name, None) + return {"ok": True, "output": "dropped"} + + +def test_a_driver_can_keep_the_keys_elsewhere(host: _Host) -> None: + driver = _OwnKeyStore(host) + + added = driver.add_local_account("alice", ALICE_KEY) + assert added["success"], added["output"] + assert driver.store == {"alice": [f"/home/alice {ALICE_KEY}"]} + assert added["account"] is not None + assert added["account"]["authorized_keys"] == [f"/home/alice {ALICE_KEY}"] + assert not [p for p in host.files if p.startswith("/home/")] + + locked = driver.lock_local_account("alice") + assert locked["success"], locked["output"] + assert driver.store == {}