feat: a driver may keep the account keys elsewhere (NetOrk/netork#1793)
driver / standard (pull_request) Successful in 16s
driver / lint (pull_request) Successful in 13s
driver / types (pull_request) Successful in 1m6s
driver / test (3.13) (pull_request) Successful in 1m8s
driver / test (3.12) (pull_request) Successful in 1m14s
driver / lowest (pull_request) Successful in 36s
driver / build (pull_request) Successful in 34s
driver / security (pull_request) Successful in 1m9s
CI / driver (pull_request) Successful in 2m44s
driver / publish (pull_request) Skipped

OpenMediaVault keeps a user's keys in its own database and deploys them
to /var/lib/openmediavault/ssh/authorized_keys, and creates a home only
when its home directory setting is on. The key store becomes three hooks
with the ~/.ssh default: _account_read_keys, _account_add_key and
_account_remove_keys, each given the account's home.
This commit is contained in:
christianmanivong committed 2026-10-31 23:29:46 +01:00
1 parent 49c65cbd5d
commit 0545d1d814
3 files changed
+77 -15

No files matched your search

+36 -13
View File
@@ -195,6 +195,10 @@ class LocalAccountsMixin:
) -> CommandResult: ...
# -- what a driver supplies ------------------------------------------------------
#
# ``_account_*`` are the driver's: the five below it must supply, the key
# store (``_account_read_keys``, ``_account_add_key``, ``_account_remove_keys``)
# it may replace. ``_accounts_*`` are this mixin's own.
def _account_create(self, name: str, password_hash: str | None) -> list[list[str]]:
"""The commands that create *name* with a home and :attr:`ACCOUNT_SHELL`.
@@ -263,7 +267,7 @@ class LocalAccountsMixin:
"groups": groups.stdout.split(),
"locked": self._account_locked(name, passwd),
"admin": self._accounts_admin(name),
"authorized_keys": self._accounts_keys(name, home),
"authorized_keys": self._account_read_keys(name, home),
}
def _accounts_admin(self, name: str) -> bool:
@@ -272,8 +276,12 @@ class LocalAccountsMixin:
raise RuntimeError(f"cannot ask sudo about {name}: {_said(result)}")
return bool(_RUN_ALL_RE.search(result.stdout))
def _accounts_keys(self, name: str, home: str) -> list[str] | None:
"""The lines of *name*'s key files, read as *name*; None when it cannot."""
def _account_read_keys(self, name: str, home: str) -> list[str] | None:
"""The lines of *name*'s key files, read as *name*; None when it cannot.
Every key sshd would take for *name*: a driver whose host keeps keys in
a place of its own as well reads that too.
"""
lines: list[str] = []
for path in authorized_keys_paths(home):
there = self.run_command(_command(["test", "-e", path]), privileged=True, timeout=30)
@@ -483,13 +491,30 @@ class LocalAccountsMixin:
def _accounts_unstage(self, path: str) -> None:
self.run_command(_command(["rm", "-f", "--", path]), timeout=30)
def _accounts_add_key(self, name: str, public_key: str) -> FixStepOutcomeDict:
def _accounts_home(self, name: str) -> str | None:
passwd = self._accounts_passwd(name)
if passwd is None:
return passwd[5] if passwd is not None else None
def _accounts_add_key(self, name: str, public_key: str) -> FixStepOutcomeDict:
home = self._accounts_home(name)
if home is None:
return {"ok": False, "output": f"no account {name}"}
home = passwd[5]
return self._account_add_key(name, home, public_key)
def _accounts_remove_keys(self, name: str) -> FixStepOutcomeDict:
home = self._accounts_home(name)
if home is None:
return {"ok": False, "output": f"no account {name}"}
return self._account_remove_keys(name, home)
def _account_add_key(self, name: str, home: str, public_key: str) -> FixStepOutcomeDict:
"""Add *public_key* to *name*'s first key file, as *name*; the others stay.
A driver whose host keeps keys elsewhere overrides this, with
:meth:`_account_read_keys` and :meth:`_account_remove_keys`.
"""
path = authorized_keys_paths(home)[0]
current = self._accounts_keys(name, home)
current = self._account_read_keys(name, home)
if current is None:
return {"ok": False, "output": f"cannot read the key files of {name}"}
blob = _key_blob(public_key)
@@ -529,15 +554,13 @@ class LocalAccountsMixin:
return result
return None
def _accounts_remove_keys(self, name: str) -> FixStepOutcomeDict:
passwd = self._accounts_passwd(name)
if passwd is None:
return {"ok": False, "output": f"no account {name}"}
paths = authorized_keys_paths(passwd[5])
def _account_remove_keys(self, name: str, home: str) -> FixStepOutcomeDict:
"""Remove *name*'s key files, as *name*."""
paths = authorized_keys_paths(home)
failed = self._accounts_run_as(name, [["rm", "-f", "--", *paths]])
if failed is not None:
return {"ok": False, "output": f"cannot remove the keys of {name}: {_said(failed)}"}
if self._accounts_keys(name, passwd[5]):
if self._account_read_keys(name, home):
return {"ok": False, "output": f"{name} still has keys"}
return {"ok": True, "output": f"keys of {name} removed"}