chronix-security 0.2.0

Security layer for Chronix — authentication, Cedar authorization, audit trail, and multi-tenancy
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
//! Cedar policy authorizer engine.
//!
//! This module wraps the Cedar policy engine and provides the core
//! `authorize()` function for Chronix.

use std::collections::{HashMap, HashSet, VecDeque};
use std::path::Path;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

use cedar_policy::{
    Authorizer, Context, Entities, PolicySet, Request, Schema, ValidationMode, Validator,
};
use metrics::counter;
use parking_lot::RwLock;
use tracing::{debug, error, info, warn};

use crate::authz::error::{AuthzError, Result};
use crate::authz::model::{
    ChronixAction, ChronixNamespace, ChronixPrincipal, ChronixResource, Decision,
};

/// Maximum number of policy versions retained in the history ring buffer.
const MAX_POLICY_VERSIONS: usize = 10;

/// A snapshot of a policy set at a particular version.
#[derive(Debug, Clone)]
pub struct PolicyVersion {
    /// Monotonically increasing version number.
    pub version: u64,
    /// The policy set at this version.
    pub policies: Arc<PolicySet>,
    /// Human-readable description of the change (e.g. "full reload", "add policy foo").
    pub source: String,
    /// Unix timestamp (seconds) when this version was created.
    pub timestamp: i64,
}

/// The Cedar-based authorization engine.
///
/// Thread-safe — can be shared across threads via `Arc<AuthzEngine>`.
///
/// # Policy Versioning & Rollback
///
/// The engine maintains an in-memory ring buffer of the last
/// `MAX_POLICY_VERSIONS` policy snapshots. Every mutating operation
/// (`load_policies`, `add_policy`, `remove_policy`) bumps the version
/// counter and pushes the *previous* state into the history before
/// swapping in the new set. Rollback to any retained version is
/// available via [`revert_to_version()`](Self::revert_to_version).
///
/// A dry-run mode ([`dryrun_load_policies()`](Self::dryrun_load_policies))
/// parses and validates a policy string without modifying the active set.
///
/// # Cross-Tenant Isolation
///
/// There is no implicit "deny cross-tenant access" policy built into
/// the engine. Tenant isolation is enforced at two layers:
///
/// 1. **Namespace scoping** — every request carries a namespace context
///    (see `ChronixNamespace`). The storage and query layers filter by
///    namespace before authorization is even checked.
/// 2. **Explicit Cedar policies** — operators must deploy `forbid`
///    policies that deny access when `resource.namespace != principal.namespace`.
///    See `docs/security.md` for a reference policy template.
///
/// This explicit-policy approach avoids magic built-in rules that are
/// invisible to policy auditors.
pub struct AuthzEngine {
    /// The Cedar authorizer (stateless, thread-safe).
    authorizer: Authorizer,
    /// The active policy set — wrapped in Arc so authorize() can
    /// snapshot the pointer without holding the lock during evaluation
    /// (Lock-free read path.)
    policies: RwLock<Arc<PolicySet>>,
    /// Optional Cedar schema for policy validation.
    schema: Option<Schema>,
    /// When true, policy loads fail if no schema is set.
    require_schema: bool,
    /// Monotonically increasing version counter.
    version_counter: AtomicU64,
    /// Ring buffer of previous policy versions for rollback.
    history: RwLock<VecDeque<PolicyVersion>>,
}

impl AuthzEngine {
    /// Create a new authorizer with the built-in cross-tenant isolation
    /// policy auto-loaded (default-deny + namespace isolation).
    ///
    /// Schema validation is automatically enabled when a schema
    /// is loaded via `with_schema()` or `load_schema()`. Without a
    /// schema, policies are accepted without type-level validation.
    /// Call `.with_require_schema(true)` to force a schema requirement.
    #[must_use]
    pub fn new() -> Self {
        let engine = Self {
            authorizer: Authorizer::new(),
            policies: RwLock::new(Arc::new(PolicySet::new())),
            schema: None,
            require_schema: false,
            version_counter: AtomicU64::new(0),
            history: RwLock::new(VecDeque::new()),
        };

        // Auto-load the cross-tenant isolation policy so that
        // namespace-scoped principals are always denied access to
        // resources in a different namespace by default.
        if let Err(e) = engine.load_cross_tenant_isolation_policy() {
            tracing::error!(error = %e, "failed to auto-load cross-tenant isolation policy");
        }

        engine
    }

    /// Create an authorizer with the given policy set.
    pub fn with_policies(policies: PolicySet) -> Self {
        let count = policies.policies().count();
        info!(
            policy_count = count,
            "AuthzEngine initialized with policies"
        );
        Self {
            authorizer: Authorizer::new(),
            policies: RwLock::new(Arc::new(policies)),
            schema: None,
            require_schema: false,
            version_counter: AtomicU64::new(1),
            history: RwLock::new(VecDeque::new()),
        }
    }

    /// Set the Cedar schema for policy validation.
    ///
    /// When a schema is set, `load_policies()` and `add_policy()` will
    /// validate policies against it and reject those that reference
    /// unknown entity types, actions, or attributes.
    ///
    /// Setting a schema automatically enables `require_schema`,
    /// which means all policies MUST validate against the schema.
    pub fn with_schema(mut self, schema: Schema) -> Self {
        self.schema = Some(schema);
        self.require_schema = true;
        self
    }

    /// Load a Cedar schema from its string representation.
    ///
    /// Loading a schema automatically enables `require_schema`.
    pub fn load_schema(&mut self, schema_src: &str) -> Result<()> {
        let (schema, _warnings) = Schema::from_cedarschema_str(schema_src)
            .map_err(|e| AuthzError::SchemaParse(format!("{e}")))?;
        self.schema = Some(schema);
        self.require_schema = true;
        info!("Cedar schema loaded (require_schema auto-enabled)");
        Ok(())
    }

    /// Require a Cedar schema for all policy operations.
    /// When enabled, `load_policies()` and `add_policy()` will fail
    /// if no schema has been loaded.
    pub fn with_require_schema(mut self, require: bool) -> Self {
        self.require_schema = require;
        self
    }

    /// Validate a policy set against the schema (if present).
    ///
    /// Logs a warning when no schema is set. If `require_schema`
    /// is true, returns an error instead.
    fn validate_policies(&self, ps: &PolicySet) -> Result<()> {
        if let Some(ref schema) = self.schema {
            let validator = Validator::new(schema.clone());
            let result = validator.validate(ps, ValidationMode::default());
            let errors: Vec<String> = result.validation_errors().map(|e| format!("{e}")).collect();
            if !errors.is_empty() {
                return Err(AuthzError::PolicyValidation(errors.join("; ")));
            }
            let warnings: Vec<String> = result
                .validation_warnings()
                .map(|w| format!("{w}"))
                .collect();
            for w in &warnings {
                warn!(warning = %w, "Cedar policy validation warning");
            }
        } else if self.require_schema {
            return Err(AuthzError::PolicyValidation(
                "no Cedar schema loaded but require_schema is enabled; \
                 call load_schema() or with_schema() before loading policies"
                    .into(),
            ));
        } else {
            tracing::warn!(
                "no Cedar schema loaded — policies are not validated \
                 against a schema; typos in entity/action names will be silently accepted"
            );
        }
        Ok(())
    }

