chronix-security 0.4.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
//! Chronix authorization model types.
//!
//! Defines the actions, resources, and principals used for Cedar policy
//! evaluation.
//!
//! Entity type names are parsed once and cached via `LazyLock` to avoid
//! per-request `from_str` + `expect()` overhead on the hot path.

use std::collections::{BTreeMap, HashMap, HashSet};
use std::str::FromStr;
use std::sync::LazyLock;

use cedar_policy::{Entity, EntityId, EntityTypeName, EntityUid, RestrictedExpression};
use serde::{Deserialize, Serialize};

use crate::authz::error::AuthzError;

// ── Cached entity type names ────────────────────────────────────────

/// Pre-parsed entity type name for `Chronix::Action`.
static TYPE_ACTION: LazyLock<EntityTypeName> =
    LazyLock::new(|| EntityTypeName::from_str("Chronix::Action").expect("valid type"));

/// Pre-parsed entity type name for `Chronix::User`.
static TYPE_USER: LazyLock<EntityTypeName> =
    LazyLock::new(|| EntityTypeName::from_str("Chronix::User").expect("valid type"));

/// Pre-parsed entity type name for `Chronix::Role`.
static TYPE_ROLE: LazyLock<EntityTypeName> =
    LazyLock::new(|| EntityTypeName::from_str("Chronix::Role").expect("valid type"));

/// Pre-parsed entity type name for `Chronix::Measurement`.
static TYPE_MEASUREMENT: LazyLock<EntityTypeName> =
    LazyLock::new(|| EntityTypeName::from_str("Chronix::Measurement").expect("valid type"));

/// Pre-parsed entity type name for `Chronix::Namespace`.
static TYPE_NAMESPACE: LazyLock<EntityTypeName> =
    LazyLock::new(|| EntityTypeName::from_str("Chronix::Namespace").expect("valid type"));

/// Returns the cached `Chronix::Role` entity type name.
pub(crate) fn role_type_name() -> &'static EntityTypeName {
    &TYPE_ROLE
}

// ── Actions ─────────────────────────────────────────────────────────

/// Actions that can be authorized in Chronix.
///
/// The original `Admin` variant was a catch-all for all
/// administrative operations.  Granular admin actions (below) allow
/// policies to grant least-privilege access — e.g. granting
/// `ManageModels` without also granting `ManageKeys`.  The `Admin`
/// variant is retained as a super-action that implies all granular
/// admin actions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ChronixAction {
    /// Write data points.
    Write,
    /// Read / query data.
    Read,
    /// Delete data (series or measurement).
    Delete,
    /// Super-admin — implies all granular admin actions below.
    Admin,
    /// Create or manage rollups.
    CreateRollup,
    /// Execute forecast queries.
    Forecast,
    /// Execute anomaly detection.
    DetectAnomalies,
    /// Subscribe to CDC events.
    Subscribe,

    // ── granular admin actions ──────────────────────────────
    /// Manage cluster nodes (register, decommission, heartbeat).
    ManageNodes,
    /// Manage regions (create, state transitions).
    ManageRegions,
    /// View cluster topology and health (read-only admin).
    ViewCluster,
    /// Trigger cluster rebalancing.
    ManageCluster,
    /// Manage analytics models (list, delete, retrain).
    ManageModels,
    /// Manage API keys (create, list, revoke).
    ManageKeys,
    /// Manage runtime configuration (e.g. log levels).
    ManageConfig,
    /// Manage backups and point-in-time restores.
    ManageBackups,
    /// Create, list, delete namespaces and view usage.
    ManageNamespaces,
}

