|
| 1 | +""" |
| 2 | +Test module for socket sink |
| 3 | +""" |
| 4 | + |
| 5 | +import importlib |
| 6 | +import socket |
| 7 | +import pytest |
| 8 | +from unittest.mock import patch, MagicMock |
| 9 | +import aikido_zen.sinks.socket # Import to ensure patching |
| 10 | +from aikido_zen.thread.thread_cache import get_cache |
| 11 | +from aikido_zen.background_process.service_config import ServiceConfig |
| 12 | + |
| 13 | + |
| 14 | +def get_patched_socket_module(): |
| 15 | + """Get a patched socket module""" |
| 16 | + import socket |
| 17 | + from aikido_zen.sinks import patch_function |
| 18 | + import aikido_zen.sinks.socket |
| 19 | + |
| 20 | + # Manually apply the patch |
| 21 | + patch_function(socket, "getaddrinfo", aikido_zen.sinks.socket._getaddrinfo_wrapper) |
| 22 | + |
| 23 | + return socket |
| 24 | + |
| 25 | + |
| 26 | +def test_socket_getaddrinfo_no_blocking(): |
| 27 | + """Test that getaddrinfo works normally when no blocking is configured""" |
| 28 | + # Reset cache to ensure clean state |
| 29 | + get_cache().reset() |
| 30 | + |
| 31 | + # Get patched socket module |
| 32 | + socket_module = get_patched_socket_module() |
| 33 | + |
| 34 | + # Mock getaddrinfo to return a known value |
| 35 | + mock_result = [(2, 1, 6, "", ("127.0.0.1", 80))] |
| 36 | + with patch.object(socket_module, "getaddrinfo", return_value=mock_result): |
| 37 | + result = socket_module.getaddrinfo("example.com", 80) |
| 38 | + assert result == mock_result |
| 39 | + |
| 40 | + |
| 41 | +def test_socket_getaddrinfo_block_specific_domain(): |
| 42 | + """Test that getaddrinfo raises exception when specific domain is blocked""" |
| 43 | + # Reset cache and set up blocking for specific domain |
| 44 | + cache = get_cache() |
| 45 | + cache.reset() |
| 46 | + cache.config.update_domains( |
| 47 | + [ |
| 48 | + {"hostname": "blocked.com", "mode": "block"}, |
| 49 | + {"hostname": "allowed.com", "mode": "allow"}, |
| 50 | + ] |
| 51 | + ) |
| 52 | + |
| 53 | + # Get patched socket module |
| 54 | + socket_module = get_patched_socket_module() |
| 55 | + |
| 56 | + # Test that blocked domain raises exception |
| 57 | + with pytest.raises(Exception) as exc_info: |
| 58 | + socket_module.getaddrinfo("blocked.com", 80) |
| 59 | + assert ( |
| 60 | + "Zen has blocked an outbound connection: socket.getaddrinfo to blocked.com" |
| 61 | + in str(exc_info.value) |
| 62 | + ) |
| 63 | + |
| 64 | + # Test that allowed domain works normally |
| 65 | + mock_result = [(2, 1, 6, "", ("127.0.0.1", 80))] |
| 66 | + with patch.object(socket_module, "getaddrinfo", return_value=mock_result): |
| 67 | + result = socket_module.getaddrinfo("allowed.com", 80) |
| 68 | + assert result == mock_result |
| 69 | + |
| 70 | + |
| 71 | +def test_socket_getaddrinfo_block_all_new_requests(): |
| 72 | + """Test that getaddrinfo raises exception when block_new_outgoing_requests is True""" |
| 73 | + # Reset cache and enable blocking for all new requests |
| 74 | + cache = get_cache() |
| 75 | + cache.reset() |
| 76 | + cache.config.set_block_new_outgoing_requests(True) |
| 77 | + cache.config.update_domains([{"hostname": "allowed.com", "mode": "allow"}]) |
| 78 | + |
| 79 | + # Get patched socket module |
| 80 | + socket_module = get_patched_socket_module() |
| 81 | + |
| 82 | + # Test that unknown domain raises exception |
| 83 | + with pytest.raises(Exception) as exc_info: |
| 84 | + socket_module.getaddrinfo("unknown.com", 80) |
| 85 | + assert ( |
| 86 | + "Zen has blocked an outbound connection: socket.getaddrinfo to unknown.com" |
| 87 | + in str(exc_info.value) |
| 88 | + ) |
| 89 | + |
| 90 | + # Test that explicitly allowed domain works even when block_new_outgoing_requests is True |
| 91 | + mock_result = [(2, 1, 6, "", ("127.0.0.1", 80))] |
| 92 | + with patch.object(socket_module, "getaddrinfo", return_value=mock_result): |
| 93 | + result = socket_module.getaddrinfo("allowed.com", 80) |
| 94 | + assert result == mock_result |
| 95 | + |
| 96 | + |
| 97 | +def test_socket_getaddrinfo_no_cache(): |
| 98 | + """Test that getaddrinfo works normally when cache is not available""" |
| 99 | + # Get patched socket module |
| 100 | + socket_module = get_patched_socket_module() |
| 101 | + |
| 102 | + # Mock get_cache to return None |
| 103 | + with patch("aikido_zen.sinks.socket.get_cache", return_value=None): |
| 104 | + mock_result = [(2, 1, 6, "", ("127.0.0.1", 80))] |
| 105 | + with patch.object(socket_module, "getaddrinfo", return_value=mock_result): |
| 106 | + result = socket_module.getaddrinfo("example.com", 80) |
| 107 | + assert result == mock_result |
| 108 | + |
| 109 | + |
| 110 | +def test_service_config_should_block_outgoing_request(): |
| 111 | + """Test the should_block_outgoing_request method""" |
| 112 | + config = ServiceConfig( |
| 113 | + endpoints=[], |
| 114 | + last_updated_at=0, |
| 115 | + blocked_uids=set(), |
| 116 | + bypassed_ips=[], |
| 117 | + received_any_stats=False, |
| 118 | + ) |
| 119 | + |
| 120 | + # Test with no blocking configured |
| 121 | + assert not config.should_block_outgoing_request("example.com") |
| 122 | + |
| 123 | + # Test with specific domain blocked |
| 124 | + config.update_domains([{"hostname": "blocked.com", "mode": "block"}]) |
| 125 | + assert config.should_block_outgoing_request("blocked.com") |
| 126 | + assert not config.should_block_outgoing_request("allowed.com") |
| 127 | + |
| 128 | + # Test with block_new_outgoing_requests enabled |
| 129 | + config.set_block_new_outgoing_requests(True) |
| 130 | + assert config.should_block_outgoing_request("unknown.com") # Unknown domain blocked |
| 131 | + assert config.should_block_outgoing_request("blocked.com") # Still blocked |
| 132 | + |
| 133 | + # Test with explicitly allowed domain when block_new_outgoing_requests is True |
| 134 | + config.update_domains([{"hostname": "allowed.com", "mode": "allow"}]) |
| 135 | + assert not config.should_block_outgoing_request("allowed.com") # Explicitly allowed |
| 136 | + assert config.should_block_outgoing_request("unknown.com") # Unknown still blocked |
| 137 | + |
| 138 | + |
| 139 | +def test_service_config_update_domains(): |
| 140 | + """Test the update_domains method""" |
| 141 | + config = ServiceConfig( |
| 142 | + endpoints=[], |
| 143 | + last_updated_at=0, |
| 144 | + blocked_uids=set(), |
| 145 | + bypassed_ips=[], |
| 146 | + received_any_stats=False, |
| 147 | + ) |
| 148 | + |
| 149 | + # Test initial state |
| 150 | + assert config.domains == {} |
| 151 | + |
| 152 | + # Test updating domains |
| 153 | + config.update_domains( |
| 154 | + [ |
| 155 | + {"hostname": "example.com", "mode": "block"}, |
| 156 | + {"hostname": "allowed.com", "mode": "allow"}, |
| 157 | + ] |
| 158 | + ) |
| 159 | + assert config.domains == {"example.com": "block", "allowed.com": "allow"} |
| 160 | + |
| 161 | + # Test updating with empty list |
| 162 | + config.update_domains([]) |
| 163 | + assert config.domains == {} |
| 164 | + |
| 165 | + |
| 166 | +def test_service_config_set_block_new_outgoing_requests(): |
| 167 | + """Test the set_block_new_outgoing_requests method""" |
| 168 | + config = ServiceConfig( |
| 169 | + endpoints=[], |
| 170 | + last_updated_at=0, |
| 171 | + blocked_uids=set(), |
| 172 | + bypassed_ips=[], |
| 173 | + received_any_stats=False, |
| 174 | + ) |
| 175 | + |
| 176 | + # Test initial state |
| 177 | + assert not config.block_new_outgoing_requests |
| 178 | + |
| 179 | + # Test setting to True |
| 180 | + config.set_block_new_outgoing_requests(True) |
| 181 | + assert config.block_new_outgoing_requests |
| 182 | + |
| 183 | + # Test setting to False |
| 184 | + config.set_block_new_outgoing_requests(False) |
| 185 | + assert not config.block_new_outgoing_requests |
0 commit comments