treetop-core 0.0.17

Core library for Treetop, a Cedar policy engine implementation.
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
use super::*;

#[test]
fn test_current_version_hash() {
    let engine = PolicyEngine::new_from_str(TEST_POLICY).unwrap();
    let version = engine.current_version();

    let expected_hash = Sha256::digest(TEST_POLICY.as_bytes())
        .iter()
        .fold(String::with_capacity(64), |mut s, b| {
            use std::fmt::Write;
            write!(s, "{b:02x}").unwrap();
            s
        });
    assert_eq!(version.hash, expected_hash);
}

#[test]
fn test_policysnapshot_policies() {
    let engine = PolicyEngine::new_from_str(TEST_POLICY).unwrap();
    let snapshot = engine.current_snapshot();
    let policies = snapshot.policy_set();
    assert_eq!(policies.policies().count(), 2);
}

#[test]
fn test_concurrent_evaluation() {
    use std::sync::Arc;
    use std::thread;

    let policies = r#"
            permit (
                principal == User::"alice",
                action == Action::"read",
                resource == Document::"doc1"
            );
        "#;

    let engine = Arc::new(PolicyEngine::new_from_str(policies).unwrap());
    let mut handles = vec![];

    // Spawn 10 threads, each doing 100 evaluations
    for i in 0..10 {
        let engine_clone = Arc::clone(&engine);
        let handle = thread::spawn(move || {
            for _ in 0..100 {
                let request = Request {
                    principal: Principal::User(User::new("alice", None, None)),
                    action: Action::new("read", None),
                    resource: Resource::new("Document", format!("doc{}", i % 5)),
                };
                let decision = engine_clone.evaluate(&request);
                assert!(decision.is_ok());
            }
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.join().unwrap();
    }
}

#[test]
fn test_concurrent_label_registry_access() {
    use std::thread;

    let patterns = vec![("test_label".to_string(), Regex::new(r"test").unwrap())];
    let labeler = RegexLabeler::new("Host", "name", "nameLabels", patterns);

    let label_registry = Arc::new(
        LabelRegistryBuilder::new()
            .add_labeler(Arc::new(labeler))
            .build(),
    );

    let mut handles = vec![];

    // Multiple threads applying labels
    for i in 0..5 {
        let registry = Arc::clone(&label_registry);
        let handle = thread::spawn(move || {
            let mut resource = Resource::new("Host", format!("test-{}", i))
                .with_attr("name", AttrValue::String(format!("test-{}", i)));

            registry.apply(&mut resource);

            // Verify labels were applied
            if let Some(AttrValue::Set(labels)) = resource.attrs().get("nameLabels") {
                assert!(!labels.is_empty());
            }
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.join().unwrap();
    }
}

#[test]
fn test_error_context_on_invalid_entity() {
    let policies = r#"
            permit (
                principal == User::"alice",
                action == Action::"read",
                resource == Document::"doc1"
            );
        "#;

    let _ = PolicyEngine::new_from_str(policies).unwrap();

    // Create request with malformed principal
    let result = "Invalid::Entity::Structure".parse::<EntityUid>();
    assert!(result.is_err());
}

#[test]
fn test_error_context_on_malformed_policy() {
    let malformed_policy = r#"
            permit (
                principal == User::"alice"
                // Missing comma and rest of policy
        "#;

    let result = PolicyEngine::new_from_str(malformed_policy);
    assert!(result.is_err());

    if let Err(PolicyError::ParseError(msg)) = result {
        assert!(msg.contains("parse") || msg.contains("expected"));
    } else {
        panic!("Expected ParseError");
    }
}

#[test]
fn test_empty_policy_text() {
    let result = PolicyEngine::new_from_str("");
    assert!(result.is_ok());

    let engine = result.unwrap();
    let request = Request {
        principal: Principal::User(User::new("alice", None, None)),
        action: Action::new("read", None),
        resource: Resource::new("Document", "doc1"),
    };

    let decision = engine.evaluate(&request).unwrap();
    assert!(matches!(decision, Decision::Deny { .. }));
}

#[test]
fn test_whitespace_only_policy() {
    let result = PolicyEngine::new_from_str("   \n\t  \n  ");
    assert!(result.is_ok());
}

#[test]
fn test_label_registry_initialization() {
    let patterns1 = vec![("label1".to_string(), Regex::new(r"test1").unwrap())];
    let labeler1 = RegexLabeler::new("Host", "name", "nameLabels", patterns1);

    let label_registry = LabelRegistryBuilder::new()
        .add_labeler(Arc::new(labeler1))
        .build();

    let mut resource = Resource::new("Host", "test1-host")
        .with_attr("name", AttrValue::String("test1-host".into()));

    label_registry.apply(&mut resource);

    if let Some(AttrValue::Set(labels)) = resource.attrs().get("nameLabels") {
        assert_eq!(labels.len(), 1);
    }
}

#[test]
fn test_apply_labels_with_no_labelers() {
    // Test that an empty registry doesn't panic
    let label_registry = LabelRegistryBuilder::new().build();

    let mut resource =
        Resource::new("Host", "test-host").with_attr("name", AttrValue::String("test-host".into()));

    // Should not panic with no labelers
    label_registry.apply(&mut resource);

    // Verify no labels were added
    assert!(resource.attrs().get("nameLabels").is_none());
}

#[test]
fn test_label_registry_replacement() {
    let patterns1 = vec![("old_label".to_string(), Regex::new(r"old").unwrap())];
    let labeler1 = RegexLabeler::new("Host", "name", "nameLabels", patterns1);

    let label_registry = LabelRegistryBuilder::new()
        .add_labeler(Arc::new(labeler1))
        .build();

    let patterns2 = vec![("new_label".to_string(), Regex::new(r"new").unwrap())];
    let labeler2 = RegexLabeler::new("Host", "name", "nameLabels", patterns2);

    // Replace labelers via reload
    label_registry.reload(vec![Arc::new(labeler2)]);

    let mut resource =
        Resource::new("Host", "new-host").with_attr("name", AttrValue::String("new-host".into()));

    label_registry.apply(&mut resource);

    if let Some(AttrValue::Set(labels)) = resource.attrs().get("nameLabels") {
        // Should have new_label, not old_label
        let has_new = labels.iter().any(|l| {
            if let AttrValue::String(s) = l {
                s == "new_label"
            } else {
                false
            }
        });
        assert!(has_new);
    }
}

#[test]
fn test_large_policy_set() {
    // Generate 100 policies
    let mut policies = String::new();
    for i in 0..100 {
        policies.push_str(&format!(
            r#"
                permit (
                    principal == User::"user{}",
                    action == Action::"read",
                    resource == Document::"doc{}"
                );
                "#,
            i, i
        ));
    }

    let engine = PolicyEngine::new_from_str(&policies).unwrap();

    // Test evaluation still works
    let request = Request {
        principal: Principal::User(User::new("user50", None, None)),
        action: Action::new("read", None),
        resource: Resource::new("Document", "doc50"),
    };

    let decision = engine.evaluate(&request).unwrap();
    assert!(matches!(decision, Decision::Allow { .. }));
}

#[test]
fn test_deeply_nested_namespaces() {
    let policies = r#"
            permit (
                principal == A::B::C::D::E::User::"alice",
                action == A::B::C::D::E::Action::"read",
                resource == A::B::C::D::E::Document::"doc1"
            );
        "#;

    let engine = PolicyEngine::new_from_str(policies).unwrap();

    let request = Request {
        principal: Principal::User(User::new(
            "alice",
            None,
            Some(vec![
                "A".into(),
                "B".into(),
                "C".into(),
                "D".into(),
                "E".into(),
            ]),
        )),
        action: Action::new(
            "read",
            Some(vec![
                "A".into(),
                "B".into(),
                "C".into(),
                "D".into(),
                "E".into(),
            ]),
        ),
        resource: Resource::new("A::B::C::D::E::Document", "doc1"),
    };

    let decision = engine.evaluate(&request).unwrap();
    assert!(matches!(decision, Decision::Allow { .. }));
}

#[test]
fn test_resource_with_many_attributes() {
    let policies = r#"
            permit (
                principal == User::"alice",
                action == Action::"read",
                resource is Document
            );
        "#;

    let engine = PolicyEngine::new_from_str(policies).unwrap();

    // Create resource with 50 attributes
    let mut resource = Resource::new("Document", "doc1");
    for i in 0..50 {
        resource = resource.with_attr(
            format!("attr{}", i),
            AttrValue::String(format!("value{}", i)),
        );
    }

    let request = Request {
        principal: Principal::User(User::new("alice", None, None)),
        action: Action::new("read", None),
        resource,
    };

    let decision = engine.evaluate(&request).unwrap();
    assert!(matches!(decision, Decision::Allow { .. }));
}

#[test]
fn test_user_with_many_groups() {
    let policies = r#"
            permit (
                principal in Group::"group25",
                action == Action::"read",
                resource == Document::"doc1"
            );
        "#;

    let engine = PolicyEngine::new_from_str(policies).unwrap();

    // Create user in 50 groups
    let groups: Vec<String> = (0..50).map(|i| format!("group{}", i)).collect();

    let request = Request {
        principal: Principal::User(User::new("alice", Some(groups), None)),
        action: Action::new("read", None),
        resource: Resource::new("Document", "doc1"),
    };

    let decision = engine.evaluate(&request).unwrap();
    assert!(matches!(decision, Decision::Allow { .. }));
}

#[test]
fn test_decision_includes_correct_version() {
    let policies = r#"
            permit (
                principal == User::"alice",
                action == Action::"read",
                resource == Document::"doc1"
            );
        "#;

    let engine = PolicyEngine::new_from_str(policies).unwrap();
    let engine_version = engine.current_version();

    let request = Request {
        principal: Principal::User(User::new("alice", None, None)),
        action: Action::new("read", None),
        resource: Resource::new("Document", "doc1"),
    };

    let decision = engine.evaluate(&request).unwrap();

    match decision {
        Decision::Allow { version, .. } => {
            assert_eq!(version.hash, engine_version.hash);
        }
        Decision::Deny { .. } => panic!("Expected Allow"),
    }
}

#[test]
fn test_multiple_snapshots_share_data() {
    let policies = r#"
            permit (
                principal == User::"alice",
                action == Action::"read",
                resource == Document::"doc1"
            );
        "#;

    let engine1 = PolicyEngine::new_from_str(policies).unwrap();
    let engine2 = engine1.clone();

    let version1 = engine1.current_version();
    let version2 = engine2.current_version();

    // Both clones should share the same snapshot
    assert_eq!(version1.hash, version2.hash);
    assert_eq!(version1.loaded_at, version2.loaded_at);
}

#[test]
fn test_multiple_policies_captured() {
    // Test that when multiple policies match, all are captured in the Decision
    let policies = r#"
            permit (
                principal,
                action == Action::"read",
                resource == Document::"public"
            );

            permit (
                principal == User::"alice",
                action,
                resource
            );
        "#;

    let engine = PolicyEngine::new_from_str(policies).unwrap();

    // Alice reading public document should match both policies
    let request = Request {
        principal: Principal::User(User::new("alice", None, None)),
        action: Action::new("read", None),
        resource: Resource::new("Document", "public"),
    };

    let decision = engine.evaluate(&request).unwrap();

    match decision {
        Decision::Allow { policies, .. } => {
            assert_eq!(
                policies.len(),
                2,
                "Should have captured both matching policies"
            );
            // Both policy0 and policy1 should be present
            let policy_ids: Vec<_> = policies.iter().map(|p| p.cedar_id.as_str()).collect();
            assert!(policy_ids.contains(&"policy0"), "Should contain policy0");
            assert!(policy_ids.contains(&"policy1"), "Should contain policy1");
        }
        Decision::Deny { .. } => panic!("Expected Allow decision"),
    }
}