fix(users): list real login accounts and read admins from the ACL
The passwd filter's precedence was inverted: it listed uid < 1000 (daemon, bin, ...) and skipped regular users. The level check looked for "Administrator" in the user record, which never names roles. The level now comes from /access/acl (Administrator on /, by user or group), root@pam always 15. Closes #19
This commit is contained in:
1 parent
037a725b74
commit
4ee74e4b3d
4 files changed
+142
-42
No files matched your search
@@ -56,6 +56,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
they are, and the old FQDN becomes the new one instead of a mix of both (#21).
|
they are, and the old FQDN becomes the new one instead of a mix of both (#21).
|
||||||
- `apply_updates()` reports a failed upgrade as `success: False`, with apt-get's
|
- `apply_updates()` reports a failed upgrade as `success: False`, with apt-get's
|
||||||
exit status in `error`; it reported success whatever apt-get did (#20).
|
exit status in `error`; it reported success whatever apt-get did (#20).
|
||||||
|
- `get_users()` lists the node's root and regular login accounts (uid 1000 and
|
||||||
|
up) instead of its system accounts (`daemon`, `bin`, …), and gives a Proxmox
|
||||||
|
user level 15 when the ACL grants it the Administrator role on `/`, directly
|
||||||
|
or through a group; `root@pam` is always 15. Before, practically every
|
||||||
|
Proxmox user was level 1 (#19).
|
||||||
|
|
||||||
## [0.8.0] - 2026-10-23
|
## [0.8.0] - 2026-10-23
|
||||||
|
|
||||||
|
|||||||
@@ -73,6 +73,40 @@ def _rename_in_hosts(hosts: str, old_short: str, new_short: str, new_domain: str
|
|||||||
return "".join(lines)
|
return "".join(lines)
|
||||||
|
|
||||||
|
|
||||||
|
#: Proxmox's superuser, whatever the ACL says.
|
||||||
|
_PVE_SUPERUSER = "root@pam"
|
||||||
|
#: The role that makes a Proxmox user an administrator of the node.
|
||||||
|
_PVE_ADMIN_ROLE = "Administrator"
|
||||||
|
#: Regular login accounts: Debian gives them uids from 1000 up; 65534 is nobody.
|
||||||
|
_FIRST_REGULAR_UID = 1000
|
||||||
|
_NOBODY_UID = 65534
|
||||||
|
|
||||||
|
|
||||||
|
def _words(value: Any, sep: str | None = None) -> list[str]:
|
||||||
|
return [w.strip() for w in str(value or "").split(sep) if w.strip()]
|
||||||
|
|
||||||
|
|
||||||
|
def _user_entry(level: int) -> _JsonDict:
|
||||||
|
# NAPALM's get_users shape; the password is never read.
|
||||||
|
return {"level": level, "password": "", "sshkeys": []} # nosec B105
|
||||||
|
|
||||||
|
|
||||||
|
def _login_accounts(passwd: str) -> list[tuple[str, int]]:
|
||||||
|
"""``(name, uid)`` of root and every regular account in *passwd*."""
|
||||||
|
accounts = []
|
||||||
|
for line in passwd.splitlines():
|
||||||
|
parts = line.split(":")
|
||||||
|
if len(parts) < 7:
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
uid = int(parts[2])
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
|
if uid == 0 or _FIRST_REGULAR_UID <= uid < _NOBODY_UID:
|
||||||
|
accounts.append((parts[0], uid))
|
||||||
|
return accounts
|
||||||
|
|
||||||
|
|
||||||
def _net(value: str) -> str:
|
def _net(value: str) -> str:
|
||||||
return str(ipaddress.ip_network(value.strip(), strict=False))
|
return str(ipaddress.ip_network(value.strip(), strict=False))
|
||||||
|
|
||||||
@@ -239,55 +273,52 @@ class ProxmoxSystemMixin:
|
|||||||
def get_users(self) -> dict[str, _JsonDict]:
|
def get_users(self) -> dict[str, _JsonDict]:
|
||||||
"""Return users configured on the Proxmox node.
|
"""Return users configured on the Proxmox node.
|
||||||
|
|
||||||
Reads from both the Proxmox access/users API and local /etc/passwd.
|
Proxmox's own users (``/access/users``) at level 15 when they hold the
|
||||||
|
Administrator role on the whole datacenter (``/``), directly or through
|
||||||
|
a group, 1 otherwise; ``root@pam`` is always 15. Then the node's login
|
||||||
|
accounts from the passwd database: root (15) and regular users
|
||||||
|
(uid 1000 and up, without ``nobody``) at 0; system accounts are left out.
|
||||||
"""
|
"""
|
||||||
result: dict[str, _JsonDict] = {}
|
result: dict[str, _JsonDict] = {}
|
||||||
try:
|
admins, admin_groups = self._pve_administrators()
|
||||||
pve_users = self._api.access.users.get() or [] # type: ignore[union-attr]
|
for user in self._pve_users():
|
||||||
except Exception as exc:
|
|
||||||
logger.debug("Failed to fetch Proxmox users: %s", exc)
|
|
||||||
pve_users = []
|
|
||||||
|
|
||||||
for user in pve_users:
|
|
||||||
uid = user.get("userid", "")
|
uid = user.get("userid", "")
|
||||||
if not uid:
|
if not uid:
|
||||||
continue
|
continue
|
||||||
# Proxmox roles: Administrator -> 15, otherwise 1
|
groups = set(_words(user.get("groups"), sep=","))
|
||||||
level = 1
|
is_admin = uid == _PVE_SUPERUSER or uid in admins or bool(groups & admin_groups)
|
||||||
try:
|
result[uid] = _user_entry(15 if is_admin else 1)
|
||||||
roles = self._api.access.users(uid).get() or {} # type: ignore[union-attr]
|
|
||||||
if "Administrator" in str(roles):
|
|
||||||
level = 15
|
|
||||||
except Exception as exc:
|
|
||||||
logger.debug("Failed to fetch roles for user %s: %s", uid, exc)
|
|
||||||
result[uid] = {
|
|
||||||
"level": level,
|
|
||||||
# NAPALM's get_users shape; the password is never read.
|
|
||||||
"password": "", # nosec B105
|
|
||||||
"sshkeys": [],
|
|
||||||
}
|
|
||||||
|
|
||||||
# Merge local OS users from /etc/passwd
|
|
||||||
raw = self._exec_ssh_command("getent passwd 2>/dev/null || cat /etc/passwd")
|
raw = self._exec_ssh_command("getent passwd 2>/dev/null || cat /etc/passwd")
|
||||||
for line in raw.splitlines():
|
for uname, uid_int in _login_accounts(raw):
|
||||||
parts = line.split(":")
|
result.setdefault(uname, _user_entry(15 if uid_int == 0 else 0))
|
||||||
if len(parts) < 7:
|
|
||||||
continue
|
|
||||||
uname, _, uid_str, *_ = parts
|
|
||||||
try:
|
|
||||||
uid_int = int(uid_str)
|
|
||||||
except ValueError:
|
|
||||||
continue
|
|
||||||
if uname not in result and uid_int < 1000 or uid_int == 0:
|
|
||||||
result[uname] = {
|
|
||||||
"level": 15 if uid_int == 0 else 0,
|
|
||||||
# NAPALM's get_users shape; the password is never read.
|
|
||||||
"password": "", # nosec B105
|
|
||||||
"sshkeys": [],
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def _pve_users(self) -> list[_JsonDict]:
|
||||||
|
try:
|
||||||
|
return self._api.access.users.get() or [] # type: ignore[union-attr]
|
||||||
|
except Exception as exc:
|
||||||
|
logger.debug("Failed to fetch Proxmox users: %s", exc)
|
||||||
|
return []
|
||||||
|
|
||||||
|
def _pve_administrators(self) -> tuple[set[str], set[str]]:
|
||||||
|
"""Users and groups holding the Administrator role on ``/`` (``/access/acl``)."""
|
||||||
|
try:
|
||||||
|
acl = self._api.access.acl.get() or [] # type: ignore[union-attr]
|
||||||
|
except Exception as exc:
|
||||||
|
logger.debug("Failed to fetch the Proxmox ACL: %s", exc)
|
||||||
|
acl = []
|
||||||
|
users: set[str] = set()
|
||||||
|
groups: set[str] = set()
|
||||||
|
for entry in acl:
|
||||||
|
if entry.get("path") != "/" or entry.get("roleid") != _PVE_ADMIN_ROLE:
|
||||||
|
continue
|
||||||
|
if entry.get("type") == "user":
|
||||||
|
users.add(entry.get("ugid", ""))
|
||||||
|
elif entry.get("type") == "group":
|
||||||
|
groups.add(entry.get("ugid", ""))
|
||||||
|
return users, groups
|
||||||
|
|
||||||
# ------------------------------------------------------------------ #
|
# ------------------------------------------------------------------ #
|
||||||
# Kernel facts (KernelFactsMixin supplies get_kernel_facts)
|
# Kernel facts (KernelFactsMixin supplies get_kernel_facts)
|
||||||
# ------------------------------------------------------------------ #
|
# ------------------------------------------------------------------ #
|
||||||
|
|||||||
@@ -16,7 +16,6 @@
|
|||||||
"napalm_proxmox/sdn_mixin.py::ProxmoxSDNMixin.set_vlan": 13,
|
"napalm_proxmox/sdn_mixin.py::ProxmoxSDNMixin.set_vlan": 13,
|
||||||
"napalm_proxmox/system_mixin.py::ProxmoxSystemMixin.get_disk_smart": 22,
|
"napalm_proxmox/system_mixin.py::ProxmoxSystemMixin.get_disk_smart": 22,
|
||||||
"napalm_proxmox/system_mixin.py::ProxmoxSystemMixin.get_packages": 16,
|
"napalm_proxmox/system_mixin.py::ProxmoxSystemMixin.get_packages": 16,
|
||||||
"napalm_proxmox/system_mixin.py::ProxmoxSystemMixin.get_users": 15,
|
|
||||||
"napalm_proxmox/utils.py::addresses_from_node_network": 15,
|
"napalm_proxmox/utils.py::addresses_from_node_network": 15,
|
||||||
"napalm_proxmox/vm_contract_mixin.py::parse_vm_config": 16,
|
"napalm_proxmox/vm_contract_mixin.py::parse_vm_config": 16,
|
||||||
"napalm_proxmox/vm_mixin.py::ProxmoxVMMixin._get_vm_disk_and_boot": 16,
|
"napalm_proxmox/vm_mixin.py::ProxmoxVMMixin._get_vm_disk_and_boot": 16,
|
||||||
@@ -35,7 +34,6 @@
|
|||||||
"napalm_proxmox/sdn_mixin.py::ProxmoxSDNMixin.set_vlan": 70,
|
"napalm_proxmox/sdn_mixin.py::ProxmoxSDNMixin.set_vlan": 70,
|
||||||
"napalm_proxmox/system_mixin.py::ProxmoxSystemMixin.get_disk_smart": 74,
|
"napalm_proxmox/system_mixin.py::ProxmoxSystemMixin.get_disk_smart": 74,
|
||||||
"napalm_proxmox/system_mixin.py::ProxmoxSystemMixin.get_packages": 66,
|
"napalm_proxmox/system_mixin.py::ProxmoxSystemMixin.get_packages": 66,
|
||||||
"napalm_proxmox/system_mixin.py::ProxmoxSystemMixin.get_users": 51,
|
|
||||||
"napalm_proxmox/system_mixin.py::ProxmoxSystemMixin.set_hostname": 114,
|
"napalm_proxmox/system_mixin.py::ProxmoxSystemMixin.set_hostname": 114,
|
||||||
"napalm_proxmox/vm_mixin.py::ProxmoxVMMixin.get_vm_interfaces": 179,
|
"napalm_proxmox/vm_mixin.py::ProxmoxVMMixin.get_vm_interfaces": 179,
|
||||||
"napalm_proxmox/vm_mixin.py::ProxmoxVMMixin.get_vms": 114,
|
"napalm_proxmox/vm_mixin.py::ProxmoxVMMixin.get_vms": 114,
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
"""get_users: Proxmox users with their level, and the node's login accounts (#19)."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
PASSWD = """\
|
||||||
|
root:x:0:0:root:/root:/bin/bash
|
||||||
|
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
|
||||||
|
bin:x:2:2:bin:/bin:/usr/sbin/nologin
|
||||||
|
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
|
||||||
|
alice:x:1000:1000:Alice:/home/alice:/bin/bash
|
||||||
|
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
|
||||||
|
"""
|
||||||
|
|
||||||
|
# GET /access/acl: who holds which role where.
|
||||||
|
ACL = [
|
||||||
|
{"path": "/", "roleid": "Administrator", "type": "user", "ugid": "admin@pve", "propagate": 1},
|
||||||
|
{"path": "/", "roleid": "Administrator", "type": "group", "ugid": "ops", "propagate": 1},
|
||||||
|
{"path": "/vms/100", "roleid": "Administrator", "type": "user", "ugid": "vm@pve"},
|
||||||
|
{"path": "/", "roleid": "PVEAuditor", "type": "user", "ugid": "audit@pve", "propagate": 1},
|
||||||
|
]
|
||||||
|
|
||||||
|
USERS = [
|
||||||
|
{"userid": "root@pam", "groups": ""},
|
||||||
|
{"userid": "admin@pve", "groups": ""},
|
||||||
|
{"userid": "bob@pve", "groups": "ops,dev"},
|
||||||
|
{"userid": "vm@pve", "groups": ""},
|
||||||
|
{"userid": "audit@pve", "groups": ""},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def _users(driver, passwd=PASSWD):
|
||||||
|
driver._api.access.users.get.return_value = USERS
|
||||||
|
driver._api.access.acl.get.return_value = ACL
|
||||||
|
with patch.object(driver, "_exec_ssh_command", return_value=passwd):
|
||||||
|
return driver.get_users()
|
||||||
|
|
||||||
|
|
||||||
|
class TestLocalAccounts:
|
||||||
|
def test_root_and_real_users_not_system_accounts(self, driver):
|
||||||
|
result = _users(driver)
|
||||||
|
|
||||||
|
assert result["root"]["level"] == 15
|
||||||
|
assert "alice" in result
|
||||||
|
for system in ("daemon", "bin", "www-data", "nobody"):
|
||||||
|
assert system not in result
|
||||||
|
|
||||||
|
|
||||||
|
class TestProxmoxUsers:
|
||||||
|
def test_administrator_on_the_whole_datacenter_is_level_15(self, driver):
|
||||||
|
result = _users(driver)
|
||||||
|
|
||||||
|
assert result["admin@pve"]["level"] == 15
|
||||||
|
|
||||||
|
def test_administrator_through_a_group_is_level_15(self, driver):
|
||||||
|
assert _users(driver)["bob@pve"]["level"] == 15
|
||||||
|
|
||||||
|
def test_root_at_pam_is_always_level_15(self, driver):
|
||||||
|
assert _users(driver)["root@pam"]["level"] == 15
|
||||||
|
|
||||||
|
def test_another_role_or_a_narrower_path_is_level_1(self, driver):
|
||||||
|
result = _users(driver)
|
||||||
|
|
||||||
|
assert result["audit@pve"]["level"] == 1
|
||||||
|
assert result["vm@pve"]["level"] == 1
|
||||||
Reference in new issue
Block a user