feat(inventory): reject unread keys and a missing Cloudflare token before the deploy

The Keycloak stage on echoserver ran ten minutes before sys-svc-dns stopped
it with "API.cloudflare.api_token must be set": the inventory still kept the
token under the flat name CLOUDFLARE_API_TOKEN, which nothing reads any
more. The validator that runs before every deploy now catches that class.

- api.py reports a host that selects DNS_PROVIDER: cloudflare without
  API.cloudflare.api_token, with group_vars/all merged under the host_vars
- variables.py reports every top-level key of host_vars and group_vars that
  core does not read: not declared in group_vars or a role's defaults or
  vars, not tested with "is defined" or "| default(", not read by name in a
  plugin, and not a SYSTEM_EMAIL_<KEY> the email lookup builds; a flat
  credential name such as CLOUDFLARE_API_TOKEN names its API.<provider>.<key>
  path; ansible_* keys are exempt

A key that is read in a way the scan cannot see takes
"# nocheck: inventory-variable", documented in suppression.md.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5.5 committed 2026-10-23 16:18:12 +02:00
1 parent a1ce0e6491
commit 285ce910d2
7 files changed
+391

No files matched your search

@@ -0,0 +1,78 @@
import shutil
import tempfile
import unittest
from pathlib import Path
from cli.administration.inventory.validate.api import (
legacy_api_keys,
validate_api_keys,
)
VAULTED_TOKEN = (
"API:\n"
" cloudflare:\n"
" api_token: !vault |\n"
" $ANSIBLE_VAULT;1.1;AES256\n"
" 3133\n"
)
class TestValidateApiKeys(unittest.TestCase):
def setUp(self):
self.root = Path(tempfile.mkdtemp())
(self.root / "host_vars").mkdir()
(self.root / "group_vars").mkdir()
def tearDown(self):
shutil.rmtree(self.root)
def write(self, relative: str, text: str) -> None:
(self.root / relative).write_text(text, encoding="utf-8")
def test_cloudflare_without_a_token_is_reported(self):
self.write("host_vars/echo.yml", "DNS_PROVIDER: cloudflare\n")
errors = validate_api_keys(self.root)
self.assertEqual(len(errors), 1)
self.assertIn("API.cloudflare.api_token is empty", errors[0])
def test_the_legacy_key_does_not_count_as_a_token(self):
self.write(
"host_vars/echo.yml",
"DNS_PROVIDER: cloudflare\nCLOUDFLARE_API_TOKEN: abc\n",
)
self.assertEqual(len(validate_api_keys(self.root)), 1)
def test_a_vaulted_token_in_the_host_file_passes(self):
self.write("host_vars/echo.yml", "DNS_PROVIDER: cloudflare\n" + VAULTED_TOKEN)
self.assertEqual(validate_api_keys(self.root), [])
def test_a_token_in_group_vars_all_passes(self):
self.write("host_vars/echo.yml", "DNS_PROVIDER: cloudflare\n")
self.write("group_vars/all.yml", VAULTED_TOKEN)
self.assertEqual(validate_api_keys(self.root), [])
def test_a_host_api_mapping_replaces_the_group_one(self):
self.write(
"host_vars/echo.yml",
"DNS_PROVIDER: cloudflare\nAPI:\n telegram:\n bot_token: x\n",
)
self.write("group_vars/all.yml", VAULTED_TOKEN)
self.assertEqual(len(validate_api_keys(self.root)), 1)
def test_another_dns_provider_needs_no_token(self):
self.write("host_vars/echo.yml", "DNS_PROVIDER: localhost\n")
self.assertEqual(validate_api_keys(self.root), [])
class TestLegacyApiKeys(unittest.TestCase):
def test_a_moved_credential_names_its_api_path(self):
self.assertEqual(
legacy_api_keys(set())["CLOUDFLARE_API_TOKEN"], "API.cloudflare.api_token"
)
def test_a_name_core_still_declares_is_not_moved(self):
self.assertNotIn("TELEGRAM_BOT_TOKEN", legacy_api_keys({"TELEGRAM_BOT_TOKEN"}))
if __name__ == "__main__":
unittest.main()
@@ -0,0 +1,94 @@
import shutil
import tempfile
import unittest
from pathlib import Path
from cli.administration.inventory.validate.variables import (
declared_variables,
read_variables,
validate_inventory_variables,
)
KNOWN = {"DOMAIN_PRIMARY", "applications"}
MOVED = {"CLOUDFLARE_API_TOKEN": "API.cloudflare.api_token"}
class TestValidateInventoryVariables(unittest.TestCase):
def setUp(self):
self.root = Path(tempfile.mkdtemp())
(self.root / "host_vars").mkdir()
(self.root / "group_vars").mkdir()
def tearDown(self):
shutil.rmtree(self.root)
def errors(self, relative: str, text: str) -> list[str]:
(self.root / relative).write_text(text, encoding="utf-8")
return validate_inventory_variables(self.root, KNOWN, MOVED)
def test_a_read_variable_passes(self):
text = "DOMAIN_PRIMARY: x\napplications: {}\n"
self.assertEqual(self.errors("host_vars/echo.yml", text), [])
def test_an_unread_constant_fails(self):
errors = self.errors("host_vars/echo.yml", "SYSTEM_EMAIL: x\n")
self.assertEqual(len(errors), 1)
self.assertIn("'SYSTEM_EMAIL' is not read by the deploy", errors[0])
def test_a_moved_credential_names_its_new_path(self):
errors = self.errors("host_vars/echo.yml", "CLOUDFLARE_API_TOKEN: x\n")
self.assertIn("move its value to API.cloudflare.api_token", errors[0])
def test_ansible_variables_pass(self):
text = "ansible_host: x\nansible_become_password: y\n"
self.assertEqual(self.errors("host_vars/echo.yml", text), [])
def test_group_vars_are_checked_too(self):
self.assertEqual(len(self.errors("group_vars/all.yml", "LEGACY: x\n")), 1)
def test_a_host_vars_directory_is_checked(self):
(self.root / "host_vars" / "echo").mkdir()
self.assertEqual(len(self.errors("host_vars/echo/main.yml", "LEGACY: x\n")), 1)
def test_a_marker_on_the_key_line_allows_it(self):
text = "OLD_DOMAIN: x # nocheck: inventory-variable\n"
self.assertEqual(self.errors("host_vars/echo.yml", text), [])
def test_a_marker_above_the_key_allows_it(self):
text = "# nocheck: inventory-variable\nOLD_DOMAIN: x\n"
self.assertEqual(self.errors("host_vars/echo.yml", text), [])
def test_a_vaulted_unread_value_fails(self):
text = "LEGACY_TOKEN: !vault |\n $ANSIBLE_VAULT;1.1;AES256\n 3133\n"
self.assertEqual(len(self.errors("host_vars/echo.yml", text)), 1)
class TestReadVariables(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.read = read_variables()
def test_declarations_count(self):
self.assertLessEqual(declared_variables(), self.read)
self.assertIn("TELEGRAM_BOT_TOKEN", self.read)
def test_guarded_overrides_count(self):
self.assertIn("OIDC_OVERRIDE", self.read)
self.assertIn("design", self.read)
def test_plugin_reads_count(self):
self.assertIn("networks", self.read)
self.assertIn("SYSTEM_EMAIL_DOMAIN", self.read)
def test_the_email_lookup_names_count(self):
self.assertIn("SYSTEM_EMAIL_HOST", self.read)
self.assertIn("SYSTEM_EMAIL_EXTERNAL", self.read)
def test_retired_names_do_not_count(self):
for name in ("CLOUDFLARE_API_TOKEN", "SYSTEM_EMAIL", "internet_interfaces"):
with self.subTest(name=name):
self.assertNotIn(name, self.read)
if __name__ == "__main__":
unittest.main()