cedarling 0.0.4

The Cedarling: a high-performance local authorization service powered by the Rust Cedar Engine.
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
// This software is available under the Apache-2.0 license.
// See https://www.apache.org/licenses/LICENSE-2.0.txt for full text.
//
// Copyright (c) 2024, Gluu, Inc.

use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::lock::health_registry::HealthStatus;
use crate::log::MetricsLogEntry;

#[derive(Debug, thiserror::Error)]
pub(super) enum MappingValidationError {
    #[error("missing required field")]
    MissingField,
}

#[derive(Debug, Deserialize)]
pub(super) struct CedarlingLogEntry {
    pub timestamp: String,
    pub log_kind: String,
    pub decision: String,
    pub action: String,
    pub level: Option<String>,
    // Cedarling emits principal as an array of entity strings
    #[serde(default)]
    pub principal: Vec<String>,
    pub resource: String,
    #[serde(default)]
    pub application_id: String,
    pub pdp_id: String,
    // Catch everything else for context_information
    #[serde(flatten)]
    pub extra: HashMap<String, Value>,
}

#[derive(Debug, Deserialize)]
pub(super) struct CedarlingMetricsEntry {
    #[serde(flatten)]
    pub metric: MetricsLogEntry,
    pub pdp_id: String,
    #[serde(default)]
    pub application_id: String,
}

/// Serializes into the lock server's expected format
#[derive(Debug, Serialize)]
pub(super) struct LockServerLogEntry {
    pub creation_date: String,
    pub event_time: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub service: Option<String>,
    pub node_name: String,
    pub event_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub severity_level: Option<String>,
    pub action: String,
    pub decision_result: String,
    pub requested_resource: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub principal_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub client_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context_information: Option<Value>,
}

/// Serializes into the lock server's expected health format.
#[derive(Debug, Serialize, Deserialize)]
pub(crate) struct LockServerHealthEntry {
    pub creation_date: String,
    pub event_time: String,
    pub service: String,
    pub node_name: String,
    pub status: String,
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub engine_status: HashMap<String, HealthStatus>,
}

/// Serializes into the lock server's expected telemetry format (3-map model).
#[derive(Debug, Serialize)]
pub(super) struct LockServerMetricsEntry {
    pub creation_date: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub service: Option<String>,
    pub node_name: String,
    pub status: String,
    pub interval_secs: i64,
    pub policy_stats: HashMap<String, i64>,
    pub error_counters: HashMap<String, i64>,
    pub operational_stats: HashMap<String, i64>,
}

impl TryFrom<CedarlingLogEntry> for LockServerLogEntry {
    type Error = MappingValidationError;

    fn try_from(value: CedarlingLogEntry) -> Result<Self, Self::Error> {
        if value.log_kind.is_empty()
            || value.decision.is_empty()
            || value.action.is_empty()
            || value.resource.is_empty()
        {
            return Err(MappingValidationError::MissingField);
        }

        let mut extra = value.extra;
        let client_id = extra
            .remove("lock_client_id")
            .and_then(|v| v.as_str().map(String::from));
        let context_information = if extra.is_empty() {
            None
        } else {
            Some(Value::Object(extra.into_iter().collect()))
        };

        Ok(Self {
            creation_date: value.timestamp.clone(),
            event_time: value.timestamp,
            service: Some(value.application_id),
            node_name: value.pdp_id,
            event_type: value.log_kind,
            severity_level: value.level,
            action: value.action,
            decision_result: value.decision,
            requested_resource: value.resource,
            principal_id: if value.principal.is_empty() {
                None
            } else {
                Some(value.principal.join(", "))
            },
            client_id,
            context_information,
        })
    }
}

impl TryFrom<CedarlingMetricsEntry> for LockServerMetricsEntry {
    type Error = MappingValidationError;