    /// Load policies from a Cedar policy string.
    ///
    /// Replaces all existing policies. If a schema is set, policies
    /// are validated against it before being accepted.
    ///
    /// The previous policy set is pushed into the version history ring
    /// buffer before being replaced.
    pub fn load_policies(&self, policy_src: &str) -> Result<usize> {
        let ps = policy_src
            .parse::<PolicySet>()
            .map_err(|e| AuthzError::PolicyParse(format!("{e}")))?;
        self.validate_policies(&ps)?;
        let count = ps.policies().count();
        let policy_ids: Vec<String> = ps.policies().map(|p| p.id().to_string()).collect();

        let new_version = self.version_counter.fetch_add(1, Ordering::Relaxed) + 1;
        self.push_history("full reload");

        *self.policies.write() = Arc::new(ps);
        info!(count, version = new_version, policies = ?policy_ids, "Policies loaded (full reload)");
        counter!("chronix_authz_policy_load", "operation" => "load_all").increment(1);
        Ok(count)
    }

    /// Dry-run policy loading: parse and validate without modifying the
    /// active policy set.
    ///
    /// Returns the number of policies that would be loaded, or an error
    /// if parsing or schema validation fails. The active policy set is
    /// **not** modified.
    pub fn dryrun_load_policies(&self, policy_src: &str) -> Result<usize> {
        let ps = policy_src
            .parse::<PolicySet>()
            .map_err(|e| AuthzError::PolicyParse(format!("{e}")))?;
        self.validate_policies(&ps)?;
        let count = ps.policies().count();
        info!(count, "Dry-run policy validation passed");
        Ok(count)
    }

    /// Add a single policy from source.
    ///
    /// Uses a clone-and-swap strategy: clones the current policy set
    /// under a brief read lock, builds and validates the candidate set
    /// without holding any lock, and only acquires a write lock for
    /// the final atomic swap.  If parsing or validation fails, the
    /// current set is left untouched (automatic rollback).
    pub fn add_policy(&self, id: &str, policy_src: &str) -> Result<()> {
        let policy = cedar_policy::Policy::parse(Some(cedar_policy::PolicyId::new(id)), policy_src)
            .map_err(|e| AuthzError::PolicyParse(format!("{e}")))?;

        // 1. Snapshot current policies under read lock (brief)
        let mut new_ps = {
            let ps = self.policies.read();
            let mut candidate = PolicySet::new();
            for p in ps.policies() {
                candidate
                    .add(p.clone())
                    .map_err(|e| AuthzError::Internal(format!("Duplicate policy: {e}")))?;
            }
            candidate
        };

        // 2. Build and validate candidate (no lock held)
        new_ps
            .add(policy)
            .map_err(|e| AuthzError::Internal(format!("Failed to add policy: {e}")))?;
        self.validate_policies(&new_ps)?;

        // 3. Atomic swap under write lock
        let total = new_ps.policies().count();
        let new_version = self.version_counter.fetch_add(1, Ordering::Relaxed) + 1;
        self.push_history(&format!("add policy {id}"));
        *self.policies.write() = Arc::new(new_ps);
        info!(
            policy_id = id,
            total_policies = total,
            version = new_version,
            "Policy added"
        );
        counter!("chronix_authz_policy_load", "operation" => "add").increment(1);
        Ok(())
    }

    /// Remove a policy by ID.
    ///
    /// Returns `true` if the policy was found and removed.
    pub fn remove_policy(&self, id: &str) -> bool {
        let mut guard = self.policies.write();
        // Clone the policy set out of the Arc so we can mutate it
        let mut ps = PolicySet::new();
        for p in guard.policies() {
            // Log rather than silently dropping add failures.
            if let Err(e) = ps.add(p.clone()) {
                tracing::error!(error = %e, "failed to re-add policy during remove_policy rebuild");
            }
        }
        let policy_id = cedar_policy::PolicyId::new(id);
        match ps.remove_static(policy_id) {
            Ok(_) => {
                let remaining = ps.policies().count();
                // Push history *before* swapping (guard is held, so
                // snapshot is the current live set).
                let prev = Arc::clone(&guard);
                let ver = self.version_counter.fetch_add(1, Ordering::Relaxed) + 1;
                {
                    let mut hist = self.history.write();
                    if hist.len() >= MAX_POLICY_VERSIONS {
                        hist.pop_front();
                    }
                    hist.push_back(PolicyVersion {
                        version: ver.saturating_sub(1),
                        policies: prev,
                        source: format!("before remove policy {id}"),
                        timestamp: now_unix(),
                    });
                }
                *guard = Arc::new(ps);
                info!(
                    policy_id = id,
                    remaining_policies = remaining,
                    version = ver,
                    "Policy removed"
                );
                counter!("chronix_authz_policy_load", "operation" => "remove").increment(1);
                true
            }
            Err(_) => {
                warn!(id, "Policy not found for removal");
                false
            }
        }
    }

    /// Push the current active policy set into the history ring buffer.
    ///
    /// Called internally before every mutating operation.
    fn push_history(&self, source: &str) {
        let current = Arc::clone(&self.policies.read());
        let ver = self.version_counter.load(Ordering::Relaxed);
        let mut hist = self.history.write();
        if hist.len() >= MAX_POLICY_VERSIONS {
            hist.pop_front();
        }
        hist.push_back(PolicyVersion {
            // The version being *replaced* is the current counter
            // before the caller incremented it. Since the caller does
            // fetch_add(1) + 1, the version of the old set is (new - 1).
            version: ver.saturating_sub(1),
            policies: current,
            source: source.to_string(),
            timestamp: now_unix(),
        });
    }

    /// Return the current policy version number.
    #[must_use]
    pub fn current_version(&self) -> u64 {
        self.version_counter.load(Ordering::Relaxed)
    }

    /// List all retained policy versions (oldest first).
    #[must_use]
    pub fn policy_versions(&self) -> Vec<PolicyVersion> {
        self.history.read().iter().cloned().collect()
    }

