-
Notifications
You must be signed in to change notification settings - Fork 567
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2017 from blacklanternsecurity/nats
New Module: NATS Output
- Loading branch information
Showing
7 changed files
with
147 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import json | ||
import nats | ||
from bbot.modules.output.base import BaseOutputModule | ||
|
||
|
||
class NATS(BaseOutputModule): | ||
watched_events = ["*"] | ||
meta = { | ||
"description": "Output scan data to a NATS subject", | ||
"created_date": "2024-11-22", | ||
"author": "@TheTechromancer", | ||
} | ||
options = { | ||
"servers": [], | ||
"subject": "bbot_events", | ||
} | ||
options_desc = { | ||
"servers": "A list of NATS server addresses", | ||
"subject": "The NATS subject to publish events to", | ||
} | ||
deps_pip = ["nats-py"] | ||
|
||
async def setup(self): | ||
self.servers = list(self.config.get("servers", [])) | ||
if not self.servers: | ||
return False, "NATS servers are required" | ||
self.subject = self.config.get("subject", "bbot_events") | ||
|
||
# Connect to the NATS server | ||
try: | ||
self.nc = await nats.connect(self.servers) | ||
except Exception as e: | ||
import traceback | ||
|
||
return False, f"Error connecting to NATS: {e}\n{traceback.format_exc()}" | ||
self.verbose("NATS client connected successfully") | ||
return True | ||
|
||
async def handle_event(self, event): | ||
event_json = event.json() | ||
event_data = json.dumps(event_json).encode("utf-8") | ||
while 1: | ||
try: | ||
await self.nc.publish(self.subject, event_data) | ||
break | ||
except Exception as e: | ||
self.warning(f"Error sending event to NATS: {e}, retrying...") | ||
await self.helpers.sleep(1) | ||
|
||
async def cleanup(self): | ||
# Close the NATS connection | ||
await self.nc.close() | ||
self.verbose("NATS client disconnected successfully") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import json | ||
import asyncio | ||
from contextlib import suppress | ||
|
||
from .base import ModuleTestBase | ||
|
||
|
||
class TestNats(ModuleTestBase): | ||
config_overrides = { | ||
"modules": { | ||
"nats": { | ||
"servers": ["nats://localhost:4222"], | ||
"subject": "bbot_events", | ||
} | ||
} | ||
} | ||
skip_distro_tests = True | ||
|
||
async def setup_before_prep(self, module_test): | ||
# Start NATS server | ||
await asyncio.create_subprocess_exec( | ||
"docker", "run", "-d", "--rm", "--name", "bbot-test-nats", "-p", "4222:4222", "nats:latest" | ||
) | ||
|
||
# Wait for NATS to be ready by checking the port | ||
await self.wait_for_port_open(4222) | ||
|
||
# Connect to NATS | ||
import nats | ||
|
||
try: | ||
self.nc = await nats.connect(["nats://localhost:4222"]) | ||
except Exception as e: | ||
self.log.error(f"Error connecting to NATS: {e}") | ||
raise | ||
|
||
# Collect events from NATS | ||
self.nats_events = [] | ||
|
||
async def message_handler(msg): | ||
event_data = json.loads(msg.data.decode("utf-8")) | ||
self.nats_events.append(event_data) | ||
|
||
await self.nc.subscribe("bbot_events", cb=message_handler) | ||
|
||
async def check(self, module_test, events): | ||
try: | ||
events_json = [e.json() for e in events] | ||
events_json.sort(key=lambda x: x["timestamp"]) | ||
|
||
self.nats_events.sort(key=lambda x: x["timestamp"]) | ||
|
||
# Verify the events match | ||
assert events_json == self.nats_events, "Events do not match" | ||
|
||
finally: | ||
with suppress(Exception): | ||
# Clean up: Stop the NATS client | ||
if self.nc.is_connected: | ||
await self.nc.drain() | ||
await self.nc.close() | ||
# Stop NATS server container | ||
await asyncio.create_subprocess_exec( | ||
"docker", "stop", "bbot-test-nats", stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters