This repository was archived by the owner on Aug 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.py
More file actions
327 lines (260 loc) · 10.9 KB
/
Copy pathmiddleware.py
File metadata and controls
327 lines (260 loc) · 10.9 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
"""Gateway Middleware — authentication, rate limiting, CORS, logging, timeout.
Each middleware is a callable that wraps the request handler. Middleware can
short-circuit (return a response directly) or pass through to the next handler.
All middleware uses only the Python standard library.
"""
from __future__ import annotations
import json
import logging
import re
import threading
import time
from collections import defaultdict
from dataclasses import dataclass, field
from typing import Any, Callable, Optional
logger = logging.getLogger("fleet.gateway.middleware")
# ---------------------------------------------------------------------------
# Data helpers
# ---------------------------------------------------------------------------
@dataclass
class Request:
"""Lightweight representation of an incoming HTTP request."""
method: str
path: str
headers: dict[str, str] = field(default_factory=dict)
body: Optional[bytes] = None
client_ip: str = "unknown"
start_time: float = field(default_factory=time.monotonic)
@property
def content_type(self) -> str:
return self.headers.get("content-type", "")
@dataclass
class Response:
"""Lightweight representation of an HTTP response."""
status: int = 200
body: bytes | str = b""
headers: dict[str, str] = field(default_factory=dict)
def json(self) -> dict[str, Any]:
if isinstance(self.body, bytes):
return json.loads(self.body)
return json.loads(self.body)
def set_json(self, data: Any, status: int | None = None) -> "Response":
payload = json.dumps(data).encode()
self.body = payload
if status is not None:
self.status = status
self.headers["content-type"] = "application/json"
return self
Handler = Callable[[Request], Response]
Middleware = Callable[[Handler], Handler]
# ---------------------------------------------------------------------------
# Auth Middleware
# ---------------------------------------------------------------------------
class AuthMiddleware:
"""Bearer-token authentication middleware.
Validates the ``Authorization: Bearer <token>`` header against a
pre-configured shared fleet secret. Skips auth for paths in the
*skip_paths* whitelist.
"""
def __init__(
self,
secret: str = "fleet-secret-token",
skip_paths: Optional[set[str]] = None,
) -> None:
self.secret = secret
self.skip_paths: set[str] = skip_paths or {"/health", "/agents", "/registry"}
def __call__(self, next_handler: Handler) -> Handler:
def handle(req: Request) -> Response:
# Allow health / discovery endpoints without auth
if req.path in self.skip_paths:
return next_handler(req)
auth_header = req.headers.get("authorization", "")
if not auth_header.startswith("Bearer "):
return Response(status=401).set_json({
"error": "missing or invalid authorization header",
})
token = auth_header[len("Bearer "):]
if token != self.secret:
logger.warning("auth failed for %s from %s", req.path, req.client_ip)
return Response(status=403).set_json({
"error": "invalid bearer token",
})
return next_handler(req)
return handle
# ---------------------------------------------------------------------------
# Rate Limiter (token-bucket, per client IP)
# ---------------------------------------------------------------------------
class RateLimiter:
"""Token-bucket rate limiter.
Each client IP gets a bucket that refills at *tokens_per_minute* / 60
tokens per second. A burst of up to *burst_size* tokens is allowed.
"""
def __init__(
self,
tokens_per_minute: int = 60,
burst_size: int = 10,
) -> None:
self.tokens_per_minute = tokens_per_minute
self.burst_size = burst_size
self.refill_rate = tokens_per_minute / 60.0
# {client_ip: {"tokens": float, "last": float}}
self._buckets: dict[str, dict[str, float]] = defaultdict(
lambda: {"tokens": float(burst_size), "last": time.monotonic()}
)
self._lock = threading.Lock()
def _consume(self, client_ip: str) -> bool:
"""Try to consume one token. Returns True on success."""
with self._lock:
bucket = self._buckets[client_ip]
now = time.monotonic()
elapsed = now - bucket["last"]
bucket["tokens"] = min(
self.burst_size,
bucket["tokens"] + elapsed * self.refill_rate,
)
bucket["last"] = now
if bucket["tokens"] >= 1.0:
bucket["tokens"] -= 1.0
return True
return False
def __call__(self, next_handler: Handler) -> Handler:
def handle(req: Request) -> Response:
if not self._consume(req.client_ip):
logger.info("rate limited %s from %s", req.path, req.client_ip)
return Response(status=429).set_json({
"error": "rate limit exceeded",
"retry_after": 60,
})
return next_handler(req)
return handle
def reset(self) -> None:
"""Clear all buckets (useful for testing)."""
with self._lock:
self._buckets.clear()
# ---------------------------------------------------------------------------
# CORS Middleware
# ---------------------------------------------------------------------------
class CORSMiddleware:
"""Add Cross-Origin Resource Sharing headers to responses.
Optionally handles preflight OPTIONS requests automatically.
"""
def __init__(
self,
allow_origins: str = "*",
allow_methods: str = "GET, POST, PUT, DELETE, OPTIONS",
allow_headers: str = "Content-Type, Authorization",
max_age: int = 86400,
) -> None:
self.allow_origins = allow_origins
self.allow_methods = allow_methods
self.allow_headers = allow_headers
self.max_age = max_age
def _cors_headers(self) -> dict[str, str]:
return {
"Access-Control-Allow-Origin": self.allow_origins,
"Access-Control-Allow-Methods": self.allow_methods,
"Access-Control-Allow-Headers": self.allow_headers,
"Access-Control-Max-Age": str(self.max_age),
}
def __call__(self, next_handler: Handler) -> Handler:
def handle(req: Request) -> Response:
# Handle preflight
if req.method == "OPTIONS":
resp = Response(status=204, body=b"")
resp.headers.update(self._cors_headers())
return resp
resp = next_handler(req)
resp.headers.update(self._cors_headers())
return resp
return handle
# ---------------------------------------------------------------------------
# Logging Middleware
# ---------------------------------------------------------------------------
class LoggingMiddleware:
"""Log every request with method, path, status, and latency."""
def __init__(self) -> None:
self.request_log: list[dict[str, Any]] = []
self._lock = threading.Lock()
self._counter = 0
def __call__(self, next_handler: Handler) -> Handler:
def handle(req: Request) -> Response:
resp = next_handler(req)
elapsed_ms = (time.monotonic() - req.start_time) * 1000
entry = {
"id": self._next_id(),
"method": req.method,
"path": req.path,
"status": resp.status,
"latency_ms": round(elapsed_ms, 2),
"client_ip": req.client_ip,
"timestamp": time.time(),
}
with self._lock:
self.request_log.append(entry)
# Keep last 1000 entries
if len(self.request_log) > 1000:
self.request_log = self.request_log[-1000:]
logger.info(
"%s %s → %d (%.1fms)",
req.method, req.path, resp.status, elapsed_ms,
)
return resp
return handle
def _next_id(self) -> int:
self._counter += 1
return self._counter
def get_log(self) -> list[dict[str, Any]]:
with self._lock:
return list(self.request_log)
def clear_log(self) -> None:
with self._lock:
self.request_log.clear()
# ---------------------------------------------------------------------------
# Timeout Middleware
# ---------------------------------------------------------------------------
class TimeoutMiddleware:
"""Per-request timeout wrapper.
Since we can't easily interrupt a synchronous function in pure stdlib
without ``signal.alarm`` (which only works in the main thread), this
middleware is a marker/tracker that other layers can consult. The actual
timeout enforcement happens in the gateway handler via
``socket.setdefaulttimeout`` / ``urllib.request`` timeout parameter.
"""
def __init__(self, default_timeout: float = 30.0) -> None:
self.default_timeout = default_timeout
# {path_prefix: timeout_seconds}
self._timeouts: dict[str, float] = {}
def set_timeout(self, path_prefix: str, timeout: float) -> None:
"""Set a per-path-prefix timeout."""
self._timeouts[path_prefix] = timeout
def get_timeout(self, path: str) -> float:
"""Return the timeout configured for *path*, or the default.
If multiple prefixes match, the longest (most specific) prefix wins.
"""
best_timeout = self.default_timeout
best_len = 0
for prefix, tout in self._timeouts.items():
if path.startswith(prefix) and len(prefix) > best_len:
best_timeout = tout
best_len = len(prefix)
return best_timeout
def __call__(self, next_handler: Handler) -> Handler:
# This middleware is pass-through — the gateway uses get_timeout()
# when making upstream requests.
return next_handler
# ---------------------------------------------------------------------------
# Middleware Chain Builder
# ---------------------------------------------------------------------------
class MiddlewareChain:
"""Assemble an ordered chain of middleware around a final handler."""
def __init__(self) -> None:
self._middlewares: list[Middleware] = []
def add(self, middleware: Middleware) -> "MiddlewareChain":
self._middlewares.append(middleware)
return self
def build(self, handler: Handler) -> Handler:
"""Wrap *handler* with all middleware (outermost first)."""
wrapped = handler
for mw in reversed(self._middlewares):
wrapped = mw(wrapped)
return wrapped