    /// Revert the active policy set to a previously retained version
    ///.
    ///
    /// The version must exist in the history ring buffer. On success
    /// the reverted-to snapshot becomes the active set and the
    /// *pre-revert* state is pushed into history (so it can itself be
    /// rolled back).
    pub fn revert_to_version(&self, version: u64) -> Result<()> {
        let snapshot = {
            let hist = self.history.read();
            hist.iter()
                .find(|v| v.version == version)
                .map(|v| Arc::clone(&v.policies))
        };
        let snapshot = snapshot.ok_or_else(|| {
            AuthzError::Internal(format!(
                "Policy version {version} not found in history (retained: {})",
                self.history
                    .read()
                    .iter()
                    .map(|v| v.version.to_string())
                    .collect::<Vec<_>>()
                    .join(", ")
            ))
        })?;

        let new_version = self.version_counter.fetch_add(1, Ordering::Relaxed) + 1;
        self.push_history(&format!("before revert to v{version}"));
        let count = snapshot.policies().count();
        *self.policies.write() = snapshot;
        info!(
            reverted_to = version,
            new_version = new_version,
            policy_count = count,
            "Policy set reverted"
        );
        counter!("chronix_authz_policy_load", "operation" => "revert").increment(1);
        Ok(())
    }

    /// Load policies from all `.cedar` files in a directory.
    pub fn load_policies_from_dir(&self, dir: &Path) -> Result<usize> {
        let mut combined = String::new();
        let mut file_count = 0;

        for entry in std::fs::read_dir(dir)? {
            let entry = entry?;
            let path = entry.path();
            if path.extension().is_some_and(|e| e == "cedar") {
                let content = std::fs::read_to_string(&path)?;
                combined.push_str(&content);
                combined.push('\n');
                file_count += 1;
                debug!(path = %path.display(), "Loaded policy file");
            }
        }

        if file_count == 0 {
            info!(dir = %dir.display(), "No .cedar files found");
            return Ok(0);
        }

        self.load_policies(&combined)
    }

    /// Evaluate an authorization request.
    ///
    /// Returns `Decision::Allow` if any policy explicitly permits the request,
    /// or `Decision::Deny` with reasons otherwise (default-deny).
    pub fn authorize(
        &self,
        principal: &ChronixPrincipal,
        action: ChronixAction,
        resource: &ChronixResource,
    ) -> Decision {
        let principal_entity = match principal.to_entity() {
            Ok(e) => e,
            Err(e) => {
                error!(error = %e, "Cedar principal entity construction failed");
                return Decision::Deny {
                    reasons: vec!["Internal authorization error".into()],
                };
            }
        };
        let resource_entity = match resource.to_entity() {
            Ok(e) => e,
            Err(e) => {
                error!(error = %e, "Cedar resource entity construction failed");
                return Decision::Deny {
                    reasons: vec!["Internal authorization error".into()],
                };
            }
        };

        // Build role entities (for membership) using cached type names
        #[allow(clippy::result_large_err)] // cedar_policy error type; not on a hot path
        let role_entities: std::result::Result<Vec<cedar_policy::Entity>, _> = principal
            .roles
            .iter()
            .map(|role| {
                let uid = cedar_policy::EntityUid::from_type_name_and_id(
                    crate::authz::model::role_type_name().clone(),
                    cedar_policy::EntityId::new(role),
                );
                cedar_policy::Entity::new(uid, HashMap::new(), HashSet::new())
            })
            .collect();

        let role_entities = match role_entities {
            Ok(v) => v,
            Err(e) => {
                error!(error = %e, "Cedar role entity construction failed");
                return Decision::Deny {
                    reasons: vec!["Internal authorization error".into()],
                };
            }
        };

        let mut all_entities = vec![principal_entity, resource_entity];
        all_entities.extend(role_entities);

        // Include namespace entity so Cedar can resolve `resource in Chronix::Namespace::"..."`.
        if let Some(ns_entity) = resource.namespace_entity() {
            all_entities.push(ns_entity);
        }

        let entities = match Entities::from_entities(all_entities, None) {
            Ok(e) => e,
            Err(e) => {
                error!(error = %e, "Cedar entity set construction failed");
                return Decision::Deny {
                    reasons: vec!["Internal authorization error".into()],
                };
            }
        };

        let request = match Request::new(
            principal.to_entity_uid(),
            action.to_entity_uid(),
            resource.to_entity_uid(),
            Context::empty(),
            None,
        ) {
            Ok(r) => r,
            Err(e) => {
                error!(error = %e, "Cedar authorization request construction failed");
                return Decision::Deny {
                    reasons: vec!["Internal authorization error".into()],
                };
            }
        };

        // Snapshot the policy set via Arc clone (brief read lock),
        // then drop the lock before the expensive authorization
        // evaluation. This prevents policy hot-reloads from blocking
        // concurrent authorize() calls.
        let policies = Arc::clone(&self.policies.read());
        let response = self
            .authorizer
            .is_authorized(&request, &policies, &entities);

        let decision_label = match response.decision() {
            cedar_policy::Decision::Allow => "allow",
            cedar_policy::Decision::Deny => "deny",
        };

        counter!("chronix_authz_decisions_total", "action" => action.to_string(), "decision" => decision_label)
            .increment(1);

        match response.decision() {
            cedar_policy::Decision::Allow => Decision::Allow,
            cedar_policy::Decision::Deny => {
                // Log detailed diagnostics server-side for debugging,
                // but only return a generic reason to callers to avoid
                // leaking policy IDs or evaluation internals to clients.
                let eval_errors: Vec<String> = response
                    .diagnostics()
                    .errors()
                    .map(std::string::ToString::to_string)
                    .collect();

                let policy_reasons: Vec<String> = response
                    .diagnostics()
                    .reason()
                    .map(std::string::ToString::to_string)
                    .collect();

                if !policy_reasons.is_empty() || !eval_errors.is_empty() {
                    tracing::debug!(
                        ?policy_reasons,
                        ?eval_errors,
                        %action,
                        principal = %principal.id,
                        "authorization denied with diagnostics"
                    );
                }

                Decision::Deny {
                    reasons: vec!["access denied".into()],
                }
            }
        }
    }

    /// Evaluate a granular admin action, falling back to the `Admin`
    /// super-action if the specific action is denied.
    ///
    /// Policies that grant `Action::"Admin"` automatically cover
    /// all granular admin actions (`ManageNodes`, `ManageKeys`, etc.).
    /// This avoids breaking existing policies while enabling least-privilege
    /// grants for new deployments.
    pub fn authorize_admin(
        &self,
        principal: &ChronixPrincipal,
        action: ChronixAction,
        resource: &ChronixResource,
    ) -> Decision {
        let result = self.authorize(principal, action, resource);
        if result.is_allowed() || action == ChronixAction::Admin {
            return result;
        }
        // Granular action was denied — try the super-action.
        self.authorize(principal, ChronixAction::Admin, resource)
    }

