cedrus-core 0.2.0

Business logic and authorization engine for Cedrus.
Documentation
use std::{collections::HashMap, error::Error};

use cedrus_cedar::{Entity, EntityUid, Policy, PolicyId, Schema, Template, TemplateLink};
use mango_query_rs::MangoQuery;
use uuid::Uuid;

use crate::{
    PageHash, PageList,
    project::{ApiKey, IdentitySource, Project},
};

#[derive(Debug)]
pub enum DatabaseError {
    NotFound,
    Unknown,
    ConfigurationError(String),
    ConnectionError(String),
    MissingAttribute(String),
    InvalidAttribute(String),
    JsonError(serde_json::Error),
    SerializationError(String),
    InternalError(String),
}

impl std::fmt::Display for DatabaseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DatabaseError::NotFound => write!(f, "not found"),
            DatabaseError::Unknown => write!(f, "unknown"),
            DatabaseError::ConfigurationError(e) => write!(f, "Configuration error: {}", e),
            DatabaseError::ConnectionError(e) => write!(f, "connection error: {}", e),
            DatabaseError::MissingAttribute(a) => write!(f, "missing attribute: {}", a),
            DatabaseError::InvalidAttribute(a) => write!(f, "invalid attribute: {}", a),
            DatabaseError::JsonError(e) => write!(f, "json error: {}", e),
            DatabaseError::SerializationError(e) => write!(f, "serialization error: {}", e),
            DatabaseError::InternalError(e) => write!(f, "internal error: {}", e),
        }
    }
}

impl Error for DatabaseError {}

impl From<serde_json::Error> for DatabaseError {
    fn from(e: serde_json::Error) -> Self {
        DatabaseError::JsonError(e)
    }
}

#[async_trait::async_trait]
pub trait Database: Send + Sync {
    async fn projects_find(&self, query: &MangoQuery) -> Result<PageList<Project>, DatabaseError>;
    async fn project_get(&self, id: &Uuid) -> Result<Option<Project>, DatabaseError>;
    async fn project_save(&self, project: &Project) -> Result<(), DatabaseError>;
    async fn project_remove(&self, id: &Uuid) -> Result<(), DatabaseError>;

    async fn project_apikeys_find(
        &self,
        project_id: &Uuid,
        query: &MangoQuery,
    ) -> Result<PageList<ApiKey>, DatabaseError>;
    async fn project_apikeys_save(
        &self,
        project_id: &Uuid,
        apikeys: &Vec<ApiKey>,
    ) -> Result<(), DatabaseError>;
    async fn project_apikeys_remove(
        &self,
        project_id: &Uuid,
        ids: &Vec<Uuid>,
    ) -> Result<(), DatabaseError>;

    async fn project_identity_source_get(
        &self,
        project_id: &Uuid,
    ) -> Result<Option<IdentitySource>, DatabaseError>;
    async fn project_identity_source_save(
        &self,
        project_id: &Uuid,
        identity_source: &IdentitySource,
    ) -> Result<(), DatabaseError>;
    async fn project_identity_source_remove(&self, project_id: &Uuid) -> Result<(), DatabaseError>;

    async fn project_schema_find(&self, project_id: &Uuid)
    -> Result<Option<Schema>, DatabaseError>;
    async fn project_schema_save(
        &self,
        project_id: &Uuid,
        schema: &Schema,
    ) -> Result<(), DatabaseError>;
    async fn project_schema_remove(&self, project_id: &Uuid) -> Result<(), DatabaseError>;

    async fn project_entities_find(
        &self,
        project_id: &Uuid,
        query: &MangoQuery,
    ) -> Result<PageList<Entity>, DatabaseError>;
    async fn project_entities_save(
        &self,
        project_id: &Uuid,
        entities: &Vec<Entity>,
    ) -> Result<(), DatabaseError>;
    async fn project_entities_remove(
        &self,
        project_id: &Uuid,
        entity_uids: &Vec<EntityUid>,
    ) -> Result<(), DatabaseError>;

    async fn project_policies_find(
        &self,
        project_id: &Uuid,
        query: &MangoQuery,
    ) -> Result<PageHash<PolicyId, Policy>, DatabaseError>;
    async fn project_policies_save(
        &self,
        project_id: &Uuid,
        policies: &HashMap<PolicyId, Policy>,
    ) -> Result<(), DatabaseError>;
    async fn project_policies_remove(
        &self,
        project_id: &Uuid,
        policy_ids: &Vec<PolicyId>,
    ) -> Result<(), DatabaseError>;

    async fn project_templates_find(
        &self,
        project_id: &Uuid,
        query: &MangoQuery,
    ) -> Result<PageHash<PolicyId, Template>, DatabaseError>;
    async fn project_templates_save(
        &self,
        project_id: &Uuid,
        templates: &HashMap<PolicyId, Template>,
    ) -> Result<(), DatabaseError>;
    async fn project_templates_remove(
        &self,
        project_id: &Uuid,
        template_ids: &Vec<PolicyId>,
    ) -> Result<(), DatabaseError>;

    async fn project_template_links_find(
        &self,
        project_id: &Uuid,
        query: &MangoQuery,
    ) -> Result<PageList<TemplateLink>, DatabaseError>;
    async fn project_template_links_save(
        &self,
        project_id: &Uuid,
        template_links: &Vec<TemplateLink>,
    ) -> Result<(), DatabaseError>;
    async fn project_template_links_remove(
        &self,
        project_id: &Uuid,
        link_ids: &Vec<PolicyId>,
    ) -> Result<(), DatabaseError>;
}