1use std::{collections::HashMap, error::Error};
2
3use cedrus_cedar::{Entity, EntityUid, Policy, PolicyId, Schema, Template, TemplateLink};
4use mango_query_rs::MangoQuery;
5use uuid::Uuid;
6
7use crate::{
8 PageHash, PageList,
9 project::{ApiKey, IdentitySource, Project},
10};
11
12#[derive(Debug)]
13pub enum DatabaseError {
14 NotFound,
15 Unknown,
16 ConfigurationError(String),
17 ConnectionError(String),
18 MissingAttribute(String),
19 InvalidAttribute(String),
20 JsonError(serde_json::Error),
21 SerializationError(String),
22 InternalError(String),
23}
24
25impl std::fmt::Display for DatabaseError {
26 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27 match self {
28 DatabaseError::NotFound => write!(f, "not found"),
29 DatabaseError::Unknown => write!(f, "unknown"),
30 DatabaseError::ConfigurationError(e) => write!(f, "Configuration error: {}", e),
31 DatabaseError::ConnectionError(e) => write!(f, "connection error: {}", e),
32 DatabaseError::MissingAttribute(a) => write!(f, "missing attribute: {}", a),
33 DatabaseError::InvalidAttribute(a) => write!(f, "invalid attribute: {}", a),
34 DatabaseError::JsonError(e) => write!(f, "json error: {}", e),
35 DatabaseError::SerializationError(e) => write!(f, "serialization error: {}", e),
36 DatabaseError::InternalError(e) => write!(f, "internal error: {}", e),
37 }
38 }
39}
40
41impl Error for DatabaseError {}
42
43impl From<serde_json::Error> for DatabaseError {
44 fn from(e: serde_json::Error) -> Self {
45 DatabaseError::JsonError(e)
46 }
47}
48
49#[async_trait::async_trait]
50pub trait Database: Send + Sync {
51 async fn projects_find(&self, query: &MangoQuery) -> Result<PageList<Project>, DatabaseError>;
52 async fn project_get(&self, id: &Uuid) -> Result<Option<Project>, DatabaseError>;
53 async fn project_save(&self, project: &Project) -> Result<(), DatabaseError>;
54 async fn project_remove(&self, id: &Uuid) -> Result<(), DatabaseError>;
55
56 async fn project_apikeys_find(
57 &self,
58 project_id: &Uuid,
59 query: &MangoQuery,
60 ) -> Result<PageList<ApiKey>, DatabaseError>;
61 async fn project_apikeys_save(
62 &self,
63 project_id: &Uuid,
64 apikeys: &Vec<ApiKey>,
65 ) -> Result<(), DatabaseError>;
66 async fn project_apikeys_remove(
67 &self,
68 project_id: &Uuid,
69 ids: &Vec<Uuid>,
70 ) -> Result<(), DatabaseError>;
71
72 async fn project_identity_source_get(
73 &self,
74 project_id: &Uuid,
75 ) -> Result<Option<IdentitySource>, DatabaseError>;
76 async fn project_identity_source_save(
77 &self,
78 project_id: &Uuid,
79 identity_source: &IdentitySource,
80 ) -> Result<(), DatabaseError>;
81 async fn project_identity_source_remove(&self, project_id: &Uuid) -> Result<(), DatabaseError>;
82
83 async fn project_schema_find(&self, project_id: &Uuid)
84 -> Result<Option<Schema>, DatabaseError>;
85 async fn project_schema_save(
86 &self,
87 project_id: &Uuid,
88 schema: &Schema,
89 ) -> Result<(), DatabaseError>;
90 async fn project_schema_remove(&self, project_id: &Uuid) -> Result<(), DatabaseError>;
91
92 async fn project_entities_find(
93 &self,
94 project_id: &Uuid,
95 query: &MangoQuery,
96 ) -> Result<PageList<Entity>, DatabaseError>;
97 async fn project_entities_save(
98 &self,
99 project_id: &Uuid,
100 entities: &Vec<Entity>,
101 ) -> Result<(), DatabaseError>;
102 async fn project_entities_remove(
103 &self,
104 project_id: &Uuid,
105 entity_uids: &Vec<EntityUid>,
106 ) -> Result<(), DatabaseError>;
107
108 async fn project_policies_find(
109 &self,
110 project_id: &Uuid,
111 query: &MangoQuery,
112 ) -> Result<PageHash<PolicyId, Policy>, DatabaseError>;
113 async fn project_policies_save(
114 &self,
115 project_id: &Uuid,
116 policies: &HashMap<PolicyId, Policy>,
117 ) -> Result<(), DatabaseError>;
118 async fn project_policies_remove(
119 &self,
120 project_id: &Uuid,
121 policy_ids: &Vec<PolicyId>,
122 ) -> Result<(), DatabaseError>;
123
124 async fn project_templates_find(
125 &self,
126 project_id: &Uuid,
127 query: &MangoQuery,
128 ) -> Result<PageHash<PolicyId, Template>, DatabaseError>;
129 async fn project_templates_save(
130 &self,
131 project_id: &Uuid,
132 templates: &HashMap<PolicyId, Template>,
133 ) -> Result<(), DatabaseError>;
134 async fn project_templates_remove(
135 &self,
136 project_id: &Uuid,
137 template_ids: &Vec<PolicyId>,
138 ) -> Result<(), DatabaseError>;
139
140 async fn project_template_links_find(
141 &self,
142 project_id: &Uuid,
143 query: &MangoQuery,
144 ) -> Result<PageList<TemplateLink>, DatabaseError>;
145 async fn project_template_links_save(
146 &self,
147 project_id: &Uuid,
148 template_links: &Vec<TemplateLink>,
149 ) -> Result<(), DatabaseError>;
150 async fn project_template_links_remove(
151 &self,
152 project_id: &Uuid,
153 link_ids: &Vec<PolicyId>,
154 ) -> Result<(), DatabaseError>;
155}