feat(accounts): take replaced and deleted keys out of OMV's database
driver / standard (pull_request) Successful in 16s
driver / lint (pull_request) Successful in 14s
driver / types (pull_request) Successful in 1m40s
driver / test (3.12) (pull_request) Successful in 1m44s
driver / lowest (pull_request) Successful in 38s
driver / test (3.13) (pull_request) Successful in 1m50s
driver / build (pull_request) Successful in 35s
driver / security (pull_request) Successful in 1m15s
CI / driver (pull_request) Successful in 3m29s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Successful in 16s
driver / lint (pull_request) Successful in 14s
driver / types (pull_request) Successful in 1m40s
driver / test (3.12) (pull_request) Successful in 1m44s
driver / lowest (pull_request) Successful in 38s
driver / test (3.13) (pull_request) Successful in 1m50s
driver / build (pull_request) Successful in 35s
driver / security (pull_request) Successful in 1m15s
CI / driver (pull_request) Successful in 3m29s
driver / publish (pull_request) Skipped
napalm-device-types 4.28.0 lets add_local_account remove the old keys (replaces) and adds remove_local_account_keys. OMV keeps a user's keys in its own database and deploys them where the person cannot write, so the mixin's key files alone would leave the old key deployed. The keys go through UserMgmt.setUser, applying only what that made dirty, and then out of ~/.ssh. Clearing the password is napalm-linux 0.17.0's. See NetOrk/netork#2013
This commit is contained in:
1 parent
509cf01a70
commit
ed083df018
4 files changed
+146
-3
No files matched your search
@@ -20,6 +20,8 @@ from napalm_openmediavault._local_accounts import OMV_KEYS_DIR, rfc4716_blob
|
||||
from napalm_openmediavault.openmediavault import OpenMediaVaultDriver
|
||||
|
||||
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
|
||||
|
||||
@@ -128,7 +130,22 @@ class _Driver(OpenMediaVaultDriver):
|
||||
if argv[:3] == ["sudo", "-n", "-u"] and argv[4] == "rm":
|
||||
self.home_keys = []
|
||||
return CommandResult("", "", 0)
|
||||
raise AssertionError(command)
|
||||
return self._write_home_keys(argv, stdin)
|
||||
|
||||
def _write_home_keys(self, argv: list[str], stdin: bytes | None) -> CommandResult:
|
||||
"""Staging a key file as the login user, and installing it as the person."""
|
||||
if stdin is not None:
|
||||
self.staged = stdin.decode()
|
||||
return CommandResult("/tmp/tmp.1", "", 0)
|
||||
if argv[:3] == ["sudo", "-n", "-u"] and argv[4] == "mkdir":
|
||||
return CommandResult("", "", 0)
|
||||
if argv[:3] == ["sudo", "-n", "-u"] and argv[4] == "install":
|
||||
assert argv[-1] == "/home/alice/.ssh/authorized_keys", "only ~/.ssh is written"
|
||||
self.home_keys = self.staged.splitlines()
|
||||
return CommandResult("", "", 0)
|
||||
if argv[:3] == ["rm", "-f", "--"] and argv[3].startswith("/tmp/"):
|
||||
return CommandResult("", "", 0)
|
||||
raise AssertionError(shlex.join(argv))
|
||||
|
||||
|
||||
def test_omv_offers_personal_accounts_of_its_own() -> None:
|
||||
@@ -253,3 +270,100 @@ def test_netorks_own_account_with_a_fixed_uid_is_not_made_on_omv_yet() -> None:
|
||||
|
||||
with pytest.raises(NotImplementedError):
|
||||
_Driver(Omv())._account_create_with_uid("netork", 900)
|
||||
|
||||
|
||||
# -- what a person changes follows them (NetOrk/netork#2013) ----------------------------
|
||||
|
||||
|
||||
def test_a_replaced_key_goes_from_omvs_database_and_the_others_stay() -> None:
|
||||
omv = Omv()
|
||||
omv.users["alice"]["sshpubkeys"] = [_rfc4716(OLD_KEY), _rfc4716(OWN_KEY)]
|
||||
omv.deployed["alice"] = [OLD_KEY, OWN_KEY]
|
||||
|
||||
outcome = _Driver(omv)._account_drop_keys("alice", "/home/alice", [OLD_KEY])
|
||||
|
||||
assert outcome["ok"], outcome["output"]
|
||||
[params] = omv.params("UserMgmt.setUser")
|
||||
assert params["sshpubkeys"] == [_rfc4716(OWN_KEY)]
|
||||
# Everything else the user had stays as it was, and only what it dirtied is applied.
|
||||
assert params["email"] == "alice@example.net" and params["groups"] == ["users", "_ssh"]
|
||||
assert omv.params("Config.applyChanges") == [{"modules": ["postfix", "ssh"], "force": False}]
|
||||
assert [rfc4716_blob(k) for k in omv.deployed["alice"]] == [rfc4716_blob(OWN_KEY)]
|
||||
assert "removed from OMV's keys" in outcome["output"]
|
||||
|
||||
|
||||
def test_a_replaced_key_in_the_home_goes_too_as_the_person() -> None:
|
||||
omv = Omv()
|
||||
driver = _Driver(omv, home_keys=[OLD_KEY, ALICE_KEY])
|
||||
|
||||
outcome = driver._account_drop_keys("alice", "/home/alice", [OLD_KEY])
|
||||
|
||||
assert outcome["ok"], outcome["output"]
|
||||
assert driver.home_keys == [ALICE_KEY]
|
||||
# The key was not in OMV's database: nothing is saved or applied there.
|
||||
assert omv.params("UserMgmt.setUser") == []
|
||||
assert omv.params("Config.applyChanges") == []
|
||||
|
||||
|
||||
def test_a_removed_key_omv_does_not_deploy_away_fails_the_step() -> None:
|
||||
omv = Omv(deploys=False)
|
||||
|
||||
outcome = _Driver(omv)._account_drop_keys("alice", "/home/alice", [OWN_KEY])
|
||||
|
||||
assert not outcome["ok"]
|
||||
assert "still has" in outcome["output"]
|
||||
|
||||
|
||||
def test_clearing_the_password_is_napalm_linuxs_and_omv_sends_none() -> None:
|
||||
# OMV keeps no password of its own: setUser is never given one (Omv asserts it).
|
||||
assert _Driver(Omv())._account_clear_password("alice") == [["usermod", "-p", "*", "alice"]]
|
||||
|
||||
|
||||
class _Account(_Driver):
|
||||
"""The account's reads too, for a whole run through the mixin."""
|
||||
|
||||
def run_command(self, command, *, privileged=False, timeout=60, stdin=None):
|
||||
answers = {
|
||||
"id -un": "netadmin\n",
|
||||
"getent passwd alice": "alice:x:1001:100::/home/alice:/bin/bash\n",
|
||||
"getent shadow alice": "alice:$6$old:20000:0:99999:7:::\n",
|
||||
"id -Gn alice": "users _ssh\n",
|
||||
"sudo -l -U alice": "User alice is not allowed to run sudo on nas.\n",
|
||||
}
|
||||
if command in answers:
|
||||
self.ran.append((command, privileged))
|
||||
return CommandResult(answers[command], "", 0)
|
||||
if shlex.split(command)[0] in {"usermod", "gpasswd", "rm"} and privileged:
|
||||
self.ran.append((command, privileged))
|
||||
return CommandResult("", "", 0)
|
||||
return super().run_command(command, privileged=privileged, timeout=timeout, stdin=stdin)
|
||||
|
||||
|
||||
def test_a_person_who_replaced_a_key_and_removed_the_password_gets_both_on_omv() -> None:
|
||||
omv = Omv()
|
||||
omv.users["alice"]["sshpubkeys"] = [_rfc4716(OLD_KEY), _rfc4716(OWN_KEY)]
|
||||
omv.deployed["alice"] = [OLD_KEY, OWN_KEY]
|
||||
driver = _Account(omv)
|
||||
|
||||
result = driver.add_local_account("alice", ALICE_KEY, replaces=[OLD_KEY], clear_password=True)
|
||||
|
||||
assert result["success"], result["output"]
|
||||
assert ("usermod -p '*' alice", True) in driver.ran
|
||||
assert omv.users["alice"]["sshpubkeys"] == [_rfc4716(OWN_KEY), _rfc4716(ALICE_KEY)]
|
||||
account = result["account"]
|
||||
assert account is not None
|
||||
assert [rfc4716_blob(k) for k in account["authorized_keys"] or []] == [
|
||||
rfc4716_blob(OWN_KEY),
|
||||
rfc4716_blob(ALICE_KEY),
|
||||
]
|
||||
|
||||
|
||||
def test_a_deleted_key_goes_through_omv_and_the_account_stays_open() -> None:
|
||||
omv = Omv()
|
||||
driver = _Account(omv)
|
||||
|
||||
result = driver.remove_local_account_keys("alice", [OWN_KEY])
|
||||
|
||||
assert result["success"], result["output"]
|
||||
assert omv.users["alice"]["sshpubkeys"] == []
|
||||
assert not [c for c, _ in driver.ran if c.startswith(("usermod", "gpasswd"))]
|
||||
Reference in new issue
Block a user