impl ChronixAction {
    /// Convert to the Cedar action entity UID.
    #[must_use]
    pub fn to_entity_uid(&self) -> EntityUid {
        let name = match self {
            Self::Write => "Write",
            Self::Read => "Read",
            Self::Delete => "Delete",
            Self::Admin => "Admin",
            Self::CreateRollup => "CreateRollup",
            Self::Forecast => "Forecast",
            Self::DetectAnomalies => "DetectAnomalies",
            Self::Subscribe => "Subscribe",
            Self::ManageNodes => "ManageNodes",
            Self::ManageRegions => "ManageRegions",
            Self::ViewCluster => "ViewCluster",
            Self::ManageCluster => "ManageCluster",
            Self::ManageModels => "ManageModels",
            Self::ManageKeys => "ManageKeys",
            Self::ManageConfig => "ManageConfig",
            Self::ManageBackups => "ManageBackups",
            Self::ManageNamespaces => "ManageNamespaces",
        };
        EntityUid::from_type_name_and_id(TYPE_ACTION.clone(), EntityId::new(name))
    }

    /// Returns `true` if this is a granular admin action (or `Admin` itself).
    ///
    /// Useful for authorization engines that want to grant `Admin` as a
    /// super-action implying all granular admin sub-actions.
    #[must_use]
    pub fn is_admin_action(&self) -> bool {
        matches!(
            self,
            Self::Admin
                | Self::ManageNodes
                | Self::ManageRegions
                | Self::ViewCluster
                | Self::ManageCluster
                | Self::ManageModels
                | Self::ManageKeys
                | Self::ManageConfig
                | Self::ManageBackups
                | Self::ManageNamespaces
        )
    }
}

impl std::fmt::Display for ChronixAction {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            Self::Write => "Write",
            Self::Read => "Read",
            Self::Delete => "Delete",
            Self::Admin => "Admin",
            Self::CreateRollup => "CreateRollup",
            Self::Forecast => "Forecast",
            Self::DetectAnomalies => "DetectAnomalies",
            Self::Subscribe => "Subscribe",
            Self::ManageNodes => "ManageNodes",
            Self::ManageRegions => "ManageRegions",
            Self::ViewCluster => "ViewCluster",
            Self::ManageCluster => "ManageCluster",
            Self::ManageModels => "ManageModels",
            Self::ManageKeys => "ManageKeys",
            Self::ManageConfig => "ManageConfig",
            Self::ManageBackups => "ManageBackups",
            Self::ManageNamespaces => "ManageNamespaces",
        };
        write!(f, "{s}")
    }
}

// ── Resource ────────────────────────────────────────────────────────

/// A Chronix resource subject to authorization.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ChronixResource {
    /// Measurement name.
    pub measurement: String,
    /// Optional namespace the measurement belongs to.
    ///
    /// When set, the Cedar entity for this resource will have the
    /// corresponding `Chronix::Namespace` as a parent, enabling
    /// namespace-scoped policies such as:
    ///
    /// ```cedar
    /// permit(
    ///   principal in Chronix::Role::"ns_reader",
    ///   action == Chronix::Action::"Read",
    ///   resource in Chronix::Namespace::"production"
    /// );
    /// ```
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub namespace: Option<String>,
    /// Optional tag constraints (for fine-grained access).
    #[serde(default)]
    pub tags: BTreeMap<String, String>,
}

impl ChronixResource {
    /// Create a new resource for a measurement.
    #[must_use]
    pub fn measurement(name: impl Into<String>) -> Self {
        Self {
            measurement: name.into(),
            namespace: None,
            tags: BTreeMap::new(),
        }
    }

    /// Set the namespace this measurement belongs to.
    ///
    /// This causes the measurement entity to have the namespace as a
    /// Cedar parent, enabling `resource in Chronix::Namespace::"..."` policies.
    #[must_use]
    pub fn with_namespace(mut self, ns: impl Into<String>) -> Self {
        self.namespace = Some(ns.into());
        self
    }

    /// Maximum number of tags allowed on a resource entity.
    /// Prevents Cedar entity attribute explosion on high-cardinality tags.
    const MAX_TAGS: usize = 64;

    /// Add a tag constraint.
    #[must_use]
    pub fn with_tag(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        if self.tags.len() < Self::MAX_TAGS {
            self.tags.insert(key.into(), value.into());
        } else {
            tracing::warn!(
                max = Self::MAX_TAGS,
                "tag limit reached on resource entity; \
                 additional tags are ignored for Cedar evaluation"
            );
        }
        self
    }

