converge-policy 3.7.2

Cedar-based Policy Decision Point for Converge gate model
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
//! Delegation tokens — scoped, time-limited authority grants.
//!
//! A supervisor or human issues a delegation to grant an agent
//! temporary elevated authority for specific actions on specific resources.
//! Tokens are CBOR-encoded, Ed25519-signed, and base64-transported.

use base64::{Engine as _, engine::general_purpose};
use ciborium::{de, ser};
use ed25519_dalek::{Signature, Signer, SigningKey, VerifyingKey};
use serde::{Deserialize, Serialize};

use crate::types::DecideRequest;
use converge_core::{AuthorityLevel, FlowAction};
use converge_pack::{ActorId, PrincipalId};

/// Scoped, time-limited authority delegation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Delegation {
    /// Suggestor persona being granted authority
    pub sub: PrincipalId,
    /// Who delegated (supervisor persona or human identifier)
    pub issuer: ActorId,
    /// The authority level being granted
    pub delegated_authority: AuthorityLevel,
    /// Allowed actions (commit, promote, etc.)
    pub actions: Vec<FlowAction>,
    /// Resource scope pattern (e.g., `flow:quote-*`)
    pub resource_pattern: String,
    /// Optional spending cap
    pub max_amount: Option<i64>,
    /// Not-before (epoch seconds)
    pub nbf_epoch: i64,
    /// Expires (epoch seconds)
    pub exp_epoch: i64,
    /// Nonce for replay protection
    pub jti: String,
    /// Ed25519 signature over all fields except sig
    pub sig: Option<Vec<u8>>,
}

/// Request to issue a delegation token.
#[derive(Debug, Deserialize)]
pub struct IssueDelegationReq {
    pub sub: PrincipalId,
    pub issuer: ActorId,
    pub delegated_authority: AuthorityLevel,
    pub actions: Vec<FlowAction>,
    pub resource_pattern: String,
    pub max_amount: Option<i64>,
    pub nbf_epoch: i64,
    pub exp_epoch: i64,
    pub jti: String,
}

/// Response from issuing a delegation token.
#[derive(Debug, Serialize)]
pub struct IssueDelegationResp {
    pub delegation_b64: String,
    pub pubkey_b64: String,
}

fn sig_message(d: &Delegation) -> Result<Vec<u8>, String> {
    let mut to_sign = d.clone();
    to_sign.sig = None;
    let mut buf = Vec::new();
    ser::into_writer(&to_sign, &mut buf).map_err(|err| err.to_string())?;
    Ok(buf)
}

/// Issue a signed delegation token.
///
/// # Errors
///
/// Returns `Err` if CBOR serialization or signing fails.
pub fn issue(
    signing_key: &SigningKey,
    req: IssueDelegationReq,
) -> Result<IssueDelegationResp, String> {
    if req.sub.as_str().trim().is_empty() {
        return Err("delegation subject cannot be empty".to_string());
    }
    if req.issuer.as_str().trim().is_empty() {
        return Err("delegation issuer cannot be empty".to_string());
    }
    if req.actions.is_empty() {
        return Err("delegation must include at least one action".to_string());
    }
    if req.resource_pattern.trim().is_empty() {
        return Err("delegation resource_pattern cannot be empty".to_string());
    }
    if req.jti.trim().is_empty() {
        return Err("delegation jti cannot be empty".to_string());
    }
    if req.exp_epoch <= req.nbf_epoch {
        return Err("delegation exp_epoch must be later than nbf_epoch".to_string());
    }
    if let Some(max_amount) = req.max_amount {
        if max_amount < 0 {
            return Err("delegation max_amount cannot be negative".to_string());
        }
    }

    let mut del = Delegation {
        sub: req.sub,
        issuer: req.issuer,
        delegated_authority: req.delegated_authority,
        actions: req.actions,
        resource_pattern: req.resource_pattern,
        max_amount: req.max_amount,
        nbf_epoch: req.nbf_epoch,
        exp_epoch: req.exp_epoch,
        jti: req.jti,
        sig: None,
    };

    let msg = sig_message(&del)?;
    let sig: Signature = signing_key.sign(&msg);
    del.sig = Some(sig.to_bytes().to_vec());

    let mut buf = Vec::new();
    ser::into_writer(&del, &mut buf).map_err(|err| err.to_string())?;

    let verifying_key = signing_key.verifying_key();
    Ok(IssueDelegationResp {
        delegation_b64: general_purpose::STANDARD_NO_PAD.encode(&buf),
        pubkey_b64: general_purpose::STANDARD_NO_PAD.encode(verifying_key.to_bytes()),
    })
}

