diff --git a/CHANGELOG.md b/CHANGELOG.md index fbabaf4..72d35a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.21.0] - 2026-12-01 + +### Added +- `get_login_sessions()` on PVE and PBS (napalm-device-types' `LoginSessionsMixin`, + 4.55.0): the interactive logins the node recorded over the last seven days -- someone's + own `ssh`, or the web interface's shell -- each with its account, terminal, address and + times, and the address of the driver's own SSH session (NetOrk/netork#2881). Over the + node's exec path; an API-token-only node, which has no SSH login, raises as the other + readings over SSH do. + ## [0.20.0] - 2026-11-29 ### Added diff --git a/napalm_proxmox/backup_driver.py b/napalm_proxmox/backup_driver.py index 4ff4fb7..ed7a5ef 100644 --- a/napalm_proxmox/backup_driver.py +++ b/napalm_proxmox/backup_driver.py @@ -25,6 +25,7 @@ from napalm_device_types import ( KernelFactsMixin, LicenseMixin, ListeningSocketsMixin, + LoginSessionsMixin, LogSourcesMixin, OpenSshServerMixin, PortSpec, @@ -73,6 +74,7 @@ class ProxmoxBackupDriver( ServicePackagesMixin, ListeningSocketsMixin, LogSourcesMixin, + LoginSessionsMixin, ServicesRestartMixin, SystemIdentityMixin, SystemdServicesMixin, diff --git a/napalm_proxmox/driver.py b/napalm_proxmox/driver.py index 025688c..51ec826 100644 --- a/napalm_proxmox/driver.py +++ b/napalm_proxmox/driver.py @@ -37,6 +37,7 @@ from napalm_device_types import ( KernelFactsMixin, LicenseMixin, ListeningSocketsMixin, + LoginSessionsMixin, LogSourcesMixin, OpenSshServerMixin, PortSpec, @@ -110,6 +111,7 @@ class ProxmoxDriver( ServicePackagesMixin, ListeningSocketsMixin, LogSourcesMixin, + LoginSessionsMixin, ServicesRestartMixin, SystemIdentityMixin, SystemdServicesMixin, diff --git a/napalm_proxmox/host_mixin.py b/napalm_proxmox/host_mixin.py index 44f86b8..e76c423 100644 --- a/napalm_proxmox/host_mixin.py +++ b/napalm_proxmox/host_mixin.py @@ -98,6 +98,12 @@ class ProxmoxHostMixin: the exec path, which runs as root either way.""" return str(self._exec_ssh_command(command)) + def _run_login_sessions_command(self, command: str) -> str: + """The transport for ``LoginSessionsMixin.get_login_sessions`` (NetOrk/netork#2881): + the exec path, where ``SSH_CONNECTION`` names this driver's own address. wtmp is + readable to every login. No stdin.""" + return str(self._exec_ssh_command(command)) + def _run_log_sources_command(self, command: str, *, privileged: bool) -> str: """The transport for ``LogSourcesMixin.get_log_sources``: the exec path, which runs as root either way.""" diff --git a/pyproject.toml b/pyproject.toml index cee0413..b482aad 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "napalm-proxmox" -version = "0.20.0" +version = "0.21.0" description = "NAPALM drivers for Proxmox VE (classic Linux networking, SDN, OVS) and Proxmox Backup Server" readme = "README.md" license = "Apache-2.0" @@ -21,7 +21,7 @@ classifiers = [ requires-python = ">=3.12" dependencies = [ "napalm>=5.0.0", - "napalm_device_types>=4.53.0", # CpuMicrocodeMixin, virtualization in the hardening; WebLoginAuthDict + "napalm_device_types>=4.55.0", # LoginSessionsMixin "paramiko>=5.0.0", # CVE-2026-44405; imported directly for the node's SSH session (_base.py) "proxmoxer>=2.0.0", "netaddr>=0.9.0", diff --git a/tests/test_login_sessions.py b/tests/test_login_sessions.py new file mode 100644 index 0000000..0ac4ef3 --- /dev/null +++ b/tests/test_login_sessions.py @@ -0,0 +1,63 @@ +"""`get_login_sessions`: the interactive logins a node recorded (NetOrk/netork#2881). + +A shell on a node that did not go through the application -- someone's own ``ssh``, or +the web interface's shell -- is in the node's wtmp. The command and its parse are +napalm-device-types' (``LoginSessionsMixin``); both drivers carry the command over their +own exec path, where ``SSH_CONNECTION`` names the driver's own address. +""" + +from __future__ import annotations + +from unittest.mock import patch + +import pytest +from napalm_device_types import LoginSessionsMixin, LoginSessionsUnavailable + +from napalm_proxmox.backup_driver import ProxmoxBackupDriver +from napalm_proxmox.driver import ProxmoxDriver + +WIRE = "\n".join( + [ + "LOGSESSIONS_BEGIN", + "[own]", + "198.51.100.2", + "[now]", + "2026-11-30T12:00:00Z", + "[wtmp since]", + "root pts/0 192.0.2.10 2026-11-30T10:15:02+01:00 still logged in", + "root pts/1 0.0.0.0 2026-11-29T09:00:00+01:00 - 2026-11-29T09:30:00+01:00 (00:30)", + "LOGSESSIONS_END", + ] +) + + +@pytest.mark.parametrize("cls", [ProxmoxDriver, ProxmoxBackupDriver]) +def test_both_drivers_declare_the_contract(cls): + assert issubclass(cls, LoginSessionsMixin) + assert hasattr(cls, "get_login_sessions") + + +def test_the_node_runs_the_shared_command_over_its_exec_path(driver): + with patch.object(driver, "_exec_ssh_command", return_value=WIRE) as exec_: + reading = driver.get_login_sessions() + + exec_.assert_called_once() + assert exec_.call_args.args[0].startswith("sh -c ") + assert reading["own_address"] == "198.51.100.2" + assert [s["address"] for s in reading["sessions"]] == ["192.0.2.10", None] + + +def test_the_backup_server_reads_it_the_same_way(): + drv = ProxmoxBackupDriver("192.0.2.80", "root", "", optional_args={"token_name": "a!b"}) + + with patch.object(drv, "_exec_ssh_command", return_value=WIRE): + reading = drv.get_login_sessions() + + assert reading["sessions"][0]["user"] == "root" + + +def test_a_node_without_login_records_raises(driver): + empty = "LOGSESSIONS_BEGIN\n[own]\n\n[now]\n2026-11-30T12:00:00Z\nLOGSESSIONS_END" + with patch.object(driver, "_exec_ssh_command", return_value=empty): + with pytest.raises(LoginSessionsUnavailable): + driver.get_login_sessions()