kanoniv-agent-auth 0.3.0

Sudo for AI agents - cryptographic delegation, Ed25519 identity, and signed audit trails
Documentation
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//! Provenance entries - signed audit trail for agent actions.
//!
//! Each provenance entry records what an agent did, to which entities,
//! with a cryptographic signature proving the agent's authorship.
//! Entries form a directed acyclic graph (DAG) via parent_ids, enabling
//! tamper-evident chaining of agent actions.

use serde::{Deserialize, Serialize};

use crate::identity::AgentKeyPair;
use crate::signing::SignedMessage;
use crate::CryptoError;

/// The type of action recorded in a provenance entry.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ActionType {
    Resolve,
    Merge,
    Split,
    Mutate,
    Ingest,
    Delegate,
    Revoke,
    Custom(String),
}

impl std::fmt::Display for ActionType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ActionType::Resolve => write!(f, "resolve"),
            ActionType::Merge => write!(f, "merge"),
            ActionType::Split => write!(f, "split"),
            ActionType::Mutate => write!(f, "mutate"),
            ActionType::Ingest => write!(f, "ingest"),
            ActionType::Delegate => write!(f, "delegate"),
            ActionType::Revoke => write!(f, "revoke"),
            ActionType::Custom(s) => write!(f, "custom:{}", s),
        }
    }
}

/// A signed provenance entry in the audit chain.
///
/// Entries link to parents via `parent_ids`, forming a DAG.
/// Use `content_hash()` as the parent_id when chaining entries.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProvenanceEntry {
    /// The DID of the agent that performed the action
    pub agent_did: String,
    /// What action was performed
    pub action: ActionType,
    /// Entity IDs affected by this action
    pub entity_ids: Vec<String>,
    /// Parent provenance entry content hashes (for DAG chaining)
    pub parent_ids: Vec<String>,
    /// Additional context
    pub metadata: serde_json::Value,
    /// The signed envelope proving authenticity
    pub signed_envelope: SignedMessage,
}

impl ProvenanceEntry {
    /// Create and sign a new provenance entry.
    pub fn create(
        keypair: &AgentKeyPair,
        action: ActionType,
        entity_ids: Vec<String>,
        parent_ids: Vec<String>,
        metadata: serde_json::Value,
    ) -> Result<Self, CryptoError> {
        let identity = keypair.identity();

        let payload = serde_json::json!({
            "agent_did": identity.did,
            "action": action,
            "entity_ids": entity_ids,
            "parent_ids": parent_ids,
            "metadata": metadata,
        });

        let signed_envelope = SignedMessage::sign(keypair, payload)?;

        Ok(Self {
            agent_did: identity.did,
            action,
            entity_ids,
            parent_ids,
            metadata,
            signed_envelope,
        })
    }

    /// Verify this provenance entry: signature AND outer field integrity.
    ///
    /// Checks that:
    /// 1. The signature is valid for the given identity
    /// 2. The outer fields (agent_did, action, entity_ids, parent_ids, metadata)
    ///    match what was actually signed in the envelope payload
    ///
    /// This prevents tampering with outer fields after signing.
    pub fn verify(&self, identity: &crate::AgentIdentity) -> Result<(), CryptoError> {
        // 1. Verify the cryptographic signature
        self.signed_envelope.verify(identity)?;

        // 2. Verify outer fields match the signed payload
        let payload = &self.signed_envelope.payload;

        if payload.get("agent_did").and_then(|v| v.as_str()) != Some(&self.agent_did) {
            return Err(CryptoError::IntegrityMismatch("agent_did".into()));
        }

        // Compare action: serialize both to JSON for consistent comparison
        let signed_action = payload.get("action");
        let outer_action = serde_json::to_value(&self.action).ok();
        if signed_action != outer_action.as_ref() {
            return Err(CryptoError::IntegrityMismatch("action".into()));
        }

        let signed_entities = payload.get("entity_ids");
        let outer_entities = serde_json::to_value(&self.entity_ids).ok();
        if signed_entities != outer_entities.as_ref() {
            return Err(CryptoError::IntegrityMismatch("entity_ids".into()));
        }

        let signed_parents = payload.get("parent_ids");
        let outer_parents = serde_json::to_value(&self.parent_ids).ok();
        if signed_parents != outer_parents.as_ref() {
            return Err(CryptoError::IntegrityMismatch("parent_ids".into()));
        }

        if payload.get("metadata") != Some(&self.metadata) {
            return Err(CryptoError::IntegrityMismatch("metadata".into()));
        }

        Ok(())
    }