    /// Convert to a Cedar entity.
    ///
    /// If a namespace is set, the namespace entity is included as a
    /// parent so that `resource in Chronix::Namespace::"..."` policies
    /// evaluate correctly.
    pub fn to_entity(&self) -> Result<Entity, AuthzError> {
        let uid = EntityUid::from_type_name_and_id(
            TYPE_MEASUREMENT.clone(),
            EntityId::new(&self.measurement),
        );

        let mut attrs: HashMap<String, RestrictedExpression> = HashMap::new();
        attrs.insert(
            "name".into(),
            RestrictedExpression::new_string(self.measurement.clone()),
        );

        // Add tags as a record attribute
        if !self.tags.is_empty() {
            let tag_pairs: Vec<(String, RestrictedExpression)> = self
                .tags
                .iter()
                .map(|(k, v)| (k.clone(), RestrictedExpression::new_string(v.clone())))
                .collect();
            attrs.insert(
                "tags".into(),
                RestrictedExpression::new_record(tag_pairs)
                    .map_err(|e| AuthzError::Internal(format!("record construction: {e}")))?,
            );
        }

        // Add namespace as a parent entity for hierarchical policies.
        let mut parents = HashSet::new();
        if let Some(ns) = &self.namespace {
            attrs.insert(
                "namespace".into(),
                RestrictedExpression::new_string(ns.clone()),
            );
            parents.insert(EntityUid::from_type_name_and_id(
                TYPE_NAMESPACE.clone(),
                EntityId::new(ns),
            ));
        }

        Entity::new(uid, attrs, parents)
            .map_err(|e| AuthzError::Internal(format!("Measurement entity: {e}")))
    }

    /// Build the namespace parent entity (if set).
    ///
    /// Call this to include in the Cedar entity set so the authorizer
    /// can resolve the parent relationship.
    pub fn namespace_entity(&self) -> Option<Entity> {
        self.namespace
            .as_ref()
            .and_then(|ns| ChronixNamespace::new(ns).to_entity().ok())
    }

    /// Convert to the Cedar entity UID.
    #[must_use]
    pub fn to_entity_uid(&self) -> EntityUid {
        EntityUid::from_type_name_and_id(TYPE_MEASUREMENT.clone(), EntityId::new(&self.measurement))
    }
}

// ── Namespace resource ──────────────────────────────────────────────

/// A Chronix namespace subject to authorization.
///
/// Cedar policies can reference `Chronix::Namespace` entities with
/// a `name` attribute, enabling rules like:
///
/// ```cedar
/// permit(
///   principal in Chronix::Role::"ns_admin",
///   action,
///   resource == Chronix::Namespace::"production"
/// );
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ChronixNamespace {
    /// Namespace identifier.
    pub name: String,
}

impl ChronixNamespace {
    /// Create a namespace resource.
    #[must_use]
    pub fn new(name: impl Into<String>) -> Self {
        Self { name: name.into() }
    }

    /// Convert to a Cedar entity.
    pub fn to_entity(&self) -> Result<Entity, AuthzError> {
        let uid =
            EntityUid::from_type_name_and_id(TYPE_NAMESPACE.clone(), EntityId::new(&self.name));

        let mut attrs: HashMap<String, RestrictedExpression> = HashMap::new();
        attrs.insert(
            "name".into(),
            RestrictedExpression::new_string(self.name.clone()),
        );

        Entity::new(uid, attrs, HashSet::new())
            .map_err(|e| AuthzError::Internal(format!("Namespace entity: {e}")))
    }

    /// Convert to the Cedar entity UID.
    #[must_use]
    pub fn to_entity_uid(&self) -> EntityUid {
        EntityUid::from_type_name_and_id(TYPE_NAMESPACE.clone(), EntityId::new(&self.name))
    }
}

// ── Principal ───────────────────────────────────────────────────────

