feat(accounts): add_service_account takes the keys the new one replaces #115

Merged
christianmanivong merged 3 commits from fix/service-account-replaces into main 2026-11-13 08:55:09 +00:00
3 changed files with 98 additions and 16 deletions

No files matched your search

+5
View File
@@ -8,6 +8,11 @@ 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).
- Each forwarding rule of `LogForwardingDict` names its `template`: the one the rule
gives (`@host;RSYSLOG_SyslogProtocol23Format`, `action(… template="…")`), else the
default set before it (`$ActionForwardDefaultTemplate`, or
+44 -16
View File
@@ -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,36 +500,59 @@ 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)
first = self._accounts_service_account_step(name, login, existing, uid)
parts = [first]
if first["success"]:
parts += self._accounts_service_keys(name, public_key, old)
return self._accounts_result(name, parts)
def _accounts_service_account_step(
self, name: str, login: str, existing: list[str] | None, uid: int | None
) -> FixResultDict:
"""The ``account`` step of netOrk's own account; nothing to do when the driver
logs in as *name* already."""
if name == login:
step: FixStepDict = {
"step": "account",
"ok": True,
"output": f"this driver logs in as {name} already",
}
first: FixResultDict = _joined([step])
else:
first = run_fix_steps(
[("account", lambda: self._accounts_service_account(name, existing, uid))]
)
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))]),
]
return self._accounts_result(name, parts)
return _joined([step])
return run_fix_steps(
[("account", lambda: self._accounts_service_account(name, existing, uid))]
)
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.
+49
View File
@@ -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)