-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathlong_poll_handler.py
More file actions
329 lines (273 loc) · 12.5 KB
/
long_poll_handler.py
File metadata and controls
329 lines (273 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# Author: sazid
from typing import Any, Callable
import traceback
import asyncio
import random
import json
import httpx
from colorama import Fore
from pathlib import Path
from urllib.parse import urlparse
import requests
from Framework.Utilities import RequestFormatter, ConfigModule, CommonUtil
from Framework.Utilities.RequestFormatter import REQUEST_TIMEOUT
from Framework.node_server_state import STATE
class DeployHandler:
"""
DeployHandler is responsible for maintaining the connection with deploy
service and fetch any necessary resources to run test cases.
"""
COMMAND_DONE = b"DONE"
COMMAND_CANCEL = b"CANCEL"
COMMAND_KEY_REQUEST = b"KEY_REQUEST"
COMMAND_PRIVATE_KEY = b"PRIVATE_KEY"
ERROR_PREFIX = b"error"
def __init__(
self,
on_connect_callback: Callable[[bool], None],
response_callback: Callable[[Any], None],
cancel_callback: Callable[[], None],
done_callback: Callable[[], bool],
) -> None:
self.on_connect_callback = on_connect_callback
self.response_callback = response_callback
self.cancel_callback = cancel_callback
# Done callback should return true if node does not want to run anymore
# test cases, false otherwise.
self.done_callback = done_callback
self.backoff_time = 0
async def on_message(self, message) -> bool:
"""Returns True if the handler should quit, False otherwise."""
if message == self.COMMAND_DONE:
# We're done for this run session.
return await self.done_callback()
elif message == self.COMMAND_CANCEL:
# Run cancelled by the user/service.
await self.cancel_callback()
return False
elif message.startswith(b'SECRET:KEY_REQUEST'):
self.handle_key_request(message.replace(b'SECRET:KEY_REQUEST::', b''))
return False
elif message.startswith(b'SECRET:PRIVATE_KEY'):
self.handle_private_key(message.replace(b'SECRET:PRIVATE_KEY::', b''))
return False
await self.response_callback(message)
return False
def on_error(self, error) -> None:
print("[deploy] Error communicating with the deploy service.")
print(error)
if self.backoff_time < 6:
self.backoff_time += 1
def handle_key_request(self, message: bytes) -> None:
"""
Handle KEY_REQUEST command.
Server is asking this node to share its private key that matches the provided public key.
"""
try:
payload = json.loads(message)
request_id = payload["request_id"]
public_key_pem = payload["public_key"]
receiver_node_ids = payload["receiver_node_ids"]
print(f"[key-request] Received request {request_id} to share key with {receiver_node_ids}")
private_key_pem = self.get_private_key_matching_public_key(public_key_pem)
if private_key_pem:
self.respond_to_key_request(request_id, private_key_pem)
else:
print("[key-request] ERROR: No private key found matching the provided public key")
except Exception as e:
print(f"[key-request] Error handling key request: {e}")
traceback.print_exc()
def handle_private_key(self, message: bytes) -> None:
"""
Handle PRIVATE_KEY command.
Server is sending us a private key.
"""
try:
payload = json.loads(message)
key_id = payload["key_id"]
private_key_pem = payload["private_key"]
print(f"[private-key] Received key {key_id}")
self.save_private_key(key_id, private_key_pem)
print(f"[private-key] Key {key_id} saved successfully")
except Exception as e:
print(f"[private-key] Error handling private key: {e}")
traceback.print_exc()
def get_private_key_matching_public_key(self, public_key_pem: str) -> str | None:
try:
from settings import ZEUZ_NODE_PRIVATE_RSA_KEYS_DIR
from cryptography.hazmat.primitives import serialization
key_folder = Path(ZEUZ_NODE_PRIVATE_RSA_KEYS_DIR)
if not key_folder.exists():
print("[key-request] Key folder does not exist")
return None
# Parse the provided public key
try:
target_public_key = serialization.load_pem_public_key(
public_key_pem.encode('utf-8')
)
target_public_key_bytes = target_public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
except Exception as e:
print(f"[key-request] Error parsing provided public key: {e}")
return None
# Get all PEM files
pem_files = list(key_folder.glob("*.pem"))
if not pem_files:
print("[key-request] No private key files found")
return None
# Search through all keys to find matching private key
for pem_file in pem_files:
try:
with open(pem_file, 'rb') as f:
private_key_bytes = f.read()
# Load the private key
private_key = serialization.load_pem_private_key(
private_key_bytes,
password=None
)
# Extract its public key
derived_public_key = private_key.public_key()
derived_public_key_bytes = derived_public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
# Compare public keys
if derived_public_key_bytes == target_public_key_bytes:
print(f"[key-request] Found matching private key: {pem_file.name}")
# Return the private key in traditional RSA PRIVATE KEY format
return private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption()
).decode('utf-8')
except Exception as e:
print(f"[key-request] Error reading key file {pem_file.name}: {e}")
continue
print("[key-request] No private key matches the provided public key")
return None
except Exception as e:
print(f"[key-request] Error searching for matching private key: {e}")
traceback.print_exc()
return None
def save_private_key(self, key_id: str, private_key_pem: str) -> None:
"""
Save the received private key to encrypted storage.
This key is saved to the same location where generated keys are stored.
"""
try:
from settings import ZEUZ_NODE_PRIVATE_RSA_KEYS_DIR
from cryptography.hazmat.primitives import serialization
key_folder = Path(ZEUZ_NODE_PRIVATE_RSA_KEYS_DIR)
key_folder.mkdir(parents=True, exist_ok=True)
private_key = serialization.load_pem_private_key(
private_key_pem.encode('utf-8'),
password=None,
)
key_filename = f"received-{key_id}.pem"
key_path = key_folder / key_filename
with open(key_path, 'wb') as f:
f.write(private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption()
))
print(f"[private-key] Saved to: {key_path}")
except Exception as e:
print(f"[private-key] Error saving private key: {e}")
traceback.print_exc()
def respond_to_key_request(self, request_id: str, private_key_pem: str) -> None:
"""
Respond to a key request - server will automatically distribute to receivers.
"""
try:
server_url = urlparse(
ConfigModule.get_config_value("Authentication", "server_address")
)
api_url = f"{server_url.scheme}://{server_url.netloc}/zsvc/deploy/v1/respond-to-key-request"
# Get node ID
node_id = CommonUtil.MachineInfo().getLocalUser().lower()
response = RequestFormatter.request(
"post",
api_url,
json={
"request_id": request_id,
"donor_node_id": node_id,
"private_key": private_key_pem
},
)
if response.ok:
result = response.json()
success_count = result.get('success_count', 0)
print(f"[key-request] Successfully distributed key to {success_count} receiver nodes")
else:
print(f"[key-request] Failed to respond to key request: {response.status_code}")
print(f"[key-request] Response: {response.text}")
except Exception as e:
print(f"[key-request] Error responding to key request: {e}")
traceback.print_exc()
async def run(self, host: str) -> None:
reconnect = False
print_online = False
while True:
if CommonUtil.run_cancelled:
break
if STATE.reconnect_with_credentials is not None:
break
if reconnect:
await asyncio.sleep(random.randint(1, 3))
await self.on_connect_callback(reconnect)
try:
reconnect = True
resp = await RequestFormatter.async_request("get", host, timeout=70)
if resp is None:
break
if resp.content.startswith(self.ERROR_PREFIX):
self.on_error(resp.content)
continue
if resp.ok and print_online:
print_online = False
node_id = CommonUtil.MachineInfo().getLocalUser().lower()
print(f"🟢 {node_id} back to online")
if resp.status_code == httpx.codes.NO_CONTENT:
continue
if resp.status_code == httpx.codes.BAD_GATEWAY:
print_online = True
print(Fore.YELLOW + "Server offline. Retrying after 30s")
await asyncio.sleep(30)
continue
if not resp.ok:
print(
"[deploy] Request Error, status code:",
resp.status_code,
"| reconnecting",
)
# Encountered a server error, retry.
await asyncio.sleep(random.randint(1, 3))
continue
should_quit = await self.on_message(resp.content)
if should_quit:
break
reconnect = False
except asyncio.CancelledError:
break
except (
requests.exceptions.ConnectTimeout,
requests.exceptions.ReadTimeout,
requests.exceptions.ConnectionError,
) as e:
# Nginx down, VM down, network issue, docker-compose stopped
if STATE.reconnect_with_credentials is not None:
return None
print_online = True
print(e)
print(Fore.YELLOW + "Retrying after 30s")
await asyncio.sleep(30)
except Exception as e:
if STATE.reconnect_with_credentials is not None:
return None
print_online = True
print(e)
print(Fore.YELLOW + "Retrying after 30s")
await asyncio.sleep(30)