    /// Verify only the cryptographic signature, without checking field integrity.
    ///
    /// Use `verify()` instead unless you have a specific reason to skip integrity checks.
    pub fn verify_signature_only(
        &self,
        identity: &crate::AgentIdentity,
    ) -> Result<(), CryptoError> {
        self.signed_envelope.verify(identity)
    }

    /// Get the content hash of this entry (usable as a parent_id for chaining).
    pub fn content_hash(&self) -> String {
        self.signed_envelope.content_hash()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn test_keypair() -> AgentKeyPair {
        AgentKeyPair::generate()
    }

    #[test]
    fn test_create_and_verify_provenance() {
        let kp = test_keypair();
        let entry = ProvenanceEntry::create(
            &kp,
            ActionType::Merge,
            vec!["entity-1".into(), "entity-2".into()],
            vec![],
            serde_json::json!({"reason": "duplicate detected"}),
        )
        .unwrap();

        assert_eq!(entry.agent_did, kp.identity().did);
        assert_eq!(entry.action, ActionType::Merge);
        assert_eq!(entry.entity_ids.len(), 2);
        assert!(entry.verify(&kp.identity()).is_ok());
    }

    #[test]
    fn test_provenance_chaining() {
        let kp = test_keypair();

        let entry1 = ProvenanceEntry::create(
            &kp,
            ActionType::Resolve,
            vec!["entity-1".into()],
            vec![],
            serde_json::json!({}),
        )
        .unwrap();

        let entry2 = ProvenanceEntry::create(
            &kp,
            ActionType::Merge,
            vec!["entity-1".into(), "entity-2".into()],
            vec![entry1.content_hash()],
            serde_json::json!({}),
        )
        .unwrap();

        assert_eq!(entry2.parent_ids.len(), 1);
        assert_eq!(entry2.parent_ids[0], entry1.content_hash());
        assert!(entry2.verify(&kp.identity()).is_ok());
    }

    #[test]
    fn test_tampered_entity_ids_fails_verify() {
        let kp = test_keypair();
        let mut entry = ProvenanceEntry::create(
            &kp,
            ActionType::Resolve,
            vec!["entity-1".into()],
            vec![],
            serde_json::json!({}),
        )
        .unwrap();

        entry.entity_ids = vec!["entity-999".into()];

        // verify() catches the tampered outer field
        assert!(matches!(
            entry.verify(&kp.identity()),
            Err(CryptoError::IntegrityMismatch(ref field)) if field == "entity_ids"
        ));

        // verify_signature_only() still passes (signature is valid)
        assert!(entry.verify_signature_only(&kp.identity()).is_ok());
    }

    #[test]
    fn test_tampered_agent_did_fails_verify() {
        let kp = test_keypair();
        let mut entry = ProvenanceEntry::create(
            &kp,
            ActionType::Resolve,
            vec!["entity-1".into()],
            vec![],
            serde_json::json!({}),
        )
        .unwrap();

        entry.agent_did = "did:agent:tampered".into();

        assert!(matches!(
            entry.verify(&kp.identity()),
            Err(CryptoError::IntegrityMismatch(ref field)) if field == "agent_did"
        ));
    }

    #[test]
    fn test_tampered_action_fails_verify() {
        let kp = test_keypair();
        let mut entry = ProvenanceEntry::create(
            &kp,
            ActionType::Resolve,
            vec!["entity-1".into()],
            vec![],
            serde_json::json!({}),
        )
        .unwrap();

        entry.action = ActionType::Merge;

        assert!(matches!(
            entry.verify(&kp.identity()),
            Err(CryptoError::IntegrityMismatch(ref field)) if field == "action"
        ));
    }

    #[test]
    fn test_tampered_metadata_fails_verify() {
        let kp = test_keypair();
        let mut entry = ProvenanceEntry::create(
            &kp,
            ActionType::Resolve,
            vec!["entity-1".into()],
            vec![],
            serde_json::json!({"original": true}),
        )
        .unwrap();

        entry.metadata = serde_json::json!({"tampered": true});

        assert!(matches!(
            entry.verify(&kp.identity()),
            Err(CryptoError::IntegrityMismatch(ref field)) if field == "metadata"
        ));
    }

    #[test]
    fn test_action_type_display() {
        assert_eq!(ActionType::Resolve.to_string(), "resolve");
        assert_eq!(ActionType::Merge.to_string(), "merge");
        assert_eq!(ActionType::Split.to_string(), "split");
        assert_eq!(ActionType::Custom("test".into()).to_string(), "custom:test");
    }

    #[test]
    fn test_action_type_serialization() {
        let action = ActionType::Merge;
        let json = serde_json::to_string(&action).unwrap();
        assert_eq!(json, "\"merge\"");

        let deserialized: ActionType = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, ActionType::Merge);
    }

