diff --git a/CHANGELOG.md b/CHANGELOG.md index 659a217..d5b30c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ recorded here; their history is the git log. ## [Unreleased] -## [4.50.0] - 2026-11-28 +## [4.51.0] - 2026-11-28 ### Added - `LoginAccountsMixin.get_login_accounts()`: every account on a Linux host a person can @@ -28,6 +28,21 @@ recorded here; their history is the git log. - `parse_login_accounts`, `LOGIN_ACCOUNTS_COMMAND`, `login_capable`, and the `LoginAccountDict` and `LoginAccountsDict` shapes (in `login_accounts`). +## [4.50.0] - 2026-11-28 + +### Added +- `login_sources`: where a device checks a person's login, and what each place asks for + (NetOrk/netork#662). `LoginSourceDict` names one source with the device's own word for its + kind and what it asks for (`LoginFactors`: `password`, `password_otp`, `external`, + `unknown`; all four in `LOGIN_FACTORS`). `WebLoginAuthDict` is the reading a driver returns + from `get_web_login_auth`: every source the web interface tries, in the device's order. + There is no mixin, so `hasattr(driver, "get_web_login_auth")` stays a truthful answer. +- `SshServerAuthDict` takes two optional keys for a platform that writes sshd_config itself + and is read from its own configuration. `password_sources` says what checks a password or + keyboard-interactive login where the platform decides that rather than PAM's stack. + `server_enabled` is False when the platform's SSH server is switched off. Readings from + `sshd -T` leave both out. + ## [4.49.0] - 2026-11-28 ### Added diff --git a/README.md b/README.md index e934ff0..4d3b8d7 100644 --- a/README.md +++ b/README.md @@ -364,6 +364,15 @@ login user and followed through `@include`, `include` and `substack`. Which of t second factor is the consumer's call. `Match` blocks are not read. `OpenSshServerMixin` carries it; a driver can also mix it in alone and supply `run_command`. +`login_sources` describes where a device checks a person's login, for platforms that try a +login against a list of authentication servers and let in whatever the first one accepts +(OPNsense). A driver that can read its web interface's list implements +`get_web_login_auth() -> WebLoginAuthDict`: each source with its name, the device's word for +its kind, and what it asks for (`password`, `password_otp`, `external` or `unknown`). Such a +platform's `get_ssh_server_auth` may add `password_sources` (what checks an SSH password) +and `server_enabled` (False when SSH is off) to `SshServerAuthDict`. Which source counts as +a second factor is the consumer's call. + `SudoHistoryMixin` (`get_history_sudo_lines`, `remove_history_sudo_lines`) deals with what a driver left behind when it typed `echo '' | sudo -S -p "" …` into an interactive shell: that line, password and all, in the login user's shell history, and files its partial diff --git a/napalm_device_types/__init__.py b/napalm_device_types/__init__.py index 623f967..48b7a53 100644 --- a/napalm_device_types/__init__.py +++ b/napalm_device_types/__init__.py @@ -236,6 +236,12 @@ from napalm_device_types.login_accounts import ( login_capable, parse_login_accounts, ) +from napalm_device_types.login_sources import ( + LOGIN_FACTORS, + LoginFactors, + LoginSourceDict, + WebLoginAuthDict, +) from napalm_device_types.mac_acl import MacAclMixin from napalm_device_types.management_controller import ManagementControllerDriver from napalm_device_types.media import MediaDriver @@ -672,6 +678,10 @@ __all__ = [ "parse_system_identity", "OpenSshServerMixin", "PamAuthEntryDict", + "LOGIN_FACTORS", + "LoginFactors", + "LoginSourceDict", + "WebLoginAuthDict", "SshServerAuthDict", "SshServerAuthMixin", "SSHD_EFFECTIVE_COMMAND", diff --git a/napalm_device_types/login_sources.py b/napalm_device_types/login_sources.py new file mode 100644 index 0000000..1f47119 --- /dev/null +++ b/napalm_device_types/login_sources.py @@ -0,0 +1,59 @@ +"""Where a device checks a person's login, and what each place asks for. + +Some platforms keep their own list of authentication servers and try a login +against each in turn, and the first one that accepts it lets the person in. On +OPNsense that list (*System → Settings → Administration → Authentication*) serves +the web interface, the console and sshd's password login alike. Whether a password +alone gets in then depends on every server in the list, not on one switch. So the +driver names each place it tries, in the device's own order, with what that place +asks for. Which of them counts as a second factor for a policy is the consumer's +call (NetOrk/netork#662). + +Two readings carry these sources: + +- ``get_web_login_auth`` returns :class:`WebLoginAuthDict`: how the device's web + interface lets people log in. A driver that cannot tell does not have the method, + so ``hasattr`` stays a truthful answer. +- :class:`~napalm_device_types.ssh_server_auth.SshServerAuthDict` takes + ``password_sources`` where the platform, not PAM's stack, decides what checks a + password over SSH. + +What a source asks for (:data:`LOGIN_FACTORS`): + +- ``password``: a password alone, as with local accounts or an LDAP bind. +- ``password_otp``: a password and a one-time code together. Without a code + there is no login. +- ``external``: another system decides, for example a RADIUS server. How many + factors it asks for cannot be seen from the device. +- ``unknown``: a kind of source the driver does not know. +""" + +from __future__ import annotations + +from typing import Literal, TypedDict + +LoginFactors = Literal["password", "password_otp", "external", "unknown"] + +#: Every value of :data:`LoginFactors`, in the order of the module docstring. +LOGIN_FACTORS: tuple[LoginFactors, ...] = ("password", "password_otp", "external", "unknown") + + +class LoginSourceDict(TypedDict): + """One place a login is checked. + + ``{"name": "TOTP", "kind": "totp", "factors": "password_otp"}`` + """ + + #: The source's name as the device shows it. + name: str + #: The device's own word for the kind, as written: ``local``, ``totp``, ``radius``. + kind: str + factors: LoginFactors + + +class WebLoginAuthDict(TypedDict): + """How a device's web interface lets people log in, as ``get_web_login_auth`` read it.""" + + #: Every source a login is tried against, in the device's order. A login that + #: any one of them accepts gets in. + sources: list[LoginSourceDict] diff --git a/napalm_device_types/ssh_server_auth.py b/napalm_device_types/ssh_server_auth.py index 302b469..b65df61 100644 --- a/napalm_device_types/ssh_server_auth.py +++ b/napalm_device_types/ssh_server_auth.py @@ -30,6 +30,8 @@ import re from collections.abc import Mapping from typing import TYPE_CHECKING, TypedDict +from napalm_device_types.login_sources import LoginSourceDict + if TYPE_CHECKING: # pragma: no cover - for type checkers only from napalm_device_types.channel import CommandResult @@ -62,7 +64,18 @@ class PamAuthEntryDict(TypedDict): args: list[str] -class SshServerAuthDict(TypedDict): +class _PlatformSshAuthDict(TypedDict, total=False): + """The keys of :class:`SshServerAuthDict` only a platform that writes sshd_config sets. + + A base of their own, as ``total=False``: ``NotRequired`` under postponed + annotations is not seen by the class at runtime. + """ + + password_sources: list[LoginSourceDict] + server_enabled: bool + + +class SshServerAuthDict(_PlatformSshAuthDict): """How an OpenSSH server lets people log in, as ``get_ssh_server_auth`` read it. The global settings ``sshd -T`` prints. A ``Match`` block can change them for @@ -77,6 +90,16 @@ class SshServerAuthDict(TypedDict): ``use_pam`` is None where sshd has no PAM (OpenBSD). ``pam_auth_stack`` is the ``auth`` stack PAM runs for sshd, includes and substacks followed; None when PAM is off or its files gave nothing for sshd. + + Two keys only a platform that writes sshd_config itself sets, read from its + own configuration rather than from a running sshd: + + - ``password_sources``: what checks a password or keyboard-interactive login, + where the platform decides that rather than PAM's stack. OPNsense's PAM + module tries its authentication servers. Absent where the stack says it. + - ``server_enabled``: False when the platform's SSH server is switched off. + Nobody then logs in over SSH, whatever the other keys say. Absent where the + reading came from a running sshd. """ password_authentication: bool diff --git a/pyproject.toml b/pyproject.toml index b468a0d..cecf56d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "napalm-device-types" -version = "4.50.0" +version = "4.51.0" description = "Abstract device-type base classes for NAPALM drivers" readme = "README.md" requires-python = ">=3.12" diff --git a/tests/test_login_sources.py b/tests/test_login_sources.py new file mode 100644 index 0000000..55f451a --- /dev/null +++ b/tests/test_login_sources.py @@ -0,0 +1,45 @@ +"""Where a device checks a login, and what each place asks for (NetOrk/netork#662). + +The module is a contract, not a reader: the shapes a driver returns from +``get_web_login_auth`` and, where its platform says it, in ``get_ssh_server_auth``. +These tests pin the vocabulary consumers match on. +""" + +from __future__ import annotations + +from typing import get_args, get_type_hints + +import napalm_device_types +from napalm_device_types.login_sources import ( + LOGIN_FACTORS, + LoginFactors, + LoginSourceDict, + WebLoginAuthDict, +) +from napalm_device_types.ssh_server_auth import SshServerAuthDict + + +def test_the_factor_names_are_the_literal_s(): + assert LOGIN_FACTORS == get_args(LoginFactors) + assert LOGIN_FACTORS == ("password", "password_otp", "external", "unknown") + + +def test_a_source_names_itself_its_kind_and_what_it_asks_for(): + assert set(get_type_hints(LoginSourceDict)) == {"name", "kind", "factors"} + + +def test_a_web_reading_is_its_sources_in_the_device_s_order(): + assert set(get_type_hints(WebLoginAuthDict)) == {"sources"} + + +def test_an_ssh_reading_may_say_where_a_password_is_checked_and_that_sshd_is_off(): + optional = SshServerAuthDict.__optional_keys__ + assert {"password_sources", "server_enabled"} <= optional + # The keys every OpenSSH reading carries stay required. + assert "password_authentication" in SshServerAuthDict.__required_keys__ + + +def test_the_package_exports_the_shapes(): + for name in ("LOGIN_FACTORS", "LoginFactors", "LoginSourceDict", "WebLoginAuthDict"): + assert name in napalm_device_types.__all__ + assert hasattr(napalm_device_types, name)