From 5c2bfb7dfa3521956dc9ac9afc3f533268d004e4 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Tue, 1 Dec 2026 12:45:05 +0100 Subject: [PATCH] feat(windows): clean_system_identity for values read without the DMI files, UserDict.sid clean_system_identity applies parse_system_identity's placeholder rules to values a host reads through an interface of its own (Windows' CIM classes, NetOrk/netork#2888); parse_system_identity now uses it. UserDict gains an optional sid beside the int uid (NetOrk/netork#2889). --- CHANGELOG.md | 10 +++++ README.md | 6 ++- napalm_device_types/__init__.py | 2 + napalm_device_types/models.py | 5 +++ napalm_device_types/system_identity.py | 53 +++++++++++++++++++----- tests/test_os_users.py | 24 +++++++++++ tests/test_system_identity.py | 57 ++++++++++++++++++++++++++ 7 files changed, 146 insertions(+), 11 deletions(-) create mode 100644 tests/test_os_users.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 847c85a..15cdedc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,16 @@ recorded here; their history is the git log. ## [Unreleased] +### Added +- `clean_system_identity(...)`: the SMBIOS identity and system firmware from values a host + read some other way than the DMI files -- Windows through its CIM classes + (NetOrk/netork#2888) -- with the same placeholders empty as `parse_system_identity`, + which now uses it. The release date is passed as `YYYY-MM-DD`; anything else reads as no + date. +- `UserDict.sid` (optional): the account's security identifier on a host that names + accounts by one (Windows, NetOrk/netork#2889). `uid` stays an int: there, the relative + identifier, the SID's last part. + ## [4.55.0] - 2026-12-01 ### Added diff --git a/README.md b/README.md index 8248567..67b7bf6 100644 --- a/README.md +++ b/README.md @@ -366,6 +366,9 @@ driver supplies `_run_system_identity_command(command, privileged=)`. The same round trip reads the host's system firmware (BIOS or UEFI): `bios_vendor`, `bios_version` and `bios_date` (`YYYY-MM-DD`, from sysfs's `MM/DD/YYYY`). Only the vendors' text defaults read as empty there; a short version such as `1` is a version. +A host that reads the same tables through an interface of its own (Windows' CIM classes) +implements `get_system_identity` itself and hands the values to `clean_system_identity`, +which applies the same rules. `OpenSshServerMixin` (`harden_ssh_server`, `get_ssh_server_algorithms`) takes algorithms out of what an OpenSSH server offers -- the ones ssh-audit flags. It reads the lists sshd @@ -815,7 +818,8 @@ class LinuxDriver(OSDriver): ... def get_os_users(self): - # return List[UserDict] -- not NAPALM's get_users, whose shape differs + # return List[UserDict] -- not NAPALM's get_users, whose shape differs; + # a host that names accounts by a SID (Windows) adds "sid", with the RID as uid ... def get_processes(self): diff --git a/napalm_device_types/__init__.py b/napalm_device_types/__init__.py index b6ff305..c755b9b 100644 --- a/napalm_device_types/__init__.py +++ b/napalm_device_types/__init__.py @@ -437,6 +437,7 @@ from napalm_device_types.switch import SwitchDriver from napalm_device_types.system_identity import ( SYSTEM_IDENTITY_COMMAND, SystemIdentityMixin, + clean_system_identity, is_placeholder_identity, parse_system_identity, ) @@ -754,6 +755,7 @@ __all__ = [ "service_clusters_command", "SystemIdentityMixin", "SYSTEM_IDENTITY_COMMAND", + "clean_system_identity", "is_placeholder_identity", "parse_system_identity", "OpenSshServerMixin", diff --git a/napalm_device_types/models.py b/napalm_device_types/models.py index 60f6744..ccccc30 100644 --- a/napalm_device_types/models.py +++ b/napalm_device_types/models.py @@ -915,11 +915,16 @@ class UserDict(TypedDict): """A local OS user account.""" username: str + #: POSIX's uid; on Windows the relative identifier, the last part of the SID + #: (500 for the built-in Administrator, 1000 and up for accounts created). uid: int gid: int home: str shell: str groups: list[str] # all supplementary group names + #: The account's security identifier, where the host names accounts by one + #: (Windows: "S-1-5-21-...-1001"); left out on a POSIX host. + sid: NotRequired[str] class ProcessDict(TypedDict): diff --git a/napalm_device_types/system_identity.py b/napalm_device_types/system_identity.py index a32a89c..2ef44a1 100644 --- a/napalm_device_types/system_identity.py +++ b/napalm_device_types/system_identity.py @@ -19,6 +19,10 @@ it they come back empty, which is "not known", not a mismatch. reads the identity reads it too. It is a fact about the host, not part of its identity: its placeholders are the vendors' text defaults, never a short value (a version of ``1`` is a version), and sysfs's ``MM/DD/YYYY`` release date is given as ISO 8601. + +**Other hosts** read the same SMBIOS tables through an interface of their own -- +Windows through its CIM classes (NetOrk/netork#2888) -- and hand the values to +:func:`clean_system_identity`, which applies the same placeholder rules. """ from __future__ import annotations @@ -124,6 +128,36 @@ def _iso_date(value: str) -> str: return "" +def _iso_or_empty(value: str) -> str: + """*value* when it is an ISO ``YYYY-MM-DD`` date, else no date.""" + value = value.strip() + try: + return date.fromisoformat(value).isoformat() if len(value) == 10 else "" + except ValueError: + return "" + + +def clean_system_identity( + *, + system_uuid: str, + product_serial: str, + board_serial: str, + bios_vendor: str, + bios_version: str, + bios_date: str, +) -> SystemIdentityDict: + """The identity from values a host read some other way than the DMI files, with the + same placeholders empty; *bios_date* is the release date as ``YYYY-MM-DD``.""" + return { + "system_uuid": _clean(system_uuid).lower(), + "product_serial": _clean(product_serial), + "board_serial": _clean(board_serial), + "bios_vendor": _clean_text(bios_vendor), + "bios_version": _clean_text(bios_version), + "bios_date": _iso_or_empty(bios_date), + } + + def parse_system_identity(output: str) -> SystemIdentityDict: """The identity from :data:`SYSTEM_IDENTITY_COMMAND`'s output, placeholders empty. @@ -136,16 +170,15 @@ def parse_system_identity(output: str) -> SystemIdentityDict: except ValueError: raise ValueError("system identity: no intact IDBEGIN/IDEND frame") from None fields = (lines[start:end] + [""] * len(_FILES))[: len(_FILES)] - uuid, product_serial, board_serial = (_clean(f) for f in fields[:3]) - bios_vendor, bios_version, bios_date = fields[3:] - return { - "system_uuid": uuid.lower(), - "product_serial": product_serial, - "board_serial": board_serial, - "bios_vendor": _clean_text(bios_vendor), - "bios_version": _clean_text(bios_version), - "bios_date": _iso_date(bios_date), - } + uuid, product_serial, board_serial, bios_vendor, bios_version, bios_date = fields + return clean_system_identity( + system_uuid=uuid, + product_serial=product_serial, + board_serial=board_serial, + bios_vendor=bios_vendor, + bios_version=bios_version, + bios_date=_iso_date(bios_date), + ) class SystemIdentityMixin: diff --git a/tests/test_os_users.py b/tests/test_os_users.py new file mode 100644 index 0000000..90cdd72 --- /dev/null +++ b/tests/test_os_users.py @@ -0,0 +1,24 @@ +"""``UserDict``: a local OS account, as ``get_os_users`` lists it (NetOrk/netork#2889). + +POSIX numbers each account with a uid; Windows names it with a security identifier +(SID). The uid stays an int for every driver, and a driver that has a SID gives it +beside it, so nothing that reads ``uid`` today changes. +""" + +from __future__ import annotations + +from typing import get_type_hints + +from napalm_device_types.models import UserDict + + +def test_a_posix_account_needs_no_sid(): + assert UserDict.__required_keys__ == frozenset( + {"username", "uid", "gid", "home", "shell", "groups"} + ) + + +def test_an_account_may_carry_its_sid_beside_its_uid(): + assert get_type_hints(UserDict)["sid"] is str + assert "sid" in UserDict.__optional_keys__ + assert get_type_hints(UserDict)["uid"] is int diff --git a/tests/test_system_identity.py b/tests/test_system_identity.py index 40365ca..0fdb8ec 100644 --- a/tests/test_system_identity.py +++ b/tests/test_system_identity.py @@ -7,6 +7,7 @@ import pytest from napalm_device_types import ( SYSTEM_IDENTITY_COMMAND, SystemIdentityMixin, + clean_system_identity, is_placeholder_identity, parse_system_identity, ) @@ -153,6 +154,62 @@ def test_terminal_noise_around_the_frame_is_ignored(): assert parse_system_identity("\x1b[?2004h" + _out(UUID, "S1", "B1"))["board_serial"] == "B1" +# ── values read some other way ──────────────────────────────────────────────── + + +def _values(**changes: str) -> dict[str, str]: + values = { + "system_uuid": UUID, + "product_serial": "S1", + "board_serial": "B1", + "bios_vendor": "Dell Inc.", + "bios_version": "2.19.0", + "bios_date": "2023-08-14", + } + return {**values, **changes} + + +def test_values_read_without_the_dmi_files_are_cleaned_the_same_way(): + # A host that reads its SMBIOS tables through its own interface (Windows' CIM classes) + # hands the values over one by one; the placeholders are the same vendors'. + assert clean_system_identity(**_values()) == { + "system_uuid": "31303043-534d-c40c-7a72-6f1300000000", + "product_serial": "S1", + "board_serial": "B1", + "bios_vendor": "Dell Inc.", + "bios_version": "2.19.0", + "bios_date": "2023-08-14", + } + + +@pytest.mark.parametrize( + "field", ["system_uuid", "product_serial", "board_serial"], ids=["uuid", "product", "board"] +) +@pytest.mark.parametrize( + "value", + ["To be filled by O.E.M.", "Default string", "00000000-0000-0000-0000-000000000000", " "], +) +def test_a_placeholder_identity_value_reads_as_empty(field, value): + assert clean_system_identity(**_values(**{field: value}))[field] == "" + + +def test_a_firmware_text_is_cleaned_as_the_dmi_files_are(): + identity = clean_system_identity(**_values(bios_vendor="Default string", bios_version="1")) + assert identity["bios_vendor"] == "" + assert identity["bios_version"] == "1" + + +@pytest.mark.parametrize("date", ["", "08/14/2023", "2023-13-01", "2023-08-14T00:00:00"]) +def test_a_firmware_date_must_be_an_iso_date(date): + assert clean_system_identity(**_values(bios_date=date))["bios_date"] == "" + + +def test_values_are_stripped(): + identity = clean_system_identity(**_values(board_serial=" B1 ", bios_date=" 2023-08-14 ")) + assert identity["board_serial"] == "B1" + assert identity["bios_date"] == "2023-08-14" + + # ── the mixin ─────────────────────────────────────────────────────────────────