fix(packages): leave removed packages with leftover config out of apt
dpkg-query -W also lists packages that were removed but whose configuration dpkg keeps (state rc), and the driver marked every line installed: old kernels and a removed agent showed up as installed software. The query now asks for db:Status-Abbrev and drops packages that are not installed or only left their configuration; held, unpacked and half-configured packages keep counting, their files are on disk. The dpkg query and its parse move to napalm_linux/_dpkg.py, a pure function under strict mypy, which keeps linux.py inside its line count. Closes #17
This commit is contained in:
1 parent
54f6844d84
commit
024f68c543
5 files changed
+108
-51
No files matched your search
+2
-46
@@ -71,6 +71,7 @@ from napalm_device_types.ssh_host_keys import HostKeyCheck, retry_refused
|
||||
from netmiko import ConnectHandler
|
||||
from netmiko.exceptions import NetmikoAuthenticationException, NetmikoTimeoutException
|
||||
|
||||
from napalm_linux._dpkg import DPKG_QUERY_COMMAND, parse_dpkg_query
|
||||
from napalm_linux._hostname import HostnameMixin
|
||||
from napalm_linux._local_accounts import LinuxLocalAccountsMixin
|
||||
from napalm_linux._shell import expect_for, prepare_shell
|
||||
@@ -1237,52 +1238,7 @@ class LinuxDriver(
|
||||
raise NotImplementedError(f"Package manager '{self._pkg_manager}' is not supported")
|
||||
|
||||
def _get_packages_apt(self) -> list[PackageDict]:
|
||||
out = self._send(
|
||||
"dpkg-query -W -f='${Package}\\t${Version}\\t${Installed-Size}"
|
||||
"\\t${source:Package}\\t${source:Version}\\t${binary:Summary}\\n' 2>/dev/null"
|
||||
)
|
||||
packages: list[PackageDict] = []
|
||||
for line in out.splitlines():
|
||||
# Summary stays last and keeps whatever it contains: maxsplit must
|
||||
# equal the number of tabs the format writes, not the field count.
|
||||
parts = line.split("\t", 5)
|
||||
if len(parts) < 2:
|
||||
continue
|
||||
name = parts[0].strip()
|
||||
version = parts[1].strip()
|
||||
size = (
|
||||
int(parts[2].strip()) * 1024 if len(parts) > 2 and parts[2].strip().isdigit() else 0
|
||||
)
|
||||
# Debian source package (e.g. openssh-server → openssh) for OSV matching.
|
||||
source_package = parts[3].strip() if len(parts) > 3 and parts[3].strip() else name
|
||||
# And its version, which is a different number from this package's.
|
||||
#
|
||||
# OSV states Debian ranges in *source* versions. A source package
|
||||
# that ships several binaries gives each its own upstream version:
|
||||
# libldb2 is 2:2.11.0+samba4.22.11+dfsg-… while its source, samba,
|
||||
# is 2:4.22.11+dfsg-…. A consumer matching on source_package and
|
||||
# comparing `version` compares two unrelated numbers — dpkg reads
|
||||
# ldb's 2.11.0 as older than the 2:4.17.4+dfsg-1 that fixed
|
||||
# CVE-2022-44640, and a host five releases past the fix was reported
|
||||
# vulnerable on four packages at once.
|
||||
#
|
||||
# dpkg leaves this empty when it equals `Version`; so does an older
|
||||
# dpkg that does not know the field at all.
|
||||
source_version = parts[4].strip() if len(parts) > 4 and parts[4].strip() else version
|
||||
description = parts[5].strip() if len(parts) > 5 else ""
|
||||
packages.append(
|
||||
{
|
||||
"name": name,
|
||||
"version": version,
|
||||
"installed": True,
|
||||
"description": description,
|
||||
"size": size,
|
||||
"source": "apt",
|
||||
"source_package": source_package,
|
||||
"source_version": source_version,
|
||||
}
|
||||
)
|
||||
return packages
|
||||
return parse_dpkg_query(self._send(DPKG_QUERY_COMMAND))
|
||||
|
||||
def _get_packages_rpm(self) -> list[PackageDict]:
|
||||
out = self._send(
|
||||
|
||||
Reference in new issue
Block a user