Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core/blob/main/crates/aien-cli/src/vault.rs # aien-sovereign-core Autonomous sovereign intelligence engine and runtime core for NVIDIA DGX Spark Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core / crates / aien-cli / src / vault.rs` 213 lines ยท 6649 bytes Raw Blame History 1 use std::path::Path; 2 use std::process::Command; 3 use std::sync::{LazyLock, Mutex, RwLock}; 4 use regex::Regex; 5 use serde_json::{json, Value}; 6 7 const VAULT_BIN: &str = "/home/drakestapleton/.local/bin/atlas-vault"; 8 9 static VAULT_SECRET_CACHE: LazyLock>>> = LazyLock::new(|| RwLock::new(None)); 10 static VAULT_MUTEX: LazyLock> = LazyLock::new(|| Mutex::new(())); 11 12 pub fn is_vault_available() -> bool { 13 Path::new(VAULT_BIN).is_file() 14 } 15 16 pub fn list_keys() -> Vec { 17 if !is_vault_available() { 18 return Vec::new(); 19 } 20 let output = Command::new(VAULT_BIN) 21 .arg("list") 22 .output(); 23 24 match output { 25 Ok(out) if out.status.success() => { 26 let stdout = String::from_utf8_lossy(&out.stdout); 27 stdout 28 .lines() 29 .map(|l| l.trim().to_string()) 30 .filter(|l| !l.is_empty()) 31 .collect() 32 } 33 _ => Vec::new(), 34 } 35 } 36 37 pub fn has_key(key: &str) -> bool { 38 let keys = list_keys(); 39 keys.iter().any(|k| k == key) 40 } 41 42 pub fn get_secret(key: &str) -> Result { 43 if !is_vault_available() { 44 return Err("atlas-vault binary not found at /home/drakestapleton/.local/bin/atlas-vault".to_string()); 45 } 46 let output = Command::new(VAULT_BIN) 47 .arg("get") 48 .arg(key) 49 .output() 50 .map_err(|e| format!("Failed to execute atlas-vault: {}", e))?; 51 52 if !output.status.success() { 53 let stderr = String::from_utf8_lossy(&output.stderr); 54 return Err(format!("atlas-vault get failed: {}", stderr.trim())); 55 } 56 57 let secret = String::from_utf8_lossy(&output.stdout).trim().to_string(); 58 if secret.is_empty() { 59 return Err(format!("Key {} returned empty value", key)); 60 } 61 Ok(secret) 62 } 63 64 pub fn get_cached_vault_secrets() -> Vec<(String, String)> { 65 if cfg!(test) { 66 return Vec::new(); 67 } 68 69 if let Ok(read) = VAULT_SECRET_CACHE.read() { 70 if let Some(ref cache) = *read { 71 return cache.clone(); 72 } 73 } 74 75 let _guard = match VAULT_MUTEX.lock() { 76 Ok(g) => g, 77 Err(poisoned) => poisoned.into_inner(), 78 }; 79 80 if let Ok(read) = VAULT_SECRET_CACHE.read() { 81 if let Some(ref cache) = *read { 82 return cache.clone(); 83 } 84 } 85 86 let keys = list_keys(); 87 let mut entries = Vec::new(); 88 for key in keys { 89 if let Ok(val) = get_secret(&key) { 90 if val.len() >= 6 { 91 entries.push((key, val)); 92 } 93 } 94 } 95 96 if let Ok(mut write) = VAULT_SECRET_CACHE.write() { 97 *write = Some(entries.clone()); 98 } 99 entries 100 } 101 102 /// Scrub text to ensure zero secret values or raw token patterns are printed. 103 pub fn redact_secrets(input: &str) -> String { 104 let mut result = input.to_string(); 105 106 // 1. Scrub actual values of known vault keys from memory cache 107 let secrets = get_cached_vault_secrets(); 108 for (key, val) in &secrets { 109 result = result.replace(val, &format!("[REDACTED_BY_ATLAS_VAULT:{}]", key)); 110 } 111 112 // 2. Scrub standard API key patterns 113 let patterns = [ 114 (r"\b(sk-ant-[a-zA-Z0-9_-]{20,})\b", "[REDACTED_ANTHROPIC_KEY]"), 115 (r"\b(ghp_[a-zA-Z0-9]{20,})\b", "[REDACTED_GITHUB_TOKEN]"), 116 (r"\b(AIzaSy[a-zA-Z0-9_-]{33})\b", "[REDACTED_GOOGLE_KEY]"), 117 (r"\b(sk-[a-zA-Z0-9_-]{20,})\b", "[REDACTED_OPENAI_KEY]"), 118 ]; 119 120 for (pat, repl) in patterns { 121 if let Ok(re) = Regex::new(pat) { 122 result = re.replace_all(&result, repl).to_string(); 123 } 124 } 125 126 result 127 } 128 129 /// Audit workspace directories for stray plaintext secret files (.env, etc.) 130 pub fn audit_workspace_secrets() -> Vec { 131 let scan_dirs = [ 132 "/home/drakestapleton/workspace", 133 "/home/drakestapleton/basecamp", 134 "/home/drakestapleton/spark-neural-os", 135 "/home/drakestapleton/aien-cli", 136 ]; 137 138 let mut findings = Vec::new(); 139 for d in scan_dirs { 140 if !Path::new(d).exists() { 141 continue; 142 } 143 let output = Command::new("find") 144 .args([d, "-maxdepth", "4", "-name", ".env*", "-not", "-path", "*/.git/*", "-not", "-path", "*/target/*"]) 145 .output(); 146 147 if let Ok(out) = output { 148 let stdout = String::from_utf8_lossy(&out.stdout); 149 for line in stdout.lines() { 150 let trimmed = line.trim(); 151 if !trimmed.is_empty() { 152 findings.push(trimmed.to_string()); 153 } 154 } 155 } 156 } 157 findings 158 } 159 160 pub fn vault_dispatch_tool(args: &Value) -> Value { 161 let action = args.get("action").and_then(Value::as_str).unwrap_or("list"); 162 match action { 163 "list" => { 164 let keys = list_keys(); 165 json!({ 166 "status": "ok", 167 "vault": "TPM-bound atlas-vault", 168 "registered_keys": keys, 169 "note": "Secret values reside in hardware TPM. Values are never written to disk or echoed in transcripts." 170 }) 171 }, 172 "check" => { 173 let key = args.get("key").and_then(Value::as_str).unwrap_or(""); 174 if key.is_empty() { 175 return json!({"error": "Missing key argument"}); 176 } 177 let exists = has_key(key); 178 json!({ 179 "status": "ok", 180 "key": key, 181 "exists": exists, 182 "storage": "TPM-bound atlas-vault" 183 }) 184 }, 185 "audit" => { 186 let findings = audit_workspace_secrets(); 187 json!({ 188 "status": "ok", 189 "stray_env_files_detected": findings.len(), 190 "findings": findings, 191 "policy": "Zero .env files on disk. Plaintext secrets strictly forbidden." 192 }) 193 }, 194 other => json!({ 195 "error": format!("Unknown vault action {}. Supported actions: list, check, audit. Direct value retrieval is restricted to in-memory runtime execution.", other) 196 }) 197 } 198 } 199 200 #[cfg(test)] 201 mod tests { 202 use super::*; 203 204 #[test] 205 fn test_secret_redaction_patterns() { 206 let sample = "Error with key sk-proj-`1234567890abcdef123456` and token ghp_abcdefghijklmnopqrstuvwxyz12"; 207 let redacted = redact_secrets(sample); 208 assert!(!redacted.contains("sk-proj-`1234567890abcdef123456`")); 209 assert!(!redacted.contains("ghp_abcdefghijklmnopqrstuvwxyz12")); 210 assert!(redacted.contains("[REDACTED_OPENAI_KEY]")); 211 assert!(redacted.contains("[REDACTED_GITHUB_TOKEN]")); 212 } 213 }