use serde::{Deserialize, Serialize};
use std::any::Any;
use std::collections::HashMap;
use anyhow::Result;
use crate::constants::*;
pub trait Module: Send + Sync {
fn name(&self) -> &str;
fn module_type(&self) -> &str;
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModuleSignature {
pub code_hash: String,
pub signature: String,
pub public_key: String,
pub timestamp: u64,
pub algorithm: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModulePermissions {
pub filesystem_access: bool,
pub network_access: bool,
pub process_spawn: bool,
pub env_access: bool,
pub system_access: bool,
pub memory_limit_mb: u64,
pub cpu_limit_percent: u8,
pub timeout_seconds: u64,
}
impl Default for ModulePermissions {
fn default() -> Self {
Self {
filesystem_access: false,
network_access: false,
process_spawn: false,
env_access: false,
system_access: false,
memory_limit_mb: DEFAULT_MEMORY_LIMIT_MB,
cpu_limit_percent: DEFAULT_CPU_LIMIT_PERCENT,
timeout_seconds: DEFAULT_TIMEOUT_SECONDS,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CodeReviewStatus {
Pending,
InProgress,
Approved { reviewer: String, timestamp: u64 },
Rejected { reviewer: String, reason: String, timestamp: u64 },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SupplyChainInfo {
pub source_url: String,
pub commit_hash: String,
pub build_timestamp: u64,
pub dependencies: HashMap<String, String>,
pub build_environment: String,
pub verifier_signature: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SandboxConfig {
pub enabled: bool,
pub filesystem_isolation: bool,
pub network_isolation: bool,
pub process_isolation: bool,
pub read_only_fs: bool,
pub allowed_paths: Vec<String>,
pub denied_paths: Vec<String>,
}
impl Default for SandboxConfig {
fn default() -> Self {
Self {
enabled: true,
filesystem_isolation: true,
network_isolation: true,
process_isolation: true,
read_only_fs: true,
allowed_paths: Vec::new(),
denied_paths: DEFAULT_DENIED_PATHS.iter().map(|s| s.to_string()).collect(),
}
}
}
#[derive(Debug, Clone)]
pub struct ModuleMetadata {
pub name: String,
pub module_type: String,
pub instantiate_fn_name: String,
pub module_path: String,
pub struct_name: String,
pub signature: Option<ModuleSignature>,
pub permissions: ModulePermissions,
pub review_status: CodeReviewStatus,
pub supply_chain: Option<SupplyChainInfo>,
pub sandbox_config: SandboxConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityReport {
pub name: String,
pub has_signature: bool,
pub signature_verified: bool,
pub is_approved: bool,
pub has_supply_chain: bool,
pub supply_chain_verified: bool,
pub permissions: ModulePermissions,
pub sandbox_enabled: bool,
}
pub type ModuleFactory = fn() -> Result<Box<dyn Any + Send + Sync>, anyhow::Error>;
pub struct ModuleRegistration {
pub name: &'static str,
pub module_type: &'static str,
pub instantiate_fn_name: &'static str,
pub module_path: &'static str,
pub struct_name: &'static str,
pub factory: ModuleFactory,
}
impl ModuleMetadata {
pub fn new(
name: String,
module_type: String,
instantiate_fn_name: String,
module_path: String,
struct_name: String,
) -> Self {
Self {
name,
module_type,
instantiate_fn_name,
module_path,
struct_name,
signature: None,
permissions: ModulePermissions::default(),
review_status: CodeReviewStatus::Pending,
supply_chain: None,
sandbox_config: SandboxConfig::default(),
}
}
pub fn secure(
name: String,
module_type: String,
instantiate_fn_name: String,
module_path: String,
struct_name: String,
signature: Option<ModuleSignature>,
permissions: ModulePermissions,
supply_chain: Option<SupplyChainInfo>,
) -> Self {
Self {
name,
module_type,
instantiate_fn_name,
module_path,
struct_name,
signature,
permissions,
review_status: CodeReviewStatus::Pending,
supply_chain,
sandbox_config: SandboxConfig::default(),
}
}
pub fn has_valid_signature(&self) -> bool {
self.signature.is_some()
}
pub fn is_approved(&self) -> bool {
matches!(self.review_status, CodeReviewStatus::Approved { .. })
}
pub fn has_supply_chain(&self) -> bool {
self.supply_chain.is_some()
}
pub fn summary(&self) -> String {
format!(
"Module: {} (type: {}) - signature: {}, approved: {}, supply_chain: {}, sandbox: {}",
self.name,
self.module_type,
self.has_valid_signature(),
self.is_approved(),
self.has_supply_chain(),
self.sandbox_config.enabled
)
}
}