Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AIK-3458 Report max x attacks per timeframe to core #142

Merged
merged 5 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions aikido_firewall/background_process/aikido_background_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
)
from aikido_firewall.helpers.check_env_for_blocking import check_env_for_blocking
from aikido_firewall.helpers.token import get_token_from_env
from aikido_firewall.background_process.api.http_api import ReportingApiHTTP
from aikido_firewall.background_process.api.http_api_ratelimited import (
ReportingApiHTTPRatelimited,
)
from .commands import process_incoming_command

EMPTY_QUEUE_INTERVAL = 5 # 5 seconds
Expand Down Expand Up @@ -70,7 +72,11 @@
) # Create an event scheduler
self.send_to_connection_manager(event_scheduler)

api = ReportingApiHTTP("https://guard.aikido.dev/")
api = ReportingApiHTTPRatelimited(

Check warning on line 75 in aikido_firewall/background_process/aikido_background_process.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/background_process/aikido_background_process.py#L75

Added line #L75 was not covered by tests
"https://guard.aikido.dev/",
max_events_per_interval=2,
interval_in_ms=15 * 1000,
bitterpanda63 marked this conversation as resolved.
Show resolved Hide resolved
)
# We need to pass along the scheduler so that the heartbeat also gets sent
self.connection_manager = CloudConnectionManager(
block=check_env_for_blocking(),
Expand Down
33 changes: 33 additions & 0 deletions aikido_firewall/background_process/api/http_api_ratelimited.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
Exports ReportingApiHTTPRatelimited
"""

from aikido_firewall.background_process.api.http_api import ReportingApiHTTP
import aikido_firewall.helpers.get_current_unixtime_ms as t


class ReportingApiHTTPRatelimited(ReportingApiHTTP):
"""HTTP Reporting API that has ratelimiting support"""

def __init__(self, reporting_url, max_events_per_interval, interval_in_ms):
super().__init__(reporting_url)
self.interval_in_ms = interval_in_ms
self.max_events_per_interval = max_events_per_interval
self.events = []

Check warning on line 16 in aikido_firewall/background_process/api/http_api_ratelimited.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/background_process/api/http_api_ratelimited.py#L13-L16

Added lines #L13 - L16 were not covered by tests

def report(self, token, event, timeout_in_sec):
bitterpanda63 marked this conversation as resolved.
Show resolved Hide resolved
if event["type"] == "detected_attack":

Check warning on line 19 in aikido_firewall/background_process/api/http_api_ratelimited.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/background_process/api/http_api_ratelimited.py#L19

Added line #L19 was not covered by tests
# Remove all outdated events :
current_time = t.get_unixtime_ms()

Check warning on line 21 in aikido_firewall/background_process/api/http_api_ratelimited.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/background_process/api/http_api_ratelimited.py#L21

Added line #L21 was not covered by tests

def event_in_interval_filter(e):
return e["time"] > current_time - self.interval_in_ms

Check warning on line 24 in aikido_firewall/background_process/api/http_api_ratelimited.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/background_process/api/http_api_ratelimited.py#L23-L24

Added lines #L23 - L24 were not covered by tests

self.events = list(filter(event_in_interval_filter, self.events))

Check warning on line 26 in aikido_firewall/background_process/api/http_api_ratelimited.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/background_process/api/http_api_ratelimited.py#L26

Added line #L26 was not covered by tests

# Check if interval is exceeded :
if len(self.events) >= self.max_events_per_interval:
return {"success": False, "error": "max_attacks_reached"}

Check warning on line 30 in aikido_firewall/background_process/api/http_api_ratelimited.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/background_process/api/http_api_ratelimited.py#L29-L30

Added lines #L29 - L30 were not covered by tests

self.events.append(event)
return super().report(token, event, timeout_in_sec)

Check warning on line 33 in aikido_firewall/background_process/api/http_api_ratelimited.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/background_process/api/http_api_ratelimited.py#L32-L33

Added lines #L32 - L33 were not covered by tests
Loading