feat(local-accounts): replace and remove keys, clear the password
driver / lint (pull_request) Successful in 15s
driver / standard (pull_request) Successful in 18s
driver / types (pull_request) Successful in 1m1s
driver / test (3.13) (pull_request) Successful in 1m10s
driver / test (3.12) (pull_request) Successful in 1m21s
driver / lowest (pull_request) Successful in 40s
driver / build (pull_request) Successful in 39s
driver / security (pull_request) Successful in 1m16s
CI / driver (pull_request) Successful in 2m44s
driver / publish (pull_request) Skipped
driver / lint (pull_request) Successful in 15s
driver / standard (pull_request) Successful in 18s
driver / types (pull_request) Successful in 1m1s
driver / test (3.13) (pull_request) Successful in 1m10s
driver / test (3.12) (pull_request) Successful in 1m21s
driver / lowest (pull_request) Successful in 40s
driver / build (pull_request) Successful in 39s
driver / security (pull_request) Successful in 1m16s
CI / driver (pull_request) Successful in 2m44s
driver / publish (pull_request) Skipped
A person who replaces a key, deletes one or removes their password had no way to see that reach the device: add_local_account only ever added a key and kept the password. add_local_account takes replaces=[...] (the old keys go, matched by their base64, the person's own keys stay) and clear_password=True (the account logs in with a key only and cannot have sudo). remove_local_account_keys takes keys off an account and leaves it open. Drivers supply _account_clear_password and may override _account_drop_keys for a key store of their own. See NetOrk/netork#2013
This commit is contained in:
1 parent
4e40f95bc1
commit
00452d0d37
3 files changed
+479
-19
No files matched your search
@@ -23,6 +23,8 @@ from napalm_device_types.local_accounts import (
|
||||
from napalm_device_types.models import FixStepOutcomeDict
|
||||
|
||||
ALICE_KEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAlice alice@laptop"
|
||||
#: The key Alice had before, on the laptop she lost.
|
||||
OLD_KEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOld alice@lost-laptop"
|
||||
OWN_KEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQOwn own@desktop"
|
||||
HASH = "$6$salt$" + "x" * 86
|
||||
#: Shell syntax a command for root must not hold: each is one fixed command.
|
||||
@@ -61,6 +63,10 @@ class _Host:
|
||||
self.ran: list[tuple[str, bool]] = []
|
||||
self.staged = 0
|
||||
|
||||
def ran_words(self, first: str) -> list[str]:
|
||||
"""The words of the last command run whose first word is *first*."""
|
||||
return next(w for w in (shlex.split(c) for c, _ in reversed(self.ran)) if w[0] == first)
|
||||
|
||||
def is_admin(self, name: str) -> bool:
|
||||
rule = self.files.get(sudoers_rule_path(_Driver.SUDOERS_DIR, name))
|
||||
return name in self.other_rules or (self.includes_dropins and rule is not None)
|
||||
@@ -200,6 +206,11 @@ class _Host:
|
||||
self.accounts[args[0]].groups.remove(args[1])
|
||||
return CommandResult("", "", 0)
|
||||
|
||||
def _clearpw(self, args: list[str], privileged: bool) -> CommandResult:
|
||||
assert privileged
|
||||
self.accounts[args[0]].password = None
|
||||
return CommandResult("", "", 0)
|
||||
|
||||
|
||||
class _Driver(LocalAccountsMixin):
|
||||
ACCOUNT_SHELL = "/bin/sh"
|
||||
@@ -238,6 +249,9 @@ class _Driver(LocalAccountsMixin):
|
||||
def _account_leave_group(self, name: str, group: str, groups: list[str]) -> list[str]:
|
||||
return ["leavegroup", name, group]
|
||||
|
||||
def _account_clear_password(self, name: str) -> list[list[str]]:
|
||||
return [["clearpw", name]]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def host() -> _Host:
|
||||
@@ -362,7 +376,9 @@ def test_a_new_account_gets_the_key_and_the_sudo_rule(host: _Host, driver: _Driv
|
||||
|
||||
|
||||
def test_every_command_root_runs_is_one_fixed_command(host: _Host, driver: _Driver) -> None:
|
||||
driver.add_local_account("alice", ALICE_KEY, password_hash=HASH, admin=True)
|
||||
driver.add_local_account("alice", OLD_KEY, password_hash=HASH, admin=True)
|
||||
driver.add_local_account("alice", ALICE_KEY, replaces=[OLD_KEY], clear_password=True)
|
||||
driver.remove_local_account_keys("alice", [ALICE_KEY])
|
||||
driver.lock_local_account("alice")
|
||||
|
||||
for command in _root_commands(host):
|
||||
@@ -690,3 +706,278 @@ def test_a_key_file_the_host_adds_is_read_too(host: _Host) -> None:
|
||||
[account] = _ExtraFile(host).get_local_accounts(["alice"])
|
||||
|
||||
assert account["authorized_keys"] == [OWN_KEY, ALICE_KEY]
|
||||
|
||||
|
||||
# -- replacing a key (NetOrk/netork#2013) ----------------------------------------------
|
||||
|
||||
|
||||
def _alice_keys(host: _Host) -> str:
|
||||
return host.files.get("/home/alice/.ssh/authorized_keys", "")
|
||||
|
||||
|
||||
def test_a_replaced_key_goes_and_the_persons_own_keys_stay(host: _Host, driver: _Driver) -> None:
|
||||
_add_alice(host, keys=f"{OLD_KEY}\n{OWN_KEY}\n")
|
||||
|
||||
result = driver.add_local_account("alice", ALICE_KEY, replaces=[OLD_KEY])
|
||||
|
||||
assert result["success"], result["output"]
|
||||
assert [s["step"] for s in result["steps"]] == ["account", "sudo", "keys", "old keys"]
|
||||
assert _alice_keys(host) == f"{OWN_KEY}\n{ALICE_KEY}\n"
|
||||
account = result["account"]
|
||||
assert account is not None and account["authorized_keys"] == [OWN_KEY, ALICE_KEY]
|
||||
|
||||
|
||||
def test_a_replaced_key_goes_from_every_key_file_with_any_options_or_comment(
|
||||
host: _Host, driver: _Driver
|
||||
) -> None:
|
||||
# The same key with options in front and another comment is the same key.
|
||||
blob = OLD_KEY.rsplit(" ", 1)[0]
|
||||
_add_alice(host, keys=f'from="10.0.0.0/8" {blob} renamed\n# the laptop\n')
|
||||
host.files["/home/alice/.ssh/authorized_keys2"] = f"{OWN_KEY}\n{blob}\n"
|
||||
|
||||
result = driver.add_local_account("alice", ALICE_KEY, replaces=[OLD_KEY])
|
||||
|
||||
assert result["success"], result["output"]
|
||||
assert _alice_keys(host) == f"# the laptop\n{ALICE_KEY}\n"
|
||||
assert host.files["/home/alice/.ssh/authorized_keys2"] == f"{OWN_KEY}\n"
|
||||
assert not [p for p in host.files if p.startswith("/tmp/")]
|
||||
|
||||
|
||||
def test_a_replaced_key_that_is_not_there_writes_nothing(host: _Host, driver: _Driver) -> None:
|
||||
_add_alice(host, keys=f"{ALICE_KEY}\n")
|
||||
|
||||
result = driver.add_local_account("alice", ALICE_KEY, replaces=[OLD_KEY])
|
||||
|
||||
assert result["success"], result["output"]
|
||||
assert host.staged == 0
|
||||
assert "none of the keys" in result["steps"][-1]["output"]
|
||||
|
||||
|
||||
def test_the_new_key_is_never_taken_for_a_replaced_one(host: _Host, driver: _Driver) -> None:
|
||||
_add_alice(host, keys=f"{OLD_KEY}\n")
|
||||
|
||||
result = driver.add_local_account("alice", ALICE_KEY, replaces=[OLD_KEY, ALICE_KEY])
|
||||
|
||||
assert result["success"], result["output"]
|
||||
assert _alice_keys(host) == f"{ALICE_KEY}\n"
|
||||
|
||||
|
||||
def test_without_replaces_no_key_goes(host: _Host, driver: _Driver) -> None:
|
||||
_add_alice(host, keys=f"{OLD_KEY}\n")
|
||||
|
||||
result = driver.add_local_account("alice", ALICE_KEY)
|
||||
|
||||
assert [s["step"] for s in result["steps"]] == ["account", "sudo", "keys"]
|
||||
assert _alice_keys(host) == f"{OLD_KEY}\n{ALICE_KEY}\n"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("replaces", [["not a key"], [OLD_KEY, ""], OLD_KEY])
|
||||
def test_a_replaced_key_that_is_none_is_refused(
|
||||
host: _Host, driver: _Driver, replaces: list[str]
|
||||
) -> None:
|
||||
with pytest.raises(ValueError, match="key"):
|
||||
driver.add_local_account("alice", ALICE_KEY, replaces=replaces)
|
||||
assert _root_commands(host) == []
|
||||
|
||||
|
||||
def test_the_old_key_goes_even_when_the_new_one_could_not_be_added(host: _Host) -> None:
|
||||
class _AddFails(_Driver):
|
||||
def _account_add_key(self, name: str, home: str, public_key: str) -> FixStepOutcomeDict:
|
||||
return {"ok": False, "output": "disk full"}
|
||||
|
||||
_add_alice(host, keys=f"{OLD_KEY}\n{OWN_KEY}\n")
|
||||
|
||||
result = _AddFails(host).add_local_account("alice", ALICE_KEY, replaces=[OLD_KEY])
|
||||
|
||||
assert not result["success"]
|
||||
assert next(s for s in result["steps"] if s["step"] == "old keys")["ok"]
|
||||
assert _alice_keys(host) == f"{OWN_KEY}\n"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("failing", "ignored", "said"),
|
||||
[
|
||||
({"as cat"}, set(), "cannot read"),
|
||||
({"as install"}, set(), "authorized_keys"),
|
||||
(set(), {"as install"}, "still has"),
|
||||
],
|
||||
)
|
||||
def test_a_replaced_key_that_did_not_go_says_so(
|
||||
host: _Host, driver: _Driver, failing: set[str], ignored: set[str], said: str
|
||||
) -> None:
|
||||
_add_alice(host, keys=f"{OLD_KEY}\n{ALICE_KEY}\n")
|
||||
host.failing |= failing
|
||||
host.ignored |= ignored
|
||||
|
||||
result = driver.add_local_account("alice", ALICE_KEY, replaces=[OLD_KEY])
|
||||
|
||||
old = next(s for s in result["steps"] if s["step"] == "old keys")
|
||||
assert not old["ok"] and said in old["output"]
|
||||
assert not result["success"]
|
||||
|
||||
|
||||
# -- removing the password (NetOrk/netork#2013) ----------------------------------------
|
||||
|
||||
|
||||
def test_clearing_the_password_leaves_a_key_only_account_without_sudo(
|
||||
host: _Host, driver: _Driver
|
||||
) -> None:
|
||||
_add_alice(host, keys=f"{ALICE_KEY}\n")
|
||||
host.files["/etc/sudoers.d/80-account-alice"] = sudoers_rule("alice")
|
||||
|
||||
result = driver.add_local_account("alice", ALICE_KEY, clear_password=True)
|
||||
|
||||
assert result["success"], result["output"]
|
||||
assert [s["step"] for s in result["steps"]] == ["account", "sudo", "password", "keys"]
|
||||
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
|
||||
assert _alice_keys(host) == f"{ALICE_KEY}\n"
|
||||
|
||||
|
||||
def test_a_new_account_without_a_password_gets_it_cleared_too(host: _Host, driver: _Driver) -> None:
|
||||
result = driver.add_local_account("alice", ALICE_KEY, clear_password=True)
|
||||
|
||||
assert result["success"], result["output"]
|
||||
assert ["clearpw", "alice"] == host.ran_words("clearpw")
|
||||
|
||||
|
||||
def test_without_clear_password_the_password_stays(host: _Host, driver: _Driver) -> None:
|
||||
_add_alice(host)
|
||||
|
||||
result = driver.add_local_account("alice", ALICE_KEY)
|
||||
|
||||
assert "password" not in [s["step"] for s in result["steps"]]
|
||||
assert host.accounts["alice"].password == HASH
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"kwargs",
|
||||
[{"password_hash": HASH}, {"admin": True}],
|
||||
)
|
||||
def test_clearing_the_password_refuses_a_password_or_sudo(
|
||||
host: _Host, driver: _Driver, kwargs: dict[str, object]
|
||||
) -> None:
|
||||
# The sudo rule asks for the password: an account without one cannot use it.
|
||||
with pytest.raises(ValueError, match="password"):
|
||||
driver.add_local_account("alice", ALICE_KEY, clear_password=True, **kwargs) # type: ignore[arg-type]
|
||||
assert _root_commands(host) == []
|
||||
|
||||
|
||||
def test_a_password_that_cannot_be_cleared_fails_its_step_only(
|
||||
host: _Host, driver: _Driver
|
||||
) -> None:
|
||||
_add_alice(host)
|
||||
host.failing.add("clearpw")
|
||||
|
||||
result = driver.add_local_account("alice", ALICE_KEY, clear_password=True)
|
||||
|
||||
assert not result["success"]
|
||||
password = next(s for s in result["steps"] if s["step"] == "password")
|
||||
assert not password["ok"] and "clearpw: failed" in password["output"]
|
||||
# sudo and the key do not wait for it.
|
||||
assert next(s for s in result["steps"] if s["step"] == "sudo")["ok"]
|
||||
assert _alice_keys(host) == f"{ALICE_KEY}\n"
|
||||
|
||||
|
||||
def test_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)
|
||||
|
||||
result = _NoClear(host).add_local_account("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 host.accounts["alice"].password == HASH
|
||||
|
||||
|
||||
# -- removing keys without a new one (NetOrk/netork#2013) -------------------------------
|
||||
|
||||
|
||||
def test_removing_keys_takes_only_those_and_leaves_the_account_open(
|
||||
host: _Host, driver: _Driver
|
||||
) -> None:
|
||||
_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])
|
||||
|
||||
assert result["success"], result["output"]
|
||||
assert [s["step"] for s in result["steps"]] == ["keys"]
|
||||
assert _alice_keys(host) == f"{OWN_KEY}\n"
|
||||
a = host.accounts["alice"]
|
||||
assert not a.locked and a.password == HASH and a.groups == ["alice", "wheel"]
|
||||
assert "/etc/sudoers.d/80-account-alice" in host.files
|
||||
account = result["account"]
|
||||
assert account is not None and account["authorized_keys"] == [OWN_KEY]
|
||||
|
||||
|
||||
def test_removing_the_last_key_leaves_an_empty_key_file(host: _Host, driver: _Driver) -> None:
|
||||
_add_alice(host, keys=f"{ALICE_KEY}\n")
|
||||
|
||||
result = driver.remove_local_account_keys("alice", [ALICE_KEY])
|
||||
|
||||
assert result["success"], result["output"]
|
||||
assert _alice_keys(host) == ""
|
||||
assert not host.accounts["alice"].locked
|
||||
|
||||
|
||||
def test_removing_keys_changes_nothing_as_root(host: _Host, driver: _Driver) -> None:
|
||||
# Root only asks whether a key file is there, and reads the account back
|
||||
# (its lock, sudo -l); the files are written as the person.
|
||||
_add_alice(host, keys=f"{ALICE_KEY}\n")
|
||||
|
||||
driver.remove_local_account_keys("alice", [ALICE_KEY])
|
||||
|
||||
for command in _root_commands(host):
|
||||
words = shlex.split(command)
|
||||
assert words[0] in {"test", "islocked", "sudo"}, command
|
||||
if words[0] == "sudo":
|
||||
assert words[:3] in (["sudo", "-n", "-u"], ["sudo", "-l", "-U"]), command
|
||||
|
||||
|
||||
def test_removing_keys_of_an_account_that_is_not_there_fails(driver: _Driver) -> None:
|
||||
result = driver.remove_local_account_keys("bob", [ALICE_KEY])
|
||||
|
||||
assert not result["success"]
|
||||
assert "no account" in result["output"]
|
||||
assert result["account"] is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("keys", [[], ["not a key"], ALICE_KEY])
|
||||
def test_removing_keys_refuses_what_is_no_list_of_keys(
|
||||
host: _Host, driver: _Driver, keys: list[str]
|
||||
) -> None:
|
||||
_add_alice(host, keys=f"{ALICE_KEY}\n")
|
||||
|
||||
with pytest.raises(ValueError, match="key"):
|
||||
driver.remove_local_account_keys("alice", keys)
|
||||
assert _alice_keys(host) == f"{ALICE_KEY}\n"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("name", "said"), [("netadmin", "logs in"), ("daemon", "system")])
|
||||
def test_removing_keys_never_touches_the_login_user_or_a_system_account(
|
||||
host: _Host, driver: _Driver, name: str, said: str
|
||||
) -> None:
|
||||
with pytest.raises(ValueError, match=said):
|
||||
driver.remove_local_account_keys(name, [ALICE_KEY])
|
||||
assert _root_commands(host) == []
|
||||
|
||||
|
||||
def test_a_driver_with_its_own_key_store_drops_keys_there(host: _Host) -> None:
|
||||
class _Store(_OwnKeyStore):
|
||||
def _account_drop_keys(self, name: str, home: str, keys: list[str]) -> FixStepOutcomeDict:
|
||||
gone = {k.split()[1] for k in keys}
|
||||
self.store[name] = [k for k in self.store.get(name, []) if k.split()[2] not in gone]
|
||||
return {"ok": True, "output": "dropped some"}
|
||||
|
||||
driver = _Store(host)
|
||||
driver.add_local_account("alice", OLD_KEY)
|
||||
driver.add_local_account("alice", ALICE_KEY, replaces=[OLD_KEY])
|
||||
assert driver.store == {"alice": [f"/home/alice {ALICE_KEY}"]}
|
||||
|
||||
removed = driver.remove_local_account_keys("alice", [ALICE_KEY])
|
||||
assert removed["success"], removed["output"]
|
||||
assert driver.store == {"alice": []}
|
||||
Reference in new issue
Block a user