refactor(agent): split orchestration into config, healing, and main modules
This commit is contained in:
1 parent
5fc5b8c114
commit
480eea8095
7 files changed
+388
-335
No files matched your search
@@ -0,0 +1,110 @@
|
||||
import logging
|
||||
import time
|
||||
|
||||
try:
|
||||
from dotenv import load_dotenv
|
||||
except ImportError:
|
||||
def load_dotenv():
|
||||
return None
|
||||
|
||||
from auditor import FirewallAuditor
|
||||
from config import load_runtime_settings, setup_logging
|
||||
from healing import execute_heal_and_verify, should_heal
|
||||
from reporter import LocalStateReporter
|
||||
|
||||
|
||||
def main():
|
||||
"""
|
||||
The Orchestrator: Links Audit findings to the State Reporter.
|
||||
"""
|
||||
load_dotenv()
|
||||
settings = load_runtime_settings()
|
||||
|
||||
setup_logging(settings.log_path)
|
||||
auditor = FirewallAuditor()
|
||||
reporter = LocalStateReporter(
|
||||
state_file=settings.state_file,
|
||||
node_name=settings.node_name,
|
||||
)
|
||||
|
||||
print(f"[*] Agent started. Check interval: {settings.check_interval}s")
|
||||
logging.info(
|
||||
f"Threat Engine Agent initialized on {settings.node_name}. "
|
||||
f"config_path={settings.config_path} "
|
||||
f"log_path={settings.log_path} state_file={settings.state_file}"
|
||||
)
|
||||
|
||||
while True:
|
||||
try:
|
||||
is_synced, count, drift = auditor.reconcile()
|
||||
reporter.update_status(is_synced, count)
|
||||
|
||||
if is_synced:
|
||||
status_msg = f"Audit Success: {count} IPs in sync."
|
||||
else:
|
||||
status_msg = (
|
||||
f"DESYNC: kernel={count}, missing={drift['missing_count']}, "
|
||||
f"extra={drift['extra_count']}, metadata={drift['metadata_drift_count']}. "
|
||||
"Healing..."
|
||||
)
|
||||
print(f"[+] {status_msg}") # This will show up in your terminal!
|
||||
|
||||
if not is_synced:
|
||||
logging.warning(
|
||||
"Desync detected "
|
||||
f"(kernel={count}, missing={drift['missing_count']}, "
|
||||
f"extra={drift['extra_count']}, metadata={drift['metadata_drift_count']})."
|
||||
)
|
||||
|
||||
history_file = reporter.state_file.replace(".json", ".history")
|
||||
|
||||
if should_heal(
|
||||
history_file,
|
||||
drift.get("fingerprint", ""),
|
||||
min_interval=settings.heal_min_interval,
|
||||
):
|
||||
logging.warning("Healing approved.")
|
||||
heal_result, post_drift = execute_heal_and_verify(
|
||||
auditor,
|
||||
reporter,
|
||||
count,
|
||||
drift,
|
||||
settings.heal_verify_delay,
|
||||
)
|
||||
|
||||
reporter.log_heal_event(
|
||||
count,
|
||||
drift.get("missing_count", 0),
|
||||
drift.get("extra_count", 0),
|
||||
drift.get("metadata_drift_count", 0),
|
||||
drift.get("fingerprint", ""),
|
||||
drift.get("reason", "unknown"),
|
||||
heal_result,
|
||||
post_drift.get("fingerprint", ""),
|
||||
)
|
||||
else:
|
||||
logging.warning("Healing skipped to prevent flapping.")
|
||||
else:
|
||||
logging.info(f"Audit Success: {count} IPs in sync.")
|
||||
|
||||
print(f"[*] Sleeping for {settings.check_interval} seconds...")
|
||||
time.sleep(settings.check_interval)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
logging.info("Agent shutting down manually.")
|
||||
break
|
||||
except Exception as e:
|
||||
logging.error(f"Main loop error: {e}")
|
||||
# Ensure the reporter notes the error state
|
||||
try:
|
||||
reporter.update_status(False, 0, last_error=str(e))
|
||||
except Exception as reporter_error:
|
||||
logging.error(
|
||||
"Reporter update failed during exception handling: "
|
||||
f"{reporter_error}"
|
||||
)
|
||||
time.sleep(60)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user