/// Verify a delegation token against the request.
///
/// Returns `Ok(true)` if the delegation is valid and covers the requested action,
/// `Ok(false)` if verification passes but constraints don't match,
/// `Err(reason)` if the token is malformed or signature fails.
///
/// # Errors
///
/// Returns `Err` if the token cannot be decoded, parsed, or the time source is invalid.
#[allow(clippy::cast_possible_wrap)]
pub fn verify(b64: &str, vkey: &VerifyingKey, req: &DecideRequest) -> Result<bool, String> {
    let raw = general_purpose::STANDARD_NO_PAD
        .decode(b64)
        .map_err(|err| format!("delegation decode failed: {err}"))?;
    let del: Delegation =
        de::from_reader(raw.as_slice()).map_err(|err| format!("delegation parse failed: {err}"))?;

    // Verify signature
    let msg = sig_message(&del)?;
    let sig_bytes = del
        .sig
        .clone()
        .ok_or_else(|| "delegation signature missing".to_string())?;
    let sig = Signature::from_slice(&sig_bytes)
        .map_err(|_| "delegation signature invalid".to_string())?;
    if vkey.verify_strict(&msg, &sig).is_err() {
        return Ok(false);
    }

    // Check time window
    let now_epoch = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_err(|err| format!("time source invalid: {err}"))?
        .as_secs() as i64;
    if now_epoch < del.nbf_epoch || now_epoch > del.exp_epoch {
        return Ok(false);
    }

    // Check subject matches principal
    if del.sub != req.principal.id {
        return Ok(false);
    }

    // Check action is allowed
    if !del.actions.contains(&req.action) {
        return Ok(false);
    }

    // Check resource matches pattern (prefix match; * is wildcard suffix)
    let pattern = del.resource_pattern.trim_end_matches('*');
    if !req.resource.id.starts_with(pattern) {
        return Ok(false);
    }

    // Check amount cap
    if let Some(max) = del.max_amount {
        if let Some(ref ctx) = req.context {
            if let Some(amount) = ctx.amount {
                if amount > max {
                    return Ok(false);
                }
            }
        }
    }

    Ok(true)
}

#[cfg(test)]
mod tests {
    use super::*;
    use converge_core::FlowPhase;
    use converge_pack::ResourceKind;

    fn valid_req() -> IssueDelegationReq {
        IssueDelegationReq {
            sub: "agent:finance".into(),
            issuer: "human:cfo".into(),
            delegated_authority: AuthorityLevel::Supervisory,
            actions: vec![FlowAction::Commit],
            resource_pattern: "flow:quote-*".into(),
            max_amount: Some(50_000),
            nbf_epoch: 1_000_000,
            exp_epoch: 2_000_000,
            jti: "nonce-1".into(),
        }
    }

    fn signing_key() -> SigningKey {
        SigningKey::from_bytes(&[42u8; 32])
    }

    #[test]
    fn issue_succeeds_with_valid_request() {
        let key = signing_key();
        let resp = issue(&key, valid_req());
        assert!(resp.is_ok());
        let resp = resp.unwrap();
        assert!(!resp.delegation_b64.is_empty());
        assert!(!resp.pubkey_b64.is_empty());
    }

    #[test]
    fn issue_rejects_empty_subject() {
        let key = signing_key();
        let mut req = valid_req();
        req.sub = "  ".into();
        let err = issue(&key, req).unwrap_err();
        assert!(err.contains("subject"));
    }

    #[test]
    fn issue_rejects_empty_issuer() {
        let key = signing_key();
        let mut req = valid_req();
        req.issuer = "".into();
        let err = issue(&key, req).unwrap_err();
        assert!(err.contains("issuer"));
    }

    #[test]
    fn issue_rejects_no_actions() {
        let key = signing_key();
        let mut req = valid_req();
        req.actions = vec![];
        let err = issue(&key, req).unwrap_err();
        assert!(err.contains("action"));
    }

    #[test]
    fn issue_rejects_empty_resource_pattern() {
        let key = signing_key();
        let mut req = valid_req();
        req.resource_pattern = "   ".into();
        let err = issue(&key, req).unwrap_err();
        assert!(err.contains("resource_pattern"));
    }

    #[test]
    fn issue_rejects_empty_jti() {
        let key = signing_key();
        let mut req = valid_req();
        req.jti = "".into();
        let err = issue(&key, req).unwrap_err();
        assert!(err.contains("jti"));
    }