    #[test]
    fn test_provenance_serialization() {
        let kp = test_keypair();
        let entry = ProvenanceEntry::create(
            &kp,
            ActionType::Ingest,
            vec!["e1".into()],
            vec![],
            serde_json::json!({"source": "crm"}),
        )
        .unwrap();

        let json = serde_json::to_string(&entry).unwrap();
        let restored: ProvenanceEntry = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.agent_did, entry.agent_did);
        assert_eq!(restored.action, entry.action);
        assert!(restored.verify(&kp.identity()).is_ok());
    }

    #[test]
    fn test_content_hash_deterministic() {
        let kp = test_keypair();
        let entry = ProvenanceEntry::create(
            &kp,
            ActionType::Resolve,
            vec!["e1".into()],
            vec![],
            serde_json::json!({}),
        )
        .unwrap();

        let h1 = entry.content_hash();
        let h2 = entry.content_hash();
        assert_eq!(h1, h2);
        assert_eq!(h1.len(), 64); // SHA-256 = 32 bytes = 64 hex chars
    }

    #[test]
    fn test_cross_agent_verification() {
        let kp_a = test_keypair();
        let kp_b = test_keypair();

        let entry = ProvenanceEntry::create(
            &kp_a,
            ActionType::Delegate,
            vec![],
            vec![],
            serde_json::json!({"delegated_to": kp_b.identity().did}),
        )
        .unwrap();

        // Agent A's identity verifies
        assert!(entry.verify(&kp_a.identity()).is_ok());
        // Agent B's identity does not
        assert!(entry.verify(&kp_b.identity()).is_err());
    }

    #[test]
    fn test_all_action_types_serialize_roundtrip() {
        let actions = vec![
            ActionType::Resolve,
            ActionType::Merge,
            ActionType::Split,
            ActionType::Mutate,
            ActionType::Ingest,
            ActionType::Delegate,
            ActionType::Revoke,
            ActionType::Custom("my_action".into()),
        ];
        for action in actions {
            let json = serde_json::to_string(&action).unwrap();
            let restored: ActionType = serde_json::from_str(&json).unwrap();
            assert_eq!(restored, action, "Roundtrip failed for {:?}", action);
        }
    }

    #[test]
    fn test_all_action_types_display() {
        assert_eq!(ActionType::Mutate.to_string(), "mutate");
        assert_eq!(ActionType::Ingest.to_string(), "ingest");
        assert_eq!(ActionType::Delegate.to_string(), "delegate");
        assert_eq!(ActionType::Revoke.to_string(), "revoke");
    }

    #[test]
    fn test_provenance_empty_entity_list() {
        let kp = test_keypair();
        let entry = ProvenanceEntry::create(
            &kp,
            ActionType::Resolve,
            vec![],
            vec![],
            serde_json::json!({}),
        )
        .unwrap();

        assert!(entry.entity_ids.is_empty());
        assert!(entry.verify(&kp.identity()).is_ok());
    }

    #[test]
    fn test_provenance_custom_action() {
        let kp = test_keypair();
        let entry = ProvenanceEntry::create(
            &kp,
            ActionType::Custom("audit_review".into()),
            vec!["e1".into()],
            vec![],
            serde_json::json!({}),
        )
        .unwrap();

        assert_eq!(entry.action, ActionType::Custom("audit_review".into()));
        assert!(entry.verify(&kp.identity()).is_ok());

        let json = serde_json::to_string(&entry).unwrap();
        let restored: ProvenanceEntry = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.action, ActionType::Custom("audit_review".into()));
    }

    #[test]
    fn test_provenance_multi_parent_chaining() {
        let kp = test_keypair();

        let e1 = ProvenanceEntry::create(
            &kp,
            ActionType::Resolve,
            vec!["a".into()],
            vec![],
            serde_json::json!({}),
        )
        .unwrap();

        let e2 = ProvenanceEntry::create(
            &kp,
            ActionType::Resolve,
            vec!["b".into()],
            vec![],
            serde_json::json!({}),
        )
        .unwrap();

        let e3 = ProvenanceEntry::create(
            &kp,
            ActionType::Merge,
            vec!["a".into(), "b".into()],
            vec![e1.content_hash(), e2.content_hash()],
            serde_json::json!({"merge_of": ["a", "b"]}),
        )
        .unwrap();

        assert_eq!(e3.parent_ids.len(), 2);
        assert!(e3.verify(&kp.identity()).is_ok());
    }

    #[test]
    fn test_different_entries_different_content_hashes() {
        let kp = test_keypair();

        let e1 = ProvenanceEntry::create(
            &kp,
            ActionType::Resolve,
            vec!["entity-1".into()],
            vec![],
            serde_json::json!({}),
        )
        .unwrap();

        let e2 = ProvenanceEntry::create(
            &kp,
            ActionType::Merge,
            vec!["entity-2".into()],
            vec![],
            serde_json::json!({}),
        )
        .unwrap();

        assert_ne!(e1.content_hash(), e2.content_hash());
    }

    #[test]
    fn test_provenance_metadata_preserved() {
        let kp = test_keypair();
        let meta = serde_json::json!({
            "source": "crm",
            "confidence": 0.95,
            "tags": ["duplicate", "auto-merged"]
        });
        let entry = ProvenanceEntry::create(
            &kp,
            ActionType::Merge,
            vec!["e1".into(), "e2".into()],
            vec![],
            meta.clone(),
        )
        .unwrap();

        assert_eq!(entry.metadata, meta);
        assert_eq!(entry.signed_envelope.payload["metadata"], meta);
    }

    #[test]
    fn test_provenance_agent_did_matches_keypair() {
        let kp = test_keypair();
        let entry = ProvenanceEntry::create(
            &kp,
            ActionType::Ingest,
            vec![],
            vec![],
            serde_json::json!({}),
        )
        .unwrap();

        assert_eq!(entry.agent_did, kp.identity().did);
        assert_eq!(
            entry.signed_envelope.payload["agent_did"].as_str().unwrap(),
            kp.identity().did
        );
        assert_eq!(entry.signed_envelope.signer_did, kp.identity().did);
    }
}