1use serde::{Deserialize, Serialize};
4use std::any::Any;
5use std::collections::HashMap;
6use anyhow::Result;
7
8use crate::constants::*;
9
10pub trait Module: Send + Sync {
12 fn name(&self) -> &str;
14
15 fn module_type(&self) -> &str;
17}
18
19#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct ModuleSignature {
22 pub code_hash: String,
24 pub signature: String,
26 pub public_key: String,
28 pub timestamp: u64,
30 pub algorithm: String,
32}
33
34#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct ModulePermissions {
37 pub filesystem_access: bool,
39 pub network_access: bool,
41 pub process_spawn: bool,
43 pub env_access: bool,
45 pub system_access: bool,
47 pub memory_limit_mb: u64,
49 pub cpu_limit_percent: u8,
51 pub timeout_seconds: u64,
53}
54
55impl Default for ModulePermissions {
56 fn default() -> Self {
57 Self {
58 filesystem_access: false,
59 network_access: false,
60 process_spawn: false,
61 env_access: false,
62 system_access: false,
63 memory_limit_mb: DEFAULT_MEMORY_LIMIT_MB,
64 cpu_limit_percent: DEFAULT_CPU_LIMIT_PERCENT,
65 timeout_seconds: DEFAULT_TIMEOUT_SECONDS,
66 }
67 }
68}
69
70#[derive(Debug, Clone, Serialize, Deserialize)]
72pub enum CodeReviewStatus {
73 Pending,
75 InProgress,
77 Approved { reviewer: String, timestamp: u64 },
79 Rejected { reviewer: String, reason: String, timestamp: u64 },
81}
82
83#[derive(Debug, Clone, Serialize, Deserialize)]
85pub struct SupplyChainInfo {
86 pub source_url: String,
88 pub commit_hash: String,
90 pub build_timestamp: u64,
92 pub dependencies: HashMap<String, String>,
94 pub build_environment: String,
96 pub verifier_signature: Option<String>,
98}
99
100#[derive(Debug, Clone, Serialize, Deserialize)]
102pub struct SandboxConfig {
103 pub enabled: bool,
105 pub filesystem_isolation: bool,
107 pub network_isolation: bool,
109 pub process_isolation: bool,
111 pub read_only_fs: bool,
113 pub allowed_paths: Vec<String>,
115 pub denied_paths: Vec<String>,
117}
118
119impl Default for SandboxConfig {
120 fn default() -> Self {
121 Self {
122 enabled: true,
123 filesystem_isolation: true,
124 network_isolation: true,
125 process_isolation: true,
126 read_only_fs: true,
127 allowed_paths: Vec::new(),
128 denied_paths: DEFAULT_DENIED_PATHS.iter().map(|s| s.to_string()).collect(),
129 }
130 }
131}
132
133#[derive(Debug, Clone)]
135pub struct ModuleMetadata {
136 pub name: String,
137 pub module_type: String,
138 pub instantiate_fn_name: String,
139 pub module_path: String,
140 pub struct_name: String,
141 pub signature: Option<ModuleSignature>,
143 pub permissions: ModulePermissions,
145 pub review_status: CodeReviewStatus,
147 pub supply_chain: Option<SupplyChainInfo>,
149 pub sandbox_config: SandboxConfig,
151}
152
153#[derive(Debug, Clone, Serialize, Deserialize)]
155pub struct SecurityReport {
156 pub name: String,
157 pub has_signature: bool,
158 pub signature_verified: bool,
159 pub is_approved: bool,
160 pub has_supply_chain: bool,
161 pub supply_chain_verified: bool,
162 pub permissions: ModulePermissions,
163 pub sandbox_enabled: bool,
164}
165
166pub type ModuleFactory = fn() -> Result<Box<dyn Any + Send + Sync>, anyhow::Error>;
169
170pub struct ModuleRegistration {
172 pub name: &'static str,
173 pub module_type: &'static str,
174 pub instantiate_fn_name: &'static str,
175 pub module_path: &'static str,
176 pub struct_name: &'static str,
177 pub factory: ModuleFactory,
178}
179
180impl ModuleMetadata {
181 pub fn new(
183 name: String,
184 module_type: String,
185 instantiate_fn_name: String,
186 module_path: String,
187 struct_name: String,
188 ) -> Self {
189 Self {
190 name,
191 module_type,
192 instantiate_fn_name,
193 module_path,
194 struct_name,
195 signature: None,
196 permissions: ModulePermissions::default(),
197 review_status: CodeReviewStatus::Pending,
198 supply_chain: None,
199 sandbox_config: SandboxConfig::default(),
200 }
201 }
202
203 pub fn secure(
205 name: String,
206 module_type: String,
207 instantiate_fn_name: String,
208 module_path: String,
209 struct_name: String,
210 signature: Option<ModuleSignature>,
211 permissions: ModulePermissions,
212 supply_chain: Option<SupplyChainInfo>,
213 ) -> Self {
214 Self {
215 name,
216 module_type,
217 instantiate_fn_name,
218 module_path,
219 struct_name,
220 signature,
221 permissions,
222 review_status: CodeReviewStatus::Pending,
223 supply_chain,
224 sandbox_config: SandboxConfig::default(),
225 }
226 }
227
228 pub fn has_valid_signature(&self) -> bool {
230 self.signature.is_some()
231 }
232
233 pub fn is_approved(&self) -> bool {
235 matches!(self.review_status, CodeReviewStatus::Approved { .. })
236 }
237
238 pub fn has_supply_chain(&self) -> bool {
240 self.supply_chain.is_some()
241 }
242
243 pub fn summary(&self) -> String {
245 format!(
246 "Module: {} (type: {}) - signature: {}, approved: {}, supply_chain: {}, sandbox: {}",
247 self.name,
248 self.module_type,
249 self.has_valid_signature(),
250 self.is_approved(),
251 self.has_supply_chain(),
252 self.sandbox_config.enabled
253 )
254 }
255}