cedar_local_agent/public/mod.rs
1//! Module contains a simple entity provider, and policy provider and authorizer.
2use std::fmt::Debug;
3use std::sync::Arc;
4
5use async_trait::async_trait;
6use cedar_policy::{Entities, PolicySet, Request};
7use thiserror::Error;
8
9pub mod events;
10pub mod file;
11pub mod log;
12pub mod simple;
13
14/// `EntityProviderError` is a general error that any implementation of trait
15/// `SimpleEntityProvider` can return as an error.
16#[derive(Error, Debug)]
17pub enum EntityProviderError {
18 /// `General` error case, designed to be used with any source std error.
19 #[error("Entity Provider failed to get the entities: {0}")]
20 General(#[source] Box<dyn std::error::Error + Send + Sync + 'static>),
21}
22
23/// `SimpleEntitySetProvider` trait provides a simple trait for gathering entities. Some use cases
24/// include getting data from a simple file system location or a database call.
25///
26/// The cedar `Request` is passed to the provider as it contains information on the `Principal` trying to
27/// perform some `Action` on a `Resource` within some `Context`. The `Principal` and `Resource` information
28/// can help inform the implementer of what resources to gather from various `Entity` providers.
29#[async_trait]
30pub trait SimpleEntityProvider: Debug + Send + Sync {
31 /// Provides the method signature to `get_entities` from any location.
32 async fn get_entities(&self, request: &Request) -> Result<Arc<Entities>, EntityProviderError>;
33}
34
35/// `PolicySetProviderError` is a general error that any implementation of trait
36/// `SimplePolicySetProvider` can return as an error.
37#[derive(Error, Debug)]
38pub enum PolicySetProviderError {
39 /// A `General` error variant that boxes
40 #[error("Policy Set provider failed to get the policy set: {0}")]
41 General(#[source] Box<dyn std::error::Error + Send + Sync + 'static>),
42}
43
44/// `SimplePolicySetProvider` trait provides a simple trait for gathering policy set data. Some
45/// use cases would be getting data out of a file or a database call.
46///
47/// The cedar `Request` is passed to the provider as it contains information on the `Principal` trying to
48/// perform some `Action` on a `Resource` within some `Context`. The information contained in this
49/// `Request` can be used to gather a slice of a `PolicySet`, i.e. only applicable `Policies` that
50/// are related to that specific `Principal`, `Action` or `Resource` based on the implementers algorithm.
51#[async_trait]
52pub trait SimplePolicySetProvider: Debug + Send + Sync {
53 /// Provides the method signature to `get_policy_set` from any location.
54 async fn get_policy_set(
55 &self,
56 request: &Request,
57 ) -> Result<Arc<PolicySet>, PolicySetProviderError>;
58}
59
60/// `UpdateProviderDataError` occurs when the `SimpleAuthorizer` cannot update the applicable
61/// provider's data via an asynchronous background thread.
62#[derive(Error, Debug)]
63pub enum UpdateProviderDataError {
64 /// A `General` error variant that implements debug that boxes other errors
65 #[error("Failed to update the provider data: {0}")]
66 General(#[source] Box<dyn std::error::Error + Send + Sync + 'static>),
67}
68
69/// `UpdateProviderData` trait provides a simple trait for allowing updating provider data in an
70/// async fashion outside the 'request' context of an `is_authorization` question.
71#[async_trait]
72pub trait UpdateProviderData: Debug + Send + Sync {
73 /// Update a providers data within the implementor.
74 async fn update_provider_data(&self) -> Result<(), UpdateProviderDataError>;
75}