tf-types 0.1.6

Core semantic types, traits, and schemas powering the TrustForge protocol.
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
//! Native TrustForge policy engine — Rust mirror of
//! `tools/tf-types-ts/src/core/policy-engine.ts`.

use std::collections::HashMap;

use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sha2::{Digest, Sha256};

use crate::canonicalize;
use crate::glob::glob_match;
use crate::guard::NegativeCapability;

#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct PolicyQuery {
    pub subject: String,
    #[serde(default)]
    pub instance: Option<String>,
    pub action: String,
    #[serde(default)]
    pub target: Option<String>,
    #[serde(default)]
    pub context: HashMap<String, Value>,
    #[serde(default)]
    pub negative_capabilities: Vec<NegativeCapability>,
    #[serde(default)]
    pub enforcement_level: Option<String>,
    #[serde(default)]
    pub now: Option<String>,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct PolicyDecision {
    pub decision_version: String,
    pub policy_engine: String,
    pub engine_version: Option<String>,
    pub trust_domain: String,
    pub subject: String,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub instance: Option<String>,
    pub action: String,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub target: Option<String>,
    pub decision: String,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub rule_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub reason: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub approval: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub proof_required: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub constraints_applied: Option<Vec<Value>>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub negative_capabilities_consulted: Option<Vec<NegativeCapability>>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub enforcement_level: Option<String>,
    pub evaluated_at: String,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub policy_manifest_hash: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub context: Option<HashMap<String, Value>>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PolicyRule {
    pub id: String,
    pub effect: String,
    #[serde(default)]
    pub action: Option<String>,
    #[serde(default)]
    pub action_pattern: Option<String>,
    #[serde(default)]
    pub subject_pattern: Option<String>,
    #[serde(default)]
    pub target_patterns: Option<Vec<String>>,
    #[serde(default)]
    pub approval: Option<String>,
    #[serde(default)]
    pub proof_required: Option<String>,
    #[serde(default)]
    pub constraints: Option<Vec<Value>>,
    #[serde(default)]
    pub reason: Option<String>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PolicyManifest {
    pub policy_version: String,
    pub trust_domain: String,
    #[serde(default)]
    pub engine_hint: Option<String>,
    pub rules: Vec<PolicyRule>,
    #[serde(default)]
    pub negative_capabilities: Vec<NegativeCapability>,
    #[serde(default)]
    pub continuous_reevaluation: Option<ContinuousReeval>,
    #[serde(default)]
    pub quorum_defaults: Option<QuorumDefaults>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ContinuousReeval {
    pub triggers: Vec<String>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct QuorumDefaults {
    pub min_approvers: u32,
    pub of: Vec<String>,
}

/// A pluggable policy engine. Implemented by the native engine, the
/// `tf-cedar` crate, and the `tf-rego` crate. Decoupling via a trait
/// (rather than a feature-gated dependency on the adapter crates) lets
/// `tf-types` stay lightweight while still letting the daemon dispatch
/// the right engine for a given `engine_hint`.
pub trait PolicyEngineImpl {
    fn evaluate(&self, query: &PolicyQuery) -> PolicyDecision;
}

pub struct NativePolicyEngine {
    policy: PolicyManifest,
    manifest_hash: String,
}

impl PolicyEngineImpl for NativePolicyEngine {
    fn evaluate(&self, query: &PolicyQuery) -> PolicyDecision {
        NativePolicyEngine::evaluate(self, query)
    }
}

/// Dispatch a `PolicyQuery` to the appropriate backend based on
/// `engine_hint`. Pass an explicit backend for the hints that need one;
/// `native` falls back to the supplied native engine. When the requested
/// hint has no backend wired in (e.g. caller didn't construct a Cedar
/// engine yet) the dispatcher returns a safe deny.
///
/// The signature uses `dyn` trait objects so callers don't have to leak
/// the cedar / rego crate types through `tf-types`. `tf-cedar` and
/// `tf-rego` each export an adapter that implements `PolicyEngineImpl`.
pub fn evaluate_with_engine(
    hint: Option<&str>,
    native: &NativePolicyEngine,
    cedar: Option<&dyn PolicyEngineImpl>,
    rego: Option<&dyn PolicyEngineImpl>,
    query: &PolicyQuery,
) -> PolicyDecision {
    match hint {
        Some("cedar") => match cedar {
            Some(eng) => eng.evaluate(query),
            None => unavailable_decision("cedar", query, native.policy.trust_domain.as_str()),
        },
        Some("rego") => match rego {
            Some(eng) => eng.evaluate(query),
            None => unavailable_decision("rego", query, native.policy.trust_domain.as_str()),
        },
        _ => native.evaluate(query),
    }
}

fn unavailable_decision(engine: &str, query: &PolicyQuery, trust_domain: &str) -> PolicyDecision {
    PolicyDecision {
        decision_version: "1".into(),
        policy_engine: engine.into(),
        engine_version: Some(format!("{engine}-stub")),
        trust_domain: trust_domain.into(),
        subject: query.subject.clone(),
        instance: query.instance.clone(),
        action: query.action.clone(),
        target: query.target.clone(),
        decision: "deny".into(),
        rule_id: None,
        reason: Some(format!(
            "{engine} engine not configured for this dispatcher (no adapter supplied)"
        )),
        approval: None,
        proof_required: None,
        constraints_applied: None,
        negative_capabilities_consulted: None,
        enforcement_level: query.enforcement_level.clone(),
        evaluated_at: now_iso8601(),
        policy_manifest_hash: None,
        context: if query.context.is_empty() {
            None
        } else {
            Some(query.context.clone())
        },
    }
}

impl NativePolicyEngine {
    pub fn new(policy: PolicyManifest) -> Self {
        let canonical_value = serde_json::to_value(&policy).unwrap_or(Value::Null);
        let canonical = canonicalize(&canonical_value).unwrap_or_default();
        let digest: [u8; 32] = Sha256::digest(canonical.as_bytes()).into();
        let hex: String = digest.iter().map(|b| format!("{:02x}", b)).collect();
        let manifest_hash = format!("sha256-{}", hex);
        NativePolicyEngine {
            policy,
            manifest_hash,
        }
    }

    pub fn evaluate(&self, query: &PolicyQuery) -> PolicyDecision {
        let now = query.now.clone().unwrap_or_else(now_iso8601);
        let neg_caps = if query.negative_capabilities.is_empty() {
            self.policy.negative_capabilities.clone()
        } else {
            query.negative_capabilities.clone()
        };
        for neg in &neg_caps {
            if negative_matches(neg, query) {
                return self.decision(
                    query,
                    "deny",
                    neg.reason
                        .clone()
                        .unwrap_or_else(|| format!("denied by negative_capability {}", neg.name)),
                    None,
                    None,
                    None,
                    None,
                    Some(&neg_caps),
                    &now,
                );
            }
        }
        for rule in &self.policy.rules {
            if !rule_matches(rule, query) {
                continue;
            }
            let reason = rule
                .reason
                .clone()
                .unwrap_or_else(|| format!("matched rule {}", rule.id));
            match rule.effect.as_str() {
                "allow" => {
                    return self.decision(
                        query,
                        "allow",
                        reason,
                        Some(rule.id.clone()),
                        rule.constraints.clone(),
                        rule.proof_required.clone(),
                        rule.approval.clone(),
                        Some(&neg_caps),
                        &now,
                    );
                }
                "deny" => {
                    return self.decision(
                        query,
                        "deny",
                        reason,
                        Some(rule.id.clone()),
                        None,
                        None,
                        None,
                        Some(&neg_caps),
                        &now,
                    );
                }
                "escalate" => {
                    let decision = if rule.approval.as_deref() == Some("quorum") {
                        "escalate"
                    } else {
                        "approval-required"
                    };
                    return self.decision(
                        query,
                        decision,
                        reason,
                        Some(rule.id.clone()),
                        rule.constraints.clone(),
                        rule.proof_required.clone(),
                        rule.approval.clone().or_else(|| Some("required".into())),
                        Some(&neg_caps),
                        &now,
                    );
                }
                "log_only" => {
                    return self.decision(
                        query,
                        "log-only",
                        reason,
                        Some(rule.id.clone()),
                        rule.constraints.clone(),
                        rule.proof_required.clone(),
                        None,
                        Some(&neg_caps),
                        &now,
                    );
                }
                _ => continue,
            }
        }
        self.decision(
            query,
            "deny",
            "no matching rule (default deny)".into(),
            None,
            None,
            None,
            None,
            Some(&neg_caps),
            &now,
        )
    }

    pub fn continuous_triggers(&self) -> Vec<String> {
        self.policy
            .continuous_reevaluation
            .as_ref()
            .map(|c| c.triggers.clone())
            .unwrap_or_default()
    }

    pub fn quorum_defaults(&self) -> Option<&QuorumDefaults> {
        self.policy.quorum_defaults.as_ref()
    }

    pub fn manifest_hash(&self) -> &str {
        &self.manifest_hash
    }

    #[allow(clippy::too_many_arguments)]
    fn decision(
        &self,
        query: &PolicyQuery,
        decision: &str,
        reason: String,
        rule_id: Option<String>,
        constraints: Option<Vec<Value>>,
        proof: Option<String>,
        approval: Option<String>,
        neg_caps: Option<&[NegativeCapability]>,
        now: &str,
    ) -> PolicyDecision {
        PolicyDecision {
            decision_version: "1".into(),
            policy_engine: "native".into(),
            engine_version: Some("tf-policy-native-0.1.0".into()),
            trust_domain: self.policy.trust_domain.clone(),
            subject: query.subject.clone(),
            instance: query.instance.clone(),
            action: query.action.clone(),
            target: query.target.clone(),
            decision: decision.into(),
            rule_id,
            reason: Some(reason),
            approval,
            proof_required: proof,
            constraints_applied: constraints.filter(|c| !c.is_empty()),
            negative_capabilities_consulted: neg_caps.map(|c| c.to_vec()).filter(|v| !v.is_empty()),
            enforcement_level: query.enforcement_level.clone(),
            evaluated_at: now.into(),
            policy_manifest_hash: Some(self.manifest_hash.clone()),
            context: if query.context.is_empty() {
                None
            } else {
                Some(query.context.clone())
            },
        }
    }
}

fn rule_matches(rule: &PolicyRule, query: &PolicyQuery) -> bool {
    if let Some(action) = &rule.action {
        if action != &query.action {
            return false;
        }
    }
    if let Some(pattern) = &rule.action_pattern {
        let re = match Regex::new(pattern) {
            Ok(r) => r,
            Err(_) => return false,
        };
        if !re.is_match(&query.action) {
            return false;
        }
    }
    if let Some(pattern) = &rule.subject_pattern {
        let re = match Regex::new(pattern) {
            Ok(r) => r,
            Err(_) => return false,
        };
        if !re.is_match(&query.subject) {
            return false;
        }
    }
    if let Some(targets) = &rule.target_patterns {
        if !targets.is_empty() {
            let Some(target) = &query.target else {
                return false;
            };
            if !targets.iter().any(|p| glob_match(p, target)) {
                return false;
            }
        }
    }
    true
}

fn negative_matches(neg: &NegativeCapability, q: &PolicyQuery) -> bool {
    if neg.name != q.action {
        return false;
    }
    let Some(target_pattern) = neg.target.as_deref() else {
        return true;
    };
    let Some(query_target) = q.target.as_deref() else {
        return false;
    };
    glob_match(target_pattern, query_target)
}


fn now_iso8601() -> String {
    let secs = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs() as i64;
    let (year, month, day, hour, minute, second) = secs_to_ymdhms(secs);
    format!(
        "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z",
        year, month, day, hour, minute, second
    )
}

fn secs_to_ymdhms(secs: i64) -> (i32, u32, u32, u32, u32, u32) {
    let days = secs.div_euclid(86_400);
    let time = secs.rem_euclid(86_400);
    let hour = (time / 3600) as u32;
    let minute = ((time % 3600) / 60) as u32;
    let second = (time % 60) as u32;
    let z = days + 719_468;
    let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
    let doe = (z - era * 146_097) as u64;
    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
    let y = yoe as i64 + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let d = (doy - (153 * mp + 2) / 5 + 1) as u32;
    let m = if mp < 10 {
        (mp + 3) as u32
    } else {
        (mp - 9) as u32
    };
    let year = if m <= 2 { y + 1 } else { y };
    (year as i32, m, d, hour, minute, second)
}