/// A Chronix principal (authenticated identity).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ChronixPrincipal {
    /// The principal identifier (e.g., API key name, JWT subject, cert CN).
    pub id: String,
    /// Roles assigned to this principal.
    #[serde(default)]
    pub roles: Vec<String>,
    /// Namespace the principal belongs to (for cross-tenant isolation).
    /// When set, the Cedar cross-tenant isolation policy will forbid access
    /// to resources in a different namespace unless the principal is an admin.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub namespace: Option<String>,
}

impl ChronixPrincipal {
    /// Create a new principal.
    #[must_use]
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            roles: Vec::new(),
            namespace: None,
        }
    }

    /// Add a role to the principal.
    #[must_use]
    pub fn with_role(mut self, role: impl Into<String>) -> Self {
        self.roles.push(role.into());
        self
    }

    /// Set the namespace for this principal (for cross-tenant isolation).
    #[must_use]
    pub fn with_namespace(mut self, namespace: impl Into<String>) -> Self {
        self.namespace = Some(namespace.into());
        self
    }

    /// Convert to a Cedar entity (includes role membership as parents).
    pub fn to_entity(&self) -> Result<Entity, AuthzError> {
        let uid = EntityUid::from_type_name_and_id(TYPE_USER.clone(), EntityId::new(&self.id));

        let parents: HashSet<EntityUid> = self
            .roles
            .iter()
            .map(|role| EntityUid::from_type_name_and_id(TYPE_ROLE.clone(), EntityId::new(role)))
            .collect();

        let mut attrs: HashMap<String, RestrictedExpression> = HashMap::new();
        attrs.insert(
            "name".into(),
            RestrictedExpression::new_string(self.id.clone()),
        );

        // Include namespace attribute so the cross-tenant isolation
        // Cedar policy (`principal has namespace`) can evaluate correctly.
        if let Some(ref ns) = self.namespace {
            attrs.insert(
                "namespace".into(),
                RestrictedExpression::new_string(ns.clone()),
            );
        }

        Entity::new(uid, attrs, parents)
            .map_err(|e| AuthzError::Internal(format!("User entity: {e}")))
    }

    /// Convert to the Cedar entity UID.
    #[must_use]
    pub fn to_entity_uid(&self) -> EntityUid {
        EntityUid::from_type_name_and_id(TYPE_USER.clone(), EntityId::new(&self.id))
    }
}

// ── Decision ────────────────────────────────────────────────────────

/// The result of an authorization decision.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Decision {
    /// Access is allowed.
    Allow,
    /// Access is denied.
    Deny {
        /// Human-readable reasons for the denial.
        reasons: Vec<String>,
    },
}

impl Decision {
    /// Returns `true` if the decision is `Allow`.
    #[must_use]
    pub fn is_allowed(&self) -> bool {
        matches!(self, Self::Allow)
    }

