feat(time-sync): read the time servers and zone of config.xml (NetOrk/netork#2363)
driver / standard (pull_request) Successful in 17s
driver / lint (pull_request) Successful in 14s
driver / types (pull_request) Successful in 1m1s
driver / test (3.12) (pull_request) Successful in 1m9s
driver / test (3.13) (pull_request) Successful in 1m3s
driver / lowest (pull_request) Successful in 36s
driver / build (pull_request) Successful in 27s
driver / security (pull_request) Successful in 58s
CI / driver (pull_request) Successful in 2m33s
driver / publish (pull_request) Skipped

netOrk aligns every device with one list of time servers and a zone per
site (NetOrk/netork#2360) through napalm-device-types 4.35.0's
TimeSyncMixin. OPNsense keeps both in config.xml, <system><timeservers>
and <system><timezone>, set on the legacy pages services_ntpd.php and
system_general.php. No API controller reads or writes them, on 26.7 and
on master, so get_time_sync() reads them from the configuration download
get_config already uses and asks /api/ntpd/service/status whether ntpd
follows a system peer.

The driver writes neither value: time_sync_writes is empty and
configure_time_sync is None. Restoring an edited config.xml or running
the initial setup wizard would rewrite far more than the clock, on a box
that is often the gateway and its LAN's time server. TIME_SERVERS_MENU
and TIME_ZONE_MENU say where a person sets them, as SSH_SETTINGS_MENU
does for the SSH algorithms.

get_ntp_servers() now returns the same configured list. It read a
"peers" field the status endpoint does not have (it answers with ntpq's
associations as "rows") and so never found a server.
This commit is contained in:
Christian Manivong committed 2026-11-11 15:20:29 +01:00
1 parent a6c3eb2604
commit 6fdc3ff2c7
10 files changed
+592 -64

No files matched your search

+164
View File
@@ -0,0 +1,164 @@
<?xml version="1.0"?>
<opnsense>
<trigger_initial_wizard/>
<theme>opnsense</theme>
<system>
<optimization>normal</optimization>
<hostname>OPNsense</hostname>
<domain>internal</domain>
<dnsallowoverride>1</dnsallowoverride>
<dnsallowoverride_exclude/>
<group>
<name>admins</name>
<description>System Administrators</description>
<scope>system</scope>
<gid>1999</gid>
<member>0</member>
<priv>page-all</priv>
</group>
<timezone>Etc/UTC</timezone>
<timeservers>0.opnsense.pool.ntp.org 1.opnsense.pool.ntp.org 2.opnsense.pool.ntp.org 3.opnsense.pool.ntp.org</timeservers>
<webgui>
<protocol>https</protocol>
<noroot>1</noroot>
</webgui>
<disablenatreflection>yes</disablenatreflection>
<usevirtualterminal>1</usevirtualterminal>
<disableconsolemenu/>
<ipv6allow>1</ipv6allow>
<powerd_ac_mode>hadp</powerd_ac_mode>
<powerd_battery_mode>hadp</powerd_battery_mode>
<powerd_normal_mode>hadp</powerd_normal_mode>
<bogons>
<interval>monthly</interval>
</bogons>
<pf_share_forward>1</pf_share_forward>
<lb_use_sticky>1</lb_use_sticky>
<ssh>
<group>admins</group>
</ssh>
</system>
<interfaces>
<wan>
<enable>1</enable>
<if>mismatch1</if>
<descr/>
<spoofmac/>
<ipaddr>dhcp</ipaddr>
<ipaddrv6>dhcp6</ipaddrv6>
<blockpriv>1</blockpriv>
<blockbogons>1</blockbogons>
<dhcphostname/>
<dhcp6-ia-pd-len>0</dhcp6-ia-pd-len>
</wan>
<lan>
<enable>1</enable>
<if>mismatch0</if>
<descr/>
<spoofmac/>
<ipaddr>192.168.1.1</ipaddr>
<subnet>24</subnet>
<ipaddrv6>idassoc6</ipaddrv6>
<track6-interface>wan</track6-interface>
<track6-prefix-id>0</track6-prefix-id>
</lan>
</interfaces>
<dnsmasq>
<enable>1</enable>
<port>53053</port>
<interface>lan</interface>
<dhcp>
<enable_ra>1</enable_ra>
</dhcp>
<dhcp_ranges>
<interface>lan</interface>
<start_addr>192.168.1.100</start_addr>
<end_addr>192.168.1.199</end_addr>
</dhcp_ranges>
<dhcp_ranges>
<interface>lan</interface>
<start_addr>::1000</start_addr>
<end_addr>::2000</end_addr>
<constructor>lan</constructor>
<ra_mode>slaac</ra_mode>
</dhcp_ranges>
</dnsmasq>
<unbound>
<enable>1</enable>
</unbound>
<nat>
<outbound>
<mode>automatic</mode>
</outbound>
</nat>
<filter>
</filter>
<rrd>
<enable/>
</rrd>
<ntpd>
<prefer>0.opnsense.pool.ntp.org</prefer>
<ispool>0.opnsense.pool.ntp.org 1.opnsense.pool.ntp.org 2.opnsense.pool.ntp.org 3.opnsense.pool.ntp.org</ispool>
</ntpd>
<OPNsense>
<Firewall>
<Filter>
<rules>
<rule>
<enabled>1</enabled>
<statetype>keep</statetype>
<state-policy/>
<sequence>1</sequence>
<action>pass</action>
<quick>1</quick>
<interfacenot>0</interfacenot>
<interface>lan</interface>
<direction>in</direction>
<ipprotocol>inet</ipprotocol>
<protocol>any</protocol>
<source_net>lan</source_net>
<source_not>0</source_not>
<source_port/>
<destination_net>any</destination_net>
<destination_not>0</destination_not>
<disablereplyto>0</disablereplyto>
<log>0</log>
<allowopts>0</allowopts>
<nosync>0</nosync>
<nopfsync>0</nopfsync>
<statetimeout/>
<tcpflags_any>0</tcpflags_any>
<description>Default allow LAN to any rule</description>
</rule>
<rule>
<enabled>1</enabled>
<statetype>keep</statetype>
<state-policy/>
<sequence>11</sequence>
<action>pass</action>
<quick>1</quick>
<interfacenot>0</interfacenot>
<interface>lan</interface>
<direction>in</direction>
<ipprotocol>inet6</ipprotocol>
<protocol>any</protocol>
<source_net>lan</source_net>
<source_not>0</source_not>
<destination_net>any</destination_net>
<destination_not>0</destination_not>
<disablereplyto>0</disablereplyto>
<log>0</log>
<allowopts>0</allowopts>
<nosync>0</nosync>
<nopfsync>0</nopfsync>
<tcpflags_any>0</tcpflags_any>
<description>Default allow LAN IPv6 to any rule</description>
</rule>
</rules>
<snatrules/>
<npt/>
<onetoone/>
</Filter>
</Firewall>
</OPNsense>
</opnsense>
@@ -0,0 +1,12 @@
{
"total": 5,
"rowCount": 5,
"current": 1,
"rows": [
{"status": "__pool", "server": "0.opnsense.pool.ntp.org", "refid": ".POOL.", "stratum": "16", "type": "p", "when": "-", "poll": "64", "reach": "0", "delay": "0.000", "offset": "+0.000", "jitter": "0.000"},
{"status": "__pool", "server": "1.opnsense.pool.ntp.org", "refid": ".POOL.", "stratum": "16", "type": "p", "when": "-", "poll": "64", "reach": "0", "delay": "0.000", "offset": "+0.000", "jitter": "0.000"},
{"status": "*", "server": "162.159.200.1", "refid": "10.217.8.4", "stratum": "3", "type": "u", "when": "33", "poll": "64", "reach": "377", "delay": "10.123", "offset": "-0.456", "jitter": "0.789"},
{"status": "+", "server": "194.59.205.229", "refid": "192.53.103.103", "stratum": "2", "type": "u", "when": "12", "poll": "64", "reach": "377", "delay": "12.801", "offset": "+0.214", "jitter": "0.333"},
{"status": "-", "server": "85.214.38.116", "refid": "131.188.3.222", "stratum": "2", "type": "u", "when": "41", "poll": "64", "reach": "377", "delay": "14.002", "offset": "+1.870", "jitter": "0.902"}
]
}
-38
View File
@@ -798,44 +798,6 @@ class TestGetLldpNeighborsDetail:
assert driver.get_lldp_neighbors_detail() == {}
# ---------------------------------------------------------------------------
# get_ntp_servers()
# ---------------------------------------------------------------------------
NTP_STATUS_RESPONSE = {
"peers": [
{"address": "pool.ntp.org", "state": "synced"},
{"address": "time.cloudflare.com", "state": "candidate"},
]
}
class TestGetNtpServers:
def test_entry_count(self, driver):
driver._get = lambda path: NTP_STATUS_RESPONSE
servers = driver.get_ntp_servers()
assert len(servers) == 2
def test_server_addresses(self, driver):
driver._get = lambda path: NTP_STATUS_RESPONSE
servers = driver.get_ntp_servers()
assert "pool.ntp.org" in servers
assert "time.cloudflare.com" in servers
def test_value_is_empty_dict(self, driver):
driver._get = lambda path: NTP_STATUS_RESPONSE
for v in driver.get_ntp_servers().values():
assert v == {}
def test_endpoint_failure_returns_empty(self, driver):
driver._get = lambda path: (_ for _ in ()).throw(Exception("service not running"))
assert driver.get_ntp_servers() == {}
def test_empty_peers(self, driver):
driver._get = lambda path: {"peers": []}
assert driver.get_ntp_servers() == {}
# ---------------------------------------------------------------------------
# get_vlans()
# ---------------------------------------------------------------------------
+251
View File
@@ -0,0 +1,251 @@
"""Time sync on OPNsense: the time servers and the zone of config.xml (NetOrk/netork#2363).
OPNsense keeps both in its configuration: ``<system><timeservers>``, space-separated
in the order of *Services -> Network Time -> General*, and ``<system><timezone>``, an
IANA name set on *System -> Settings -> General*. No API controller reads or writes
either, up to 26.7 and on ``master``: both pages are legacy PHP. So the driver reads
them from config.xml (``/api/core/backup/download/this``, as ``get_config`` does),
asks ntpd over ``/api/ntpd/service/status`` whether the clock follows a peer, and
writes nothing: ``time_sync_writes`` is empty and ``configure_time_sync`` is None.
``config_xml_sample.xml`` is OPNsense's own ``src/etc/config.xml.sample`` (core
``master``, 2026-11) without the default root user. ``ntpd_service_status.json`` is
the answer of ``/api/ntpd/service/status`` in its 26.7 shape: ``ntpq -pnw`` as rows,
the pool associations marked ``__pool``, the system peer ``*``.
"""
from __future__ import annotations
import json
from pathlib import Path
from unittest.mock import MagicMock, patch
from xml.etree.ElementTree import ParseError
import pytest
import requests
from napalm_device_types import TimeSyncMixin
from napalm_opnsense.opnsense import OPNsenseDriver
FIXTURES = Path(__file__).parent / "fixtures"
CONFIG_XML = (FIXTURES / "config_xml_sample.xml").read_text()
NTPD_STATUS = json.loads((FIXTURES / "ntpd_service_status.json").read_text())
CONFIG_PATH = "/api/core/backup/download/this"
STATUS_PATH = "/api/ntpd/service/status"
POOLS = [f"{n}.opnsense.pool.ntp.org" for n in range(4)]
def _configured(servers: str | None = None, zone: str | None = None) -> str:
"""The sample config.xml with other time servers or another zone ("" = element empty,
None = as the sample has it)."""
xml = CONFIG_XML
if servers is not None:
xml = xml.replace(" ".join(POOLS), servers)
if zone is not None:
xml = xml.replace("<timezone>Etc/UTC</timezone>", f"<timezone>{zone}</timezone>")
return xml
def _without(element: str, xml: str = CONFIG_XML) -> str:
start = xml.index(f"<{element}>")
end = xml.index(f"</{element}>") + len(f"</{element}>")
return xml[:start] + xml[end:]
class Firewall:
"""Answers GETs as the firewall's API would; every request is recorded."""
def __init__(self, config=CONFIG_XML, status=NTPD_STATUS, status_error=None):
self.config = config
self.status = status
self.status_error = status_error
self.paths: list[str] = []
def get(self, url, timeout=None):
path = url.removeprefix("https://fw")
self.paths.append(path)
response = MagicMock()
response.raise_for_status.return_value = None
if path == CONFIG_PATH:
if isinstance(self.config, Exception):
response.raise_for_status.side_effect = self.config
response.text = self.config
response.json.side_effect = json.JSONDecodeError("Expecting value", "<", 0)
return response
if path == STATUS_PATH:
if self.status_error is not None:
response.raise_for_status.side_effect = self.status_error
response.json.return_value = self.status
return response
raise AssertionError(f"unexpected GET {path}")
@pytest.fixture
def driver():
with patch("napalm_opnsense.opnsense.requests.Session"):
drv = OPNsenseDriver(
hostname="fw", username="k", password="s", optional_args={"verify": False}
)
drv.session = MagicMock()
yield drv
def _on(driver, firewall: Firewall) -> Firewall:
driver.session.get.side_effect = firewall.get
return firewall
class TestGetTimeSync:
def test_the_servers_and_zone_of_config_xml(self, driver):
_on(driver, Firewall())
assert driver.get_time_sync() == {
"ntp_servers": POOLS,
"timezone": "Etc/UTC",
"synchronized": True,
"daemon": "ntpd",
}
def test_the_servers_in_the_order_the_firewall_keeps_them(self, driver):
_on(driver, Firewall(config=_configured("10.0.0.1 ptbtime1.ptb.de time.cloudflare.com")))
assert driver.get_time_sync()["ntp_servers"] == [
"10.0.0.1",
"ptbtime1.ptb.de",
"time.cloudflare.com",
]
def test_the_zone_by_its_iana_name(self, driver):
_on(driver, Firewall(config=_configured(zone="Europe/Berlin")))
assert driver.get_time_sync()["timezone"] == "Europe/Berlin"
@pytest.mark.parametrize(
"config", [_configured(zone=""), _without("timezone")], ids=["empty", "missing"]
)
def test_no_zone_is_the_utc_opnsense_runs_then(self, driver, config):
# system_timezone_configure() puts Etc/UTC in place for an empty or missing zone.
_on(driver, Firewall(config=config))
assert driver.get_time_sync()["timezone"] == "Etc/UTC"
@pytest.mark.parametrize(
"config", [_configured(servers=""), _without("timeservers")], ids=["empty", "missing"]
)
def test_no_servers_is_an_unsynchronised_clock_and_ntpd_is_not_asked(self, driver, config):
# Without <timeservers> OPNsense does not run ntpd (ntpd_enabled()).
firewall = _on(driver, Firewall(config=config))
sync = driver.get_time_sync()
assert sync["ntp_servers"] == []
assert sync["synchronized"] is False
assert STATUS_PATH not in firewall.paths
def test_a_configuration_without_a_system_section(self, driver):
_on(driver, Firewall(config=_without("system")))
sync = driver.get_time_sync()
assert (sync["ntp_servers"], sync["timezone"]) == ([], "Etc/UTC")
def test_no_system_peer_is_an_unsynchronised_clock(self, driver):
rows = [
dict(row, status="+" if row["status"] == "*" else row["status"])
for row in NTPD_STATUS["rows"]
]
_on(driver, Firewall(status={**NTPD_STATUS, "rows": rows}))
assert driver.get_time_sync()["synchronized"] is False
def test_ntpd_without_peers_yet_is_an_unsynchronised_clock(self, driver):
_on(driver, Firewall(status={"total": 0, "rowCount": 0, "current": 1, "rows": []}))
assert driver.get_time_sync()["synchronized"] is False
@pytest.mark.parametrize("status", [None, [], {"rows": "x"}], ids=["null", "list", "rows"])
def test_a_status_of_another_shape_is_unknown(self, driver, status):
_on(driver, Firewall(status=status))
assert driver.get_time_sync()["synchronized"] is None
def test_a_failed_status_read_is_unknown_and_keeps_the_rest(self, driver):
_on(driver, Firewall(status_error=requests.HTTPError("403 Forbidden")))
sync = driver.get_time_sync()
assert sync["synchronized"] is None
assert sync["ntp_servers"] == POOLS
assert sync["timezone"] == "Etc/UTC"
def test_a_failed_configuration_read_raises(self, driver):
# "No servers" would be a reading; a key without the backup privilege is none.
_on(driver, Firewall(config=requests.HTTPError("403 Forbidden")))
with pytest.raises(requests.HTTPError, match="403"):
driver.get_time_sync()
def test_a_configuration_that_is_no_xml_raises(self, driver):
_on(driver, Firewall(config="<html><body>Login</body>"))
with pytest.raises(ParseError):
driver.get_time_sync()
def test_a_reading_only_reads(self, driver):
firewall = _on(driver, Firewall())
driver.get_time_sync()
assert firewall.paths == [CONFIG_PATH, STATUS_PATH]
driver.session.post.assert_not_called()
class TestGetNtpServers:
def test_the_configured_servers_as_napalm_keys_them(self, driver):
_on(driver, Firewall())
servers = driver.get_ntp_servers()
assert servers == {name: {} for name in POOLS}
assert list(servers) == POOLS
def test_the_same_servers_as_get_time_sync(self, driver):
_on(driver, Firewall(config=_configured("10.0.0.1 ptbtime1.ptb.de")))
assert list(driver.get_ntp_servers()) == driver.get_time_sync()["ntp_servers"]
def test_only_the_configuration_is_read(self, driver):
firewall = _on(driver, Firewall())
driver.get_ntp_servers()
assert firewall.paths == [CONFIG_PATH]
def test_no_servers_configured(self, driver):
_on(driver, Firewall(config=_without("timeservers")))
assert driver.get_ntp_servers() == {}
def test_a_failed_read_raises_instead_of_reading_as_no_servers(self, driver):
_on(driver, Firewall(config=requests.HTTPError("403 Forbidden")))
with pytest.raises(requests.HTTPError):
driver.get_ntp_servers()
class TestWhatTheDriverOffers:
def test_it_reads_time_sync(self):
assert issubclass(OPNsenseDriver, TimeSyncMixin)
assert callable(getattr(OPNsenseDriver, "get_time_sync", None))
def test_it_writes_nothing(self):
# Both values are set on legacy pages; no API controller writes them.
assert OPNsenseDriver.time_sync_writes == frozenset()
assert getattr(OPNsenseDriver, "configure_time_sync", None) is None
def test_where_a_person_sets_the_servers(self):
assert OPNsenseDriver.TIME_SERVERS_MENU == ("Services", "Network Time", "General")
def test_where_a_person_sets_the_zone(self):
assert OPNsenseDriver.TIME_ZONE_MENU == ("System", "Settings", "General")