From 679584766f3589864ebe1b5484d873c87a487cd2 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Thu, 12 Nov 2026 21:55:13 +0100 Subject: [PATCH] feat(accounts): add_service_account takes the keys the new one replaces netOrk moves a host from its installation-wide key to its site's (NetOrk/netork#2462): the old key has to go from its own account, which remove_local_account_keys refuses (the login's own account, a system account). With replaces, the old keys go once the new key is in -- never the new key itself, and not while it could not be added. --- CHANGELOG.md | 7 ++++ napalm_device_types/local_accounts.py | 38 ++++++++++++++++----- tests/test_service_account.py | 49 +++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d85bc6..ed55b6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ recorded here; their history is the git log. ## [Unreleased] +### Added +- `add_service_account(..., replaces=[...])`: netOrk's keys the new one takes the place of + go from its account once the new key is in it (step `old keys`), never the new key + itself; while the new key could not be added they stay, so netOrk keeps its way in. + netOrk calls it through a login with the new key, after trying that login, to move a + host from its installation-wide key to its site's (NetOrk/netork#2462). + ### Fixed - `apply_dhcp_reservationset` writes its changes so an address is released before it is taken: an update that moves a reservation off its address goes before the change that diff --git a/napalm_device_types/local_accounts.py b/napalm_device_types/local_accounts.py index 98ea236..a5eae0c 100644 --- a/napalm_device_types/local_accounts.py +++ b/napalm_device_types/local_accounts.py @@ -481,7 +481,12 @@ class LocalAccountsMixin: return self._accounts_result(name, parts) def add_service_account( - self, name: str, public_key: str, *, uid: int | None = None + self, + name: str, + public_key: str, + *, + uid: int | None = None, + replaces: Sequence[str] = (), ) -> LocalAccountChangeDict: """Make *name* netOrk's own account: it logs in with *public_key*, and gains root without a password. @@ -495,17 +500,25 @@ class LocalAccountsMixin: once no other account has it; an account that is there keeps its own, as a uid is never changed under files that carry it. + *replaces* are netOrk's keys *public_key* takes the place of: they go + from the account once *public_key* is in it, matched as in + :meth:`add_local_account`, and never *public_key* itself. Call it so + through a login with *public_key*, tried before: netOrk keeps its way + in (NetOrk/netork#2462). + Steps as in :meth:`add_local_account`: ``account``, then ``sudo`` and - ``keys``. The new login itself is for the caller to try. + ``keys``, and ``old keys`` with *replaces* when ``keys`` went. The new + login itself is for the caller to try. An account that is there and cannot log in (``nologin``, ``false``) is another service's -- a satellite's ``netork``, say -- and is left alone. - :raises ValueError: for a name or key no host takes, root, a uid of 0 or - less, or another service's account. + :raises ValueError: for a name or key no host takes, a replaced key that + is none, root, a uid of 0 or less, or another service's account. :raises RuntimeError: when the login user cannot be told. """ check_public_key(public_key) + old = [key for key in _checked_keys(replaces) if _key_blob(key) != _key_blob(public_key)] login, existing = self._accounts_service_target(name, uid) if name == login: step: FixStepDict = { @@ -520,12 +533,21 @@ class LocalAccountsMixin: ) parts = [first] if first["success"]: - parts += [ - run_fix_steps([("sudo", lambda: self._accounts_grant_service_sudo(name))]), - run_fix_steps([("keys", lambda: self._accounts_add_key(name, public_key))]), - ] + parts += self._accounts_service_keys(name, public_key, old) return self._accounts_result(name, parts) + def _accounts_service_keys( + self, name: str, public_key: str, old: list[str] + ) -> list[FixResultDict]: + """``sudo`` and ``keys`` of netOrk's own account, then ``old keys`` once the key is in.""" + parts = [ + run_fix_steps([("sudo", lambda: self._accounts_grant_service_sudo(name))]), + run_fix_steps([("keys", lambda: self._accounts_add_key(name, public_key))]), + ] + if old and parts[-1]["success"]: + parts.append(run_fix_steps([("old keys", lambda: self._accounts_drop_keys(name, old))])) + return parts + def _accounts_service_target(self, name: str, uid: int | None) -> tuple[str, list[str] | None]: """The login user and *name*'s passwd line, once *name* may become netOrk's. diff --git a/tests/test_service_account.py b/tests/test_service_account.py index 9bc270a..8731676 100644 --- a/tests/test_service_account.py +++ b/tests/test_service_account.py @@ -299,3 +299,52 @@ def test_an_account_that_cannot_log_in_is_another_services_and_left_alone( driver.add_service_account("netork", NETORK_KEY) assert not _root_commands(host) assert service_sudoers_rule_path(_Driver.SUDOERS_DIR, "netork") not in host.files + + +OLD_KEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOldNetork netork@central" + + +def test_a_replaced_key_goes_once_the_new_one_is_in(host: _ServiceHost, driver: _Driver) -> None: + host.login = "netork" + host.accounts["netork"] = _Account(900, "/home/netork", groups=["netork"]) + host.files["/home/netork/.ssh/authorized_keys"] = f"{OWN_KEY}\n{OLD_KEY}\n" + + result = driver.add_service_account("netork", NETORK_KEY, replaces=[OLD_KEY]) + + assert result["success"], result["output"] + assert [s["step"] for s in result["steps"]] == ["account", "sudo", "keys", "old keys"] + assert host.files["/home/netork/.ssh/authorized_keys"] == f"{OWN_KEY}\n{NETORK_KEY}\n" + + +def test_the_new_key_is_never_one_that_goes(host: _ServiceHost, driver: _Driver) -> None: + host.login = "netork" + host.accounts["netork"] = _Account(900, "/home/netork", groups=["netork"]) + host.files["/home/netork/.ssh/authorized_keys"] = f"{NETORK_KEY}\n" + + result = driver.add_service_account( + "netork", NETORK_KEY, replaces=[f"{NETORK_KEY.split()[0]} {NETORK_KEY.split()[1]}"] + ) + + assert result["success"], result["output"] + assert [s["step"] for s in result["steps"]] == ["account", "sudo", "keys"] + assert host.files["/home/netork/.ssh/authorized_keys"] == f"{NETORK_KEY}\n" + + +def test_a_replaced_key_stays_while_the_new_one_is_not_in( + host: _ServiceHost, driver: _Driver +) -> None: + host.login = "netork" + host.accounts["netork"] = _Account(900, "/home/netork", groups=["netork"]) + host.files["/home/netork/.ssh/authorized_keys"] = f"{OLD_KEY}\n" + host.failing.add("as install") + + result = driver.add_service_account("netork", NETORK_KEY, replaces=[OLD_KEY]) + + assert not result["success"] + assert "old keys" not in [s["step"] for s in result["steps"]] + assert host.files["/home/netork/.ssh/authorized_keys"] == f"{OLD_KEY}\n" + + +def test_replaced_keys_are_a_list_not_one_string(driver: _Driver) -> None: + with pytest.raises(ValueError, match="list"): + driver.add_service_account("netork", NETORK_KEY, replaces=OLD_KEY)