diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d85bc6..f0487a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,14 @@ recorded here; their history is the git log. ## [Unreleased] +### Added +- Each forwarding rule of `LogForwardingDict` names its `template`: the one the rule + gives (`@host;RSYSLOG_SyslogProtocol23Format`, `action(… template="…")`), else the + default set before it (`$ActionForwardDefaultTemplate`, or + `module(load="builtin:omfwd" template="…")` for omfwd actions), else None -- rsyslog's + own default, RFC 3164, which sends the host's local time without a time zone. A + reader can tell a rule whose timestamps a collector files shifted (NetOrk/netork#493). + ### Fixed - `apply_dhcp_reservationset` writes its changes so an address is released before it is taken: an update that moves a reservation off its address goes before the change that diff --git a/napalm_device_types/log_forwarding.py b/napalm_device_types/log_forwarding.py index 94cbb2e..a6d60c5 100644 --- a/napalm_device_types/log_forwarding.py +++ b/napalm_device_types/log_forwarding.py @@ -35,6 +35,9 @@ class ForwardRuleDict(TypedDict): selector: str # "*.*", "authpriv.*", or the condition around it complete: bool # every message of its ruleset goes ruleset: str | None # None: the default ruleset, which gets the journal and syslog + #: The template its messages are sent in; None: rsyslog's default, RFC 3164, + #: which carries the sender's local time without a zone (NetOrk/netork#493). + template: str | None class FileInputDict(TypedDict): @@ -165,6 +168,10 @@ class _Reader: self.pending: tuple[str, str | None] | None = None self.legacy_ruleset: str | None = None self.last_selector = "*.*" + #: ``$ActionForwardDefaultTemplate``, for the legacy rules after it. + self.legacy_template: str | None = None + #: ``module(load="builtin:omfwd" template=…)``, for every omfwd action. + self.module_template: str | None = None def ruleset(self) -> str | None: names = [value for kind, value in self.stack if kind == "ruleset"] @@ -175,7 +182,13 @@ class _Reader: return once or (conditions[-1] if conditions else None) def forward( - self, protocol: str, target: str, port: int | None, selector: str, once: str | None + self, + protocol: str, + target: str, + port: int | None, + selector: str, + once: str | None, + template: str | None, ) -> None: condition = self.condition(once) self.parsed["rules"].append( @@ -186,6 +199,7 @@ class _Reader: "selector": condition or selector, "complete": condition is None and selector in _EVERYTHING, "ruleset": self.ruleset(), + "template": template, } ) @@ -225,6 +239,8 @@ class _Reader: def call(self, name: str, params: dict[str, str], before: str, once: str | None) -> None: if name == "module": self.module(params.get("load", "")) + if params.get("load", "").removeprefix("builtin:") == "omfwd": + self.module_template = params.get("template") or self.module_template elif name == "ruleset": self.pending = ("ruleset", params.get("name")) elif name == "input" and params.get("type") == "imfile" and params.get("file"): @@ -240,7 +256,10 @@ class _Reader: kind = params.get("type", "") if kind in _FORWARD_TYPES and params.get("target"): protocol = "relp" if kind == "omrelp" else params.get("protocol", "udp").lower() - self.forward(protocol, params["target"], _port(params.get("port")), selector, once) + template = params.get("template") or (self.module_template if kind == "omfwd" else None) + self.forward( + protocol, params["target"], _port(params.get("port")), selector, once, template + ) def module(self, name: str) -> None: if name == "imjournal": @@ -257,6 +276,8 @@ class _Reader: self.parsed["file_inputs"].append({"path": value, "ruleset": self.legacy_ruleset}) elif name == "$ruleset": self.legacy_ruleset = None if value == "RSYSLOG_DefaultRuleset" else value + elif name == "$actionforwarddefaulttemplate": + self.legacy_template = value or None def legacy(self, text: str, once: str | None) -> None: match = _PROPERTY_RE.match(text) or _LEGACY_RE.match(text) @@ -264,13 +285,27 @@ class _Reader: return selector = self.last_selector if match["sel"] == "&" else match["sel"] self.last_selector = selector - forward = _FORWARD_RE.match(match["action"].strip()) + action = match["action"].strip() + forward = _FORWARD_RE.match(action) if forward: protocol = {"@": "udp", "@@": "tcp"}.get(forward["at"] or "", "relp") self.forward( - protocol, forward["v6"] or forward["host"], _port(forward["port"]), selector, once + protocol, + forward["v6"] or forward["host"], + _port(forward["port"]), + selector, + once, + self.legacy_template_of(action[forward.end() :], protocol), ) + def legacy_template_of(self, rest: str, protocol: str) -> str | None: + """The template after a legacy rule's target (``;Name``), else the default + set before it; RELP takes no forwarding default.""" + _, semicolon, named = rest.partition(";") + if semicolon and named.strip(): + return named.strip() + return self.legacy_template if protocol != "relp" else None + def parse_rsyslog(text: str) -> RsyslogDict: """rsyslog's configuration -- ``rsyslog.conf`` and ``rsyslog.d`` as one text -- diff --git a/tests/test_log_forwarding.py b/tests/test_log_forwarding.py index 0dc449c..3a66336 100644 --- a/tests/test_log_forwarding.py +++ b/tests/test_log_forwarding.py @@ -37,9 +37,40 @@ class TestRules: "selector": "*.*", "complete": True, "ruleset": None, + "template": "RSYSLOG_SyslogProtocol23Format", }, ] + def test_a_rule_without_a_template_sends_rsyslogs_default(self): + """None: RFC 3164, the sender's local time without a zone (NetOrk/netork#493).""" + (rule,) = _rules("*.* @graylog.example.net:514\n") + assert rule["template"] is None + + def test_an_actions_template_parameter(self): + (rule,) = _rules( + 'action(type="omfwd" target="10.0.0.7" Template="RSYSLOG_ForwardFormat")\n' + ) + assert rule["template"] == "RSYSLOG_ForwardFormat" + + def test_the_legacy_default_template_holds_for_the_rules_after_it(self): + rules = _rules( + "*.* @a\n$ActionForwardDefaultTemplate RSYSLOG_SyslogProtocol23Format\n" + "*.* @@b:514\n*.* @c;RSYSLOG_TraditionalForwardFormat\n" + ) + assert [(r["target"], r["template"]) for r in rules] == [ + ("a", None), + ("b", "RSYSLOG_SyslogProtocol23Format"), + ("c", "RSYSLOG_TraditionalForwardFormat"), + ] + + def test_the_forward_modules_template_holds_for_its_actions(self): + text = ( + 'module(load="builtin:omfwd" template="RSYSLOG_SyslogProtocol23Format")\n' + 'action(type="omfwd" target="10.0.0.7")\n' + ) + (rule,) = _rules(text) + assert rule["template"] == "RSYSLOG_SyslogProtocol23Format" + def test_two_ats_are_tcp_and_a_missing_port_stays_unknown(self): (rule,) = _rules("*.* @@logs.lan\n") assert (rule["protocol"], rule["target"], rule["port"]) == ("tcp", "logs.lan", None)