    /// Returns `true` if the decision is `Deny`.
    #[must_use]
    pub fn is_denied(&self) -> bool {
        !self.is_allowed()
    }
}

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

    #[test]
    fn action_display() {
        assert_eq!(ChronixAction::Write.to_string(), "Write");
        assert_eq!(
            ChronixAction::DetectAnomalies.to_string(),
            "DetectAnomalies"
        );
    }

    #[test]
    fn action_to_entity_uid() {
        let uid = ChronixAction::Read.to_entity_uid();
        assert!(uid.to_string().contains("Read"));
    }

    #[test]
    fn resource_builder() {
        let res = ChronixResource::measurement("cpu").with_tag("host", "server-01");
        assert_eq!(res.measurement, "cpu");
        assert_eq!(res.tags.get("host").unwrap(), "server-01");
    }

    #[test]
    fn resource_to_entity() {
        let res = ChronixResource::measurement("cpu");
        let entity = res.to_entity().unwrap();
        assert!(entity.uid().to_string().contains("cpu"));
    }

    #[test]
    fn principal_builder() {
        let p = ChronixPrincipal::new("alice")
            .with_role("admin")
            .with_role("data-science");
        assert_eq!(p.id, "alice");
        assert_eq!(p.roles.len(), 2);
    }

    #[test]
    fn principal_to_entity() {
        let p = ChronixPrincipal::new("bob").with_role("reader");
        let entity = p.to_entity().unwrap();
        assert!(entity.uid().to_string().contains("bob"));
    }

    #[test]
    fn decision_is_allowed() {
        assert!(Decision::Allow.is_allowed());
        assert!(!Decision::Deny { reasons: vec![] }.is_allowed());
    }

    #[test]
    fn decision_is_denied() {
        assert!(Decision::Deny {
            reasons: vec!["no policy".into()]
        }
        .is_denied());
        assert!(!Decision::Allow.is_denied());
    }

    #[test]
    fn action_serde_roundtrip() {
        let action = ChronixAction::Forecast;
        let json = serde_json::to_string(&action).unwrap();
        let parsed: ChronixAction = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, action);
    }

    #[test]
    fn resource_serde_roundtrip() {
        let res = ChronixResource::measurement("cpu").with_tag("host", "s1");
        let json = serde_json::to_string(&res).unwrap();
        let parsed: ChronixResource = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, res);
    }

    #[test]
    fn principal_serde_roundtrip() {
        let p = ChronixPrincipal::new("alice").with_role("admin");
        let json = serde_json::to_string(&p).unwrap();
        let parsed: ChronixPrincipal = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, p);
    }

    #[test]
    fn namespace_builder() {
        let ns = ChronixNamespace::new("production");
        assert_eq!(ns.name, "production");
    }

    #[test]
    fn namespace_to_entity() {
        let ns = ChronixNamespace::new("staging");
        let entity = ns.to_entity().unwrap();
        assert!(entity.uid().to_string().contains("staging"));
    }

    #[test]
    fn namespace_serde_roundtrip() {
        let ns = ChronixNamespace::new("production");
        let json = serde_json::to_string(&ns).unwrap();
        let parsed: ChronixNamespace = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, ns);
    }

    #[test]
    fn resource_with_namespace_builder() {
        let res = ChronixResource::measurement("cpu")
            .with_namespace("production")
            .with_tag("host", "s1");
        assert_eq!(res.measurement, "cpu");
        assert_eq!(res.namespace.as_deref(), Some("production"));
        assert_eq!(res.tags.get("host").unwrap(), "s1");
    }

    #[test]
    fn resource_with_namespace_entity_has_parent() {
        let res = ChronixResource::measurement("cpu").with_namespace("prod");
        let entity = res.to_entity().unwrap();
        // Verify the entity UID is correct
        assert!(entity.uid().to_string().contains("cpu"));
        // Verify the namespace entity helper returns the right thing
        let ns = res
            .namespace_entity()
            .expect("should have namespace entity");
        assert!(ns.uid().to_string().contains("prod"));
        assert!(ns.uid().to_string().contains("Namespace"));
    }

    #[test]
    fn resource_without_namespace_has_no_parent() {
        let res = ChronixResource::measurement("cpu");
        let entity = res.to_entity().unwrap();
        assert!(entity.uid().to_string().contains("cpu"));
        assert!(res.namespace_entity().is_none());
    }

    #[test]
    fn resource_namespace_entity_helper() {
        let res = ChronixResource::measurement("cpu").with_namespace("staging");
        let ns_entity = res
            .namespace_entity()
            .expect("should produce namespace entity");
        assert!(ns_entity.uid().to_string().contains("staging"));

        let res_no_ns = ChronixResource::measurement("cpu");
        assert!(res_no_ns.namespace_entity().is_none());
    }

    #[test]
    fn resource_with_namespace_serde_roundtrip() {
        let res = ChronixResource::measurement("cpu")
            .with_namespace("prod")
            .with_tag("host", "s1");
        let json = serde_json::to_string(&res).unwrap();
        let parsed: ChronixResource = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, res);
        assert_eq!(parsed.namespace.as_deref(), Some("prod"));
    }
}