#!/usr/bin/env bash # E2E prototype test: mock VyOS API + fake Suricata EVE -> controller decisions. # Run from repo root: bash tests/run_e2e.sh set -euo pipefail ROOT="$(cd "$(dirname "$0")/.." && pwd)" T="$ROOT/tests/.e2e" PORT=18443 VAULT="$(python3 -c 'import secrets; print(secrets.token_urlsafe(18))')" # ephemeral test credential rm -rf "$T"; mkdir -p "$T" trap 'kill $(cat "$T/pids" 2>/dev/null) 2>/dev/null' EXIT # seed a foreign member directly into mock state (controller should adopt+expire it) python3 - "$T/mock_state.json" <<'PY' import json,sys json.dump({"members": ["198.51.100.7"]}, open(sys.argv[1],"w")) PY # --- 1. mock vyos api --------------------------------------------------- python3 "$ROOT/tests/mock_vyos_server.py" --port $PORT --key "$VAULT" \ --state-file "$T/mock_state.json" > "$T/mock.log" 2>&1 & echo $! >> "$T/pids" sleep 1 grep -q "mock vyos api" "$T/mock.log" || { echo "FAIL: mock api not started"; cat "$T/mock.log"; exit 1; } # --- 2. controller config ------------------------------------------------ cat > "$T/config.yaml" <> "$T/eve.json" fi } # --- 3. run controller --------------------------------------------------- ( cd "$ROOT/controller" && LEAF_VAULT=$VAULT exec python3 -m leaf_controller.main \ --config "$T/config.yaml" ) > "$T/controller.log" 2>&1 & echo $! >> "$T/pids" sleep 2 alert() { # sid dest_ip action python3 -c ' import json,sys print(json.dumps({"timestamp":"2026-11-03T12:00:00.000000Z","event_type":"alert", "src_ip":"203.0.113.7","dest_ip":sys.argv[2],"src_port":41000,"dest_port":80, "proto":"TCP","alert":{"signature_id":int(sys.argv[1]),"signature":"e2e test", "severity":2,"action":"allowed", "metadata":{"leaf_action":[sys.argv[3]]}}}))' "$1" "$2" "$3" | send_event; } # --- 4. threshold: first hit no-op, second hit must block 192.0.2.5 ------ alert 1000001 192.0.2.5 block; sleep 1.5 alert 1000001 192.0.2.5 block; sleep 2 grep -q "192.0.2.5" "$T/mock_state.json" || { echo "FAIL: expected block of 192.0.2.5"; cat "$T/controller.log"; exit 1; } # --- 5. allow-list: LAN dest must never be blockable ---------------------- alert 1000001 10.66.0.9 block; alert 1000001 10.66.0.9 block; sleep 2 grep -q "10.66.0.9" "$T/mock_state.json" && { echo "FAIL: allowlisted IP was blocked!"; exit 1; } # --- 6. log rotation: rotate eve.json, new events must still be seen ----- mv "$T/eve.json" "$T/eve.json.1"; touch "$T/eve.json" send_event() { if [ -n "${LEAF_E2E_SOCKET:-}" ]; then python3 -c 'import socket,sys; s=socket.socket(socket.AF_UNIX); s.connect(sys.argv[1]); s.sendall(sys.stdin.buffer.read()); s.close()' "$LEAF_E2E_SOCKET" else cat >> "$T/eve.json" fi } alert 1000002 192.0.2.9 block; sleep 1 alert 1000002 192.0.2.9 block; sleep 2 grep -q "192.0.2.9" "$T/mock_state.json" || { echo "FAIL: rotation: expected block of 192.0.2.9"; cat "$T/controller.log"; exit 1; } # --- 7. foreign member adoption: 198.51.100.7 was adopted, then expires -- grep -q "adopted foreign member 198.51.100.7" "$T/controller.log" || { echo "FAIL: foreign member not adopted"; grep -i adopt "$T/controller.log"; exit 1; } # --- 8. TTL expiry: both blocked IPs disappear within ~10s --------------- end=$((SECONDS+15)) while [ $SECONDS -lt $end ]; do if ! grep -q "192.0.2.5" "$T/mock_state.json" && ! grep -q "192.0.2.9" "$T/mock_state.json"; then break; fi sleep 1 done grep -q "192.0.2.5" "$T/mock_state.json" && { echo "FAIL: TTL expiry didn't unblock 192.0.2.5"; cat "$T/mock_state.json"; exit 1; } grep -q "192.0.2.9" "$T/mock_state.json" && { echo "FAIL: TTL expiry didn't unblock 192.0.2.9"; cat "$T/mock_state.json"; exit 1; } # --- 9b. src-mode: block_src metadata + policy.block_src=true ------------ # attacker src from TEST-NET, victim on allowlisted LAN -> src must be blocked, # victim dest must NOT be blocked, and allowlisted src must be ignored. # allowlisted (RFC1918-floor) src must never be blocked, even in src mode cat > "$T/fakeeve_floor.py" <<'PY' import json,sys print(json.dumps({"timestamp":"2026-11-03T12:04:00.000000Z","event_type":"alert", "src_ip":"10.130.0.5","dest_ip":"10.66.0.9","src_port":41000,"dest_port":22, "proto":"TCP","alert":{"signature_id":1000020,"signature":"e2e floor test","severity":2, "action":"allowed","metadata":{"leaf_action":["block_src"]}}})) PY python3 "$T/fakeeve_floor.py" | send_event; sleep 1 python3 "$T/fakeeve_floor.py" | send_event; sleep 2 grep -q "10.130.0.5" "$T/mock_state.json" && { echo "FAIL: allowlisted src was blocked!"; exit 1; } cat > "$T/fakeeve_src.py" <<'PY' import json,sys # src from TEST-NET (blockable) hitting allowlisted dest line = json.dumps({"timestamp":"2026-11-03T12:05:00.000000Z","event_type":"alert", "src_ip":"198.51.100.42","dest_ip":"10.66.0.9","src_port":41001,"dest_port":22, "proto":"TCP","alert":{"signature_id":1000020,"signature":"e2e src test","severity":2, "action":"allowed","metadata":{"leaf_action":["block_src"]}}}) print(line) PY python3 "$T/fakeeve_src.py" | send_event; sleep 1 python3 "$T/fakeeve_src.py" | send_event; sleep 2 grep -q "198.51.100.42" "$T/mock_state.json" || { echo "FAIL: src-mode block of 198.51.100.42 missing"; cat "$T/controller.log"; exit 1; } # --- 9c. unix_stream socket replay: fake suricata client -> controller ---- # exercises the real AF_UNIX path end-to-end: the controller listens, a # client connects and streams newline-delimited JSON; the same threshold # pair must block 192.0.2.42 through the socket path alone if [ -n "${LEAF_E2E_SOCKET:-}" ]; then python3 - "$LEAF_E2E_SOCKET" <<'PY' import json, socket, sys s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.connect(sys.argv[1]) rec = {"timestamp": "2026-11-03T12:06:00.000000Z", "event_type": "alert", "src_ip": "203.0.113.77", "dest_ip": "192.0.2.42", "src_port": 41002, "dest_port": 80, "proto": "TCP", "alert": {"signature_id": 1000001, "signature": "socket e2e", "severity": 2, "action": "allowed", "metadata": {"leaf_action": ["block"]}}} for _ in range(2): s.sendall((json.dumps(rec) + "\n").encode()) s.close() PY sleep 2 grep -q "192.0.2.42" "$T/mock_state.json" || { echo "FAIL: socket replay: expected block of 192.0.2.42"; cat "$T/controller.log"; exit 1; } fi # --- 10. audit log sanity -------------------------------------------------- grep -q '"action": "block"' "$T/decisions.jsonl" || { echo "FAIL: audit missing block entries"; exit 1; } grep -q '"action": "unblock"' "$T/decisions.jsonl" || { echo "FAIL: audit missing unblock entries"; exit 1; } echo "E2E PASS"