cedarling 0.0.65

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
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
// 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.

#![deny(missing_docs)]
#![warn(unreachable_pub)]
#![allow(clippy::missing_errors_doc)]

//! # Cedarling
//! The Cedarling is a performant local authorization service that runs the Rust Cedar Engine.
//! Cedar policies and schema are loaded at startup from a locally cached "Policy Store".
//! In simple terms, the Cedarling returns the answer: should the application allow this action on this resource given these JWT tokens.
//! "Fit for purpose" policies help developers build a better user experience.
//! For example, why display form fields that a user is not authorized to see?
//! The Cedarling is a more productive and flexible way to handle authorization.

mod async_sleep;
mod authz;
mod bootstrap_config;
mod common;
mod context_data_api;
mod entity_builder;
mod http;
mod http_utils;
mod init;
mod jwt;
mod lock;
mod log;
// is reexported in hidden bindings module
#[doc(hidden)]
pub mod sparkv;

#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "blocking")]
pub mod blocking;

#[doc(hidden)]
#[cfg(test)]
mod tests;

use std::collections::{HashMap, HashSet};
use std::{fmt::Write, sync::Arc};

use crate::authz::metrics::MetricsCollector;
use crate::context_data_api::DataStore;
pub use crate::context_data_api::{
    CedarType, CedarValueMapper, ConfigValidationError, DataApi, DataEntry, DataError,
    DataStoreConfig, DataStoreStats, DataValidator, ExtensionValue, ValidationConfig,
    ValidationError, ValidationResult, ValueMappingError,
};
pub use crate::jwt::TrustedIssuerLoadingInfo;
use authz::Authz;
pub use authz::request::{
    AuthorizeMultiIssuerRequest, BatchAuthorizeMultiIssuerRequest, BatchAuthorizeResponse,
    BatchAuthorizeUnsignedRequest, BatchItem, CedarEntityMapping, EntityData, RequestUnsigned,
    TokenInput,
};
pub use authz::{AuthorizeError, AuthorizeResult, BatchItemError, MultiIssuerAuthorizeResult};
pub use bootstrap_config::*;
/// Identifier of a Cedar policy, re-exported from [`cedar_policy`] so callers can
/// pass the policy IDs from `response.diagnostics().reason()` to the annotation
/// lookup methods without depending on `cedar_policy` directly.
pub use cedar_policy::PolicyId;
use common::app_types::{self, ApplicationName};
#[cfg(feature = "tools")]
pub use common::policy_store::validate::{
    Diagnostic, LevelResult, ValidateInfraError, ValidationReport,
};
pub use common::policy_store::{PolicyEffect, PolicyMetadata};
pub use http::HttpClientConfig;
use init::ServiceFactory;
use init::policy_store::{LoadedPolicyStore, load_policy_store};
use init::policy_store_refresh::{
    AuthzRebuilder, PolicyStoreRefreshHandle, RefreshSource, RefreshWorkerSeed, WorkerContext,
    spawn_refresh_worker,
};
use init::service_config::{ServiceConfig, ServiceConfigError};
use init::service_factory::ServiceInitError;
use lock::InitLockServiceError;
use lock::health_registry::HealthStatus;
use log::interface::LogWriter;
use log::{BaseLogEntry, LogEntry};
pub use log::{LogLevel, LogStorage};

use semver::Version;

/// Git commit hash at build time (`None` if git is unavailable or
/// `CEDARLING_BUILD_COMMIT` was not set at compile time).
const BUILD_COMMIT: Option<&str> = option_env!("CEDARLING_BUILD_COMMIT");
/// Build timestamp in RFC 3339 format (`None` if
/// `CEDARLING_BUILD_TIMESTAMP` was not set at compile time).
const BUILD_TIMESTAMP: Option<&str> = option_env!("CEDARLING_BUILD_TIMESTAMP");

#[doc(hidden)]
pub mod bindings {
    pub use cedar_policy;

    pub use super::log::{
        AuthorizationLogInfo, Decision, Diagnostics, LogEntry, PolicyEvaluationError,
    };
    pub use crate::http::spawn_task;
    pub use crate::sparkv;
    pub use serde_json;
    pub use serde_yaml_ng;
}

