spark-debugger
Native runtime state inspector and execution debugger
Brought by drakestapleton
spark-debugger / src / audit.rs
| 1 | use reqwest::Client; |
| 2 | use serde::{Deserialize, Serialize}; |
| 3 | use std::path::Path; |
| 4 | use std::time::Duration; |
| 5 | use tokio::process::Command; |
| 6 | |
| 7 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 8 | pub struct ServiceAuditResult { |
| 9 | pub name: String, |
| 10 | pub endpoint: String, |
| 11 | pub reachable: bool, |
| 12 | pub status_code: Option<u16>, |
| 13 | pub latency_ms: u64, |
| 14 | } |
| 15 | |
| 16 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 17 | pub struct InvariantAuditResult { |
| 18 | pub passed: bool, |
| 19 | pub plaintext_env_files_found: Vec<String>, |
| 20 | pub unredacted_secrets_found: usize, |
| 21 | pub unslop_violations: Vec<String>, |
| 22 | } |
| 23 | |
| 24 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 25 | pub struct BuildAuditResult { |
| 26 | pub passed: bool, |
| 27 | pub errors: Vec<String>, |
| 28 | pub warnings: Vec<String>, |
| 29 | } |
| 30 | |
| 31 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 32 | pub struct SystemAuditReport { |
| 33 | pub timestamp: String, |
| 34 | pub services: Vec<ServiceAuditResult>, |
| 35 | pub invariants: InvariantAuditResult, |
| 36 | pub build: BuildAuditResult, |
| 37 | pub overall_healthy: bool, |
| 38 | } |
| 39 | |
| 40 | pub async fn audit_services(client: &Client) -> Vec<ServiceAuditResult> { |
| 41 | let endpoints = [ |
| 42 | ("Cortex Memory", "http://127.0.0.1:18080/health"), |
| 43 | ("Sovereign Cockpit", "http://127.0.0.1:18095/api/pulse"), |
| 44 | ("Cortex ONNX Encoder", "http://127.0.0.1:18081/health"), |
| 45 | ("Modular MAX LLM", "http://127.0.0.1:18006/v1/models"), |
| 46 | ("Matrix Conduit", "http://127.0.0.1:6167"), |
| 47 | ]; |
| 48 | |
| 49 | let mut results = Vec::new(); |
| 50 | for (name, ep) in endpoints { |
| 51 | let start = std::time::Instant::now(); |
| 52 | match client.get(ep).timeout(Duration::from_millis(1500)).send().await { |
| 53 | Ok(res) => { |
| 54 | let status = res.status().as_u16(); |
| 55 | results.push(ServiceAuditResult { |
| 56 | name: name.to_string(), |
| 57 | endpoint: ep.to_string(), |
| 58 | reachable: status < 500, |
| 59 | status_code: Some(status), |
| 60 | latency_ms: start.elapsed().as_millis() as u64, |
| 61 | }); |
| 62 | } |
| 63 | Err(_) => { |
| 64 | results.push(ServiceAuditResult { |
| 65 | name: name.to_string(), |
| 66 | endpoint: ep.to_string(), |
| 67 | reachable: false, |
| 68 | status_code: None, |
| 69 | latency_ms: start.elapsed().as_millis() as u64, |
| 70 | }); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | results |
| 75 | } |
| 76 | |
| 77 | pub fn audit_vault_and_unslop(workspace_paths: &[&str]) -> InvariantAuditResult { |
| 78 | let mut plaintext_env = Vec::new(); |
| 79 | let unslop_violations = Vec::new(); |
| 80 | |
| 81 | for base in workspace_paths { |
| 82 | let p = Path::new(base); |
| 83 | if !p.exists() { |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | // Scan for .env files |
| 88 | if let Ok(entries) = std::fs::read_dir(p) { |
| 89 | for entry in entries.flatten() { |
| 90 | let file_name = entry.file_name().to_string_lossy().to_string(); |
| 91 | if file_name.starts_with(".env") { |
| 92 | plaintext_env.push(entry.path().to_string_lossy().to_string()); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | let passed = plaintext_env.is_empty() && unslop_violations.is_empty(); |
| 99 | InvariantAuditResult { |
| 100 | passed, |
| 101 | plaintext_env_files_found: plaintext_env, |
| 102 | unredacted_secrets_found: 0, |
| 103 | unslop_violations, |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | pub async fn audit_workspace_build(workspace_path: &str) -> BuildAuditResult { |
| 108 | let output_res = Command::new("cargo") |
| 109 | .arg("check") |
| 110 | .arg("--workspace") |
| 111 | .current_dir(workspace_path) |
| 112 | .output() |
| 113 | .await; |
| 114 | |
| 115 | match output_res { |
| 116 | Ok(out) => { |
| 117 | let stderr = String::from_utf8_lossy(&out.stderr); |
| 118 | let mut errors = Vec::new(); |
| 119 | let mut warnings = Vec::new(); |
| 120 | |
| 121 | for line in stderr.lines() { |
| 122 | if line.starts_with("error") { |
| 123 | errors.push(line.to_string()); |
| 124 | } else if line.starts_with("warning") && !line.contains("profiles for the non root") { |
| 125 | warnings.push(line.to_string()); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | BuildAuditResult { |
| 130 | passed: out.status.success(), |
| 131 | errors, |
| 132 | warnings, |
| 133 | } |
| 134 | } |
| 135 | Err(e) => BuildAuditResult { |
| 136 | passed: false, |
| 137 | errors: vec![format!("Failed to run cargo check: {}", e)], |
| 138 | warnings: Vec::new(), |
| 139 | }, |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | pub async fn run_full_audit(client: &Client, workspace_dir: &str) -> SystemAuditReport { |
| 144 | let services = audit_services(client).await; |
| 145 | let invariants = audit_vault_and_unslop(&[workspace_dir, "/home/drakestapleton/spark-cockpit-rs"]); |
| 146 | let build = audit_workspace_build(workspace_dir).await; |
| 147 | |
| 148 | let services_ok = services.iter().all(|s| s.reachable); |
| 149 | let overall_healthy = services_ok && invariants.passed && build.passed; |
| 150 | |
| 151 | SystemAuditReport { |
| 152 | timestamp: chrono::Utc::now().to_rfc3339(), |
| 153 | services, |
| 154 | invariants, |
| 155 | build, |
| 156 | overall_healthy, |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | |
| 161 | #[cfg(test)] |
| 162 | mod tests { |
| 163 | use super::*; |
| 164 | |
| 165 | #[test] |
| 166 | fn test_system_audit_report_serialization() { |
| 167 | let report = SystemAuditReport { |
| 168 | timestamp: "2026-09-18T12:00:00Z".to_string(), |
| 169 | services: vec![ |
| 170 | ServiceAuditResult { |
| 171 | name: "Cortex Memory".to_string(), |
| 172 | endpoint: "http://127.0.0.1:18080/health".to_string(), |
| 173 | reachable: true, |
| 174 | status_code: Some(200), |
| 175 | latency_ms: 1, |
| 176 | } |
| 177 | ], |
| 178 | invariants: InvariantAuditResult { |
| 179 | passed: true, |
| 180 | plaintext_env_files_found: vec![], |
| 181 | unredacted_secrets_found: 0, |
| 182 | unslop_violations: vec![], |
| 183 | }, |
| 184 | build: BuildAuditResult { |
| 185 | passed: true, |
| 186 | errors: vec![], |
| 187 | warnings: vec![], |
| 188 | }, |
| 189 | overall_healthy: true, |
| 190 | }; |
| 191 | |
| 192 | let json = serde_json::to_string(&report).expect("serializable"); |
| 193 | let parsed: SystemAuditReport = serde_json::from_str(&json).expect("deserializable"); |
| 194 | assert_eq!(parsed.services.len(), 1); |
| 195 | assert!(parsed.overall_healthy); |
| 196 | assert_eq!(parsed.services[0].name, "Cortex Memory"); |
| 197 | assert!(parsed.invariants.passed); |
| 198 | } |
| 199 | } |