    fn try_from(value: CedarlingMetricsEntry) -> Result<Self, Self::Error> {
        let timestamp = value.metric.base.timestamp.unwrap_or_default();

        Ok(Self {
            creation_date: timestamp,
            service: (!value.application_id.is_empty()).then_some(value.application_id),
            node_name: value.pdp_id,
            status: "running".to_string(),
            interval_secs: value.metric.interval_secs,
            policy_stats: value.metric.policy_stats,
            error_counters: value.metric.error_counters,
            operational_stats: value.metric.operational_stats,
        })
    }
}

#[cfg(test)]
mod test {
    use std::collections::HashSet;

    use super::*;
    use crate::common::app_types::{ApplicationName, PdpID};
    use crate::log::log_strategy::LogEntryWithClientInfo;
    use crate::log::{
        BaseLogEntry, Decision, DecisionLogEntry, DiagnosticsSummary, LogTokensInfo, LogType,
        MetricsLogEntry, PushedDataInfo,
    };

    /// Verifies that a real `DecisionLogEntry` (wrapped in `LogEntryWithClientInfo`)
    /// is correctly serialized, deserialized into `CedarlingLogEntry`, and then mapped
    /// to the Lock server's `LockServerLogEntry` format with all fields in the right places.
    #[test]
    fn decision_log_entry_maps_to_lock_server_format() {
        let request_id = crate::log::gen_uuid7();
        let base = BaseLogEntry::new_decision(request_id);

        let decision_entry = DecisionLogEntry {
            base,
            policystore_id: "test-store".into(),
            policystore_version: "1.0.0".into(),
            principal: vec!["Jans::User".into(), "Jans::Workload".into()],
            lock_client_id: Some("lock-client-123".to_string()),
            action: "Jans::Action::\"Update\"".to_string(),
            resource: "Jans::Issue::\"random_id\"".to_string(),
            decision: Decision::Allow,
            tokens: LogTokensInfo::empty(),
            decision_time_micro_sec: 42,
            diagnostics: DiagnosticsSummary {
                reason: HashSet::default(),
                errors: Vec::new(),
            },
            pushed_data: Some(PushedDataInfo {
                keys: vec!["extra_context".into()],
            }),
        };

        let pdp_id = PdpID::new();
        let app_name = ApplicationName::from("my_test_app".to_string());

        // Wrap in LogEntryWithClientInfo — this is what the LockService actually receives
        let wrapped = LogEntryWithClientInfo::from_loggable(decision_entry, pdp_id, Some(app_name));

        // Serialize exactly as LockService.log_any() does
        let json_str = serde_json::to_string(&wrapped).expect("serialize wrapped entry");

        // Deserialize into the intermediate CedarlingLogEntry
        let cedarling_entry: CedarlingLogEntry =
            serde_json::from_str(&json_str).expect("deserialize into CedarlingLogEntry");

        // Verify key fields are correctly extracted
        assert_eq!(cedarling_entry.log_kind, "Decision");
        assert_eq!(cedarling_entry.decision, "ALLOW");
        assert_eq!(cedarling_entry.action, "Jans::Action::\"Update\"");
        assert_eq!(cedarling_entry.resource, "Jans::Issue::\"random_id\"");
        assert_eq!(cedarling_entry.application_id, "my_test_app");
        assert_eq!(
            cedarling_entry.principal,
            vec!["Jans::User", "Jans::Workload"]
        );
        assert!(
            !cedarling_entry.pdp_id.is_empty(),
            "pdp_id should not be empty"
        );

        // The extra fields should contain diagnostics, decision_time, etc.
        assert!(
            cedarling_entry.extra.contains_key("diagnostics"),
            "extra should contain diagnostics"
        );
        assert!(
            cedarling_entry
                .extra
                .contains_key("decision_time_micro_sec"),
            "extra should contain decision_time_micro_sec"
        );
        assert!(
            cedarling_entry.extra.contains_key("lock_client_id"),
            "extra should contain lock_client_id"
        );

        // Map to LockServerLogEntry
        let lock_entry =
            LockServerLogEntry::try_from(cedarling_entry).expect("map to LockServerLogEntry");

        // Verify all Lock server fields
        assert!(!lock_entry.creation_date.is_empty());
        assert!(!lock_entry.event_time.is_empty());
        assert_eq!(lock_entry.service.as_deref(), Some("my_test_app"));
        assert!(!lock_entry.node_name.is_empty()); // pdp_id
        assert_eq!(lock_entry.event_type, "Decision");
        assert_eq!(lock_entry.action, "Jans::Action::\"Update\"");
        assert_eq!(lock_entry.decision_result, "ALLOW");
        assert_eq!(lock_entry.requested_resource, "Jans::Issue::\"random_id\"");
        assert_eq!(
            lock_entry.principal_id.as_deref(),
            Some("Jans::User, Jans::Workload")
        );
        // lock_client_id should be extracted from extra into client_id
        assert_eq!(lock_entry.client_id.as_deref(), Some("lock-client-123"));
        // Remaining extra fields (diagnostics, decision_time, etc.) go into context_information
        let ctx = lock_entry
            .context_information
            .expect("context_information should be present");
        assert!(ctx.get("diagnostics").is_some());
        assert!(ctx.get("decision_time_micro_sec").is_some());
        assert!(ctx.get("policystore_id").is_some());
        assert!(ctx.get("pushed_data").is_some());
    }