/// Errors that can occur during initialization Cedarling.
#[derive(Debug, thiserror::Error)]
pub enum InitCedarlingError {
    /// Error while preparing config for internal services
    #[error(transparent)]
    ServiceConfig(#[from] ServiceConfigError),
    /// Error while initializing a Service
    #[error(transparent)]
    ServiceInit(#[from] ServiceInitError),
    /// Error while parse [`BootstrapConfigRaw`]
    #[error(transparent)]
    BootstrapConfigLoading(#[from] BootstrapConfigLoadingError),
    /// Error while initializing the `DataStore` (invalid configuration)
    #[error(transparent)]
    DataStoreInit(#[from] ConfigValidationError),
    #[cfg(feature = "blocking")]
    /// Error while init tokio runtime
    #[error(transparent)]
    RuntimeInit(std::io::Error),
    /// Error returned when Cedarling fails to obtain client credentials for sending
    /// logs to the Lock Server.
    #[error("failed to initialize the Lock Service: {0}")]
    InitLockService(#[from] InitLockServiceError),
}

/// The instance of the Cedarling application.
/// It is safe to share between threads.
#[derive(Clone)]
pub struct Cedarling {
    log: log::Logger,
    /// Wrapped in [`ArcSwap`] so the policy-store refresh worker can publish a
    /// freshly built [`Authz`] (with new policy store, rebuilt JWT service and
    /// entity builder) atomically. Every public method snapshots via
    /// [`ArcSwap::load`] so an in-flight authorization keeps using the
    /// pre-swap instance.
    authz: Arc<arc_swap::ArcSwap<Authz>>,
    data: Arc<DataStore>,
    /// Held purely for its `Drop` side effect: dropping the last `Arc` closes
    /// the worker's `oneshot` shutdown channel so the background refresh loop
    /// exits when [`Cedarling`] goes away. The leading `_` tells the compiler
    /// the field is intentionally not read.
    _refresh_handle: Option<Arc<PolicyStoreRefreshHandle>>,
}

impl Cedarling {
    /// Create a new instance of the Cedarling application.
    /// Initialize instance from enviroment variables and from config.
    /// Configuration structure has lower priority.
    #[cfg(not(target_arch = "wasm32"))]
    pub async fn new_with_env(
        raw_config: Option<BootstrapConfigRaw>,
    ) -> Result<Cedarling, InitCedarlingError> {
        let config = BootstrapConfig::from_raw_config_and_env(raw_config)?;
        Self::new(&config).await
    }

    /// Create a new instance of the Cedarling application.
    pub async fn new(config: &BootstrapConfig) -> Result<Cedarling, InitCedarlingError> {
        let pdp_id = app_types::PdpID::new();
        let app_name = (!config.application_name.is_empty())
            .then(|| ApplicationName::from(config.application_name.clone()));

        let metrics = Arc::new(
            if config
                .lock_config
                .as_ref()
                .is_some_and(|c| c.telemetry_interval.is_some())
            {
                MetricsCollector::new(0)
            } else {
                MetricsCollector::disabled()
            },
        );

        let log = crate::log::init_logger(
            &config.log_config,
            pdp_id,
            app_name,
            config.lock_config.as_ref(),
            metrics.clone(),
            config.http_client_config,
        )
        .await?;

        log.log_any(
            LogEntry::new(BaseLogEntry::new_system_opt_request_id(
                LogLevel::INFO,
                None,
            ))
            .set_message("Cedarling initialization started".to_string())
            .set_build_info(BUILD_COMMIT, BUILD_TIMESTAMP),
        );

        // Bootstrap-load: build HttpClient + load policy store. Returns the
        // service config plus the refresh-worker seed in one shot so the
        // seed values (initial body hash, initial cache validators) stay in
        // lexical scope right next to the refresh-worker spawn below.
        let (service_config, refresh_seed) = perform_bootstrap_load(config, &log).await?;

        let policy_count = service_config
            .policy_store
            .policies
            .get_set()
            .num_of_policies();
        metrics.set_policy_count(policy_count);

        // Initialize data store first so it can be passed to authz service
        let data = Arc::new(DataStore::new(
            config.data_store_config.clone(),
            metrics.clone(),
        )?);

        let mut service_factory = ServiceFactory::new(
            config,
            service_config,
            log.clone(),
            data.clone(),
            metrics.clone(),
        );

        // Log policy store metadata if available (new format only)
        if let Some(metadata) = service_factory.policy_store_metadata() {
            log_policy_store_metadata(&log, metadata);
        }

        if let Some(registry) = log.health_registry() {
            registry.register("core", || HealthStatus::Success);
            registry.register("policy_load", move || {
                if policy_count > 0 {
                    HealthStatus::Success
                } else {
                    HealthStatus::Failure
                }
            });
        }

        let authz = service_factory.authz_service().await?;
        let authz_swap = Arc::new(arc_swap::ArcSwap::from(authz));

        let refresh_handle = maybe_spawn_refresh_worker(
            config,
            &service_factory,
            authz_swap.clone(),
            log.clone(),
            data.clone(),
            metrics.clone(),
            refresh_seed,
        );

        Ok(Cedarling {
            log,
            authz: authz_swap,
            data,
            _refresh_handle: refresh_handle,
        })
    }

    // The following public methods retain async signatures for API compatibility
    // to avoid breaking changes. They use #[allow(unknown_lints, clippy::unused_async, clippy::unused_async_trait_impl)] since
    // they no longer await internally. Future maintainers can safely remove
    // or refactor these methods when compatibility constraints allow.

    /// Authorize request with unsigned data.
    /// makes authorization decision based on the [`RequestUnverified`]
    #[allow(unknown_lints, clippy::unused_async, clippy::unused_async_trait_impl)]
    pub async fn authorize_unsigned(
        &self,
        request: RequestUnsigned,
    ) -> Result<AuthorizeResult, AuthorizeError> {
        self.authz.load().authorize_unsigned(&request)
    }

    /// Authorize a batch of unsigned requests against one shared principal.
    ///
    /// Runs setup work (principal build + pushed-data snapshot) once and
    /// evaluates each item with its own resource and context. Results are
    /// returned in input order, wrapped in a [`BatchAuthorizeResponse`] that
    /// carries a shared `batch_id` for audit correlation.
    ///
    /// Batch-level failures (validation, principal parse) return `Err(AuthorizeError)`;
    /// per-item failures are returned as `Err(BatchItemError)` for that item,
    /// while genuine Cedar denials remain `Ok(AuthorizeResult)` with `decision=false`.
    #[allow(unknown_lints, clippy::unused_async, clippy::unused_async_trait_impl)]
    pub async fn authorize_unsigned_batch(
        &self,
        request: BatchAuthorizeUnsignedRequest,
    ) -> Result<BatchAuthorizeResponse<Result<AuthorizeResult, BatchItemError>>, AuthorizeError>
    {
        self.authz.load().authorize_unsigned_batch(&request)
    }

    /// Authorize multi-issuer request.
    /// makes authorization decision based on multiple JWT tokens from different issuers
    #[allow(unknown_lints, clippy::unused_async, clippy::unused_async_trait_impl)]
    pub async fn authorize_multi_issuer(
        &self,
        request: AuthorizeMultiIssuerRequest,
    ) -> Result<MultiIssuerAuthorizeResult, AuthorizeError> {
        self.authz.load().authorize_multi_issuer(&request)
    }

    /// Authorize a batch of multi-issuer requests against one shared token set.
    ///
    /// Validates tokens and builds token/issuer entities once, then evaluates
    /// each item with its own resource and context. Results are returned in
    /// input order, wrapped in a [`BatchAuthorizeResponse`] carrying a shared
    /// `batch_id`. Batch-level failures (validation, JWT verification,
    /// status-list refresh) return `Err(AuthorizeError)`; per-item failures are
    /// returned as `Err(BatchItemError)`, while genuine Cedar denials remain
    /// `Ok(MultiIssuerAuthorizeResult)` with `decision=false`.
    #[allow(unknown_lints, clippy::unused_async, clippy::unused_async_trait_impl)]
    pub async fn authorize_multi_issuer_batch(
        &self,
        request: BatchAuthorizeMultiIssuerRequest,
    ) -> Result<
        BatchAuthorizeResponse<Result<MultiIssuerAuthorizeResult, BatchItemError>>,
        AuthorizeError,
    > {
        self.authz.load().authorize_multi_issuer_batch(&request)
    }

    /// Returns metadata for all policies whose scope constraints are compatible
    /// with the given principals, actions, and resources.
    ///
    /// This performs scope-level filtering only (principal/action/resource constraints).
    /// Policies with `when`/`unless` conditions may still not apply at evaluation time.
    pub fn get_matching_policies_unsigned(
        &self,
        principal: Option<&EntityData>,
        actions: &[String],
        resources: &[EntityData],
    ) -> Result<Vec<PolicyMetadata>, AuthorizeError> {
        self.authz
            .load()
            .get_matching_policies_unsigned(principal, actions, resources)
    }

    /// Returns metadata for all policies whose scope constraints are compatible
    /// with the given token-derived principals, actions, and resources.
    ///
    /// Tokens are validated and their mapping types used as principal entity types.
    pub fn get_matching_policies_multi_issuer(
        &self,
        tokens: &[TokenInput],
        actions: &[String],
        resources: &[EntityData],
    ) -> Result<Vec<PolicyMetadata>, AuthorizeError> {
        self.authz
            .load()
            .get_matching_policies_multi_issuer(tokens, actions, resources)
    }

    /// Merge the annotations (`@key("value")`) of the given policies into a single map.
    ///
    /// Intended for resolving the determining policies of an authorization
    /// decision: pass the IDs from `result.response.diagnostics().reason()`.
    ///
    /// Lossy: if the same annotation key appears on several policies, one value
    /// wins arbitrarily (order undefined). Use [`Self::annotation_values`] or
    /// [`Self::annotations_by_policy`] when duplicates matter.
    ///
    /// Resolve annotations promptly after `authorize*()`: a concurrent policy-store
    /// refresh may swap the store, in which case IDs that no longer resolve are
    /// silently dropped from the result.
    pub fn annotations_map<'a>(
        &self,
        ids: impl IntoIterator<Item = &'a PolicyId>,
    ) -> HashMap<String, String> {
        self.authz.load().annotations_map(ids)
    }

    /// Collect every value of the annotation `key` across the given policies,
    /// preserving duplicates.
    ///
    /// Intended for resolving the determining policies of an authorization
    /// decision: pass the IDs from `result.response.diagnostics().reason()`.
    ///
    /// Resolve annotations promptly after `authorize*()`: a concurrent policy-store
    /// refresh may swap the store, in which case IDs that no longer resolve are
    /// silently dropped from the result.
    pub fn annotation_values<'a>(
        &self,
        ids: impl IntoIterator<Item = &'a PolicyId>,
        key: &str,
    ) -> Vec<String> {
        self.authz.load().annotation_values(ids, key)
    }

    /// Return the annotations of each given policy, grouped by policy ID —
    /// the loss-free companion to [`Self::annotations_map`].
    ///
    /// Intended for resolving the determining policies of an authorization
    /// decision: pass the IDs from `result.response.diagnostics().reason()`.
    ///
    /// Resolve annotations promptly after `authorize*()`: a concurrent policy-store
    /// refresh may swap the store, in which case IDs that no longer resolve are
    /// silently dropped from the result.
    pub fn annotations_by_policy<'a>(
        &self,
        ids: impl IntoIterator<Item = &'a PolicyId>,
    ) -> HashMap<String, HashMap<String, String>> {
        self.authz.load().annotations_by_policy(ids)
    }

    /// Closes the connections to the Lock Server and pushes all available logs.
    pub async fn shut_down(&self) {
        self.log.shut_down().await;
    }
}

impl TrustedIssuerLoadingInfo for Cedarling {
    fn is_trusted_issuer_loaded_by_name(&self, issuer_id: &str) -> bool {
        self.authz
            .load()
            .is_trusted_issuer_loaded_by_name(issuer_id)
    }

    fn is_trusted_issuer_loaded_by_iss(&self, iss_claim: &str) -> bool {
        self.authz.load().is_trusted_issuer_loaded_by_iss(iss_claim)
    }

    fn total_issuers(&self) -> usize {
        self.authz.load().total_issuers()
    }

    fn loaded_trusted_issuers_count(&self) -> usize {
        self.authz.load().loaded_trusted_issuers_count()
    }

    fn loaded_trusted_issuer_ids(&self) -> HashSet<String> {
        self.authz.load().loaded_trusted_issuer_ids()
    }

    fn failed_trusted_issuer_ids(&self) -> HashSet<String> {
        self.authz.load().failed_trusted_issuer_ids()
    }
}

/// Build the HTTP client and load the policy store from the bootstrap config.
/// Returns the parts the rest of `Cedarling::new` needs: the [`ServiceConfig`]
/// for service-factory construction, and the [`RefreshWorkerSeed`] for the
/// refresh worker's first-tick short-circuit. Wraps both fallible steps so
/// the existing "configuration parsed successfully" / "...with error" log
/// behavior covers the whole bootstrap unit.
async fn perform_bootstrap_load(
    config: &BootstrapConfig,
    log: &log::Logger,
) -> Result<(ServiceConfig, RefreshWorkerSeed), ServiceConfigError> {
    let raw_load: Result<(http::HttpClient, LoadedPolicyStore), ServiceConfigError> = async {
        let http_client = http::HttpClient::new(config.http_client_config)?;
        let loaded = load_policy_store(
            &config.policy_store_config,
            &http_client,
            config.authorization_config.strict_schema_validation,
        )
        .await?;
        Ok((http_client, loaded))
    }
    .await;

    let (http_client, loaded) = raw_load
        .inspect(|_| {
            log.log_any(
                LogEntry::new(BaseLogEntry::new_system_opt_request_id(
                    LogLevel::DEBUG,
                    None,
                ))
                .set_message("configuration parsed successfully".to_string()),
            );
        })
        .inspect_err(|err| {
            log.log_any(
                LogEntry::new(BaseLogEntry::new_system_opt_request_id(
                    LogLevel::ERROR,
                    None,
                ))
                .set_error(err.to_string())
                .set_message("configuration parsed with error".to_string()),
            );
        })?;

    let LoadedPolicyStore {
        store: policy_store,
        body_hash,
        validators,
    } = loaded;
    Ok((
        ServiceConfig {
            policy_store,
            http_client,
        },
        RefreshWorkerSeed {
            initial_body_hash: body_hash,
            initial_validators: validators,
        },
    ))
}

/// Spawn the background policy-store refresh worker if the source is a remote
/// URL and a non-zero refresh interval was configured. Returns `None` for
/// local sources or when refresh is disabled. The `seed` carries the
/// `body_hash` and `validators` captured during initial bootstrap so the
/// first periodic tick can short-circuit — passed in directly from the
/// bootstrap-load result rather than detoured through `ServiceFactory`.
fn maybe_spawn_refresh_worker(
    config: &BootstrapConfig,
    service_factory: &ServiceFactory<'_>,
    authz_swap: Arc<arc_swap::ArcSwap<authz::Authz>>,
    log: log::Logger,
    data: Arc<context_data_api::DataStore>,
    metrics: Arc<authz::metrics::MetricsCollector>,
    seed: RefreshWorkerSeed,
) -> Option<Arc<PolicyStoreRefreshHandle>> {
    if !config.policy_store_config.refresh_enabled() {
        return None;
    }
    let source = RefreshSource::from_policy_store_source(&config.policy_store_config.source)?;
    let (interval_secs, clamped) = config.policy_store_config.effective_refresh_interval();
    if clamped {
        log.log_any(
            LogEntry::new(BaseLogEntry::new_system_opt_request_id(LogLevel::WARN, None))
                .set_message(format!(
                    "CEDARLING_POLICY_STORE_REFRESH_INTERVAL={} is below the minimum; clamped to {} seconds",
                    config.policy_store_config.refresh_interval_secs,
                    interval_secs,
                )),
        );
    }
    let rebuilder = AuthzRebuilder {
        jwt_config: config.jwt_config.clone(),
        authorization_config: config.authorization_config.clone(),
        http_client: service_factory.http_client_for_refresh(),
        log: log.clone(),
        data_store: data,
        metrics: metrics.clone(),
    };
    let ctx = WorkerContext {
        source,
        interval_secs,
        http_client: service_factory.http_client_for_refresh(),
        rebuilder,
        authz_swap,
        metrics,
        log,
        initial_body_hash: seed.initial_body_hash,
        initial_validators: seed.initial_validators,
        strict_schema_validation: config.authorization_config.strict_schema_validation,
    };
    Some(Arc::new(spawn_refresh_worker(ctx)))
}

/// Log detailed information about the loaded policy store metadata, including
/// ID, version, description, Cedar version, timestamps, and compatibility with
/// the runtime Cedar version.
fn log_policy_store_metadata(
    log: &log::Logger,
    metadata: &crate::common::policy_store::PolicyStoreMetadata,
) {
    // Build detailed log message using accessor methods
    let mut details = format!(
        "Policy store '{}' (ID: {}) v{} loaded",
        metadata.name(),
        if metadata.id().is_empty() {
            "<auto>"
        } else {
            metadata.id()
        },
        metadata.version()
    );

    // Add description if available
    if let Some(desc) = metadata.description() {
        let _ = write!(details, " - {desc}");
    }

    // Add Cedar version info
    let _ = write!(details, " [Cedar {}]", metadata.cedar_version());

    // Add timestamp info if available
    if let Some(created) = metadata.created_date() {
        let _ = write!(details, " (created: {})", created.format("%Y-%m-%d"));
    }
    if let Some(updated) = metadata.updated_date() {
        let _ = write!(details, " (updated: {})", updated.format("%Y-%m-%d"));
    }

    log.log_any(
        LogEntry::new(BaseLogEntry::new_system_opt_request_id(
            LogLevel::DEBUG,
            None,
        ))
        .set_message(details),
    );

    // Log version compatibility check with current Cedar
    let current_cedar_version: Version = cedar_policy::get_lang_version();
    match metadata.is_compatible_with_cedar(&current_cedar_version) {
        Ok(true) => {
            log.log_any(
                LogEntry::new(BaseLogEntry::new_system_opt_request_id(
                    LogLevel::DEBUG,
                    None,
                ))
                .set_message(format!(
                    "Policy store Cedar version {} is compatible with runtime version {}",
                    metadata.cedar_version(),
                    current_cedar_version
                )),
            );
        },
        Ok(false) => {
            log.log_any(
                LogEntry::new(BaseLogEntry::new_system_opt_request_id(
                    LogLevel::WARN,
                    None,
                ))
                .set_message(format!(
                    "Policy store Cedar version {} may not be compatible with runtime version {}",
                    metadata.cedar_version(),
                    current_cedar_version
                )),
            );
        },
        Err(e) => {
            log.log_any(
                LogEntry::new(BaseLogEntry::new_system_opt_request_id(
                    LogLevel::WARN,
                    None,
                ))
                .set_message(format!("Could not check Cedar version compatibility: {e}")),
            );
        },
    }

    // Log parsed version for debugging if available
    if let Some(parsed_version) = metadata.version_parsed() {
        log.log_any(
            LogEntry::new(BaseLogEntry::new_system_opt_request_id(
                LogLevel::TRACE,
                None,
            ))
            .set_message(format!(
                "Policy store semantic version: {}.{}.{}",
                parsed_version.major, parsed_version.minor, parsed_version.patch
            )),
        );
    }
}

// implements LogStorage for Cedarling
// we can use this methods outside crate only when import trait
impl LogStorage for Cedarling {
    fn pop_logs(&self) -> Vec<serde_json::Value> {
        self.log.pop_logs()
    }

    fn get_log_by_id(&self, id: &str) -> Option<serde_json::Value> {
        self.log.get_log_by_id(id)
    }

    fn get_log_ids(&self) -> Vec<String> {
        self.log.get_log_ids()
    }

    fn get_logs_by_tag(&self, tag: &str) -> Vec<serde_json::Value> {
        self.log.get_logs_by_tag(tag)
    }

    fn get_logs_by_request_id(&self, request_id: &str) -> Vec<serde_json::Value> {
        self.log.get_logs_by_request_id(request_id)
    }

    fn get_logs_by_request_id_and_tag(&self, id: &str, tag: &str) -> Vec<serde_json::Value> {
        self.log.get_logs_by_request_id_and_tag(id, tag)
    }
}

// implements DataApi for Cedarling
// Helper function to calculate capacity usage and check memory alert threshold
fn calculate_capacity_usage(
    entry_count: usize,
    max_entries: usize,
    memory_alert_threshold: f64,
) -> (f64, bool) {
    // Precision loss is acceptable for percentage calculation
    #[allow(clippy::cast_precision_loss)]
    let capacity_usage_percent = if max_entries > 0 {
        (entry_count as f64 / max_entries as f64) * 100.0
    } else {
        0.0 // Unlimited capacity, no percentage
    };
    let memory_alert_triggered = capacity_usage_percent >= memory_alert_threshold;
    (capacity_usage_percent, memory_alert_triggered)
}

// provides public interface for pushing and retrieving data
impl DataApi for Cedarling {
    fn push_data_ctx(
        &self,
        key: &str,
        value: serde_json::Value,
        ttl: Option<std::time::Duration>,
    ) -> Result<(), DataError> {
        self.data.push(key, value, ttl)?;

        // Check memory usage and log warning if threshold is exceeded
        let config = self.data.config();
        if config.max_entries > 0 {
            let entry_count = self.data.count();
            let (capacity_usage_percent, memory_alert_triggered) = calculate_capacity_usage(
                entry_count,
                config.max_entries,
                config.memory_alert_threshold,
            );
            if memory_alert_triggered {
                let log_entry = LogEntry::new(BaseLogEntry::new_system_opt_request_id(
                    LogLevel::WARN,
                    None,
                ))
                .set_message(format!(
                    "DataStore memory usage alert: {:.1}% capacity used ({}/{} entries), threshold: {:.1}%",
                    capacity_usage_percent,
                    entry_count,
                    config.max_entries,
                    config.memory_alert_threshold
                ));
                self.log.log_any(log_entry);
            }
        }

        Ok(())
    }

    fn get_data_ctx(&self, key: &str) -> Result<Option<serde_json::Value>, DataError> {
        Ok(self.data.get(key))
    }

    fn get_data_entry_ctx(&self, key: &str) -> Result<Option<DataEntry>, DataError> {
        Ok(self.data.get_entry(key))
    }

    fn remove_data_ctx(&self, key: &str) -> Result<bool, DataError> {
        Ok(self.data.remove(key))
    }

    fn clear_data_ctx(&self) -> Result<(), DataError> {
        self.data.clear();
        Ok(())
    }

    fn list_data_ctx(&self) -> Result<Vec<DataEntry>, DataError> {
        Ok(self.data.list_entries())
    }

    fn get_stats_ctx(&self) -> Result<DataStoreStats, DataError> {
        let config = self.data.config();
        let entry_count = self.data.count();
        let total_size_bytes = self.data.total_size();
        let avg_entry_size_bytes = total_size_bytes.checked_div(entry_count).unwrap_or(0);

        // Calculate capacity usage percentage
        let (capacity_usage_percent, memory_alert_triggered) = calculate_capacity_usage(
            entry_count,
            config.max_entries,
            config.memory_alert_threshold,
        );

        Ok(DataStoreStats {
            entry_count,
            max_entries: config.max_entries,
            max_entry_size: config.max_entry_size,
            metrics_enabled: config.enable_metrics,
            total_size_bytes,
            avg_entry_size_bytes,
            capacity_usage_percent,
            memory_alert_threshold: config.memory_alert_threshold,
            memory_alert_triggered,
        })
    }
}

// Tooling & Diagnostics
#[cfg(feature = "tools")]
impl Cedarling {
    /// Return metadata for every policy in the store.
    ///
    /// Intended for tooling that needs to enumerate the full policy set
    /// (coverage reports, dashboards). Ordering is unspecified.
    #[must_use]
    pub fn all_policy_metadata(&self) -> Vec<PolicyMetadata> {
        self.authz.load().all_policy_metadata()
    }

    /// Run parse / schema / metadata validation against a policy-store source
    /// without initializing the authorization engine. Never returns Err for
    /// validation failures — those land in the returned report; only
    /// returns Err for underlying I/O or network failures.
    #[allow(clippy::too_many_lines)]
    pub async fn validate_policy_store(
        config: &PolicyStoreConfig,
        http_config: &crate::http::HttpClientConfig,
    ) -> Result<ValidationReport, ValidateInfraError> {
        // 1. Build a fresh HttpClient
        let http_client = crate::http::HttpClient::new(*http_config)?;

        // 2. Call load_policy_store
        let load_result =
            crate::init::policy_store::load_policy_store(config, &http_client, false).await;

        match load_result {
            Err(e) => {
                use crate::init::policy_store::PolicyStoreLoadError;

                let err_str = e.to_string();

                let is_metadata = matches!(&e, PolicyStoreLoadError::Validation(_));

                let is_parse = matches!(
                    &e,
                    PolicyStoreLoadError::ParseJson(_)
                        | PolicyStoreLoadError::ParseYaml(_)
                        | PolicyStoreLoadError::Conversion(_)
                        | PolicyStoreLoadError::InvalidStore(_)
                );

                match e {
                    PolicyStoreLoadError::FetchFromLockServer(_)
                    | PolicyStoreLoadError::Archive(_)
                    | PolicyStoreLoadError::Directory(_) => {
                        Err(ValidateInfraError::Io(std::io::Error::other(err_str)))
                    },
                    PolicyStoreLoadError::ParseFile(_, io_err) => {
                        Err(ValidateInfraError::Io(io_err))
                    },
                    _ if is_metadata => {
                        let diag = Diagnostic {
                            file: "<policy-store>".into(),
                            line: None,
                            column: None,
                            message: err_str,
                        };
                        Ok(ValidationReport {
                            parse: LevelResult::Skipped {
                                reason: "metadata check failed".into(),
                            },
                            schema: LevelResult::Skipped {
                                reason: "metadata check failed".into(),
                            },
                            metadata: LevelResult::Failed { errors: vec![diag] },
                        })
                    },
                    _ if is_parse => {
                        let diag = Diagnostic {
                            file: "<policy-store>".into(),
                            line: None,
                            column: None,
                            message: err_str,
                        };
                        Ok(ValidationReport {
                            parse: LevelResult::Failed { errors: vec![diag] },
                            schema: LevelResult::Skipped {
                                reason: "parse failed".into(),
                            },
                            metadata: LevelResult::Skipped {
                                reason: "parse failed".into(),
                            },
                        })
                    },
                    _ => Err(ValidateInfraError::Io(std::io::Error::other(err_str))),
                }
            },
            Ok(loaded) => {
                // Parse succeeded! Now run schema and metadata independently.

                // Schema Level
                let schema_res = if let Some(schema) = &loaded.store.store.schema {
                    let validator = cedar_policy::Validator::new(schema.schema.clone());
                    let result = validator.validate(
                        loaded.store.store.policies.get_set(),
                        cedar_policy::ValidationMode::Strict,
                    );
                    if result.validation_passed() {
                        LevelResult::Ok
                    } else {
                        let errors = result
                            .validation_errors()
                            .map(|e| Diagnostic {
                                file: e.policy_id().to_string(),
                                line: None,
                                column: None,
                                message: e.to_string(),
                            })
                            .collect();
                        LevelResult::Failed { errors }
                    }
                } else {
                    LevelResult::Skipped {
                        reason: "no schema present".into(),
                    }
                };

                // Metadata Level
                // For directory/archive, the loader already ran MetadataValidator.
                // For legacy YAML/JSON stores, we run validate_legacy_metadata.
                let metadata_res = match &loaded.store.metadata {
                    Some(metadata) => {
                        use crate::common::policy_store::validator::MetadataValidator;
                        match MetadataValidator::validate(metadata) {
                            Ok(()) => LevelResult::Ok,
                            Err(e) => LevelResult::Failed {
                                errors: vec![Diagnostic {
                                    file: "<metadata>".into(),
                                    line: None,
                                    column: None,
                                    message: e.to_string(),
                                }],
                            },
                        }
                    },
                    None => {
                        match crate::common::policy_store::validator::validate_legacy_metadata(
                            &loaded.store.store,
                        ) {
                            Ok(()) => LevelResult::Ok,
                            Err(e) => LevelResult::Failed {
                                errors: vec![Diagnostic {
                                    file: "<inline>".into(),
                                    line: None,
                                    column: None,
                                    message: e.to_string(),
                                }],
                            },
                        }
                    }
                };

                Ok(ValidationReport {
                    parse: LevelResult::Ok,
                    schema: schema_res,
                    metadata: metadata_res,
                })
            },
        }
    }

}