docs(readme): sync architecture and runtime flow with current agent behavior

This commit is contained in:
patrick committed 2026-02-10 21:57:11 -05:00
1 parent 234224a5c6
commit 6dac06c117
1 file changed
+34 -22
+34 -22
View File
@@ -6,58 +6,70 @@ A self-healing synchronization agent designed to ensure the local `nftables` ker
## 🎯 Purpose ## 🎯 Purpose
The standard `crowdsec-firewall-bouncer` can occasionally experience "ghost" desynchronization where the LAPI removes a ban, but the kernel retains it (or vice-versa). This agent acts as a high-integrity supervisor to: The standard `crowdsec-firewall-bouncer` can occasionally experience "ghost" desynchronization where the LAPI removes a ban, but the kernel retains it (or vice-versa). This agent acts as a high-integrity supervisor to:
1. **Audit:** Compare LAPI "Truth" vs. Nftables "Reality" every 5 minutes. 1. **Audit:** Compare LAPI "Truth" vs. `nftables` "Reality" every cycle.
2. **Heal:** Automatically restart remediation services if a desync is detected. 2. **Heal:** Restart `crowdsec-firewall-bouncer` on desync to force a fresh LAPI pull.
3. **Filter:** Handle protocol mismatches (filtering IPv6 noise) and expired "zombie" decisions. 3. **Verify:** Re-check state after healing and record success/failure telemetry.
## 🛠️ Technical Architecture ## 🛠️ Technical Architecture
### The Auditor (`auditor.py`) ### The Auditor (`auditor.py`)
- **Deep-Harvest Parser:** Recursively scans the `cscli` Alert-style JSON. This bypasses schema variations where IPs might be hidden in `source`, `decisions`, or `meta` blocks. - **Schema-tolerant parsing:** Supports both flat decision JSON and nested alert-style JSON from `cscli`.
- **Protocol Filtering:** Automatically ignores IPv6 addresses (detected by `:`) to prevent insertion errors in IPv4-only `nft` tables. - **State reconciliation:** Compares LAPI decision IP set against kernel IP set and computes drift as `missing` and `extra`.
- **Global Table Scan:** Rather than checking a single set, it scans the **entire** `ip crowdsec` table. This ensures that IPs placed in different "drawers" (like Range sets) are still accounted for. - **Drift fingerprinting:** Generates a deterministic fingerprint for flap suppression and heal history.
- **Temporal Awareness:** Filters out decisions with negative durations (`startswith('-')`) to ignore bans that are technically expired but not yet vacuumed from the database. - **Metadata drift check:** Flags long-duration decisions that are near expiry in kernel state.
- **Kernel scan scope:** Scans `ip crowdsec` and `ip6 crowdsec6` tables via `nft -j`.
### The Service
- **Root-Level Execution:** Necessary for direct interaction with `nftables` and the `cscli` local socket.
- **Dependency Management:** Configured to start only after `crowdsec.service` is initialized to prevent false-positive desyncs on boot.
### The Orchestrator (`agent.py`)
- Runs the reconcile loop (default `600s` interval).
- Triggers heal when any drift is detected.
- Applies flap suppression using drift fingerprint, not raw kernel count.
- Performs post-heal verification after a short delay (default `5s`).
### The Reporter (`reporter.py`)
- Writes local health state JSON atomically.
- Persists structured heal history, including pre/post fingerprints and heal result.
- Default state path: `/root/threat-engine-agent/threat-agent-state.json`.
## 📂 Project Structure ## 📂 Project Structure
```text ```text
threat-engine-agent/ threat-engine-agent/
├── main.py # Logic loop and orchestration ├── agent.py # Main loop and heal orchestration
├── auditor.py # The "Brain": Recursive parsing and kernel scanning ├── auditor.py # LAPI vs nft reconciliation and drift fingerprinting
├── reporter.py # Local state and heal history writer
├── config.yaml # Static config (currently not wired into runtime)
├── systemd/ # Deployment configurations ├── systemd/ # Deployment configurations
│ └── threat-agent.service │ └── threat-agent.service
└── logs/ # Local audit history (Git ignored) └── logs/ # Runtime logs
``` ```
## 🚀 Installation & Deployment ## 🚀 Installation & Deployment
1. Environment Setup: 1. Environment Setup:
```chmod +x main.py ```bash
mkdir logs mkdir -p /root/threat-engine-agent/logs
cd /root/threat-engine-agent
``` ```
2. Systemd Integration: 2. Systemd Integration:
```sudo cp systemd/threat-agent.service /etc/systemd/system/ ```bash
sudo cp systemd/threat-agent.service /etc/systemd/system/
sudo systemctl daemon-reload sudo systemctl daemon-reload
sudo systemctl enable --now threat-agent sudo systemctl enable --now threat-agent
``` ```
3. Monitoring: 3. Monitoring:
```# Watch the auditor work in real-time ```bash
tail -f logs/threat-agent.log # Watch the agent in real time
tail -f /root/threat-engine-agent/logs/threat-agent.log
``` ```
## 🔍 Troubleshooting ## 🔍 Troubleshooting
If you see a "Heal Loop" in the logs: If you see a "Heal Loop" in the logs:
1. **Manual Check**: Run `/usr/bin/cscli decisions list -a -o json` and check if the count matches `nft list table ip crowdsec`. 1. **Check decision source**: Run `/usr/bin/cscli decisions list -a -o json` and confirm output shape/contents are valid.
2. **IPv6 Ghosting**: Verify if the "Missing from Kernel" IPs are actually IPv6. The auditor should filter these, but new formats may require a regex update. 2. **Check kernel state**: Run `nft -j list table ip crowdsec` and `nft -j list table ip6 crowdsec6`.
3. **Set Naming**: Ensure the bouncer hasn't created a new table name outside of the `crowdsec` namespace. 3. **Check drift telemetry**: Review `missing`, `extra`, `drift_fingerprint`, `heal_result`, and `post_drift_fingerprint` in logs/history.
4. **Check service health**: `systemctl status threat-agent crowdsec-firewall-bouncer`.