    #[test]
    fn issue_rejects_exp_before_nbf() {
        let key = signing_key();
        let mut req = valid_req();
        req.exp_epoch = req.nbf_epoch;
        let err = issue(&key, req).unwrap_err();
        assert!(err.contains("exp_epoch"));
    }

    #[test]
    fn issue_rejects_negative_max_amount() {
        let key = signing_key();
        let mut req = valid_req();
        req.max_amount = Some(-1);
        let err = issue(&key, req).unwrap_err();
        assert!(err.contains("max_amount"));
    }

    #[test]
    fn verify_roundtrip_valid_token() {
        let key = signing_key();
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64;

        let req = IssueDelegationReq {
            sub: "agent:finance".into(),
            issuer: "human:cfo".into(),
            delegated_authority: AuthorityLevel::Supervisory,
            actions: vec![FlowAction::Commit],
            resource_pattern: "flow:quote-*".into(),
            max_amount: Some(50_000),
            nbf_epoch: now - 100,
            exp_epoch: now + 3600,
            jti: "nonce-rt".into(),
        };

        let resp = issue(&key, req).unwrap();
        let vkey = key.verifying_key();

        let decide_req = DecideRequest {
            principal: crate::types::PrincipalIn {
                id: "agent:finance".into(),
                authority: AuthorityLevel::Supervisory,
                domains: vec!["finance".into()],
                policy_version: None,
            },
            resource: crate::types::ResourceIn {
                id: "flow:quote-2025-001".into(),
                resource_type: Some(ResourceKind::new("quote")),
                phase: Some(FlowPhase::Commitment),
                gates_passed: None,
            },
            action: FlowAction::Commit,
            context: Some(crate::types::ContextIn {
                commitment_type: Some("quote".into()),
                amount: Some(10_000),
                human_approval_present: Some(true),
                required_gates_met: Some(true),
            }),
            delegation_b64: None,
        };

        let result = verify(&resp.delegation_b64, &vkey, &decide_req).unwrap();
        assert!(result);
    }

    #[test]
    fn verify_rejects_wrong_principal() {
        let key = signing_key();
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64;

        let req = IssueDelegationReq {
            sub: "agent:finance".into(),
            issuer: "human:cfo".into(),
            delegated_authority: AuthorityLevel::Supervisory,
            actions: vec![FlowAction::Commit],
            resource_pattern: "flow:*".into(),
            max_amount: None,
            nbf_epoch: now - 100,
            exp_epoch: now + 3600,
            jti: "nonce-wp".into(),
        };

        let resp = issue(&key, req).unwrap();
        let vkey = key.verifying_key();

        let decide_req = DecideRequest {
            principal: crate::types::PrincipalIn {
                id: "agent:other".into(),
                authority: AuthorityLevel::Advisory,
                domains: vec![],
                policy_version: None,
            },
            resource: crate::types::ResourceIn {
                id: "flow:x".into(),
                resource_type: None,
                phase: None,
                gates_passed: None,
            },
            action: FlowAction::Commit,
            context: None,
            delegation_b64: None,
        };

        let result = verify(&resp.delegation_b64, &vkey, &decide_req).unwrap();
        assert!(!result);
    }

    #[test]
    fn verify_rejects_amount_over_cap() {
        let key = signing_key();
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64;

        let req = IssueDelegationReq {
            sub: "agent:finance".into(),
            issuer: "human:cfo".into(),
            delegated_authority: AuthorityLevel::Supervisory,
            actions: vec![FlowAction::Commit],
            resource_pattern: "flow:*".into(),
            max_amount: Some(1_000),
            nbf_epoch: now - 100,
            exp_epoch: now + 3600,
            jti: "nonce-cap".into(),
        };

        let resp = issue(&key, req).unwrap();
        let vkey = key.verifying_key();

        let decide_req = DecideRequest {
            principal: crate::types::PrincipalIn {
                id: "agent:finance".into(),
                authority: AuthorityLevel::Supervisory,
                domains: vec![],
                policy_version: None,
            },
            resource: crate::types::ResourceIn {
                id: "flow:x".into(),
                resource_type: None,
                phase: None,
                gates_passed: None,
            },
            action: FlowAction::Commit,
            context: Some(crate::types::ContextIn {
                commitment_type: None,
                amount: Some(5_000),
                human_approval_present: None,
                required_gates_met: None,
            }),
            delegation_b64: None,
        };

        let result = verify(&resp.delegation_b64, &vkey, &decide_req).unwrap();
        assert!(!result);
    }
}