Skip to content

Commit f76b8ee

Browse files
committed
fix(forward-email): reject unknown keys in the forward_email block and its defaults
A misspelled `defualts:` (or `defaults: {setings: ...}`) was silently ignored, so the package defaults were enforced instead of the operator's override — e.g. a declared `retention_days: 0` became a PUT of 30. The per-domain file already rejected unknown top-level keys; the config block and its `defaults:` sub-block now do the same, through the existing `_check_keys`. Also documents the CLI exit codes in the module docstring (0 clean, 1 any per-domain error or finding, 2 an invalid invocation); the split was only implicit in the tests. Round-2 Codex lens on #1, full-scope confirm: one blocking finding fixed (this), one rejected (per-domain config errors keep rc 1 — nothing documents rc 2 for them and the suite asserts 1).
1 parent d473257 commit f76b8ee

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎src/octodns_gitops/cli/forward_email.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
plan / apply diff desired state against the account (default)
1111
--export write `<directory>/<domain>.yaml` from live state (bootstrap / re-baseline)
1212
--drift compare FE's generated DNS records and read-only expectations with the repo
13+
14+
Exit codes: 0 nothing wrong; 1 a finding, a refused prune, or any per-domain error (a bad
15+
per-domain file included — the other domains still run); 2 the invocation itself is invalid
16+
(the `forward_email:` block, a missing token, an unclaimed `--domain`).
1317
"""
1418

1519
from __future__ import annotations

‎src/octodns_gitops/forward_email/config.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@
159159
DEFAULT_DIRECTORY = "mail/forward-email"
160160
DEFAULT_TOKEN_ENV = "FORWARD_EMAIL_API_TOKEN"
161161

162+
# Keys of the `forward_email:` block and of its `defaults:` sub-block. Checked like every other
163+
# mapping here: an unknown key is an error, never something silently ignored.
164+
BLOCK_FIELDS: frozenset[str] = frozenset({"token", "directory", "domains", "defaults"})
165+
DEFAULTS_FIELDS: frozenset[str] = frozenset({"settings", "expect", "alias"})
166+
162167

163168
class ForwardEmailConfigError(ValueError):
164169
"""Raised when the block or a domain file is invalid."""
@@ -273,6 +278,8 @@ def load_forward_email(config_path: str) -> ForwardEmailConfig | None:
273278
raise ForwardEmailConfigError(
274279
"forward_email: expected a non-empty mapping (remove the key entirely to opt out)"
275280
)
281+
# A misspelled key (`defualts:`) must not silently fall back to the package defaults.
282+
_check_keys("forward_email", block, BLOCK_FIELDS)
276283

277284
token = block.get("token", f"env/{DEFAULT_TOKEN_ENV}")
278285
if not isinstance(token, str) or not token.startswith("env/") or len(token) <= 4:
@@ -299,6 +306,7 @@ def load_forward_email(config_path: str) -> ForwardEmailConfig | None:
299306
domains.append(n)
300307

301308
defaults = _section("forward_email.defaults", block, "defaults")
309+
_check_keys("forward_email.defaults", defaults, DEFAULTS_FIELDS)
302310
settings_over = _section("forward_email.defaults.settings", defaults, "settings")
303311
expect_over = _section("forward_email.defaults.expect", defaults, "expect")
304312
alias_over = _section("forward_email.defaults.alias", defaults, "alias")

‎tests/forward_email/test_config.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,21 @@ def test_unknown_setting_in_defaults_rejected(self, tmp_path):
133133
with pytest.raises(ForwardEmailConfigError, match="has_strict_dmarc"):
134134
load_forward_email(str(cfg))
135135

136+
@pytest.mark.parametrize(
137+
"block, needle",
138+
[
139+
("defualts:\n settings:\n retention_days: 0\n", "defualts"),
140+
("defaults:\n setings:\n retention_days: 0\n", "setings"),
141+
("tokn: env/X\n", "tokn"),
142+
],
143+
)
144+
def test_unknown_keys_in_the_block_are_rejected_not_ignored(self, tmp_path, block, needle):
145+
# A misspelled `defaults` would silently enforce the package defaults instead of the
146+
# operator's override — the same present-but-wrong class as an empty block.
147+
cfg = _write(tmp_path, "config.yaml", f"forward_email:\n domains: [x.be]\n {block}")
148+
with pytest.raises(ForwardEmailConfigError, match=needle):
149+
load_forward_email(str(cfg))
150+
136151

137152
class TestTypeValidation:
138153
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)