-
Notifications
You must be signed in to change notification settings - Fork 11
add outbound blocking #550
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
Changes from all commits
ba836ca
693faa3
e4334f1
7d72f58
3162f63
e3d7216
9bcb674
2d05b84
54ac2ae
6a9a7df
f25687e
69e8750
aaf9933
8f51b51
138f4fe
27d1e85
236101a
4afaa38
f9754f3
7515687
3efddb2
adbec91
fbae003
9ed0614
bc631c7
fa320ba
97bc477
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| """Test module for update_service_config function""" | ||
|
|
||
| import pytest | ||
| from unittest.mock import MagicMock, patch | ||
| from .update_service_config import update_service_config | ||
| from aikido_zen.background_process.service_config import ServiceConfig | ||
|
|
||
|
|
||
| def test_update_service_config_outbound_blocking(): | ||
| """Test that update_service_config handles outbound request blocking configuration""" | ||
|
|
||
| # Create a mock connection manager with a real ServiceConfig | ||
| connection_manager = MagicMock() | ||
| connection_manager.conf = ServiceConfig( | ||
| endpoints=[], | ||
| last_updated_at=0, | ||
| blocked_uids=set(), | ||
| bypassed_ips=[], | ||
| received_any_stats=False, | ||
| ) | ||
| connection_manager.block = False | ||
|
|
||
| # Test response with blockNewOutgoingRequests | ||
| res = { | ||
| "success": True, | ||
| "blockNewOutgoingRequests": True, | ||
| "domains": [ | ||
| {"hostname": "example.com", "mode": "block"}, | ||
| {"hostname": "allowed.com", "mode": "allow"}, | ||
| ], | ||
| } | ||
|
|
||
| update_service_config(connection_manager, res) | ||
|
|
||
| # Verify that the outbound blocking configuration was set | ||
| assert connection_manager.conf.block_new_outgoing_requests is True | ||
| assert connection_manager.conf.domains == { | ||
| "example.com": "block", | ||
| "allowed.com": "allow", | ||
| } | ||
|
|
||
|
|
||
| def test_update_service_config_outbound_blocking_false(): | ||
| """Test that update_service_config handles blockNewOutgoingRequests=False""" | ||
|
|
||
| # Create a mock connection manager with a real ServiceConfig | ||
| connection_manager = MagicMock() | ||
| connection_manager.conf = ServiceConfig( | ||
| endpoints=[], | ||
| last_updated_at=0, | ||
| blocked_uids=set(), | ||
| bypassed_ips=[], | ||
| received_any_stats=False, | ||
| ) | ||
| connection_manager.block = True | ||
|
|
||
| # Test response with blockNewOutgoingRequests=False | ||
| res = {"success": True, "blockNewOutgoingRequests": False, "domains": []} | ||
|
|
||
| update_service_config(connection_manager, res) | ||
|
|
||
| # Verify that the outbound blocking configuration was set | ||
| assert connection_manager.conf.block_new_outgoing_requests is False | ||
| assert connection_manager.conf.domains == {} | ||
|
|
||
|
|
||
| def test_update_service_config_outbound_blocking_missing(): | ||
| """Test that update_service_config works when outbound blocking fields are missing""" | ||
|
|
||
| # Create a mock connection manager with a real ServiceConfig | ||
| connection_manager = MagicMock() | ||
| connection_manager.conf = ServiceConfig( | ||
| endpoints=[], | ||
| last_updated_at=0, | ||
| blocked_uids=set(), | ||
| bypassed_ips=[], | ||
| received_any_stats=False, | ||
| ) | ||
| connection_manager.block = False | ||
|
|
||
| # Test response without outbound blocking fields | ||
| res = { | ||
| "success": True, | ||
| "endpoints": [], | ||
| "configUpdatedAt": 1234567890, | ||
| } | ||
|
|
||
| update_service_config(connection_manager, res) | ||
|
|
||
| # Verify that the outbound blocking configuration was not changed | ||
| assert connection_manager.conf.block_new_outgoing_requests is False | ||
| assert connection_manager.conf.domains == {} | ||
|
|
||
|
|
||
| def test_update_service_config_failure(): | ||
| """Test that update_service_config does nothing when response indicates failure""" | ||
|
|
||
| # Create a mock connection manager with a real ServiceConfig | ||
| connection_manager = MagicMock() | ||
| connection_manager.conf = ServiceConfig( | ||
| endpoints=[], | ||
| last_updated_at=0, | ||
| blocked_uids=set(), | ||
| bypassed_ips=[], | ||
| received_any_stats=False, | ||
| ) | ||
| connection_manager.block = False | ||
|
|
||
| # Set initial values | ||
| connection_manager.conf.set_block_new_outgoing_requests(True) | ||
| connection_manager.conf.update_domains([{"hostname": "test.com", "mode": "block"}]) | ||
|
|
||
| # Test failed response | ||
| res = {"success": False, "blockNewOutgoingRequests": False, "domains": []} | ||
|
|
||
| update_service_config(connection_manager, res) | ||
|
|
||
| # Verify that nothing was changed due to failure | ||
| assert connection_manager.conf.block_new_outgoing_requests is True | ||
| assert connection_manager.conf.domains == {"test.com": "block"} | ||
|
|
||
|
|
||
| def test_update_service_config_complete(): | ||
| """Test that update_service_config handles all fields correctly""" | ||
|
|
||
| # Create a mock connection manager with a real ServiceConfig | ||
| connection_manager = MagicMock() | ||
| connection_manager.conf = ServiceConfig( | ||
| endpoints=[], | ||
| last_updated_at=0, | ||
| blocked_uids=set(), | ||
| bypassed_ips=[], | ||
| received_any_stats=False, | ||
| ) | ||
| connection_manager.block = False | ||
|
|
||
| # Test complete response | ||
| res = { | ||
| "success": True, | ||
| "block": True, | ||
| "endpoints": [{"route": "/test", "graphql": False}], | ||
| "configUpdatedAt": 1234567890, | ||
| "blockedUserIds": ["user1", "user2"], | ||
| "allowedIPAddresses": ["192.168.1.1"], | ||
| "receivedAnyStats": True, | ||
| "blockNewOutgoingRequests": True, | ||
| "domains": [ | ||
| {"hostname": "blocked.com", "mode": "block"}, | ||
| {"hostname": "allowed.com", "mode": "allow"}, | ||
| {"hostname": "test.com", "mode": "block"}, | ||
| ], | ||
| } | ||
|
|
||
| update_service_config(connection_manager, res) | ||
|
|
||
| # Verify all configurations were updated | ||
| assert connection_manager.block is True | ||
| assert len(connection_manager.conf.endpoints) == 1 | ||
| assert connection_manager.conf.last_updated_at == 1234567890 | ||
| assert connection_manager.conf.blocked_uids == {"user1", "user2"} | ||
| assert connection_manager.conf.received_any_stats is True | ||
| assert connection_manager.conf.block_new_outgoing_requests is True | ||
| assert connection_manager.conf.domains == { | ||
| "blocked.com": "block", | ||
| "allowed.com": "allow", | ||
| "test.com": "block", | ||
| } | ||
|
|
||
|
|
||
| def test_update_service_config_domains_only(): | ||
| """Test that update_service_config handles domains update only""" | ||
|
|
||
| # Create a mock connection manager with a real ServiceConfig | ||
| connection_manager = MagicMock() | ||
| connection_manager.conf = ServiceConfig( | ||
| endpoints=[], | ||
| last_updated_at=0, | ||
| blocked_uids=set(), | ||
| bypassed_ips=[], | ||
| received_any_stats=False, | ||
| ) | ||
| connection_manager.block = False | ||
|
|
||
| # Test response with only domains | ||
| res = { | ||
| "success": True, | ||
| "domains": [ | ||
| {"hostname": "api.example.com", "mode": "block"}, | ||
| {"hostname": "cdn.example.com", "mode": "allow"}, | ||
| ], | ||
| } | ||
|
|
||
| update_service_config(connection_manager, res) | ||
|
|
||
| # Verify that only domains were updated | ||
| assert connection_manager.conf.block_new_outgoing_requests is False # Not changed | ||
| assert connection_manager.conf.domains == { | ||
| "api.example.com": "block", | ||
| "cdn.example.com": "allow", | ||
| } | ||
|
|
||
|
|
||
| def test_update_service_config_block_new_outgoing_requests_only(): | ||
| """Test that update_service_config handles blockNewOutgoingRequests update only""" | ||
|
|
||
| # Create a mock connection manager with a real ServiceConfig | ||
| connection_manager = MagicMock() | ||
| connection_manager.conf = ServiceConfig( | ||
| endpoints=[], | ||
| last_updated_at=0, | ||
| blocked_uids=set(), | ||
| bypassed_ips=[], | ||
| received_any_stats=False, | ||
| ) | ||
| connection_manager.block = False | ||
|
|
||
| # Set initial domains | ||
| connection_manager.conf.update_domains( | ||
| [{"hostname": "existing.com", "mode": "allow"}] | ||
| ) | ||
|
|
||
| # Test response with only blockNewOutgoingRequests | ||
| res = { | ||
| "success": True, | ||
| "blockNewOutgoingRequests": True, | ||
| } | ||
|
|
||
| update_service_config(connection_manager, res) | ||
|
|
||
| # Verify that only blockNewOutgoingRequests was updated | ||
| assert connection_manager.conf.block_new_outgoing_requests is True | ||
| assert connection_manager.conf.domains == {"existing.com": "allow"} # Not changed |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,8 @@ def __init__( | |
| self.update( | ||
| endpoints, last_updated_at, blocked_uids, bypassed_ips, received_any_stats | ||
| ) | ||
| self.block_new_outgoing_requests = False | ||
| self.domains = {} | ||
|
|
||
| def update( | ||
| self, | ||
|
|
@@ -74,3 +76,21 @@ def set_bypassed_ips(self, bypassed_ips): | |
| def is_bypassed_ip(self, ip): | ||
| """Checks if the IP is on the bypass list""" | ||
| return self.bypassed_ips.has(ip) | ||
|
|
||
| def update_domains(self, domains): | ||
| self.domains = {domain["hostname"]: domain["mode"] for domain in domains} | ||
|
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. update_domains mutates self.domains (a shared dict) without synchronization; concurrent readers may observe partial updates or race conditions. Details✨ AI Reasoning 🔧 How do I fix it? More info - Comment |
||
|
|
||
| def set_block_new_outgoing_requests(self, value: bool): | ||
| """Set whether to block new outgoing requests""" | ||
| self.block_new_outgoing_requests = bool(value) | ||
|
|
||
| def should_block_outgoing_request(self, hostname: str) -> bool: | ||
| mode = self.domains.get(hostname) | ||
|
|
||
| if self.block_new_outgoing_requests: | ||
| # Only allow outgoing requests if the mode is "allow" | ||
| # mode is None for unknown hostnames, so they get blocked | ||
| return mode != "allow" | ||
|
|
||
| # Only block outgoing requests if the mode is "block" | ||
| return mode == "block" | ||
Uh oh!
There was an error while loading. Please reload this page.