feat(local-accounts): remove_local_account_keys can clear the password too #92

Merged
christianmanivong merged 1 commits from feat/remove-keys-clear-password into main 2026-11-04 14:29:43 +00:00
3 changed files with 91 additions and 8 deletions

No files matched your search

+7
View File
@@ -7,6 +7,13 @@ recorded here; their history is the git log.
## [Unreleased]
### Added
- `remove_local_account_keys(name, keys, *, clear_password=False)`: with `clear_password`
the account's password goes along with the keys, and so do its sudo rule and admin
groups, as sudo asks for that password. For a person who deleted their key and whose
password went with it. The account stays open; the steps are `keys`, `password` and
`sudo`, each run when another failed (NetOrk/netork#2033).
## [4.28.0] - 2026-11-04
### Added
+18 -8
View File
@@ -292,7 +292,8 @@ class LocalAccountsMixin:
Not an empty password -- PAM's ``nullok`` lets anyone in without one --
and not a lock or an expiry, which sshd or PAM apply to a key too.
Only ``add_local_account(clear_password=True)`` asks for it.
Only ``add_local_account(clear_password=True)`` and
``remove_local_account_keys(clear_password=True)`` ask for it.
"""
raise NotImplementedError
@@ -424,15 +425,21 @@ class LocalAccountsMixin:
steps.append(("old keys", lambda: self._accounts_drop_keys(name, old)))
return steps
def remove_local_account_keys(self, name: str, keys: Sequence[str]) -> LocalAccountChangeDict:
def remove_local_account_keys(
self, name: str, keys: Sequence[str], *, clear_password: bool = False
) -> LocalAccountChangeDict:
"""Take *keys* off *name*'s account, for a person who deleted them.
Only those keys go, matched by their base64 whatever a line's options
or comment; the other keys, the password and sudo stay, and the
account stays open -- to get a new key once there is one. A locked
account has none left to remove: the lock took them all.
or comment; the other keys stay, and the account stays open -- to get
a new key once there is one. A locked account has none left to
remove: the lock took them all. Without *clear_password* the password
and sudo stay too; with it the password goes, and so do the sudo rule
and :attr:`ACCOUNT_ADMIN_GROUPS`, as sudo asks for that password -- for
a person whose password went with their key.
Step: ``keys``.
Steps: ``keys``, then ``password`` and ``sudo`` (with *clear_password*),
each of which runs when another failed.
:raises ValueError: for a name no host takes, no keys or one that is
none, the login user's own account, or a system account.
@@ -444,8 +451,11 @@ class LocalAccountsMixin:
if self._accounts_changeable(name) is None:
step: FixStepDict = {"step": "keys", "ok": False, "output": f"no account {name}"}
return {**_joined([step]), "account": None}
parts = [run_fix_steps([("keys", lambda: self._accounts_drop_keys(name, wanted))])]
return self._accounts_result(name, parts)
steps: list[FixStep] = [("keys", lambda: self._accounts_drop_keys(name, wanted))]
if clear_password:
steps.append(("password", lambda: self._accounts_clear_password(name)))
steps.append(("sudo", lambda: self._accounts_set_sudo(name, admin=False)))
return self._accounts_result(name, [run_fix_steps([step]) for step in steps])
def lock_local_account(self, name: str) -> LocalAccountChangeDict:
"""Lock *name*: its keys go, every login is refused, sudo goes; its home stays.
+66
View File
@@ -981,3 +981,69 @@ def test_a_driver_with_its_own_key_store_drops_keys_there(host: _Host) -> None:
removed = driver.remove_local_account_keys("alice", [ALICE_KEY])
assert removed["success"], removed["output"]
assert driver.store == {"alice": []}
# -- removing keys and the password at once (NetOrk/netork#2033) ------------------------
def test_removing_keys_with_clear_password_takes_the_password_and_sudo_too(
host: _Host, driver: _Driver
) -> None:
"""A person who deleted their key: the password netOrk no longer knows goes too, and
sudo, which asks for it. The account stays open, its other keys stay."""
_add_alice(host, keys=f"{ALICE_KEY}\n{OWN_KEY}\n")
host.files["/etc/sudoers.d/80-account-alice"] = sudoers_rule("alice")
result = driver.remove_local_account_keys("alice", [ALICE_KEY], clear_password=True)
assert result["success"], result["output"]
assert [s["step"] for s in result["steps"]] == ["keys", "password", "sudo"]
assert _alice_keys(host) == f"{OWN_KEY}\n"
a = host.accounts["alice"]
assert a.password is None and not a.locked and a.groups == ["alice"]
assert "/etc/sudoers.d/80-account-alice" not in host.files
account = result["account"]
assert account is not None and not account["admin"] and not account["locked"]
def test_removing_keys_with_clear_password_runs_each_step_when_one_failed(
host: _Host, driver: _Driver
) -> None:
_add_alice(host, keys=f"{ALICE_KEY}\n")
host.files["/etc/sudoers.d/80-account-alice"] = sudoers_rule("alice")
host.failing.add("clearpw")
result = driver.remove_local_account_keys("alice", [ALICE_KEY], clear_password=True)
assert not result["success"]
steps = {s["step"]: s for s in result["steps"]}
assert steps["keys"]["ok"] and not steps["password"]["ok"]
# sudo does not wait for the password: without a known password it has to go anyway.
assert steps["sudo"]["ok"]
assert "/etc/sudoers.d/80-account-alice" not in host.files
assert _alice_keys(host) == ""
def test_removing_keys_with_a_driver_that_cannot_clear_a_password_says_so(host: _Host) -> None:
class _NoClear(_Driver):
_account_clear_password = LocalAccountsMixin._account_clear_password
_add_alice(host, keys=f"{ALICE_KEY}\n")
result = _NoClear(host).remove_local_account_keys("alice", [ALICE_KEY], clear_password=True)
password = next(s for s in result["steps"] if s["step"] == "password")
assert not password["ok"] and "cannot remove" in password["output"]
assert not result["success"]
def test_removing_keys_of_an_account_that_is_not_there_clears_nothing(driver: _Driver) -> None:
result = driver.remove_local_account_keys("bob", [ALICE_KEY], clear_password=True)
assert not result["success"] and "no account" in result["output"]
assert [s["step"] for s in result["steps"]] == ["keys"]
def test_clear_password_is_a_keyword(driver: _Driver) -> None:
with pytest.raises(TypeError):
driver.remove_local_account_keys("alice", [ALICE_KEY], True) # type: ignore[misc]