    /// Verifies that a [`DecisionLogEntry`] with a DENY decision maps correctly.
    #[test]
    fn deny_decision_maps_correctly() {
        let base = BaseLogEntry::new_decision(crate::log::gen_uuid7());
        let entry = DecisionLogEntry {
            base,
            policystore_id: "store".into(),
            policystore_version: "1.0".into(),
            principal: vec![],
            lock_client_id: None,
            action: "Jans::Action::\"Read\"".to_string(),
            resource: "Jans::Resource::\"doc\"".to_string(),
            decision: Decision::Deny,
            tokens: LogTokensInfo::empty(),
            decision_time_micro_sec: 100,
            diagnostics: DiagnosticsSummary {
                reason: HashSet::default(),
                errors: Vec::new(),
            },
            pushed_data: None,
        };

        let pdp_id = PdpID::new();
        let wrapped = LogEntryWithClientInfo::from_loggable(entry, pdp_id, None);
        let json_str = serde_json::to_string(&wrapped).expect("serialize wrapped DecisionLogEntry");
        let cedarling: CedarlingLogEntry =
            serde_json::from_str(&json_str).expect("deserialize to CedarlingLogEntry");
        let lock_entry = LockServerLogEntry::try_from(cedarling)
            .expect("convert CedarlingLogEntry to LockServerLogEntry");

        assert_eq!(lock_entry.decision_result, "DENY");
        assert_eq!(lock_entry.principal_id, None); // empty principal list
        assert_eq!(lock_entry.client_id, None); // no lock_client_id
        assert_eq!(lock_entry.service, Some(String::new())); // no app_name → empty string
        assert_eq!(lock_entry.severity_level, None); // Decision logs have no level
    }

    #[test]
    fn mapping_fails_when_required_field_is_empty() {
        let cedarling = CedarlingLogEntry {
            timestamp: "2026-03-23T12:00:00Z".to_string(),
            log_kind: "Decision".to_string(),
            decision: "ALLOW".to_string(),
            action: String::new(),
            level: None,
            principal: vec![],
            resource: "Jans::Resource::\"doc\"".to_string(),
            application_id: "my_test_app".to_string(),
            pdp_id: "pdp-1".to_string(),
            extra: HashMap::new(),
        };

        let err = LockServerLogEntry::try_from(cedarling)
            .expect_err("empty action must fail LockServerLogEntry mapping");
        assert!(
            matches!(err, MappingValidationError::MissingField),
            "expected MissingField when a required input field is empty"
        );
    }

