-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathtest_identity.py
More file actions
443 lines (349 loc) · 14.1 KB
/
test_identity.py
File metadata and controls
443 lines (349 loc) · 14.1 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""Tests for AgentMesh Identity module."""
import pytest
from datetime import datetime, timedelta
from urllib.parse import urlparse
from agentmesh.identity import (
AgentIdentity,
AgentDID,
Credential,
CredentialManager,
ScopeChain,
DelegationLink,
HumanSponsor,
RiskScorer,
RiskScore,
SPIFFEIdentity,
)
class TestAgentDID:
"""Tests for AgentDID."""
def test_generate_did(self):
"""Test generating a new DID."""
did = AgentDID.generate("test-agent")
assert did.method == "mesh"
assert len(did.unique_id) == 32
assert str(did).startswith("did:mesh:")
def test_generate_did_with_org(self):
"""Test generating DID with organization."""
did = AgentDID.generate("test-agent", org="acme-corp")
assert did.method == "mesh"
assert str(did).startswith("did:mesh:")
def test_parse_did_string(self):
"""Test parsing a DID string."""
did_str = "did:mesh:abc123def456"
did = AgentDID.from_string(did_str)
assert did.unique_id == "abc123def456"
assert str(did) == did_str
def test_invalid_did_string(self):
"""Test parsing invalid DID raises error."""
with pytest.raises(ValueError):
AgentDID.from_string("invalid:did:format")
def test_did_hash(self):
"""Test DID is hashable for use in sets/dicts."""
did1 = AgentDID.generate("agent-1")
did2 = AgentDID.generate("agent-2")
did_set = {did1, did2}
assert len(did_set) == 2
class TestAgentIdentity:
"""Tests for AgentIdentity."""
def test_create_identity(self):
"""Test creating a new agent identity."""
identity = AgentIdentity.create(
name="test-agent",
sponsor="sponsor@example.com",
)
assert identity.name == "test-agent"
assert str(identity.did).startswith("did:mesh:")
assert identity.public_key is not None
assert identity.sponsor_email == "sponsor@example.com"
assert identity.status == "active"
def test_create_with_capabilities(self):
"""Test creating identity with capabilities."""
identity = AgentIdentity.create(
name="capable-agent",
sponsor="sponsor@example.com",
capabilities=["read", "write", "execute"],
)
assert set(identity.capabilities) == {"read", "write", "execute"}
def test_identity_unique(self):
"""Test that each identity is unique."""
id1 = AgentIdentity.create("agent-1", "s@e.com")
id2 = AgentIdentity.create("agent-2", "s@e.com")
assert str(id1.did) != str(id2.did)
assert id1.public_key != id2.public_key
def test_sign_and_verify(self):
"""Test signing and verification."""
identity = AgentIdentity.create("signer", "s@e.com")
message = b"Hello, AgentMesh!"
signature = identity.sign(message)
assert identity.verify_signature(message, signature)
assert not identity.verify_signature(b"Modified message", signature)
def test_delegate_creates_child(self):
"""Test delegating to create sub-agent."""
parent = AgentIdentity.create(
"parent-agent",
"s@e.com",
capabilities=["read", "write", "delete"],
)
child = parent.delegate(
"child-agent",
capabilities=["read", "write"],
)
assert child.parent_did == str(parent.did)
assert set(child.capabilities) == {"read", "write"}
assert child.delegation_depth == parent.delegation_depth + 1
def test_delegate_capability_narrowing(self):
"""Test that delegation cannot widen capabilities."""
parent = AgentIdentity.create(
"parent-agent",
"s@e.com",
capabilities=["read"],
)
with pytest.raises(ValueError):
parent.delegate(
"child-agent",
capabilities=["read", "write"], # Can't add "write"
)
class TestCredentials:
"""Tests for Credential and CredentialManager."""
def test_credential_creation(self):
"""Test creating credentials."""
cred = Credential.issue(
agent_did="did:mesh:test123",
capabilities=["read", "write"],
)
assert cred.agent_did == "did:mesh:test123"
assert cred.is_valid()
def test_credential_default_ttl(self):
"""Test credential has 15-minute default TTL."""
cred = Credential.issue(agent_did="did:mesh:test")
# Should expire in approximately 15 minutes
time_diff = cred.expires_at - datetime.utcnow()
assert 14 * 60 < time_diff.total_seconds() < 16 * 60
def test_credential_expiry(self):
"""Test credential expiration."""
cred = Credential.issue(
agent_did="did:mesh:test",
ttl_seconds=-60, # Expired 1 minute ago
)
assert not cred.is_valid()
def test_credential_manager_issue(self):
"""Test credential manager issues credentials."""
manager = CredentialManager()
cred = manager.issue("did:mesh:test", capabilities=["read"])
assert cred is not None
assert cred.agent_did == "did:mesh:test"
assert manager.validate(cred.token)
def test_credential_manager_revoke(self):
"""Test credential revocation."""
manager = CredentialManager()
cred = manager.issue("did:mesh:test")
assert manager.validate(cred.token)
manager.revoke(cred.credential_id, "test revocation")
assert not manager.validate(cred.token)
def test_credential_manager_rotate(self):
"""Test credential rotation."""
manager = CredentialManager()
old_cred = manager.issue("did:mesh:test")
new_cred = manager.rotate(old_cred.credential_id)
assert new_cred.credential_id != old_cred.credential_id
assert not manager.validate(old_cred.token)
assert manager.validate(new_cred.token)
class TestDelegation:
"""Tests for ScopeChain."""
def test_create_chain(self):
"""Test creating scope chain."""
chain, root_link = ScopeChain.create_root(
sponsor_email="sponsor@example.com",
root_agent_did="did:mesh:root123",
capabilities=["read", "write", "admin"],
)
assert chain.root_sponsor_email == "sponsor@example.com"
assert chain.leaf_did == "did:mesh:root123"
assert len(chain.links) == 0
def test_add_delegation_link(self):
"""Test adding delegation links."""
chain, root_link = ScopeChain.create_root(
sponsor_email="sponsor@example.com",
root_agent_did="did:mesh:root",
capabilities=["read", "write"],
)
# Add the root link
chain.add_link(root_link)
# Create a child link
import uuid
child_link = DelegationLink(
link_id=f"link_{uuid.uuid4().hex[:12]}",
depth=1,
parent_did="did:mesh:root",
child_did="did:mesh:child",
parent_capabilities=["read", "write"],
delegated_capabilities=["read"],
parent_signature="test_signature",
link_hash="",
previous_link_hash=root_link.link_hash,
)
child_link.link_hash = child_link.compute_hash()
chain.add_link(child_link)
assert len(chain.links) == 2
assert chain.leaf_capabilities == ["read"]
def test_capability_narrowing_enforced(self):
"""Test that capabilities can only narrow, never widen."""
chain, root_link = ScopeChain.create_root(
sponsor_email="sponsor@example.com",
root_agent_did="did:mesh:root",
capabilities=["read"],
)
chain.add_link(root_link)
# Create a child link with proper narrowing
import uuid
child_link = DelegationLink(
link_id=f"link_{uuid.uuid4().hex[:12]}",
depth=1,
parent_did="did:mesh:root",
child_did="did:mesh:child",
parent_capabilities=["read"],
delegated_capabilities=["read"],
parent_signature="test_signature",
link_hash="",
previous_link_hash=root_link.link_hash,
)
child_link.link_hash = child_link.compute_hash()
chain.add_link(child_link)
# Attempt to widen should fail
grandchild_link = DelegationLink(
link_id=f"link_{uuid.uuid4().hex[:12]}",
depth=2,
parent_did="did:mesh:child",
child_did="did:mesh:grandchild",
parent_capabilities=["read"],
delegated_capabilities=["read", "write"], # Can't add "write"
parent_signature="test_signature",
link_hash="",
previous_link_hash=child_link.link_hash,
)
grandchild_link.link_hash = grandchild_link.compute_hash()
with pytest.raises(ValueError):
chain.add_link(grandchild_link)
def test_verify_chain(self):
"""Test chain verification."""
chain, root_link = ScopeChain.create_root(
sponsor_email="sponsor@example.com",
root_agent_did="did:mesh:root",
capabilities=["read", "write"],
)
chain.add_link(root_link)
# Create a child link
import uuid
child_link = DelegationLink(
link_id=f"link_{uuid.uuid4().hex[:12]}",
depth=1,
parent_did="did:mesh:root",
child_did="did:mesh:child",
parent_capabilities=["read", "write"],
delegated_capabilities=["read"],
parent_signature="test_signature",
link_hash="",
previous_link_hash=root_link.link_hash,
)
child_link.link_hash = child_link.compute_hash()
chain.add_link(child_link)
is_valid, error = chain.verify()
assert is_valid
class TestSponsor:
"""Tests for HumanSponsor."""
def test_create_sponsor(self):
"""Test creating a human sponsor."""
sponsor = HumanSponsor.create(
email="sponsor@example.com",
name="Test Sponsor",
)
assert sponsor.email == "sponsor@example.com"
assert sponsor.status == "active"
def test_sponsor_agent(self):
"""Test sponsoring an agent."""
sponsor = HumanSponsor.create(
email="sponsor@example.com",
name="Test Sponsor",
)
sponsor.add_agent("did:mesh:test")
assert "did:mesh:test" in sponsor.agent_dids
def test_revoke_sponsorship(self):
"""Test revoking sponsorship."""
sponsor = HumanSponsor.create(
email="sponsor@example.com",
name="Test Sponsor",
)
sponsor.add_agent("did:mesh:test")
sponsor.remove_agent("did:mesh:test")
assert "did:mesh:test" not in sponsor.agent_dids
class TestRiskScoring:
"""Tests for RiskScorer."""
def test_initial_score(self):
"""Test initial risk score."""
scorer = RiskScorer()
score = scorer.get_score("did:mesh:new-agent")
assert isinstance(score, RiskScore)
assert score.total_score >= 0
assert score.total_score <= 1000
def test_add_risk_event(self):
"""Test adding risk events affects behavior score."""
scorer = RiskScorer()
initial = scorer.get_score("did:mesh:test")
initial_behavior = initial.behavior_score
# Add multiple high-risk signals
from agentmesh.identity.risk import RiskSignal
for _ in range(5):
scorer.add_signal(
agent_did="did:mesh:test",
signal=RiskSignal(
signal_type="behavior.suspicious_activity",
severity="high",
value=0.9,
),
)
after = scorer.recalculate("did:mesh:test")
# Behavior score should decrease due to negative signals
assert after.behavior_score < initial_behavior
def test_risk_decay(self):
"""Test that risk decays over time."""
scorer = RiskScorer()
from agentmesh.identity.risk import RiskSignal
scorer.add_signal(
agent_did="did:mesh:test",
signal=RiskSignal(
signal_type="minor_violation",
severity="low",
value=0.3,
),
)
# Risk should be present
score = scorer.recalculate("did:mesh:test")
assert score.total_score >= 0
class TestSPIFFE:
"""Tests for SPIFFE identity."""
def test_create_spiffe_identity(self):
"""Test creating SPIFFE identity."""
spiffe = SPIFFEIdentity.create(
trust_domain="agentmesh.io",
agent_did="did:mesh:test123",
agent_name="test-agent",
)
parsed = urlparse(spiffe.spiffe_id)
assert parsed.scheme == "spiffe"
assert parsed.hostname == "agentmesh.io"
assert parsed.path.startswith("/")
assert spiffe.trust_domain == "agentmesh.io"
def test_spiffe_id_format(self):
"""Test SPIFFE ID follows standard format."""
spiffe = SPIFFEIdentity.create(
trust_domain="example.com",
agent_did="did:mesh:abc",
agent_name="test-agent",
)
# SPIFFE ID should be: spiffe://<trust-domain>/<path>
parsed = urlparse(spiffe.spiffe_id)
assert parsed.scheme == "spiffe"
assert parsed.hostname == "example.com"
assert parsed.path.startswith("/")