cedrus-core 0.2.0

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

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

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

#[derive(Debug)]
pub enum CacheError {
    Connection,
    NotFound,
    ConfigurationError(String),
    DecodeError(String),
    JsonError(String),
}

impl std::fmt::Display for CacheError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CacheError::ConfigurationError(e) => write!(f, "Configuration error: {}", e),
            CacheError::Connection => write!(f, "Connection error"),
            CacheError::NotFound => write!(f, "Not found"),
            CacheError::DecodeError(err) => write!(f, "Decode error: {}", err),
            CacheError::JsonError(err) => write!(f, "Json error: {}", err),
        }
    }
}

impl Error for CacheError {}

#[async_trait::async_trait]
pub trait Cache: Send + Sync {
    async fn projects_get(&self) -> Result<Vec<Project>, CacheError>;
    async fn project_get(&self, project_id: &Uuid) -> Result<Option<Project>, CacheError>;
    async fn project_set(&self, project: &Project) -> Result<(), CacheError>;
    async fn project_del(&self, project_id: &Uuid) -> Result<(), CacheError>;

    async fn project_apikeys_get(&self, project_id: &Uuid) -> Result<Vec<ApiKey>, CacheError>;
    async fn project_apikeys_set(
        &self,
        project_id: &Uuid,
        apikeys: &Vec<ApiKey>,
    ) -> Result<(), CacheError>;
    async fn project_apikeys_del(
        &self,
        project_id: &Uuid,
        ids: &Vec<Uuid>,
    ) -> Result<(), CacheError>;

    async fn project_identity_source_get(
        &self,
        project_id: &Uuid,
    ) -> Result<Option<IdentitySource>, CacheError>;
    async fn project_identity_source_set(
        &self,
        project_id: &Uuid,
        identity_source: &IdentitySource,
    ) -> Result<(), CacheError>;
    async fn project_identity_source_del(&self, project_id: &Uuid) -> Result<(), CacheError>;

    async fn project_schema_get(&self, project_id: &Uuid) -> Result<Option<Schema>, CacheError>;
    async fn project_schema_set(
        &self,
        project_id: &Uuid,
        schema: &Schema,
    ) -> Result<(), CacheError>;
    async fn project_schema_del(&self, project_id: &Uuid) -> Result<(), CacheError>;

    async fn project_entities_get(
        &self,
        project_id: &Uuid,
        entity_uids: &[EntityUid],
    ) -> Result<Vec<Entity>, CacheError>;
    async fn project_entities_set(
        &self,
        project_id: &Uuid,
        entities: &[Entity],
    ) -> Result<(), CacheError>;
    async fn project_entities_del(
        &self,
        project_id: &Uuid,
        entity_uids: &[EntityUid],
    ) -> Result<(), CacheError>;

    async fn project_policies_get(
        &self,
        project_id: &Uuid,
    ) -> Result<HashMap<PolicyId, Policy>, CacheError>;
    async fn project_policies_set(
        &self,
        project_id: &Uuid,
        policies: &HashMap<PolicyId, Policy>,
    ) -> Result<(), CacheError>;
    async fn project_policies_del(
        &self,
        project_id: &Uuid,
        policy_ids: &[PolicyId],
    ) -> Result<(), CacheError>;

    async fn project_templates_get(
        &self,
        project_id: &Uuid,
    ) -> Result<HashMap<PolicyId, Template>, CacheError>;
    async fn project_templates_set(
        &self,
        project_id: &Uuid,
        templates: &HashMap<PolicyId, Template>,
    ) -> Result<(), CacheError>;
    async fn project_templates_del(
        &self,
        project_id: &Uuid,
        policy_ids: &[PolicyId],
    ) -> Result<(), CacheError>;

    async fn project_template_links_get(
        &self,
        project_id: &Uuid,
    ) -> Result<Vec<TemplateLink>, CacheError>;
    async fn project_template_links_set(
        &self,
        project_id: &Uuid,
        template_links: &[TemplateLink],
    ) -> Result<(), CacheError>;
    async fn project_template_links_del(
        &self,
        project_id: &Uuid,
        policy_ids: &[PolicyId],
    ) -> Result<(), CacheError>;

    async fn project_policy_set_get(&self, project_id: &Uuid) -> Result<PolicySet, CacheError>;
    async fn project_policy_set_set(
        &self,
        project_id: &Uuid,
        policy_set: &PolicySet,
    ) -> Result<(), CacheError>;

    async fn project_cedar_schema_get(
        &self,
        project_id: &Uuid,
    ) -> Result<Arc<Option<cedar_policy::Schema>>, CacheError>;

    /// Get the Cedar Entities object for a project.
    /// If entity_uids is not empty, only return these entities and their ancestors.
    async fn project_cedar_entities_get(
        &self,
        project_id: &Uuid,
        entity_uids: &Vec<EntityUid>,
        add_entities: &[Entity],
        cedar_schema: Option<&cedar_policy::Schema>,
    ) -> Result<cedar_policy::Entities, CacheError>;

    /// Get the Cedar PolicySet object for a project.
    async fn project_cedar_policy_set_get(
        &self,
        project_id: &Uuid,
    ) -> Result<Arc<cedar_policy::PolicySet>, CacheError>;

    async fn apikeys_by_key_get(&self, key: &str) -> Option<ApiKey>;
    async fn projects_entities_is_ancestor_of(
        &self,
        project_id: &Uuid,
        ancestor: &EntityUid,
        descendant: &EntityUid,
    ) -> Result<bool, CacheError>;
}