    #[test]
    fn metrics_log_entry_maps_to_lock_server_format() {
        let request_id = crate::log::gen_uuid7();
        let base = BaseLogEntry::new_metric(request_id);

        let mut policy_stats = std::collections::HashMap::new();
        policy_stats.insert("allow_read_docs".to_string(), 340);
        policy_stats.insert("deny_admin".to_string(), 12);

        let mut operational_stats = std::collections::HashMap::new();
        operational_stats.insert("authz.requests_total".to_string(), 1240);
        operational_stats.insert("authz.decision_allow".to_string(), 1218);

        let mut error_counters = std::collections::HashMap::new();
        error_counters.insert("jwt.validation_failed".to_string(), 8);

        let metrics_entry = MetricsLogEntry {
            base,
            policy_stats,
            error_counters,
            operational_stats,
            interval_secs: 60,
        };

        let pdp_id = PdpID::new();
        let app_name = ApplicationName::from("jans-auth".to_string());

        let wrapped = LogEntryWithClientInfo::from_loggable(metrics_entry, pdp_id, Some(app_name));

        let json_str = serde_json::to_string(&wrapped).expect("serialize wrapped metrics entry");

        let cedarling_entry: CedarlingMetricsEntry =
            serde_json::from_str(&json_str).expect("deserialize into CedarlingMetricsEntry");

        assert_eq!(cedarling_entry.metric.base.log_kind, LogType::Metric);
        assert_eq!(cedarling_entry.metric.interval_secs, 60);
        assert_eq!(cedarling_entry.application_id, "jans-auth");
        assert_eq!(
            cedarling_entry.metric.policy_stats.get("allow_read_docs"),
            Some(&340)
        );
        assert_eq!(
            cedarling_entry
                .metric
                .error_counters
                .get("jwt.validation_failed"),
            Some(&8)
        );
        assert_eq!(
            cedarling_entry
                .metric
                .operational_stats
                .get("authz.requests_total"),
            Some(&1240)
        );

        let lock_entry = LockServerMetricsEntry::try_from(cedarling_entry)
            .expect("map to LockServerMetricsEntry");

        assert_eq!(lock_entry.service.as_deref(), Some("jans-auth"));
        assert_eq!(lock_entry.node_name, pdp_id.to_string());
        assert_eq!(lock_entry.status, "running");
        assert_eq!(lock_entry.interval_secs, 60);
        assert_eq!(lock_entry.policy_stats.get("allow_read_docs"), Some(&340));
        assert_eq!(
            lock_entry.error_counters.get("jwt.validation_failed"),
            Some(&8)
        );
        assert_eq!(
            lock_entry.operational_stats.get("authz.requests_total"),
            Some(&1240)
        );
    }

    #[test]
    fn metrics_log_entry_without_app_name_maps_correctly() {
        let base = BaseLogEntry::new_metric(crate::log::gen_uuid7());

        let metrics_entry = MetricsLogEntry {
            base,
            policy_stats: std::collections::HashMap::new(),
            error_counters: std::collections::HashMap::new(),
            operational_stats: std::collections::HashMap::new(),
            interval_secs: 30,
        };

        let pdp_id = PdpID::new();
        let wrapped = LogEntryWithClientInfo::from_loggable(metrics_entry, pdp_id, None);
        let json_str = serde_json::to_string(&wrapped).expect("serialize wrapped MetricsLogEntry");
        let cedarling: CedarlingMetricsEntry =
            serde_json::from_str(&json_str).expect("deserialize to CedarlingMetricsEntry");
        let lock_entry = LockServerMetricsEntry::try_from(cedarling)
            .expect("convert CedarlingMetricsEntry to LockServerMetricsEntry");

        assert_eq!(lock_entry.service, None);
        assert_eq!(lock_entry.status, "running");
        assert_eq!(lock_entry.interval_secs, 30);
        assert!(lock_entry.policy_stats.is_empty());
        assert!(lock_entry.error_counters.is_empty());
    }
}