Skip to main content

cedrus_core/
cache.rs

1use std::{collections::HashMap, error::Error, sync::Arc};
2
3use cedrus_cedar::{
4    Entity, EntityUid, Policy, PolicyId, PolicySet, Schema, Template, TemplateLink,
5};
6use uuid::Uuid;
7
8use crate::project::{ApiKey, IdentitySource, Project};
9
10#[derive(Debug)]
11pub enum CacheError {
12    Connection,
13    NotFound,
14    ConfigurationError(String),
15    DecodeError(String),
16    JsonError(String),
17}
18
19impl std::fmt::Display for CacheError {
20    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21        match self {
22            CacheError::ConfigurationError(e) => write!(f, "Configuration error: {}", e),
23            CacheError::Connection => write!(f, "Connection error"),
24            CacheError::NotFound => write!(f, "Not found"),
25            CacheError::DecodeError(err) => write!(f, "Decode error: {}", err),
26            CacheError::JsonError(err) => write!(f, "Json error: {}", err),
27        }
28    }
29}
30
31impl Error for CacheError {}
32
33#[async_trait::async_trait]
34pub trait Cache: Send + Sync {
35    async fn projects_get(&self) -> Result<Vec<Project>, CacheError>;
36    async fn project_get(&self, project_id: &Uuid) -> Result<Option<Project>, CacheError>;
37    async fn project_set(&self, project: &Project) -> Result<(), CacheError>;
38    async fn project_del(&self, project_id: &Uuid) -> Result<(), CacheError>;
39
40    async fn project_apikeys_get(&self, project_id: &Uuid) -> Result<Vec<ApiKey>, CacheError>;
41    async fn project_apikeys_set(
42        &self,
43        project_id: &Uuid,
44        apikeys: &Vec<ApiKey>,
45    ) -> Result<(), CacheError>;
46    async fn project_apikeys_del(
47        &self,
48        project_id: &Uuid,
49        ids: &Vec<Uuid>,
50    ) -> Result<(), CacheError>;
51
52    async fn project_identity_source_get(
53        &self,
54        project_id: &Uuid,
55    ) -> Result<Option<IdentitySource>, CacheError>;
56    async fn project_identity_source_set(
57        &self,
58        project_id: &Uuid,
59        identity_source: &IdentitySource,
60    ) -> Result<(), CacheError>;
61    async fn project_identity_source_del(&self, project_id: &Uuid) -> Result<(), CacheError>;
62
63    async fn project_schema_get(&self, project_id: &Uuid) -> Result<Option<Schema>, CacheError>;
64    async fn project_schema_set(
65        &self,
66        project_id: &Uuid,
67        schema: &Schema,
68    ) -> Result<(), CacheError>;
69    async fn project_schema_del(&self, project_id: &Uuid) -> Result<(), CacheError>;
70
71    async fn project_entities_get(
72        &self,
73        project_id: &Uuid,
74        entity_uids: &[EntityUid],
75    ) -> Result<Vec<Entity>, CacheError>;
76    async fn project_entities_set(
77        &self,
78        project_id: &Uuid,
79        entities: &[Entity],
80    ) -> Result<(), CacheError>;
81    async fn project_entities_del(
82        &self,
83        project_id: &Uuid,
84        entity_uids: &[EntityUid],
85    ) -> Result<(), CacheError>;
86
87    async fn project_policies_get(
88        &self,
89        project_id: &Uuid,
90    ) -> Result<HashMap<PolicyId, Policy>, CacheError>;
91    async fn project_policies_set(
92        &self,
93        project_id: &Uuid,
94        policies: &HashMap<PolicyId, Policy>,
95    ) -> Result<(), CacheError>;
96    async fn project_policies_del(
97        &self,
98        project_id: &Uuid,
99        policy_ids: &[PolicyId],
100    ) -> Result<(), CacheError>;
101
102    async fn project_templates_get(
103        &self,
104        project_id: &Uuid,
105    ) -> Result<HashMap<PolicyId, Template>, CacheError>;
106    async fn project_templates_set(
107        &self,
108        project_id: &Uuid,
109        templates: &HashMap<PolicyId, Template>,
110    ) -> Result<(), CacheError>;
111    async fn project_templates_del(
112        &self,
113        project_id: &Uuid,
114        policy_ids: &[PolicyId],
115    ) -> Result<(), CacheError>;
116
117    async fn project_template_links_get(
118        &self,
119        project_id: &Uuid,
120    ) -> Result<Vec<TemplateLink>, CacheError>;
121    async fn project_template_links_set(
122        &self,
123        project_id: &Uuid,
124        template_links: &[TemplateLink],
125    ) -> Result<(), CacheError>;
126    async fn project_template_links_del(
127        &self,
128        project_id: &Uuid,
129        policy_ids: &[PolicyId],
130    ) -> Result<(), CacheError>;
131
132    async fn project_policy_set_get(&self, project_id: &Uuid) -> Result<PolicySet, CacheError>;
133    async fn project_policy_set_set(
134        &self,
135        project_id: &Uuid,
136        policy_set: &PolicySet,
137    ) -> Result<(), CacheError>;
138
139    async fn project_cedar_schema_get(
140        &self,
141        project_id: &Uuid,
142    ) -> Result<Arc<Option<cedar_policy::Schema>>, CacheError>;
143
144    /// Get the Cedar Entities object for a project.
145    /// If entity_uids is not empty, only return these entities and their ancestors.
146    async fn project_cedar_entities_get(
147        &self,
148        project_id: &Uuid,
149        entity_uids: &Vec<EntityUid>,
150        add_entities: &[Entity],
151        cedar_schema: Option<&cedar_policy::Schema>,
152    ) -> Result<cedar_policy::Entities, CacheError>;
153
154    /// Get the Cedar PolicySet object for a project.
155    async fn project_cedar_policy_set_get(
156        &self,
157        project_id: &Uuid,
158    ) -> Result<Arc<cedar_policy::PolicySet>, CacheError>;
159
160    async fn apikeys_by_key_get(&self, key: &str) -> Option<ApiKey>;
161    async fn projects_entities_is_ancestor_of(
162        &self,
163        project_id: &Uuid,
164        ancestor: &EntityUid,
165        descendant: &EntityUid,
166    ) -> Result<bool, CacheError>;
167}