test_quota_kerberos_transition.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. """Regression tests for quota ACL transitions around proxy authentication."""
  2. from __future__ import annotations
  3. from contextlib import contextmanager
  4. from pathlib import Path
  5. from types import SimpleNamespace
  6. from services.quota import quota_service
  7. from utils.admin import SquidConfigManager as RealSquidConfigManager
  8. class _ConfigManager:
  9. """Minimal file-backed manager for one quota transition."""
  10. is_valid = True
  11. is_modular = False
  12. errors: list[str] = []
  13. def __init__(self, tmp_path: Path, content: str, *, proxy_auth: bool) -> None:
  14. self.config_path = str(tmp_path / "squid.conf")
  15. self.config_dir = str(tmp_path / "squid.d")
  16. self.config_content = content
  17. self.proxy_auth = proxy_auth
  18. Path(self.config_path).write_text(content, encoding="utf-8")
  19. def load_config(self) -> bool:
  20. self.config_content = Path(self.config_path).read_text(encoding="utf-8")
  21. return True
  22. def _check_modular_config(self) -> None:
  23. return None
  24. def has_proxy_authentication(self) -> bool:
  25. return self.proxy_auth
  26. def save_config(self, content: str) -> bool:
  27. self.config_content = content
  28. Path(self.config_path).write_text(content, encoding="utf-8")
  29. return True
  30. def _configure_transition(monkeypatch, tmp_path, manager, blocked_file, *, valid=True):
  31. """Patch host-only validation/runtime dependencies for a focused test."""
  32. class _ManagerFactory:
  33. _atomic_write = staticmethod(RealSquidConfigManager._atomic_write)
  34. def __new__(cls):
  35. return manager
  36. @contextmanager
  37. def lock(_config_path):
  38. yield
  39. monkeypatch.setattr(quota_service, "SquidConfigManager", _ManagerFactory)
  40. monkeypatch.setattr(quota_service, "squid_config_write_lock", lock)
  41. monkeypatch.setattr(quota_service, "_BLOCKED_USERS_PATH", str(blocked_file))
  42. monkeypatch.setattr(
  43. quota_service,
  44. "_find_squid_runtime",
  45. lambda: SimpleNamespace(kind="local", executable="squid"),
  46. )
  47. monkeypatch.setattr(
  48. quota_service.SquidConfigSplitter,
  49. "_validate_squid_config",
  50. lambda _self: {"success": valid, "error_message": "invalid"},
  51. )
  52. monkeypatch.setattr(quota_service, "reconfigure_squid", lambda *_args: (True, "ok"))
  53. def test_enabling_kerberos_replaces_src_quota_representation(tmp_path, monkeypatch):
  54. """A source include must not survive next to the proxy_auth quota ACL."""
  55. blocked_file = tmp_path / "usuarios_bloqueados.txt"
  56. blocked_file.write_text(
  57. "acl usuarios_bloqueados src 10.20.30.40\n", encoding="utf-8"
  58. )
  59. manager = _ConfigManager(
  60. tmp_path,
  61. "\n".join(
  62. [
  63. "http_port 3128",
  64. f"include {blocked_file}",
  65. f'acl usuarios_bloqueados proxy_auth -i "{blocked_file}"',
  66. "http_access deny usuarios_bloqueados",
  67. "http_access allow localnet",
  68. "http_access deny all",
  69. "",
  70. ]
  71. ),
  72. proxy_auth=True,
  73. )
  74. _configure_transition(monkeypatch, tmp_path, manager, blocked_file)
  75. changed, existing = quota_service._sync_blocked_users_and_squid_rules(
  76. str(blocked_file), {"alice@EXAMPLE.TEST"}
  77. )
  78. assert changed is True
  79. assert existing == set()
  80. assert blocked_file.read_text(encoding="utf-8") == "alice@EXAMPLE.TEST\n"
  81. assert (
  82. f'acl usuarios_bloqueados proxy_auth -i "{blocked_file}"'
  83. in manager.config_content
  84. )
  85. assert f"include {blocked_file}" not in manager.config_content
  86. assert "acl usuarios_bloqueados src" not in manager.config_content
  87. assert manager.config_content.count("acl usuarios_bloqueados") == 1
  88. def test_disabling_kerberos_replaces_proxy_auth_quota_representation(
  89. tmp_path, monkeypatch
  90. ):
  91. """A plain identity list must be converted before its source include exists."""
  92. blocked_file = tmp_path / "usuarios_bloqueados.txt"
  93. blocked_file.write_text("alice@EXAMPLE.TEST\n", encoding="utf-8")
  94. manager = _ConfigManager(
  95. tmp_path,
  96. "\n".join(
  97. [
  98. "http_port 3128",
  99. f'acl usuarios_bloqueados proxy_auth -i "{blocked_file}"',
  100. f"include {blocked_file}",
  101. "http_access deny usuarios_bloqueados",
  102. "http_access allow localnet",
  103. "http_access deny all",
  104. "",
  105. ]
  106. ),
  107. proxy_auth=False,
  108. )
  109. _configure_transition(monkeypatch, tmp_path, manager, blocked_file)
  110. changed, existing = quota_service._sync_blocked_users_and_squid_rules(
  111. str(blocked_file), {"10.20.30.40"}
  112. )
  113. assert changed is True
  114. assert existing == set()
  115. assert blocked_file.read_text(encoding="utf-8") == (
  116. "acl usuarios_bloqueados src 10.20.30.40\n"
  117. )
  118. assert f"include {blocked_file}" in manager.config_content
  119. assert (
  120. f'acl usuarios_bloqueados proxy_auth -i "{blocked_file}"'
  121. not in manager.config_content
  122. )
  123. assert manager.config_content.count("usuarios_bloqueados") == 2
  124. def test_failed_kerberos_quota_transition_restores_config_and_list(
  125. tmp_path, monkeypatch
  126. ):
  127. """Validation failure rolls back both halves of the mode migration."""
  128. blocked_file = tmp_path / "usuarios_bloqueados.txt"
  129. original_list = "acl usuarios_bloqueados src 10.20.30.40\n"
  130. blocked_file.write_text(original_list, encoding="utf-8")
  131. original_config = "\n".join(
  132. [
  133. "http_port 3128",
  134. f"include {blocked_file}",
  135. "http_access deny usuarios_bloqueados",
  136. "http_access allow localnet",
  137. "http_access deny all",
  138. "",
  139. ]
  140. )
  141. manager = _ConfigManager(tmp_path, original_config, proxy_auth=True)
  142. _configure_transition(monkeypatch, tmp_path, manager, blocked_file, valid=False)
  143. changed, _existing = quota_service._sync_blocked_users_and_squid_rules(
  144. str(blocked_file), {"alice@EXAMPLE.TEST"}
  145. )
  146. assert changed is False
  147. assert manager.config_content == original_config
  148. assert Path(manager.config_path).read_text(encoding="utf-8") == original_config
  149. assert blocked_file.read_text(encoding="utf-8") == original_list
  150. def test_src_value_file_drops_plain_or_injected_identity_lines(tmp_path):
  151. """The source include accepts only complete generated src ACL directives."""
  152. blocked_file = tmp_path / "usuarios_bloqueados.txt"
  153. blocked_file.write_text("alice@EXAMPLE.TEST\n# operator note\n", encoding="utf-8")
  154. changed, _existing = quota_service._sync_blocked_users_file(
  155. str(blocked_file), {"10.20.30.40", "bad\nhttp_access allow all"}, True
  156. )
  157. assert changed is True
  158. content = blocked_file.read_text(encoding="utf-8")
  159. assert "alice@EXAMPLE.TEST" not in content
  160. assert "http_access" not in content
  161. assert content == "# operator note\nacl usuarios_bloqueados src 10.20.30.40\n"