| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639 |
- import os
- import re
- import subprocess # nosec B404
- import tempfile
- from dataclasses import dataclass
- from datetime import datetime
- from flask_babel import gettext as _
- from loguru import logger
- @dataclass(frozen=True)
- class Rule:
- filename: str
- patterns: list[re.Pattern]
- class SquidConfigSplitter:
- def __init__(
- self, input_file: str = None, output_dir: str = None, strict: bool = False
- ):
- self.input_file = input_file or "/etc/squid/squid.conf"
- self.output_dir = output_dir or "/etc/squid/squid.d"
- self.strict = strict
- self.unknown_file = "999_unknown.conf"
- self.has_auth = False
- self.auth_lines = []
- self.auth_patterns = [re.compile(r"^auth_param\b"), re.compile(r"^acl auth\b")]
- self.rules = self._compile_rules()
- def _compile_rules(self) -> list[Rule]:
- """
- Compile the directive classification rules.
- The order matters and is intentional.
- """
- return [
- Rule(
- "00_ports.conf",
- [
- re.compile(r"^http_port\b"),
- ],
- ),
- Rule(
- "10_misc.conf",
- [
- re.compile(r"^max_filedesc\b"),
- re.compile(r"^pconn_lifetime\b"),
- re.compile(r"^visible_hostname\b"),
- re.compile(r"^pid_filename\b"),
- re.compile(r"^client_db\b"),
- re.compile(r"^cache_mgr\b"),
- ],
- ),
- Rule(
- "20_security.conf",
- [
- re.compile(r"^via\b"),
- re.compile(r"^forwarded_for\b"),
- re.compile(r"^request_header_access\b"),
- re.compile(r"^quick_abort"),
- re.compile(r"^httpd_suppress_version_string\b"),
- re.compile(r"^reload_into_ims\b"),
- re.compile(r"^read_ahead_gap\b"),
- re.compile(r"^negative_ttl\b"),
- re.compile(r"^positive_dns_ttl\b"),
- re.compile(r"^negative_dns_ttl\b"),
- re.compile(r"^range_offset_limit\b"),
- re.compile(r"^pinger_enable\b"),
- re.compile(r"^server_persistent_connections\b"),
- re.compile(r"^client_persistent_connections\b"),
- re.compile(r"^check_hostnames\b"),
- re.compile(r"^half_closed_clients\b"),
- ],
- ),
- Rule(
- "60_logs.conf",
- [
- re.compile(r"^access_log\b"),
- re.compile(r"^cache_log\b"),
- re.compile(r"^cache_store_log\b"),
- re.compile(r"^cache_access_log\b"),
- re.compile(r"^icap_log\b"),
- re.compile(r"^logformat\b"),
- re.compile(r"^strip_query_terms\b"),
- re.compile(r"^coredump_dir\b"),
- ],
- ),
- Rule(
- "30_cache.conf",
- [
- re.compile(
- r"^cache_(?!log\b|mgr\b|store_log\b|access_log\b|peer(?:\b|_))"
- ),
- re.compile(r"^maximum_object_"),
- re.compile(r"^minimum_object_"),
- re.compile(r"^memory_"),
- re.compile(r"^ipcache_"),
- re.compile(r"^fqdncache_"),
- ],
- ),
- Rule(
- "40_refresh_patterns.conf",
- [
- re.compile(r"^refresh_pattern\b"),
- ],
- ),
- Rule(
- "55_ssl_bump.conf",
- [
- re.compile(r"^ssl_"),
- re.compile(r"^sslcrtd_"),
- re.compile(r"^acl\s+\S+\s+at_step\b"),
- re.compile(r"sslproxy_"),
- ],
- ),
- Rule(
- "70_icap.conf",
- [
- re.compile(r"^icap_(?!log\b)"),
- re.compile(r"^adaptation_"),
- ],
- ),
- Rule(
- "80_dns.conf",
- [
- re.compile(r"^dns_"),
- re.compile(r"^hosts_file\b"),
- re.compile(r"^connect_timeout\b"),
- re.compile(r"^read_timeout\b"),
- ],
- ),
- Rule(
- "50_auth.conf",
- [
- re.compile(r"^auth_param\b"),
- re.compile(r"^authenticate_"),
- re.compile(r"^acl auth\b"),
- ],
- ),
- Rule(
- "100_acls.conf",
- [
- re.compile(r"^acl(?! auth\b)(?!.*\bat_step\b)"),
- re.compile(r"^external_acl_type\b"),
- ],
- ),
- Rule(
- "110_delay_pools.conf",
- [
- re.compile(r"^delay_"),
- ],
- ),
- Rule(
- "115_cache_control.conf",
- [
- re.compile(r"^no_cache\b"),
- re.compile(r"^icp_access\b"),
- re.compile(r"^cache_peer"),
- re.compile(r"^never_direct\b"),
- ],
- ),
- Rule(
- "120_http_access.conf",
- [
- re.compile(r"^http_access\b"),
- re.compile(r"^deny_info\b"),
- ],
- ),
- Rule(
- "90_includes.conf",
- [
- re.compile(r"^include\b"),
- ],
- ),
- ]
- def _get_load_order(self) -> list[str]:
- """
- Return the correct load order for config files.
- ACLs and auth must be loaded before being referenced.
- """
- return [
- "00_ports.conf",
- "10_misc.conf",
- "20_security.conf",
- "30_cache.conf",
- "40_refresh_patterns.conf",
- "50_auth.conf", # Auth defines 'auth' ACL
- "60_logs.conf",
- "80_dns.conf",
- "90_includes.conf", # Includes may define ACLs (e.g. usuarios_bloqueados)
- "100_acls.conf", # All other ACLs defined here
- "70_icap.conf", # Uses 'infoaccess' ACL
- "110_delay_pools.conf", # Uses 'auth', 'work_time_*', 'research_files' ACLs
- "115_cache_control.conf",
- "120_http_access.conf", # Uses all ACLs
- "55_ssl_bump.conf", # Can go anywhere but typically after ACLs
- self.unknown_file, # Catch-all for unclassified
- ]
- def _classify_line(self, line: str) -> str:
- matches = [
- rule.filename
- for rule in self.rules
- for pattern in rule.patterns
- if pattern.search(line)
- ]
- if len(matches) > 1:
- raise ValueError(
- f"Ambiguous rule match for line:\n{line}\nMatches: {matches}"
- )
- if matches:
- return matches[0]
- return self.unknown_file
- @staticmethod
- def _atomic_write(path: str, content: str, encoding: str = "utf-8") -> None:
- """Write *content* to *path* atomically via a temp file + os.replace.
- A plain open(path, 'w') truncates the file immediately; if the process
- dies mid-write the config file ends up empty/corrupt. Writing to a
- temporary file in the same directory and calling os.replace() is atomic
- on POSIX — the destination is either the old content or the new content,
- never a partial write.
- """
- abs_path = os.path.abspath(path)
- dir_name = os.path.dirname(abs_path)
- tmp_path = None
- try:
- with tempfile.NamedTemporaryFile(
- mode="w",
- encoding=encoding,
- dir=dir_name,
- delete=False,
- suffix=".squidstats.tmp",
- ) as tmp:
- tmp.write(content)
- tmp_path = tmp.name
- os.replace(tmp_path, abs_path)
- except Exception:
- if tmp_path and os.path.exists(tmp_path):
- try:
- os.unlink(tmp_path)
- except Exception as cleanup_err:
- logger.warning(
- "Could not remove temporary file %s: %s", tmp_path, cleanup_err
- )
- raise
- def split_config(self) -> dict[str, int]:
- # Verify that the input file exists
- if not os.path.exists(self.input_file):
- raise FileNotFoundError(f"File not found: {self.input_file}")
- # Create the output directory if it doesn't exist
- try:
- if not os.path.exists(self.output_dir):
- os.makedirs(self.output_dir, exist_ok=True)
- logger.info(f"Output directory created: {self.output_dir}")
- else:
- logger.info(f"Output directory already exists: {self.output_dir}")
- except PermissionError as e:
- logger.error(f"Permission denied to create directory: {self.output_dir}")
- raise PermissionError(
- f"No permissions to create directory: {self.output_dir}"
- ) from e
- except OSError as e:
- logger.error(f"Failed to create output directory: {e}")
- raise RuntimeError(
- f"Failed to create output directory: {self.output_dir}"
- ) from e
- # Read original content into memory so we can restore squid.conf atomically
- # if validation fails after _generate_main_config() has already overwritten it.
- # (File-based backup is intentionally disabled; in-memory copy avoids I/O cost.)
- with open(self.input_file, encoding="utf-8") as f:
- _original_content = f.read()
- buffers: dict[str, list[str]] = {}
- pending_comments: list[str] = []
- results: dict[str, int] = {}
- try:
- with open(self.input_file, encoding="utf-8") as f:
- in_continuation = False
- continuation_target = None
- for lineno, raw_line in enumerate(f, start=1):
- line = raw_line.rstrip("\n")
- stripped = line.strip()
- # Handle backslash continuation lines
- if in_continuation:
- buffers.setdefault(continuation_target, [])
- buffers[continuation_target].extend(pending_comments)
- pending_comments.clear()
- buffers[continuation_target].append(raw_line)
- if not line.rstrip().endswith("\\"):
- in_continuation = False
- continuation_target = None
- continue
- # Comments and empty lines
- if not stripped or stripped.startswith("#"):
- pending_comments.append(raw_line)
- continue
- # Check for auth lines and collect them
- if any(p.search(stripped) for p in self.auth_patterns):
- self.has_auth = True
- self.auth_lines.append(raw_line)
- try:
- target = self._classify_line(stripped)
- except ValueError:
- logger.exception(
- "Error classifying line %s: %s", lineno, stripped
- )
- raise RuntimeError("Error classifying configuration line")
- if target == self.unknown_file and self.strict:
- logger.error(
- "Unknown directive at line %s: %s", lineno, stripped
- )
- raise RuntimeError(
- "Unknown directive found in strict mode. Review configuration."
- )
- buffers.setdefault(target, [])
- buffers[target].extend(pending_comments)
- pending_comments.clear()
- buffers[target].append(raw_line)
- # Start tracking continuation if line ends with backslash
- if line.rstrip().endswith("\\"):
- in_continuation = True
- continuation_target = target
- # Orphaned final comments
- if pending_comments:
- buffers.setdefault(self.unknown_file, []).extend(pending_comments)
- # Ensure auth file is created if auth config exists
- self._ensure_auth_file(buffers)
- # Final writing
- for filename, content in sorted(buffers.items()):
- path = os.path.join(self.output_dir, filename)
- try:
- self._atomic_write(path, "".join(content))
- results[filename] = len(content)
- logger.info(f"[OK] {path} ({len(content)} lines)")
- except PermissionError:
- logger.exception("Permission denied writing to file: %s", path)
- raise RuntimeError("Permission denied writing generated file")
- except Exception:
- logger.exception("Failed to write file: %s", path)
- raise RuntimeError("Failed to write generated file")
- # Generate the new main squid.conf with includes
- self._generate_main_config(buffers)
- logger.info(f"Generated new main config: {self.input_file}")
- # Validate Squid configuration
- validation_result = self._validate_squid_config()
- if not validation_result["success"]:
- error_details = validation_result.get("error_message", "Unknown error")
- logger.error(
- "Squid configuration validation failed. Rolling back changes. Error: %s",
- error_details,
- )
- # Restore squid.conf from the in-memory copy taken before any
- # writes. _generate_main_config() has already overwritten it with
- # include-only content, so this is the only way to get it back.
- try:
- self._atomic_write(self.input_file, _original_content)
- logger.info(
- "squid.conf rolled back to original content successfully"
- )
- except Exception:
- logger.exception(
- "Rollback of squid.conf FAILED — manual intervention required!"
- )
- raise RuntimeError(
- f"Squid configuration validation failed. Changes have been reverted.\n\nSquid output:\n{error_details}"
- )
- logger.info("Squid configuration validated successfully.")
- return results
- except Exception:
- logger.exception("Error splitting configuration file")
- raise
- def _validate_squid_config(self) -> dict:
- """
- Validate Squid configuration using 'squid -k parse'.
- First tries the system squid command; if not found, falls back to Docker.
- Returns dict with 'success' (bool), 'output' (str), and 'error_message' (str) if failed.
- """
- def _run_command(cmd: list[str]) -> dict:
- """Run a validation command and return a result dict."""
- try:
- result = subprocess.run( # nosec B603 # noqa: S603
- cmd,
- capture_output=True,
- text=True,
- timeout=30,
- )
- stdout_output = result.stdout.strip() if result.stdout else ""
- stderr_output = result.stderr.strip() if result.stderr else ""
- full_output = ""
- if stdout_output:
- full_output += f"STDOUT:\n{stdout_output}\n"
- if stderr_output:
- full_output += f"STDERR:\n{stderr_output}\n"
- if not full_output:
- full_output = "No output from squid command"
- return {
- "ran": True,
- "returncode": result.returncode,
- "output": full_output,
- }
- except subprocess.TimeoutExpired as e:
- stdout_partial = e.stdout.strip() if e.stdout else ""
- stderr_partial = e.stderr.strip() if e.stderr else ""
- partial_output = ""
- if stdout_partial:
- partial_output += f"STDOUT (partial):\n{stdout_partial}\n"
- if stderr_partial:
- partial_output += f"STDERR (partial):\n{stderr_partial}\n"
- return {
- "ran": True,
- "returncode": -1,
- "output": partial_output,
- "timeout": True,
- }
- except FileNotFoundError:
- return {"ran": False, "output": ""}
- except Exception as e:
- return {"ran": True, "returncode": -1, "output": str(e)}
- # --- Try system squid first ---
- logger.info("Validating Squid configuration with 'squid -k parse'...")
- res = _run_command(["squid", "-k", "parse"])
- if not res["ran"]:
- # squid binary not found on the system, try Docker
- logger.warning(
- "'squid' not found on system. Trying Docker fallback "
- "('docker exec squid_proxy squid -k parse')..."
- )
- res = _run_command(
- ["docker", "exec", "squid_proxy", "squid", "-k", "parse"]
- )
- if not res["ran"]:
- error_msg = (
- "Neither 'squid' nor 'docker' command found. "
- "Cannot validate configuration. "
- "Please ensure Squid is installed (system or Docker)."
- )
- logger.error(error_msg)
- return {"success": False, "error_message": error_msg}
- # --- Evaluate result ---
- if res.get("timeout"):
- error_msg = f"Squid configuration validation timed out after 30 seconds.\n{res['output']}"
- logger.error(error_msg)
- return {
- "success": False,
- "error_message": error_msg,
- "output": res["output"],
- }
- if res["returncode"] == 0:
- logger.info(
- f"Squid configuration is valid.\nCommand output:\n{res['output']}"
- )
- return {
- "success": True,
- "output": res["output"],
- "return_code": res["returncode"],
- }
- error_msg = (
- f"Validation failed with return code {res['returncode']}\n{res['output']}"
- )
- logger.error(f"Squid configuration validation failed:\n{error_msg}")
- return {
- "success": False,
- "error_message": error_msg,
- "output": res["output"],
- "return_code": res["returncode"],
- }
- def _generate_main_config(self, buffers: dict[str, list[str]]) -> None:
- header = [
- "# ============================================================\n",
- "# Archivo generado automáticamente por SquidStats\n",
- f"# Fecha: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n",
- "# NO EDITAR A MANO - Los cambios se perderán\n",
- "# ============================================================\n",
- "\n",
- ]
- includes = []
- load_order = self._get_load_order()
- for filename in load_order:
- if filename in buffers:
- include_path = os.path.join(self.output_dir, filename)
- includes.append(f"include {include_path}\n")
- try:
- self._atomic_write(self.input_file, "".join(header) + "".join(includes))
- logger.info(
- f"Main configuration file regenerated with {len(includes)} includes in correct dependency order"
- )
- except Exception:
- logger.exception("Failed to generate main config")
- raise RuntimeError("Failed to generate main config")
- def _ensure_auth_file(self, buffers: dict[str, list[str]]) -> None:
- """
- Ensure that 50_auth.conf is created if there are auth-related lines in the original config.
- """
- auth_filename = "50_auth.conf"
- if auth_filename in buffers:
- return # Already exists
- if self.has_auth:
- # Create auth file content from collected lines
- auth_content = ["# Auth Configuration\n"] + self.auth_lines
- buffers[auth_filename] = auth_content
- logger.info(
- f"Created {auth_filename} with authentication configuration from original file"
- )
- def get_split_info(self) -> dict[str, str]:
- return {
- "00_ports.conf": "HTTP ports configuration",
- "10_misc.conf": "Miscellaneous configurations",
- "20_security.conf": "Security configuration",
- "30_cache.conf": "Cache configuration",
- "40_refresh_patterns.conf": "Refresh patterns",
- "50_auth.conf": "Authentication configuration",
- "55_ssl_bump.conf": "SSL Bump configuration",
- "60_logs.conf": "Logs configuration",
- "70_icap.conf": "ICAP configuration",
- "80_dns.conf": "DNS configuration",
- "100_acls.conf": "Access control lists (ACLs)",
- "110_delay_pools.conf": "Delay pools configuration",
- "115_cache_control.conf": "Cache control and peering",
- "120_http_access.conf": "HTTP access rules",
- "90_includes.conf": "Additional include directives",
- "999_unknown.conf": "Unclassified directives",
- }
- def check_output_dir_exists(self) -> bool:
- return os.path.exists(self.output_dir) and os.path.isdir(self.output_dir)
- def count_files_in_output_dir(self) -> int:
- if not self.check_output_dir_exists():
- return 0
- return len([f for f in os.listdir(self.output_dir) if f.endswith(".conf")])
- @staticmethod
- def get_split_files_info(output_dir: str = None) -> dict:
- if output_dir is None:
- output_dir = "/etc/squid/squid.d"
- if not os.path.exists(output_dir):
- return {
- "status": "error",
- "message": f"El directorio {output_dir} no existe",
- "code": 404,
- }
- try:
- files_info = []
- for filename in sorted(os.listdir(output_dir)):
- if filename.endswith(".conf"):
- file_path = os.path.join(output_dir, filename)
- try:
- # Get file statistics
- stat_info = os.stat(file_path)
- file_size = stat_info.st_size
- modified_time = datetime.fromtimestamp(
- stat_info.st_mtime
- ).strftime("%Y-%m-%d %H:%M:%S")
- # Count lines
- with open(file_path, encoding="utf-8") as f:
- line_count = sum(1 for _ in f)
- files_info.append(
- {
- "filename": filename,
- "size": file_size,
- "size_human": f"{file_size / 1024:.2f} KB"
- if file_size > 1024
- else f"{file_size} B",
- "lines": line_count,
- "modified": modified_time,
- }
- )
- except Exception as e:
- logger.error(f"Error al leer archivo {filename}: {e}")
- continue
- return {
- "status": "success",
- "data": {
- "output_dir": output_dir,
- "files": files_info,
- "total_files": len(files_info),
- },
- }
- except PermissionError as e:
- logger.error(f"Error de permisos al leer archivos: {e}")
- return {
- "status": "error",
- "message": _(
- "No se tienen permisos suficientes para leer los archivos"
- ),
- "code": 403,
- }
- except Exception:
- logger.exception("Error al obtener archivos de configuración")
- return {
- "status": "error",
- "message": _("Error interno al obtener los archivos"),
- "code": 500,
- }
|