test_telemetry_stack.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. """Stack 20 (telemetry) is a skeleton that creates nothing until a flag is true.
  2. The tofu tests prove the plans. These tests read the sources, so a new resource
  3. or a resource without a flag gate fails here even if nobody wrote a tofu test
  4. for it, and they keep the secret and cost wiring honest.
  5. """
  6. import json
  7. import re
  8. from pathlib import Path
  9. ROOT = Path(__file__).resolve().parent.parent
  10. STACK = ROOT / "stacks" / "20-telemetry"
  11. def code(path: Path) -> str:
  12. return re.sub(r"(?m)(#|//).*$", "", path.read_text())
  13. def resource_blocks() -> dict[str, str]:
  14. """{type.name: body} for every resource block in the stack's .tf files."""
  15. found = {}
  16. for path in sorted(STACK.glob("*.tf")):
  17. text = code(path)
  18. for m in re.finditer(r'(?m)^resource "([^"]+)" "([^"]+)" \{\n', text):
  19. end = text.index("\n}\n", m.end())
  20. found[f"{m.group(1)}.{m.group(2)}"] = text[m.end() : end]
  21. return found
  22. def test_the_stack_has_exactly_the_expected_resources():
  23. # A new resource must be added here on purpose, with its flag gate.
  24. assert sorted(resource_blocks()) == [
  25. "twingate_resource.logging",
  26. "twingate_resource.scrape",
  27. "twingate_resource.site_logging",
  28. "twingate_resource.site_scrape",
  29. "twingate_service_account.logging",
  30. "twingate_service_account.scrape",
  31. "twingate_service_account.site_logging",
  32. "twingate_service_account.site_scrape",
  33. ]
  34. def test_every_resource_is_gated_on_a_flag():
  35. for name, body in resource_blocks().items():
  36. gate = "logging" if name.endswith("logging") else "metrics"
  37. assert re.search(
  38. rf"(?m)^\s*(count|for_each)\s*=.*(var\.{gate}_enabled|local\.(logging_names|scrape_pairs|extra_{"logging" if gate == "logging" else "scrape"}\w*))",
  39. body,
  40. ), f"{name} is not gated on {gate}_enabled"
  41. text = code(STACK / "main.tf")
  42. assert "var.logging_enabled ? toset(" in text
  43. assert "var.metrics_enabled ? nonsensitive(" in text
  44. def test_only_the_twingate_provider_is_used():
  45. versions = code(STACK / "versions.tf")
  46. assert re.findall(r'(?m)^provider "([^"]+)"', versions) == ["twingate"]
  47. for path in STACK.glob("*.tf"):
  48. assert not re.search(r"digitalocean_|aws_", code(path)), path.name
  49. def test_no_service_key_is_created():
  50. # Keys are issued out of band, one per host (design section 2.8).
  51. for path in STACK.glob("*.tf"):
  52. assert "service_account_key" not in code(path), path.name
  53. def test_private_variables_are_sensitive_and_flags_default_false():
  54. text = (STACK / "variables.tf").read_text()
  55. for name in (
  56. "logging_servers",
  57. "connector_private_ips",
  58. "home_remote_network_id",
  59. "cloud_remote_network_id",
  60. ):
  61. body = re.search(rf'variable "{name}" \{{\n(.*?)\n\}}\n', text, re.DOTALL).group(1)
  62. assert re.search(r"(?m)^\s*sensitive\s*=\s*true", body), name
  63. for name in ("logging_enabled", "metrics_enabled"):
  64. body = re.search(rf'variable "{name}" \{{\n(.*?)\n\}}\n', text, re.DOTALL).group(1)
  65. assert re.search(r"(?m)^\s*default\s*=\s*false", body), name
  66. body = re.search(r'variable "metrics_port_connector" \{\n(.*?)\n\}\n', text, re.DOTALL).group(1)
  67. assert re.search(r"(?m)^\s*default\s*=\s*null", body), "no default port for the unverified bind port"
  68. def test_the_scrape_targets_output_is_sensitive_and_the_logging_list_is_not_output():
  69. outputs = code(STACK / "outputs.tf")
  70. scrape = re.search(r'output "scrape_targets" \{\n(.*?)\n\}\n', outputs, re.DOTALL).group(1)
  71. assert re.search(r"(?m)^\s*sensitive\s*=\s*true", scrape)
  72. assert "logging_servers" not in outputs
  73. assert "remote_network_id" not in outputs
  74. def test_the_cost_manifest_is_empty():
  75. manifest = json.loads((STACK / "cost.json").read_text())
  76. assert manifest["resources"] == []