    /// Evaluate an authorization request against a namespace resource.
    ///
    /// This is used for namespace-level policies, e.g.:
    /// ```cedar
    /// permit(
    ///   principal in Chronix::Role::"ns_admin",
    ///   action,
    ///   resource == Chronix::Namespace::"production"
    /// );
    /// ```
    pub fn authorize_namespace(
        &self,
        principal: &ChronixPrincipal,
        action: ChronixAction,
        namespace: &ChronixNamespace,
    ) -> Decision {
        let principal_entity = match principal.to_entity() {
            Ok(e) => e,
            Err(e) => {
                error!(error = %e, "Cedar principal entity construction failed");
                return Decision::Deny {
                    reasons: vec!["Internal authorization error".into()],
                };
            }
        };
        let namespace_entity = match namespace.to_entity() {
            Ok(e) => e,
            Err(e) => {
                error!(error = %e, "Cedar namespace entity construction failed");
                return Decision::Deny {
                    reasons: vec!["Internal authorization error".into()],
                };
            }
        };

        #[allow(clippy::result_large_err)] // cedar_policy error type; not on a hot path
        let role_entities: std::result::Result<Vec<cedar_policy::Entity>, _> = principal
            .roles
            .iter()
            .map(|role| {
                let uid = cedar_policy::EntityUid::from_type_name_and_id(
                    crate::authz::model::role_type_name().clone(),
                    cedar_policy::EntityId::new(role),
                );
                cedar_policy::Entity::new(uid, HashMap::new(), HashSet::new())
            })
            .collect();

        let role_entities = match role_entities {
            Ok(v) => v,
            Err(e) => {
                error!(error = %e, "Cedar role entity construction failed");
                return Decision::Deny {
                    reasons: vec!["Internal authorization error".into()],
                };
            }
        };

        let mut all_entities = vec![principal_entity, namespace_entity];
        all_entities.extend(role_entities);

        let entities = match Entities::from_entities(all_entities, None) {
            Ok(e) => e,
            Err(e) => {
                error!(error = %e, "Cedar entity set construction failed");
                return Decision::Deny {
                    reasons: vec!["Internal authorization error".into()],
                };
            }
        };

        let request = match Request::new(
            principal.to_entity_uid(),
            action.to_entity_uid(),
            namespace.to_entity_uid(),
            Context::empty(),
            None,
        ) {
            Ok(r) => r,
            Err(e) => {
                error!(error = %e, "Cedar authorization request construction failed");
                return Decision::Deny {
                    reasons: vec!["Internal authorization error".into()],
                };
            }
        };

        // Snapshot policy set via Arc clone for lock-free evaluation.
        let policies = Arc::clone(&self.policies.read());
        let response = self
            .authorizer
            .is_authorized(&request, &policies, &entities);

        let decision_label = match response.decision() {
            cedar_policy::Decision::Allow => "allow",
            cedar_policy::Decision::Deny => "deny",
        };

        counter!(
            "chronix_authz_decisions_total",
            "action" => action.to_string(),
            "resource_type" => "namespace",
            "decision" => decision_label
        )
        .increment(1);

        match response.decision() {
            cedar_policy::Decision::Allow => Decision::Allow,
            cedar_policy::Decision::Deny => {
                let eval_errors: Vec<String> = response
                    .diagnostics()
                    .errors()
                    .map(std::string::ToString::to_string)
                    .collect();
                let policy_reasons: Vec<String> = response
                    .diagnostics()
                    .reason()
                    .map(std::string::ToString::to_string)
                    .collect();

                if !policy_reasons.is_empty() || !eval_errors.is_empty() {
                    tracing::debug!(
                        ?policy_reasons,
                        ?eval_errors,
                        %action,
                        principal = %principal.id,
                        namespace = %namespace.name,
                        "namespace authorization denied with diagnostics"
                    );
                }

                Decision::Deny {
                    reasons: vec!["access denied".into()],
                }
            }
        }
    }

    /// Returns the number of active policies.
    #[must_use]
    pub fn policy_count(&self) -> usize {
        self.policies.read().policies().count()
    }

    /// List all policy IDs.
    #[must_use]
    pub fn policy_ids(&self) -> Vec<String> {
        self.policies
            .read()
            .policies()
            .map(|p| p.id().to_string())
            .collect()
    }

    /// **** Load the default cross-tenant isolation policy.
    ///
    /// This policy forbids any principal from accessing resources in a
    /// namespace that differs from the principal's own namespace attribute.
    /// Without this, data is visible across tenants unless explicit Cedar
    /// `forbid` policies are deployed.
    ///
    /// The policy is idempotent — calling this multiple times is safe
    /// (the existing built-in policy is removed before re-adding).
    pub fn load_cross_tenant_isolation_policy(&self) -> Result<()> {
        const POLICY_ID: &str = "chronix-builtin-cross-tenant-deny";
        // Remove if already present (idempotent)
        self.remove_policy(POLICY_ID);
        self.add_policy(POLICY_ID, Self::CROSS_TENANT_ISOLATION_POLICY)
    }

    /// **** Check if any namespace-scoped policies are loaded.
    ///
    /// Logs a warning if no policies reference `Chronix::Namespace`,
    /// which likely means cross-tenant isolation is not enforced.
    pub fn warn_if_no_namespace_policies(&self) {
        let policies = self.policies.read();
        let has_ns = policies.policies().any(|p| {
            let src = format!("{p}");
            src.contains("Namespace")
        });
        if !has_ns {
            warn!(
                "No namespace-scoped Cedar policies detected! \
                 Cross-tenant data isolation may not be enforced. \
                 Call `load_cross_tenant_isolation_policy()` or deploy \
                 explicit namespace policies. See docs/security.md."
            );
        }
    }

    /// Built-in Cedar policy that denies cross-tenant resource access.
    ///
    /// Uses Cedar's `forbid` with `unless` to deny any request where
    /// the resource's namespace does not match the principal's namespace,
    /// unless the principal has the `admin` role.
    pub const CROSS_TENANT_ISOLATION_POLICY: &'static str = r#"
    forbid(
        principal,
        action,
        resource
    )
    when {
        resource has namespace &&
        principal has namespace &&
        resource.namespace != principal.namespace
    }
    unless {
        principal in Chronix::Role::"admin"
    };
    "#;
}

/// Returns the current Unix timestamp in seconds.
fn now_unix() -> i64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs() as i64
}

