Merge pull request 'feat(accounts): take replaced and deleted keys out of OMV's database' (#22) from fix/local-account-key-replace into main
driver / lint (push) Successful in 17s
driver / standard (push) Successful in 19s
driver / types (push) Successful in 1m11s
driver / test (3.13) (push) Successful in 1m5s
driver / test (3.12) (push) Successful in 1m13s
driver / lowest (push) Successful in 31s
driver / build (push) Successful in 33s
driver / security (push) Successful in 1m23s
CI / driver (push) Successful in 2m47s
driver / publish (push) Skipped
driver / lint (push) Successful in 17s
driver / standard (push) Successful in 19s
driver / types (push) Successful in 1m11s
driver / test (3.13) (push) Successful in 1m5s
driver / test (3.12) (push) Successful in 1m13s
driver / lowest (push) Successful in 31s
driver / build (push) Successful in 33s
driver / security (push) Successful in 1m23s
CI / driver (push) Successful in 2m47s
driver / publish (push) Skipped
This commit was merged in pull request #22.
This commit is contained in:
commit
6727f0a79c
4 files changed
+146
-3
No files matched your search
@@ -6,6 +6,18 @@ All notable changes to this driver. The format follows
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
- Personal accounts on OpenMediaVault follow what the person changes
|
||||
(NetOrk/netork#2013): `add_local_account(..., replaces=[...])` takes the old keys out of
|
||||
OMV's own key store (through `UserMgmt.setUser`, applying only what that made dirty) and
|
||||
out of `~/.ssh`, `remove_local_account_keys` does the same for deleted keys and leaves the
|
||||
account open, and `add_local_account(..., clear_password=True)` removes the password
|
||||
(`usermod -p '*'`, as napalm-linux does): the account logs in with its key only and has
|
||||
no sudo. Keys the person added stay.
|
||||
|
||||
### Changed
|
||||
- Needs napalm-device-types 4.28.0 and napalm-linux 0.17.0.
|
||||
|
||||
### Added
|
||||
- Scheduled jobs (NetOrk/netork#1933, #18), on top of napalm-linux's reading and writers.
|
||||
The web UI's "Scheduled Jobs" and the power-management schedule are read through
|
||||
|
||||
@@ -13,6 +13,10 @@ plain password, and netOrk holds only the hash, so the hash is set with
|
||||
``usermod -p``; OMV keeps no password of its own. The lock is napalm-linux's,
|
||||
an expiry, which OMV leaves alone. Like the web UI, ``setUser`` gives an
|
||||
account OMV's primary group ``users``, one made outside OMV too.
|
||||
|
||||
A key the person replaced or deleted (NetOrk/netork#2013) goes from OMV's
|
||||
database the same way, and from ``~/.ssh`` as napalm-linux takes it out; the
|
||||
other keys stay. A password removed is napalm-linux's ``usermod -p '*'``.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -145,6 +149,19 @@ class OmvLocalAccountsMixin(LocalAccountsMixin):
|
||||
outcome = super()._account_remove_keys(name, home)
|
||||
return {"ok": outcome["ok"], "output": said + outcome["output"]}
|
||||
|
||||
def _account_drop_keys(self, name: str, home: str, keys: list[str]) -> FixStepOutcomeDict:
|
||||
"""*keys* out of OMV's database, deployed, then out of ``~/.ssh``; the others stay."""
|
||||
blobs = {rfc4716_blob(key) for key in keys}
|
||||
user = self._omv_rpc("UserMgmt", "getUser", {"name": name})
|
||||
stored = list(user.get("sshpubkeys") or [])
|
||||
kept = [key for key in stored if rfc4716_blob(key) not in blobs]
|
||||
said = ""
|
||||
if len(kept) != len(stored):
|
||||
applied = self._omv_set_keys(user, kept)
|
||||
said = f"{len(stored) - len(kept)} removed from OMV's keys of {name} ({applied})\n"
|
||||
outcome = super()._account_drop_keys(name, home, keys)
|
||||
return {"ok": outcome["ok"], "output": said + outcome["output"]}
|
||||
|
||||
def _omv_set_keys(self, user: dict[str, Any], keys: list[str]) -> str:
|
||||
"""Give *user* *keys* in OMV's database and deploy them; what was applied.
|
||||
|
||||
|
||||
+2
-2
@@ -27,8 +27,8 @@ classifiers = [
|
||||
]
|
||||
dependencies = [
|
||||
"napalm>=4.0",
|
||||
"napalm-device-types>=4.27.0",
|
||||
"napalm-linux>=0.16.0",
|
||||
"napalm-device-types>=4.28.0",
|
||||
"napalm-linux>=0.17.0",
|
||||
]
|
||||
|
||||
[project.optional-dependencies]
|
||||
|
||||
@@ -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