-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
358 lines (302 loc) · 9.24 KB
/
Copy pathconftest.py
File metadata and controls
358 lines (302 loc) · 9.24 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
"""Shared pytest fixtures and configuration for all tests."""
from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
import subprocess
import textwrap
from typing import AsyncGenerator, Awaitable, Callable
import pytest
from redis.asyncio import Redis
from mas_agent import TlsClientConfig
from mas_gateway.config import GatewaySettings, RedisSettings
from mas_server import AgentDefinition, MASServer, MASServerSettings, TlsConfig
# Use anyio for async test support
pytestmark = pytest.mark.asyncio
def _run_openssl(args: list[str], *, cwd: Path) -> None:
proc = subprocess.run(
["openssl", *args],
cwd=str(cwd),
check=False,
capture_output=True,
text=True,
)
if proc.returncode != 0:
raise RuntimeError(
"openssl failed: "
+ " ".join(args)
+ "\nstdout:\n"
+ proc.stdout
+ "\nstderr:\n"
+ proc.stderr
)
def _write_text(path: Path, content: str) -> None:
path.write_text(content, encoding="utf-8")
@dataclass(slots=True)
class TestTlsPaths:
base_dir: Path
ca_pem: str
ca_key: str
server_cert: str
server_key: str
def client(self, agent_id: str) -> TlsClientConfig:
self._ensure_agent_cert(agent_id)
return TlsClientConfig(
root_ca_path=self.ca_pem,
client_cert_path=str(self.base_dir / f"{agent_id}.pem"),
client_key_path=str(self.base_dir / f"{agent_id}.key"),
)
def _ensure_agent_cert(self, agent_id: str) -> None:
cert_path = self.base_dir / f"{agent_id}.pem"
key_path = self.base_dir / f"{agent_id}.key"
if cert_path.exists() and key_path.exists():
return
csr_path = self.base_dir / f"{agent_id}.csr"
conf_path = self.base_dir / f"{agent_id}.cnf"
_write_text(
conf_path,
textwrap.dedent(
f"""
[req]
distinguished_name = dn
req_extensions = req_ext
prompt = no
[dn]
CN = {agent_id}
[req_ext]
keyUsage = critical, digitalSignature, keyEncipherment
extendedKeyUsage = clientAuth
subjectAltName = @alt_names
[alt_names]
URI.1 = spiffe://mas/agent/{agent_id}
"""
).lstrip(),
)
_run_openssl(["genrsa", "-out", str(key_path), "2048"], cwd=self.base_dir)
_run_openssl(
[
"req",
"-new",
"-key",
str(key_path),
"-out",
str(csr_path),
"-config",
str(conf_path),
],
cwd=self.base_dir,
)
_run_openssl(
[
"x509",
"-req",
"-in",
str(csr_path),
"-CA",
self.ca_pem,
"-CAkey",
self.ca_key,
"-CAcreateserial",
"-out",
str(cert_path),
"-days",
"3650",
"-sha256",
"-extensions",
"req_ext",
"-extfile",
str(conf_path),
],
cwd=self.base_dir,
)
@pytest.fixture
async def redis():
"""
Redis connection fixture.
Provides a Redis connection that is cleaned up after each test.
Flushes the database before and after each test to ensure isolation.
"""
r = Redis.from_url("redis://localhost:6379", decode_responses=True)
# Ensure a clean DB at the start of each test.
await r.flushdb()
yield r
# Cleanup
await r.flushdb()
await r.aclose() # type: ignore[unresolved-attribute]
@pytest.fixture(autouse=True)
async def cleanup_agent_keys():
"""
Auto-use fixture to clean up Redis agent keys and streams before each test.
This ensures tests don't interfere with each other by cleaning up:
- agent registration keys (agent:*)
- agent state keys (agent.state:*)
- agent delivery streams (agent.stream:*)
- gateway streams (mas.gateway.*)
This runs before the redis fixture cleanup, so it's safe for tests
that use the redis fixture.
"""
redis = Redis.from_url("redis://localhost:6379", decode_responses=True)
# Collect all keys/streams to delete
keys_to_delete = []
# Agent registration and heartbeat keys
async for key in redis.scan_iter("agent:*"):
keys_to_delete.append(key)
# Agent state keys
async for key in redis.scan_iter("agent.state:*"):
keys_to_delete.append(key)
# Agent delivery streams (including instance-specific streams)
async for key in redis.scan_iter("agent.stream:*"):
keys_to_delete.append(key)
# Gateway streams (ingress, dlq, etc.)
async for key in redis.scan_iter("mas.gateway.*"):
keys_to_delete.append(key)
# DLQ streams
async for key in redis.scan_iter("dlq:*"):
keys_to_delete.append(key)
# Rate limit keys
async for key in redis.scan_iter("rate_limit:*"):
keys_to_delete.append(key)
async for key in redis.scan_iter("ratelimit:*"):
keys_to_delete.append(key)
# ACL keys
async for key in redis.scan_iter("acl:*"):
keys_to_delete.append(key)
# Audit keys
async for key in redis.scan_iter("audit:*"):
keys_to_delete.append(key)
# Circuit breaker keys
async for key in redis.scan_iter("circuit:*"):
keys_to_delete.append(key)
if keys_to_delete:
await redis.delete(*keys_to_delete)
await redis.aclose() # type: ignore[unresolved-attribute]
yield
@pytest.fixture
def test_tls(tmp_path_factory) -> TestTlsPaths:
base_dir = tmp_path_factory.mktemp("certs")
ca_key = base_dir / "ca.key"
ca_pem = base_dir / "ca.pem"
server_key = base_dir / "server.key"
server_csr = base_dir / "server.csr"
server_cert = base_dir / "server.pem"
server_conf = base_dir / "server.cnf"
_run_openssl(["genrsa", "-out", str(ca_key), "2048"], cwd=base_dir)
_run_openssl(
[
"req",
"-x509",
"-new",
"-nodes",
"-key",
str(ca_key),
"-sha256",
"-days",
"3650",
"-subj",
"/CN=MAS Test CA",
"-out",
str(ca_pem),
],
cwd=base_dir,
)
_write_text(
server_conf,
textwrap.dedent(
"""
[req]
distinguished_name = dn
req_extensions = req_ext
prompt = no
[dn]
CN = localhost
[req_ext]
keyUsage = critical, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
DNS.1 = localhost
IP.1 = 127.0.0.1
"""
).lstrip(),
)
_run_openssl(["genrsa", "-out", str(server_key), "2048"], cwd=base_dir)
_run_openssl(
[
"req",
"-new",
"-key",
str(server_key),
"-out",
str(server_csr),
"-config",
str(server_conf),
],
cwd=base_dir,
)
_run_openssl(
[
"x509",
"-req",
"-in",
str(server_csr),
"-CA",
str(ca_pem),
"-CAkey",
str(ca_key),
"-CAcreateserial",
"-out",
str(server_cert),
"-days",
"3650",
"-sha256",
"-extensions",
"req_ext",
"-extfile",
str(server_conf),
],
cwd=base_dir,
)
tls = TestTlsPaths(
base_dir=base_dir,
ca_pem=str(ca_pem),
ca_key=str(ca_key),
server_cert=str(server_cert),
server_key=str(server_key),
)
# Pre-generate the certs used across the test suite.
for agent_id in [
"sender",
"worker",
"requester",
"responder",
"discoverer",
]:
tls._ensure_agent_cert(agent_id)
return tls
@pytest.fixture
async def mas_server_factory(
test_tls: TestTlsPaths,
) -> AsyncGenerator[
Callable[[dict[str, AgentDefinition] | None], Awaitable[MASServer]],
None,
]:
servers: list[MASServer] = []
async def _start(agents: dict[str, AgentDefinition] | None = None) -> MASServer:
agent_defs = agents or {}
settings = MASServerSettings(
redis_url="redis://localhost:6379",
listen_addr="127.0.0.1:0",
tls=TlsConfig(
server_cert_path=test_tls.server_cert,
server_key_path=test_tls.server_key,
client_ca_path=test_tls.ca_pem,
),
agents=agent_defs,
)
gateway = GatewaySettings(redis=RedisSettings(url="redis://localhost:6379"))
server = MASServer(settings=settings, gateway=gateway)
await server.start()
servers.append(server)
return server
yield _start
for server in servers:
await server.stop()