-
Notifications
You must be signed in to change notification settings - Fork 5
Only check firewall lists in background process #450
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
Open
bitterpanda63
wants to merge
15
commits into
main
Choose a base branch
from
do-ip-check-in-background-process
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
cffe7d3
Create new firewall lists storage object
bitterpanda63 f7f0390
Add FirewallLists to the connection manager
bitterpanda63 0ea89d1
remove unused is_ip_allowed_by_allowlist
bitterpanda63 1486c64
Create CHECK_FIREWALL_LISTS
bitterpanda63 d0b96fa
request_handler update to use CHECK_FIREWALL_LISTS
bitterpanda63 f3f94a4
add some correct checks on data coming from new command
bitterpanda63 6c673ad
Update test cases for update_firewall_lists_test
bitterpanda63 33a1855
Add 20ms timeout for firewall lists
bitterpanda63 15034a2
firewall_lists: Add a check on self.allowed_ips, fix test cases
bitterpanda63 a766ed0
Move test case from service_config tests to request_handler test cases
bitterpanda63 c814ebb
remove "regex" pkg from e2e checks (since this is now done in bg proc…
bitterpanda63 efdef65
Merge branch 'main' into do-ip-check-in-background-process
bitterpanda63 b667320
with IPC calls, remove benchmarks to 0-delay routes
bitterpanda63 3953cce
also update starlette-postgres-uvicorn app for benchies
bitterpanda63 e80aac1
flask benchmarks +1ms with new ipc call
bitterpanda63 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
45 changes: 45 additions & 0 deletions
45
aikido_zen/background_process/commands/check_firewall_lists.py
This file contains hidden or 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,45 @@ | ||
"""Exports process_check_firewall_lists""" | ||
|
||
from aikido_zen.api_discovery.update_route_info import update_route_info | ||
from aikido_zen.background_process.cloud_connection_manager import ( | ||
CloudConnectionManager, | ||
) | ||
from aikido_zen.background_process.packages import PackagesStore | ||
from aikido_zen.helpers.logging import logger | ||
|
||
|
||
def process_check_firewall_lists( | ||
connection_manager: CloudConnectionManager, data, conn, queue=None | ||
): | ||
""" | ||
Checks whether an IP is blocked | ||
data: {"ip": string, "user-agent": string} | ||
returns -> {"blocked": boolean, "type": string, "reason": string} | ||
""" | ||
ip = data["ip"] | ||
if ip is not None and isinstance(ip, str): | ||
# Global IP Allowlist (e.g. for geofencing) | ||
if not connection_manager.firewall_lists.is_allowed_ip(ip): | ||
return {"blocked": True, "type": "allowlist"} | ||
|
||
# Global IP Blocklist (e.g. blocking known threat actors) | ||
reason = connection_manager.firewall_lists.is_blocked_ip(ip) | ||
if reason: | ||
return { | ||
"blocked": True, | ||
"type": "blocklist", | ||
"reason": reason, | ||
} | ||
|
||
user_agent = data["user-agent"] | ||
if user_agent is not None and isinstance(user_agent, str): | ||
# User agent blocking (e.g. blocking AI scrapers) | ||
if connection_manager.firewall_lists.is_user_agent_blocked(user_agent): | ||
return { | ||
"blocked": True, | ||
"type": "bot-blocking", | ||
} | ||
|
||
return { | ||
"blocked": False, | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 | ||||
---|---|---|---|---|---|---|
|
@@ -6,7 +6,7 @@ | |||||
from aikido_zen.helpers.logging import logger | ||||||
from aikido_zen.thread.thread_cache import get_cache | ||||||
from .ip_allowed_to_access_route import ip_allowed_to_access_route | ||||||
from ...helpers.is_ip_allowed_by_allowlist import is_ip_allowed_by_allowlist | ||||||
import aikido_zen.background_process.comms as c | ||||||
bitterpanda63 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
|
||||||
def request_handler(stage, status_code=0): | ||||||
|
@@ -49,21 +49,32 @@ def pre_response(): | |||||
message += f" (Your IP: {context.remote_address})" | ||||||
return message, 403 | ||||||
|
||||||
# Global IP Allowlist (e.g. for geofencing) | ||||||
if not is_ip_allowed_by_allowlist(cache.config, context.remote_address): | ||||||
# Do a check on firewall lists, this happens in background because of the heavy data. | ||||||
comms = c.get_comms() | ||||||
check_fw_lists_res = comms.send_data_to_bg_process( | ||||||
bitterpanda63 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
action="CHECK_FIREWALL_LISTS", | ||||||
obj={ | ||||||
"ip": context.remote_address, | ||||||
"user-agent": context.get_user_agent(), | ||||||
}, | ||||||
receive=True, | ||||||
timeout_in_sec=(20 / 1000), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
) | ||||||
if not check_fw_lists_res["success"] or not check_fw_lists_res["data"]["blocked"]: | ||||||
return | ||||||
|
||||||
block_type = check_fw_lists_res["data"]["type"] | ||||||
|
||||||
if block_type == "allowlist": | ||||||
bitterpanda63 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
message = "Your IP address is not allowed." | ||||||
message += " (Your IP: " + context.remote_address + ")" | ||||||
return message, 403 | ||||||
|
||||||
# Global IP Blocklist (e.g. blocking known threat actors) | ||||||
reason = cache.config.is_blocked_ip(context.remote_address) | ||||||
if reason: | ||||||
message = "Your IP address is blocked due to " + reason | ||||||
if block_type == "blocklist": | ||||||
message = "Your IP address is blocked due to " | ||||||
message += check_fw_lists_res["data"]["reason"] | ||||||
message += " (Your IP: " + context.remote_address + ")" | ||||||
return message, 403 | ||||||
|
||||||
# User agent blocking (e.g. blocking AI scrapers) | ||||||
if cache.config.is_user_agent_blocked(context.get_user_agent()): | ||||||
if block_type == "bot-blocking": | ||||||
msg = "You are not allowed to access this resource because you have been identified as a bot." | ||||||
return msg, 403 | ||||||
|
||||||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.