patterns/import_apache_waf.py

122 lines
4.4 KiB
Python
Raw Normal View History

2024-12-21 09:05:54 +01:00
import os
import subprocess
import logging
from pathlib import Path
import shutil
2025-02-28 11:20:17 +01:00
import filecmp # Import for file comparison
2024-12-21 09:05:54 +01:00
2025-02-28 11:20:17 +01:00
# --- Configuration ---
LOG_LEVEL = logging.INFO # DEBUG, INFO, WARNING, ERROR
WAF_DIR = Path(os.getenv("WAF_DIR", "waf_patterns/apache")).resolve()
APACHE_WAF_DIR = Path(os.getenv("APACHE_WAF_DIR", "/etc/modsecurity.d/")).resolve()
APACHE_CONF = Path(os.getenv("APACHE_CONF", "/etc/apache2/apache2.conf")).resolve()
INCLUDE_STATEMENT = "IncludeOptional /etc/modsecurity.d/*.conf"
BACKUP_DIR = Path(os.getenv("BACKUP_DIR", "/etc/modsecurity.d/backup")).resolve()
2024-12-21 09:05:54 +01:00
2025-02-28 11:20:17 +01:00
# --- Logging Setup ---
logging.basicConfig(level=LOG_LEVEL, format="%(asctime)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)
2024-12-21 09:05:54 +01:00
def copy_waf_files():
2025-02-28 11:20:17 +01:00
"""Copies WAF files, handling existing files and creating backups."""
logger.info("Copying Apache WAF patterns...")
2025-02-28 11:20:17 +01:00
# Ensure target directory exists
APACHE_WAF_DIR.mkdir(parents=True, exist_ok=True)
logger.info(f"Target directory: {APACHE_WAF_DIR}")
2025-02-28 11:20:17 +01:00
# Ensure backup directory exists
BACKUP_DIR.mkdir(parents=True, exist_ok=True)
logger.info(f"Backup directory: {BACKUP_DIR}")
2025-02-28 11:20:17 +01:00
for conf_file in WAF_DIR.glob("*.conf"):
dst_path = APACHE_WAF_DIR / conf_file.name
2025-02-28 11:20:17 +01:00
try:
if dst_path.exists():
2025-02-28 11:20:17 +01:00
# Compare files. If identical, skip. If different, backup and replace.
if filecmp.cmp(conf_file, dst_path, shallow=False):
logger.info(f"Skipping {conf_file.name} (identical file exists).")
continue # Identical file, skip
2025-02-28 11:20:17 +01:00
# Different file exists: create backup
backup_path = BACKUP_DIR / f"{dst_path.name}.{int(time.time())}" # Timestamped backup
logger.warning(f"Existing file {dst_path.name} differs. Backing up to {backup_path}")
shutil.copy2(dst_path, backup_path) # Backup existing file
2024-12-21 09:05:54 +01:00
2025-02-28 11:20:17 +01:00
# Copy the new file (or overwrite if it was different)
shutil.copy2(conf_file, dst_path) # Copy with metadata
logger.info(f"Copied {conf_file.name} to {dst_path}")
2025-02-28 11:20:17 +01:00
except OSError as e:
logger.error(f"Error copying {conf_file.name}: {e}")
raise # Re-raise for critical error handling
def update_apache_conf():
"""Ensures the include statement is present, avoiding duplicates."""
logger.info("Checking Apache configuration for WAF include...")
2024-12-21 09:05:54 +01:00
try:
with open(APACHE_CONF, "r") as f:
2025-02-28 11:20:17 +01:00
config_lines = f.readlines()
# Check if the include statement *already* exists.
include_present = any(INCLUDE_STATEMENT in line for line in config_lines)
2025-02-28 11:20:17 +01:00
if not include_present:
# Append the include statement to the *end* of the file.
with open(APACHE_CONF, "a") as f:
2025-02-28 11:20:17 +01:00
f.write(f"\n{INCLUDE_STATEMENT}\n") # Add a newline for safety
logger.info(f"Added include statement to {APACHE_CONF}")
else:
2025-02-28 11:20:17 +01:00
logger.info("Include statement already present.")
except FileNotFoundError:
logger.error(f"Apache configuration file not found: {APACHE_CONF}")
raise # Critical error
except OSError as e:
logger.error(f"Error updating Apache configuration: {e}")
raise
2024-12-21 09:05:54 +01:00
def reload_apache():
2025-02-28 11:20:17 +01:00
"""Tests the Apache configuration and reloads if valid."""
logger.info("Reloading Apache...")
try:
2025-02-28 11:20:17 +01:00
# Test configuration
subprocess.run(["apachectl", "configtest"], check=True, capture_output=True, text=True)
logger.info("Apache configuration test successful.")
# Reload Apache
2025-02-28 11:20:17 +01:00
subprocess.run(["systemctl", "reload", "apache2"], check=True, capture_output=True, text=True)
logger.info("Apache reloaded.")
except subprocess.CalledProcessError as e:
2025-02-28 11:20:17 +01:00
logger.error(f"Apache command failed: {e.cmd} - Return code: {e.returncode}")
logger.error(f"Stdout: {e.stdout}")
logger.error(f"Stderr: {e.stderr}")
raise # Re-raise to signal failure
except FileNotFoundError:
2025-02-28 11:20:17 +01:00
logger.error("apachectl or systemctl command not found. Is Apache/systemd installed?")
raise
def main():
2025-02-28 11:20:17 +01:00
"""Main function."""
try:
copy_waf_files()
update_apache_conf()
reload_apache()
2025-02-28 11:20:17 +01:00
logger.info("Apache WAF configuration updated successfully.")
except Exception as e:
2025-02-28 11:20:17 +01:00
logger.critical(f"Script failed: {e}")
exit(1)
2024-12-21 09:05:54 +01:00
if __name__ == "__main__":
2025-02-28 11:20:17 +01:00
main()