From 847c9021937fb5bb3133207e75be77662e146278 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Tue, 3 Nov 2026 07:56:46 +0100 Subject: [PATCH 1/2] feat(ssh): SSHD_CONFIG_GENERATED keeps the hardening to the drop-in (#88) A platform that writes sshd_config from its own database (OpenMediaVault) had to opt out of harden_ssh_server altogether. Where that generated file includes /etc/ssh/sshd_config.d, the drop-in is safe; only the fallback that rewrites sshd_config itself would be undone on the next apply. With SSHD_CONFIG_GENERATED set, a sshd_config without the include is left alone and the result says why. --- CHANGELOG.md | 7 +++++++ napalm_device_types/ssh_server.py | 20 ++++++++++++++++++-- tests/test_ssh_server.py | 27 +++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2f6e25..98a7a3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ recorded here; their history is the git log. ## [Unreleased] +### Added +- `OpenSshServerMixin.SSHD_CONFIG_GENERATED`: a driver whose platform writes sshd_config + from its own database, but includes `/etc/ssh/sshd_config.d` (OpenMediaVault), sets it + and keeps `harden_ssh_server`. The hardening then goes only into the drop-in; a + sshd_config without that include is left alone and the result says why, since the + platform would write over a change to it on its next apply (#88, NetOrk/netork#1950). + ## [4.25.0] - 2026-11-02 ### Added diff --git a/napalm_device_types/ssh_server.py b/napalm_device_types/ssh_server.py index 6c750bf..a7e10b4 100644 --- a/napalm_device_types/ssh_server.py +++ b/napalm_device_types/ssh_server.py @@ -15,7 +15,9 @@ accident. ``/etc/ssh/sshd_config.d/*.conf`` -- Debian and Ubuntu do, at the top -- the lists go to :data:`DROPIN_PATH`, which sorts first and so wins. sshd_config stays as the distribution shipped it, and removing the file undoes the change. -Only without such an ``Include`` is sshd_config itself rewritten, after a copy. +Only without such an ``Include`` is sshd_config itself rewritten, after a copy -- +and not at all on a platform that generates it +(:attr:`OpenSshServerMixin.SSHD_CONFIG_GENERATED`). **Nobody is locked out, by three checks.** @@ -426,9 +428,16 @@ class OpenSshServerMixin: whose platform writes sshd_config itself (an appliance that regenerates it from its own database) must not inherit this, or must opt out with ``harden_ssh_server = None``: its file would be overwritten on the next - apply. + apply. Where that generated file still includes the drop-in directory, the + driver sets :attr:`SSHD_CONFIG_GENERATED` instead and keeps the hardening. """ + #: True where the platform writes sshd_config from its own database + #: (OpenMediaVault): only the drop-in directory is written, and a + #: sshd_config that does not include it is left alone, since the platform + #: would write over a change to it on its next apply. + SSHD_CONFIG_GENERATED: bool = False + if TYPE_CHECKING: # pragma: no cover - declared for type checkers only def run_command( @@ -565,6 +574,13 @@ class OpenSshServerMixin: main = self._ssh_read(MAIN_CONFIG) if main is None: raise RuntimeError(f"cannot read {MAIN_CONFIG}") + if self.SSHD_CONFIG_GENERATED and not has_dropin_include(main): + result.message = ( + f"Not changed: {MAIN_CONFIG} does not include {DROPIN_DIR}, and this" + " platform writes it from its own settings." + ) + say(result.message) + return result restore = self._write_change(plan, main, result, say) return self._apply_or_restore( result, diff --git a/tests/test_ssh_server.py b/tests/test_ssh_server.py index ad1db25..869a899 100644 --- a/tests/test_ssh_server.py +++ b/tests/test_ssh_server.py @@ -14,6 +14,7 @@ import pytest from napalm_device_types.channel import CommandResult from napalm_device_types.ssh_server import ( + DROPIN_DIR, DROPIN_PATH, MAIN_CONFIG, OpenSshServerMixin, @@ -401,6 +402,32 @@ def test_mixin_falls_back_to_sshd_config_without_an_include(): assert host._effective()["mac"] == ["hmac-sha2-256"] +class GeneratedConfigHost(FakeHost): + """A platform that writes sshd_config from its own database (OpenMediaVault).""" + + SSHD_CONFIG_GENERATED = True + + +def test_generated_sshd_config_still_gets_the_dropin(): + host = GeneratedConfigHost() + result = host.harden_ssh_server({"mac": ["hmac-sha1"]}) + assert result.applied + assert result.path == DROPIN_PATH + assert host.files[MAIN_CONFIG] == "Include /etc/ssh/sshd_config.d/*.conf\nPort 22\n" + + +def test_generated_sshd_config_without_an_include_is_left_alone(): + """The platform would write its own file over the change on its next apply.""" + host = GeneratedConfigHost(main="Port 22\nMACs hmac-sha1,hmac-sha2-256\n") + result = host.harden_ssh_server({"mac": ["hmac-sha1"]}) + assert not result.applied and not result.rolled_back + assert result.path is None + assert host.files[MAIN_CONFIG] == "Port 22\nMACs hmac-sha1,hmac-sha2-256\n" + assert list(host.files) == [MAIN_CONFIG] + assert host.reloads == 0 + assert DROPIN_DIR in result.message + + def test_mixin_restores_when_sshd_rejects_the_config(): host = FakeHost() host.syntax_ok = False From 25f28297e4292196afd474733d7662e4fdd06152 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Tue, 3 Nov 2026 07:56:46 +0100 Subject: [PATCH 2/2] chore(release): 4.26.0 --- CHANGELOG.md | 2 ++ pyproject.toml | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 98a7a3a..938a7c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ recorded here; their history is the git log. ## [Unreleased] +## [4.26.0] - 2026-11-03 + ### Added - `OpenSshServerMixin.SSHD_CONFIG_GENERATED`: a driver whose platform writes sshd_config from its own database, but includes `/etc/ssh/sshd_config.d` (OpenMediaVault), sets it diff --git a/pyproject.toml b/pyproject.toml index f5d3ab1..9c7a9ce 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "napalm-device-types" -version = "4.25.0" +version = "4.26.0" description = "Abstract device-type base classes for NAPALM drivers" readme = "README.md" requires-python = ">=3.12"