impl Default for AuthzEngine {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Debug for AuthzEngine {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AuthzEngine")
            .field("policy_count", &self.policy_count())
            .finish()
    }
}

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

    fn admin_allow_all_policy() -> &'static str {
        r#"
        permit(
            principal in Chronix::Role::"admin",
            action,
            resource
        );
        "#
    }

    fn read_only_policy() -> &'static str {
        r#"
        permit(
            principal,
            action == Chronix::Action::"Read",
            resource
        );
        "#
    }

    fn measurement_scoped_policy() -> &'static str {
        r#"
        permit(
            principal in Chronix::Role::"data-science",
            action == Chronix::Action::"Forecast",
            resource == Chronix::Measurement::"energy"
        );
        "#
    }

    #[test]
    fn default_deny_no_policies() {
        let engine = AuthzEngine::new();
        let principal = ChronixPrincipal::new("alice");
        let resource = ChronixResource::measurement("cpu");
        let decision = engine.authorize(&principal, ChronixAction::Read, &resource);
        assert!(decision.is_denied());
    }

    #[test]
    fn admin_role_allows_all() {
        let engine = AuthzEngine::new();
        engine.load_policies(admin_allow_all_policy()).unwrap();

        let admin = ChronixPrincipal::new("alice").with_role("admin");
        let resource = ChronixResource::measurement("cpu");

        assert!(engine
            .authorize(&admin, ChronixAction::Write, &resource)
            .is_allowed());
        assert!(engine
            .authorize(&admin, ChronixAction::Read, &resource)
            .is_allowed());
        assert!(engine
            .authorize(&admin, ChronixAction::Delete, &resource)
            .is_allowed());
        assert!(engine
            .authorize(&admin, ChronixAction::Admin, &resource)
            .is_allowed());
    }

    #[test]
    fn non_admin_denied_without_matching_policy() {
        let engine = AuthzEngine::new();
        engine.load_policies(admin_allow_all_policy()).unwrap();

        let user = ChronixPrincipal::new("bob"); // no role
        let resource = ChronixResource::measurement("cpu");

        assert!(engine
            .authorize(&user, ChronixAction::Write, &resource)
            .is_denied());
    }

    #[test]
    fn read_only_allows_read_denies_write() {
        let engine = AuthzEngine::new();
        engine.load_policies(read_only_policy()).unwrap();

        let user = ChronixPrincipal::new("bob");
        let resource = ChronixResource::measurement("cpu");

        assert!(engine
            .authorize(&user, ChronixAction::Read, &resource)
            .is_allowed());
        assert!(engine
            .authorize(&user, ChronixAction::Write, &resource)
            .is_denied());
    }

    #[test]
    fn measurement_scoped_authorization() {
        let engine = AuthzEngine::new();
        engine.load_policies(measurement_scoped_policy()).unwrap();

        let ds = ChronixPrincipal::new("carol").with_role("data-science");

        // Allowed: forecast on energy
        assert!(engine
            .authorize(
                &ds,
                ChronixAction::Forecast,
                &ChronixResource::measurement("energy")
            )
            .is_allowed());

        // Denied: forecast on cpu (wrong measurement)
        assert!(engine
            .authorize(
                &ds,
                ChronixAction::Forecast,
                &ChronixResource::measurement("cpu")
            )
            .is_denied());

        // Denied: write on energy (wrong action)
        assert!(engine
            .authorize(
                &ds,
                ChronixAction::Write,
                &ChronixResource::measurement("energy")
            )
            .is_denied());
    }

    #[test]
    fn add_and_remove_policy() {
        let engine = AuthzEngine::new();
        // Engine starts with 1 built-in cross-tenant isolation policy
        let builtin_count = engine.policy_count();
        assert!(builtin_count >= 1);

        engine
            .add_policy(
                "read-all",
                r#"permit(principal, action == Chronix::Action::"Read", resource);"#,
            )
            .unwrap();
        assert_eq!(engine.policy_count(), builtin_count + 1);

        let user = ChronixPrincipal::new("alice");
        let resource = ChronixResource::measurement("cpu");
        assert!(engine
            .authorize(&user, ChronixAction::Read, &resource)
            .is_allowed());

        assert!(engine.remove_policy("read-all"));
        assert_eq!(engine.policy_count(), builtin_count);
        assert!(engine
            .authorize(&user, ChronixAction::Read, &resource)
            .is_denied());
    }

    #[test]
    fn remove_nonexistent_policy() {
        let engine = AuthzEngine::new();
        assert!(!engine.remove_policy("does-not-exist"));
    }

    #[test]
    fn policy_ids_listing() {
        let engine = AuthzEngine::new();
        // Engine starts with built-in cross-tenant isolation policy
        let builtin_count = engine.policy_ids().len();
        engine
            .add_policy(
                "p1",
                r#"permit(principal, action == Chronix::Action::"Read", resource);"#,
            )
            .unwrap();
        engine
            .add_policy(
                "p2",
                r#"permit(principal, action == Chronix::Action::"Write", resource);"#,
            )
            .unwrap();

        let ids = engine.policy_ids();
        assert_eq!(ids.len(), builtin_count + 2);
        assert!(ids.contains(&"p1".to_string()));
        assert!(ids.contains(&"p2".to_string()));
    }

    #[test]
    fn invalid_policy_rejected() {
        let engine = AuthzEngine::new();
        let result = engine.load_policies("this is not valid cedar");
        assert!(result.is_err());
    }

    #[test]
    fn hot_reload_replaces_policies() {
        let engine = AuthzEngine::new();
        engine.load_policies(read_only_policy()).unwrap();

        let user = ChronixPrincipal::new("alice");
        let resource = ChronixResource::measurement("cpu");
        assert!(engine
            .authorize(&user, ChronixAction::Read, &resource)
            .is_allowed());
        assert!(engine
            .authorize(&user, ChronixAction::Write, &resource)
            .is_denied());

        // Hot-reload with admin policy
        engine.load_policies(admin_allow_all_policy()).unwrap();

        // Now only admin role has access
        assert!(engine
            .authorize(&user, ChronixAction::Read, &resource)
            .is_denied());
        let admin = ChronixPrincipal::new("alice").with_role("admin");
        assert!(engine
            .authorize(&admin, ChronixAction::Write, &resource)
            .is_allowed());
    }

    #[test]
    fn load_from_directory() {
        let dir = tempfile::tempdir().unwrap();
        let policy_path = dir.path().join("allow_read.cedar");
        std::fs::write(
            &policy_path,
            r#"permit(principal, action == Chronix::Action::"Read", resource);"#,
        )
        .unwrap();

        // Non-cedar files should be ignored
        std::fs::write(dir.path().join("notes.txt"), "not a policy").unwrap();

        let engine = AuthzEngine::new();
        let count = engine.load_policies_from_dir(dir.path()).unwrap();
        assert_eq!(count, 1);

        let user = ChronixPrincipal::new("alice");
        let resource = ChronixResource::measurement("cpu");
        assert!(engine
            .authorize(&user, ChronixAction::Read, &resource)
            .is_allowed());
    }

    #[test]
    fn empty_directory_loads_zero() {
        let dir = tempfile::tempdir().unwrap();
        let engine = AuthzEngine::new();
        let count = engine.load_policies_from_dir(dir.path()).unwrap();
        assert_eq!(count, 0);
    }

    #[test]
    fn multiple_actions_with_combined_policies() {
        let policies = r#"
            permit(
                principal in Chronix::Role::"ops",
                action == Chronix::Action::"Read",
                resource
            );
            permit(
                principal in Chronix::Role::"ops",
                action == Chronix::Action::"Write",
                resource
            );
            permit(
                principal in Chronix::Role::"admin",
                action,
                resource
            );
        "#;

        let engine = AuthzEngine::new();
        engine.load_policies(policies).unwrap();
        assert_eq!(engine.policy_count(), 3);

        let ops = ChronixPrincipal::new("dave").with_role("ops");
        let admin = ChronixPrincipal::new("eve").with_role("admin");
        let resource = ChronixResource::measurement("cpu");

        // ops can read and write
        assert!(engine
            .authorize(&ops, ChronixAction::Read, &resource)
            .is_allowed());
        assert!(engine
            .authorize(&ops, ChronixAction::Write, &resource)
            .is_allowed());
        // ops cannot delete or admin
        assert!(engine
            .authorize(&ops, ChronixAction::Delete, &resource)
            .is_denied());
        assert!(engine
            .authorize(&ops, ChronixAction::Admin, &resource)
            .is_denied());
        // admin can do everything
        assert!(engine
            .authorize(&admin, ChronixAction::Delete, &resource)
            .is_allowed());
        assert!(engine
            .authorize(&admin, ChronixAction::Forecast, &resource)
            .is_allowed());
    }

    #[test]
    fn forbid_policy_overrides_permit() {
        let policies = r#"
            permit(principal, action == Chronix::Action::"Read", resource);
            forbid(principal, action == Chronix::Action::"Read", resource == Chronix::Measurement::"secret");
        "#;

        let engine = AuthzEngine::new();
        engine.load_policies(policies).unwrap();

        let user = ChronixPrincipal::new("alice");

        // Can read normal measurements
        assert!(engine
            .authorize(
                &user,
                ChronixAction::Read,
                &ChronixResource::measurement("cpu")
            )
            .is_allowed());
        // Cannot read secret measurement (forbid overrides permit)
        assert!(engine
            .authorize(
                &user,
                ChronixAction::Read,
                &ChronixResource::measurement("secret")
            )
            .is_denied());
    }

    #[test]
    fn analytics_authorization() {
        let policies = r#"
            permit(
                principal in Chronix::Role::"data-science",
                action == Chronix::Action::"Forecast",
                resource
            );
            permit(
                principal in Chronix::Role::"data-science",
                action == Chronix::Action::"DetectAnomalies",
                resource
            );
            permit(
                principal in Chronix::Role::"ops",
                action == Chronix::Action::"Read",
                resource
            );
        "#;

        let engine = AuthzEngine::new();
        engine.load_policies(policies).unwrap();

        let ds = ChronixPrincipal::new("data-alice").with_role("data-science");
        let ops = ChronixPrincipal::new("ops-bob").with_role("ops");
        let resource = ChronixResource::measurement("energy");

        // data-science can forecast and detect anomalies
        assert!(engine
            .authorize(&ds, ChronixAction::Forecast, &resource)
            .is_allowed());
        assert!(engine
            .authorize(&ds, ChronixAction::DetectAnomalies, &resource)
            .is_allowed());
        // data-science cannot write
        assert!(engine
            .authorize(&ds, ChronixAction::Write, &resource)
            .is_denied());

        // ops can read but not forecast
        assert!(engine
            .authorize(&ops, ChronixAction::Read, &resource)
            .is_allowed());
        assert!(engine
            .authorize(&ops, ChronixAction::Forecast, &resource)
            .is_denied());
    }

    #[test]
    fn subscribe_action_authorization() {
        let policies = r#"
            permit(
                principal in Chronix::Role::"subscriber",
                action == Chronix::Action::"Subscribe",
                resource
            );
        "#;

        let engine = AuthzEngine::new();
        engine.load_policies(policies).unwrap();

        let sub = ChronixPrincipal::new("stream-user").with_role("subscriber");
        let non_sub = ChronixPrincipal::new("basic-user");
        let resource = ChronixResource::measurement("cpu");

        assert!(engine
            .authorize(&sub, ChronixAction::Subscribe, &resource)
            .is_allowed());
        assert!(engine
            .authorize(&non_sub, ChronixAction::Subscribe, &resource)
            .is_denied());
    }

    #[test]
    fn decision_deny_includes_reasons() {
        let engine = AuthzEngine::new();
        let decision = engine.authorize(
            &ChronixPrincipal::new("nobody"),
            ChronixAction::Write,
            &ChronixResource::measurement("cpu"),
        );
        match decision {
            Decision::Deny { reasons } => {
                assert!(!reasons.is_empty());
                // Only a generic message should be exposed — no policy IDs
                assert!(reasons[0].contains("access denied"));
            }
            Decision::Allow => panic!("Expected deny"),
        }
    }

    #[test]
    fn namespace_scoped_measurement_policy() {
        // Policy: allow reads on any resource that is *in* the "prod" namespace.
        let policies = r#"
            permit(
                principal in Chronix::Role::"reader",
                action == Chronix::Action::"Read",
                resource in Chronix::Namespace::"prod"
            );
        "#;

        let engine = AuthzEngine::new();
        engine.load_policies(policies).unwrap();

        let reader = ChronixPrincipal::new("alice").with_role("reader");

        // Measurement in "prod" namespace → allowed
        let cpu_prod = ChronixResource::measurement("cpu").with_namespace("prod");
        assert!(
            engine
                .authorize(&reader, ChronixAction::Read, &cpu_prod)
                .is_allowed(),
            "measurement in prod namespace should be readable"
        );

        // Measurement in "staging" namespace → denied
        let cpu_staging = ChronixResource::measurement("cpu").with_namespace("staging");
        assert!(
            engine
                .authorize(&reader, ChronixAction::Read, &cpu_staging)
                .is_denied(),
            "measurement in staging namespace should be denied"
        );

        // Measurement with no namespace → denied (not in any namespace)
        let cpu_none = ChronixResource::measurement("cpu");
        assert!(
            engine
                .authorize(&reader, ChronixAction::Read, &cpu_none)
                .is_denied(),
            "measurement without namespace should be denied"
        );

        // Write is not permitted even in prod
        assert!(
            engine
                .authorize(&reader, ChronixAction::Write, &cpu_prod)
                .is_denied(),
            "write should be denied even in prod namespace"
        );
    }

    // ── Namespace authorization tests ────────────────────────────────

    #[test]
    fn namespace_default_deny() {
        let engine = AuthzEngine::new();
        let user = ChronixPrincipal::new("alice");
        let ns = ChronixNamespace::new("production");
        let decision = engine.authorize_namespace(&user, ChronixAction::Read, &ns);
        assert!(decision.is_denied());
    }

    #[test]
    fn namespace_scoped_permit() {
        let policies = r#"
            permit(
                principal in Chronix::Role::"ns_admin",
                action,
                resource == Chronix::Namespace::"production"
            );
        "#;

        let engine = AuthzEngine::new();
        engine.load_policies(policies).unwrap();

        let ns_admin = ChronixPrincipal::new("alice").with_role("ns_admin");
        let production = ChronixNamespace::new("production");
        let staging = ChronixNamespace::new("staging");

        // ns_admin can access "production"
        assert!(engine
            .authorize_namespace(&ns_admin, ChronixAction::Read, &production)
            .is_allowed());
        assert!(engine
            .authorize_namespace(&ns_admin, ChronixAction::Write, &production)
            .is_allowed());
        // ns_admin cannot access other namespaces
        assert!(engine
            .authorize_namespace(&ns_admin, ChronixAction::Read, &staging)
            .is_denied());
    }

    #[test]
    fn namespace_forbid_cross_namespace() {
        let policies = r#"
            permit(
                principal in Chronix::Role::"team_a",
                action,
                resource == Chronix::Namespace::"team_a_ns"
            );
            forbid(
                principal in Chronix::Role::"team_a",
                action,
                resource == Chronix::Namespace::"team_b_ns"
            );
        "#;

        let engine = AuthzEngine::new();
        engine.load_policies(policies).unwrap();

        let team_a = ChronixPrincipal::new("dev1").with_role("team_a");
        let ns_a = ChronixNamespace::new("team_a_ns");
        let ns_b = ChronixNamespace::new("team_b_ns");

        assert!(engine
            .authorize_namespace(&team_a, ChronixAction::Write, &ns_a)
            .is_allowed());
        assert!(engine
            .authorize_namespace(&team_a, ChronixAction::Read, &ns_b)
            .is_denied());
    }

    /// Cross-tenant isolation policy can be loaded and denies
    /// cross-namespace access.
    #[test]
    fn cross_tenant_isolation_policy_loads() {
        let engine = AuthzEngine::new();
        engine.load_cross_tenant_isolation_policy().unwrap();
        assert!(engine
            .policy_ids()
            .contains(&"chronix-builtin-cross-tenant-deny".to_string()));
    }

    /// Cross-tenant isolation policy is idempotent.
    #[test]
    fn cross_tenant_isolation_policy_idempotent() {
        let engine = AuthzEngine::new();
        engine.load_cross_tenant_isolation_policy().unwrap();
        engine.load_cross_tenant_isolation_policy().unwrap();
        // Still only 1 policy (replaced)
        assert_eq!(engine.policy_count(), 1);
    }

    /// Full cross-tenant integration test.
    ///
    /// Creates two namespaces with independent principals and verifies that
    /// the Cedar cross-tenant isolation policy prevents any principal from
    /// accessing resources in another namespace — for all actions.
    #[test]
    fn cross_tenant_full_isolation_integration() {
        let engine = AuthzEngine::new();
        // Use add_policy (not load_policies) to preserve the auto-loaded
        // cross-tenant isolation forbid policy.
        engine
            .add_policy(
                "team_a_permit",
                r#"permit(principal in Chronix::Role::"team_a_dev", action, resource);"#,
            )
            .unwrap();
        engine
            .add_policy(
                "team_b_permit",
                r#"permit(principal in Chronix::Role::"team_b_dev", action, resource);"#,
            )
            .unwrap();

        // Team A principal in namespace "team_a"
        let alice = ChronixPrincipal::new("alice")
            .with_role("team_a_dev")
            .with_namespace("team_a");

        // Team B principal in namespace "team_b"
        let bob = ChronixPrincipal::new("bob")
            .with_role("team_b_dev")
            .with_namespace("team_b");

        // Resources in each namespace
        let cpu_a = ChronixResource::measurement("cpu").with_namespace("team_a");
        let cpu_b = ChronixResource::measurement("cpu").with_namespace("team_b");
        let mem_a = ChronixResource::measurement("memory").with_namespace("team_a");
        let mem_b = ChronixResource::measurement("memory").with_namespace("team_b");

        // ── Same-namespace access: ALLOWED ──
        for action in [
            ChronixAction::Read,
            ChronixAction::Write,
            ChronixAction::Delete,
        ] {
            assert!(
                engine.authorize(&alice, action, &cpu_a).is_allowed(),
                "Alice should access team_a resources ({action:?})"
            );
            assert!(
                engine.authorize(&bob, action, &cpu_b).is_allowed(),
                "Bob should access team_b resources ({action:?})"
            );
        }

        // ── Cross-namespace access: DENIED ──
        for action in [
            ChronixAction::Read,
            ChronixAction::Write,
            ChronixAction::Delete,
        ] {
            assert!(
                engine.authorize(&alice, action, &cpu_b).is_denied(),
                "Alice must NOT access team_b cpu ({action:?})"
            );
            assert!(
                engine.authorize(&alice, action, &mem_b).is_denied(),
                "Alice must NOT access team_b memory ({action:?})"
            );
            assert!(
                engine.authorize(&bob, action, &cpu_a).is_denied(),
                "Bob must NOT access team_a cpu ({action:?})"
            );
            assert!(
                engine.authorize(&bob, action, &mem_a).is_denied(),
                "Bob must NOT access team_a memory ({action:?})"
            );
        }

        // ── Admin override: admin principal CAN cross namespaces ──
        let admin = ChronixPrincipal::new("superadmin")
            .with_role("admin")
            .with_namespace("team_a");
        // Need an admin-allow policy
        engine
            .add_policy(
                "admin_allow",
                r#"permit(principal in Chronix::Role::"admin", action, resource);"#,
            )
            .unwrap();
        assert!(
            engine
                .authorize(&admin, ChronixAction::Read, &cpu_b)
                .is_allowed(),
            "Admin should cross namespace boundaries"
        );
    }

    /// warn_if_no_namespace_policies doesn't panic.
    #[test]
    fn warn_if_no_namespace_policies_works() {
        let engine = AuthzEngine::new();
        // Should log warning but not panic
        engine.warn_if_no_namespace_policies();

        // After loading cross-tenant policy, no warning
        engine.load_cross_tenant_isolation_policy().unwrap();
        engine.warn_if_no_namespace_policies();
    }

    // ── Policy Versioning & Rollback ─────────────────────────

    #[test]
    fn version_increments_on_load() {
        let engine = AuthzEngine::new();
        // Version starts at 1 after auto-loading isolation policy
        let base = engine.current_version();

        engine.load_policies(read_only_policy()).unwrap();
        assert_eq!(engine.current_version(), base + 1);

        engine.load_policies(admin_allow_all_policy()).unwrap();
        assert_eq!(engine.current_version(), base + 2);
    }

    #[test]
    fn version_increments_on_add_and_remove() {
        let engine = AuthzEngine::new();
        let base = engine.current_version();
        engine
            .add_policy(
                "p1",
                r#"permit(principal, action == Chronix::Action::"Read", resource);"#,
            )
            .unwrap();
        assert_eq!(engine.current_version(), base + 1);

        assert!(engine.remove_policy("p1"));
        assert_eq!(engine.current_version(), base + 2);
    }

    #[test]
    fn history_contains_previous_versions() {
        let engine = AuthzEngine::new();
        let base = engine.current_version();
        engine.load_policies(read_only_policy()).unwrap();
        engine.load_policies(admin_allow_all_policy()).unwrap();

        let versions = engine.policy_versions();
        // History contains entries from auto-loaded policy + 2 loads
        assert!(versions.len() >= 2);
        // The last two entries should be the ones from our loads
        let last_two = &versions[versions.len() - 2..];
        assert_eq!(last_two[0].version, base);
        assert_eq!(last_two[1].version, base + 1);
    }

    #[test]
    fn revert_to_previous_version() {
        let engine = AuthzEngine::new();
        let base = engine.current_version();
        engine.load_policies(read_only_policy()).unwrap();
        engine.load_policies(admin_allow_all_policy()).unwrap();

        let user = ChronixPrincipal::new("alice");
        let resource = ChronixResource::measurement("cpu");
        // Currently admin_allow_all — non-admin denied
        assert!(engine
            .authorize(&user, ChronixAction::Read, &resource)
            .is_denied());

        // Revert to the read_only_policy version
        engine.revert_to_version(base + 1).unwrap();
        assert!(engine
            .authorize(&user, ChronixAction::Read, &resource)
            .is_allowed());
        assert!(engine
            .authorize(&user, ChronixAction::Write, &resource)
            .is_denied());
    }

    #[test]
    fn revert_to_nonexistent_version_fails() {
        let engine = AuthzEngine::new();
        engine.load_policies(read_only_policy()).unwrap();
        let err = engine.revert_to_version(999);
        assert!(err.is_err());
        let msg = format!("{}", err.unwrap_err());
        assert!(msg.contains("999"));
    }

    #[test]
    fn revert_itself_creates_history_entry() {
        let engine = AuthzEngine::new();
        let base = engine.current_version();
        engine.load_policies(read_only_policy()).unwrap();
        engine.load_policies(admin_allow_all_policy()).unwrap();
        engine.revert_to_version(base + 1).unwrap();

        let versions = engine.policy_versions();
        // Should have at least 3 entries from our operations
        assert!(versions.len() >= 3);

        // Can revert back to admin_allow_all version
        engine.revert_to_version(base + 2).unwrap();
        let admin = ChronixPrincipal::new("alice").with_role("admin");
        let resource = ChronixResource::measurement("cpu");
        assert!(engine
            .authorize(&admin, ChronixAction::Write, &resource)
            .is_allowed());
    }

    #[test]
    fn history_ring_buffer_bounded() {
        let engine = AuthzEngine::new();
        // Load 15 times — history should only retain MAX_POLICY_VERSIONS
        for _ in 0..15 {
            engine.load_policies(read_only_policy()).unwrap();
        }
        let versions = engine.policy_versions();
        assert!(versions.len() <= super::MAX_POLICY_VERSIONS);
    }

    #[test]
    fn dryrun_does_not_modify_active_set() {
        let engine = AuthzEngine::new();
        engine.load_policies(read_only_policy()).unwrap();
        let v_before = engine.current_version();
        let count_before = engine.policy_count();

        let dryrun_count = engine
            .dryrun_load_policies(admin_allow_all_policy())
            .unwrap();
        assert!(dryrun_count > 0);

        // Version and active set unchanged
        assert_eq!(engine.current_version(), v_before);
        assert_eq!(engine.policy_count(), count_before);

        // Authorization still uses old policy
        let user = ChronixPrincipal::new("alice");
        let resource = ChronixResource::measurement("cpu");
        assert!(engine
            .authorize(&user, ChronixAction::Read, &resource)
            .is_allowed());
    }

    #[test]
    fn dryrun_rejects_invalid_policy() {
        let engine = AuthzEngine::new();
        let result = engine.dryrun_load_policies("not valid cedar syntax {{}}");
        assert!(result.is_err());
    }

    #[test]
    fn policy_version_has_timestamp() {
        let engine = AuthzEngine::new();
        let before = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64;

        engine.load_policies(read_only_policy()).unwrap();

        let versions = engine.policy_versions();
        // At least 1 entry from auto-loaded policy + our load
        assert!(!versions.is_empty());
        // The latest entry should be from our load
        let latest = versions.last().unwrap();
        assert!(latest.timestamp >= before);
    }

    // ── granular admin action tests ─────────────────────────

    #[test]
    fn ent06_admin_super_action_covers_granular() {
        let policies = r#"
            permit(
                principal in Chronix::Role::"admin",
                action == Chronix::Action::"Admin",
                resource
            );
        "#;
        let engine = AuthzEngine::new();
        engine.load_policies(policies).unwrap();

        let admin = ChronixPrincipal::new("alice").with_role("admin");
        let resource = ChronixResource::measurement("cpu");

        // Admin super-action is allowed.
        assert!(engine
            .authorize_admin(&admin, ChronixAction::Admin, &resource)
            .is_allowed());
        // All granular admin actions are covered via fallback.
        assert!(engine
            .authorize_admin(&admin, ChronixAction::ManageNodes, &resource)
            .is_allowed());
        assert!(engine
            .authorize_admin(&admin, ChronixAction::ManageKeys, &resource)
            .is_allowed());
        assert!(engine
            .authorize_admin(&admin, ChronixAction::ManageBackups, &resource)
            .is_allowed());
        assert!(engine
            .authorize_admin(&admin, ChronixAction::ManageModels, &resource)
            .is_allowed());
        assert!(engine
            .authorize_admin(&admin, ChronixAction::ViewCluster, &resource)
            .is_allowed());
    }

    #[test]
    fn ent06_granular_action_without_admin_super() {
        // Grant only ManageModels — must NOT grant other admin ops.
        let policies = r#"
            permit(
                principal in Chronix::Role::"ml-eng",
                action == Chronix::Action::"ManageModels",
                resource
            );
        "#;
        let engine = AuthzEngine::new();
        engine.load_policies(policies).unwrap();

        let ml_eng = ChronixPrincipal::new("bob").with_role("ml-eng");
        let resource = ChronixResource::measurement("cpu");

        assert!(engine
            .authorize_admin(&ml_eng, ChronixAction::ManageModels, &resource)
            .is_allowed());
        // Must NOT have other admin capabilities.
        assert!(engine
            .authorize_admin(&ml_eng, ChronixAction::ManageKeys, &resource)
            .is_denied());
        assert!(engine
            .authorize_admin(&ml_eng, ChronixAction::ManageBackups, &resource)
            .is_denied());
        assert!(engine
            .authorize_admin(&ml_eng, ChronixAction::Admin, &resource)
            .is_denied());
    }

    #[test]
    fn ent06_is_admin_action_classification() {
        assert!(ChronixAction::Admin.is_admin_action());
        assert!(ChronixAction::ManageNodes.is_admin_action());
        assert!(ChronixAction::ManageKeys.is_admin_action());
        assert!(ChronixAction::ManageNamespaces.is_admin_action());
        // Non-admin actions should return false.
        assert!(!ChronixAction::Read.is_admin_action());
        assert!(!ChronixAction::Write.is_admin_action());
        assert!(!ChronixAction::Subscribe.is_admin_action());
    }
}