From b50732c63cb3152d57b6737873a697599f7128cf Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sat, 7 Nov 2026 16:39:12 +0100 Subject: [PATCH] feat(ssh): say where the SSH algorithm lists are set OPNsense writes sshd_config from config.xml (), set on the legacy Administration page; no API controller reads or writes that section (26.1, 26.7, master). The driver cannot harden the SSH server, so it declares, as data, the page and the field of each algorithm list: SSH_SETTINGS_MENU and SSH_SETTINGS_FIELDS, keyed like device-types' SSH_ALGORITHM_CLASSES. An application tells a person what to set there. The CARP master check moves to virtual_ips, next to the CARP reader, so opnsense.py stays under its line ratchet. Refs NetOrk/netork#1769 --- CHANGELOG.md | 5 +++++ README.md | 13 ++++++++++++ napalm_opnsense/opnsense.py | 15 ++++++-------- napalm_opnsense/ssh_settings.py | 32 +++++++++++++++++++++++++++++ napalm_opnsense/virtual_ips.py | 5 +++++ tests/quality_baseline.json | 2 +- tests/unit/test_ssh_settings.py | 36 +++++++++++++++++++++++++++++++++ 7 files changed, 98 insertions(+), 10 deletions(-) create mode 100644 napalm_opnsense/ssh_settings.py create mode 100644 tests/unit/test_ssh_settings.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 604fb20..e35e4dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ All notable changes to this driver. The format follows ## [Unreleased] ### Added +- Where the SSH server's algorithm lists are set (NetOrk/netork#1769): + `SSH_SETTINGS_MENU` (*System → Settings → Administration → Secure Shell*) and + `SSH_SETTINGS_FIELDS`, each list's field by its algorithm class. OPNsense writes + sshd_config from config.xml and has no API for that section, so the driver does not + harden the SSH server itself; an application tells a person what to set there. - Scheduled jobs (NetOrk/netork#1933, #43): `get_scheduled_jobs()` reads every cron job of config.xml over `/api/cron/settings/get`, with who owns it -- another component's job is `platform`, one written by `put_scheduled_job()` `application`, the rest diff --git a/README.md b/README.md index 6ccb0e3..81dbc47 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,19 @@ what OPNsense writes into the crontab beyond config.xml (`periodic`, the plugins' system jobs) cannot be read over the API; the time zone is not read (`None`). +### SSH server settings + +OPNsense writes sshd_config from config.xml (``: `kex`, `ciphers`, +`macs`, `keys`, `keysig`), set on the legacy page *System → Settings → +Administration → Secure Shell* (`system_advanced_admin.php`). No API controller +reads or writes that section, up to OPNsense 26.7 and on `master`, so the driver +does not offer `harden_ssh_server`. It says, as data, where a person sets the +algorithm lists instead: `SSH_SETTINGS_MENU` is the page's menu path and +`SSH_SETTINGS_FIELDS` names each list's field by its algorithm class +(napalm-device-types' `SSH_ALGORITHM_CLASSES`). A field left empty means +OpenSSH's defaults. In an HA pair the configuration sync carries the section to +the backup when its "OpenSSH" item (`ssh`) is ticked. + ## Config management OPNsense does not expose a single generic "push config" endpoint. Config is diff --git a/napalm_opnsense/opnsense.py b/napalm_opnsense/opnsense.py index 19ee39d..e1be239 100644 --- a/napalm_opnsense/opnsense.py +++ b/napalm_opnsense/opnsense.py @@ -75,7 +75,8 @@ from napalm_opnsense.ping_mixin import OPNsensePingMixin from napalm_opnsense.port_forwards import alias_index, port_forwards, wan_interfaces from napalm_opnsense.resolver_mixin import OPNsenseResolverMixin from napalm_opnsense.scheduled_jobs import OPNsenseScheduledJobsMixin -from napalm_opnsense.virtual_ips import carp_addresses +from napalm_opnsense.ssh_settings import OPNsenseSshSettingsMixin +from napalm_opnsense.virtual_ips import carp_addresses, carp_master from napalm_opnsense.wireguard import wireguard_tunnels logger = logging.getLogger(__name__) @@ -96,13 +97,9 @@ def _opnsense_version(versions: Any) -> str: return "" -def _carp_master(entry: dict[str, Any]) -> bool: - """Whether this node is MASTER for an interface address bound to a CARP VHID.""" - return str(entry.get("status") or "").strip().upper() == "MASTER" - - class OPNsenseDriver( OPNsenseScheduledJobsMixin, + OPNsenseSshSettingsMixin, OPNsensePingMixin, ConfigSyncMixin, OPNsenseResolverMixin, @@ -391,11 +388,11 @@ class OPNsenseDriver( _add(name, addr6, "ipv6") for family in ("ipv4", "ipv6"): for entry in iface.get(family) or []: - if isinstance(entry, dict) and entry.get("vhid") and _carp_master(entry): + if isinstance(entry, dict) and entry.get("vhid") and carp_master(entry): _add(name, entry.get("ipaddr") or "", family) if not addr4: for entry in iface.get("ipv4") or []: - if entry.get("vhid") and not _carp_master(entry): + if entry.get("vhid") and not carp_master(entry): continue ip_field = entry.get("ipaddr") or entry.get("ip", "") subnet = entry.get("subnetbits") or entry.get("prefix_length") @@ -404,7 +401,7 @@ class OPNsenseDriver( _add(name, cidr, "ipv4") if not addr6: for entry in iface.get("ipv6") or []: - if entry.get("vhid") and not _carp_master(entry): + if entry.get("vhid") and not carp_master(entry): continue ip_field = entry.get("ipaddr") or entry.get("ip", "") prefix = entry.get("prefixlen") or entry.get("prefix_length") diff --git a/napalm_opnsense/ssh_settings.py b/napalm_opnsense/ssh_settings.py new file mode 100644 index 0000000..1519179 --- /dev/null +++ b/napalm_opnsense/ssh_settings.py @@ -0,0 +1,32 @@ +"""Where the firewall's SSH server takes its algorithm lists from (NetOrk/netork#1769). + +OPNsense writes sshd_config itself, from config.xml (````: ``kex``, +``ciphers``, ``macs``, ``keys`` and ``keysig``, comma-separated), whenever the +service is configured (``openssh_configure_do`` in ``plugins.inc.d/openssh.inc``). +The section is set on the legacy page ``system_advanced_admin.php``; no API +controller reads or writes it, up to 26.7 and on ``master``. So this driver cannot +take algorithms out of what sshd offers -- it does not offer ``harden_ssh_server`` +-- and says, as data, where a person sets the lists instead. + +A field left empty is OpenSSH's defaults. In an HA pair the configuration sync +carries the section to the backup when its "OpenSSH" item is ticked (sync id +``ssh``, :data:`napalm_opnsense.ha.SYNC_ITEM_SERVICES`). +""" + +from __future__ import annotations + + +class OPNsenseSshSettingsMixin: + """The page and fields that set the SSH server's algorithm lists, as the web + interface names them.""" + + #: The menu path to the page, ending in the section that holds the lists. + SSH_SETTINGS_MENU: tuple[str, ...] = ("System", "Settings", "Administration", "Secure Shell") + #: Each list's field, by its algorithm class (napalm-device-types' + #: ``SSH_ALGORITHM_CLASSES``). + SSH_SETTINGS_FIELDS: dict[str, str] = { + "kex": "Key exchange algorithms", + "cipher": "Ciphers", + "mac": "MACs", + "hostkey": "Host key algorithms", + } diff --git a/napalm_opnsense/virtual_ips.py b/napalm_opnsense/virtual_ips.py index 79402b7..6c1eefd 100644 --- a/napalm_opnsense/virtual_ips.py +++ b/napalm_opnsense/virtual_ips.py @@ -31,6 +31,11 @@ _CARP_STATES: dict[str, tuple[bool, bool]] = { } +def carp_master(entry: dict[str, Any]) -> bool: + """Whether this node is MASTER for an interface address bound to a CARP VHID.""" + return str(entry.get("status") or "").strip().upper() == "MASTER" + + def _address(text: str) -> tuple[str, int | None] | None: """``"10.0.0.1/24"`` -> ``("10.0.0.1", 24)``; None for what is no address.""" host, slash, prefix = text.partition("/") diff --git a/tests/quality_baseline.json b/tests/quality_baseline.json index 4edd572..e9dfd78 100644 --- a/tests/quality_baseline.json +++ b/tests/quality_baseline.json @@ -55,7 +55,7 @@ "napalm_opnsense.resolver_mixin" ], "python_file_lines": { - "napalm_opnsense/opnsense.py": 3058 + "napalm_opnsense/opnsense.py": 3055 }, "untyped_public": [ "napalm_opnsense/opnsense.py::OPNsenseDriver.get_snmp_config" diff --git a/tests/unit/test_ssh_settings.py b/tests/unit/test_ssh_settings.py new file mode 100644 index 0000000..b16231a --- /dev/null +++ b/tests/unit/test_ssh_settings.py @@ -0,0 +1,36 @@ +"""Where the SSH server's algorithm lists are set (NetOrk/netork#1769). + +OPNsense writes sshd_config from config.xml (````), and no API +controller reads or writes that section: the lists are set on the legacy +Administration page. The driver therefore does not harden the SSH server; it +says, as data, where a person sets the lists by hand. +""" + +from __future__ import annotations + +from napalm_device_types.ssh_server import SSH_ALGORITHM_CLASSES + +from napalm_opnsense import OPNsenseDriver + + +def test_the_page_that_sets_the_lists(): + assert OPNsenseDriver.SSH_SETTINGS_MENU == ( + "System", + "Settings", + "Administration", + "Secure Shell", + ) + + +def test_a_field_for_every_algorithm_class(): + assert OPNsenseDriver.SSH_SETTINGS_FIELDS == { + "kex": "Key exchange algorithms", + "cipher": "Ciphers", + "mac": "MACs", + "hostkey": "Host key algorithms", + } + assert set(OPNsenseDriver.SSH_SETTINGS_FIELDS) == set(SSH_ALGORITHM_CLASSES) + + +def test_the_driver_does_not_harden_the_ssh_server_itself(): + assert not callable(getattr(OPNsenseDriver, "harden_ssh_server", None))