diff --git a/napalm_linux/_readings.py b/napalm_linux/_readings.py index 9f6d1ff..36164cb 100644 --- a/napalm_linux/_readings.py +++ b/napalm_linux/_readings.py @@ -1,5 +1,6 @@ """Readings of the host's processes: listening sockets, log sources, running backups, -and the services that still run replaced code. +and the services that still run replaced code; and the file shares the host serves +and has mounted (``_shares``, NetOrk/netork#3104). The first two commands are napalm-device-types' and read-only. Root only widens what they see -- ``ss`` names every process behind a socket, ``/proc//fd`` shows every @@ -26,6 +27,7 @@ from napalm_device_types import ListeningSocketsMixin, LogSourcesMixin, Services from napalm_linux._hardening import LinuxHardeningMixin from napalm_linux._logins import LinuxLoginAccountsMixin from napalm_linux._running_backups import LinuxRunningBackupsMixin +from napalm_linux._shares import LinuxSharesMixin #: Seconds either reading may take; a busy host lists thousands of descriptors. _READ_TIMEOUT = 60 @@ -38,6 +40,7 @@ class LinuxProcessReadingsMixin( LinuxRunningBackupsMixin, LinuxHardeningMixin, LinuxLoginAccountsMixin, + LinuxSharesMixin, ): """The transports for ``get_listening_sockets``, ``get_log_sources`` and ``get_services_need_restart``, and ``get_running_backups``, ``get_hardening`` diff --git a/napalm_linux/_shares.py b/napalm_linux/_shares.py new file mode 100644 index 0000000..dd4d79c --- /dev/null +++ b/napalm_linux/_shares.py @@ -0,0 +1,38 @@ +"""The file shares the host serves and the ones it has mounted (NetOrk/netork#3104). + +Both readings are napalm-device-types': the NFS exports and Samba shares the host +serves (``PosixSharesMixin``) and the NFS exports and SMB shares it has mounted +(``NetworkMountsMixin``). Both commands are read-only and read files every user may +read, so they run on an exec channel under ``sh`` as the login user, never through sudo. +""" + +from __future__ import annotations + +from shlex import quote +from typing import TYPE_CHECKING + +from napalm_device_types import NetworkMountsMixin, PosixSharesMixin + +if TYPE_CHECKING: # pragma: no cover - for type checkers only + from napalm_device_types import CommandResult + + +class LinuxSharesMixin(PosixSharesMixin, NetworkMountsMixin): + """The transports for ``get_shares`` and ``get_network_mounts``.""" + + if TYPE_CHECKING: # pragma: no cover - declared for type checkers only + + def run_command( + self, + command: str, + *, + privileged: bool = False, + timeout: float = 60, + stdin: bytes | None = None, + ) -> CommandResult: ... + + def _run_shares_command(self, command: str) -> str: + return self.run_command(f"sh -c {quote(command)}", timeout=60).stdout + + def _run_network_mounts_command(self, command: str) -> str: + return self.run_command(f"sh -c {quote(command)}", timeout=30).stdout diff --git a/napalm_linux/linux.py b/napalm_linux/linux.py index 98f4795..7176f35 100644 --- a/napalm_linux/linux.py +++ b/napalm_linux/linux.py @@ -40,10 +40,8 @@ from napalm_device_types import ( HostStatusMixin, KernelFactsMixin, LldpNeighborDict, - NetworkMountsMixin, OpenSshServerMixin, OSDriver, - PosixSharesMixin, ServiceClustersMixin, ServicePackagesMixin, SnmpAgentMixin, @@ -277,8 +275,6 @@ class LinuxDriver( AptProxyMixin, KernelFactsMixin, ServicePackagesMixin, - PosixSharesMixin, - NetworkMountsMixin, LinuxProcessReadingsMixin, HostFirewallMixin, VirtualIpsMixin, @@ -1046,20 +1042,6 @@ class LinuxDriver( """ return self.run_command(f"sh -c {_shlex_quote(command)}", timeout=120).stdout - def _run_shares_command(self, command: str) -> str: - """The transport for ``PosixSharesMixin.get_shares``: the NFS exports and Samba - shares the host serves (NetOrk/netork#3104). - - An exec channel under ``sh``, like the service packages. The export table and - Samba's configuration are readable to everyone, so never through sudo. - """ - return self.run_command(f"sh -c {_shlex_quote(command)}", timeout=60).stdout - - def _run_network_mounts_command(self, command: str) -> str: - """The transport for ``NetworkMountsMixin.get_network_mounts``: the NFS exports - and SMB shares the host has mounted (NetOrk/netork#3104). Readable by everyone.""" - return self.run_command(f"sh -c {_shlex_quote(command)}", timeout=30).stdout - # ------------------------------------------------------------------ # HostFirewallMixin – the transport for get_host_firewall # ------------------------------------------------------------------